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
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]
|
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:
- Provide the handle that is returned by the iAlligator() function;
- Provide the buffer number (Fig.150) where:
- Zero is the Alligator’s jaws;
- One is the Alligator’s teeth;
- And two is the Alligator’s lips.
- 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 iAlligator() function.
- Provide three variables where the data will be stored. The variables are of “double type.”
How to retrieve Alligator Indicator data with CopyBuffer() function?
- Declare a variable for the handle: int IndicatorHandle = iAlligator();
- Declare three variables to store indicator data:
- double alligator_jaws;
- double alligator_teeth;
- double alligator_lips.
- Copy the data to the indicator variable and retrieve data points:
- CopyBuffer(IndicatorHandle, 0, 0, 100, alligator_jaws);
- CopyBuffer(IndicatorHandle, 0, 0, 100, alligator_teeth);
- CopyBuffer(IndicatorHandle, 0, 0, 100, alligator_lips);
- Retrieve Alligator Indicator data point with alligator_jaws[0], alligator_jaws[1];
- Retrieve Alligator Indicator data point with alligator_teeth[0], alligator_teeth[1];
- Retrieve Alligator Indicator data point with alligator_lips[0], alligator_lips[1].
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:
- 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 jaws line averaging period, for instance, 13 for the 13-period.
- Provide the jaws line shift relative to the attached chart, for instance, 8 for 8-period.
- Provide the teeth line averaging period, for instance, 8 for the 8-period.
- Provide the teeth line shift relative to the chart, for instance, 5 for the 5-period.
- Provide the lips line averaging period, for instance, 5 for the 5-period.
- Provide the lips line shift relative to the chart, for instance, 3 for the 3-period.
- Provide the applied averaging method using the ENUM_MA_METHOD constant:
- MODE_SMA for simple averaging;
- MODE_EMA for exponential averaging;
- MODE_SMMA for smoothed averaging;
- MODE_LWMA for linear-weighted averaging.
- Provide the data source using the identifier of the indicator line:
- MODE_GATORJAW to get jaws line data points;
- MODE_GATORTEETH to get teeth line data point;
- MODE_GATORLIPS to get lips line data point.
- Provide the applied price with the constant ENUM_APPLIED_PRICE:
- PRICE_CLOSE for the close price;
- PRICE_OPEN for the open price;
- PRICE_HIGH for the highest price in the period;
- PRICE_LOW for the lowest price in the period;
- PRICE_MEDIAN for the median price;
- PRICE WEIGHTED for the weighted close, high, and low.
- Provide the index value from the indicator buffer where zero is the latest data and one the previous data point.
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:
- 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 to;
- PERIOD_M1, for the one-minute timeframe;
- PERIOD_D1, for the daily timeframe.
- Provide the jaws line averaging period, for instance, 13 for the 13-period.
- Provide the jaws line shift relative to the attached chart, for instance, 8 for 8-period.
- Provide the teeth line averaging period, for instance, 8 for the 8-period.
- Provide the teeth line shift relative to the chart, for instance, 5 for the 5-period.
- Provide the lips line averaging period, for instance, 5 for the 5-period.
- Provide the lips line shift relative to the chart, for instance, 3 for the 3-period.
- Provide the applied averaging method using the ENUM_MA_METHOD constant:
- MODE_SMA for simple averaging;
- MODE_EMA for exponential averaging;
- MODE_SMMA for smoothed averaging;
- MODE_LWMA for linear-weighted averaging.
- Provide the applied price with the constant ENUM_APPLIED_PRICE:
- PRICE_CLOSE for the close price;
- PRICE_OPEN for the open price;
- PRICE_HIGH for the highest price in the period;
- PRICE_LOW for the lowest price in the period;
- PRICE_MEDIAN for the median price;
- 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().
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.
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.
Fig.155 Compare previous Alligator Indicator data points with the current data point in MQL4