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.

Sculptural head crowned by a glowing slab of market-green light

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.

What is a custom indicator?

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.

Before you start

Creating an indicator is mostly about deciding what to calculate and where to draw it. Before writing code, get clear on:

How to create a custom indicator in MT5, step by step

  1. Plan the plot and buffers

    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.

  2. Create the indicator in MetaEditor

    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.

  3. Declare properties and buffers

    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.

  4. Set up buffers in OnInit

    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.

  5. Calculate values in OnCalculate

    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.

  6. Add alerts (optional)

    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.

  7. Compile, test and refine

    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.

A minimal MQL5 indicator

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.

Simple moving average · MQL5 indicator
#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.

Common mistakes to avoid

Prefer to have it built?

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.

Frequently asked

What is a custom indicator in MetaTrader?

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.

What language are MT5 indicators written in?

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.

What is OnCalculate in an MQL5 indicator?

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.

Can I add alerts to a custom indicator?

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.

Do I need to know how to code to create a custom indicator?

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.

Skip the buffers. Get the indicator.

Bring the idea. We build a clean, tested MT5 indicator, source code included.

Custom indicators Book a call