Converted MQL5 indicator shows nothing on the chart: checklist

Short answer

First check the Experts tab of the Toolbox for a runtime error such as array out of range. If there is none, the values exist but the plot setup hides them: check that the buffer and plot counts, SetIndexBuffer, EMPTY_VALUE, DRAW_BEGIN and the window all agree.

The indicator compiles without errors, appears in the chart's indicator list, and draws nothing: no line, no histogram, sometimes not even a value in the Data Window. MetaTrader 5 does not show a dialog for this. When the cause is a runtime error, the only report is one line in the Experts tab of the Toolbox, such as array out of range in 'nothing_d.mq5' (20,11). When there is no error at all, the indicator is computing values and something in the plot setup hides them. This checklist covers both, in the order that finds the cause fastest.

Why it happens when converting Pine Script

In Pine, plot(x) does everything: it declares the output, picks the window from overlay=, treats na as "draw nothing", and starts drawing wherever the values start. A coloured line is still one call, plot(x, color = cond ? color.green : color.red).

An MQL5 indicator spreads the same job over five places that must agree: #property indicator_buffers (how many arrays the terminal allocates), #property indicator_plots (how many things are drawn), #property indicator_typeN (how each plot is drawn), SetIndexBuffer (which array is which buffer, and its role) and PlotIndexSet* (empty value, draw begin, colours). A hand port that gets one count wrong compiles, because these are checked only when the indicator runs. The usual mismatch comes from Pine's coloured plot: in MQL5 it is a DRAW_COLOR_LINE, which needs two buffers, one for values and one for colour indexes, so the buffer count is no longer the plot count.

Fix

A port of plot(close, color = close > open ? color.green : color.red) that compiles in MetaEditor 5 without warnings and draws nothing:

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrGreen, clrRed
double Line[], Clr[];
int OnInit()
{
   if(!SetIndexBuffer(0, Line, INDICATOR_DATA)) Print("SetIndexBuffer 0 failed ", GetLastError());
   if(!SetIndexBuffer(1, Clr, INDICATOR_COLOR_INDEX)) Print("SetIndexBuffer 1 failed ", GetLastError());
   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[])
{
   for(int i = prev_calculated > 0 ? prev_calculated - 1 : 0; i < rates_total; i++)
   {
      Line[i] = close[i];
      Clr[i] = close[i] > open[i] ? 0 : 1;
   }
   return rates_total;
}

Run in MetaTrader 5 (build 6140), it writes two lines to the Experts journal and stops:

SetIndexBuffer 1 failed 4602
array out of range in 'nothing_d.mq5' (20,11)

The terminal allocated one buffer because indicator_buffers said 1, so binding a second one failed (error 4602, a wrong buffer index), the colour array was never sized, and the first write to it on line 20 stopped OnCalculate. Two other common versions of the same mistake gave the same journal message in the same test: an indicator with indicator_plots 2 but indicator_buffers 1 (array out of range at the write to the second buffer), and one that declared its buffer but never called SetIndexBuffer (array out of range at the first write). An indicator with indicator_buffers 0 and indicator_plots 1 raised no error at all and returned no data.

A correct version with a two-colour line and a second, conditional line (plot(close < ma ? ma : na) in Pine). It compiles without warnings and draws both:

#property indicator_chart_window
// Buffers = every array bound with SetIndexBuffer (data + colour index + calculations).
// Plots   = what is drawn. A DRAW_COLOR_LINE plot uses TWO buffers: values and colour index.
#property indicator_buffers 3
#property indicator_plots   2

#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrSeaGreen, clrCrimson
#property indicator_width1  2
#property indicator_label1  "MA"

#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDodgerBlue
#property indicator_label2  "MA above close"

input int Length = 20;   // Pine: len = input.int(20)

double MaBuf[];          // plot 1, values
double MaClr[];          // plot 1, colour index (0 = first colour, 1 = second)
double AboveBuf[];       // plot 2, Pine: plot(close > ma ? ma : na)

int OnInit()
{
   // Bind in plot order: a plot's value buffer, then its colour buffer, then the next plot.
   SetIndexBuffer(0, MaBuf,    INDICATOR_DATA);
   SetIndexBuffer(1, MaClr,    INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2, AboveBuf, INDICATOR_DATA);

   // Pine's na: tell each plot which value means "draw nothing".
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   // The first Length - 1 bars have no value in Pine either.
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, Length - 1);
   PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, Length - 1);
   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[])
{
   int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
   for(int i = start; i < rates_total; i++)
   {
      if(i < Length - 1)
      {
         MaBuf[i] = EMPTY_VALUE;
         MaClr[i] = 0;
         AboveBuf[i] = EMPTY_VALUE;
         continue;
      }
      double sum = 0.0;
      for(int k = 0; k < Length; k++)
         sum += close[i - k];
      double ma = sum / Length;

      MaBuf[i]    = ma;
      MaClr[i]    = close[i] >= ma ? 0 : 1;
      AboveBuf[i] = close[i] < ma ? ma : EMPTY_VALUE;   // every bar written, na included
   }
   return rates_total;
}

