close and open metatrader

 

hi friends,

I faced with a problem that my written indicator works well when the metatrader is open and it works accurately. But when I close the metatrader and reopen it after a while, my indicator does not examine the newly added candles that are created when the metatrader was off, but as I reset my indicator by changing the timeframe or remove and reattach my indicator to the chart it again works properly and calculate the written codes on the newly added candles.

I'd appreciate if anyone could help me how can I solve this problem.

thanking in advance 

 
It's bugs in your code. You need to debug your code.
 
please consider this example: I want to draw vertical lines on the candle numbers 1,2,3,4,5 and update them by passing the time that I mean after appearing a new candle delete all previously created and draw new one. Please be noted that I write my code in a way that it draws the 1st line through OnInit function and the rest in the OnCalculate function. As you can see below I've written the code for the above mentioned and it works properly but the problem is that when I close the Metatrader and reopen it after passing some time, my indicator does not work properly and does not draw the vertical lines for the newly added candles and instead it still shows the vertical lines that were drawn before closing Metatrader. All I want is to update the indicator with newly added candles as I reopen the Metatrader and draw the vertical lines for the newly added candles instead of the previously drawn ones.

Here is my code :

//+------------------------------------------------------------------+
//|                                                       Test 4.mq4 |
//|                                                        Parham.PM |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Parham.trader"
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

datetime last_recognized_candle_time;
int k;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   last_recognized_candle_time=iTime(Symbol(),0,0);
   k=1;
   ObjectCreate(StringConcatenate("x",k),OBJ_VLINE,0,iTime(Symbol(),0,k),0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {

//---

   if(last_recognized_candle_time!=iTime(Symbol(),0,0))
     {
      k=0;
      ObjectsDeleteAll();
      OnInit();
      return(rates_total);
     }

   if(k<5)
     {
      k++;
      ObjectCreate(StringConcatenate("x",k),OBJ_VLINE,0,iTime(Symbol(),0,k),0);
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectsDeleteAll();
  }
//+------------------------------------------------------------------+

 


 

 
 
 
Guarding just time of the last candle is not a safe detection, compare rather the prev_calculated parameter.
 
Ovo:
Guarding just time of the last candle is not a safe detection, compare rather the prev_calculated parameter.
Thank you very much for the response , I'd truly appreciate if you kindly guide me about prev_calculated parameter and post related codes here to solve the mentioned problem I because I do not know anything about it.
 
Ovo:
Guarding just time of the last candle is not a safe detection, compare rather the prev_calculated parameter.

I google it and found it out ,

As you said, using perv_calculated was actually the most suitable solution for this issue.

Thank you again. 

 

what if I want to use the prev_calculated parameters only when the Metatrader terminal opens not the time when I change the timeframe. consider the following code: 

//+------------------------------------------------------------------+
//|                                                       Test 4.mq4 |
//|                                                        Parham.PM |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Parham.trader"
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

int k;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   k=1;
   ObjectCreate(StringConcatenate("x",k),OBJ_VLINE,0,iTime(Symbol(),0,k),0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {

//---

   if(prev_calculated==0)
     {
      k=0;
      ObjectsDeleteAll();
      OnInit();
      return(rates_total);
     }

   if(k<5)
     {
      k++;
      ObjectCreate(StringConcatenate("x",k),OBJ_VLINE,0,iTime(Symbol(),0,k),0);
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   ObjectsDeleteAll();
  }
//+------------------------------------------------------------------+

 As you can see above, the prev_calculated==0 would be true when I change the chart timeframe or close and reopen the metatrader application.

Is there any way to code it in a way to make prev_calculated==0 to be true only if I open the metatrader not when I change the chart timeframe?

Thanking in advance, 

 
You cannot, prev_calculated will automatically be set to 0
Reason: