Understanding Buffers and Plots
In MQL5, indicator buffers and plots are separate concepts:
- Buffers are arrays that store calculated values. Some are drawn on the chart, others are used internally for calculations.
- Plots are the visual representations. Each plot uses one or more buffers depending on the drawing style.
The number of buffers is always >= the number of plots. For example, a candlestick plot requires 4 buffers (open, high, low, close) but is only 1 plot.
Drawing Styles Reference
MQL5 offers 18 drawing styles. Here are the most commonly used:
DRAW_LINE — A continuous line connecting values. Uses 1 buffer. The most common style for moving averages, channels, and signal lines.
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
#property indicator_style1 STYLE_SOLID // SOLID, DASH, DOT, DASHDOT
DRAW_HISTOGRAM — Vertical bars from zero line. Uses 1 buffer. Perfect for oscillator values, volume displays, and momentum bars.
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 clrGreen
#property indicator_width1 3
DRAW_HISTOGRAM2 — Vertical bars between two values. Uses 2 buffers. Great for showing ranges and fill areas.
DRAW_ARROW — Draws symbols at specific points. Uses 1 buffer. Ideal for buy/sell signals, entry/exit markers.
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrLime
// Set the arrow code in OnInit:
PlotIndexSetInteger(0, PLOT_ARROW, 233); // up arrow
// Common codes: 233=up arrow, 234=down arrow, 159=dot, 108=star
DRAW_COLOR_LINE — A line that changes color based on conditions. Uses 2 buffers (data + color index). Powerful for showing trend direction with color.
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrLime,clrRed,clrGray // up to 64 colors
#property indicator_width1 2
Multi-Color Line Example
A moving average that is green when price is above it and red when below:
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrLime,clrRed
#property indicator_width1 2
input int MAPeriod = 20;
double maBuffer[];
double colorBuffer[]; // 0=green(above), 1=red(below)
int OnInit()
{
SetIndexBuffer(0, maBuffer, INDICATOR_DATA);
SetIndexBuffer(1, colorBuffer, INDICATOR_COLOR_INDEX);
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) ? MAPeriod : prev_calculated - 1;
for(int i = start; i < rates_total; i++)
{
// Calculate SMA
double sum = 0;
for(int j = 0; j < MAPeriod; j++)
sum += close[i - j];
maBuffer[i] = sum / MAPeriod;
// Set color: 0=green if price above MA, 1=red if below
colorBuffer[i] = (close[i] >= maBuffer[i]) ? 0 : 1;
}
return rates_total;
}
Buy/Sell Signal Arrows
Using DRAW_ARROW to mark entry points:
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrLime
#property indicator_width1 2
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrRed
#property indicator_width2 2
double buySignal[];
double sellSignal[];
int OnInit()
{
SetIndexBuffer(0, buySignal, INDICATOR_DATA);
SetIndexBuffer(1, sellSignal, INDICATOR_DATA);
// Arrow codes: 233=up arrow (buy), 234=down arrow (sell)
PlotIndexSetInteger(0, PLOT_ARROW, 233);
PlotIndexSetInteger(1, PLOT_ARROW, 234);
// Show arrows slightly offset from price
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0);
return INIT_SUCCEEDED;
}
In your OnCalculate, set buySignal[i] = low[i] - 10*_Point when a buy condition is met (places arrow below the candle), and sellSignal[i] = high[i] + 10*_Point for sells (above the candle). Set to 0 (empty value) when no signal.
Separate Window Indicators
For oscillators and indicators that do not overlay on the price chart:
#property indicator_separate_window // draw in separate sub-window
#property indicator_minimum 0 // fix Y-axis minimum
#property indicator_maximum 100 // fix Y-axis maximum
#property indicator_level1 30 // horizontal reference line
#property indicator_level2 70 // horizontal reference line
#property indicator_levelcolor clrSilver
Buffer Management Tips
- Buffers used for drawing must come first, calculation-only buffers last
- Use
INDICATOR_CALCULATIONSfor helper buffers that should not appear in the Data Window - Use
PlotIndexSetDouble(index, PLOT_EMPTY_VALUE, 0)to define what "no data" looks like — the indicator skips drawing at those points - Set
IndicatorSetString(INDICATOR_SHORTNAME, "Name")so your indicator shows a meaningful name in the chart and Data Window