How to create a custom indicator in MT5.
A practical, step-by-step guide to building a custom MetaTrader 5 indicator in MQL5, with a working example you can read and extend.
ZENKEI
A practical, step-by-step guide to building a custom MetaTrader 5 indicator in MQL5, with a working example you can read and extend.
To create a custom indicator in MT5, you make a new indicator in MetaEditor, declare its plot buffers, set them up in OnInit, and fill them with your calculation in OnCalculate as new bars arrive. The steps below walk through the whole thing in MQL5, from an idea to a line drawn on your chart.
A custom indicator is a technical tool you code yourself, plotted on the price chart or in a sub-window below it, to visualise a calculation, signal or condition that MetaTrader's built-in indicators do not provide. In MetaTrader 5 they are written in MQL5 using MetaEditor, the IDE that ships with the platform. An indicator only draws and calculates; it does not place trades.
Creating an indicator is mostly about deciding what to calculate and where to draw it. Before writing code, get clear on:
Decide how many lines you draw and whether they sit on the chart window or a separate sub-window. Each plotted line needs one buffer, an array MT5 reads to draw it.
Use the MQL Wizard to make a new Custom Indicator. It generates a skeleton with the two functions that matter: OnInit, which runs once at start, and OnCalculate, which runs whenever prices update.
At the top of the file, set the window with #property indicator_chart_window or indicator_separate_window, declare how many buffers and plots you use, and define each buffer array.
Bind each array to a plot with SetIndexBuffer, choose the draw type such as a line or histogram, and give the indicator a short name. This is the one-time wiring MT5 needs before it can draw.
Use the prev_calculated value so you only recompute new bars, loop over the bars, and write each result into the buffer. Getting this incremental update right is what keeps an indicator fast on live charts.
When your condition is met, trigger a popup, sound, email or mobile push alert, and optionally draw an arrow or object, so you are notified the moment the indicator signals.
Compile in MetaEditor, drop the indicator on a chart, and check it against known values. Watch behaviour at the first bars and across timeframes, where indicators most often go wrong.
Here is a complete, minimal custom indicator: a simple moving average drawn as a line on the chart. It shows the structure, the buffer wiring and the OnCalculate loop that every MT5 indicator is built on.
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
input int InpPeriod = 14; // averaging period
double SmaBuffer[];
int OnInit()
{
SetIndexBuffer(0, SmaBuffer, INDICATOR_DATA);
PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);
IndicatorSetString(INDICATOR_SHORTNAME, "MySMA(" + (string)InpPeriod + ")");
return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
if(rates_total < InpPeriod) return(0);
int start = (prev_calculated > 0) ? prev_calculated - 1 : InpPeriod - 1;
for(int i = start; i < rates_total; i++)
{
double sum = 0.0;
for(int j = 0; j < InpPeriod; j++)
sum += price[i - j];
SmaBuffer[i] = sum / InpPeriod;
}
return(rates_total);
}
From here you add more buffers for extra lines, colour and style, multi-timeframe reads, and alerts. The pattern stays the same: wire buffers in OnInit, calculate into them in OnCalculate.
prev_calculated makes the indicator slow and can lag the chart.Robust, non-repainting indicators with alerts and multi-timeframe logic take real MQL5 experience. If you would rather hand over an idea and get back a clean indicator with source code, that is our custom indicator development service. See also MQL5 programming or the full trading systems hub.
A custom indicator is a bespoke technical tool you code yourself, plotted on the chart or in a sub-window, to visualise a calculation, signal or condition that MetaTrader's built-in indicators do not provide.
MetaTrader 5 indicators are written in MQL5, using the MetaEditor IDE that ships with the platform. MetaTrader 4 uses MQL4, which is similar but a separate language. See MQL5 vs MQL4 for the differences.
OnCalculate is the function MT5 calls to compute an indicator's values. It receives the price data and how many bars were already calculated, so you only recalculate new bars and write results into the indicator's plot buffers.
Yes. You can trigger popup, sound, email and mobile push notifications from within OnCalculate when your condition is met, so you are notified the moment the indicator signals.
To build a robust indicator by hand you need working MQL5 knowledge. Simple ideas are approachable, but anything with multiple buffers, multi-timeframe logic or alerts is easier and safer to have a developer build.
Bring the idea. We build a clean, tested MT5 indicator, source code included.