The Alligator Indicator is another technical indicator developed by Bill Williams. The indicator is a combination of three smoothed moving averages (SMA) of 5-period, 8-period, and 13-period. The SMA movement and crossover determine the prospective convergence and divergence in the price action. Each of the three moving averages has an attributed name, jaw, teeth, and lips. The jaws line is the slowest turn while the lips line is the fastest.

In this guide, we will go through how to detect a buy and a sell signal with the Alligator Indicator and explain to you how to store the data points for each SMA and how to retrieve and exploit them. MQL4 Alligator Indicator function retrieves the indicator’s data point, unlike MQL5, which requires the data to be stored in the buffer and in a declared “double type” array. It may seem confusing, but it is actually not. We will explain step by step this MQL5 specific feature. And finally, we will show you a practical example of usage.

How does the indicator signal work for the Alligator Indicator?

Before we dive into the Alligator Indicator code implementation, let us explain the buy and sell signals interpretation. As this indicator is based on moving averages, the signal will be mainly derived from the price average and the three Alligator lines crossover representing a slow and fast MA.

Alligator Indicator signals

Alligator Indicator signal on chart
Fig.149 Alligator Indicator signal on chart

The Alligator’s jaw represented by the blue line is the slow-moving average, while the alligator’s lips line in green is the fast-moving average. Buy and sell signals occur when there is a crossover. A buy signal happens when the green line crosses above the blue line, while for a sell signal, the blue line must cross above the green line (Fig.149). The Alligator Indicator is also indicating the strength of a signal. If the line angle is near horizontal and narrow, it means that the underlying instrument is in a consolidation. It is recommended to enter a trade only if a trend has been established.

How Alligator Indicator stores its data points?

The Alligator Indicator keeps its data point in an indicator buffer which is a dynamic array. The size of the array is managed automatically by MetaTrader during the Expert Advisor loading phase. An indicator buffer is a time-series array and, therefore, specifically stores its data in reverse indexing. It means that the array is sorted by descending where the most recent data point is on the left side of the array. Time-series is used to store historical data and include 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 price at index zero and yesterday’s data at the position index one (Fig.150).

Note that the Alligator Indicator comprises three lines, the jaws, the teeth, and the lips. In MQL5, the jaws buffer index is zero, one for the teeth and two for the jaws. This concept of buffer index applies only to MQL5. MQL4 Alligator indicator returns the data point directly according to provided parameters.  

            [0]                                [1]                               [2]

Indicator data points array for the Alligator Indicator

Fig.150 Indicator data points array for the Alligator Indicator

MQL5

To access the Alligator Indicator data, MQL5 requires the use of the CopyBuffer() function. This method does not apply to MQL4 as the MQL4 indicators function directly return the value of the Alligator Indicator data point instead of the handle. The following parameters should be provided:

  1. Provide the handle that is returned by the iAlligator() function;
  2. Provide the buffer number (Fig.150) where:
    1. Zero is the Alligator’s jaws;
    2. One is the Alligator’s teeth;
    3. And two is the Alligator’s lips.
  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 iAlligator() function.
  5. Provide three variables where the data will be stored. The variables are of “double type.”

How to retrieve Alligator Indicator data with CopyBuffer() function?

  1. Declare a variable for the handle: int IndicatorHandle = iAlligator();
  2. Declare three variables to store indicator data:
    1. double alligator_jaws;
    2. double alligator_teeth;
    3. double alligator_lips.
  3. Copy the data to the indicator variable and retrieve data points:
    1. CopyBuffer(IndicatorHandle, 0, 0, 100, alligator_jaws);
    2. CopyBuffer(IndicatorHandle, 0, 0, 100, alligator_teeth);
    3. CopyBuffer(IndicatorHandle, 0, 0, 100, alligator_lips);
    4. Retrieve Alligator Indicator data point with alligator_jaws[0], alligator_jaws[1];
    5. Retrieve Alligator Indicator data point with alligator_teeth[0], alligator_teeth[1];
    6. Retrieve Alligator Indicator data point with alligator_lips[0], alligator_lips[1].

CopyBuffer() Function
Fig.151 CopyBuffer() Function

How to access the Alligator Indicator data?

MQL4

The iAlligator() function will return the data point according to these twelve 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 jaws line averaging period, for instance, 13 for the 13-period.
  4. Provide the jaws line shift relative to the attached chart, for instance, 8 for 8-period.
  5. Provide the teeth line averaging period, for instance, 8 for the 8-period.
  6. Provide the teeth line shift relative to the chart, for instance, 5 for the 5-period.
  7. Provide the lips line averaging period, for instance, 5 for the 5-period.
  8. Provide the lips line shift relative to the chart, for instance, 3 for the 3-period.
  9. Provide the applied averaging method using the ENUM_MA_METHOD constant:
    1. MODE_SMA for simple averaging;
    2. MODE_EMA for exponential averaging;
    3. MODE_SMMA for smoothed averaging;
    4. MODE_LWMA for linear-weighted averaging.
  10. Provide the data source using the identifier of the indicator line:
    1. MODE_GATORJAW to get jaws line data points;
    2. MODE_GATORTEETH to get teeth line data point;
    3. MODE_GATORLIPS to get lips line data point.
  11. Provide the applied price with the constant ENUM_APPLIED_PRICE:
    1. PRICE_CLOSE for the close price;
    2. PRICE_OPEN for the open price;
    3. PRICE_HIGH for the highest price in the period;
    4. PRICE_LOW for the lowest price in the period;
    5. PRICE_MEDIAN for the median price;
    6. PRICE WEIGHTED for the weighted close, high, and low.
  12. Provide the index value from the indicator buffer where zero is the latest data and one the previous data point.

MQL4 Alligator Indicator function
Fig.152 MQL4 Alligator Indicator function

MQL5

When calling the iAlligator() function, the function will create the Alligator Indicator in the MetaTrader global cache and returns its handle according to these ten 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. Provide the jaws line averaging period, for instance, 13 for the 13-period.
  4. Provide the jaws line shift relative to the attached chart, for instance, 8 for 8-period.
  5. Provide the teeth line averaging period, for instance, 8 for the 8-period.
  6. Provide the teeth line shift relative to the chart, for instance, 5 for the 5-period.
  7. Provide the lips line averaging period, for instance, 5 for the 5-period.
  8. Provide the lips line shift relative to the chart, for instance, 3 for the 3-period.
  9. Provide the applied averaging method using the ENUM_MA_METHOD constant:
    1. MODE_SMA for simple averaging;
    2. MODE_EMA for exponential averaging;
    3. MODE_SMMA for smoothed averaging;
    4. MODE_LWMA for linear-weighted averaging.
  10. Provide the applied price with the constant ENUM_APPLIED_PRICE:
    1. PRICE_CLOSE for the close price;
    2. PRICE_OPEN for the open price;
    3. PRICE_HIGH for the highest price in the period;
    4. PRICE_LOW for the lowest price in the period;
    5. PRICE_MEDIAN for the median price;
    6. PRICE WEIGHTED for the weighted close, high, and low.

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

MQL5 Alligator Indicator function
Fig.153 MQL5 Alligator Indicator function

Practical case

MQL5

Fig.154 demonstrates the use of the Alligator Indicator to generate a simple signal comparing the data points of the Alligator jaws line and Alligator lips line.

Line 181: Declare the indicator handle and assign the Alligator Indicator data according to provided parameters.

Line 182 – 184: Declare variables that will be used to store the Alligator lines data points.

Line 186 – 188: Copy the Alligator Indicator data points to the local buffer. Note that the second parameter 0, 1, and 2 is the buffer index that is specific to the Alligator lines, where zero is the jaws, one is the teeth and two is the lips.

Line 190 – 201: Conditional statement to compare the jaws and lips lines to execute instructions accordingly. The IF statement evaluates the jaws and lips lines crossover. 

Compare previous Alligator Indicator data points with the current data point in MQL5
Fig.154 Compare previous Alligator Indicator data points with the current data point in MQL5

MQL4

Fig.155 demonstrates the use of the Alligator Indicator to generate a simple signal comparing the data points of the Alligator jaws line and Alligator lips line.

Line 207 – 2017: Declare variables that will store the Alligator data points according to parameters provided to the iAlligator() function.

Line 219 – 230: Conditional statement to compare the jaws and lips lines to execute instructions accordingly. The IF statement evaluates the jaws and lips lines crossover.

Compare previous Alligator Indicator data points with the current data point in MQL4
Fig.155 Compare previous Alligator Indicator data points with the current data point in MQL4

Leave a Reply

9  +  1  =