MetaTrader 5 MT5 Libraries MovingAverages – library MetaTrader 5

MovingAverages – library MetaTrader 5

The MovingAverages library is a part of Standard package of MetaTrader 5 client terminal.

The library contains functions for calculation of different types of moving averages. Totally, there are 8 functions that can be divided into 2 groups of functions of the same type, each containing 4 of them.

The first group contains functions that receive an array and simply return a value of a moving average at a specified position:

  • SimpleMA() – for calculating the value of a simple average;
  • ExponentialMA() – for calculating the value of an exponential average;
  • SmoothedMA() – for calculating the value of a smoothed average;
  • LinearWeightedMA() – for calculating the value of a linear-weighted average.

These functions are intended for obtaining the value of an average once for an array, and are not optimized for multiple calls. If you need to use a function from this group in a loop (to calculate values of an average and further write each calculated value into an array), you’ll have to organize an optimal algorithm.

The second group of functions is intended for filling out the recipient array by values of a moving average based on the array of initial values:

  • SimpleMAOnBuffer() – fills out the output array buffer[] by values of a simple average from the price[] array;
  • ExponentialMAOnBuffer() – fills out the output array buffer[] by values of an exponential average from the price[] array;
  • SmoothedMAOnBuffer() – fills out the output array buffer[] by values of a smoothed average from the price[] array;
  • LinearWeightedMAOnBuffer() – fills out the output array buffer[] by values of a linear weighted average from the price[] array.
Alternative:   Session Open H-Line - indicator MetaTrader 4

Functions:

//+------------------------------------------------------------------+
//|                                               MovingAverages.mqh |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                               |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      ""
//+------------------------------------------------------------------+
//| Simple Moving Average                                            |
//+------------------------------------------------------------------+
double SimpleMA(const int position,const int period,const double &price[])
//+------------------------------------------------------------------+
//| Exponential Moving Average                                       |
//+------------------------------------------------------------------+
double ExponentialMA(const int position,const int period,const double prev_value,const double &price[])
//+------------------------------------------------------------------+
//| Smoothed Moving Average                                          |
//+------------------------------------------------------------------+
double SmoothedMA(const int position,const int period,const double prev_value,const double &price[])
//+------------------------------------------------------------------+
//| Linear Weighted Moving Average                                   |
//+------------------------------------------------------------------+
double LinearWeightedMA(const int position,const int period,const double &price[])

//+------------------------------------------------------------------+
//| Simple moving average on price array                             |
//+------------------------------------------------------------------+
int SimpleMAOnBuffer(const int rates_total,const int prev_calculated,const int begin,
//+------------------------------------------------------------------+
//| Exponential moving average on price array                        |
//+------------------------------------------------------------------+
int ExponentialMAOnBuffer(const int rates_total,const int prev_calculated,const int begin,
                          const int period,const double& price[],double& buffer[])
//+------------------------------------------------------------------+
//| Smoothed moving average on price array                           |
//+------------------------------------------------------------------+
int SmoothedMAOnBuffer(const int rates_total,const int prev_calculated,const int begin,
                       const int period,const double& price[],double& buffer[])
//+------------------------------------------------------------------+
//| Linear Weighted moving average on price array                    |
//+------------------------------------------------------------------+
int LinearWeightedMAOnBuffer(const int rates_total,const int prev_calculated,const int begin,
                             const int period,const double& price[],double& buffer[])

Example:

Alternative:   CHOWithFlat - indicator MetaTrader 5

For examples of its use see article “MQL5: Create Your Own Indicator”


/ru/code/77

📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks