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

  • Platform: Metatrader5
  • Currency pairs: Any pairs, indices and metals
  • Trading Time: Around the clock
  • Recommended broker: icmarkets, XM
  • 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.

    Get E-Book Worth $100 for Free!!!
    //+------------------------------------------------------------------+
    //| 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.

  • 1. Open MT5 trading software – “File” – “Open Data Folder” in the upper right corner
  • 2, find the “MQL5” folder – “Experts”, put the EA in this folder
  • 3, need to refresh or restart the trading software to display EA, find “EA” in “Navigation” – “EA Trading”
  • 4, find the EA you need to use, right click the mouse, select “attach to the chart” (additional chart: EA uses the currency to be linked, if you need to hang Europe and America, please attach to the AUDUSD chart)
  • 5. After adding the chart, set the EA parameter. In the “Common” option, select “Multi-empty position” and check the box in front of “Allow real-time automatic trading”
  • 6, in the “input parameters”, set the relevant parameters, generally modify the number of single hand, jiacang distance, loss ratio, 10,000 US dollars according to the minimum parameter settings, after setting “confirm
  • 7. After setting the EA parameters, start the EA. In the “Tools” – “Options” – “EA” – check “Allow DLL import” to confirm
  • 0/5 (0 Reviews)