Analogue to iBarShift - page 15

 
Alexey Kozitsyn:
By the way, about the Bars() function. It might be the cause of the clincher.
This is easy to check. Change all Bars to my iBars. If the wedge disappears, then the problem is in that function. I too couldn't figure out the reason for some of my indicators freezing. Turned out to be this particular bug, everything is flying now.
 
fxsaber:

Cotypes can go for all characters except the one of interest.

OK, looked at the new source code. Saw that the edits that were discussed weren't made. Exit.

It only makes sense to use SYMBOL_TIME when the requested symbol is not in the Market Watch window. Then TimeCurrent won't do its job. But this variant of using Bars seems unlikely to me. But the price of getting the current time via SYMBOL_TIME is much higher because SymbolInfoInteger(symbol_name,SYMBOL_TIME) takes almost an order of magnitude longer. Of course, you can check if the symbol is in the market report and depending on the result useTimeCurrent or SYMBOL_TIME, but it's not free, moreover you always have to monitor if a new symbol is added or deleted from the market report. Therefore it is easier to make a clause that for correct work of iBars it is reasonable to have the requested symbol in the market report.

About SERIES_LASTBAR_DATE I think you are wrong.SymbolInfoInteger(symbol_name,SYMBOL_TIME) is the lesser evil.

The SeriesInfoInteger function doesn't cause any history paging. If there is anything that causes it, it's a request for Bars, which is logical. And the source of brakes can be seen in this short script, if you run it

void OnStart()
  {
   Print("1");
   Print(Bars(_Symbol,PERIOD_W1,D'2020.01.01 00:00',UINT_MAX));
   Print("2");
  }
 

Generally a very strange bug. I checked the effect of the download history on it when I found that suddenly today on the EURUSD symbol it almost didn't show up.

Forced me to download all the history. And the bug appeared again.

I guess the download history has no effect on this bug.

I don't understand why this bug is floating around.

Used this script to test it:

Files:
TestiBars.mq5  11 kb
 
Nikolai Semko:

I don't understand why this bug is floating around.

Is the SD aware of this whole topic?

 
In general, I think data loading/uploading is the weak point of the terminal.
 
Alexey Kozitsyn:

Is the SD aware of this whole topic?

Yes, already wrote there on 30.03.2018 - so far silence.

Alexey Kozitsyn:
In general, I think data loading/uploading is the weak point of the terminal.

Agreed, but it's also one of the most difficult tasks.

 
Nikolai Semko:

The iBars function is quite cumbersome, but I still recommend using it instead of the regular Bars, until MQ fixes the hang-up bug in it.

The iBar hangs when logically it should return 0. As a rule, it returns it for more than 10 seconds. There is no such bug in MQL4.

In the majority of tasks, iBars will work faster than the regular Bars since it will not only avoid the bug, but tries not to use the Bars and SeriesInfoInteger functions whenever possible due to the algorithm of saving previous values.

I have tested this function far and wide. It seems to be a full copy of Bars.

Perhaps it can be done in a more elegant way. If you have a desire, you are welcome. If you find errors, we will fix them.

So...

Then the full analogue of iBarsShift function will have the following form:

And the variant without the last parameter, which is used in the vast majority of cases will look like this:

I use your code iBarsShift+iBars (and other iBarsShift) and get 0 from iBarsShift, and an error when TF chart H1 and calculating for H1

2018.04.21 14:38:01.059 SVA_LinearRegression_test (Si Splice,H1)        zero divide in 'SVA_LinearRegression_test.mq5' (176,44)

which corresponds to this line of code

   if(timeframe<PERIOD_W1) TimeCur-=TimeCur% PerSec;

Here is the code of indicator as a whole

#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3

//--- plot Label1
#property indicator_label1  "LR_line"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrGold
#property indicator_style1  STYLE_DOT
#property indicator_width1  1
//--- plot Label2
#property indicator_label2  "Sup_line"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrAquamarine
#property indicator_style2  STYLE_DOT
#property indicator_width2  1
//--- plot Label3
#property indicator_label3  "Res_line"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrOrangeRed
#property indicator_style3  STYLE_DOT
#property indicator_width3  1


//--- input parameters
input ENUM_TIMEFRAMES TF=PERIOD_D1;
input int Bar=3;
input bool UseClose = true;


//--- indicator buffers
double LR_line_Ind[];
double Sup_line_Ind[];
double Res_line_Ind[];

//---
int limit,start;

//Список переменных:
static datetime TimeN=0;
int  barsToCount=0;

int InpChannelPeriod=1000;
double OpenI[];
double HighI[];
double LowI[];
double CloseI[];
double arr[];

double Calc_LR_line=0.0;
double Calc_Sup_line=0.0;
double Calc_Res_line=0.0;


//////////////////////////////////////////////////////////////////////

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,LR_line_Ind,INDICATOR_DATA);
   SetIndexBuffer(1,Sup_line_Ind,INDICATOR_DATA);
   SetIndexBuffer(2,Res_line_Ind,INDICATOR_DATA);   
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- set first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpChannelPeriod);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpChannelPeriod);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpChannelPeriod);   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                         |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{


}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   ArraySetAsSeries(LR_line_Ind,true); 
   ArraySetAsSeries(Sup_line_Ind,true); 
   ArraySetAsSeries(Res_line_Ind,true); 
   ArraySetAsSeries(time,true); 

//--- check for rates
   if(rates_total<InpChannelPeriod) return(0);
//--- preliminary calculations
   if(prev_calculated==0) limit=InpChannelPeriod;
   else limit=prev_calculated;
