Hourly buffer (binary) and 0-23 hourly buffer - for data collection purposes - MetaTrader 5 script


Users can collect data for modeling. Time dummies are often used in economic modeling. The simple indicator below can be easily extended to be provided hourly as a vector of binary buffer arrays. The final buffer array vector stores the hours themselves.
For example, if you collect data for other metrics into a CSV, for example using the CopyBuffer function, this metric allows you to collect a virtual column for that hour as additional data.
Browse the code
# Property indicator chart window # Property indicator buffer 25 #Property Indicator Chart 25
Buffer label
#property- indicator tag 1 "00 points" #property- indicator tag 2 "01 points" #propertyindicatortag3 " 02 points" #property- indicators tag 4 “03 points” #property- indicators tag 5 “04 points” #property- indicators tag 6 “05 points” #property- indicators tag 7 “06 points” #property- indicators tag 8 “07 points” #property- indicators tag 9 “08 points” #property- indicators tag 10 "09 points" #propertyindextag11 " 10 points" #property- indicator tag 12 "11 o'clock" #property- indicator tag 13 "12 o'clock" #propertyindextag14 " 13 points" #propertyindextag15 " 14 points" #propertyindextag16 " 15 points" #propertyindextag17 "16 points" #propertyindextag18 "17 points" #propertyindextag 19 "18 points" #propertyindextag20 "19 points" #propertyindextag21 "20 points" #propertyindextag22 "Blackjack" #propertyindextag23 "22 points" #propertyindextag24 "23 hours" #property- indicator tag 25 "hours"
buffer declaration
Double hourBuffer0[]; Double hourBuffer1[]; Double hourBuffer2[]; Double hourBuffer3[]; Double hourBuffer4 []; Double hourBuffer5[]; Double hourB uffer6[]; double hourBuffer7[]; double hourBuffer8[]; double hourBuffer9[]; double hourBuffer10[]; double hourBuffer11[]; double hourBuffer1 2[]; double hourBuffer13[]; double hourBuffer14[]; double hourBuffer15[]; double hourBuffer16[]; double hourBuffer17[]; double hourBuffer18[ ]; double hourBuffer19[]; double hourBuffer20[]; double hourBuffer21[]; double hourBuffer22[]; double hourBuffer23[]; double hourBuffer[] ;integer bar hours;
Indexing and plotting
Set the index of all buffers to data and use a loop to turn off the plot (trying to use a loop for indexing gives you an error, you can't pass eg hourBuffer[q] via SetIndexBuffer, so do this one by one; but the loop seems to work with PlotIndexSetInteger, which helps).
// Assign buffer to index, hide from chart, show in data window Set index buffer ( 0 , hourBuffer0, INDICATOR_DATA ); Set index buffer ( 1 , hourBuffer1, INDICATOR_DATA ); Set index buffer ( 2 , hourBuffer2, INDICATOR_DATA ); Set index buffer ( 3 , hourBuffer3, INDICATOR_DATA ); Set index buffer ( 4 , hourBuffer4, INDICATOR_DATA ); Set index buffer ( 5 , hourBuffer5, INDICATOR_DATA ); Set index buffer ( 6 , hourBuffer6, INDICATOR_DATA ); Set index buffer ( 7 , hourBuffer7, INDICATOR_DATA ); Set index buffer ( 8 , hourBuffer8, INDICATOR_DATA ); Set index buffer ( 9 , hourBuffer9, INDICATOR_DATA ); Set index buffer ( 10 , hour buffer 10, INDICATOR_DATA ); Set index buffer ( 11 , hourBuffer11, INDICATOR_DATA ); Set index buffer ( 12 , hourBuffer12, INDICATOR_DATA ); Set index buffer ( 13 , hourBuffer13, INDICATOR_DATA ); Set index buffer ( 14 , hourBuffer14, INDICATOR_DATA ); Set index buffer ( 15 , hourBuffer15, INDICATOR_DATA ); Set index buffer ( 16 , hourBuffer16, INDICATOR_DATA ); Set index buffer ( No. 17 , hourBuffer17, INDICATOR_DATA ); Set index buffer ( 18 , hourBuffer18, INDICATOR_DATA ); Set index buffer ( 19 , hourBuffer19, INDICATOR_DATA ); Set index buffer ( 20 , hour buffer 20, INDICATOR_DATA ); Set index buffer ( 21 , hourBuffer21, INDICATOR_DATA ); Set index buffer ( 22 , hourBuffer22, INDICATOR_DATA ); Set index buffer ( 23 , hourBuffer23, INDICATOR_DATA ); Set index buffer ( 24 , hour buffer, INDICATOR_DATA ); for ( integer i = 0 ; i < 24 ; i++) { DrawIndexSetInteger (i, DrawType , DRAW_NONE ); plotIndexSetInteger (i, plotDisplayData , true ); } Return ( initialization successful ); }
OnCalculate function loops and procedures
if (rate_total <= 0 ) return ( 0 ); // We will start by recalculating the integer from the first unprocessed bar = (previous calculation > 0 ? previously calculated - 1 : 0 ); for ( integer i = start; i// Calculate the hour of bar i (0..23) bar hour = ( integer )((time[i]% 86400 )/ 3600 ); // 1) Set all 24 buffers of item i to 0 hourBuffer0[i] = 0.0 ; hourBuffer1[i] = 0.0 ; hourBuffer2[i] = 0.0 ; hourBuffer3[i] = 0.0 ; hourBuffer4[i] = 0.0 ; hourBuffer5[i] = 0.0 ; hourBuffer6[i] = 0.0 ; hourBuffer7[i] = 0.0 ; hourBuffer8[i] = 0.0 ; hourBuffer9[i] = 0.0 ; hourBuffer10[i] = 0.0 ; hourBuffer11[i] = 0.0 ; hourBuffer12[i] = 0.0 ; hourBuffer13[i] = 0.0 ; hourBuffer14[i] = 0.0 ; hourBuffer15[i] = 0.0 ; hourBuffer16[i] = 0.0 ; hourBuffer17[i] = 0.0 ; hourBuffer18[i] = 0.0 ; hourBuffer19[i] = 0.0 ; hourBuffer20[i] = 0.0 ; hourBuffer21[i] = 0.0 ; hourBuffer22[i] = 0.0 ; hourBuffer23[i] = 0.0 ; hour buffer[i] = empty_VALUE ; // 2) Now only set the match buffer to 1 transition (bar hours) { Case 0 : hourBuffer0[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 1 : hourBuffer1[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 2 : hourBuffer2[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 3 : hourBuffer3[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 4 : hourBuffer4[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 5 : hourBuffer5[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 6 : hourBuffer6[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 7 : hourBuffer7[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 8 : hourBuffer8[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 9 : hourBuffer9[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 10 : hourBuffer10[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; Case 11 : hourBuffer11[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; case>12: hourBuffer12[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case 13 : hourBuffer13[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case 14 : hourBuffer14[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case 15 : hourBuffer15[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case 16 : hourBuffer16[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case 17 : hourBuffer17[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case 18 : hourBuffer18[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case 19 : hourBuffer19[i] = 1.0 ; hourBuffer[ i ] = bar_hour; break ; case20 : hourBuffer20[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case21 : hourBuffer21[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case22 : hourBuffer22[i] = 1.0 ; hourBuffer[i] = bar_hour; break ; case23 : hourBuffer23[i] = 1.0 ; hourBuffer[i] = bar_hour; rest ; } string localHourText = HourToText(bar_hour); comment ( "Time is:" , local time text); } // Return the number of bars processed return (rate_total);
Features that add flavor to comments
string hour to text( integer b) { string text time; transform (bh) { Case 0 : text time = "12 AM" ; // midnight time break in 12 hour format ; Case 1 : text time = "1 am" ; rest ; Case 2 : text time = "2 am" ; rest ; Case 3 : text time = "3 am" ; rest ; Case 4 : text time = "4 am" ; rest ; Case 5 : text time = "5am" ; rest ; Case 6 : text time = "6 am" ; rest ; Case 7 : text time = "7 a.m." ; rest ; Case 8 : text time = "8 AM" ; rest ; Case 9 : text time = "9 AM" ; rest ; Case 10 : text time = "10 am" ; rest ; Case 11 : text time = "11 am" ; rest ; Case 12 : Text time = "12 noon" ; // Noon time break in 12 hour format ; Case 13 : text time = "1pm" ; rest ; Case 14 : text time = "2pm" ; rest ; Case 15 : text time = "3pm" ; rest ; Case 16 : text time = "4pm" ; rest ; Case No. 17 : text time = "5pm" ; rest ; Case 18 : text time = "6pm" ; rest ; Case 19 : text time = "7pm" ; rest ; Case 20 : text time = "8pm" ; rest ; Case 21 : text time = "9pm" ; rest ; Case 22 : text time = "10pm" ; rest ; Case 23 : text time = "11pm" ; rest ; default : // Just in case 'bh' is out of range (0..23) text time = "unknown" ; rest ; } return text time;
Note that this is currently only displayed via the data window.
I wish you all a Merry Christmas and a Happy New Year 2025.
Attachment download
📎thehour_screenshot.png (78.75 KB)
📎thehour2.mq5 (9.33 KB)
Source: MQL5 #54446
Hourly Buffer (Binary) and 0-23 Hourly Buffer - For Data Collection Purposes - Important Points Before Using MetaTrader 5 Scripts
This page has been supplemented with structured instructions, focusing on the purpose of trading scripts, MT4/MT5 platform compatibility, testing procedures and risk control. Before downloading or deploying, it is recommended to first determine whether it is consistent with your own variety, cycle and account environment.
Suitable for who to use
- Users who want to quickly screen trading scripts and are willing to do simulation verification first.
- People who need to compare MT4/MT5 EAs, indicators, scripts or source code projects.
- Traders who wish to record backtests, parameters and risk boundaries before placing a trade.
Testing and risk control suggestions
- First verify the default parameters in the strategy tester or demo account, and then adjust the symbol, period, spread and trading period one by one.
- Scalping, grid, martin, and high-frequency strategies should focus on observing maximum drawdowns, consecutive losses, slippage, and trading frequency.
- It is not recommended to run any document directly on the real disk. At least observe the simulated disk performance for 2-4 weeks first.
FAQ
Can MT4 and MT5 be used interchangeably?
Usually cannot be used directly. EX4/MQ4 corresponds to MT4, EX5/MQ5 corresponds to MT5, and the source code needs to be compiled in the corresponding MetaEditor.
Is this resource guaranteed to be profitable?
cannot. EAs, indicators and scripts can only be used as trading tools, and the results depend on parameters, market environment, spreads, slippage and risk control.
What else can I continue to watch?
Forex EA download , foreign exchange indicator download , EA evaluation , MQL5 CodeBase .
English Search Notes
This page is also optimized for English search intent around hourly buffer (binary) and 0-23 hourly buffer - for data collection purposes - MetaTrader 5 script . Related search terms include: trading script, MT4 script, MT5 script. Test any Forex EA, Expert Advisor, MT4/MT5 indicator, script or MQL source code with historical data and a demo account before live trading.
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •