Analogue to iBarShift - page 17

 
Nikolai Semko:

It turned out to be very nuanced.

If I'd known how complicated it would be, I wouldn't have got involved ))))

This version should work correctly.
If anyone finds it working incorrectly, I'd be grateful if they could report the problem.


Well done!

Thank you!

Built it, all seems well so far, the only thing I have a problem with is some jagged charts (from the current TF I am looking for H1), but I have decided not to understand why there is a difference between the other version (maybe it is not the right calculation)...

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

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

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

   return(Res);
  }  

But the last variant worked better on weeks!

 
Aleksey Vyazmikin:

Oh, that's great!

Thank you!

Built it, everything seems good so far, the only problem is with the torn charts (from the current TF looking for H1), but I decided not to investigate why there is a difference between the other version (maybe it's not the right calculation)...

But the last version worked better for weeks!

This version of iBarShift in your last post(also mine) is best forgotten. It has a number of flaws. It's a long explanation.

Use this version.

 
Nikolai Semko:

that version of iBarShift in your last post (also mine) is best forgotten. It has a number of bugs. It's a long explanation.

Use this version, but only with this add-on.

Ok.