Analogue to iBarShift - page 11

 
Nikolai Semko:

Yeah, well, that's actually strange behaviour for a standard function. After all, we are dealing exactly with fitting values to its "benchmark".

The standard function iBarShift of MQL4, when the requested time falls into a hole, returns the left bar number (i.e. Saturday in this case), and iBarShift3 returns the right bar number from the hole (i.e. Monday), which makes more sense.

If we are in the hole, it is logical to return the left bar, the last known value, but not the future.

 
Vitaly Muzichenko:

If we are in a hole, it is logical to return the left bar, the last known value, but not the future.

Right, I'm not making any sense. I confused the past with the future.) Probably because 1 (past) > 0 (future). Programmed.
 
Alain Verleyen:


In most cases this function returns the wrong result

PS: Is it needed to post English version or Russian translation is correct ?

Yes, the translation is correct.

Run this script and you will see that the functions produce exactly the same result.

Can you give an example of a timeframe and the time at which the values of these functions are different?

By the way, your function is often mistaken. Returns the value -1

2018.04.04 22:51:05.666 TestBarShift2 AUDUSD,M5: BarShiftX  PERIOD_M1   0   -1    2018.04.05 05:45:00
2018.04.04 22:50:46.867 TestBarShift2 AUDUSD,M5: BarShiftX  PERIOD_M1   0   -1    2018.04.05 05:46:00
2018.04.04 22:50:41.255 TestBarShift2 AUDUSD,M5: BarShiftX  PERIOD_M1   0   -1    2018.04.05 05:47:00
2018.04.04 22:50:23.496 TestBarShift2 AUDUSD,M5: BarShiftX  PERIOD_M1   0   -1    2018.04.05 05:45:00
2018.04.04 22:50:05.596 TestBarShift2 AUDUSD,M5: BarShiftX  PERIOD_M1   0   -1    2018.04.05 05:47:00
2018.04.04 22:48:38.638 TestBarShift2 AUDUSD,M5: BarShiftX  PERIOD_M1   0   -1    2018.04.05 05:47:00
Files:
 
Alain Verleyen:

The requirement is to have ONLY as a version of mql4


Your function is not working correctly with the exact=true parameter.

This can be seen in this script.

And here is a full working analogue of iBarShift function with exact parameter:

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

Without this parameter it can be simplified to this form:

Bars(Symb,TimeFrame,time+1,UINT_MAX);
Files:
 
Nikolai Semko:

Yes, the translation is correct.

Run this script and you will see that the functions produce exactly the same result.

Can you give an example of a timeframe and the time at which the values of these functions are different?

By the way, your function is often mistaken. Returns the value -1

Please be serious. My code is for mql5.

It returns -1 because Bars() function is unreliable under MT4.

...   
   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);
     }
...

Script running on AUDUSD, M5 chart.

This is not happening with MT5.

 
Alain Verleyen:

Please be serious. My code is for mql5.

It returns -1 because Bars() function is unreliable under MT4.

Script running on AUDUSD, M5 chart.

This is not happening with MT5.

You said it should work like in MQL4.

But this script can be run on MQL5 too

2018.04.05 09:52:40.760 TestBarShift2 (EURUSD,M12)      PERIOD_M3  BarShift1 = -1  BarShift2 = 0    2018.08.10 18:39:49   exact = true
2018.04.05 09:52:40.760 TestBarShift2 (EURUSD,M12)      PERIOD_M10  BarShift1 = -1  BarShift2 = 0    2018.04.15 00:25:08   exact = true
2018.04.05 09:52:40.761 TestBarShift2 (EURUSD,M12)      PERIOD_M6  BarShift1 = -1  BarShift2 = 0    2018.04.21 04:26:03   exact = true
2018.04.05 09:52:40.761 TestBarShift2 (EURUSD,M12)      PERIOD_M15  BarShift1 = -1  BarShift2 = 0    2018.05.29 17:10:52   exact = true
2018.04.05 09:52:40.761 TestBarShift2 (EURUSD,M12)      PERIOD_M12  BarShift1 = -1  BarShift2 = 0    2018.09.16 13:10:33   exact = true
2018.04.05 09:52:40.762 TestBarShift2 (EURUSD,M12)      PERIOD_M10  BarShift1 = -1  BarShift2 = 0    2018.09.25 21:24:50   exact = true
2018.04.05 09:52:40.762 TestBarShift2 (EURUSD,M12)      PERIOD_M10  BarShift1 = -1  BarShift2 = 0    2018.05.30 20:04:10   exact = true
2018.04.05 09:52:40.762 TestBarShift2 (EURUSD,M12)      PERIOD_M5  BarShift1 = -1  BarShift2 = 0    2018.04.29 16:12:16   exact = true

If exact=True and future time you have to return -1.

My script has also found a strange error:

This error is confirmed by this check:

Print (iBarShift2(_Symbol,PERIOD_MN1,D'2015.12.31 21:03:54',true)); // -1
Print (iBarShift1(_Symbol,PERIOD_MN1,D'2015.12.31 21:03:54',true)); // 28
So I was right about theexistence of irregularities in your algorithm after all.
Files:
 

From all the analysis I've done, we can conclude that this full analogue of the iBarShift:

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

is by far the most correct, and at the same time the fastest and with the simplest and shortest algorithm.

If you don't need the exact parameter, you can use the simplified version:

int iBarShift(const string Symb,const ENUM_TIMEFRAMES TimeFrame,datetime time)
  {
   return(Bars(Symb,TimeFrame,time+1,UINT_MAX));
  }

or just use this equivalent version without calling the function:

Bars(Symb,TimeFrame,time+1,UINT_MAX);
Or am I wrong?
 
Silence is a sign of agreement.
 
Nikolai Semko:
Silence is a sign of agreement.

Through CTRL+SHIFT+F in ME did a search for "BarShift". Turns out I don't use a similar function. Apparently didn't need it myself.

Once wrote a variant to convert MT4 EAs to MT5 in one line. Seems to be the only reason for writing it.

I don't work with bars and don't understand why everyone else doesn't do the same.

Didn't get into your code. But always glad when you get a quick solution.

 
fxsaber:

Through CTRL+SHIFT+F in ME did a search for "BarShift". Turns out I don't use a similar function. Apparently didn't need it myself.

Once wrote a variant to convert MT4 EAs to MT5 in one line. Seems to be the only reason for writing it.

I don't work with bars and don't understand why everyone else doesn't do the same.

Didn't get into your code. But always glad when you get a quick solution.

I understand what you mean, but working with bars is still urgent for me. Maybe one day it will become a rudiment for me too.

Reason: