plotshape() in MQL5: DRAW_ARROW markers and Wingdings codes

Short answer

plotshape() becomes a DRAW_ARROW buffer that holds low[i] or high[i] on signal bars and EMPTY_VALUE elsewhere. Set the glyph with PLOT_ARROW, a Wingdings code, and move it off the bar with PLOT_ARROW_SHIFT.

plotshape() puts a marker on the bars where a condition is true: a triangle under a bar for a buy signal, a cross above it for a sell. MQL5 has the same idea as a plot type: DRAW_ARROW draws one Wingdings glyph on every bar whose buffer value is not EMPTY_VALUE, at the price the buffer holds. So a plotshape becomes a buffer that is low[i] or high[i] on signal bars and EMPTY_VALUE everywhere else, plus a PLOT_ARROW code for the glyph and a PLOT_ARROW_SHIFT to move it a few pixels off the bar.

What plotshape() computes

plotshape(series, title, style, location, color, offset, text, textcolor, size, display) treats series as a condition: a bool is used directly, a number counts as true when it is neither na nor zero. On a true bar the shape named by style (shape.triangleup, shape.triangledown, shape.circle, shape.xcross, shape.diamond, shape.labelup and so on) is drawn at the location: location.abovebar (default) sits just above the high, location.belowbar just below the low, location.top and location.bottom pin it to the pane edge, and location.absolute uses the numeric value of series as the y coordinate. text prints a short label next to the shape, size scales it, and offset shifts it by whole bars. Like plot(), it counts as one of the script's 64 plot outputs and must be called at global scope.

The MQL5 equivalent

The Pine version marks EMA crossovers with triangles.

//@version=6
indicator("Shape demo", overlay=true)
f = ta.ema(close, 9)
s = ta.ema(close, 21)
up = ta.crossover(f, s)
dn = ta.crossunder(f, s)
plotshape(up, "Buy", shape.triangleup, location.belowbar, color.green, size=size.small)
plotshape(dn, "Sell", shape.triangledown, location.abovebar, color.red, size=size.small)

The MQL5 version keeps two buffers. The glyph is a Wingdings character code set with PLOT_ARROW; PLOT_ARROW_SHIFT is in pixels, positive downwards, so a marker anchored at the low gets a positive shift and one anchored at the high a negative one. The EMAs come from iMA handles so the example stays short.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_label1  "Buy"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrLimeGreen
#property indicator_width1  2
#property indicator_label2  "Sell"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_width2  2

input int InpFast = 9;    // Fast EMA
input int InpSlow = 21;   // Slow EMA

double BufBuy[], BufSell[];
int    hFast, hSlow;

