Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

MQL5 Wizard - Trading signals based on the crossover of two exponentially smoothed moving averages - MetaTrader 5 Expert

author EAcpu | 5 reads | 0 comments |

MQL5 Wizard - Trade Signals Based on Crossover of Two Exponentially Smoothed Moving Averages - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Crossover of Two Exponentially Smoothed Moving Averages - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Crossover of Two Exponentially Smoothed Moving Averages - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Crossover of Two Exponentially Smoothed Moving Averages - expert for MetaTrader 5

MQL5 Wizard allows automatic creation of Expert Advisor codes. See Creating a Ready-made Expert Advisor in MQL5 Wizard for details.

Here we will consider a strategy based on the crossover of two exponentially smoothed moving averages (fast EMA and slow EMA). This strategy is called "Signal based on the intersection of two EMAs" (when automatically creating an EA in the MQL5 Wizard).

Trade signals:

This strategy is implemented in the CSignalCrossEMA class.

MQL5 Wizard - Trade Signals Based on Crossover of Two Exponentially Smoothed Moving Averages - expert for MetaTrader 5

Figure 1. Trading signal based on the crossover of two exponentially smoothed moving averages

The trading strategy is implemented in the CSignalCrossEMA class, which has some protected methods to simplify access to indicator values:

 Double Fast EMA ( Integer Industrial) // Returns the value of the fast moving average of the bar
Double Slow EMA ( Integer Industrial) // Returns the value of the slow moving average of the bar
Double State EMA ( Integer Industrial) // Returns the difference between the fast moving average and the slow moving average of the bar


1. Open a long position

Conditions for opening a long position:

 //+------------------------------------------------------------------+
//|Check the conditions for opening a long position (Buy) |
//+------------------------------------------------------------------+
Boolean CSignalCrossEMA::CheckOpenLong( double &price, double &sl, double &tp,datetime&expiry)
  {
if (!(statusEMA( 2 )< 0 &&statusEMA( 1 )> 0 )) return ( wrong ); //--- price = 0.0 ;
  SL = 0.0 ;
  tp = 0.0 ; //--- return ( true );
  }

2. Long position closing

Long position closing conditions:

 //+------------------------------------------------------------------+
//|Check closing conditions| //+-----------------------------------------------------------------+
boolean CSignalCrossEMA::CheckCloseLong( double &price)
  {
if (!(statusEMA( 2 )> 0 &&statusEMA( 1 )< 0 )) return ( false ); //--- price = 0.0 ; //--- return ( true );
  }


3. Open a short position

The conditions for opening a short position are the same as those for closing a long position.

 //+------------------------------------------------------------------+
//|Check the conditions for opening a short position (sell) |
//+------------------------------------------------------------------+
boolean CSignalCrossEMA::CheckOpenShort( double &price, double &sl, double &tp,datetime&expiry)
  {
if (!(statusEMA( 2 )> 0 &&statusEMA( 1 )< 0 )) return ( wrong ); //--- price = 0.0 ;
  SL = 0.0 ;
  tp = 0.0 ; //--- return ( true );
  }

4. Close position

The conditions for closing a short position are the same as those for opening a long position.

 //+------------------------------------------------------------------+
//|Check short position closing conditions| //+----------------------------------------------------------------------------------+
boolean CSignalCrossEMA::CheckCloseShort( double &price)
  {
if (!(statusEMA( 2 )< 0 &&statusEMA( 1 )> 0 )) return ( false ); //--- price = 0.0 ; //--- 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 the crossing of two EMAs " in the "Create ready-made Expert Advisor" option in the MQL5 Wizard :

MQL5 Wizard - Trade Signals Based on Crossover of Two Exponentially Smoothed Moving Averages - expert for MetaTrader 5

Figure 2. Select "Signal, based on cross of two EMAs" 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.

In addition, the standard library classes contain " Signals based on the intersection of two MAs ", implemented in the CSignalCrossMA class. The trading concept is similar, but it offers many additional features (specification of types, shift and averaging methods and use of take profit and stop loss levels).

MQL5 Wizard - Trade Signals Based on Crossover of Two Exponentially Smoothed Moving Averages - expert for MetaTrader 5

Figure 3. "Signal, based on the intersection of two MAs" in the MQL5 Wizard

Test results

Let us consider backtesting EA trading based on historical data (EURUSD H1, test period: January 1, 2010 - January 5, 2011, FastPeriod=12, SlowPeriod=24).

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 ).

MQL5 Wizard - Trade Signals Based on Crossover of Two Exponentially Smoothed Moving Averages - expert for MetaTrader 5

Figure 4. Historical backtesting results of EA trading (based on the intersection of two EMAs)

Attachment: SignalCrossEMA.mqh with CSignalCrossEMA class must be adjusted to the Terminal_data_folder\MQL5\Include\Expert\Signal folder.

crossover_2ema.mq5 contains the code of the Expert Advisor created using the MQL5 Wizard.


Attachment download

📎 signalcrossema.mqh (10.88 KB)

📎 crossover_2ema.mq5 (5.88 KB)

Source: MQL5 #261

Verification code Refresh