Pine plot() to MQL5 indicator buffers and colours
Short answer
One Pine plot() becomes a declared plot, one or more indicator buffers, a draw type and an empty value, wired up in OnInit. indicator_plots counts the things drawn and indicator_buffers the arrays behind them, which is usually more, because some draw types need several buffers.
In Pine Script one plot() call is enough to draw a line. In MQL5 the same line needs a declared plot, one or more indicator buffers, a draw type and an empty value, all wired up in OnInit. This guide is for developers moving a TradingView indicator to MetaTrader 5 who want to map every plot(), plotshape() and colour argument to the right buffer by hand.
The model: plots own buffers
An MQL5 indicator declares two counts. indicator_plots is the number of things drawn (each Pine plot(), plotshape() or fill() is one). indicator_buffers is the number of double arrays behind them, and it is usually larger, because some draw types need more than one array. Buffers are bound in order with SetIndexBuffer: plot 0 takes the first buffers, plot 1 the next ones, and any calculation-only buffers (INDICATOR_CALCULATIONS) come after all drawn ones.
Properties such as PLOT_LINE_COLOR and PLOT_ARROW are addressed by plot index, while SetIndexBuffer uses the buffer index. Mixing the two up is the most common reason a converted indicator draws the wrong line in the wrong colour.
| Pine Script v6 | MQL5 draw type | Buffers |
|---|---|---|
plot(x) | DRAW_LINE | 1 |
plot(x, color = cond ? a : b) | DRAW_COLOR_LINE | 2 (data + colour index) |
plot(x, style = plot.style_columns) / style_histogram | DRAW_HISTOGRAM / DRAW_COLOR_HISTOGRAM | 1 / 2 |
plot(x, style = plot.style_circles) | DRAW_ARROW with glyph 159 | 1 |
plotshape(), plotchar(), plotarrow() | DRAW_ARROW | 1 |
plotcandle(o, h, l, c) | DRAW_CANDLES | 4 |
fill(p1, p2) | DRAW_FILLING | 2 |
A worked example
This Pine indicator draws a 20-bar SMA that is teal when price closes above it and red below, and marks bullish crosses with a triangle under the bar.
//@version=6
indicator("SMA trend", overlay = true)
len = input.int(20, "Length", minval = 1)
s = ta.sma(close, len)
plot(s, "SMA", color = close > s ? color.teal : color.red, linewidth = 2)
plotshape(ta.crossover(close, s), "Cross up", shape.triangleup, location.belowbar, color.teal)The MQL5 version needs two plots and three buffers: the SMA values, the SMA colour index, and the marker prices.
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 2
#property indicator_label1 "SMA"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrTeal,clrRed
#property indicator_width1 2
#property indicator_label2 "Cross up"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrTeal
input int InpLen = 20; // Length
double SmaBuf[];
double SmaColor[];
double CrossBuf[];
int OnInit()
{
if(InpLen < 1) return INIT_PARAMETERS_INCORRECT;
SetIndexBuffer(0, SmaBuf, INDICATOR_DATA);
SetIndexBuffer(1, SmaColor, INDICATOR_COLOR_INDEX);
SetIndexBuffer(2, CrossBuf, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(1, PLOT_ARROW, 233); // Wingdings up arrow
PlotIndexSetInteger(1, PLOT_ARROW_SHIFT, 10); // pixels below the price
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++)
{
SmaBuf[i] = EMPTY_VALUE; // Pine: na
CrossBuf[i] = EMPTY_VALUE;
SmaColor[i] = 0;
if(i < InpLen - 1) continue;
double sum = 0.0;
for(int k = 0; k < InpLen; k++) sum += close[i - k];
SmaBuf[i] = sum / InpLen;
SmaColor[i] = close[i] > SmaBuf[i] ? 0 : 1; // index into indicator_color1
if(i > 0 && SmaBuf[i - 1] != EMPTY_VALUE
&& close[i] > SmaBuf[i] && close[i - 1] <= SmaBuf[i - 1])
CrossBuf[i] = low[i];
}
return rates_total;
}Things to notice. Every bar first writes EMPTY_VALUE, which is what Pine's na becomes once PLOT_EMPTY_VALUE is set to it; without that line MetaTrader draws a value of 0 and the line dives to the bottom of the chart during warm-up. The arrays are left in their default MQL5 direction, so index 0 is the oldest bar and i - k looks back in time, like close[k] in Pine. The crossover test repeats ta.crossover's definition: above now, at or below on the previous bar.
Colours that change per bar
A constant colour is a single setting: #property indicator_colorN or PlotIndexSetInteger(plot, PLOT_LINE_COLOR, clr). A colour that depends on the bar needs a DRAW_COLOR_* type and a second buffer registered as INDICATOR_COLOR_INDEX, placed straight after the data buffer. That buffer does not hold colours; it holds an index into the plot's palette, which you declare as a comma-separated list in indicator_colorN or set entry by entry with PlotIndexSetInteger(plot, PLOT_LINE_COLOR, index, clr). A plot can have up to 64 palette entries.
So a Pine ternary close > s ? color.teal : color.red becomes two palette entries and a per-bar 0 or 1. When the Pine colour is computed (a gradient, color.from_gradient, or color.rgb from data), you cannot list the palette in advance: build it at run time, adding each new colour to the palette with PLOT_COLOR_INDEXES and PLOT_LINE_COLOR the first time it appears, and store its index.
Transparency does not carry over. color.new(color.red, 80) is an RGB colour plus 80% transparency in Pine; an MQL5 buffer colour is plain RGB and is drawn opaque. Pick a lighter or darker shade by hand if the faded look matters.
plotshape, plotchar and plotarrow
All three become a DRAW_ARROW plot: a buffer that holds a price where a marker should appear and EMPTY_VALUE everywhere else. The marker is a character from the Wingdings font, chosen with PLOT_ARROW (233 and 234 are up and down arrows, 159 a dot, 241 and 242 outlined arrows). location.belowbar and location.abovebar translate to writing low[i] or high[i] and nudging the glyph with PLOT_ARROW_SHIFT in pixels (positive moves it down). location.absolute writes the series value itself.
Two things have no buffer equivalent. Wingdings has no letters, so text = "Buy" on a shape, or plotchar(cond, char = "B") where the letter matters, needs an OBJ_TEXT chart object per marker (see the drawings guide below). And plotarrow() draws up arrows for positive values and down arrows for negative ones; one DRAW_ARROW plot has one glyph, so a series with both signs needs two plots.
fill, hline, offset and display
The remaining plot features each have a direct setting:
// fill(p1, p2, color) -> one DRAW_FILLING plot over two buffers
#property indicator_type3 DRAW_FILLING
#property indicator_color3 clrLightSteelBlue,clrMistyRose // fill when buf1 > buf2, and when below
SetIndexBuffer(3, UpperBuf, INDICATOR_DATA);
SetIndexBuffer(4, LowerBuf, INDICATOR_DATA);
// hline(70) in a separate window -> an indicator level, no buffer needed
IndicatorSetInteger(INDICATOR_LEVELS, 1);
IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, 70.0);
// plot(x, offset = 3) -> shift the plot 3 bars to the right
PlotIndexSetInteger(0, PLOT_SHIFT, 3);
// plot(x, display = display.none) -> computed but not drawn or listed
PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_NONE);
PlotIndexSetInteger(0, PLOT_SHOW_DATA, false); // hide it from the Data Window tooDRAW_FILLING uses the first colour where the first buffer is above the second and the second colour otherwise, which is handy for a Pine fill whose colour flips with the order of the two lines. MQL4 has no DRAW_FILLING; there you need two histogram buffers or chart objects.
What Stratlate does with plots
The converter applies the same mapping. Each plot() becomes an MQL5 plot with PLOT_EMPTY_VALUE set to EMPTY_VALUE; a colour that changes per bar gets a DRAW_COLOR_LINE, DRAW_COLOR_HISTOGRAM or DRAW_COLOR_ARROW plot with an INDICATOR_COLOR_INDEX buffer whose palette is filled at run time (up to the 64-entry limit). plotshape, plotchar and plotarrow become DRAW_ARROW with a Wingdings glyph close to the Pine shape, and a shape's text is drawn as chart text. plotcandle becomes DRAW_CANDLES, fill becomes DRAW_FILLING, offset becomes PLOT_SHIFT, and display.none becomes DRAW_NONE. A plotarrow whose sign can change is reported as an approximation. Each conversion is compared bar by bar against the original by a differential verifier, but you should still compile the result and check it on a chart.
Pitfalls
- Plot index used where a buffer index is expected. With a colour line as plot 0, the marker plot is plot 1 but buffer 2. Symptom: the arrow settings land on the wrong plot, or a line appears made of dots.
- Colour index buffer not directly after its data buffer. Symptom: the line is drawn in one colour, or the colour indexes are plotted as a line near zero.
indicator_bufferstoo small. A colour line counts two, a candle plot four, a fill two. Symptom:SetIndexBufferfails for the extra buffers and those plots never appear.- No empty value. Forgetting
PLOT_EMPTY_VALUEor writing 0 forna. Symptom: lines that drop to zero during warm-up, and markers on every bar. - Series indexing switched on. Calling
ArraySetAsSeries(buf, true)and then writing with an oldest-first loop index. Symptom: the plot is mirrored left to right. - Marker buffer left over from the previous tick. The live bar is recalculated on each tick; if the condition turns false, the buffer must be reset to
EMPTY_VALUE. Symptom: an arrow that stays although TradingView removed it.
FAQ
How many buffers does a Pine plot need in MQL5?
A plain line needs one buffer. A line, histogram or arrow whose colour changes per bar needs two (data plus a colour index buffer), a fill needs two and a plotcandle needs four (five with a colour index). Count them all in indicator_buffers.
How do I convert plotshape() to MQL5?
Use a DRAW_ARROW plot: write the bar's high or low into the buffer where the condition is true and EMPTY_VALUE elsewhere, then choose a Wingdings glyph with PLOT_ARROW. Text on the shape needs a separate OBJ_TEXT chart object.
Why does my converted line drop to zero at the start?
The buffer holds 0 where Pine had na. Write EMPTY_VALUE on those bars and set PLOT_EMPTY_VALUE to EMPTY_VALUE in OnInit so MetaTrader leaves them blank.
Convert the plots automatically
Paste a Pine Script indicator and get an MQL5 file with its plots, colour buffers and markers already wired up, plus a report of anything approximated.
Open the converter