MQL5 Wizard - Trading signals based on Bullish Engulfing / Bearish Engulfing + MFI - MetaTrader 5 Expert











There is the book "Strategies of the Best Traders " (in Russian), where many trading strategies are considered, we will focus on reversal candlestick patterns, represented by Stochastic , CCI , Micro Institutions and RSI oscillators.
The best way is to create a separate class derived from Expert Signal for checking the formation of candlestick patterns. For the confirmation of trading signals generated by candlestick patterns, it is enough to write a class derived from CCandlePattern and add in it the necessary functionality (for example, confirmation by oscillator).
Here, we will consider signals based on the Bullish Engulfing/Bearish Engulfing reversal candlestick pattern and confirm it with the Market Facilitation Index (MFI) indicator. The trading signals module is based on the CC candle pattern class, which is a simple example of creating trading signals using candlestick patterns.
1. “Bullish Engulfing” and “Bearish Engulfing” Reversal Candlestick Patterns
1.1. Bullish Engulfing
A "bullish engulfing" reversal pattern forms in a downtrend when a small black candlestick is followed by a large white candlestick, completely covering ("engulfing") the previous day's candlestick. The shadow (tail) of the small candlestick is short, which allows the body of the larger candlestick to cover the entire candlestick of the previous day.

Figure 1. “Bullish Engulfing” Candlestick Pattern
The identification of "Bullish Engulfing" patterns is implemented in the CheckPatternBullishEgulfing() method of the CC candle pattern class:
//+------------------------------------------------------------------+ //|Check the formation of the "Bullish Engulfing" candlestick pattern | //+------------------------------------------------------------------+ Boolean valueCCandlePattern ::CheckPatternBullishEngulfing() { //--- bullish engulfing if ((open( 2 )>close( 2 )) && // the previous candle was bearish (close( 1 )-open( 1 )>average body ( 1 )) && // the body of the bullish candle is above the average of the body (close (1 ) >open( 2 )) && // the closing price of the bullish candle is higher than the open price of the bearish candle ( open - close (2 ) ) ; }
CheckCandlestickPattern(CANDLE_PATTERN_BULLISH_ENGULFING) method CC Candlestick Pattern class is used to check the formation of the "Bullish Engulfing" candlestick pattern.
1.2. Bearish Engulfing
A "Bearish Engulfing" reversal pattern forms in an uptrend when a small white candlestick is followed by a large black candlestick, completely covering ("engulfing") the previous day's candlestick. The shadow (tail) of the small candlestick is short, which allows the body of the larger candlestick to cover the entire candlestick of the previous day.

Figure 2. “Bearish Engulfing” Candlestick Pattern
The recognition of the "Bearish Engulfing" pattern is the CC candle pattern class implemented in the CheckPatternBearishEgulfing() method:
//+------------------------------------------------------------------+ //|Check the formation of the "Bearish Engulfing" candlestick pattern | //+------------------------------------------------------------------+ BooleanCCandlePattern ::CheckPatternBearishEngulfing() { //--- bearish engulfing if ((open( 2 )<close( 2 )) && // the previous candle was bearish (open( 1 )-close( 1 )>average( 1 )) && // candle body is above the average of the candle body (close( 1 )<open( 2 )) && // the closing price of the bearish candle is lower than the open price of the bullish candle (open( 2 )>closed average( 2 )) && // Uptrend (Open( 1 )>Close( 2 ))) // The opening price of the bearish candle is higher than the closing price of the bullish candle return ( true ); //--- return ( false ); }
CheckCandlestickPattern(CANDLE_PATTERN_BEARISH_ENGULFING) method CC Candlestick Pattern class is used to check the formation of "Bearish Engulfing" candlestick pattern.
2. Trading signals confirmed by the MFI indicator
The closing of an open position depends on the value of the MFI indicator. This can be done in two situations:

