Journal reports custom indicator removed, while it still updates with candles

 

I've written this simple custom indicator, when I change Timeframe, the terminal log shows it loaded successfully, then immediately removed.

while the changes on the current bar (most recent bar in chart) still updates the indicator.

why it's so ?


#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_width1  3
#property indicator_color1  LimeGreen

input int Backsteps = 3;    // Baksteps Period
int BackS=1;
double  VAL_Buffer[];


int OnInit()
{
   if(Backsteps<1) printf("invalid input. [Baksteps Period = %d]. Corrected value : %d.", Backsteps, BackS); else BackS = Backsteps;
   SetIndexBuffer(0, VAL_Buffer, INDICATOR_DATA);
   IndicatorSetString(INDICATOR_SHORTNAME, "uDF("+string(BackS)+")");
   PlotIndexSetString(0, PLOT_LABEL, "uDF("+string(BackS)+")");
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN, BackS+1);
   IndicatorSetInteger(INDICATOR_DIGITS, 2);
   IndicatorSetDouble(INDICATOR_MINIMUM,-1);
   IndicatorSetDouble(INDICATOR_MAXIMUM, 1);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   return(INIT_SUCCEEDED);
 }

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(rates_total<=BackS+1) return(0); // not enough bars for calculation
        int i, start;
        if(prev_calculated==0)
        {
           for(i=0; i<=BackS && !_StopFlag; i++) VAL_Buffer[i]=0.0;
           start = BackS+1;
        }
        else start = prev_calculated-1;
        
        for(i=start; i<rates_total && !_StopFlag; i++)
        {
                double P_h=0, P_l=DBL_MAX, Val=0;
                for(int b=0; b<BackS; b++)
                {
                        P_h = fmax(high[i-b], P_h);
                        P_l = fmin(low[i-b], P_l);
                }
                VAL_Buffer[i] = (P_h-P_l>FLT_EPSILON)? (close[i]-open[i-(BackS-1)])/(P_h-P_l) : 0;
        }
   return(rates_total);
}
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
  • www.mql5.com
MetaQuotes Programming Language 5 (MQL5), included in MetaTrader 5 Client Terminal, has many new possibilities and higher performance, compared to MQL4. This article will help you to get acquainted with this new programming language. The simple examples of how to write an Expert Advisor and Custom Indicator are presented in this article. We will also consider some details of MQL5 language, that are necessary to understand these examples.