Here's an MQL5 code snippet that calculates the values of four moving averages (Simple, Exponential, Smoothed, and Linear Weighted), sorts them, and stores the value between the highest and lowest moving average values.
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 4
// Variable for storing Moving Averages
double Simple_MA_Buffer[];
double Exponential_MA_Buffer[];
double Smoothed_MA_Buffer[];
double LinearWeighted_MA_Buffer[];
input int MA_Period = 14; // Moving Average Period
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, Simple_MA_Buffer);
SetIndexBuffer(1, Exponential_MA_Buffer);
SetIndexBuffer(2, Smoothed_MA_Buffer);
SetIndexBuffer(3, LinearWeighted_MA_Buffer);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[])
{
ArraySetAsSeries(close, true);
// Calculate Moving Averages
SimpleMAOnBuffer(close, MA_Period, 0, Simple_MA_Buffer);
ExponentialMAOnBuffer(close, MA_Period, 0, Exponential_MA_Buffer);
SmoothedMAOnBuffer(close, MA_Period, 0, Smoothed_MA_Buffer);
LinearWeightedMAOnBuffer(close, MA_Period, 0, LinearWeighted_MA_Buffer);
// Find the highest and lowest values among the four moving averages
double highest_value = -DBL_MAX;
double lowest_value = DBL_MAX;
for (int i = 0; i < rates_total; i++)
{
double ma_values[4] = {Simple_MA_Buffer[i], Exponential_MA_Buffer[i], Smoothed_MA_Buffer[i], LinearWeighted_MA_Buffer[i]};
// Sort the values
ArraySort(ma_values);
if (ma_values[3] > highest_value)
{
highest_value = ma_values[3];
}
if (ma_values[0] < lowest_value)
{
lowest_value = ma_values[0];
}
}
// Store the value of the difference from the highest to lowest value
double highest_lowest_difference = highest_value - lowest_value;
return(rates_total);
}
//+------------------------------------------------------------------+
//| Simple Moving Average |
//+------------------------------------------------------------------+
void SimpleMAOnBuffer(const double &price[], int period, int shift, double &buffer[])
{
int rates_total = ArraySize(price);
for (int i = shift; i < rates_total; i++)
{
buffer[i] = iMAOnArray(price, rates_total, period, 0, MODE_SMA, i);
}
}
//+------------------------------------------------------------------+
//| Exponential Moving Average |
//+------------------------------------------------------------------+
void ExponentialMAOnBuffer(const double &price[], int period, int shift, double &buffer[])
{
int rates_total = ArraySize(price);
for (int i = shift; i < rates_total; i++)
{
buffer[i] = iMAOnArray(price, rates_total, period, 0, MODE_EMA, i);
}
}
//+------------------------------------------------------------------+
//| Smoothed Moving Average |
//+------------------------------------------------------------------+
void SmoothedMAOnBuffer(const double &price[], int period, int shift, double &buffer[])
{
int rates_total = ArraySize(price);
for (int i = shift; i < rates_total; i++)
{
buffer[i] = iMAOnArray(price, rates_total, period, 0, MODE_SMMA, i);
}
}
//+------------------------------------------------------------------+
//| Linear Weighted Moving Average |
//+------------------------------------------------------------------+
void LinearWeightedMAOnBuffer(const double &price[], int period, int shift, double &buffer[])
{
int rates_total = ArraySize(price);
for (int i = shift; i < rates_total; i++)
{
buffer[i] = iMAOnArray(price, rates_total, period, 0, MODE_LWMA, i);
}
}
This code calculates the four types of Moving Averages (Simple, Exponential, Smoothed, and Linear Weighted) for the specified period, sorts them, and finds the highest and lowest values. It then calculates the difference between these two values and stores it in the variable named highest_lowest_difference.