There are several approaches to interpret the Envelopes indicator. The main approach is to identify upper or lower band price breakout, which occurs while price crossover the upper or lower band. The alternative approach is a mean reversion strategy, whereas price is projected to return to its midpoint upon reaching the upper or lower band. The Envelopes indicator consists of a fixed positive and negative deviation from the defined moving average, which is reflected by an upper and lower band (Fig.199).

Envelopes Indicator formula
Fig.199 Envelopes Indicator formula

In this guide, we will go through how to detect a buy and a sell signal with the Envelopes indicator and go through the method to store the data points and how to retrieve them in a trading strategy. MQL4 Envelopes function retrieves the indicator’s data point. Conversely, MQL5 requires the data to be stored in the indicator buffer and in a declared “double type” array. 

You will find it is a straightforward process as you go into the details step by step with us. And finally, we will show you a practical example of usage with ready-to-use code.

How does the Envelopes indicator work?

Before we go into the details of the Envelopes function implementation, let us explain the buy and sell signals interpretation. The Envelopes indicator is very similar to the Bollinger bands indicator. It uses a moving average as the midpoint and a fixed deviation for the upper and lower bands.

Buy signal

A lower band crossover buy signal occurs when the price action evolves below the lower band and closes its bar above it. Typically, a crossover of the lower band signifies a possible upside reversal. A lower band crossover buy signal occurs when the price action evolves below the lower band and closes its bar above it.
A breakout trade can be identified when the underlying instrument crosses above the upper band, suggesting built-up momentum that may lead to an upside continuation. A breakout trade can be identified when the underlying instrument crosses above the upper band, suggesting built-up momentum that may lead to an upside continuation.

Sell signal

A fading bullish move can be recognized when the underlying instrument crosses below the upper band after some time above it. A sell signal can be identified when the price action closes below the upper band. A fading bullish move can be recognized when the underlying instrument crosses below the upper band after some time above it.
A price action breakdown can be identified when the candlestick closed below the lower band. When the bar crosses below the lower band, a price drop can be anticipated. A price action breakdown can be identified when the candlestick closed below the lower band.

 

How does the Envelopes indicator store its data points?

The Envelopes indicator keeps its data point in an indicator buffer which is a dynamic array. The size of the array is managed by MetaTrader during the Expert Advisor initialization. An indicator buffer is a time-series array; hence it has the particularity of reverse indexing. It means that the array is sorted by descending where the latest data point is on the left side of the array. 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 indicator bar at index zero and yesterday’s data at the position index one (Fig.200). 

The Envelopes indicator has a particularity to have two buffers, one for the upper band and one for the lower band. The upper band data points are stored in the buffer index zero, while the lower band data points are saved in the buffer index one.

Indicator data points array
Fig.200 Indicator data points array 

MQL5

To access the Envelopes indicator data, MQL5 requires the use of the CopyBuffer() function. This method does not apply to MQL4 as the MQL4 indicators function directly returns the Envelopes’ data point instead of the indicator handle. The following parameters should be provided to the CopyBuffer() function to retrieve the indicator data:

  1. The handle that is returned by the iEnvelopes() function.
  2. Provide the buffer number corresponding to the upper or lower band.
    1. Zero for the upper band.
    2. One for the lower band.
  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 iEnvelopes() function.
  5. Provide the variable where the data will be stored. The variable is a double type.

How to retrieve the Envelopes data with CopyBuffer() function?

  1. Declare a variable for the handle: int IndicatorHandle = iEnvelopes();
  2. Declare two variables to store the upper and lower band data points:
    1. double env_indicator_upper;
    2. double env_indicator_lower;
  3. Copy the data to the indicator variables and retrieve data points
    1. CopyBuffer(IndicatorHandle, 0, 0, 100, env_indicator_upper);
    2. CopyBuffer(IndicatorHandle, 0, 1, 100, env_indicator_lower);
    3. Retrieve Envelopes data point with:
      1. env_indicator_upper[0], env_indicator_upper[1];
      2. env_indicator_lower[0], env_indicator_lower[1];

CopyBuffer() Function
Fig.201 CopyBuffer() Function

How to access the indicator data?

MQL4

