plot() in MQL5: indicator buffers, draw types and offsets

Short answer

One Pine plot() becomes one double buffer registered with SetIndexBuffer() plus one plot description in #property indicator_typeN, indicator_colorN and indicator_widthN, or PlotIndexSetInteger() in OnInit(). plot's offset becomes PLOT_SHIFT.

plot() draws a series on the chart and, in Pine, that one call also declares the output that other scripts and alerts can read. MQL5 splits the same job in two: a double array registered with SetIndexBuffer() carries the values, and a set of #property indicator_* lines (or PlotIndexSetInteger() calls in OnInit()) describe how the plot is drawn. One Pine plot() is one buffer plus one plot description, and the arguments of plot() map almost one to one onto plot properties.

What plot() computes

plot(series, title, color, linewidth, style, trackprice, histbase, offset, join, editable, show_last, display) evaluates series on every bar, including the realtime bar on every tick, and records the value against that bar. A value of na produces a gap. style picks the renderer: plot.style_line, style_linebr (a line that breaks at na), style_stepline, style_histogram and style_columns (bars from histbase, default 0), style_circles, style_cross and style_area. offset moves the drawn series left (negative) or right (positive) by whole bars without changing the data. display controls where the plot appears: display.all, display.none (computed, invisible, still readable by other scripts), display.data_window and so on. color may be a per-bar series, so one plot can change colour bar by bar. A script may have at most 64 plot outputs, and plot() cannot be called inside a function or an if block.

The MQL5 equivalent

The Pine version plots RSI as a line, RSI minus 50 as a histogram and a hidden copy shifted two bars to the right.

//@version=6
indicator("Plot demo", overlay=false)
len = input.int(14, "Length")
r = ta.rsi(close, len)
plot(r, "RSI", color=color.blue, linewidth=2)
plot(r - 50, "Hist", style=plot.style_histogram, color=color.gray)
plot(r, "Shifted", offset=2, display=display.none)

In MQL5 each plot is a buffer plus a block of properties. indicator_typeN replaces style, indicator_widthN replaces linewidth, PLOT_SHIFT replaces offset, and display.none becomes DRAW_NONE with PLOT_SHOW_DATA switched off. Buffers are indexed oldest-first here (no ArraySetAsSeries), so bar i of the buffer is bar i of the input arrays.

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3
#property indicator_label1  "RSI"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_width1  2
#property indicator_label2  "RSI - 50"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrGray
#property indicator_width2  1
#property indicator_label3  "RSI shifted"
#property indicator_type3   DRAW_NONE

input int InpLength = 14;   // Length

double BufRsi[], BufHist[], BufShift[];
int    hRsi;

int OnInit()
{
   SetIndexBuffer(0, BufRsi,   INDICATOR_DATA);
   SetIndexBuffer(1, BufHist,  INDICATOR_DATA);
   SetIndexBuffer(2, BufShift, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(2, PLOT_SHIFT, 2);           // plot(..., offset=2)
   PlotIndexSetInteger(2, PLOT_SHOW_DATA, false);   // display=display.none
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpLength);
   PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, InpLength);
   hRsi = iRSI(_Symbol, _Period, InpLength, PRICE_CLOSE);
   return hRsi == INVALID_HANDLE ? INIT_FAILED : 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(BarsCalculated(hRsi) < rates_total)
      return 0;
   int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
   int count = rates_total - start;
   if(CopyBuffer(hRsi, 0, 0, count, BufRsi) < 0)   // oldest-first, like the buffers
      return 0;
   // CopyBuffer writes to the END of a non-series buffer, so fill the rest from there
   for(int i = start; i < rates_total; i++)
     {
      BufHist[i]  = i < InpLength ? EMPTY_VALUE : BufRsi[i] - 50.0;
      BufShift[i] = BufRsi[i];
     }
   return rates_total;
}

The style mapping, argument by argument:

