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:
| Pine | MQL5 | Note |
|---|---|---|
plot.style_line | DRAW_LINE | Default on both sides. |
plot.style_linebr | DRAW_LINE + EMPTY_VALUE | A line plot already breaks at empty values. |
plot.style_histogram, style_columns | DRAW_HISTOGRAM | Always drawn from zero; histbase has no equivalent. |
plot.style_circles | DRAW_ARROW, PLOT_ARROW 159 | Wingdings dot; join=true is lost. |
plot.style_cross | DRAW_ARROW, PLOT_ARROW 251 | Wingdings cross glyph. |
plot.style_area, style_stepline | none | Area: DRAW_FILLING against a zero buffer. Step line: no draw type. |
per-bar color | DRAW_COLOR_LINE + colour-index buffer | Second buffer declared INDICATOR_COLOR_INDEX; colours listed in indicator_colorN. |
offset=n | PLOT_SHIFT = n | Same sign: positive moves right. |
display.none | DRAW_NONE, PLOT_SHOW_DATA false | Buffer still readable through iCustom. |
Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Missing value | na leaves a gap | EMPTY_VALUE (DBL_MAX) by default; PLOT_EMPTY_VALUE can redefine it, often to 0 for histograms |
| Warm-up | Early bars are na automatically | You write EMPTY_VALUE yourself, or set PLOT_DRAW_BEGIN so the first bars are not drawn |
| Index direction | Bar 0 is the current bar, series[1] the previous | OnCalculate arrays are oldest-first; the newest bar is rates_total - 1 unless you call ArraySetAsSeries |
| Realtime bar | Recalculated on each tick; history is fixed | Same when you start the loop at prev_calculated - 1; starting at prev_calculated freezes the forming bar |
| Where it may be called | Global scope only | Buffer writes can happen anywhere, including inside loops and functions |
| Colour per bar | Any series colour | Up to 64 preset colours selected by index |
| Window | overlay=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
0where Pine hadna. The line drops to zero instead of breaking. UseEMPTY_VALUE, and keepPLOT_EMPTY_VALUEconsistent with it. - Declaring
indicator_bufferssmaller than the number ofSetIndexBuffercalls. The extra buffers are silently not drawn; the terminal shows nothing for those plots. - Starting the loop at
prev_calculatedinstead ofprev_calculated - 1. The forming bar keeps the value of its first tick, so the MT5 plot lags TradingView by one bar in realtime. - Expecting
histbaseto work.DRAW_HISTOGRAMalways 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_LINEneeds two buffers in theindicator_bufferscount but only one inindicator_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