The MQL5 Wizard allows creating ready-made Expert Advisors based on the Standard library classes delivered together with the client terminal (see Creating Ready-Made Expert Advisors in MQL5 Wizard for the details). It allows to check your trade ideas quickly, all you need is to create your own trading signals class. The structure of this class and example can be found in the article MQL5 Wizard: How to Create a Module of Trading Signals.
The generic idea is the following: the class of trading signals is derived from CExpertSignal, the next, it’s necessary to override the LongCondition() and ShortCondition() virtual methods with your own methods.
There is a book “Strategies of best traders” (in Russian), there are many trading strategies are considered there, we will focus on reversal candlestick patterns, confirmed by Stochastic, CCI, MFI and RSI oscillators.
The best way is to create the separate class, derived from CExpertSignal for checking of formation of candlestick patterns. For confirmation of trade signals, generated by candlestick patterns, it’s sufficient to write the class, derived from CCandlePattern and add the necessary features (for example, confirmation by oscillators) there.
Here we will consider the signals, based “Dark Cloud Cover/Piercing Line” reversal candlestick pattern, confirmed by CCI indicator. The module of trade signals is based on the CCandlePattern class, it’s simple example of its use for creation of trade signals with candlestick patterns.
1. “Dark Cloud Cover” and “Piercing Line” reversal candlestick patterns
1.1. Dark Cloud Cover
It’s a bearish candlestick reversal that occurs at the end of uptrend. A long white candlestick is formed on the first day and a gap up is created on the second day. However, the second day closes below the midpoint of the first day.
Fig. 1. “Dark Cloud Cover” candlestick pattern
The recognition of “Dark Cloud Cover” pattern is implemented in CheckPatternDarkCloudCover() method of CCandlePattern class:
//+------------------------------------------------------------------+ //| Checks formation of "Dark Cloud Cover" candlestick pattern      | //+------------------------------------------------------------------+ bool CCandlePattern::CheckPatternDarkCloudCover()   { //--- Dark Cloud Cover   if((Close(2)-Open(2)>AvgBody(1))  && // (long white)       (Close(1)<Close(2))            && //       (Close(1)>Open(2))            && // (close within previous body)       (MidOpenClose(2)>CloseAvg(1))  && // (uptrend)       (Open(1)>High(2)))                // (open at new high)       return(true); //---   return(false);   }
The CheckCandlestickPattern(CANDLE_PATTERN_DARK_CLOUD_COVER) method of CCandlePattern class is used to check formation of “Dark Cloud Cover” candlestick pattern.
1.2. Piercing Line
The gap down on the second day perpetuates the downtrend. However, the second day’s close is above the midpoint of the first day’s body. This suggests to the bears that a bottom could be forming. This price action is not nearly as discernable using bar charts as it is with candlestick charts. The more penetration of the close on the second day to the first day’s body, the more probable the reversal signal will succeed.
Fig. 2. “Piercing Line” candlestick pattern
The recognition of “Piercing Line” pattern is implemented in CheckPatternPiercingLine() method of CCandlePattern class:
//+------------------------------------------------------------------+ //| Checks formation of "Piercing Line" candlestick pattern          | //+------------------------------------------------------------------+ bool CCandlePattern::CheckPatternPiercingLine()   { //--- Piercing Line   if((Close(1)-Open(1)>AvgBody(1)) && // (long white)       (Open(2)-Close(2)>AvgBody(1)) && // (long black)       (Close(1)>Close(2))          && //        (Close(1)<Open(2))            && // (close inside previous body)       (MidOpenClose(2)<CloseAvg(2)) && // (downtrend)       (Open(1)<Low(2)))                // (open lower than previous Low)       return(true); //---   return(false);   }
The CheckCandlestickPattern(CANDLE_PATTERN_PIERCING_LINE) method of CCandlePattern class is used to check formation of “Piercing Line” candlestick pattern.
2. Trade signals, confirmed by CCI indicator
The trading signals to open long or short position must be confirmed by CCI indicator. The value of CCI must be greater/lower than critical levels (-50 for long position and 50 for short position).
The closing of opened position depends on the values of CCI. It can be done in 2 cases:
- if CCI line has reached the opposite critical level (80 for long position and -80 for short position)
- if the reverse signal isn’t confirmed (when CCI reaches the following levels: -80 for long position and 80 for short position)
Fig. 3. “Dark Cloud Cover” pattern, confirmed by CCI indicator
- int CDC_PL_CCI::LongCondition() – checks conditions to open long position (returns 80) and close of the short position (returns 40);
- int CDC_PL_CCI::ShortCondition() – checks conditions to open short position (returns 80) and close of the long position (returns 40).
2.1. Open long position/Close short position
The formation of “Piercing Line” pattern must be confirmed by CCI indicator: CCI(1)<-50 (the value of the CCI of the last completed bar must be less than -50).
The short position must be closed if the CCI indicator has crossed upward the critical level -80 or crossed downward the critical level 80.
//+------------------------------------------------------------------+ //| Checks conditions for entry and exit from market                | //| 1) Market entry (open long position, result=80)                  | //| 2) Market exit (close short position, result=40)                | //+------------------------------------------------------------------+ int CDC_PL_CCI::LongCondition()   {   int result=0; //--- idx can be used to determine Expert Advisor work mode //--- idx=0 - in this case EA checks trade conditions at each tick //--- idx=1 - in this case EA checks trade consition only at news bars   int idx  =StartIndex(); //--- checking of conditions to open long position //--- formation of Piercing Line pattern and CCI<-50   if(CheckCandlestickPattern(CANDLE_PATTERN_PIERCING_LINE) && (CCI(1)<-50))     result=80; //--- checking of conditions to close short position //--- signal line crossover of overbought/oversold levels (downward -80, downward -80)   if(((CCI(1)>-80) && (CCI(2)<-80)) || ((CCI(1)<80) && (CCI(2)>80)))     result=40; //--- return result   return(result);   }
2.2. Open short position/Close long position
The formation of “Dark Cloud Cover” pattern must be confirmed by CCI indicator: CCI(1)>50 (the value of the CCI indicator of the last completed bar must be greater than 50).
The long position must be closed if CCI indicator has crossed downward the -80 or 80 levels.
//+------------------------------------------------------------------+ //| Checks conditions for entry and exit from market                | //| 1) Market entry (open short position, result=80)                | //| 2) Market exit (close long position, result=40)                  | //+------------------------------------------------------------------+ int CDC_PL_CCI::ShortCondition()   {   int result=0; //--- idx can be used to determine Expert Advisor work mode //--- idx=0 - in this case EA checks trade conditions at each tick //--- idx=1 - in this case EA checks trade consition only at news bars   int idx  =StartIndex(); //--- checking of conditions to open short position //--- formation of Dark Cloud Cover pattern and CCI>50   if(CheckCandlestickPattern(CANDLE_PATTERN_DARK_CLOUD_COVER) && (CCI(1)>50))     result=80; //--- checking of conditions to close long position //--- signal line crossover of overbought/oversold levels (downward -80, downward 80)   if(((CCI(1)<80) && (CCI(2)>80)) || ((CCI(1)<-80) && (CCI(2)>-80)))     result=40; //--- return result   return(result);   }
2.3. Creating Expert Advisor using MQL5 Wizard
The CDC_PL_CCI class isn’t included in the Standard Library classes, to use it, it’s necessary to download the acdc_pl_cci.mqh file (see attachments) and save it to the client_terminal_data\folder\MQL5\Include\Expert\Signal\MySignals. The same should be done with the candlepatterns.mqh file. You can use it in MQL5 Wizard after restart of the MetaEditor.
To create an Expert Advisor launch MQL5 Wizard:
Fig. 4. Creating Expert Advisor using MQL5 Wizard
Let’s specify the name of the Expert Advisor:
Fig. 5. General properties of the Expert Advisor
After that we need to select the modules of trade signals used.
Fig. 6. Signal properties of the Expert Advisor
In our case we use only one module of trade signals.
Adding the “Signals based on Dark Cloud Cover/Piercing Line confirmed by CCI” module of trading signals:
Fig. 7. Signal properties of the Expert Advisor
Module of trade signals added:
Fig. 8. Signal properties of the Expert Advisor
You can select any trailing properties, but we will use “Trailing Stop not used”:
Fig. 9. Trailing properties of the Expert Advisor
Concerning the money management properties, we will use “Trading with fixed trade volume”:
Fig. 10. Money management properties of the Expert Advisor
By pressing the “Finish” button, we will get the code of the generated Expert Advisor, located in Expert_ADC_PL_CCI.mq5, it will be saved in terminal_data_folder\MQL5\Experts\.
The default input parameters of the generated Expert Advisor:
//--- inputs for main signal input int            Signal_ThresholdOpen  =10;    // Signal threshold value to open [0...100] input int            Signal_ThresholdClose  =10;    // Signal threshold value to close [0...100] input double        Signal_PriceLevel      =0.0;   // Price level to execute a deal input double        Signal_StopLevel      =50.0;  // Stop Loss level (in points) input double        Signal_TakeLevel      =50.0; // Take Profit level (in points)
must be replaced to:
//--- inputs for main signal input int            Signal_ThresholdOpen  =40;    // Signal threshold value to open [0...100] input int            Signal_ThresholdClose  =20;    // Signal threshold value to close [0...100] input double        Signal_PriceLevel      =0.0;   // Price level to execute a deal input double        Signal_StopLevel      =0.0;    // Stop Loss level (in points) input double        Signal_TakeLevel      =0.0;    // Take Profit level (in points)
The Signal_ThresholdOpen/Signal_ThresholdClose input parameters allow to specify threshold levels for open and close of positions.
In code of the LongCondition() and ShortCondition() methods of the trade signals class we have specified the fixed values of the threshold:
- Open position: 80;
- Close position: 40.
The Expert Advisor, generated by MQL5 Wizard open and close position using the “votes” from the modules of trade signals. The vote of the main module (as container, it consist of all the modules added) is also used, but its LongCondition() and ShortCondition() methods always return 0.
The vote results of the main module is also used in “votes” averaging. In our case we have:Â main module + 1 module of trade signals, so we need to take this fact into account when setting of the threshold values. Because of this fact the ThresholdOpen and ThresholdClose must be set as 40=(0+80)/2 and 20=(0+40)/2.
The values of Signal_StopLevel and Signal_TakeLevel input parameters is set to 0, it means that closing of the positions will be done only when closing conditions will be true.
2.4. History backtesting results
Let’s consider backtesting of the Expert Advisor on historical data (EURUSD H1, testing period: 2010.01.01-2011.03.16, 2010.01.01-2011.03.16, PeriodCCI=15, MA_period=19).
In creation of Expert Advisor we used the fixed volume (Trading Fixed Lot, 0.1), Trailing Stop algorithm is not used (Trailing not used).
Fig. 11. Testing results of the Expert Advisor, based on Dark Cloud Cover/Piercing Line + CCI
The best set of input parameters can be found using the Strategy Tester of MetaTrader 5 client terminal.
The code of the Expert Advisor, created by MQL5 Wizard is attached in expert_adc_pl_cci.mq5.