Overview
In MQL5, an indicator analyzes market data and visualizes results on a chart or subwindow. Unlike Scripts and Expert Advisors, indicators do not send orders; they compute values and draw them as lines, histograms, or arrows so you can interpret trends and signals. You can use built-in indicators (e.g., Moving Average, RSI) or create custom ones with MQL5.
Ad
What You Will Be Able to Do
- Explain how indicators process price data and render values.
- Differentiate built-in vs. custom indicators.
- Create, compile, and attach a simple custom indicator.
- Locate compiled files under
MQL5/Indicators/.
Code Example
//+------------------------------------------------------------------+
//| MyFirstIndicator.mq5 |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property version "1.00"
#property indicator_chart_window
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_label1 "MySMA"
#property indicator_width1 2
#property indicator_buffers 1
input int Period = 10; // MA period
input ENUM_MA_METHOD Method = MODE_SMA; // MA method
input ENUM_APPLIED_PRICE Price = PRICE_CLOSE; // Applied price
double Buf[]; // output buffer (what we draw)
int ma_handle = INVALID_HANDLE;
int OnInit()
{
// Plot starts after we have enough bars for the period
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, Period - 1);
// Bind the output buffer to the first (and only) plot
SetIndexBuffer(0, Buf, INDICATOR_DATA);
// Create a handle of the built-in iMA to reuse its tested logic
ma_handle = iMA(_Symbol, _Period, Period, 0, Method, Price);
if(ma_handle == INVALID_HANDLE)
return(INIT_FAILED);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
if(ma_handle != INVALID_HANDLE)
IndicatorRelease(ma_handle);
}
// Called whenever the terminal has new data or needs a redraw
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double& price[])
{
// Fill our drawing buffer from the iMA values
int copied = CopyBuffer(ma_handle, 0, 0, rates_total, Buf);
if(copied <= 0)
return(prev_calculated);
return(rates_total);
}
Ad
Execution Steps
- Open MetaEditor → File → New → Custom Indicator and name it
MyFirstIndicator. - Replace the template with the code above and press F7 to compile.
- In MetaTrader 5, attach
MyFirstIndicatorto any chart. - Use the Inputs tab to change Period, Method, or Price and observe the line update.
- Find the compiled file at
MQL5/Indicators/MyFirstIndicator.ex5.
Key Point
Indicators compute and draw values; they do not trade. They are often used as inputs to Expert Advisors or for visual decision support on charts.
Ad
Next Section
→ Next: Reading Indicator Values


