Analogue to iBarShift - page 5

 
Aleksey Vyazmikin:

Why is the script so slow?

The answer is in the profiler.

 
Andrey Khatimlianskii:

The answer is in the profiler.

OK, the profiler refers to this piece of code

Res=::Bars(Symb,TimeFrame,time,UINT_MAX)-1;

What doesn't it like? The function is not mine, but the author above claimed it should work quickly...

 
Aleksey Vyazmikin:

OK, the profiler refers to this piece of code

What doesn't it like? The function is not mine, but the author above claimed it should work fast...

I don't really understand the humor, why did you overload the MQL4iBarShift function? The compiler probably gets confused because of it.
The iBarShift2 function is designed for MQL5, and in MQL4, it only tested the correctness of its execution, comparing it with the standard function.

 
Nikolai Semko:

I don't understand the humour, why did you overload the MQL4 iBarShift function? The compiler probably gets confused because of it.

No one has cancelled

#ifdef __MQL5__
/**************************/
#else
/**************************/
#endif

And there should not be any confusion.

 
Nikolai Semko:

I don't understand the humor, why did you overload the MQL4 iBarShift function? The compiler may get confused because of it.
The iBarShift2 function is designed for MQL5, and in MQL4, it only tested the correctness of its execution, comparing it with the standard function.

I guess I'm not paying attention, but I looked at your posts again - i.e. your functions for MT4? It's just that the topic was about MT5, I guess that's what misled me.

I've added: Damn, I've found another code, it's all blurred, I apologise.

 
Aleksey Vyazmikin:

I must not have been paying attention, but I looked at your posts again - i.e. your functions for MT4? It's just that the topic was about MT5, I guess that's what misled me.

Added: Shit, that's it - found another code - it all got blurry, sorry.

Wow. No. I tried your code in MQL5 and got confused. Now I understand what you mean.
Here is an interesting picture.
If your script runs on native TF=1 Day, everything is OK.
But if I run it on another TF I get unbelievable disproportionate lags, which I can't explain yet, except for some bug in the compiler.
At the same time in debug mode or profiling mode there are no these brakes!!!
I will now experiment and look for the source of the brakes.

 
Nikolai Semko:

Whoa. No. I have tried your code in MQL5 and got confused. I now understand what you mean.
Here is an interesting picture.
If your script runs on native TF=1 Day, everything is OK.
But if I run it on another TF I get unbelievable disproportionate lags, which I can't explain yet, except for some bug in the compiler.
At the same time in debug mode or profiling mode there are no these brakes!!!
I will now experiment and look for the source of the brakes.

Start your search by substituting a normal datetime value

Res=::Bars(Symb,TimeFrame,time,UINT_MAX)-1;
 
Aleksey Vyazmikin:

Why is the script so slow?


Yes, it is surprising.

I have localised the location of the brakes.

//+------------------------------------------------------------------+
//|                                                    iBarShift.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property script_show_inputs

input ENUM_TIMEFRAMES TF=PERIOD_D1;
input int Bar=3;
input int calcN=1;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   for(int index=0;index<calcN;index++)
     {
      Print("0");
      datetime T=iTime(_Symbol,PERIOD_CURRENT,index);
      Print("00  "+ TimeToString(T));
      int Day_Shift=iBarShift(_Symbol,TF,T,false);
      Print("1");
      int Start=iBarShift(_Symbol,PERIOD_CURRENT,iTime(_Symbol,TF,Bar+Day_Shift),false);
      Print("2");
      int Stop=iBarShift(_Symbol,PERIOD_CURRENT,iTime(_Symbol,TF,Day_Shift),false);
      Print("3");

      if(index<3)Print("1 Start=",Start," Stop=",Stop," Day_Shift=",Day_Shift," index=",index);
      Print("4");

      if(index<3)Print("1 Start=",TimeToString(iTime(_Symbol,TF,Bar+Day_Shift),TIME_DATE|TIME_MINUTES),
         " Stop=",TimeToString(iTime(_Symbol,TF,Day_Shift),TIME_DATE|TIME_MINUTES),
         " Day_Shift=",TimeToString(iTime(_Symbol,PERIOD_CURRENT,index),TIME_DATE|TIME_MINUTES)," index=",index);
     }

   Print("5");

  }
//+------------------------------------------------------------------+ 
//| Получим iBarShift для заданного номера бара                      | 
//+------------------------------------------------------------------+   
int iBarShift(const string Symb,const ENUM_TIMEFRAMES TimeFrame,datetime time,bool exact=false)
  {
   static int Res=-1;
   static string LastSymb=NULL;
   static ENUM_TIMEFRAMES LastTimeFrame=0;
   static datetime LastTime=0;

   if((time!=LastTime) || (Symb!=LastSymb) || (TimeFrame!=LastTimeFrame))
     {
      Print("10");
      Res=::Bars(Symb,TimeFrame,time,UINT_MAX)-1;
      Print("11");
      if(Res<0) Res=0;

      LastTime = time;
      LastSymb = Symb;
      LastTimeFrame=TimeFrame;
     }
   return(Res);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
datetime iTime(string symbol,ENUM_TIMEFRAMES tf,int index)
  {
   if(index < 0) return(-1);
   datetime Arr[];
   if(CopyTime(symbol,tf,index,1,Arr)>0)
      return(Arr[0]);
   else return(-1);
  }
//+------------------------------------------------------------------+

If you run this script on, say, H4

you can see that the first time it accesses

Res=::Bars(Symb,TimeFrame,time,UINT_MAX)-1;

the system freezes.

And this stupor is not present when profiling or debugging.

A clear issue for the service desk

 
Aleksey Vyazmikin:

Why is the script so slow?

2018.03.30 09:21:05.208 BS (Si Splice,H4) 1 Start=15 Stop=3 Day_Shift=0 index=0

2018.03.30 09:21:05.208 BS (Si Splice,H4) 1 Start=2018.03.26 00:00 Stop=2018.03.29 00:00 Day_Shift=2018.03.29 20:00 index=0

2018.03.30 09:21:20.209 BS (Si Splice,H4) 2 Start=15 Stop=3 Day_Shift=0 index=0

2018.03.30 09:21:20.209 BS (Si Splice,H4) 2 Start=2018.03.26 00:00 Stop=2018.03.29 00:00 Day_Shift=2018.03.29 20:00 index=0

2018.03.30 09:20:49.300 Scripts script BS (Si Splice,H4) loaded successfully

2018.03.30 09:21:20.209 Scripts script BS (Si Splice,H4) removed

Thanks Alexey for the observation.
This is an obvious bug in the Bars() function.
Opened the question in the bugs, bugs, queries thread

 
Nikolai Semko:

Thanks Alexey for the observation.
This is a clear bug in Bars() function.
Opened a question in the bugs, bugs, queries thread

Thanks for the analysis, so I'm not completely insane yet...

Reason: