hline() and fill() in MQL5: indicator levels and DRAW_FILLING

Short answer

hline() becomes #property indicator_levelN, one line per level with no buffer. fill() becomes a DRAW_FILLING plot that reads two buffers and paints between them, with one colour where the first is above the second and another where it is below.

hline() draws a horizontal line at a constant price, typically the 70 and 30 of an RSI, and fill() shades the area between two plots or two hlines. MQL5 covers both with features of the indicator window: horizontal levels are #property indicator_levelN (one line of code per level, no buffer), and the shaded band is a plot of type DRAW_FILLING that reads two buffers and paints between them, one colour where the first is above the second and another where it is below.

What hline() and fill() compute

hline(price, title, color, linestyle, linewidth, editable, display) takes a constant price (a literal or an input, never a series) and draws a line across the whole pane; it returns an hline handle. fill(hline1, hline2, color) shades between two such levels with one constant colour. fill(plot1, plot2, color, title, fillgaps, display) shades between two plot handles bar by bar; here color may be a series, so a band can be green where the fast line is above the slow one and red below, and fillgaps=true bridges bars where one plot is na. Pine v5 added a gradient form, fill(plot1, plot2, top_value, bottom_value, top_color, bottom_color), that interpolates the colour by the plot's value. Transparency is part of the colour via color.new(c, transp). Both functions are global-scope declarations and hline does not consume one of the 64 plot slots.

The MQL5 equivalent

The Pine version plots RSI with overbought and oversold lines, a band between them, and a second band between two hidden EMAs.

//@version=6
indicator("RSI bands", overlay=false)
r = ta.rsi(close, 14)
p = plot(r, "RSI", color=color.purple)
ob = hline(70, "OB", color=color.gray)
os = hline(30, "OS", color=color.gray)
fill(ob, os, color=color.new(color.purple, 90))
b1 = plot(ta.ema(close, 9), "Fast", display=display.none)
b2 = plot(ta.ema(close, 21), "Slow", display=display.none)
fill(b1, b2, color=color.new(color.blue, 85))

In MQL5 the two levels are properties, and the band between them is a DRAW_FILLING plot whose two buffers hold the constants 70 and 30. The filling plot is declared first so that the RSI line is drawn over it, the same order TradingView uses. For a band between two moving plots you copy the two plot values into the filling buffers instead of constants.

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1  70          // hline(70)
#property indicator_level2  30          // hline(30)
#property indicator_levelcolor clrGray
#property indicator_levelstyle STYLE_DOT
#property indicator_buffers 3
#property indicator_plots   2
#property indicator_label1  "Band"
#property indicator_type1   DRAW_FILLING  // fill(ob, os, ...)
#property indicator_color1  C'235,222,240', C'235,222,240'
#property indicator_label2  "RSI"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrPurple
#property indicator_width2  2

input int InpLength = 14;   // Length

double BufTop[], BufBot[], BufRsi[];
int    hRsi;

int OnInit()
{
   // the filling plot comes FIRST so the RSI line is drawn on top of it
   SetIndexBuffer(0, BufTop, INDICATOR_DATA);
   SetIndexBuffer(1, BufBot, INDICATOR_DATA);
   SetIndexBuffer(2, BufRsi, INDICATOR_DATA);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(0, PLOT_SHOW_DATA, false);
   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;
   if(CopyBuffer(hRsi, 0, 0, rates_total - start, BufRsi) < 0)
      return 0;
   for(int i = start; i < rates_total; i++)
     {
      BufTop[i] = 70.0;   // both band edges are constants here;
      BufBot[i] = 30.0;   // for fill(plotA, plotB) copy the two plot values instead
     }
   return rates_total;
}

Three details matter. indicator_levelcolor, indicator_levelstyle and indicator_levelwidth apply to all levels at once; Pine's per-hline colour has no property equivalent, so an hline with its own colour becomes a constant-valued DRAW_LINE buffer instead. Levels can be changed at run time with IndicatorSetDouble(INDICATOR_LEVELVALUE, n, price), which is how an input-driven hline(input.int(70)) is handled. And DRAW_FILLING takes two colours in indicator_colorN: the first where buffer 0 is above buffer 1, the second where it is below, which maps a conditional Pine colour whose test is exactly "plot1 above plot2".

Where the two differ

TopicPine ScriptMQL5
Where hline worksAny pane, including the main chart with overlay=trueindicator_levelN is for a separate window; on the main chart use an OBJ_HLINE or a constant buffer
Gaps in a fillna in a plot leaves the band open unless fillgaps=trueDRAW_FILLING has no empty value: an EMPTY_VALUE is bridged, so write the same value (such as 0) into both buffers to leave a region blank
Per-bar colourAny series colourTwo colours, chosen by which buffer is higher
Gradient fillSupported since v5No equivalent
Transparencycolor.new(c, 90) is honouredPlot colours have no alpha; pick a pale colour, or blend towards the chart background yourself
Level stylingPer hlineShared by all levels
Buffers usedNone for hline; none for fillNone for levels; two per DRAW_FILLING, which count towards indicator_buffers

What the Stratlate converter emits

An hline() is emitted as a constant-valued DRAW_LINE buffer (Buf[i] = 70 on every bar) rather than an indicator_level, which keeps its own colour and works in the chart window as well as a subwindow; it is graded faithful with no note. A fill() between two plot or two hline handles becomes a DRAW_FILLING plot over two extra buffers named PineFillN, placed before the other plots so lines draw on top, with its two colours set through PlotIndexSetInteger(p, PLOT_LINE_COLOR, 0/1, ...); a color.new transparency is approximated by blending the colour towards the chart background with the pine_color_new helper, since MetaTrader plots have no alpha. A conditional colour whose test is "first plot above second" maps onto the two colours exactly; other per-bar colours are drawn in their first branch and noted. The gradient form of fill() is dropped and reported as cosmetic (PM240). Compile the output and compare the bands visually; the lines they sit between are exact.

Pitfalls

  • Putting the DRAW_FILLING plot after the lines. MetaTrader draws plots in order, so the band paints over the RSI line and hides it.
  • Writing EMPTY_VALUE into one filling buffer to make a gap. The fill bridges the gap instead; set both buffers to the same value where there should be no band.
  • Using indicator_levelN in an indicator_chart_window indicator. Nothing appears; the level properties only apply to a separate window.
  • Expecting color.new(color.purple, 90) to look the same. The alpha is discarded by MetaTrader, so a 90 percent transparent purple becomes solid purple unless you choose a pale colour by hand.
  • Counting the filling buffers in indicator_plots instead of indicator_buffers. A DRAW_FILLING is one plot but two buffers; getting either number wrong leaves plots undrawn.

FAQ

How do I make an hline follow an input in MQL5?

Declare the input and call IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, InpOverbought) in OnInit; IndicatorSetString(INDICATOR_LEVELTEXT, 0, "OB") labels it. The #property value is only the default.

Can a fill change colour per bar like in Pine?

Only between two colours, decided by which buffer is higher on that bar. Any other rule (colour by RSI value, by trend state) needs a second DRAW_FILLING plot that is active on the bars where the first is blank.

Why does my band look darker than on TradingView?

Pine fills are usually 80 to 95 percent transparent and MetaTrader ignores the alpha channel. Choose a colour close to the chart background, for example C'235,222,240' on a white chart, to imitate a transparent purple.

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

Related