//--- the main loop of calculations
   for(int C=limit;C<rates_total && !IsStopped();C++)
     {
       LRegrf(C);
       LR_line_Ind[C]=Calc_LR_line;
       Sup_line_Ind[C]=Calc_Sup_line;
       Res_line_Ind[C]=Calc_Res_line;    
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
  
//+------------------------------------------------------------------+
double LRegrf(int index)
{
int Day_Shift=iBarShift(_Symbol,TF,iTime(_Symbol,PERIOD_CURRENT,index),false);

Print(iTime(_Symbol,PERIOD_CURRENT,index));
Print(Day_Shift);

return (0);
}
//-------------------------------------------------------------------
//==MQL4toMQL5
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
datetime iTime(string symbol,ENUM_TIMEFRAMES tf,int index)
  {
   if(index < 0) return(-1);
//   ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
   //ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT;
   datetime Arr[];
   if(CopyTime(symbol,tf,index,1,Arr)>0)
      return(Arr[0]);
   else return(-1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int iBarShift(const string Symb,const ENUM_TIMEFRAMES TimeFrame,datetime time,bool exact=false)
  {
   int Res=iBars(Symb,TimeFrame,time+1,UINT_MAX);
   if(exact) if((TimeFrame!=PERIOD_MN1 || time>TimeCurrent()) && Res==iBars(Symb,TimeFrame,time-PeriodSeconds(TimeFrame)+1,UINT_MAX)) return(-1);
   return(Res);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int iBars(string symbol_name,ENUM_TIMEFRAMES  timeframe,datetime start_time,datetime stop_time)
  {
   static string LastSymb=NULL;
   static ENUM_TIMEFRAMES LastTimeFrame=0;
   static datetime LastTime=0;
   static datetime LastTime0=0;
   static int PerSec=0;
   static int PreBars=0;
   static datetime LastBAR=0;
   static datetime LastTimeCur=0;
   datetime TimeCur;
   if(stop_time<start_time) {TimeCur=stop_time; stop_time=start_time; start_time=TimeCur; }
   TimeCur=TimeCurrent();
   if(LastTimeFrame!=timeframe) if(timeframe==PERIOD_MN1) PerSec=2419200; else PerSec=::PeriodSeconds(timeframe);
   if(timeframe<PERIOD_W1) TimeCur-=TimeCur%PerSec;
   if(start_time>TimeCur) {LastSymb=NULL; return(0);}
   if(LastTimeFrame!=timeframe || LastSymb!=symbol_name || ((TimeCur-LastBAR)>0 && TimeCur!=LastTimeCur))
      LastBAR=(datetime)SeriesInfoInteger(symbol_name,timeframe,SERIES_LASTBAR_DATE);

   LastTimeCur=TimeCur;
   if(PerSec==0) return(0);
   if(start_time>LastBAR)
     {LastTimeFrame=timeframe; LastSymb=symbol_name; return(0);}

   datetime tS,tF=0;
   bool check=true;
   if(timeframe<PERIOD_W1) tS=start_time-(start_time-1)%PerSec-1;
   else if(timeframe==PERIOD_W1) tS=start_time-(start_time-259201)%PerSec-1;
   else
     {
      PerSec=2678400;
      MqlDateTime dt;
      TimeToStruct(start_time-1,dt);
      tS=dt.year*12+dt.mon;
     }
   if(stop_time<=LastBAR)
     {
      if(timeframe<PERIOD_W1) tF=stop_time-(stop_time)%PerSec;
      else if(timeframe==PERIOD_W1) tF=stop_time-(stop_time-259200)%PerSec;
      else
        {
         MqlDateTime dt0;
         TimeToStruct(stop_time,dt0);
         tF=dt0.year*12+dt0.mon;
        }
      if(tS==tF) {PreBars=0; check=false;}
     }
   if((LastTimeFrame!=timeframe || LastSymb!=symbol_name || tS!=LastTime || tF!=LastTime0) && check)
      PreBars=Bars(symbol_name,timeframe,start_time,stop_time);
   LastTime=tS; LastTime0=(datetime)tF;
   LastTimeFrame=timeframe;
   LastSymb=symbol_name;
   return(PreBars);
  }

WhyPrint(Day_Shift) always returns zero, while date and time are correct?

It seems to be a weekend effect, since everything was working correctly the other day (although with a different function, but it doesn't work today either).

 
Aleksey Vyazmikin:

I use your code iBarsShift+iBars (and other iBarsShift) and get 0 from iBarsShift , and when TF chart H1 and calculating on H1 an error

which corresponds to this line of code

Here is the code of indicator as a whole

WhyPrint(Day_Shift) always returns zero, while date and date are correct?

It seems to be a weekend effect, since everything was working correctly the other day (although with a different function, but it is not working today too).

I apologise for leaving the code in the wrong form.

I noticed the inaccuracy back then and almost fixed it, but there was still a small, easily fixable problem.
I just abandoned the code due to the fact that I'm studying now and exams have started and I just don't have time. The last exam is on the 24th of April.
After that I will fix everything and post it on CB.

I've already started publishing it, but have put it on hold.


 
Nikolai Semko:

I apologise for leaving the code in the wrong form.

I noticed the inaccurate work back then and almost fixed it, but there was still a small easily fixable problem.
I just abandoned the code because I'm studying now and exam time has started and I just don't have the time. The last exam is on the 24th of April.
After that I will fix everything and post it on CB.

I have already started posting but have put it on hold.


I will wait for the corrections in the final form, thanks for responding.

Good luck with the exams!

 
Aleksey Vyazmikin:

I will wait for the corrections in the final form, thank you for replying.

Good luck with your exams!

Thank you))
Reason: