Indicator correctly displayed in debug mode, but doesn't display any value when I add it below a chart

 

I try to create an indicator (i'm a beginner) and I have a problem to display my indicator below a chart.

In MetaEditor, when I press F5 to start the debug, a window opens with a chart and my indicator displays correctly below the chart (I see good values and the indicator is updated correctly at each bar). BUT then, when I want to add the indicator to a chart, it displays nothing... I don't understand why?? The lines "minimum" / "maximum" are displayed correctly, but the value of the indicator is not displayed...

The code of my indicator:

//+------------------------------------------------------------------+
//|                                                  SpreadRatio.mq4 |
//|                                                             jiyu |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "jiyu"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots  1
#property indicator_minimum -35
#property indicator_maximum 35

#property indicator_label1  "BufferRSI"
#property indicator_type1  DRAW_HISTOGRAM
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 3

double   BufferRSI[];
double   BufferRatio[];

extern string sym1   = "NDX100";
extern string sym2   = "SPX500";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   IndicatorBuffers(2);
   
   SetIndexBuffer(0, BufferRSI, INDICATOR_DATA );
   SetIndexBuffer(1, BufferRatio, INDICATOR_CALCULATIONS);
  
   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[])
  {
   
   int counted_bars = prev_calculated;
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
   int limit=fmin(rates_total-counted_bars,rates_total-1);   
       
   for(int i=limit; i>=0; i--)
   {
      BufferRatio[i] = iClose(sym1, 0, i) / iClose(sym2, 0, i);
   }
   
   for(int i=limit; i>=0; i--)
   {
      int period     = (rates_total - i) < 14 ? (rates_total - i) : 14;
      BufferRSI[i]   = iRSIOnArray(BufferRatio, 0, period, i) - 50;
   }
      
   return(rates_total);
  }
  
//+------------------------------------------------------------------+
Thanks in advance!!
 
for(int i=limit; i>=0; i--)
{
   if (iClose(sym2, 0, i) > 0)    
      BufferRatio[i] = iClose(sym1, 0, i) / iClose(sym2, 0, i);
}

A division by zero error has occurred.

 
  1.       BufferRatio[i] = iClose(sym1, 0, i) / iClose(sym2, 0, i);
    

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

  2.    if(counted_bars > 0) counted_bars--;
       int limit=fmin(rates_total-counted_bars,rates_total-1);   

    You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

 
Nagisa Unada #:

A division by zero error has occurred.

Thanks! My indicator is ok now! :) (I didn't think to check the division by zero...)


William Roeder #:
  1. You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

Ok I will check this! I had written my indicator by looking at a lot of examples (maybe a bit old)