The Moving Average indicator, colloquially named MA, is an indicator that can be used in different scenarios. Moving Averages can be employed to ensure that a trend is still in place or to identify a trend reversal from price/MA crossover. Another approach is to use multiple Moving Averages plotted with various averaging periods and to recognize a developing trend or price reversal MA crossover. The most popular averaging periods are the 20-period, 50-period, and 200-period.

There are numerous variances of moving averages; the simplest is known as the SMA or simple moving average, which consists of the average price of the underlying instrument for a given period (Fig.230).

*Fig.230 Simple Moving Average Formula*

Another popular variant of the Moving Average indicator is the EMA, which is the Exponential Moving Average. The EMA’s advantage over the SMA is that it provides less lag and is more responsive to price movement by adding more weight to the most recent data points. The result of adding more weight to the recent prices provides a smoothing factor (Fig.231).

*Fig.231 Exponential Moving Average Formula*

In this guide, we will go through how to effectively use various types of Moving Averages to identify potential buy and sell signals from a variety of approaches such as trend following, trend reversal, MA crossover, and price/MA crossover. We will also develop and guide you through the method to store the data points and how to retrieve them to build a trading strategy with the moving averages. MQL4 Moving Average function retrieves the indicator data points directly.

On the other hand, MQL5 requires the data to be initialized and to be stored in the global cache before being attributed to a declared double type array. It may appear a bit confusing at first glance, but the algorithm is very straightforward. We will go into the details step by step for this MQL5 particularity. Finally, we will illustrate a practical example of usage with code ready to use.

## How does the Moving Average indicator work?

Before we go into the details of the Moving Average indicator code implementation, let us illustrate the most prevalent price/MA crossover buy and sell signals interpretation.

### Buy signal

The Moving Average and price crossover are some of the most popular trading signals. A buy signal is recognized when the price action crosses above the Moving Average and closes the bar or candle above it. Additionally, if the slope of the MA is upward, it may indicate a strong buy signal.

### Sell signal

A bearish Moving Average crossover occurs when the price crosses below and closes its bar or candle below the MA line. Moreover, if the MA slope is trending down, it may indicate a strong sell signal.

### Moving Average crossover

Traders can recognize a developing trend reversal by using multiple Moving Averages, a fast and a slow averaging period. The most popular averaging period crossover strategy is the 20-period and 50-period crossover, as demonstrated in Fig.232. The fast line is represented in red with the 20-period, and the slow line is defined in green with the 50-period.

The fast line (20MA) provides the pace of change. When the fast MA line crosses above the slow MA line, it signals a strong change in the price momentum, which is often the beginning of a developing uptrend. Conversely, when the fast MA line crosses below the slow MA line, a downtrend may be the case.

*Fig.232 Moving Average crossover*

## How does the Moving Average indicator store its data points?

The MQL5 Moving Average keeps its data point in a global cache which is a dynamic array that is the indicator buffer. The size of the array is managed by MetaTrader and initialized when the Expert Advisor loads. An indicator buffer is a time-series array; hence it has the particularity of reverse indexing. It means that the array is sorted in descending order where the latest data point is on the left side of the array. A time-series is used to store historical data and comprise the time information.

Consequently, the time-series element with index zero contains the latest quote of a symbol. For instance, an indicator on the daily timeframe will have the current oscillator bar at index zero and yesterday’s data at the position index one (Fig.233).

*Fig.233 Indicator data points array*

### MQL5

To access the Moving Average indicator data, MQL5 requires the use of the **CopyBuffer()** function. This approach does not apply to MQL4 as the MQL4 indicators function directly returns the value of the MA’s data point instead of the indicator handle. The following parameters should be provided:

- Provide the handle that is returned by the
**iMA() function**. - Provide the buffer number. You may provide zero as the default value.
- Provide the start position. You may provide zero to retrieve from the most recent data point. This number represents the index where zero is the most recent data point, and one is the previous data point.
- Specify the number of data points to get from the
**iMA() function.** - Provide the variable where the data will be stored. The variable is a double type.

**How to retrieve Moving Average data with CopyBuffer() function?**

- Declare a variable for the handle:
**int IndicatorHandle = iMA();** - Declare a variable to store indicator data:
**double ma_indicator;** - Copy the data to the indicator variable and retrieve data points
**CopyBuffer(IndicatorHandle, 0, 0, 100, ma_indicator);**- Retrieve the
**Moving Average**data point with**ma_indicator[0]**,**ma_indicator[1].**

Fig.234 CopyBuffer() Function

## How to access the indicator data?

### MQL4

