EMPTY_VALUE in Pine Script: na and gaps in plots
Short answer
EMPTY_VALUE is na in Pine: plot(cond ? value : na) leaves a gap, and warm-up bars are na automatically, so PLOT_DRAW_BEGIN is not needed. Test for emptiness with na(x), not a comparison.
EMPTY_VALUE is MQL5's marker for "nothing to draw on this bar": a buffer element holding it is skipped by the chart and shown blank in the Data Window. PlotIndexSetDouble(n, PLOT_EMPTY_VALUE, v) changes which value plays that role for one plot (often 0 for arrow buffers), and PLOT_DRAW_BEGIN hides the first bars. Pine Script has a single built-in answer to all three: na. A plotted na is not drawn, warm-up bars are na on their own, and the only thing to change in translated code is the test for emptiness, which must be na(x) rather than a comparison.
What EMPTY_VALUE computes
EMPTY_VALUE is a constant equal to DBL_MAX (about 1.8e308). It is the default empty value of every plot, so writing it into Buf[i] leaves a gap. It is an ordinary double: arithmetic on it gives huge or infinite numbers, and a comparison like Buf[i] != EMPTY_VALUE is the standard emptiness test. A DRAW_LINE plot does not draw a segment into or out of an empty bar, so isolated values vanish and runs of values show as separate segments. PLOT_EMPTY_VALUE replaces the marker for one plot: with 0.0, every bar where the buffer holds 0 is blank, which is convenient for arrow buffers that are zero-initialised. PLOT_DRAW_BEGIN tells the terminal to ignore the first n bars of the plot whatever they hold, so authors can leave garbage in the warm-up region. Buffers are not cleared automatically between full recalculations, which is why careful code writes EMPTY_VALUE explicitly on every bar it does not fill.
In Pine, na is a value of every type that means "not available". It comes from history before the first bar (close[50] on bar 10), from ta.* functions during their warm-up, and from explicit na. Arithmetic with na yields na, and any comparison with it yields false. plot() draws nothing on an na bar; plot.style_line then joins the points on either side of the gap, while plot.style_linebr breaks the line there, which is what MetaTrader's DRAW_LINE does at EMPTY_VALUE.
The Pine Script equivalent
A line that is drawn only while the close is above its SMA, plus a dot under each upward cross, written with both MQL5 empty-value conventions.
//@version=6
indicator("Above SMA with cross marks", overlay = true)
len = input.int(20, "SMA length")
sma = ta.sma(close, len) // na on the first len - 1 bars, so no PLOT_DRAW_BEGIN is needed
// EMPTY_VALUE -> na. style_linebr breaks the line at na instead of leaving a dangling end.
above = close > sma ? close : na
plot(above, "Above SMA", color = color.rgb(46, 139, 87), linewidth = 2, style = plot.style_linebr)
// PLOT_EMPTY_VALUE 0.0 on an arrow buffer -> na where there is no mark.
crossMark = ta.crossover(close, sma) ? low : na
plot(crossMark, "Cross", color = color.rgb(218, 165, 32), style = plot.style_circles, linewidth = 3)
// or: plotshape(ta.crossover(close, sma), "Cross", shape.circle, location.belowbar)#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrSeaGreen
#property indicator_width1 2
#property indicator_label1 "Above SMA"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrGoldenrod
#property indicator_label2 "Cross"
input int InpLen = 20; // SMA length
double AboveBuffer[];
double CrossBuffer[];
int OnInit()
{
SetIndexBuffer(0, AboveBuffer, INDICATOR_DATA);
SetIndexBuffer(1, CrossBuffer, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE); // the default: DBL_MAX means "no value"
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpLen); // and nothing before bar InpLen
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0); // arrows: 0 means "no arrow"
PlotIndexSetInteger(1, PLOT_ARROW, 159); // a dot
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) ? InpLen : prev_calculated - 1;
for(int i = start; i < rates_total; i++)
{
double sum = 0.0, prevSum = 0.0;
for(int k = 0; k < InpLen; k++)
{
sum += close[i - k];
prevSum += close[i - 1 - k];
}
double sma = sum / InpLen, prevSma = prevSum / InpLen;
AboveBuffer[i] = (close[i] > sma) ? close[i] : EMPTY_VALUE; // gap when below
CrossBuffer[i] = (close[i] > sma && close[i - 1] <= prevSma) ? low[i] : 0.0;
}
return(rates_total);
}| MQL5 | Pine |
|---|---|
Buf[i] = EMPTY_VALUE | x = na, usually inside cond ? value : na |
Buf[i] != EMPTY_VALUE | not na(x) |
PLOT_EMPTY_VALUE 0 on an arrow buffer | cond ? price : na, or plotshape(cond) |
PLOT_DRAW_BEGIN n | Nothing if the value is already na early; otherwise bar_index < n ? na : x |
ArrayInitialize(Buf, EMPTY_VALUE) | Nothing; a series starts as whatever you assign on each bar |
| Value or fallback | nz(x, fallback) |
| Last non-empty value | fixnan(x) or ta.valuewhen(not na(x), x, 0) |
Where the two differ
Comparisons. In MQL5 x == EMPTY_VALUE works because the marker is a number. In Pine x == na is always false (any comparison with na is false), so a literal translation of the emptiness test silently never matches. Always use na(x).
Arithmetic leaks. MQL5 code that forgets to check sometimes computes with DBL_MAX and produces an absurd value that scales the window to 1e308. Pine propagates na instead, which is safer but can hide that a whole calculation is empty; plots simply show nothing.
Integer and boolean types. EMPTY_VALUE only exists for doubles. In Pine na works for int, float, color and drawing handles, but a bool cannot be na in v6: it is true or false.
Warm-up. MQL5 needs PLOT_DRAW_BEGIN or explicit empties for the first bars. Pine's ta.sma(close, 20) is na for the first 19 bars by itself. A recursive calculation you write (a manual EMA with nz(prev)) is not, so add bar_index < n ? na : x where the original used DRAW_BEGIN.
History reads. A Pine series that was na on a bar stays na in history: x[1] is na after a gap. MQL5 code that reads Buf[i-1] after an empty bar gets DBL_MAX; both need a guard, with different tests.
What the Stratlate converter emits
The MQL5 to Pine engine maps EMPTY_VALUE to float(na) (PineCatalog constant). PlotIndexSetDouble(n, PLOT_EMPTY_VALUE, v) and the MQL4 SetIndexEmptyValue are applied to the emitted plot as a mask, plot(Buf == v ? float(na) : Buf), and PLOT_DRAW_BEGIN / SetIndexDrawBegin as bar_index < n ? float(na) : …; both setters are reported as absorbed when they land on an emitted plot (IndicatorLowering). For the program above the arrow buffer came out as plot((CrossBuffer == 0.0 ? float(na) : CrossBuffer)), which is correct, and the line plot carries the draw-begin wrapper. When the empty value is EMPTY_VALUE itself the mask is emitted as a comparison with float(na), which never matches in Pine; it is harmless because the buffer already holds na there, but do not copy the pattern. Two review points from the same conversion: the arrow plot had no dot style (the DRAW_ARROW came only from #property indicator_type2), and the previous-bar SMA in the cross test (close[i - 1 - k]) was emitted as the current bar's SMA, so the cross condition must be checked against the original. Compile and compare on the same bars.
Pitfalls
- Writing
if x == naorx != na. Always false; the branch never runs. Usena(x). - Translating
EMPTY_VALUEas its number. A plot that contains 1.8e308 on some bars compresses the price scale until everything else is a flat line. - Using 0 as "no value" in Pine because the MQL5 arrow buffer did. A zero is drawn at price 0 and drags an overlay chart's scale down; use
na. - Expecting
nz()to reproduce an empty plot.nz(x)turns gaps into zeros, which are drawn. Use it for calculations, not for what you plot. - Assuming a
boolcan carry "no signal" asna. In v6 it cannot; usefalse, or anint/floatwithnaif three states are needed.
FAQ
What is the numeric value of EMPTY_VALUE and does Pine have one?
In MQL5 it is DBL_MAX, about 1.7976931348623157e308. Pine has no numeric marker; na is a separate state that every numeric type can hold, tested with na().
How do I hide the first bars of a plot like PLOT_DRAW_BEGIN?
Usually nothing is needed because built-in indicators return na while warming up. For your own calculations, plot bar_index < n ? na : value.
Why does my converted line show isolated points?
With plot.style_linebr, a value that alternates between numbers and na on consecutive bars leaves single points with no neighbour to join, so they are barely visible. Plot fixnan(x) to hold the last value, switch to plot.style_line to join the points across gaps, or use plot.style_circles / plotshape if isolated points are intended.
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