The standard deviation (StdDev) is a statistical measure that allows traders to gauge the volatility of a traded instrument. It simply measures how prices are dispersed from the average price within a period. In the case that the traded instrument is trading in a narrow range of price, the standard deviation will likely return a small value which indicates low volatility. On the other hand, if the underlying instrument exhibits a reckless and wide price swing, the standard deviation will likely return a high value indicating a peak in volatility.

The standard deviation or StdDev is computed in three parts. The first calculation involves the sum of all the data points divided by the number of data points within the specified period. The default standard deviation period in MetaTrader 4 and MetaTrader 5 is twenty. The first part determines the mean value. The second part involves the subtraction of the mean from each data point within the selected period to define the variance. And finally, the third part is the square root of the variance, which will determine the standard deviation. 

Standard Deviation formula
Fig.259 Standard Deviation formula

The Standard Deviation is not precisely a trading indicator in the traditional sense as it can not be used to produce buy and sell signals. However, it is an excellent indicator when it is combined with moving averages or with oscillators, such as the RSI or MACD. 

In this guide, we will walk you through one of the methods to produce signals combining the standard deviation indicator with the RSI. The RSI indicator will identify potential entry and exit signals, while the standard deviation will help to set stop loss and target price levels. 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 RSI and the standard deviation indicator. 

MQL4 StdDev function retrieves the indicator’s data point directly. Conversely, 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 seems a bit confusing, but it is actually not. We will go into the details step by step for this MQL5 specific feature. Finally, we will illustrate a practical example of usage with code ready to be used.

How does the Standard Deviation indicator work?

Before we dive into the details of the Standard Deviation indicator code implementation, let us explain to you how to read the indicator. The StdDev indicator is represented by a line which is the deviation value in price. When the line rises, it symbolizes that prices are becoming more volatile. As the price moves back to its mean and an asset trades in a narrow range, the standard deviation heads lower. A spike in the standard deviation signifies a surge in price volatility, while a lower reading suggests low volatility.

Standard Deviation Indicator
Fig.260 Standard Deviation Indicator

How does the Standard Deviation indicator store its data points?

The MQL5 StdDev indicator 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; as such, the indexing is reversed. It means that the array is sorted by descending 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.

Therefore, 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.261).

Indicator data points array 

Fig.261 Indicator data points array 

MQL5

To access the Standard Deviation indicator data, MQL5 requires the use of the CopyBuffer() function. This approach does not apply to MQL4 as the MQL4 indicator function directly provides the value of the StdDev data point instead of the indicator handle. The indicator handle is a sort of address to locate the data in the MetaTrader global cache. To initialize the StdDev indicator data, the following parameters should be provided to the CopyBuffer() function:

  1. Specify the handle that is returned by the iStdDev() function
  2. Provide the buffer number. You may provide zero as the default value.
  3. 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.
  4. Specify the number of data points to get from the iStdDev() function.
  5. Provide the variable where the data will be stored. The variable must be a double type.

How to retrieve the Standard Deviation indicator data with the CopyBuffer() function?

  1. Declare a variable for the handle: int IndicatorHandle = iStdDev();
  2. Declare a variable to store indicator data: double stddev_indicator;
  3. Copy the data to the indicator variable and retrieve data points
    1. CopyBuffer(IndicatorHandle, 0, 0, 100, stddev_indicator);
    2. Retrieve the StdDev data point with stddev_indicator[0], stddev_indicator[1].

CopyBuffer() Function
Fig.262 CopyBuffer() Function

How to access the indicator data?

MQL4

The iStdDev() function will return the indicator data point values according to the following parameters:

  1. 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.
  2. Provide the time frame using the enumeration value from the constant ENUM_TIMEFRAMES, for instance:
    1. PERIOD_CURRENT, for the current timeframe from the chart,
    2. PERIOD_M1, for the one-minute timeframe,
    3. PERIOD_D1, for the daily timeframe.
  3. Specify the averaging period to compute. The default value is 20-period.
  4. Specify the moving average shift.
  5. Provide the averaging method with the enumeration value from the constant ENUM_MA_METHOD, for instance:
    1. MODE_SMA, for the “simple averaging,”
    2. MODE_EMA, for the “exponential averaging,”
    3. MODE_SMMA, for the “smoothed averaging,”
    4. MODE_LWMA, for the “linear-weighted averaging.”
  6. Specify the applied price for the calculation with the enumeration value from the constant ENUM_APPLIED_PRICE, for instance:
    1. PRICE_CLOSE, for the close price,
    2. PRICE_OPEN, for the open price,
    3. PRICE_HIGH, to use the highest price data,
    4. PRICE_LOW, to use the lowest price data.
  7. Provide the index of the indicator buffer where zero is the latest data point and one the previous data point.

