plotcandle() and plotbar() in MQL5: DRAW_CANDLES, 4 buffers
Short answer
plotcandle() becomes a DRAW_CANDLES plot over four buffers in open, high, low, close order, and plotbar() becomes DRAW_BARS. The DRAW_COLOR_* variants add a fifth buffer of colour indexes, so one plotcandle costs four or five buffers but only one plot.
plotcandle() draws a synthetic candle from four series, and plotbar() draws the same data as an OHLC bar. Heikin Ashi, renko-style smoothing and "trend candles" that recolour price all use them. MQL5 has both as plot types: DRAW_CANDLES and DRAW_BARS read four consecutive indicator buffers in open, high, low, close order, and their DRAW_COLOR_* variants add a fifth buffer holding a colour index per bar. One plotcandle therefore costs four (or five) buffers but only one plot.
What plotcandle() computes
plotcandle(open, high, low, close, title, color, wickcolor, editable, show_last, bordercolor, display) takes four series and draws, on every bar, a candle whose body runs from open to close and whose wick runs from low to high. If any of the four is na on a bar, no candle is drawn there. color may be a per-bar series and fills the body; wickcolor and bordercolor colour the wick and the outline, and when they are left out they follow color. plotbar(open, high, low, close, title, color, ...) has the same first four arguments and one colour. Both count as one plot output and must be called at global scope. The series are plain numbers: nothing forces high to be the largest of the four, so a script that computes them wrongly draws odd shapes on both platforms.
The MQL5 equivalent
The Pine version computes Heikin Ashi candles; the open is seeded from the first bar's real open and close and then carried forward through haOpen[1].
//@version=6
indicator("HA demo", overlay=true)
haClose = (open + high + low + close) / 4
haOpen = float(na)
haOpen := na(haOpen[1]) ? (open + close) / 2 : (haOpen[1] + haClose[1]) / 2
haHigh = math.max(high, haOpen, haClose)
haLow = math.min(low, haOpen, haClose)
plotcandle(haOpen, haHigh, haLow, haClose, "HA", color=haClose >= haOpen ? color.teal : color.red)The MQL5 version uses DRAW_COLOR_CANDLES: four data buffers in O, H, L, C order followed by one INDICATOR_COLOR_INDEX buffer, whose value selects one of the colours listed in indicator_color1. The label line uses semicolons to name the four values in the Data Window. Because the buffers are oldest-first, BufO[i - 1] is the previous bar's Heikin Ashi open, the same thing haOpen[1] means in Pine.
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 1
#property indicator_label1 "HA open;HA high;HA low;HA close"
#property indicator_type1 DRAW_COLOR_CANDLES
#property indicator_color1 clrTeal, clrRed
#property indicator_width1 1
double BufO[], BufH[], BufL[], BufC[], BufClr[];
int OnInit()
{
SetIndexBuffer(0, BufO, INDICATOR_DATA);
SetIndexBuffer(1, BufH, INDICATOR_DATA);
SetIndexBuffer(2, BufL, INDICATOR_DATA);
SetIndexBuffer(3, BufC, INDICATOR_DATA);
SetIndexBuffer(4, BufClr, INDICATOR_COLOR_INDEX);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
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++)
{
double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0;
double haOpen = i == 0 ? (open[i] + close[i]) / 2.0 : (BufO[i - 1] + BufC[i - 1]) / 2.0;
BufO[i] = haOpen;
BufC[i] = haClose;
BufH[i] = MathMax(high[i], MathMax(haOpen, haClose));
BufL[i] = MathMin(low[i], MathMin(haOpen, haClose));
BufClr[i] = haClose >= haOpen ? 0 : 1; // index into indicator_color1
}
return rates_total;
}For plotbar() change indicator_type1 to DRAW_BARS (or DRAW_COLOR_BARS); the buffer layout and the colour index are identical. With a plain DRAW_CANDLES and a constant colour you drop the fifth buffer and set indicator_buffers 4. DRAW_CANDLES also accepts up to three colours in indicator_color1: with one colour the whole candle uses it, with two the first colours the outline and wick and the second the body, and with three the second and third colour bullish and bearish bodies, which is the closest match for Pine's bordercolor and wickcolor.
Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Missing candle | Any of the four series na | EMPTY_VALUE in any of the four buffers |
| Per-bar colour | color series, unlimited | Colour index buffer; up to 64 preset colours in indicator_color1 |
| Wick and border colours | Separate per-bar series | Second and third colour of the plot, static |
| Buffer count | One plot output | 4 or 5 buffers, 1 plot; indicator_buffers must count them all |
| Order | Argument order O, H, L, C | Buffer order O, H, L, C; swapping H and L draws inverted wicks |
| Seeding a recursive open | na(haOpen[1]) test on bar 0 | i == 0 branch; the buffer has no history before index 0 |
| Realtime bar | Recomputed each tick | Same if the loop restarts at prev_calculated - 1; the previous bar's values are final |
| Hiding the real candles | Users hide the symbol's candles in chart settings | The same; or set the chart's candle colours to the background |
What the Stratlate converter emits
plotcandle() becomes a DRAW_CANDLES plot over four consecutive buffers written as open, high, low, close in that order, with #property indicator_type1 DRAW_CANDLES and PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_CANDLES) in OnInit. plotbar() is lowered to the same plot kind, so bars come out as candles rather than DRAW_BARS. In our test the per-bar color ternary was not carried into a colour-index buffer and no note was raised for it, so a converted Heikin Ashi draws all candles in one colour until you add the fifth buffer by hand as shown above; wickcolor and bordercolor are likewise not applied. The values themselves are exact and the conversion is graded CLEAN; compile it and compare a few candles against TradingView before you trust the colouring.
Pitfalls
- Declaring
indicator_buffers 4while binding a fifth colour buffer. The colour buffer is ignored and every candle gets colour 0. - Binding the colour buffer as
INDICATOR_DATA. MetaTrader reads it as the close of a second plot and the chart shows garbage values in the Data Window. - Putting the buffers in the order the Pine variables were declared (close first in many Heikin Ashi scripts).
DRAW_CANDLESdoes not know the names, only the order: open, high, low, close. - Reading
BufO[i - 1]wheniis 0. Seed the first bar explicitly; the array has no element before index 0. - Leaving the real candles visible underneath. On both platforms the synthetic candles are drawn over the symbol's candles, and the user has to hide the originals in the chart settings.
FAQ
Why does my converted plotcandle draw one colour?
A per-bar colour needs DRAW_COLOR_CANDLES and a fifth buffer declared INDICATOR_COLOR_INDEX. Write 0 or 1 into it on each bar and list the two colours in indicator_color1; a plain DRAW_CANDLES has no per-bar colour at all.
Can I draw plotcandle in a separate window?
Yes. DRAW_CANDLES works in a separate window with its own scale, which is how oscillator candles (for example an RSI drawn as Heikin Ashi) are shown. Set indicator_separate_window and keep the four buffers in the same order.
How many buffers does MetaTrader allow?
Enough for any script that compiles in Pine: TradingView stops at 64 plot outputs, and a plotcandle is one of them there but four or five buffers in MQL5. Count the buffers, not the plots, when you fill in indicator_buffers.
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