Creating an Expert Advisor for Dark Cloud Cover and Piercing Line Patterns with CCI Confirmation

Mike 2011.02.25 02:37 165 0 0

The MQL5 Wizard is a powerful tool that allows you to create custom Expert Advisors (EAs) based on the Standard Library classes that come with the MetaTrader 5 client terminal. If you want to quickly test your trading ideas, all you need to do is create a trading signals class. You can find the structure of this class and an example in the article MQL5 Wizard: How to Create a Module of Trading Signals.

The basic concept here is that you derive your trading signals class from CExpertSignal. After that, you’ll need to override the virtual methods LongCondition() and ShortCondition() with your custom methods.

There’s a book called "Strategies of Best Traders" (in Russian), which covers various trading strategies. We’ll be focusing on reversal candlestick patterns confirmed by indicators like Stochastic, CCI, MFI, and RSI.

The best approach is to create a separate class derived from CExpertSignal to check for the formation of candlestick patterns. For confirming trade signals generated by these patterns, it’s enough to write a class derived from CCandlePattern and add the necessary features, such as confirmation by oscillators.

In this post, we're going to look at trade signals based on the "Dark Cloud Cover/Piercing Line" reversal candlestick patterns confirmed by the CCI indicator. The module for these trade signals will utilize the CCandlePattern class, which offers a simple example for creating trade signals using candlestick patterns.


1. Understanding the Candlestick Patterns

1.1. Dark Cloud Cover

The Dark Cloud Cover is a bearish reversal pattern that appears at the end of an uptrend. This pattern starts with a long white candlestick on the first day, followed by a gap up on the second day. However, the second day closes below the midpoint of the first day.

Fig. 1. Dark Cloud Cover Candlestick Pattern

Fig. 1. Dark Cloud Cover Candlestick Pattern

The recognition of the Dark Cloud Cover pattern is implemented in the CheckPatternDarkCloudCover() method of the 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 the CCandlePattern class is used to check the formation of the Dark Cloud Cover candlestick pattern.


1.2. Piercing Line

The Piercing Line pattern starts with a gap down on the second day, continuing the downtrend. However, the close of the second day is above the midpoint of the first day's body, indicating that a bottom may be forming. This pattern is easier to recognize on candlestick charts than on bar charts. The deeper the close of the second day into the body of the first day, the more reliable the reversal signal is likely to be.

Fig. 2. Piercing Line Candlestick Pattern

Fig. 2. Piercing Line Candlestick Pattern

The recognition of the Piercing Line pattern is implemented in the CheckPatternPiercingLine() method of the 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 the CCandlePattern class is used to check the formation of the Piercing Line candlestick pattern.


2. Trade Signals Confirmed by the CCI Indicator

To open long or short positions, the trading signals must be confirmed by the CCI indicator. Specifically, the CCI value needs to exceed certain critical levels: below -50 for long positions and above 50 for short positions.

Closing an open position depends on the CCI values and can occur under two scenarios:

  1. If the CCI line has reached the opposite critical level (80 for long positions and -80 for short positions).
  2. If the reverse signal isn’t confirmed (when the CCI reaches -80 for long positions and 80 for short positions).

Fig. 3. Dark Cloud Cover Pattern Confirmed by CCI Indicator

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 the short position (returns 40);
  • int CDC_PL_CCI::ShortCondition() - checks conditions to open short position (returns 80) and close the long position (returns 40).

2.1. Opening Long Position / Closing Short Position

  1. The formation of the "Piercing Line" pattern must be confirmed by the CCI indicator: CCI(1) < -50 (the last completed bar's CCI value must be less than -50).

  2. The short position should be closed if the CCI indicator crosses upward the critical level -80 or 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 conditions only at new 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. Opening Short Position / Closing Long Position

  1. The formation of the "Dark Cloud Cover" pattern must be confirmed by the CCI indicator: CCI(1) > 50 (the value of the last completed bar's CCI must be greater than 50).

  2. The long position should be closed if the CCI indicator crosses 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 conditions only at new 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 an Expert Advisor Using MQL5 Wizard

The CDC_PL_CCI class isn’t part of the Standard Library classes, so you’ll need to download the acdc_pl_cci.mqh file (check the attachments) and save it to the client_terminal_data\folder\MQL5\Include\Expert\Signal\MySignals. You’ll do the same with the candlepatterns.mqh file. You can use these in MQL5 Wizard after restarting the MetaEditor.

To create an Expert Advisor, launch the MQL5 Wizard:

Fig. 4. Creating Expert Advisor Using MQL5 Wizard

Fig. 4. Creating Expert Advisor Using MQL5 Wizard

Next, specify the name of your Expert Advisor:

Fig. 5. General Properties of the Expert Advisor

Fig. 5. General Properties of the Expert Advisor

After that, select the modules of trade signals you wish to use.

Fig. 6. Signal Properties of the Expert Advisor

Fig. 6. Signal Properties of the Expert Advisor

In our case, we’ll be using just one module of trade signals.

Add the "Signals based on Dark Cloud Cover/Piercing Line confirmed by CCI" module of trading signals:

Fig. 7. Signal Properties of the Expert Advisor

Fig. 7. Signal Properties of the Expert Advisor

Module of trade signals added:

Fig. 8. Signal Properties of the Expert Advisor

Fig. 8. Signal Properties of the Expert Advisor

You can choose any trailing properties, but we’ll stick with "Trailing Stop not used":

Fig. 9. Trailing Properties of the Expert Advisor

Fig. 9. Trailing Properties of the Expert Advisor

For money management properties, we’ll select "Trading with fixed trade volume":

Fig. 10. Money Management Properties of the Expert Advisor

Fig. 10. Money Management Properties of the Expert Advisor

By clicking the "Finish" button, you’ll get the code of the generated Expert Advisor, which will be located in Expert_ADC_PL_CCI.mq5, saved in the terminal_data_folder\MQL5\Experts\.

The default input parameters of the generated Expert Advisor are as follows:

//--- 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)

These parameters must be replaced with:

//--- 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 you to specify threshold levels for opening and closing positions.

In the code for the LongCondition() and ShortCondition() methods of the trade signals class, we initially set fixed values for the thresholds:

  • Open position: 80;
  • Close position: 40.

The Expert Advisor generated by MQL5 Wizard will open and close positions based on the "votes" from the trade signals modules. The main module's vote (which acts as a container for all added modules) is also taken into account, but its LongCondition() and ShortCondition() methods will always return 0.

The results from the main module are also included in the average of the "votes". In our case, we have one main module plus one trade signals module, so we need to consider this when setting the threshold values. That’s why the ThresholdOpen and ThresholdClose should be set to 40 = (0 + 80)/2 and 20 = (0 + 40)/2, respectively.

Setting Signal_StopLevel and Signal_TakeLevel to 0 means that positions will only close when the closing conditions are met.


2.4. History Backtesting Results

Let’s examine the backtesting results of the Expert Advisor on historical data (EURUSD H1, testing period: 01/01/2010 - 16/03/2011, PeriodCCI = 15, MA_period = 19).

For the Expert Advisor, we used a fixed volume (Trading Fixed Lot, 0.1), and the Trailing Stop algorithm was not utilized (Trailing Not Used).

Fig. 11. Testing Results of the Expert Advisor Based on Dark Cloud Cover/Piercing Line + CCI

Fig. 11. Testing Results of the Expert Advisor Based on Dark Cloud Cover/Piercing Line + CCI


The best set of input parameters can be determined using the Strategy Tester available in the MetaTrader 5 client terminal.

The code for the Expert Advisor created by the MQL5 Wizard is attached as expert_adc_pl_cci.mq5.


List
Comments 0