Watch how to download trading robots for free
Find us on Telegram!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Experts

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

Views:
22372
Rating:
(33)
Published:
2011.01.11 20:30
Updated:
2016.11.22 07:32
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

MQL5 Wizard allows to create the code of Expert Advisors automatically. See Creating Ready-Made Expert Advisors in MQL5 Wizard for the details.

Here we will consider the strategy based on crossover of two exponentially smoothed Moving Averages (fast EMA and slow EMA). The strategy called "Signals based on crossover of two EMA" (when creating EA automatically in MQL5 Wizard).

The trade signals:

  • Buy: the Fast EMA crossovers upward the slow EMA
  • Sell: the Fast EMA crossovers downward the slow EMA

This strategy is implemented in CSignalCrossEMA class.

Figure 1. Trade signals, based on crossover of two exponentially smoothed moving averages

Figure 1. Trade signals, based on crossover of two exponentially smoothed moving averages


Trade Signals

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

double   FastEMA(int ind)      // returns the value of fast moving average of the bar
double   SlowEMA(int ind)      // returns the value of slow moving average of the bar
double   StateEMA(int ind)     // returns the difference between the fast and slow moving averages of the bar


1. Open long position

Conditions to open long position:

  • StateEMA(1)>0 and StateEMA(2)<0: the Fast EMA has crossovered upward the slow EMA on the last completed bar.
//+------------------------------------------------------------------+
//| Checks conditions to open long position (buy)                    |
//+------------------------------------------------------------------+
bool CSignalCrossEMA::CheckOpenLong(double& price,double& sl,double& tp,datetime& expiration)
  {
   if(!(StateEMA(2)<0 && StateEMA(1)>0)) return(false);
//---
   price=0.0;
   sl   =0.0;
   tp   =0.0;
//---
   return(true);
  }

2. Close long position

Conditions to close long position:

  • StateEMA(1)<0 and StateEMA(2)>0: the Fast EMA has crossovered downward the slow EMA on the last completed bar.
//+------------------------------------------------------------------+
//| Checks conditions to close long position                         |
//+------------------------------------------------------------------+
bool CSignalCrossEMA::CheckCloseLong(double& price)
  {
   if(!(StateEMA(2)>0 && StateEMA(1)<0)) return(false);
//---
   price=0.0;
//---
   return(true);
  }


3. Open short position

The conditions to open short position are the same as long position closing conditions.

//+------------------------------------------------------------------+
//| Checks conditions to open short position (sell)                  |
//+------------------------------------------------------------------+
bool CSignalCrossEMA::CheckOpenShort(double& price,double& sl,double& tp,datetime& expiration)
  {
   if(!(StateEMA(2)>0 && StateEMA(1)<0)) return(false);
//---
   price=0.0;
   sl   =0.0;
   tp   =0.0;
//---
   return(true);
  }

4. Close short position

The conditions to close short position are the same as long position opening conditions.

//+------------------------------------------------------------------+
//| Checks conditions to close short position                        |
//+------------------------------------------------------------------+
bool CSignalCrossEMA::CheckCloseShort(double& price)
  {
   if(!(StateEMA(2)<0 && StateEMA(1)>0)) return(false);
//---
   price=0.0;
//---
   return(true);
  }

Creating Expert Advisor using MQL5 Wizard

To create a trading robot based on the strategy you need to choose the signal properties as "Signals based on crossover of two EMA" in "Creating Ready-Made Expert Advisors" option of MQL5 Wizard:

Figure 2. Choose "Signals, based on crossover of two EMA" in MQL5 Wizard

Figure 2. Choose "Signals, based on crossover of two EMA" in MQL5 Wizard

The next you have to specify the needed trailing stop algorithm and money and risk management system. The code of Expert Advisor will be created automatically, you can compile it and test in Strategy Tester of MetaTrader 5 client terminal.

In addition, the Standard library classes contains the "Signals based on crossover of two MA", implemented in the CSignalCrossMA class. The trading idea is similar, but it provides many additional features (specify types, shifts and averaging methods and use of Take Profit and Stop Loss levels).

Figure 3. "Signals, based on crossover of two MA" in MQL5 Wizard

Figure 3. "Signals, based on crossover of two MA" in MQL5 Wizard


Testing Results

Let's consider backtesting of the Expert Advisor on historical data (EURUSD H1, testing period: 1.1.2010-05.01.2011,   FastPeriod=12, SlowPeriod=24).

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

Figure 4. Historical backtesting results of the Expert Advisor, based on crossover of two EMA

Figure 4. Historical backtesting results of the Expert Advisor, based on crossover of two EMA


Attachments: The SignalCrossEMA.mqh with CSignalCrossEMA class must be paced to terminal_data_folder\MQL5\Include\Expert\Signal folder.

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

Translated from Russian by MetaQuotes Ltd.
Original code: https://www.mql5.com/ru/code/261

SelfGenerator SelfGenerator

This script generates a file with its source code (solution of the classical program in MQL5). It may be useful to study programming and algorithms.

MQL5 Wizard - Trade Signals Based on Price Crossover with Moving Average, confirmed by ADX MQL5 Wizard - Trade Signals Based on Price Crossover with Moving Average, confirmed by ADX

Trade signals based on price crossover with moving average indicator confirmed by ADX is considered. The code of the Expert Advisor based on this strategy can be generated automatically using the MQL5 Wizard.

MQL5 Wizard - Trade Signals Based on Crossover of Two EMA with intraday time filter MQL5 Wizard - Trade Signals Based on Crossover of Two EMA with intraday time filter

Trade signals based on price crossover of two exponentially smoothed moving averages with intraday filter is considered. The code of the Expert Advisor based on this strategy can be generated automatically using the MQL5 Wizard.

MQL5 Wizard - Trade Signals Based on Crossover of Main and Signal lines of MACD indicator MQL5 Wizard - Trade Signals Based on Crossover of Main and Signal lines of MACD indicator

Trade signals based on crossover of main and signal lines of MACD indicator (CSignalMACD from MQL5 Standard Library) is considered. The code of the Expert Advisor based on this strategy can be generated automatically using the MQL5 Wizard.