The checklist

  1. Read the Experts tab first. An array out of range line almost always means a buffer that was not bound or a history read before bar 0. Fix that before anything else; until OnCalculate completes, nothing is drawn. The array out of range guide covers the history case.
  2. Count buffers and plots. indicator_buffers = data buffers + colour-index buffers + calculation buffers. indicator_plots = lines, histograms and arrows drawn. DRAW_COLOR_LINE and DRAW_COLOR_HISTOGRAM take two buffers, DRAW_FILLING two, DRAW_CANDLES four, DRAW_COLOR_CANDLES five.
  3. Bind every buffer, in order. SetIndexBuffer(index, array, role) for indexes 0 to indicator_buffers - 1, with each plot's data buffer followed by its colour buffer, and calculation buffers last. Check the return value while debugging.
  4. Is there a draw type? Give every plot an explicit indicator_typeN. A plot set to DRAW_NONE is not drawn, though its values still appear in the Data Window; a colour of clrNONE has the same effect.
  5. Are the values in the visible range? Hover over a bar and open the Data Window (Ctrl+D). If the values are there but the line is not, the plot is in the wrong window: an oscillator from a Pine script with overlay=false needs #property indicator_separate_window; on the price chart it lands far outside the price range.
  6. Is "no value" really empty? Write EMPTY_VALUE on bars that Pine left as na, and declare it with PlotIndexSetDouble(n, PLOT_EMPTY_VALUE, EMPTY_VALUE). Writing 0 draws a line to zero; writing nothing leaves whatever the array held.
  7. Check PLOT_DRAW_BEGIN. It tells the terminal how many leading bars to skip. Setting it from rates_total, or from a length input with a large default, hides the whole plot on a short chart.
  8. Check the array direction. If you call ArraySetAsSeries(buf, true) on a buffer and fill it oldest-first, the values are drawn backwards: the plot appears flipped or only at the left edge.
  9. Does the loop reach the old bars? On the first call (prev_calculated == 0) the loop must start at bar 0. A start computed as rates_total - 1 fills only the newest bar, and one point is not a visible line. Returning early before the loop on the first call (a data check that always fails, for instance) leaves the buffers empty.

Other causes of the same message

  • A CopyBuffer from a handle that is not ready. Right after attaching, an iMA or iRSI handle may not have calculated yet and CopyBuffer returns -1. If you return rates_total in that case, the terminal thinks the work is done. Return 0 so the next tick retries.
  • Multi-timeframe data not loaded. An indicator that reads another timeframe with iClose or CopyRates gets nothing until the terminal downloads it, so the first run can draw nothing. The same retry rule applies.
  • Visibility settings. The Visualization tab of the indicator's properties can hide it on some timeframes, and the Colors tab can have the line set to None from an earlier session.
  • Converted code. In output from the Stratlate converter the declarations are derived from the Pine plots: indicator_buffers and indicator_plots are counted from them (a plot with a series colour becomes DRAW_COLOR_LINE with an INDICATOR_COLOR_INDEX buffer, so two buffers for one plot), every buffer is bound with SetIndexBuffer in OnInit, every plot gets PLOT_EMPTY_VALUE, and the window follows the script's overlay= argument. If a converted indicator draws nothing, look first at the Experts journal and then at the conversion report, which lists any plot or drawing that was not carried over instead of leaving it out silently.

Pitfalls

  • Debugging the formula first. When nothing is drawn, the plot setup is the cause far more often than the maths. Put a constant in the buffer (Buf[i] = close[i];) and get that drawn before checking values.
  • Fixing the counts but not the colour indexes. A colour-index buffer holds 0, 1, 2 ... into the plot's indicator_colorN list. Writing a color value such as clrGreen into it, as a Pine port of color= tends to, points past the list and the bar is not drawn in the colour you meant.
  • Adding a colour plot without updating the counts. Turning a DRAW_LINE into a DRAW_COLOR_LINE adds a buffer and shifts every buffer index after it. Renumber the SetIndexBuffer calls.
  • Plotting a calculation buffer. A buffer bound as INDICATOR_CALCULATIONS is never drawn, whatever its indicator_type says. Use INDICATOR_DATA for anything that should be visible.
  • Assuming the Data Window proves the plot works. Values in the Data Window only prove the buffer is bound and filled. The draw type, colour, window and empty value can still hide the line.

FAQ

Why does my MQL5 indicator compile but show nothing on the chart?

Usually a runtime error stops OnCalculate before the buffers are filled, and the only trace is a line in the Experts tab such as array out of range. The most common cause in ported code is indicator_buffers set lower than the buffers the code binds, often because a coloured plot needs a second buffer for its colour index.

How many buffers does a coloured line need in MQL5?

Two: one INDICATOR_DATA buffer with the values and one INDICATOR_COLOR_INDEX buffer holding, for each bar, the index of the colour to use from the plot's colour list. Both count towards indicator_buffers, while the line counts once towards indicator_plots.

What is the MQL5 equivalent of na in a plot?

EMPTY_VALUE written into the buffer on that bar, with PlotIndexSetDouble(plot, PLOT_EMPTY_VALUE, EMPTY_VALUE) in OnInit so the terminal treats it as a gap. Zero is a real value and is drawn.

Let the converter do the mechanical part

Paste the original script and get a conversion that compiles, with every construct that could not be carried over reported instead of hidden. Review, compile and backtest the result before trading.

Open the converter

Related