Missing data when restarting MT4 after some time.

 

I made an indicator an indicator to plot vlines on every candle but when closing the MT4 and opening it again after some time, there is also gap where there is no plotted vlines. See image below:

I closed the MT4 then open it again after 3 minutes.


Here is the code:

#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

string unique_id = "hlines_";
input int bars_limit = 20;

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

datetime t_0, prev_time;
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[])
  {
   static int limit;
   int i;
   if(prev_calculated==0 && prev_time==0)
     {
      limit=0;
      i=rates_total-1;
     }
   else 
     {
      limit=prev_calculated-1;
      i = (Time[0] - prev_time)/PeriodSeconds();
      
      //i=iBarShift(NULL,0,prev_time)-1;
      //Print(" i: ", i);
     }
     
   for(i; i>=0; i--)
     {
      t_0 = time[i];
      if (i>rates_total-2 || i>bars_limit) continue;   
      //Print(t_0, " i: ", i);
      
      string _name = unique_id+TimeToString(t_0);
      if (ObjectFind(0,_name)<0){
         ObjectCreate(0,_name,OBJ_VLINE,0,t_0,0);
         Print(_name, " rates: ", rates_total, " prev: ", prev_calculated, " i: ", i, " limit: ", limit, " prev_time: ", prev_time);   
      }   
      prev_time = t_0;

     }
   return(rates_total);
  }
  
  
void OnDeinit(const int reason)
{
   ObjectsDeleteAll(0,unique_id);
}   


Please help fix the missing values on other candles. Thanks!

 
i = (Time[0] - prev_time)/PeriodSeconds();

"i" is not a time. use bars.

i=limit; ?


prev_time = t_0;

prev_time = i; // not used anyways
 
  1. datetime t_0, prev_time;
    
    if(prev_calculated==0 && prev_time==0)
    1. You properly used strict. Therefor, prev_time has a random value and your if can fail.
    2. Why are you saving the time? Rates_total and prev_calculated is all you need.
                How to do your lookbacks correctly #9#14 & #19 (2016)

  2.    i = (Time[0] - prev_time)/PeriodSeconds();

    This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (2006)
              No candle if open = close ? - MQL4 programming forum (2010)

    Use Rates_total and prev_calculated

 
Thank you! I have fixed it.