MQL4 RSI function
Fig.263 MQL4 RSI function

MQL5

When calling iStdDev(), the function will create the Standard Deviation indicator in the MetaTrader global cache and returns its handle according to the following parameters:

  1. 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.
  2. Provide the time frame using the enumeration value from the constant ENUM_TIMEFRAMES for instance:
    1. PERIOD_CURRENT, for the current timeframe from the chart,
    2. PERIOD_M1, for the one-minute timeframe,
    3. PERIOD_D1, for the daily timeframe.
  3. Specify the averaging period to compute. The default value is 20-period.
  4. Specify the moving average shift.
  5. Provide the averaging method with the enumeration value from the constant ENUM_MA_METHOD, for instance:
    1. MODE_SMA, for the “simple averaging,”
    2. MODE_EMA, for the “exponential averaging,”
    3. MODE_SMMA, for the “smoothed averaging,”
    4. MODE_LWMA, for the “linear-weighted averaging,”
  6. Specify the applied price for the calculation with the enumeration value from the constant. ENUM_APPLIED_PRICE for instance:
    1. PRICE_CLOSE, for close price
    2. PRICE_OPEN, for open price
    3. PRICE_HIGH, to use the highest price data
    4. PRICE_LOW, to use the lowest price data

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

MQL5 RSI function
Fig.264 MQL5 RSI function

Practical case

MQL5

Fig.265 demonstrates the use of the Standard Deviation indicator in MQL5 to set an appropriate stop loss and target price based on the market volatility from the last 20-period. The StdDev indicator is used in combination with the RSI indicator to spot overbought and oversold opportunities.

Line 4: Declare the Standard Deviation indicator handle and assign the iStdDev function handle with the specified parameters. For instance, we provide the default 20-period.

Line 5: Declare the indicator “double type” array to store the iStdDev() data points.

Line 7: Declare the indicator handle and assign the iRSI() function handle with the specified parameters. For this example, we will provide the default settings of the 14-period.

Line 8: Declare the indicator “double type” array to store the iRSI() indicator data points.

Line 11: Copy the latest hundred RSI data points into the indicator array rsi_indicator.

Line 12: Copy the latest hundred StdDev data points into the indicator array stddev_indicator.

Line 14 – 15: Declare the stop loss and target price variables.

Line 17 – 33: IF statement to identify the RSI oversold and overbought pattern. The first IF block captures the RSI oscillator line movement of an overbought pattern. If the previous data points are above the 70-level and move below this threshold, a sell signal is recognized. 

The second IF block captures the RSI oversold pattern. An oversold condition is when the RSI oscillator line is below the 30-level and moves above this threshold. Stop loss and target price levels are set according to the market volatility defined by the standard deviation of the last 20-period. The applied risk/reward ratio is set to 1:2, as attested by the stop loss set at half of the standard deviation value, while the target price is equivalent to the current standard deviation.

RSI oversold/overbought signal combined with StdDev stop loss and target price in MQL5
Fig.265 RSI oversold/overbought signal combined with StdDev stop loss and target price in MQL5

MQL4

Fig.266 exhibits the use of the Standard Deviation indicator in MQL4 to set an appropriate stop loss and target price based on the market volatility from the last 20-period. The StdDev indicator is used in combination with the RSI indicator to identify overbought and oversold opportunities.

Line 40: Declare the variable that will store the last standard deviation data point. Assign the most recent data point returned from the function iStdDev().

Line 42 – 44: Declare variables that will store RSI the last three most recent data points. Variables are assigned the value from the function iRSI().

Line 46 – 47: Declare the stop loss and target price variables.

Line 50 – 66: The IF statement is to identify the RSI oversold and overbought pattern. The first IF block captures the RSI oscillator line movement of an overbought pattern. 

If the previous data points are above the 70-level and move below this threshold, a sell signal is recognized. The second IF block captures the RSI oversold pattern. An oversold condition is when the RSI oscillator line is below the 30-level and moves above this threshold. Stop loss and target price levels are set according to the market volatility defined by the standard deviation of the last 20-period. The applied risk/reward ratio is set to 1:2, as attested by the stop loss set at half of the standard deviation value, while the target price is equivalent to the current standard deviation.

RSI oversold/overbought signal combined with StdDev stop loss and target price in MQL4
Fig.266 RSI oversold/overbought signal combined with StdDev stop loss and target price in MQL4

Leave a Reply

  +  83  =  88