PineMQL5Note
plot.style_lineDRAW_LINEDefault on both sides.
plot.style_linebrDRAW_LINE + EMPTY_VALUEA line plot already breaks at empty values.
plot.style_histogram, style_columnsDRAW_HISTOGRAMAlways drawn from zero; histbase has no equivalent.
plot.style_circlesDRAW_ARROW, PLOT_ARROW 159Wingdings dot; join=true is lost.
plot.style_crossDRAW_ARROW, PLOT_ARROW 251Wingdings cross glyph.
plot.style_area, style_steplinenoneArea: DRAW_FILLING against a zero buffer. Step line: no draw type.
per-bar colorDRAW_COLOR_LINE + colour-index bufferSecond buffer declared INDICATOR_COLOR_INDEX; colours listed in indicator_colorN.
offset=nPLOT_SHIFT = nSame sign: positive moves right.
display.noneDRAW_NONE, PLOT_SHOW_DATA falseBuffer still readable through iCustom.

Where the two differ

TopicPine ScriptMQL5
Missing valuena leaves a gapEMPTY_VALUE (DBL_MAX) by default; PLOT_EMPTY_VALUE can redefine it, often to 0 for histograms
Warm-upEarly bars are na automaticallyYou write EMPTY_VALUE yourself, or set PLOT_DRAW_BEGIN so the first bars are not drawn
Index directionBar 0 is the current bar, series[1] the previousOnCalculate arrays are oldest-first; the newest bar is rates_total - 1 unless you call ArraySetAsSeries
Realtime barRecalculated on each tick; history is fixedSame when you start the loop at prev_calculated - 1; starting at prev_calculated freezes the forming bar
Where it may be calledGlobal scope onlyBuffer writes can happen anywhere, including inside loops and functions
Colour per barAny series colourUp to 64 preset colours selected by index
Windowoverlay=true in indicator()#property indicator_chart_window or indicator_separate_window

What the Stratlate converter emits

Each plot() becomes one double buffer written at Buf[__pineBar] in OnCalculate, with #property indicator_typeN chosen from the style: DRAW_LINE (or DRAW_COLOR_LINE with a colour-index buffer when the colour changes per bar), DRAW_HISTOGRAM or DRAW_COLOR_HISTOGRAM for style_columns and style_histogram, and DRAW_ARROW with glyph 159 for style_circles. A constant colour is applied through PLOT_LINE_COLOR, linewidth through indicator_widthN, offset through PLOT_SHIFT, and display.none through DRAW_NONE plus PLOT_SHOW_DATA false. A non-zero histbase, style_area, style_stepline and join=true on circles are reported as approximations in the report (codes PM298, PM295, PM296); the values are still exact, only the look changes. Compile and backtest the result as you would any indicator.

Pitfalls

  • Writing 0 where Pine had na. The line drops to zero instead of breaking. Use EMPTY_VALUE, and keep PLOT_EMPTY_VALUE consistent with it.
  • Declaring indicator_buffers smaller than the number of SetIndexBuffer calls. The extra buffers are silently not drawn; the terminal shows nothing for those plots.
  • Starting the loop at prev_calculated instead of prev_calculated - 1. The forming bar keeps the value of its first tick, so the MT5 plot lags TradingView by one bar in realtime.
  • Expecting histbase to work. DRAW_HISTOGRAM always draws from zero; subtract the base from the series yourself.
  • Calling ArraySetAsSeries(close, true) on the inputs and then indexing your buffer oldest-first. Half the arrays are reversed and the plot is mirrored in time.
  • Forgetting that a DRAW_COLOR_LINE needs two buffers in the indicator_buffers count but only one in indicator_plots.

FAQ

Does PLOT_SHIFT move the data or only the drawing?

Only the drawing, exactly like Pine's offset. The buffer at index i still holds the value computed for bar i; iCustom and the Data Window read the unshifted values.

How do I reproduce plot(..., display=display.none)?

Give the buffer a plot with indicator_typeN DRAW_NONE and set PlotIndexSetInteger(N, PLOT_SHOW_DATA, false). The values stay available to other indicators and EAs through CopyBuffer, which is what display.none is for in Pine.

Is there a limit on the number of plots?

Pine stops at 64 plot outputs per script. MQL5 has no comparable per-indicator limit that you will reach in practice, but every buffer costs memory per bar, so a converted script with many hidden plots is better served by keeping only the buffers that are drawn or read.

Convert the whole script

Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.

Open the converter

Related