Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2011.02.16 16:08
Libraries

MQL5 Wizard - Candlestick Patterns Class - library for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

Views:
9387
Rating:
votes: 41

The MQL5 Wizard allows creating ready-made Expert Advisors based on the Standard library classes delivered together with the client terminal. It allows to check your trade ideas quickly, all you need is to create your own trading signals class. The structure of this class and example can be found in the article MQL5 Wizard: How to Create a Module of Trading Signals.

The generic idea is the following: the class of trading signals is derived from CExpertSignal, the next, it's necessary to override the LongCondition() and ShortCondition() virtual methods with your own methods.

There is a book "Strategies of best traders" (in Russian), there are many trading strategies are considered there, we will focus on reversal candlestick patterns, confirmed by Stochastic, CCI, MFI and RSI oscillators.

The best way is to create the separate class, derived from CExpertSignal for checking of formation of candlestick patterns. For confirmation of trade signals, generated by candlestick patterns, it's sufficient to write the class, derived from CCandlePattern and add the necessary features (for example, confirmation by oscillators) there.

Here we will consider the CCandlePattern class, it allows to simplify the creation of trade signal classes with candlestick patterns for MQL5 Wizard.


CCandlePattern class

The CCandlePattern class is derived from the CExpertSignal class (base class of trading signals)

class CCandlePattern : public CExpertSignal
  {
protected:
   //--- indicators
   CiMA              m_MA;
   //--- time series
   CiOpen            m_open;
   CiHigh            m_high;
   CiLow             m_low;
   CiClose           m_close;
   //--- input parameters
   int               m_ma_period;

public:
   //--- constructor
                     CCandlePattern();
   //--- method of input parameter setting
   void              MAPeriod(int period)             { m_ma_period=period;                 } 
   //--- initialization methods
   virtual bool      ValidationSettings();
   virtual bool      InitIndicators(CIndicators *indicators);

   //--- check formation of a certain candlestick pattern
   bool              CheckCandlestickPattern(ENUM_CANDLE_PATTERNS CandlePattern);
   //--- check formation of bullish/bearish candlestick pattern
   bool              CheckPatternAllBullish();
   bool              CheckPatternAllBearish();

protected:
   //--- initialization of indicators and time series
   bool              InitMA(CIndicators *indicators);
   bool              InitOpen(CIndicators *indicators);
   bool              InitHigh(CIndicators *indicators);
   bool              InitLow(CIndicators *indicators);
   bool              InitClose(CIndicators *indicators);
   //--- methods, used for candlestick checking
   double            AvgBodySize(int ind);
   double            MA(int ind)                const { return(m_MA.Main(ind));             }
   double            Open(int ind)              const { return(m_open.GetData(ind));        }
   double            High(int ind)              const { return(m_high.GetData(ind));        }
   double            Low(int ind)               const { return(m_low.GetData(ind));         }
   double            Close(int ind)             const { return(m_close.GetData(ind));       }
   double            CloseAvg(int ind)          const { return(MA(ind));                    }
   double            MidPoint(int ind)          const { return(0.5*(High(ind)+Low(ind)));   }
   double            MidOpenClose(int ind)      const { return(0.5*(Open(ind)+Close(ind))); }
   //--- methods for checking of a certain candlestick model
   bool              CheckPatternThreeBlackCrows();
   bool              CheckPatternThreeWhiteSoldiers();
   bool              CheckPatternDarkCloudCover();
   bool              CheckPatternPiercingLine();
   bool              CheckPatternMorningDoji();
   bool              CheckPatternEveningDoji();
   bool              CheckPatternBearishEngulfing();
   bool              CheckPatternBullishEngulfing();
   bool              CheckPatternEveningStar();
   bool              CheckPatternMorningStar();
   bool              CheckPatternHammer();
   bool              CheckPatternHangingMan();
   bool              CheckPatternBearishHarami();
   bool              CheckPatternBullishHarami();
   bool              CheckPatternBearishMeetingLines();
   bool              CheckPatternBullishMeetingLines();
  };


Using CCandlePattern in trade signal classes for MQL5 Wizard

The CCandlePattern class can be used a parent class for trade signal classes for MQL5 Wizard. It's necessary to derive the trade signal class from CCandlePattern class, also it's necessary to add methods for checking of long/short position opening/closing (in addition to indicators, etc...).

Formation of reversal candlestick pattern can be used as a trade signal, but it's better to check confirmation (for example, using the oscillators).

  • bool CheckOpenLong(double &price,double &sl,double &tp,datetime &expiration) - Checks conditions of long position opening;
  • bool CheckCloseLong(double &price)  - Checks conditions of long position closing;
  • bool CheckOpenShort(double &price,double &sl,double &tp,datetime &expiration) - Checks conditions of short position opening;
  • bool CheckCloseShort(double &price) - Checks conditions of short position closing;