The **iMA()** function will return the bar value according to the following parameters:

- The symbol of the traded instrument. You may provide a string such as “EURUSD” or use the function
**Symbol()**to retrieve the symbol from the chart where the Expert Advisor is attached to. - Provide the time frame using the enumeration value from the constant
**ENUM_TIMEFRAMES,**for instance:- PERIOD_CURRENT, for the current timeframe from the chart,
- PERIOD_M1, for the one-minute timeframe,
- PERIOD_D1, for the daily timeframe.

- Provide the averaging period such as 20, 50 for 20-period and 50-period, respectively.
- Specify the data points shift for the graph offset.
- Provide the averaging method using the enumeration values of the constant
**ENUM_MA_METHOD**, for instance:- MODE_SMA, to retrieve the simple moving average data points.
- MODE_EMA, to get the exponential averaging.
- MODE_SMMA, to get the smoothed averaging method.
- MODE_LWMA, to compute the averaging with the linear-weighted method.

- Provide the applied price method using enumeration values of the constant
**ENUM_APPLIED_PRICE**, for instance:- PRICE_CLOSE, for the close price
- PRICE_OPEN, for the open price.
- PRICE_HIGH, for the highest price of the selected range.
- PRICE_LOW, for the lowest price of the selected range.

- Provide the index of the indicator buffer where zero is the latest data point and one the previous data point.

*Fig.235 MQL4 Moving Average function*

### MQL5

When calling the **iMA()** function, the function will create the Awesome Oscillator in the MetaTrader global cache and return its handle according to these provided parameters:

- The symbol of the traded instrument. You may provide a string such as “EURUSD,” or you can use the function
**Symbol()**to get the symbol from the chart where the Expert Advisor is attached to. - Provide the time frame of the data point to extract using the enumeration value from the constant
**ENUM_TIMEFRAMES:**- PERIOD_CURRENT, to get data points from the current timeframe from the chart to which the Expert Advisor is attached.
- PERIOD_M1, for the one-minute timeframe.
- PERIOD_D1, for the daily timeframe.

- Specify the averaging period, for instance, 20, 50 for 20-period and 50-period, respectively.
- Specify the data points shift for the graph offset.
- Provide the averaging method using the enumeration values of the constant
**ENUM_MA_METHOD**, for instance:- MODE_SMA, to retrieve the simple moving average data points.
- MODE_EMA, to get the exponential averaging.
- MODE_SMMA, to get the smoothed averaging method.
- MODE_LWMA, to compute the averaging with the linear-weighted method.

- Provide the applied price method using enumeration values of the constant
**ENUM_APPLIED_PRICE**, for instance:- PRICE_CLOSE, for the close price.
- PRICE_OPEN, for the open price.
- PRICE_HIGH, for the highest price of the selected range.
- PRICE_LOW, for the lowest price of the selected range.

**Note that to retrieve data points from the iMA() function, you need to use it in combination with the function CopyBuffer().**

*Fig.236 MQL5 Moving Average function*

## Practical case

### MQL5

Fig237 demonstrates the use of the Moving Average indicator to generate a buy and sell signal employing the price/MA crossover strategy with a 20-period simple moving average.

Line 74: Declare the indicator handle and assign **iMA() function** handle with the specified parameters, in our case, a 20-period simple moving average with closed price data points.

Line 75: Declare the indicator “double type” array to store the **iMA()** data points.

Line 78: Copy the latest hundred data points into the indicator array ma_indicator.

Line 80 – 102: IF statement to identify the moving average trend as well as the price/MA crossover. The first block defines the conditions of a sell signal, first by confirming that the MA slope is downward and then making sure that the price crosses and closes below the Moving average. The second block assesses the conditions of a buy signal by ensuring that the MA slope is upward and the price crosses and closes above the Moving Average.

*Fig.237 Moving Average trend and price/MA crossover in MQL5*

### MQL4

Fig238 exhibits the use of the Moving Average indicator to create a buy and sell signal using the price/MA crossover strategy with a 20-period simple moving average.

Line 108 – 113: Declare variables and assign the value of each data point returned from the **iMA() **functions. Note that the last parameter from the **iMA()** function defines the position of the data point where zero is the latest value, while one is the previous value.

Line 116 – 138: IF statement to identify the moving average trend as well as the price/MA crossover. The first block defines the conditions of a sell signal, first by confirming that the MA slope is downward and then making sure that the price crosses and closes below the Moving average. The second block assesses the conditions of a buy signal by ensuring that the MA slope is upward and the price crosses and closes above the Moving Average.

*Fig.238 Moving Average and price/MA crossover in MQL4*