Libraries: iBarShift - page 3

 
angevoyageur:

Thank you, I have to fix my code also when exact is true, as I misunderstood this parameter. English documentation for iBarShift is wrong.

I think this is my error too, iBarShift should return bar which contain specified time (not with exact open time).

Could I use your function in my library (with your copyrights, of course)? If it is faster and correct, why should I reinvent the wheel?)

 
komposter:

I think this is my error too, iBarShift should return bar which contain specified time (not with exact open time).

Could I use your function in my library (with your copyrights, of course)? If it is faster and correct, why should I reinvent the wheel?)

Of course.
 
Corrected code, taking well into account 'exact' parameter, has been published.
 
angevoyageur:

Thank you, I have to fix my code also when exact is true, as I misunderstood this parameter. English documentation for iBarShift is wrong.

The mql5 bug is on Bars() function, I reported it to Service Desk.

Seems the Bars() bug is now corrected in build 880.
 
Build 880 fix the bug on Bars(), and all is working well now.
 
angevoyageur:
Build 880 fix the bug on Bars(), and all is working well now.
 

on m2 with m1 the values at trudy, for some reason have values not -1, and at zero bar there is a return to the 1st bar.

 

The function is crooked in at least two places:

1) if(time>LastBar)

return(0);

If exact=true, the logic should return -1 in case time does not explicitly belong to the last bar, i.e. if time>LastBar+PeriodSeconds(timeframe).

2) if(CopyTime(symbol,timeframe,time,1,checkcandle)==1)

...

return(-1);

Again, if exact=false, the nearest bar should be returned, in this case Bars(symbol,timeframe)-1.

 

I think there is a minor bug in your code:

//--- if time > LastBar we always return 0
   if(time>LastBar)
      return(0);

If the the last tick 'opens' a new bar then if(time>LastBar) becomes false - because now time == LastBar

And later we land here:

 if(checkcandle[0]==time)
         return(shift-1);

And now shift == 0 which returns -1 instead of 0!

So I think this:

//--- if time > LastBar we always return 0
   if(time>LastBar)
      return(0);

should be changed to

//--- if time > LastBar we always return 0
   if(time>=LastBar)
      return(0);

Calli

 
Carl Schreiber:

I think there is a minor bug in your code:

If the the last tick 'opens' a new bar then if(time>LastBar) becomes false - because now time == LastBar

Not sure what you mean ? I think 'last tick' is irrelevant. When you use the function either time=LastBar or not.

And later we land here:

And now shift == 0 which returns -1 instead of 0!

if time==Lastbar then shift=1  and not 0. So there is no bug (shift comes from Bars() which is the bars count, that's why we return 'shift-1').

So I think this:

should be changed to

Calli

Your correction would work too, but the case 'time==LastBar' is processed as a "normal" case.

The statement

 if(time>LastBar)

was added as a bug fix if time is greater than current bar 0 time.