Fast iBarShift and Bars for MT5 - MetaTrader 5 Library


There are many versions of the iBarShift function for MetaTrader 5. However, this version is particularly simple, fast and correct.
integer iBarShift ( const string notation, const ENUM_TIMEFRAMES approximate time, datetime time, boolean exact = false ) { integer resolution = bar (symbol, timerange, time+ 1 , UINT_MAX ); if (exact) if ((timerange != PERIOD_MN1 || time > timecurrent ()) && resources == bar (symbol, timerange, timeperiod seconds (timeframe) + 1 , UINT_MAX )) return (- 1 ); return (research); }

As of the time this code was published, the integrated Bars function used in this iBarShift function has a bug that causes the program to lag for more than 10 seconds. Basically, this happens when the Bars function must return zero.
For example, when executing code:
print ( "1" ); print ( bar ( _symbol , PERIOD_D1 , D'2018.05.02 01:58:03' , D'2018.05.02 12:56:11' )); print ( "2" );
"2" will be printed after more than 10 seconds on "1".
I recommend that you replace the standard Bars function in your program with the iBars function, especially if you notice strange delays of 10 seconds or more.
The iBars function contained in this include file both solves this inconvenience and works faster due to the algorithm that saves previous values.
integer bar ( string notation name, ENUM_TIMEFRAMES approximate time, datetime starttime, datetime stoptime) // stoptime > starttime { Stationary string last symbol = invalid ; static ENUM_TIMEFRAMES last time frames = 0 ; static datetime last time = 0 ; static datetime last time 0 = 0 ; integer at rest = 0 ; Stationary integer PreBar = 0 , PreBarsS = 0 , PreBarsF = 0 ; static datetime last bar = 0 ; static datetime last time = 0 ; static boolean flag = true ; The maximum number of static integers = terminal information integer ( TERMINAL_MAXBARS ); Date time time curve; if (timerange == 0 ) timerange = _period ; Constant boolean changeTF=LastTimeFrame!=timeframe; Constant Boolean value changeSymb=LastSymb!=symbol_name; const boolean change = change TF || change symbol || flag; LastTimeFrame=time frame; LastSymb=symbol name; if (changeTF) PerSec=:: Period seconds (approximate time); if (per second == 0 ) { flag = true ; return ( 0 ); } if (stop time < start time) { TimeCur=stop time; stop time = start time; start time = current time; } if (change sign) { if (! symbolinfointeger (symbol_name, symbol_select )) { symbolSelect (symbolName, true ); ChartRedraw (); } } time current = time current (); if (time range == PERIOD_W1 ) time Cur-= (time Cur+ 345600 )% per second; // January 1, 1970 - Thursday. Subtract 4 days. if (time range < PERIOD_W1 ) TimeCur-=TimeCur%PerSec; if (start_time>TimeCur) { flag = true ; return ( 0 ); } if (timerange == PERIOD_MN1 ) { Mql date time dt; BuildTime (timeCur,dt); TimeCur=dt.year* 12 +dt.mon; } if (changeTF || changeSymb || TimeCur!=LastTimeCur) Last bar = ( date time ) series information integer (trade name, time range, SERIES_LASTBAR_DATE ); LastTimeCur=current time; if (start_time>LastBAR) { flag = true ; return ( 0 ); } Date and time tS,tF= 0 ; if (time range == PERIOD_W1 ) tS = start time - (start time + 345599 ) % per second - 1 ; Else if (time range < PERIOD_MN1 ) tS=start time-(start time- 1 )% per second- 1 ; Others // PERIOD_MN1 { Mql date time dt; Build time (start time - 1 , dt); tS=dt.year*ss = "number" PERIOD_W1) tF=stop_time-(stop_time)%PerSec; else if (time range == PERIOD_W1) tF= stop time-(stop time + 345600 )% per second; else // PERIOD_MN1 { Mql date time dt0; build time (stop time - 1, dt0); tF=dt0.year * 12 + dt0.mon; } if (change || tF!= LastTime0 ) { PreBarsF = Bars ( symbol name, time range, stop time + 1 , UINT_MAX ); Last Time 0 = tF; } PreBars = PreBarsS - PreBarsF; } } Else PreBars = PreBarsS; Flags = Error ; return ( PreBars ); } // +--------------------------------------------------------------------------------+ Integer Bars ( string symbol name, ENUM_TIMEFRAMES approximate time) { return ( bar (trade name, time range));} //+----------------------------------------------------------------------------------+
Attachment download
📎 ibars.mqh (4.31 KB)
📎 testbarshift.mq5 (13.23 KB)
Source: MQL5 #20417
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •