Analogue to iBarShift - page 7

 

Here is the fastest, correct working version on all TFs:

int iBarShift3(const string Symb,const ENUM_TIMEFRAMES TimeFrame,datetime time)
  {
   static int Res=-1;
   static string LastSymb=NULL;
   static ENUM_TIMEFRAMES LastTimeFrame=0;
   static datetime LastTime=0;
   static int PerSec=::PeriodSeconds(LastTimeFrame);

   if(LastTimeFrame!=TimeFrame) PerSec=::PeriodSeconds(TimeFrame);
   if(TimeFrame<PERIOD_D1) time-=time%PerSec;

   if((time!=LastTime) || (Symb!=LastSymb) || (TimeFrame!=LastTimeFrame))
     {
      Res=::Bars(Symb,TimeFrame,time,UINT_MAX);
      if(TimeFrame<PERIOD_D1) Res--;
      if(Res<0) Res=0;

      LastTime = time;
      LastSymb = Symb;
      LastTimeFrame=TimeFrame;
     }

   return(Res);
  }

Speed slows down for TF D1, W1 and MN1

And here is a test indicator, which clearly shows the response of different versions, as well as their speed ( the second number. The smaller the number, the faster the function)


Both versions 3 and 4 work correctly. But the 3rd one is faster.

Variants with CopyTime are the slowest.

This can be checked in MQL4 (see indicator attached).

I do not specify the authorship of different variants, because I've already got confused.
But the third, most useful way is 80% from@fxsaber. I have only tweaked it a little bit.

Files:
 
Nikolai Semko:

Both options 3 and 4 work correctly. But 3 is the fastest. CopyTime variants are the slowest.

Forum on trading, automated trading systems and strategy testing

Discussion on "LifeHack for trader: mixing ForEach on defines (#define)"

fxsaber, 2018.02.14 11:58

ZZY Function speed measurements should be measured in an environment where performance is important - Tester.

 
fxsaber:

The measurements don't really matter here. You can see that the logic is the fastest.

 
Nikolai Semko:

Here is the fastest, correct working version on all TFs:

Speed slows down for TF D1, W1 and MN1

And here is a test indicator, which clearly shows the response of different versions, as well as their speed ( the second number. The smaller the number, the faster the function)


Both versions 3 and 4 work correctly. But the 3rd one is faster.

Variants with CopyTime are the slowest.

It can be checked in MQL4 (see indicator attached).

I am not indicating the authorship of different variants because I am already confused.
But the third, most working variant is 80% from@fxsaber. I only tweaked it a bit.

I have added TF to your indicator - the first two algorithms are not useful at all.

In the figures, the current TF is H1, and the calculation is based on daily rates.



 
Renat Akhtyamov:
Yes (highlighted)

-1 is a one minus (to clarify), returned by the error function, which says that there is no such bar

So my function

also needs to be refined

though...

Documentation:

"Note.

If data for the timeseries with the specified parameters when calling the Bars() function have not yet been generated in the terminal, or data of the timeseries are not synchronized with the trade server at the moment of the function call, then function will return zero value. "

====

If res==0, then we will catch -1 from the function as it is.

===

So it all works, use it to your advantage!

On the indicator above you can clearly see how false this method is.

 
Nikolai Semko:

Here is the fastest, correct working version on all TFs:

Speed slows down for TF D1, W1 and MN1

And here is a test indicator, which clearly shows the response of different versions, as well as their speed ( the second number. The smaller the number, the faster the function)


Both versions 3 and 4 work correctly. But the 3rd one is faster.

Variants with CopyTime are the slowest.

It can be checked on MQL4 (see indicator attached).

I do not specify the authorship of different variants, because I've already got confused.
But the third, most working variant is 80% from@fxsaber. I only tweaked it a little bit.

TheiBarShift3 variantdoes not work correctly.

 

Here's my version. Seems to work with both junior and senior frames. Speed is a little bit faster than iBarshift3.


int iBarShift(string symbol, ENUM_TIMEFRAMES timeframe, datetime time){

datetime t1 = TimeCurrent()+10000000;

int ps = PeriodSeconds(timeframe);

double div = time/(double)ps;

double mant = div - MathFloor(div);

int ret = Bars(symbol, timeframe, (datetime)(time-(ps*mant)), t1)-1;

return(ret);

}

 
Vitaly Muzichenko:

TheiBarShift3 option doesnot work correctly.

Can I have an example of it not working correctly?

So that leaves only option four?

 
Vitaly Muzichenko:

TheiBarShift3 optiondoesn't work right.

Vocabulary is our everything.

 
Nikolai Semko:

Vocabulary is our everything.

I did a quick check: there is a function that has been working for more than a day, I substitutediBarShift3 instead and got the EA to work incorrectly. That's how I came to my conclusion.

Here is the one I use

int iBarShift(string symbol,ENUM_TIMEFRAMES timeframe,datetime time,bool exact=false) {
 datetime LastBAR;
  if(!SeriesInfoInteger(symbol,timeframe,SERIES_LASTBAR_DATE,LastBAR)) {
   datetime opentimelastbar[1];
    if(CopyTime(symbol,timeframe,0,1,opentimelastbar)==1)
      LastBAR=opentimelastbar[0];
    else
      return(-1);
  }
//--- if time > LastBar we always return 0
  if(time>LastBAR)
    return(0);
//---
 int shift=Bars(symbol,timeframe,time,LastBAR);
 datetime checkcandle[1];

  if(CopyTime(symbol,timeframe,time,1,checkcandle)==1) {
   if(checkcandle[0]==time)
     return(shift-1);
   else if(exact && time>checkcandle[0]+PeriodSeconds(timeframe))
     return(-1);
   else
     return(shift);
  }
  return(-1);
 }