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.03.28 12:38
Experts

MQL5 Wizard - Trade Signals Based on Morning/Evening Stars + MFI - expert for MetaTrader 5

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

Views:
2906
Rating:
votes: 18
\MQL5\Include\Expert\Signal\MySignals\

The MQL5 Wizard allows creating ready-made Expert Advisors based on the Standard library classes delivered together with the client terminal (see Creating Ready-Made Expert Advisors in MQL5 Wizard for the details). 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 signals, based on "Morning Star/Evening Star" (Morning Doji Star/Evening Doji Star) reversal candlestick patterns, confirmed by MFI indicator. The module of trade signals is based on the CCandlePattern class, it's simple example of its use for creation of trade signals with candlestick patterns.


1. "Morning Star" and "Evening Star" reversal candlestick patterns

1.1. Morning Star

This pattern indicates the reversal of downtrend, it consists of three candles (Fig. 1). After a long black candle there is a candle (the color isn't important) with a small body, which lies outside the body of the black candle. The small body of a candle means that strengths of the bulls and bears are equal and market is ready to change the trend.

The third candle of the pattern is the bullish candle, it's body isn't overlapped with the body of the second candle, and close price lies inside the body of the first (bearish) candle. The resulting candle of the model is also plotted at Figure 1.

For the case if the second candle is doji-like-candle, the model is named "Morning Doji Star".

Fig. 1. "Morning Star" and "Morning Doji Star" candlestick patterns

Fig. 1. "Morning Star" and "Morning Doji Star" candlestick patterns

The identifying of "Morning Star" pattern is implemented in CheckPatternMorningStar() and CheckPatternMorningDoji() methods of CCandlePattern class:

//+------------------------------------------------------------------+
//| Check formation of the "Morning Star" pattern                    |
//+------------------------------------------------------------------+
bool CCandlePattern::CheckPatternMorningStar()
  {
//--- Morning Star
   if((Open(3)-Close(3)>AvgBody(1))             && // bearish candle, its body greater than average candle body
      (MathAbs(Close(2)-Open(2))<AvgBody(1)*0.5) && // second candle has small body (lower than half of the average body)
      (Close(2)<Close(3))                       && // close of the second candle is lower than close of the first 
      (Open(2)<Open(3))                         && // open of the second canlde is lower than open of the first
      (Close(1)>MidOpenClose(3)))                  // close of the last completed candle is higher than center of the first 
      return(true);
//---
   return(false);
  }
//+------------------------------------------------------------------+
//| Check formation of the "Morning Doji Star" pattern               |
//+------------------------------------------------------------------+
bool CCandlePattern::CheckPatternMorningDoji()
  {
//--- Morning Doji Star
   if((Open(3)-Close(3)>AvgBody(1)) && // bearish candle with body greater than average candle body 
      (AvgBody(2)<AvgBody(1)*0.1)   && // the second candle has a very small body (doji) 
      (Close(2)<Close(3))           && // close of the second candle is lower than close of the first 
      (Open(2)<Open(3))             && // open of the second candle is lower than open of the first
      (Open(1)>Close(2))            && // up gap at the last completed candle
      (Close(1)>Close(2)))             // close of the last completed candle is higher than close of the second
      return(true);
//---
   return(false);
  }

The CheckCandlestickPattern(CANDLE_PATTERN_MORNING_STAR) and CheckCandlestickPattern(CANDLE_PATTERN_MORNING_DOJI) methods of CCandlePattern class are used to check formation of "Morning Star" and "Morning Doji Star" candlestick patterns.


1.2. Evening Star

This pattern indicates the reversal of uptrend, it consists of three candles (Fig. 2). After a long white candles there is a candle (the color isn't important) with a small body, which lies outside the body of the white candle. The small body of a candle means that strengths of the bulls and bears are equal and market is ready to change the trend.

The third candle of the pattern is the bearish candle, it's body isn't overlapped with the body of the second candle, and close price lies inside the body of the first (bullish) candle. The resulting candle of the model is also plotted at Figure 2.

For the case if the second candle is doji-like-candle, the model is named "Evening Doji Star".

Fig. 2. "Evening Star" and "Evening Doji Star" candlestick patterns

Fig. 2. "Evening Star" and "Evening Doji Star" candlestick patterns

Here are the methods for identifying of "Evening Star" and "Evening Doji Star" patterns:

//+------------------------------------------------------------------+
//| Check formation of the "Evening Star" pattern                    |
//+------------------------------------------------------------------+
bool CCandlePattern::CheckPatternEveningStar()
  {
//--- Evening Star
   if((Close(3)-Open(3)>AvgBody(1))             && // bullish candle with body higher than average body 
      (MathAbs(Close(2)-Open(2))<AvgBody(1)*0.5) && // second candle has a small body (less than half of the average)
      (Close(2)>Close(3))                       && // close of the second candle is higher than close of the first
      (Open(2)>Open(3))                         && // open of the second candle is higher than open of the first
      (Close(1)<MidOpenClose(3)))                  // close of the last completed candle is lower than center of the first 
      return(true);
//---
   return(false);
  }
//+------------------------------------------------------------------+
//| Check formation of the "Evening Doji Star" pattern               |
//+------------------------------------------------------------------+
bool CCandlePattern::CheckPatternEveningDoji()
  {
//--- Evening Doji Star
   if((Close(3)-Open(3)>AvgBody(1)) && // bullish candle with body higher than average 
      (AvgBody(2)<AvgBody(1)*0.1)   && // second candle has a very small body (doji) 
      (Close(2)>Close(3))           && // close of the second candle is higher than close of the first
      (Open(2)>Open(3))             && // opend of the second candle is higher than open of the first
      (Open(1)<Close(2))            && // down gap at the last completed candle
      (Close(1)<Close(2)))             // close of the last completed candle is lower than close of the second 
      return(true);
//---
   return(false);
  }

The CheckCandlestickPattern(CANDLE_PATTERN_EVENING_STAR) and CheckCandlestickPattern(CANDLE_PATTERN_EVENING_DOJI) methods of CCandlePattern class are used to check formation of "Evening Star" and "Evening Doji Star" candlestick patterns.


2. Trade signals, confirmed by MFI indicator

The trading signals to open long or short position must be confirmed by MFI indicator. The value of MFI indicator must be lower than 40 (for long positions) or greater than 60 (for short positions).

The closing of opened position depends on the values of MFI indicator. It can be done in 2 cases:

  1. if MFI has reached the opposite critical level (70 for long position and 30 for short position)
  2. if the reverse signal isn't confirmed (when MFI indicator reaches the following levels: 30 for long position and 70 for short position)

Figure 3. The "Morning Star" pattern, confirmed by MFI indicator

Figure 3. The "Morning Star" pattern, confirmed by MFI indicator


  • int CMS_ES_MFI::LongCondition() - checks conditions to open long position (returns 80) and close of the short position (returns 40);
  • int CMS_ES_MFI::ShortCondition() - checks conditions to open short position (returns 80) and close of the long position (returns 40).

2.1. Open long position/Close short position

  1. The formation of "Morning Star" pattern must be confirmed by MFI indicator: MFi(1)<40 (the value of MFI indicator of the last completed bar must be less than 40).

  2. The short position must be closed if MFI indicator has crossed upward the critical levels (70 or 30).

//+------------------------------------------------------------------+
//| 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 CMS_ES_MFI::LongCondition()
  {
   int result=0;
//--- idx can be used to determine Expert Advisor work mode
//--- idx=0 - in this case EA checks trade conditions at each tick
//--- idx=1 - in this case EA checks trade consition only at news bars
   int idx   =StartIndex();
//--- checking of conditions to open long position
//--- formation of Morning Star pattern and MFI<40
  if(CheckCandlestickPattern(CANDLE_PATTERN_MORNING_STAR) && (MFI(1)<40))
     result=80;
//--- checking of conditions to close short position
//--- signal line crossover of overbought/oversold levels (upward 30, upward 70)
  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

  1. The formation of "Evening Star" pattern must be confirmed by MFI indicator: MFI(1)>60 (the value of MFI indicator of the last completed bar must be greater than 60).

  2. The long position must be closed if MFI indicator has crossed upward the critical levels (70 or 30).

//+------------------------------------------------------------------+
//| 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 CMS_ES_MFI::ShortCondition()
  {
   int result=0;
//--- idx can be used to determine Expert Advisor work mode
//--- idx=0 - in this case EA checks trade conditions at each tick
//--- idx=1 - in this case EA checks trade consition only at news bars
   int idx   =StartIndex();
//--- checking of conditions to open short position
//--- formation of Evening Star pattern and MFI>60
  if(CheckCandlestickPattern(CANDLE_PATTERN_EVENING_STAR) && (MFI(1)>60))
     result=80;
//--- checking of conditions to close long position
//--- signal line crossover of overbought/oversold levels (upward 70, downward 30)
   if(((MFI(1)>70) && (MFI(2)<70)) || ((MFI(1)<30) && (MFI(2)>30)))
     result=40;
//--- return result
   return(result);
  }


2.3. Creating Expert Advisor using MQL5 Wizard

The CMS_ES_MFI class isn't included in the Standard Library classes, to use it, it's necessary to download the ams_es_mfi.mqh file (see attachments) and save it to the client_terminal_data_folder\MQL5\Include\Expert\Signal\MySignals. The same should be done with the acandlepatterns.mqh file. You can use it in MQL5 Wizard after restart of the MetaEditor.

To create an Expert Advisor launch MQL5 Wizard:

Fig. 4. Creating Expert Advisor using MQL5 Wizard

Fig. 4. Creating Expert Advisor using MQL5 Wizard

Let's specify the name of the Expert Advisor:

Fig. 5. General properties of the Expert Advisor

Fig. 5. General properties of the Expert Advisor

After that we need to select the modules of trade signals used.

Fig. 6. Signal properties of the Expert Advisor

Fig. 6. Signal properties of the Expert Advisor

In our case we use only one module of trade signals.

Adding the "Signals based on Morning Star/Evening Star confirmed by MFI" module of trading signals:

Fig. 7. Signal properties of the Expert Advisor

Fig. 7. Signal properties of the Expert Advisor

Module of trade signals added:

Fig. 8. Signal properties of the Expert Advisor

Fig. 8. Signal properties of the Expert Advisor

You can select any trailing properties, but we will use "Trailing Stop not used":

Fig. 9. Trailing properties of the Expert Advisor

Fig. 9. Trailing properties of the Expert Advisor

Concerning the money management properties, we will use "Trading with fixed trade volume":

Fig. 10. Money management properties of the Expert Advisor

Fig. 10. Money management properties of the Expert Advisor

By pressing the "Finish" button, we will get the code of the generated Expert Advisor, located in Expert_AMS_ES_MFI.mq5, it will be saved in terminal_data_folder\MQL5\Experts\.

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

must be replaced 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/Signal_ThresholdClose input parameters allow to specify threshold levels for open and close of positions.

In code of the LongCondition() and ShortCondition() methods of the trade signals class we have specified the fixed values of the threshold:

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

The Expert Advisor, generated by MQL5 Wizard open and close position using the "votes" from the modules of trade signals. The vote of the main module (as container, it consist of all the modules added) is also used, but its LongCondition() and ShortCondition() methods always return 0.

The vote results of the main module is also used in "votes" averaging. In our case we have:  main module + 1 module of trade signals, so we need to take this fact into account when setting of the threshold values. Because of this fact the ThresholdOpen and ThresholdClose must be set as 40=(0+80)/2 and 20=(0+40)/2.

The values of  Signal_StopLevel and Signal_TakeLevel input parameters is set to 0, it means that closing of the positions will be done only when closing conditions will be true.


2.4. History backtesting results

Let's consider backtesting of the Expert Advisor on historical data (EURUSD H1, testing period: 2010.01.01-2011.03.16, PeriodMFI=49, MA_period=3).

In creation of Expert Advisor we used the fixed volume (Trading Fixed Lot, 0.1), Trailing Stop algorithm is not used (Trailing not used).

Fig. 11. Testing results of the Expert Advisor, based on Morning/Evening Stars + MFI

Fig. 11. Testing results of the Expert Advisor, based on Morning/Evening Stars + MFI


The best set of input parameters can be found using the Strategy Tester of MetaTrader 5 client terminal.

The code of the Expert Advisor, created by MQL5 Wizard is attached in expert_ams_es_mfi.mq5.


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

MQL5 Wizard - Trade Signals Based on Morning/Evening Stars + CCI MQL5 Wizard - Trade Signals Based on Morning/Evening Stars + CCI

Trade signals based on "Morning Star/Evening Star" candlestick patterns, 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.

Demo_BitmapOffset (OBJPROP_XOFFSET and OBJPROP_YOFFSET) Demo_BitmapOffset (OBJPROP_XOFFSET and OBJPROP_YOFFSET)

If you need to show just a part of the image at the moment and hide other, you can use the moving window by specifying the visible area of the image.

MQL5 Wizard - Trade Signals Based on Morning/Evening Stars + RSI MQL5 Wizard - Trade Signals Based on Morning/Evening Stars + RSI

Trade signals based on "Morning Star/Evening Star" candlestick patterns, confirmed by Relative Strength Index (RSI) indicator is considered. The code of the Expert Advisor based on this strategy can be generated automatically using the MQL5 Wizard.

iChartsSwitchSymbol iChartsSwitchSymbol

When the chart symbol is changed, it will change symbols for all other charts.