//+------------------------------------------------------------------+
//|                                          CSampleCandleSignal.mqh |
//+------------------------------------------------------------------+
// include CCandlePattern class
// the candlepatterns.mqh must be located in the same folder
#include "CandlePatterns.mqh"
// wizard description start
//+------------------------------------------------------------------+
//| Description of the class                                         |
//| Title=Test signal                                                |
//| Type=Signal                                                      |
//| Name=CSampleCandleSignal                                         |
//| Class=CSampleCandleSignal                                        |
//| Page=                                                            |
//| Parameter=param1,int,9                                           |
....
//| Parameter=paramN,int,13                                          |
//| Parameter=MAPeriod,int,12                                        |
//+------------------------------------------------------------------+
// wizard description end
//+------------------------------------------------------------------+
class CSampleCandleSignal : public CCandlePattern
  {
protected:
   //--- indicators
   ....
   //--- input parameters
   ...
public:
   //--- constructor
                     CTestClass();
   //--- methods for setting of input parameters
   ...
   //--- initialization of indicators and time series/checking of input parameters
   virtual bool      ValidationSettings();
   virtual bool      InitIndicators(CIndicators *indicators);
   //--- checking of trade signals
   virtual bool      CheckOpenLong(double &price,double &sl,double &tp,datetime &expiration);
   virtual bool      CheckCloseLong(double &price);
   virtual bool      CheckOpenShort(double &price,double &sl,double &tp,datetime &expiration);
   virtual bool      CheckCloseShort(double &price);

protected:
   //--- methods of indicators initialization
   ...
   //--- methods for access to indicator's values
   ...
  };

The details about the structure of trading signal classes, used in MQL5 Wizard, can be found in the article MQL5 Wizard: How to Create a Module of Trading Signals.

Take a look at the line:

//| Parameter=MAPeriod,int,12                                        |

in the wizard description section.

The MAPeriod method is used in the CCandlePattern parent class for calculation of average closing price and averaged values of the candle's body. By default, m_ma_period=12 is set in CCandlePattern() class constructor. However, it's better to set it using the input parameter, it will allow you to use it in Strategy Tester of MetaTrader 5.


Don't forget to call ValidationSettings() and InitIndicators() of parent class

Note, that it's necessary to call the CCandlePattern::ValidationSettings and CCandlePattern::InitIndicators methods of parent class in the corresponding class methods.

It's better to call these methods first:

bool CSampleCandleSignal ::ValidationSettings()
  {
//--- call of ValidationSettings of parent CCandlePattern class
   if(!CCandlePattern::ValidationSettings()) return(false);
//--- your code
..
//--- ok
   return(true);
  }

The same is for the InitIndicators() method:

bool CSampleCandleSignal ::InitIndicators(CIndicators *indicators)
  {
//--- call of InitIndicators of parent CCandlePattern class
   if(!CCandlePattern::InitIndicators(indicators)) return(false);   
//--- your code
...
//--- ok
   return(true);
  }


Checking candlestick patterns

To check the formation of a certain candlestick pattern it's necessary to call the CheckCandlestickPattern(ENUM_CANDLE_PATTERNS CandlePattern) method with pattern, passed to the function.

Also you can check formation of one of the bullish/bearish candlestick patterns using the CheckPatternAllBullish() and CheckPatternAllBearish() methods.

To simplify the work of candlestick patterns, the ENUM_CANDLE_PATTERNS enumeration is used:

enum ENUM_CANDLE_PATTERNS  // candlestick patterns list
  {
   CANDLE_PATTERN_THREE_BLACK_CROWS     = 1,
   CANDLE_PATTERN_THREE_WHITE_SOLDIERS  = 2,
   CANDLE_PATTERN_DARK_CLOUD_COVER      = 3,
   CANDLE_PATTERN_PIERCING_LINE         = 4,
   CANDLE_PATTERN_MORNING_DOJI          = 5,
   CANDLE_PATTERN_EVENING_DOJI          = 6,
   CANDLE_PATTERN_BEARISH_ENGULFING     = 7,
   CANDLE_PATTERN_BULLISH_ENGULFING     = 8,
   CANDLE_PATTERN_EVENING_STAR          = 9,
   CANDLE_PATTERN_MORNING_STAR          = 10,
   CANDLE_PATTERN_HAMMER                = 11,
   CANDLE_PATTERN_HANGING_MAN           = 12,
   CANDLE_PATTERN_BEARISH_HARAMI        = 13,
   CANDLE_PATTERN_BULLISH_HARAMI        = 14,
   CANDLE_PATTERN_BEARISH_MEETING_LINES = 15,
   CANDLE_PATTERN_BULLISH_MEETING_LINES = 16
  };

Checking bullish candlestick patterns:

///--- Check formation of "3 White Soldiers" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_THREE_WHITE_SOLDIERS)

///--- Check formation of "Piercing Line" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_PIERCING_LINE)

///--- Check formation of "Morning Doji" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_MORNING_DOJI)

///--- Check formation of "Bullish Engulfing" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_BULLISH_ENGULFING)

///--- Check formation of "Bullish Haramii" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_BULLISH_HARAMI)

