Home System Trading Post

Harnessing MQL5 Wizard for Bullish and Bearish Harami Signals with CCI Confirmation

Attachments
311.zip (6.48 KB, Download 2 times)

Hey fellow traders! If you’re looking to enhance your trading strategy using the MQL5 Wizard, you’re in for a treat. This powerful tool allows you to whip up ready-made Expert Advisors (EAs) based on the Standard Library. This means you can swiftly check your trade ideas by creating your own trading signals class.

To get started, you’ll want to derive your trading signals class from CExpertSignal. Next, override the LongCondition() and ShortCondition() methods with your own logic.

For those interested in trading strategies, a great resource is the book "Strategies of Best Traders". It dives deep into various strategies, but today, we're focusing on reversal candlestick patterns confirmed by oscillators like Stochastic, CCI, MFI, and RSI.

Creating a separate class derived from CExpertSignal is the best route for checking candlestick patterns. For confirmation of the signals generated by these patterns, you can create another class based on CCandlePattern and integrate the necessary features, like oscillator confirmations.

1. Understanding Bullish and Bearish Harami Patterns

1.1. Bullish Harami

The Bullish Harami forms in a downward trend, characterized by a large bearish candlestick followed by a smaller bullish one whose body is nestled within the larger one. This pattern indicates that the downtrend might be reversing and signals a good opportunity to enter a long position. The second candlestick typically opens with a gap up, and the smaller the bullish candlestick, the stronger the reversal signal.

Bullish Harami reversal pattern

Fig. 1. Bullish Harami Candlestick Pattern

You can identify the Bullish Harami pattern using the CheckPatternBullishHarami() method within the CCandlePattern class:

//+------------------------------------------------------------------+
//| Checks formation of Bullish Harami candlestick pattern           |
//+------------------------------------------------------------------+
bool CCandlePattern::CheckPatternBullishHarami()
  {
//--- Bullish Harami
    if((Close(1)>Open(1))&&
        ((Open(2)-Close(2)>AvgBody(1))&&
        ((Close(1)<Open(2))&&
          (Open(1)>Close(2))&&
        (MidPoint(2)<CloseAvg(2)))
        return (true);
//---
    return(false);
  }

The CheckCandlestickPattern(CANDLE_PATTERN_BULLISH_HARAMI) method will confirm the formation of the Bullish Harami pattern.

1.2. Bearish Harami

On the flip side, the Bearish Harami appears during an uptrend, where a large bullish candlestick is followed by a smaller bearish one, again with its body contained within the larger body. This pattern suggests that the uptrend may be reversing, signaling a good time to enter a short position, typically with the second candlestick opening with a gap down.

Bearish Harami reversal pattern

Fig. 2. Bearish Harami Candlestick Pattern

To identify the Bearish Harami, you can use the CheckPatternBearishHarami() method from the CCandlePattern class:

//+------------------------------------------------------------------+
//| Checks formation of Bearish Harami candlestick pattern           |
//+------------------------------------------------------------------+
bool CCandlePattern::CheckPatternBearishHarami()
  {
//--- Bearish Harami
    if((Close(1)<Open(1))&&
    ((Close(2)-Open(2)>AvgBody(1))&&
    ((Close(1)>Open(2))&&
      (Open(1)<Close(2))&&
      (MidPoint(2)>CloseAvg(2)))
    return (true);
//---
    return(false);
  }

The CheckCandlestickPattern(CANDLE_PATTERN_BEARISH_HARAMI) method will confirm the formation of the Bearish Harami pattern.

2. Trade Signals Confirmed by CCI Indicator

To open long or short positions based on these candlestick patterns, we need confirmation from the CCI indicator. The CCI values should exceed or fall below critical levels: -50 for long positions and 50 for short positions.

Closing positions will depend on CCI values, which can happen in two scenarios:

  • If the CCI line reaches the opposite critical level (80 for long positions and -80 for short positions).
  • If the reversal signal isn't confirmed (when CCI hits -80 for long positions and 80 for short positions).

Bullish Harami pattern confirmed by CCI

Fig. 3. Bullish Harami Pattern, Confirmed by CCI Indicator

  • int CBH_BH_CCI::LongCondition() - checks conditions for opening long positions (returns 80) and closing short positions (returns 40);
  • int CBH_BH_CCI::ShortCondition() - checks conditions for opening short positions (returns 80) and closing long positions (returns 40).

2.1. Opening Long Positions and Closing Short Positions

  1. We must confirm the formation of a Bullish Harami pattern with the CCI indicator: CCI(1) < -50 (the last completed bar's CCI should be less than -50).

  2. Close the short position if the CCI indicator crosses upwards above the critical level -80 or downwards below 80.

//+------------------------------------------------------------------+
//| Checks conditions for market entry and exit                      |
//| 1) Market entry (open long position, result=80)                  |
//| 2) Market exit (close short position, result=40)                 |
//+------------------------------------------------------------------+
int CBH_BH_CCI::LongCondition()
  {
    int result=0;
    //--- idx indicates Expert Advisor work mode
    //--- idx=0 - EA checks trade conditions at each tick
    //--- idx=1 - EA checks trade conditions only at news bars
    int idx   =StartIndex();
    //--- checking conditions for opening long position
    //--- formation of Bullish Harami pattern and CCI < -50
    if(CheckCandlestickPattern(CANDLE_PATTERN_BULLISH_HARAMI) && (CCI(1)<-50))
      result=80;
    //--- checking conditions to close short position
    //--- signal line crossover of overbought/oversold levels (below -80)
    if(((CCI(1)>-80) && (CCI(2)<-80)) || ((CCI(1)<80) && (CCI(2)>80)))
      result=40;
    //--- return result
    return(result);
  }

2.2. Opening Short Positions and Closing Long Positions

  1. The formation of a Bearish Harami pattern must be confirmed by the CCI indicator: CCI(1) > 50 (the last completed bar's CCI should be greater than 50).

  2. Close the long position if the CCI indicator crosses downwards below -80 or upwards above 80.

//+------------------------------------------------------------------+
//| Checks conditions for market entry and exit                      |
//| 1) Market entry (open short position, result=80)                 |
//| 2) Market exit (close long position, result=40)                  |
//+------------------------------------------------------------------+
int CBH_BH_CCI::ShortCondition()
  {
    int result=0;
    //--- idx indicates Expert Advisor work mode
    //--- idx=0 - EA checks trade conditions at each tick
    //--- idx=1 - EA checks trade conditions only at news bars
    int idx   =StartIndex();
    //--- checking conditions for opening short position
    //--- formation of Bearish Harami pattern and CCI > 50
    if(CheckCandlestickPattern(CANDLE_PATTERN_BEARISH_HARAMI) && (CCI(1)>50))
      result=80;
    //--- checking conditions to close long position
    //--- signal line crossover of overbought/oversold levels (above 80)
    if(((CCI(1)<80) && (CCI(2)>80)) || ((CCI(1)>-80) && (CCI(2)<-80)))
      result=40;
    //--- return result
    return(result);
  }

2.3. Creating Your Expert Advisor with MQL5 Wizard

The CBH_BH_CCI class is not included in the Standard Library, so you’ll need to download the acbh_bh_cci.mqh file and save it to the client terminal's data folder under MQL5/Include/Expert/Signal/MySignals. Do the same for the candlepatterns.mqh file. After restarting MetaEditor, you can use them in the MQL5 Wizard.

To create your EA, launch the MQL5 Wizard:

Creating Expert Advisor using MQL5 Wizard

Fig. 4. Creating Expert Advisor using MQL5 Wizard

Next, give your Expert Advisor a name:

General properties of the Expert Advisor

Fig. 5. General Properties of the Expert Advisor

Then select the trading signal modules you’ll use:

Signal properties of the Expert Advisor

Fig. 6. Signal Properties of the Expert Advisor

In this case, we’ll use just one module of trading signals:

Add the Signals based on Bullish Harami/Bearish Harami confirmed by CCI module:

Signal properties of the Expert Advisor

Fig. 7. Signal Properties of the Expert Advisor

Module of trade signals added:

Signal properties of the Expert Advisor

Fig. 8. Signal Properties of the Expert Advisor

You can select trailing properties as needed, but we’ll go with “Trailing Stop not used”:

Trailing properties of the Expert Advisor

Fig. 9. Trailing Properties of the Expert Advisor

For money management, we’ll use “Trading with fixed trade volume”:

Money management properties of the Expert Advisor

Fig. 10. Money Management Properties of the Expert Advisor

With everything set, hit the “Finish” button, and you’ll have your EA code generated and saved in Expert_ABH_BH_CCI.mq5 within your terminal data folder under MQL5/Experts/.

The default input parameters for the generated EA include:

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

You’ll need to adjust these parameters as follows:

//--- 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 and Signal_ThresholdClose parameters let you set the threshold levels for opening and closing positions.

In the code for the LongCondition() and ShortCondition() methods, we initially set fixed threshold values:

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

The EA generated by MQL5 Wizard opens and closes positions based on “votes” from the trading signal modules. The main module’s vote is also included in the averaging, but its LongCondition() and ShortCondition() methods always return 0. Therefore, we need to consider this when setting threshold values. In our case, we should set ThresholdOpen to 40=(0+80)/2 and ThresholdClose to 20=(0+40)/2.

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

2.4. Backtesting Results

Let’s take a look at the backtesting results of our Expert Advisor using historical data (EURUSD H1, testing period: 2010.01.01-2011.03.16, PeriodCCI=11, MA_period=5).

During the EA creation, we used a fixed volume method (Trading Fixed Lot, 0.1), and no trailing stop algorithm was implemented (Trailing not used).

Testing results of the Expert Advisor

Fig. 11. Testing Results of the Expert Advisor Based on Bullish Harami/Bearish Harami + CCI

Curious about finding the best set of input parameters? Use the Strategy Tester in your MetaTrader 5 client terminal.

Attached is the code for the Expert Advisor, created using MQL5 Wizard, named expert_abh_bh_cci.mq5.

Related Posts

Comments (0)