Figure 3. “Bullish Engulfing” Pattern Confirmed by MFI Indicator
2.1. Open long position/close short position
The formation of the "Bullish Engulfing" pattern must pass through the Micro Financial Institutions indicator: MFi(1)<40 (the MFI indicator value of the last completed bar must be less than 40).
Short positions must be closed if the MFI indicator crosses above key levels (70 or 30).
//+------------------------------------------------------------------+ //|Check the conditions for entering and exiting the market | //| 1) Enter the market (open long position, result = 80) | //| 2) Market Exit (Close Position, Result=40) | //+------------------------------------------------------------------+ Integer CBE_BE_MFI::long condition() { Integer result = 0 ; //--- idx can be used to determine the Expert Advisor working mode //--- idx=0 - in this case the EA checks the trading conditions on every price move //--- idx=1 - in this case the EA only checks the trading status of the news bar integer idx =StartIndex(); //--- checks the conditions for opening a long position //--- Bullish engulfing pattern is formed and MFI < 40 if (check candlestick pattern (CANDLE_PATTERN_BULLISH_ENGULFING) && (MFI( 1 ) < 40 )) Result = 80 ; //--- check closing conditions //--- Signal lines for overbought/oversold levels are crossed (30 up, 70 up) if (((MFI( 1 )> 30 ) && (MFI( 2 )< 30 )) || ((MFI( 1 )> 70 ) && (MFI( 2 )< 70 ))) result = 40 ; //--- return result return (result); }
2.2. Open short position/close long position
The formation of the "Bearish Engulfing" pattern must be achieved by the Micro Financial Institutions indicator: MFI(1)>60 (the MFI indicator value of the last completed bar must be greater than 60).
Long positions must be closed if the MFI indicator crosses above key levels (70 or 30).
//+------------------------------------------------------------------+ //|Check the conditions for entering and exiting the market | //| 1) Enter the market (short position, result = 80) | //| 2) Market Exit (Close Position, Result=40) | //+------------------------------------------------------------------+ Integer CBE_BE_MFI::short condition() { Integer result = 0 ; //--- idx can be used to determine the Expert Advisor working mode //--- idx=0 - in this case the EA checks the trading conditions on every price move //--- idx=1 - in this case the EA only checks the trading status of the news bar integer idx =StartIndex(); //--- checks the conditions for opening a short position //--- Bearish engulfing pattern formed and MFI>60 if (check candlestick pattern (CANDLE_PATTERN_BEARISH_ENGULFING) && (MFI( 1 )> 60 )) Result = 80 ; //--- check closing conditions //--- Signal lines for overbought/oversold levels are crossed (70 up, 30 down) if (((MFI( 1 )> 70 ) && (MFI( 2 )< 70 )) || ((MFI( 1 )< 30 ) && (MFI( 2 )> 30 ))) result = 40 ; //--- return result return (result); }
2.3. Use the MQL5 Wizard to create an Expert Advisor
The CBE_BE_MFI class is not included in the standard library classes, to use it you need to download the abe_be_mfi.mqh file (see attachment) and save it to client_terminal_data\folder\MQL5\Include\Expert\Signal\MySignals. The same operation should be performed for the candlepatterns.mqh file. After restarting MetaEditor, you can use it in the MQL5 Wizard.
Create Expert Advisor Launch MQL5 Wizard :

Figure 4. Creating an Expert Advisor using the MQL5 Wizard
Let's specify the name of the Expert Advisor:

Figure 5. General properties of EA trading
After that we need to choose the trading signal module to use.

Figure 6. Signal properties of EA trading
In our case we use only one trading signals module.

Figure 7. Signal properties of EA trading
Added trading signals module:

Figure 8. Signal properties of EA trading
You can choose any trailing attribute, but we will use "Unused Trailing Stop":

Figure 9. Tracking properties of Expert Advisor
Regarding the money management properties, we will use "Fixed Volume Trading":

Figure 10. Money management attributes of EA trading
Press the "Finish" button and we will get the code of the generated Expert Advisor in Expert_ABE_BE_MFI.mq5, which will be saved in terminal_data_folder\MQL5\Experts\.
Default input parameters of the generated Expert Advisor:
//--- Main signal input Input integer Signal_ThresholdOpen = 10 ; //Open signal threshold[0...100] Input integer Signal_ThresholdClose = 10 ; // Close signal threshold [0...100] Enter double Signal_PriceLevel = 0.0 ; // price level at which to execute the trade Enter double Signal_StopLevel = 50.0 ; // Stop loss level in pips Enter double Signal_TakeLevel = 50.0 ; // Take profit level (in pips)
Must be replaced with:
//--- Main signal input Input integer Signal_ThresholdOpen = 40 ; //Open signal threshold[0...100] Input integer Signal_ThresholdClose = 20 ; // Close signal threshold [0...100] Enter double Signal_PriceLevel = 0.0 ; // price level at which to execute the trade Enter double Signal_StopLevel = 0.0 ; // Stop loss level in points Enter double Signal_TakeLevel = 0.0 ; // Take profit level (in pips)
Signal_ThresholdOpen/Signal_ThresholdClose input parameters allow specifying threshold levels for opening and closing positions.
In the code of the LongCondition() and ShortCondition() methods of the trading signal class, we specify the fixed value of the threshold:
The Expert Advisor generates opening and closing positions by the MQL5 Wizard using "votes" in the Trading Signals module. The vote of the main module (which acts as a container and consists of all added modules) is also used, but its LongCondition() and ShortCondition() methods always return 0.
The voting results from the main module are also used for "voting" averaging. In our case we have: main module + 1 trading signals module, so we need to take this fact into account when setting the thresholds. Therefore, ThresholdOpen and ThresholdClose must be set to 40=(0+80)/2 and 20=(0+40)/2.
The value of the Signal_StopLevel and Signal_TakeLevel input parameters is set to 0, which means that the position will be closed only if the closing conditions are met.
2.4. Historical backtest results
Let us consider Expert Advisor's backtest on historical data (EURUSD H1, test period: 2010.01.01-2011.03.16, PeriodMFI=37, MA_period=5).
When creating the EA, we used a fixed trading volume ( fixed lot size for trading , 0.1) and did not use the trailing stop algorithm ( trailing was not used ).

Figure 11. EA trading test results based on bullish engulfing/bearish engulfing + MFI
The best set of input parameters for the Strategy Tester MetaTrader 5 client can be found using the following command.
The code for the Expert Advisor created by the MQL5 Wizard is attached in Expert_abe_be_mfi.mq5.
Attachment download
📎 acandlepatterns.mqh (20.34 KB)
📎 acbe_be_mfi.mqh (8.02 KB)
📎 expert_abe_be_mfi.mq5 (6.63 KB)
Source: MQL5 #307
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •