SetIndexBuffer in Pine Script: buffers become plot() calls

Short answer

A buffer becomes a series variable, and its binding plus #property indicator_typeN, colorN and widthN collapse into one plot(series, title, color, linewidth, style) call. A colour-index buffer becomes a colour expression, and EMPTY_VALUE becomes na.

SetIndexBuffer binds a global double array to one of the indicator's plot slots so MetaTrader draws it; the #property indicator_typeN, indicator_colorN, indicator_widthN lines and PlotIndexSetInteger / PlotIndexSetDouble calls say how. In Pine Script a buffer is just a series variable and all of that configuration collapses into one plot() call: plot(series, title, color, linewidth, style). The sentinel EMPTY_VALUE that hides a bar becomes na.

What SetIndexBuffer computes

It computes nothing; it wires. After SetIndexBuffer(0, Buf, INDICATOR_DATA) the array Buf is resized by the terminal to rates_total, indexed oldest-first by default, and whatever you store in Buf[i] inside OnCalculate is drawn at bar i with plot 0's style. The third argument selects the buffer's role: INDICATOR_DATA is drawn, INDICATOR_COLOR_INDEX holds an integer that selects a colour from the indicator_colorN list for the preceding data buffer (used by DRAW_COLOR_LINE, DRAW_COLOR_HISTOGRAM, DRAW_COLOR_CANDLES), and INDICATOR_CALCULATIONS is an invisible working buffer. #property indicator_buffers counts all three kinds, indicator_plots only the drawn ones. The drawing style (DRAW_LINE, DRAW_HISTOGRAM, DRAW_ARROW, DRAW_SECTION, DRAW_FILLING, DRAW_CANDLES) determines how many data buffers a plot consumes: two for a filling, four for candles.

In Pine the plot and the data are one statement. plot() takes the series and the style arguments; a dynamic colour is just a colour-typed series passed to color=; plotshape / plotchar cover DRAW_ARROW, fill() covers DRAW_FILLING and plotcandle covers DRAW_CANDLES. There is no slot numbering: plots appear in source order, and that order is also what the Data Window and the style dialog show.

The Pine Script equivalent

Two plots and three buffers: a momentum line with a warm-up gap, and a one-bar change histogram coloured by sign through a colour-index buffer.

//@version=6
indicator("Momentum and change", overlay = false)

len = input.int(14, "Momentum length")

// Buffer 0 -> a series. na where MQL5 wrote EMPTY_VALUE; close[len] is na on the first len bars anyway.
mom = bar_index < len ? na : close - close[len]

// Buffer 1 -> a series; buffer 2 (the colour index) -> a colour expression.
chg      = close - close[1]
chgColor = chg >= 0 ? color.rgb(50, 205, 50) : color.rgb(255, 99, 71)   // clrLimeGreen, clrTomato

// #property indicator_type / color / width / label all become plot() arguments.
plot(mom, title = "Momentum", color = color.rgb(30, 144, 255), linewidth = 2, style = plot.style_line)
plot(chg == 0 ? na : chg, title = "Change", color = chgColor, linewidth = 3, style = plot.style_histogram)
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   2
// plot 0: the oscillator line
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
#property indicator_label1  "Momentum"
// plot 1: a histogram whose colour comes from a colour-index buffer
#property indicator_type2   DRAW_COLOR_HISTOGRAM
#property indicator_color2  clrLimeGreen, clrTomato
#property indicator_width2  3
#property indicator_label2  "Change"

input int InpLen = 14;   // Momentum length

double MomBuffer[];
double ChangeBuffer[];
double ChangeColor[];

int OnInit()
{
   SetIndexBuffer(0, MomBuffer,  INDICATOR_DATA);
   SetIndexBuffer(1, ChangeBuffer, INDICATOR_DATA);
   SetIndexBuffer(2, ChangeColor,  INDICATOR_COLOR_INDEX);

   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);   // bars holding EMPTY_VALUE are not drawn
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpLen);        // and the first InpLen bars are skipped
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);           // for the histogram, 0 means "nothing"
   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) ? 1 : prev_calculated - 1;
   for(int i = start; i < rates_total; i++)
   {
      MomBuffer[i]  = (i < InpLen) ? EMPTY_VALUE : close[i] - close[i - InpLen];
      ChangeBuffer[i] = close[i] - close[i - 1];
      ChangeColor[i]  = (ChangeBuffer[i] >= 0.0) ? 0 : 1;   // index into indicator_color2
   }
   return(rates_total);
}
MQL5Pine
#property indicator_type1 DRAW_LINEstyle = plot.style_line (the default)
DRAW_HISTOGRAM / DRAW_COLOR_HISTOGRAMstyle = plot.style_histogram (or plot.style_columns for wide bars)
DRAW_ARROW + PLOT_ARROW codeplotshape(cond, style = shape.triangleup, location = …) or plotchar
DRAW_SECTIONplot.style_line on a series that is na between the points (Pine joins the points across na)
DRAW_FILLING (two buffers)fill(plot1, plot2, color)
indicator_color1 clrA, clrB + colour indexcolor = idx == 0 ? colorA : colorB
indicator_width1 2 / PLOT_LINE_WIDTHlinewidth = 2
indicator_style1 STYLE_DOT / PLOT_LINE_STYLENo dotted plot style; use plot.style_circles or plot.style_cross, or draw with line.new
indicator_label1 "X" / PLOT_LABELtitle = "X"
PLOT_EMPTY_VALUEna is always the empty value
PLOT_DRAW_BEGIN nbar_index < n ? na : value
PLOT_SHIFT noffset = n

Where the two differ

Working buffers. INDICATOR_CALCULATIONS buffers exist only to keep history of an intermediate value. Any Pine variable keeps its history, so they become ordinary assignments with no plot.

Plot count is fixed at compile time in both, but Pine also limits it: 64 plot-type calls per script (plot, plotshape, plotchar, fill, hline and bgcolor all count).

Colour index versus colour series. MQL5 colours a bar by an integer index into a fixed list the user can edit in the style dialog. Pine colours it by a colour value computed in code; the user can only override the whole plot colour unless you expose input.color for each.

Warm-up. MQL5 draws from PLOT_DRAW_BEGIN and treats EMPTY_VALUE (DBL_MAX) as a gap. Pine draws wherever the value is not na; most warm-up gaps appear by themselves because close[len] and ta.* outputs are na until enough bars exist.

Line breaks. A DRAW_LINE in MetaTrader breaks at EMPTY_VALUE bars, while DRAW_SECTION joins the non-empty points across them. Pine is the other way round by default: plot.style_line joins the points on either side of an na gap, and plot.style_linebr breaks the line. So DRAW_LINE with gaps translates to plot.style_linebr, and DRAW_SECTION to plot.style_line.

What the Stratlate converter emits

The MQL5 to Pine engine treats SetIndexBuffer as structural: each bound INDICATOR_DATA buffer is emitted as one plot() whose title is the buffer variable's name and whose colour comes from the matching #property indicator_colorN as color.rgb(r, g, b) when the colour name is recognised (some, such as clrSeaGreen in our tests, were not and the plot kept the default colour; the call is reported as absorbed, "the buffer is emitted as a plot()"). A draw style given through the MQL4-style SetIndexStyle call is carried (DRAW_HISTOGRAM as style=plot.style_histogram, DRAW_ARROW as plot.style_circles, DRAW_SECTION as plot.style_stepline, plus its width and colour); #property indicator_typeN is used to group multi-buffer plots such as candles and fillings, but in our MQL5 tests a single-buffer DRAW_HISTOGRAM or DRAW_ARROW declared only that way came out as a plain line, and indicator_widthN was not carried. PLOT_DRAW_BEGIN is honoured as a bar_index < n ? na : value wrapper and PLOT_EMPTY_VALUE as a mask of that value to na; both calls are reported as absorbed, while other PlotIndexSetInteger / PlotIndexSetDouble calls (width, style, arrow code) are reported as styling Pine sets at the call site and are not carried. For the program above the INDICATOR_COLOR_INDEX buffer came out as a hidden plot(ChangeColor, display=display.none) and the histogram as an uncoloured plot without the histogram style: the two-colour DRAW_COLOR_HISTOGRAM is not reconstructed, so add the colour expression and style by hand after reviewing the output.

Pitfalls

  • Plotting a colour-index buffer as a number. The 0/1 series is meaningless on the chart; convert it to a colour expression and pass it to color=.
  • Assigning EMPTY_VALUE's numeric value. If a conversion literally writes 1.7976931348623157e308, the plot's scale collapses so every other value looks flat. Use na.
  • Using plot() inside an if. Pine requires plot calls at global scope; pass the condition into the value instead: plot(cond ? v : na). Symptom: "Cannot use 'plot' in local scope".
  • Expecting the histogram to colour from a separate series by default. plot.style_histogram draws from zero to the value in one colour unless you compute the colour per bar.
  • Exceeding 64 plot calls when translating an indicator with many buffers. Combine into fewer plots or drop invisible working buffers.

FAQ

What is the Pine equivalent of INDICATOR_CALCULATIONS buffers?

A plain variable. Pine keeps the history of every series automatically, so an intermediate value that MQL5 stored in a hidden buffer is simply assigned and read back with [1], [2] when needed. Nothing is plotted.

How do I translate DRAW_ARROW with a wingding code?

Use plotshape(condition, style = shape.triangleup, location = location.belowbar, color = …) or plotchar(condition, char = "▲"). The arrow buffer's price value becomes the location (above/below bar or location.absolute with the series as the value).

Can the user change plot colours in TradingView like in the MT5 style dialog?

Yes for a static colour: every plot() appears in the Style tab. A colour computed per bar in code is shown as a single swatch and overriding it applies to all bars; expose input.color values if the user must control each state's colour.

Convert the whole program

Paste an MQL4 or MQL5 indicator or Expert Advisor and get Pine Script v6 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