MQL5 Wizard - Trading Signals Based on Reversal Candlestick Pattern - MetaTrader 5 Expert - MT4/MT5 Resources




MQL5 Wizard provides automatic creation of Expert Advisors (see MQL5 Wizard: Creating Expert Advisors without Programming ).
CSignalCandles class provides trading signals based on reversal candlestick patterns. This strategy is called "Signals based on reversal candlestick patterns" (when automatically creating an EA in the MQL5 Wizard).
The idea of this system is to identify reversal patterns by counting compound candles. Reversal patterns are similar to the "hammer" and "hanging man" patterns in Japanese candlestick analysis. But it uses compound candles instead of single candles, and does not require the small real body of the compound candle to confirm a reversal.
Input parameters:
The reversal candlestick pattern is determined as follows.
It calculates the compound candle parameters from the most recently completed bar (index 1) to the number of bars defined by the range input parameter. If the compound candle size is larger than the value specified by the minimum input parameter, it will check the reversal condition of the compound candle by analyzing its shadow.
The strength of the short side is characterized by the size of the upper shadow line of the composite candle, and the strength of the bull side is characterized by the size of the lower shadow line.
In addition to the reversal strategy, it is also possible to use the segmentation strategy by specifying a negative value for the Limit input parameter (see MQL5 Wizard - Trading signals based on two EMA crossovers and intraday time filter ).
Depending on the restrictions, three different market entry methods are used:
This strategy is implemented in the CSignalCandles class.

Figure 1. Trading signals based on reversal candlestick patterns
Trading signals are implemented in the CSignalCandles class and have a Candle() function for analysis:
Integer candle ( integer industry); // Returns a positive/negative number depending on the type of compound candle // The return value is the number of bars (candles) in the compound candle (starting from ind)
Multiple candles (bars) can be used to construct composite candles. The minimum number of bars is defined by the Range parameter. In some cases, compound candles can be formed from candles smaller than the range (when size/shading conditions are met). If a compound candle cannot be formed, the Candle function will return 0.
1. Open a long position
To open a long position, a bullish compound candle is required. The function checks it and returns if the composite candle has not formed yet or if the composite candle is bearish. On the other hand, it calculates the composite candle size (required for calculating price, stop loss and take profit levels) and calculates the price level for pending orders.
Please note that the pending order type (Buy Limit or Buy Stop) depends on the sign of the Limit input parameter (if Limit=0 or |price-ask|
//+------------------------------------------------------------------+ //| Check the conditions for opening a long position (Buy) | //+------------------------------------------------------------------+ Boolean CSignalCandles::CheckOpenLong( double &price, double &SL, double &tp,DateTime&ExpiryDate) { //--- Check the fact that a bullish compound candle was formed if (candles( 1 )<= 0 ) returns ( false ); //--- Get the size of the compound candle Double size = m_high_composite-m_low_composite; //--- Calculate the pending order price Price = m_symbol.NormalizePrice(m_symbol.Ask()-m_limit*size); //--- Calculate stop loss price sl =m_symbol.NormalizePrice(price-m_stop_loss*size); //--- Calculate take profit price tp =m_symbol.NormalizePrice(price+m_take_profit*size); //--- Set order expiration time Expiration +=m_expiration*PeriodSeconds(m_period); //--- If the condition is met, return true and return ( true ); }
2. Long position closing
If a bearish compound candle has formed, the long position is closed.
//+------------------------------------------------------------------+ //|Check closing conditions| //+------------------------------------------------------------------+ boolean CSignalCandles::CheckCloseLong( double &price) { //--- Check the fact that a bearish composite candle is formed if (candles( 1 )>= 0 ) returns ( false ); //--- price = 0.0 ; //--- condition is met, returns true returns ( true ); }
3. Open a short position
A bearish compound candle must form to open a short position. Return if the composite candle did not form, or is not bearish. Additionally, we determine its size and calculate the price level of the pending order
(The order type depends on the sign of the Limit input parameter, see "Opening a long position").
//+------------------------------------------------------------------+ //|Check the conditions for opening a short position (sell) | //+------------------------------------------------------------------+ boolean CSignalCandles::CheckOpenShort( double &price, double &SL, double &tp,datetime&expiryDate) { //--- Check the fact that a bearish compound candle was formed if (candles( 1 )>= 0 ) returns ( false ); //--- Get the size of the compound candle Double size = m_high_composite-m_low_composite; //--- Calculate the pending order price Price = m_symbol.NormalizePrice(m_symbol.Bid()+m_limit*size); //--- Calculate stop loss price sl =m_symbol.NormalizePrice(price+m_stop_loss*size); //--- Calculate take profit price tp =m_symbol.NormalizePrice(price-m_take_profit*size); //--- Set order expiration time Expiration+=m_expiration*PeriodSeconds(m_period); //--- If the condition is met, return true and return ( true ); }
4. Close position
If a bullish composite candle has formed, the short position is closed.
//+------------------------------------------------------------------+ //|Check short position closing conditions| //+------------------------------------------------------------------+ boolean CSignalCandles::CheckCloseShort( double &price) { //--- Check the fact that a bullish compound candle is formed If (candle( 1 )<= 0 ) return ( false ); //--- price = 0.0 ; //--- if the condition is met, return true return ( true ); }
Create an Expert Advisor using the MQL5 Wizard
To create a trading robot based on the strategy, you need to select the signal property as " Signal based on reversal candlestick pattern " in the "Create ready-made Expert Advisor" option in the MQL5 Wizard :

Figure 2. Select "Signals based on reversal candlestick patterns" in the MQL5 Wizard
Next you must specify the required trailing stop algorithm and money and risk management system. The code of the Expert Advisor will be automatically created and you can compile and test it with the Strategy Tester MetaTrader 5 client terminal.
Test results
Let's consider the EA backtesting of historical data (EURUSD M15, test period: January 1, 2010 - January 5, 2011, Range=3, Minimum=50, ShadowBig=0.5, ShadowSmall=0.2, Limit=0, Stop Loss=2.0, Take Profit=1.0, Expiry=4).
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 ).

Attachment: SignalCandles.mqh with CSignalCandles class must be placed in terminal_data_folder\MQL5\Include\Expert\Signal folder.
Expert_candles.mq5 Contains the code for Expert Advisors created using the MQL5 Wizard.
Attachment download
📎 signalcandles.mqh (19.85 KB)
📎 expert_candles.mq5 (6.45 KB)
Source: MQL5 #268
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •