Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

MetaTrader 5 Libraries | Fast iBarShift and Bars for MT5

There are many versions of the iBarShift functions for MetaTrader 5. However, this version is especially simple, fast, and correct.

int iBarShift(const string Symb,const ENUM_TIMEFRAMES TimeFrame,datetime time,bool exact=false)    {     int Res=iBars(Symb,TimeFrame,time+1,UINT_MAX);     if(exact) if((TimeFrame!=PERIOD_MN1 || time>TimeCurrent()) && Res==iBars(Symb,TimeFrame,time-PeriodSeconds(TimeFrame)+1,UINT_MAX)) return(-1);     return(Res);    }  

MetaTrader Experts, Indicators, Scripts and Libraries

As of the publication of this code, the integrated Bars function used in this iBarShift function has a bug that manifests in the program lags for more than 10 seconds. Basically, this happens where the Bars function has to return zero.

For example, in executing the code:

   Print("1");     Print(Bars(_Symbol,PERIOD_D1,D'2018.05.02 01:58:03',D'2018.05.02 12:56:11'));     Print("2");  

"2" will be printed in more than 10 seconds upon "1".

I would recommend you to replace the standard Bars function in your programs with this iBars function, especially if you notice weird lags for 10 seconds or longer.

The iBars function contained in this include file both fixes this inconvenience and works much faster due to the algorithm of saving the preceding values.

int iBars(string symbol_name,ENUM_TIMEFRAMES  timeframe,datetime start_time,datetime stop_time) // stop_time > start_time    {     static string LastSymb=NULL;     static ENUM_TIMEFRAMES LastTimeFrame=0;     static datetime LastTime=0;     static datetime LastTime0=0;     static int PerSec=0;     static int PreBars=0,PreBarsS=0,PreBarsF=0;     static datetime LastBAR=0;     static datetime LastTimeCur=0;     static bool flag=true;     static int max_bars=TerminalInfoInteger(TERMINAL_MAXBARS);     datetime TimeCur;     if (timeframe==0) timeframe=_Period;     const bool changeTF=LastTimeFrame!=timeframe;     const bool changeSymb=LastSymb!=symbol_name;     const bool change=changeTF || changeSymb || flag;       LastTimeFrame=timeframe; LastSymb=symbol_name;     if(changeTF) PerSec=::PeriodSeconds(timeframe); if(PerSec==0) { flag=true; return(0);}       if(stop_time<start_time)       {        TimeCur=stop_time;        stop_time=start_time;        start_time=TimeCur;       }     if(changeSymb)       {        if(!SymbolInfoInteger(symbol_name,SYMBOL_SELECT))          {           SymbolSelect(symbol_name,true);           ChartRedraw();          }       }     TimeCur=TimeCurrent();     if(timeframe==PERIOD_W1) TimeCur-=(TimeCur+345600)%PerSec; // 01.01.1970 - Thursday. Minus 4 days.     if(timeframe<PERIOD_W1) TimeCur-=TimeCur%PerSec;     if(start_time>TimeCur) { flag=true; return(0);}     if(timeframe==PERIOD_MN1)       {        MqlDateTime dt;        TimeToStruct(TimeCur,dt);        TimeCur=dt.year*12+dt.mon;       }       if(changeTF || changeSymb || TimeCur!=LastTimeCur)        LastBAR=(datetime)SeriesInfoInteger(symbol_name,timeframe,SERIES_LASTBAR_DATE);       LastTimeCur=TimeCur;     if(start_time>LastBAR) { flag=true; return(0);}       datetime tS,tF=0;     if(timeframe==PERIOD_W1) tS=start_time-(start_time+345599)%PerSec-1;     else if(timeframe<PERIOD_MN1) tS=start_time-(start_time-1)%PerSec-1;     else  //  PERIOD_MN1       {        MqlDateTime dt;        TimeToStruct(start_time-1,dt);        tS=dt.year*12+dt.mon;       }     if(change || tS!=LastTime) { PreBarsS=Bars(symbol_name,timeframe,start_time,UINT_MAX); LastTime=tS;}     if(stop_time<=LastBAR)       {        if(PreBarsS>=max_bars) PreBars=Bars(symbol_name,timeframe,start_time,stop_time);        else          {           if(timeframe<PERIOD_W1) tF=stop_time-(stop_time)%PerSec;           else if(timeframe==PERIOD_W1) tF=stop_time-(stop_time+345600)%PerSec;           else //  PERIOD_MN1             {              MqlDateTime dt0;              TimeToStruct(stop_time-1,dt0);              tF=dt0.year*12+dt0.mon;             }           if(change || tF!=LastTime0)             { PreBarsF=Bars(symbol_name,timeframe,stop_time+1,UINT_MAX); LastTime0=tF; }           PreBars=PreBarsS-PreBarsF;          }       }     else PreBars=PreBarsS;     flag=false;     return(PreBars);    }  //+------------------------------------------------------------------+  int iBars(string symbol_name,ENUM_TIMEFRAMES  timeframe) {return(Bars(symbol_name,timeframe));}  //+------------------------------------------------------------------+  
20417

Best MetaTrader Indicators + Profitable Expert Advisors