Learn how to design a trading system by Alligator
Introduction
Here is a new article from our series about how to design a trading system based on the most popular technical indicators. In this article, we will learn in detail about the Alligator indicator by learning what it is, it measures, how we can calculate it, and how we can read and use it. Then we will create a trading system based on some simple strategies based on the main objective of this indicator. We will cover this indicator through the following topics:
- Alligator definition
- Alligator strategy
- Alligator strategy blueprint
- Alligator trading system
- Conclusion
I advise you to try to apply what you learn by yourself especially the codes in this article to develop your programming skills as this is an important step to achieve this objective of development. We will use in this article MQL5 (MetaQuotes Language 5) IDE which is built-in in the MetaTrader 5 trading terminal, if you do not know how to download the MetaTrader 5 and how to use MQL5 you can read the topic of Writing MQL5 code in MetaEditor to learn more about that.
Disclaimer: All information provided 'as is' only for educational purposes and is not prepared for trading purposes or advice. The information does not guarantee any kind of result. If you choose to use these materials on any of your trading accounts, you will do that at your own risk and you will be the only responsible.
Alligator definition
The Alligator technical indicator is a trend-following, developed by Bill Williams. It is based on that trend is not the most time of the age of financial market instruments and it is only 15% to 30% of the time while it consumes the most of time ranging or sideways it takes about 85% to 70% of the time. So, we have a trend sometimes either uptrend or downtrend but most of the time we have ranging periods. If you want to learn more about trends, their types, and how we can identify them, you can read the topic of trend definition from my previous article it will be useful in this context. It uses some of Fibonacci numbers in the calculation the same as we will see as it uses three smoothed moving averages with a set of numbers five, eight, thirteen periods. As we said that the Alligator indicator consists of three smoothed moving averages, and they are the same as the following:
- The Alligator Jaw.
- The Alligator Teeth.
- The Alligator Lips.
We can calculate this indicator manually through the following steps:
The Alligator Jaw = SMMA (Median price, 13, 8)
The Alligator Teeth = SMA (Median price, 8, 5)
The Alligator Lips = SMMA (Median price, 5, 3)
Median Price = (High + Low)/2
Where:
SMMA = smoothed Moving Average
Median price = the median price of data
High = the highest price
Low = the lowest price
After the previous steps, we will get the Alligator indicator, but we do not need to calculate it manually as we have it as a built-in indicator in the MetaTrader 5 terminal and all we need to do is choose it from the available technical indicators the same as the following:
While Opening the MetaTrader 5 terminal, we will press Insert --> Indicators --> Bill Williams --> Alligator.
After that we will find the parameters of it the same as the following:
1 - the period of the Alligator Jaws.
2 - the horizontal shift of the Alligator Jaws.
3 - the period of the Alligator Teeth.
4 - the horizontal shift of the Alligator Teeth.
5 - the period of the Alligator Lips.
6 - the horizontal shift of the Alligator Lips.
7 - the method of smoothing.
8 - the usable price type.
Through the following window we can determine the style of the indicator:
1 - the color of the Jaws line.
2 - the type of the jaws line.
3 - the thickness of the jaws line.
4 - the color of the teeth line.
5 - the type of the teeth line.
6 - the thickness of the teeth line.
7 - the color of the lips line.
8 - the type of the lips line.
9 - the thickness of the lips line.
After determining all parameters and pressing "OK", we can find that the indicator is attached to the chart the same as the following:
As we can see in the previous chart we have the indicator inserted into the chart and we have three lines:
1 - Alligator Lips
2 - Alligator Teeth
3 - Alligator Jaws
We can read the indicator simply by observing the position of these lines from prices. If the price is above lips, teeth, then jaws, we can say the trend is up. If the price is below lips, teeth, then jaws, we can say that the trend is down. If the price is moving through the three lines, we can say that we have a sideways.
Alligator strategy
There are many ways to use the Alligator indicator in trading, but I'll mention simple strategies that can be used based on the main idea of this topic for education and you must test them before using them in your real account to make sure that they will be profitable. You may find that you need to optimize mentioned strategies, and this is normal you can do that to reach the best formula that can be useful for your trading style.
Based on this strategy, we need to get signals of bullish and bearish signals based on specific conditions. If the lips value is greater than two values the teeth value and jaws value and at the same time the teeth value and the jaws value, it will be a bullish signal. In other case, if the lips value is lower than the teeth value and jaws value and at the same time the teeth value is lower than the jaws value, it will be a bearish signal.
lips > teeth value, lips > jaws value, and teeth > jaws value --> bullish
lips < teeth value, lips < jaws value, and teeth < jaws value --> bearish
Based on this strategy, we need to get signals of buy and sell depending on the crossover between lips, teeth, and jaw values. If the lips value is greater than the teeth value and the jaw value, this will be a buy signal. In the other scenario, if the lips value is lower than the teeth and jaws value, it will be a sell signal.
lips > teeth value, lips > jaws value --> buy
lips < teeth value, lips < jaws value --> sell
Based on this strategy, we need to get signals of buy and sell based on the crossover between lips, teeth, and jaws after that the crossover between the closing price and the teeth value. If the lips line is lower than the teeth and the jaws then the closing price is greater than the teeth value, it will be a buy signal. In the other case, if the lips line is greater than the teeth and the jaws then the closing price is lower than the teeth value, it will be a sell signal.
lips < teeth value, lips < jaws value and closing price > teeth --> buy
lips > teeth value, lips > jaws value and closing price < teeth --> sell
Alligator strategy blueprint
Now, we need to create a trading system based on mentioned strategies in the previous topic, and to do that we will design a step-by-step blueprint to help us create this trading system.
According to this strategy, we need to create a trading system that can be used to generate signals of bullish or bearish based on continuously checking for the following values:
- Lips value
- Teeth value
- Jaws value
We need the trading system to determine the positions of these values to generate suitable signals based on that. If the lips line is greater than teeth, lips greater than jaws, and teeth line is greater than the jaws, in this case, we need the trading system to return the following values as a comment on the chart:
- Bullish
- Jaws value = n
- teeth value = n
- lips value = n
In the other case, if the lips line is lower than the teeth line, the lips line is lower than the jaws line and at the same time, the teeth line is lower than the jaws line, in this case, we need the trading system to return the following values as a comment on the chart:
- Bearish
- Jaws value = n
- teeth value = n
- lips value = n
The following is a step-by-step blueprint of this trading system:
According to this strategy, we need to create a trading system that can be used to generate signals of buy or sell based on continuously checking the following values to determine the positions of each one of them:
- Lips value
- Teeth value
- Jaws value
If the lips line is greater than the teeth value and at the same time, the lips line is greater than the jaws value, in this case, we need the trading system to return a comment on the chart with the following values:
- Buy
- Jaws value = n
- teeth value = n
- lips value = n
In the other case, if the lips line is lower than the teeth value and at the same time, the lips line is lower than the jaws line, we need the trading system to return a comment on the chart as a signal with the following values:
- Sell
- Jaws value = n
- teeth value = n
- lips value = n
The following is a step-by-step blueprint of this trading system:
According to this strategy, we need to create a trading system to generate buy and sell signals based on checking the following values to determine the position of each one of them:
- Lips value
- Teeth value
- Jaws value
- Closing price
If the lips value is lower than the teeth one, the lips line is lower than the jaws line and then, the closing price became above the teeth value, we need the trading system to return a comment on the chart as a signal with the following values:
- Buy
- Lips value = n
- Teeth value = n
- Closing price = n
- Jaws value = n
If the lips line is greater than the teeth line, the lips line is higher than the jaws line and then, the closing price became below the teeth value, we need the trading system to return a comment on the chart as a signal with the following values:
- Sell
- Lips value = n
- Teeth value = n
- Closing price = n
- Jaws value = n
The following is a step-by-step blueprint of this trading system:
Alligator trading system
Now, we will create a trading system for each mentioned strategy to be executed in the MetaTrader 5 terminal to generate desired signals automatically. First, we will create a base trading system to generate a signal of the Alligator components values to use in our trading system. The following are for steps to create this type of system:
Creating arrays of each one of the Alligator components (Lips, Teeth, Jaws).
double jawsArray[]; double teethArray[]; double lipsArray[];
Sorting data in these arrays by using the "ArraySetAsSeries" function. Its parameters:
- array[]
- flag
ArraySetAsSeries(jawsArray,true); ArraySetAsSeries(teethArray,true); ArraySetAsSeries(lipsArray,true);
Defining the Alligator by using the "iAlligator" function. Its parameters:
- symbol: we will use (_Symbol) to be applied to the current symbol.
- period: we will use (_Period) to be applied to the current time frame.
- jaw_period: to determine the period of the calculation of jaws, we will use (13).
- jaw_shift: to determine the horizontal shift of jaws, we will use (8).
- teeth_period: to determine the period of the calculation of teeth, we will use (8).
- teeth_shift: to determine the horizontal shift of teeth, we will use (5).
- lips_period: to determine the period of the calculation of lips, we will use (5).
- lips_shift: to determine the horizontal shift of lips, we will use (3).
- ma_method: to determine the type of moving average, we will use(MODE_SMA).
- applied_price: to determine the type of price, we will use (PRICE_MEDIAN).
int alligatorDef=iAlligator(_Symbol,_Period,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN);
Defining data and storing results by using the "CopyBuffer" function. Its parameters:
- indicator_handle: to determine the indicator handle, we will use (alligatorDef).
- buffer_num: to determine the indicator buffer number, we will use (0 for jaws), (1 for teeth), and (2 for lips).
- start_pos: to determine the start position, we will determine (0).
- count: to determine the amount to copy, we will use (3).
- buffer[]: to determine the target array to copy, we will use (jawsArray, teethArray, lipsArray).
CopyBuffer(alligatorDef,0,0,3,jawsArray); CopyBuffer(alligatorDef,1,0,3,teethArray); CopyBuffer(alligatorDef,2,0,3,lipsArray);
Getting values of three components.
double jawsValue=NormalizeDouble(jawsArray[0],5); double teethValue=NormalizeDouble(teethArray[0],5); double lipsValue=NormalizeDouble(lipsArray[0],5);
Commenting with three components of the indicator.
Comment("jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue);
The following is the full code to create this trading system:
//+------------------------------------------------------------------+ //| Simple Alligator System.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //-------------------------------------------------------------------- void OnTick() { //creating price array double jawsArray[]; double teethArray[]; double lipsArray[]; //Sorting data ArraySetAsSeries(jawsArray,true); ArraySetAsSeries(teethArray,true); ArraySetAsSeries(lipsArray,true); //define Alligator int alligatorDef=iAlligator(_Symbol,_Period,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN); //define data and store result CopyBuffer(alligatorDef,0,0,3,jawsArray); CopyBuffer(alligatorDef,1,0,3,teethArray); CopyBuffer(alligatorDef,2,0,3,lipsArray); //get value of current data double jawsValue=NormalizeDouble(jawsArray[0],5); double teethValue=NormalizeDouble(teethArray[0],5); double lipsValue=NormalizeDouble(lipsArray[0],5); //comment on the chart Comment("jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue); } //+------------------------------------------------------------------+
After compiling this code, we will find it in the navigator among Expert Advisors folder the same as the following:
By dragging and dropping it on the desired chart, we will find the window of this EA the same as the following:
After pressing "OK" after ticking next to "Allow Algo Trading", we will the EA attached to the chart the same as the following:
As we can on the previous chart in the top right corner that the EA is attached. Now, we're ready to receive signals. The following is an example from testing to check for generated signals.
As we can see in the previous chart, we have a comment on the top left corner with the following values:
Jaws value = n
teeth value = n
lips value = n
We can create a trading system based on this strategy the same as the following full code:
//+------------------------------------------------------------------+ //| Alligator Trend Identifier.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ void OnTick() { //creating three arrays of Alligator components double jawsArray[]; double teethArray[]; double lipsArray[]; //Sorting data ArraySetAsSeries(jawsArray,true); ArraySetAsSeries(teethArray,true); ArraySetAsSeries(lipsArray,true); //define Alligator int alligatorDef=iAlligator(_Symbol,_Period,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN); //define data and store result CopyBuffer(alligatorDef,0,0,13,jawsArray); CopyBuffer(alligatorDef,1,0,13,teethArray); CopyBuffer(alligatorDef,2,0,13,lipsArray); //get value of current data double jawsValue=NormalizeDouble(jawsArray[0],5); double teethValue=NormalizeDouble(teethArray[0],5); double lipsValue=NormalizeDouble(lipsArray[0],5); //conditions of strategy if(lipsValue>teethValue && lipsValue>jawsValue && teethValue>jawsValue) { Comment("Bullish","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue); } if(lipsValue<teethValue && lipsValue<jawsValue && teethValue<jawsValue) { Comment("Bearish","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue); } } //+------------------------------------------------------------------+
Differences in this code.
Conditions of the strategy, in case of bullish:
if(lipsValue>teethValue && lipsValue>jawsValue && teethValue>jawsValue) { Comment("Bullish","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue); }
In case of bearish:
if(lipsValue<teethValue && lipsValue<jawsValue && teethValue<jawsValue) { Comment("Bearish","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue); }
After compiling this code, and executing the created expert the same as we learned, we will find this EA is attached to the chart the same as the following:
As we can see in the previous chart that we have the EA is attached to the chart in the top right corner and we're ready to get our bullish or bearish signals based on this strategy. The following are examples of these signals:
In case of bullish:
As we can see in the previous chart, we have a comment as a signal on the top right corner with the following values:
- Bullish
- Jaws value = n
- teeth value = n
- lips value = n
In case of bearish:
As we can see in the previous chart, we have a comment as a signal of bearish on the top right corner with the following values:
- Bearish
- Jaws value = n
- teeth value = n
- lips value = n
We have three lines moving above prices, So, we got a bearish signal.
The following code is for creating a trading system based on this strategy:
//+------------------------------------------------------------------+ //| Alligator Signals System .mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ void OnTick() { //creating three arrays of Alligator components double jawsArray[]; double teethArray[]; double lipsArray[]; //Sorting data ArraySetAsSeries(jawsArray,true); ArraySetAsSeries(teethArray,true); ArraySetAsSeries(lipsArray,true); //define Alligator int alligatorDef=iAlligator(_Symbol,_Period,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN); //define data and store result CopyBuffer(alligatorDef,0,0,13,jawsArray); CopyBuffer(alligatorDef,1,0,13,teethArray); CopyBuffer(alligatorDef,2,0,13,lipsArray); //get value of current data double jawsValue=NormalizeDouble(jawsArray[0],5); double teethValue=NormalizeDouble(teethArray[0],5); double lipsValue=NormalizeDouble(lipsArray[0],5); //conditions of strategy if(lipsValue>teethValue && lipsValue>jawsValue) { Comment("Buy","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue); } if(lipsValue<teethValue && lipsValue<jawsValue) { Comment("Sell","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue); } } //+------------------------------------------------------------------+
Differences in this code.
Conditions of this strategy.
In case of buy signal:
if(lipsValue>teethValue && lipsValue>jawsValue) { Comment("Buy","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue); }
In case of sell signal:
if(lipsValue<teethValue && lipsValue<jawsValue) { Comment("Sell","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "lipsValue = ",lipsValue); }
After compiling this code and executing it in the trading terminal, we can find it is attached to the chart the same as the following:
We see on the top right corner in the previous chart that the EA is attached and we're ready to receive our buy or sell signals based on this strategy the following are for examples from testing.
In case of buy signal:
As we can see that we have our desired signal with the following values:
- Buy
- Jaws value = n
- teeth value = n
- lips value = n
In case of sell signal:
We have our desired signal with the following values:
- Sell
- Jaws value = n
- teeth value = n
- lips value = n
We have three lines that are moving above prices, So, we got a sell signal.
The following is for the full code to create a trading system based on this strategy:
//+------------------------------------------------------------------+ //| Alligator Signals System Enhancement.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ void OnTick() { //creating three arrays of Alligator components double jawsArray[]; double teethArray[]; double lipsArray[]; MqlRates pArray[]; //Sorting data ArraySetAsSeries(jawsArray,true); ArraySetAsSeries(teethArray,true); ArraySetAsSeries(lipsArray,true); int Data=CopyRates(_Symbol,_Period,0,1,pArray); //define Alligator int alligatorDef=iAlligator(_Symbol,_Period,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN); //define data and store result CopyBuffer(alligatorDef,0,0,13,jawsArray); CopyBuffer(alligatorDef,1,0,13,teethArray); CopyBuffer(alligatorDef,2,0,13,lipsArray); //get value of current data double jawsValue=NormalizeDouble(jawsArray[0],5); double teethValue=NormalizeDouble(teethArray[0],5); double lipsValue=NormalizeDouble(lipsArray[0],5); //conditions of strategy if(lipsValue<teethValue && lipsValue<jawsValue && pArray[0].close>teethValue) { Comment("Buy","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "closingPrice = ",pArray[0].close,"\n", "lipsValue = ",lipsValue); } if(lipsValue>teethValue && lipsValue>jawsValue && pArray[0].close<teethValue) { Comment("Sell","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "closingPrice = ",pArray[0].close,"\n", "lipsValue = ",lipsValue); } } //+------------------------------------------------------------------+
Differences in this code.
Creating one more array for prices by using the "MqlRates" function to store information about prices, volumes, and spread.
MqlRates pArray[];
Getting the historical data of MqlRates by using the "CopyRates". Its parameters:
- symbol_name: to determine the symbol name, we will use (_Symbol).
- timeframe: to determine the period, we will use (_period).
- start_pos: to determine the start position, we will use (0).
- count: to determine the data count to copy, we will use (1).
- rates_array[]: to determine the target array to copy, we will use (pArray).
int Data=CopyRates(_Symbol,_Period,0,1,pArray);
Conditions of the strategy.
In case of buy signal:
if(lipsValue<teethValue && lipsValue<jawsValue && pArray[0].close>teethValue) { Comment("Buy","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "closingPrice = ",pArray[0].close,"\n", "lipsValue = ",lipsValue); }
In case of sell signal:
if(lipsValue>teethValue && lipsValue>jawsValue && pArray[0].close<teethValue) { Comment("Sell","\n", "jawsValue = ",jawsValue,"\n", "teethValue = ",teethValue,"\n", "closingPrice = ",pArray[0].close,"\n", "lipsValue = ",lipsValue); }
After compiling this code and executing the EA we will find it attached to the chart the same as the following to get our buy and sell signals.
We have the EA attached in the top right corner and we can receive our desired signals the following are examples from testing.
In case of buy signal:
We have a comment in the top left corner with the following values:
- Buy
- Jaws value = n
- teeth value = n
- Closing price = n
- lips value = n
In case of sell signal:
We have a comment as a signal in the top left corner:
- Sell
- Jaws value = n
- teeth value = n
- Closing price = n
- lips value = n
As three lines are moving below prices then we can find that the closing price is closed below the Alligator teeth line.
Conclusion
We learned in this article in detail about the Alligator technical indicator which can be used to confirm the trend in addition to generating buy and sell signals. We learned how we can calculate it manually, and how we can use based on the simple mentioned strategies:
- Alligator Trend Identifier: To generate bullish or bearish signals based on the position of the three lines of Alligator (Lips, Teeth, and Jaws).
- Alligator Signals System: To generate buy or sell signals by classical method crossover based on the crossover between the three lines of the indicator.
- Alligator Signals System Enhancement: To generate buy or sell signals by another method to get these signals earlier based on the position of three lines of the indicators and the crossover between the closing price and the teeth.
We designed a step-by-step blueprint for each mentioned strategy to help us to organize our ideas to create a trading system smoothly, easily, and effectively. We also created a trading system for each mentioned strategy to be used in the MetaTrader 5 trading terminal and generate signals automatically as per what we designed and coded. I need to confirm here again that you must test any mentioned strategy before using it on your real account to make sure that they are useful and profitable for you as there is nothing is suitable for all of us in addition to that the main objective here is to share knowledge or the main purpose of this article is education only.
I hope that you tried to apply what you learned by yourself the same I advised you to improve your coding skills and get complete benefit from reading this article and I hope that you found it useful for your trading and got useful insights into the topic of this article or even any related topic. If you want to read more similar articles, you can read my previous articles in this series as we shared the most popular technical indicators like stochastic, RSI, Bollinger Bands, Moving Averages, Envelopes, MACD, ADX, etc., and how we can create a trading system based on them.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
New article Learn how to design a trading system by Alligator has been published:
Author: Mohamed Abdelmaaboud
Hi,
I found that this link of your article is not valid: Writing MQL5 code in MetaEditor. One get a 404 error.
Beside that this is wrong:
We have three lines moving below prices, So, we got a bearish signal.
It should be (imho):
Please can you correct it?
Hi,
I found that this link of your article is not valid: Writing MQL5 code in MetaEditor. One get a 404 error.
Beside that this is wrong:
We have three lines moving below prices, So, we got a bearish signal.
It should be (imho):
Please can you correct it?
Thanks for your note and I will correct them.