int OnInit()
{
   SetIndexBuffer(0, BufBuy,  INDICATOR_DATA);
   SetIndexBuffer(1, BufSell, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(0, PLOT_ARROW, 233);         // Wingdings up arrow  (shape.triangleup)
   PlotIndexSetInteger(1, PLOT_ARROW, 234);         // Wingdings down arrow (shape.triangledown)
   PlotIndexSetInteger(0, PLOT_ARROW_SHIFT, 10);    // pixels below the low  (location.belowbar)
   PlotIndexSetInteger(1, PLOT_ARROW_SHIFT, -10);   // pixels above the high (location.abovebar)
   hFast = iMA(_Symbol, _Period, InpFast, 0, MODE_EMA, PRICE_CLOSE);
   hSlow = iMA(_Symbol, _Period, InpSlow, 0, MODE_EMA, PRICE_CLOSE);
   return (hFast == INVALID_HANDLE || hSlow == 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(hFast) < rates_total || BarsCalculated(hSlow) < rates_total)
      return 0;
   static double f[], s[];
   if(CopyBuffer(hFast, 0, 0, rates_total, f) < 0 || CopyBuffer(hSlow, 0, 0, rates_total, s) < 0)
      return 0;
   int start = MathMax(prev_calculated - 1, 1);
   for(int i = start; i < rates_total; i++)
     {
      bool up = f[i] > s[i] && f[i - 1] <= s[i - 1];   // ta.crossover
      bool dn = f[i] < s[i] && f[i - 1] >= s[i - 1];   // ta.crossunder
      BufBuy[i]  = up ? low[i]  : EMPTY_VALUE;
      BufSell[i] = dn ? high[i] : EMPTY_VALUE;
     }
   return rates_total;
}

Wingdings codes that cover the common Pine shapes:

Pine shapePLOT_ARROWGlyph
shape.triangleup, shape.arrowup233 or 241Up arrow (233 is the thick one)
shape.triangledown, shape.arrowdown234 or 242Down arrow
shape.circle159 or 108Small dot, filled circle
shape.square110Filled square
shape.diamond116Filled diamond
shape.xcross, shape.cross251Cross mark
check mark (no Pine shape)252Useful for confirmed signals

size.tiny to size.huge has no direct property; indicator_widthN (1 to 5) scales the glyph, and that is the closest control. text= cannot be rendered by DRAW_ARROW at all: create an OBJ_TEXT object on the signal bar instead (see the plotchar page for a compiled example).

Where the two differ

TopicPine ScriptMQL5
Anchor priceImplied by locationYou write the price into the buffer: low[i], high[i] or the value itself
Vertical gapAutomatic, scales with sizePLOT_ARROW_SHIFT in pixels; constant regardless of zoom
location.top / bottomPinned to the pane edgeNo equivalent for a buffer plot; use a chart object with screen coordinates or anchor at high/low
Texttext= drawn with the shapeSeparate OBJ_TEXT objects, one per signal bar
Colour per barSeries colour allowedDRAW_COLOR_ARROW with a colour-index buffer
offsetShifts the marker by barsPLOT_SHIFT on the plot; the anchor price still comes from the bar the buffer index names
Numeric seriesNon-zero means true; value used only for location.absoluteYou decide: test the condition, store the price

What the Stratlate converter emits

A plotshape() is lowered to a DRAW_ARROW buffer, not dropped: the buffer is written as cond ? high[i] : EMPTY_VALUE (or low[i] for location.belowbar and location.bottom, the series value for location.absolute), and PLOT_ARROW is set from the shape: 241 for triangleup, arrowup and labelup, 242 for their down counterparts, 251 for cross and xcross, 110 for square and diamond, 159 for circle and anything unknown. A constant colour passed as color= is applied with PLOT_LINE_COLOR; in our test a colour passed positionally was not picked up and the marker kept the default colour. text= is drawn as an OBJ_TEXT on the signal bar by the pine_shape_text runtime helper and the glyph is moved 16 pixels away with PLOT_ARROW_SHIFT; size is ignored. The conversion is graded CLEAN; still compile and check the markers on a chart before you rely on them.

Pitfalls

  • Leaving the buffer at zero on non-signal bars. DRAW_ARROW then draws a glyph at price 0 on every bar and the chart auto-scales to it. Write EMPTY_VALUE and set PLOT_EMPTY_VALUE to match.
  • Using the wrong sign for PLOT_ARROW_SHIFT. Positive moves the glyph down, so a buy marker anchored at the low needs a positive shift or it overlaps the candle.
  • Testing the crossover with i - 1 when the loop starts at 0. The first iteration reads index -1 and the indicator stops with an array out of range error; start at 1 or MathMax(prev_calculated - 1, 1).
  • Assuming Pine's size.small and MT5's width 1 look alike. The glyph sizes differ between the two platforms; pick a width by eye.
  • Passing na as the series on some bars and expecting a marker. Both platforms treat na as false; in MQL5 the equivalent is simply not writing the price.

FAQ

How do I draw the Pine text label next to the marker?

Create an OBJ_TEXT named after the bar time on each signal bar, set OBJPROP_TEXT, colour and OBJPROP_ANCHOR (ANCHOR_LOWER for text above a high, ANCHOR_UPPER below a low), and delete it again if the signal disappears while the bar is still forming.

Can one buffer show two different shapes?

No. A plot has exactly one PLOT_ARROW code. Pine scripts that pass a per-bar shape, or a plotarrow() that is sometimes positive and sometimes negative, need two buffers in MQL5, one per glyph.

Where do I find the Wingdings code for a symbol?

The code is the character's position in the Wingdings font, 32 to 255. The Windows Character Map with the Wingdings font selected shows it, and the codes in the table above cover the shapes plotshape offers.

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