The iEnvelopes() function will return the data points according to these nine 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. Provide the moving average period. The default setting is a 20-period SMA.
  4. Specify the averaging method by providing an enumeration constant ENUM_MA_METHOD:
    1. MODE_SMA, for a simple averaging,
    2. MODE_EMA, for an exponential averaging,
    3. MODE_SMMA, for a smoothed average,
    4. MODE_LWMA, for a linear-weighted averaging.
  5. Provide the moving average shift. The default value is zero.
  6. Specify the applied price using the enumeration value from the constant ENUM_APPLIED_PRICE:
    1. PRICE_CLOSE, for the close price,
    2. PRICE_OPEN, for the open price,
    3. PRICE_HIGH, for the highs within the specified period,
    4. PRICE_LOW, for the lows within the specified period.
  7. Provide the price deviation of the Envelopes upper and lower band from the moving average in percentage.
  8. Specify which data points to collect:
    1. MODE_MAIN, for midpoints,
    2. MODE_UPPER, for the upper band,
    3. MODE_LOWER, for the lower band.
  9. Provide the index of the indicator buffer where zero is the latest data point and one the previous data point.

MQL4 Envelopes function
Fig.202 MQL4 Envelopes function

MQL5

When calling the iEnvelopes() function, the function will create the Envelopes data points 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 you can use the function Symbol() to get the symbol from the chart where the Expert Advisor is attached to.
  2. Provide the time frame of the data point to extract using the enumeration value from the constant ENUM_TIMEFRAMES:
    1. PERIOD_CURRENT, to get data points from the current timeframe from the chart to which the Expert Advisor is attached to.
    2. PERIOD_M1, for the one-minute timeframe
    3. PERIOD_D1, for the daily timeframe.
  3. Specify the moving average period. The default setting is 20-period.
  4. Provide the horizontal shift of the indicator. The default value is zero.
  5. Specify the averaging method with the enumeration value from the constant ENUM_MA_METHOD:
    1. MODE_SMA, for the simple averaging.
    2. MODE_EMA, for the exponential averaging.
    3. MODE_SMMA, for the smoothed average.
    4. MODE_LVMA, for the linear-weighted averaging.
  6. Provide the applied price method with the enumeration value from the constant ENUM_APPLIED_PRICE:
    1. PRICE_CLOSE, for the close price.
    2. PRICE_OPEN, for the open price.
    3. PRICE_HIGH, for the highs within the specified period.
    4. PRICE_LOW, for the lows within the specified period.
  7. Specify the price deviation of the Envelopes indicator upper and lower band from the moving average expressed in percentage.

Note that to retrieve data points from the iEnvelopes() function, you need to use it in combination with the function CopyBuffer(). You may need to declare two dynamic arrays to store the upper and lower band data points.

MQL5 Envelopes function
Fig.203 MQL5 Envelopes function

Practical case

MQL5

Fig.204 demonstrates the use of the Envelopes indicator to generate a price breakout and price breakdown trading strategy using the upper and lower band crossover to identify signals.

Line 6: Declare the indicator handle and assign iEnvelopes() function handle with the specified parameters.

Line 7 – 8: Declare the indicator “double type” array to store the Envelopes data points.

Line 10 – 11: Copy the latest hundred data points for each data set. Note the upper band buffer index is zero while the lower band buffer index is one.

Line 13 – 27: IF statement to assess the underlying instrument’s price action with the indicator upper and lower band to define the condition of the buy and sell signal.

Envelopes indicator breakout and breakdown strategy in MQL5
Fig.204 Envelopes indicator breakout and breakdown strategy in MQL5

MQL4

Fig.205 demonstrates the use of the Envelopes indicator to create a simple price breakout and price breakdown trading strategy. A buy and sell signal is identified when the underlying instrument’s price crosses the upper or the lower band.

Line 34 – 42: Declare variables that store the indicator’s data points. Note, the last parameter from the function iEnvelopes() is the index that defines the data points position where zero is the most recent data.

Line 45 – 62: IF statement to assess the underlying instrument’s price action with the indicator upper and lower band to define the condition of the buy and sell signal.

205 Envelopes indicator breakout and breakdown strategy in MQL4
Fig.205 Envelopes indicator breakout and breakdown strategy in MQL4

Leave a Reply

3  +  4  =