While MQL4 offers a vast range of built-in indicators, there may be times when traders need a specific tool tailored to their strategy. This is where custom indicators come into play. Custom indicators are user-defined tools that can analyze price dynamics and depict them in a graphical form.
Custom indicators typically start with the `#property` directives to set certain properties:
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
The main function to define in a custom indicator is `OnInit()`, which initializes the indicator, and `OnCalculate()`, where the main logic is written.
Though SMA is a built-in indicator, this example will give you an idea of how to structure a custom indicator:
// Define the input parameters
input int period=14; // SMA Period
// Define global variables
double ExtMapBuffer1[];
int OnInit()
{
// Indicator buffer allocation
SetIndexBuffer(0, ExtMapBuffer1, INDICATOR_DATA);
IndicatorDigits(Digits);
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[])
{
// Calculating the SMA
for(int i=0; i<rates_total-period+1; i++)
{
ExtMapBuffer1[i] = 0;
for(int j=0; j<period; j++)
{
ExtMapBuffer1[i] += close[i+j];
}
ExtMapBuffer1[i] /= period;
}
return(rates_total);
}
Custom indicators offer an opportunity to enhance your trading toolkit with bespoke tools, tailored to your strategy. By understanding the structure and logic behind creating custom indicators in MQL4, you can develop unique tools to elevate your trading analysis.
NEXT UP: Creating Scripts for Repetative Tasks in MQL4