Analogue to iBarShift - page 9

 
Alain Verleyen:

What problem did you find?

Here is an example of your function falsely working under the name "BarShift1"


 
Aleksey Vyazmikin :

Here is an example of your function working falsely - it works under the name "BarShift1"


Actually it only shows the opposite, my version is the only one which is correct. (And the original iBarShift1 from this code is correct).

My version was designed as a standalone function, just like mql4 iBarShift.

It is not optimised for multiple queries, so the time comparison is irrelevant. @nicholishen published a good library optimised for mass calling.


Actually it shows just the reverse, my version is the only one that is correct. (Also the original iBarShift1 from this code is correct).

My version was conceived to be used as a standalone function, exactly like mql4 iBarShift.

It's not optimized for multiple requests, so time comparison is irrelevant. @nicholishen published a good library optimized for bulk call.

iBarShift
iBarShift
  • votes: 46
  • 2013.10.25
  • Alain Verleyen
  • www.mql5.com
Многие ищут функцию iBarShift, которая была в языке MQL4 (например, 1,2,3). В языке MQL5 ее нет, но есть все возможности для ее реализации в виде библиотеки. В качестве альтернативы многие программисты предложили свои варианты реализации этой функции на MQL5. Обнаружилось, что все 4 версии содержат ошибки (не воспроизводят в точности работу...
 
Alain Verleyen:

In fact it only shows the opposite, my version is the only one which is correct. (And the original iBarShift1 from this code is correct).

My version was designed as a standalone function, just like mql4 iBarShift.

It is not optimised for multiple queries, so the time comparison is irrelevant. @nicholishen published a good library optimised for mass calling.


Actually it shows just the reverse, my version is the only one that is correct. (Also the original iBarShift1 from this code is correct).

My version was conceived to be used as a standalone function, exactly like mql4 iBarShift.

It's not optimized for multiple requests, so time comparison is irrelevant. @nicholishen published a good library optimized for bulk call.

I'm not talking about processing time, I'm talking about bar number.

In the picture you can see that at 8:37 the code thinks that the nearest bar is the bar of the previous day 23:00 , but in fact the nearest bar is 10:00. It is closer both mathematically and logically.

 
Aleksey Vyazmikin:

I'm not talking about processing time, I'm talking about the bar number.

The picture shows that at 8:37 the code considers that the nearest bar is the bar of the previous day at 23:00, but in fact the nearest bar is 10:00. He is closer both in mathematics and in logic.

let's simplify it.

Forum on trading, automated trading systems and strategy testing

Analogue to iBarShift

Alain Verleyen, 2018.04.05 00:18

Actually it only shows the opposite, my version is the only one that is correct. (And the original iBarShift1 from this code is correct).

My version was conceived as a standalone function, just like mql4 iBarShift.

Try mql4, compare, same thing.
 
Alain Verleyen:
Let's simplify it.
Try mql4, compare, same thing.

I see your argument. I am not going to check it. Apparently, it's my task that differs from the accepted logic.

 
Alain Verleyen:

What problem have you discovered?

I wrote this a long time ago. I don't remember anymore, but something confused me.
Anyway, now I've found only one abnormal situation.
When not yet loaded symbol data may return -1 and should return 0.

This can be checked using the script for MQL4 that I'm attaching.

This script takes random time and random timeframe. If iBarShiftX value does not match the regular iBarShift function, then message via Print.

If you run this script on a newly opened symbol, you will see errors. Re-running the script on the same symbol will not give errors.

But it is a trifle. In my version is the same problem.

I have only one complaint about your version: it is very complicated and slow.

Here is your variant:

int iBarShift2(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)
      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);
  }

And here is my function, which does the same thing, but with a simple algorithm and much faster:

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

You can use an even shorter one without the function:

Bars(Symb,TimeFrame,time+1,UINT_MAX);

Try to prove otherwise. Find a single combination of parameters when your function and mine will show different values.

Only I haven't implemented the last parameter exact in it, because I don't understand why it's needed at all. I personally have never needed it.

But if someone really needs it, it can be implemented.

I left the iBarShift3 variant, because it incorrectly handles history holes. It can be fixed, but I don't see the point, as the above option is enough.

Files:
 
Nikolai Semko:


I left the iBarShift3 option because it doesn't correctly handle history holes. It can be fixed, but I don't see the point, because the above option is sufficient.

Are the holes because of the lack of pro-trading on the bar, or other ones? And, how does the inaccuracy manifest itself?

 
Aleksey Vyazmikin:

Are the holes due to a lack of pro-trading on the bar, or other? And what is the inaccuracy?

Let's take a Sunday any time when there is no trading:

Print("iBarShift  = "+IntegerToString(iBarShift (_Symbol,PERIOD_H1,D'01.04.2018 10:00:00')));  
Print("iBarShift3 = "+IntegerToString(iBarShift3(_Symbol,PERIOD_H1,D'01.04.2018 10:00:00'))); // показывает на единицу меньше
 
Nikolai Semko:

Suppose we take Sunday any time when there is no trading:

So it shows Monday? That's what I want... :)

 
Why don't you try my function? There's a solution there that neutralises the start and end of bars in time. It seems to calculate everything correctly. And it is faster in time than your fastest version 3. Or the branch is more important?) Or is there an error there too?) I've been using it for a long time....
Reason: