Home System Trading Post

Mastering Bullish and Bearish Harami Patterns with Stochastic in MT5

Attachments
310.zip (6.59 KB, Download 1 times)

If you're looking to enhance your trading game, the MQL5 Wizard is your go-to tool for creating ready-made trading systems in MetaTrader 5. With it, you can quickly validate your trading ideas by crafting your own signal classes. For more details on how to get started, check out the guide on Standard library classes.

The core concept is straightforward: derive your trading signals class from CExpertSignal. From there, you just need to override the LongCondition() and ShortCondition() methods to suit your strategy.

While there's a useful resource titled Strategies of Best Traders, which delves into various trading strategies, we’ll focus on reversal candlestick patterns validated by indicators like Stochastic, CCI, MFI, and RSI.

To keep things organized, it’s best to create a dedicated class derived from CExpertSignal to check for candlestick pattern formations. You can also create another class derived from CCandlePattern to add confirmation features using oscillators.

In this article, we'll explore signals based on the "Bullish Harami" and "Bearish Harami" reversal patterns, confirmed by the Stochastic indicator. The module for trade signals will utilize the CCandlePattern class, providing a simple example for generating trade signals using candlestick patterns.

1. Understanding Bullish and Bearish Harami Patterns

1.1. Bullish Harami

The Bullish Harami pattern emerges during a downtrend, characterized by a large bearish candlestick followed by a smaller bullish candlestick whose body is contained within the larger one. This pattern suggests a potential trend reversal, signaling a good time to enter a long position. Ideally, the second candlestick opens with a gap up.

The smaller the second (white) candlestick, the more reliable the reversal signal becomes.

Bullish Harami Pattern

Figure 1. Bullish Harami Candlestick Pattern

The detection of the Bullish Harami pattern is done through the CheckPatternBullishHarami() method in the CCandlePattern class:

//+------------------------------------------------------------------+
//| Checks formation of "Bullish Harami" candlestick pattern |
//+------------------------------------------------------------------+
bool CCandlePattern::CheckPatternBullishHarami() {
	//--- Bullish Harami
	if ((Close(1) > Open(1) &&  // last completed bar is bullish
		((Open(2) - Close(2) > AvgBody(1)) &&  // previous candle is bearish
		((Close(1) < Open(2) &&  // close price of the bullish candle
		(Open(1) > Close(2)) &&  // open price of the bullish candle
		(MidPoint(2) < CloseAvg(2)))) {
		return (true);
	}
	return (false);
}

The CheckCandlestickPattern(CANDLE_PATTERN_BULLISH_HARAMI) method in the CCandlePattern class checks for the formation of the Bullish Harami pattern.

1.2. Bearish Harami

The Bearish Harami pattern forms during an uptrend, where a large bullish candlestick is followed by a smaller bearish candlestick, with its body contained within the larger one. This indicates a potential reversal of the uptrend, suggesting it might be time to enter a short position. The second candlestick typically opens with a gap down.

As with the Bullish Harami, the smaller the second (black) candlestick, the more likely the reversal.

Bearish Harami Pattern

Figure 2. Bearish Harami Candlestick Pattern

The Bearish Harami pattern is recognized through the CheckPatternBearishHarami() method in the CCandlePattern class:

//+------------------------------------------------------------------+
//| Checks formation of "Bearish Harami" candlestick pattern |
//+------------------------------------------------------------------+
bool CCandlePattern::CheckPatternBearishHarami() {
	//--- Bearish Harami
	if ((Close(1) < Open(1) &&  // last completed bar is bearish
		((Close(2) - Open(2) > AvgBody(1)) &&  // previous candle is bullish
		((Close(1) > Open(2) &&  // close price of the bearish candle
		(Open(1) < Close(2)) &&  // open price of the bearish candle
		(MidPoint(2) > CloseAvg(2)))) {
		return (true);
	}
	return (false);
}

The CheckCandlestickPattern(CANDLE_PATTERN_BEARISH_HARAMI) method in the CCandlePattern class checks for the formation of the Bearish Harami pattern.

2. Trade Signals Confirmed by Stochastic Indicator

When it comes to entering long or short positions, it's crucial that the signals generated by the candlestick patterns are validated by the Stochastic oscillator. For a long position, the %D line must be below 30, while for a short position, it should be above 70.

Closing your position hinges on the values of the %D indicator and can occur in two scenarios:

  • If the %D line hits the opposite critical levels (80 for long, 20 for short).
  • If a reverse signal isn't confirmed (when the %D line reaches levels of 20 for long and 80 for short).

Bearish Harami Pattern Confirmed by Stochastic Indicator

Figure 3. Bearish Harami Pattern Confirmed by Stochastic Indicator

To check trade conditions for entry and exit, you’ll implement two methods:

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

2.1. Opening Long Position/Closing Short Position

  1. Confirm the formation of the "Bullish Harami" pattern through the Stochastic indicator: StochSignal(1) < 30 (the signal line for the last completed bar must be below 30).

  2. Close the short position if the Stochastic signal line crosses upward past the 20 or 80 levels.

//+------------------------------------------------------------------+
//| 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 CBH_BH_Stoch::LongCondition() {
	int result=0;
	//--- idx can be used to determine Expert Advisor work mode
	//--- idx=0 - checks trade conditions at each tick
	//--- idx=1 - checks trade conditions only at news bars
	int idx = StartIndex();
	//--- checking conditions to open long position
	//--- formation of Bullish Harami and signal line<30
	if (CheckCandlestickPattern(CANDLE_PATTERN_BULLISH_HARAMI) && (StochSignal(1) < 30)) {
		result = 80;
	}
	//--- checking conditions to close short position
	//--- signal line crossover of overbought/oversold levels (downward 20, upward 80)
	if ((((StochSignal(1) > 20) && (StochSignal(2) < 201) > 80) && (StochSignal(2) < 8040;
	}
	//--- return result
	return (result);
}

2.2. Opening Short Position/Closing Long Position

  1. Confirm the formation of the "Bearish Harami" pattern with the Stochastic indicator: StochSignal(1) > 70 (the signal line of the last completed bar must be above 70).

  2. Close the long position if the Stochastic signal line crosses downward past the 80 or 20 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 CBH_BH_Stoch::ShortCondition() {
	int result=0;
	//--- idx can be used to determine Expert Advisor work mode
	//--- idx=0 - checks trade conditions at each tick
	//--- idx=1 - checks trade conditions only at news bars
	int idx = StartIndex();
	//--- checking conditions to open short position
	//--- formation of Bearish Harami and signal line>70
	if (CheckCandlestickPattern(CANDLE_PATTERN_BEARISH_HARAMI) && (StochSignal(1) > 70)) {
		result = 80;
	}
	//--- checking conditions to close long position
	//--- signal line crossover of overbought/oversold levels (downward 80, upward 20)
	if ((((StochSignal(1) < 80) && (StochSignal(2) > 801) < 20) && (StochSignal(2) > 2040;
	}
	//--- return result
	return (result);
}

2.3. Creating Your Expert Advisor with MQL5 Wizard

The CBH_BH_Stoch class isn’t included in the Standard Library, so you’ll need to download the acbh_bh_stoch.mqh file (check attachments) and save it in the client_terminal_data\MQL5\Include\Expert\Signal\MySignals directory. Do the same for the candlepatterns.mqh file. After restarting the MetaEditor, you can use it in the MQL5 Wizard.

To create your EA, simply launch the MQL5 Wizard:

Creating Expert Advisor using MQL5 Wizard

Figure 4. Creating Expert Advisor using MQL5 Wizard

First, specify the name of your Expert Advisor:

General properties of the Expert Advisor

Figure 5. General Properties of the Expert Advisor

Next, select the trading signal modules you want to use.

Signal properties of the Expert Advisor

Figure 6. Signal Properties of the Expert Advisor

In our case, we will only use one module of trading signals.

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

Signal properties of the Expert Advisor

Figure 7. Signal Properties of the Expert Advisor

The trading signal module has been added:

Signal properties of the Expert Advisor

Figure 8. Signal Properties of the Expert Advisor

You can choose any trailing properties, but we will opt for "Trailing Stop not used":

Trailing properties of the Expert Advisor

Figure 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

Figure 10. Money Management Properties of the Expert Advisor

After pressing the "Finish" button, your generated Expert Advisor’s code will be saved as Expert_ABH_BH_Stoch.mq5 in the terminal_data_folder\MQL5\Experts\.

The default input parameters for 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)

These should be updated 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 and Signal_ThresholdClose parameters let you specify threshold levels for opening and closing positions.

In the LongCondition() and ShortCondition() methods of the trading signals class, we set fixed threshold values:

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

The Expert Advisor generated by MQL5 Wizard opens and closes positions using the "votes" from the trading signal modules. The main module's vote is also utilized in averaging the results, but its LongCondition() and ShortCondition() methods always return 0.

Given that we have one module of trade signals plus the main module, we need to adjust the threshold values accordingly. Hence, ThresholdOpen should be set to 40=(0+80)/2 and ThresholdClose to 20=(0+40)/2.

Both Signal_StopLevel and Signal_TakeLevel should be set to 0, meaning positions will only close when the exit conditions are met.

2.4. Backtesting Results

Now, let’s dive into backtesting the Expert Advisor using historical data (EURUSD H1, testing period: 01/01/2010 to 04/03/2011, PeriodK=47, PeriodD=9, PeriodSlow=13, MA_period=5).

We utilized fixed volume trading (Trading Fixed Lot, 0.1) and did not implement a Trailing Stop algorithm (Trailing not used).

Testing Results of the Expert Advisor

Figure 11. Testing Results of the Expert Advisor Based on "Bullish/Bearish Harami + Stochastic"


The optimum set of input parameters can be discovered using the Strategy Tester in the MetaTrader 5 client terminal.

The code for the Expert Advisor you created with MQL5 Wizard is attached as expert_abh_bh_stoch.mq5.

Related Posts

Comments (0)