Lines disappearing when Terminal restarts

 

Hi!

I'm facing a problem here that is getting me crazy!

I have an INDICATOR that works on the last close price (like Close[1]), and everything is functioning perfectly, either with TESTER or using real-time charts. I have no problem at all changing Timeframe and so forth.

The problem: if I close the Terminal with the Indicator loaded (it uses either the MAIN chart as a specific chart window), when I call again Metatrader5 it:

  • Loads itself again;
  • Its panel shows all calculations;
  • The indicator lines appear as they should;
  • But after a few seconds, its lines (and only them!) suddenly disappear. Important: The arrays which the lines are based are filled with valid values, and I can see this in PANEL but not in the INDICATOR string.

First I will show the normal screen:



And now, what appears in the Terminal when I close the terminal and load it again:


I'm aware of the "Prev_Calculated" index, and as I said before, the problem occurs ONLY in that specific situation: when I close META and open it again. For all other cases, the indicator works normally.

Follows, which I understand to be the relevant pieces of code:


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   
        SetIndexBuffer(0,Trend_Percents,INDICATOR_DATA);
        SetIndexBuffer(1,CounterTrend_Percents,INDICATOR_DATA);

        SetIndexBuffer(2,Trend_Numbers,INDICATOR_DATA);
        SetIndexBuffer(3,CounterTrend_Numbers,INDICATOR_DATA);
        PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_NONE);
        PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_NONE);


        ArrayInitialize(Trend_Percents,EMPTY_VALUE);
        ArrayInitialize(CounterTrend_Percents,EMPTY_VALUE);

        ArrayInitialize(Trend_Numbers,EMPTY_VALUE);
        ArrayInitialize(CounterTrend_Numbers,EMPTY_VALUE);




        if (ShowLines == SIM)
        {
        PlotIndexSetInteger(0,PLOT_LINE_COLOR, T_InpColor);
        PlotIndexSetInteger(0,PLOT_LINE_STYLE,T_InpEstilo);
        PlotIndexSetInteger(0,PLOT_LINE_WIDTH,T_InpLargura);
                
        PlotIndexSetInteger(1,PLOT_LINE_COLOR, CT_InpColor);
        PlotIndexSetInteger(1,PLOT_LINE_STYLE, CT_InpEstilo);
        PlotIndexSetInteger(1,PLOT_LINE_WIDTH, CT_InpLargura);


        // ----- LEVELS
        IndicatorSetInteger(INDICATOR_LEVELS,5); 
                
        IndicatorSetDouble(INDICATOR_LEVELVALUE,0,0);
        IndicatorSetDouble(INDICATOR_LEVELVALUE,1,25);
        IndicatorSetDouble(INDICATOR_LEVELVALUE,2,50);
        IndicatorSetDouble(INDICATOR_LEVELVALUE,3,75);
        IndicatorSetDouble(INDICATOR_LEVELVALUE,4,100);
                
        IndicatorSetDouble(INDICATOR_MINIMUM,MinMargin);
        IndicatorSetDouble(INDICATOR_MAXIMUM,MaxMargin);                

      
        }
        else
        {
        PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_NONE);
        PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);

        PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_NONE);
        PlotIndexSetInteger(1,PLOT_SHOW_DATA,false);

        }
        
        IndicatorSetString(INDICATOR_SHORTNAME,shortname);
        IndicatorSetInteger(INDICATOR_DIGITS,2);
        

   return(INIT_SUCCEEDED);

}
  


//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+    
void OnDeinit(const int reason)
  {

   bool   m_backtest = MQLInfoInteger(MQL_TESTER);
   if(!m_backtest)
   {
       if ( !ObjectDelete(0,object_T) ) Print("Error deleting <" + object_T + "> from Chart") ;
       if ( !ObjectDelete(0,object_CT) ) Print("Error deleting <" + object_CT + "> from Chart") ;
       if ( !ObjectDelete(0,object_TO) ) Print("Error deleting <" + object_TO + "> from Chart") ;
       if ( !ObjectDelete(0,object_CTO) ) Print("Error deleting <" + object_CTO + "> from Chart") ;

   	if (ShowPanel == SIM)
    	{  if ( !ObjectDelete(0,"Painel") ) Print("Error deleting PAINEL from Chart") ;
    	}
    	
    
    	if (PlotLine == SIM)
    	{
    		string obj_name;
    		for (int i = LinesCounter ; i >=0; i--)
    		{
    			obj_name = "linha" + IntegerToString(i,0);
    			if ( !ObjectDelete(0,obj_name) ) Print("Error deleting <" + obj_name + "> from Chart") ;
    		}
    	}
    	
    	
    	if(reason == REASON_INITFAILED)
        {
          int subwindows = (int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
          for(int i=subwindows;i>=0;i--)
            {
             int indicators=ChartIndicatorsTotal(0,i);
             ChartIndicatorDelete(0,i, shortname);
            }
    	   
        }
   	
        ChartRedraw();
        
   }

}  
  



And the OnCalculate event:

//+------------------------------------------------------------------+
//| 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>=1)
   {    
        pos=prev_calculated-1;
   }

   else
   {
        pos = 1;
   }

        
//--- main cycle
        for(int i=pos; i<rates_total && !IsStopped(); i++)
        {
                // ... the logic of the indicator
        }


Important to say again: the same ARRAYS which the PANEL values are based, are the arrays utilized as BUFFERS for the lines.

What am I missing here?

I appreciate any help on it.

 

An addendum:

Debugging the code I can see that the OnCalculate event is being called 2 or 3 times while METATRADER5 is being loaded with "Prev_Calculated" = 0.

I don't understand this sequential calling without any "prev_calculated", nor why my loop does not handle it accordingly.  

 

Wow!  It was solved...

This is the normal behavior of INDICATORS, and due to a global variable, almost half of the code wasn't being executed.