///--- Check formation of "Morning Star" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_MORNING_STAR)

///--- Check formation of "Bullish Meeting Lines" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_BULLISH_MEETING_LINES)

///--- Check formation of "Hammer" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_HAMMER)

///--- Check formation of one of the bullish candlestick patterns
  CheckPatternAllBullish();

Checking bearish candlestick patterns:

///--- Check formation of "3 Black Crows" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_THREE_BLACK_CROWS)

///--- Check formation of "Dark Cloud Cover" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_DARK_CLOUD_COVER)

///--- Check formation of "Evening Doji" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_EVENING_DOJI)

///--- Check formation of "Bearish Engulfing" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_BEARISH_ENGULFING)

///--- Check formation of "Evening Star" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_EVENING_STAR)

///--- Check formation of "Hanging Man" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_HANGING_MAN)

///--- Check formation of "Bearish Harami" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_BEARISH_HARAMI)

///--- Check formation of "Bearish Meeting Lines" pattern:
  CheckCandlestickPattern(CANDLE_PATTERN_BEARISH_MEETING_LINES)

///--- Check formation of one of the bearish patterns
  CheckPatternAllBearish();

Here are examples of use of the methods:

1. Open long position

//+------------------------------------------------------------------+
//| Checking condition of long position opening                      |
//+------------------------------------------------------------------+
bool CSampleCandleSignal::CheckOpenLong(double &price,double &sl,double &tp,datetime &expiration)
  {
//--- check conditions to open long position
//--- it's better to use this code in addition to indicator's checking
//--- for example, let's check formation of "3 white soldiers" pattern:
   if CheckCandlestickPattern(CANDLE_PATTERN_THREE_WHITE_SOLDIERS) return(true):
//--- no signal
   return(false);
  }

2. Close long position

//-------------------------------------------------------------------+
//| Checking condition of long position closing                      |
//+------------------------------------------------------------------+
bool CSampleCandleSignal::CheckCloseLong(double &price)
  {
//--- check conditions to close long position
//--- for example, let's check formation of one of the bearish pattern:
   if CheckPatternAllBearish() return(true):
//--- no signal
   return(false);
  }

3. Open short position

//-------------------------------------------------------------------+
//| Checking condition of short position opening                     |
//+------------------------------------------------------------------+
bool CSampleCandleSignal::CheckOpenShort(double &price,double &sl,double &tp,datetime &expiration)
  {
//--- check conditions to open short position
//--- it's better to use this code in addition to indicator's checking
//--- for example, let's check formation of "3 black crows" pattern:
    if CheckCandlestickPattern(CANDLE_PATTERN_THREE_BLACK_CROWS) return(true):
//--- no signal
   return(false);
  }

4. Close short position

//-------------------------------------------------------------------+
//| Checking condition of short position closing                     |
//+------------------------------------------------------------------+
bool CSampleCandleSignal::CheckCloseShort(double &price)
  {
//--- check conditions to close short position
//--- it's better to use this code in addition to indicator's checking
//--- for example, let's check formation of one of the bullish pattern:
   if CheckPatternAllBullish() return(true):
//--- no signal
   return(false);
  }

Recommendations

To reduce false signals, the reversal candlestick patterns must be confirmed by other indicators, such as oscillators.

The following patterns are considered in the "Strategies of best traders" book:

  • 3 Black Crows/3 White Soldiers
  • Dark Cloud Cover/Piercing Line
  • Morning Doji/Evening Doji
  • Bearish Engulfing/Bullish Engulfing
  • Evening Star/Morning Star
  • Hammer/Hanging Man
  • Bearish Harami/Bullish Harami
  • Bearish Meeting Lines/Bullish Meeting Lines

confirmed by Stochastic, CCI, MFI and RSI oscillators.

Later we will provide the classes of trade signals for use in MQL5 Wizard.

Translated from Russian by MetaQuotes Software Corp.
Original code: https://www.mql5.com/ru/code/291

MQL5 Wizard - Trade Signals Based on 3 Black Crows/3 White Soldiers + Stochastic MQL5 Wizard - Trade Signals Based on 3 Black Crows/3 White Soldiers + Stochastic

Trade signals based on "3 Black Crows/3 White Soldiers" candlestick pattern, confirmed by Stochastic indicator is considered. The code of the Expert Advisor based on this strategy can be generated automatically using the MQL5 Wizard.

Demo_resource_EA Demo_resource_EA

The example of use of the resources. It creates a button, using the object of OBJ_BITMAP_LABEL type.

cIntSpeech cIntSpeech

It speaks the specified text using the speech engine.

MQL5 Wizard - Trade Signals Based on 3 Black Crows/3 White Soldiers + CCI MQL5 Wizard - Trade Signals Based on 3 Black Crows/3 White Soldiers + CCI

Trade signals based on "3 Black Crows/3 White Soldiers" candlestick pattern, confirmed by Commodity Channel Index (CCI) indicator is considered. The code of the Expert Advisor based on this strategy can be generated automatically using the MQL5 Wizard.