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 Three Moving Averages - MetaTrader 5 Expert - MT4/MT5 Resources

author EAcpu | 3 reads | 0 comments |

MQL5 Wizard - Trade Signals Based on Three Moving Averages - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Three Moving Averages - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Three 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 trend strategy based on three moving averages. This strategy is called "Signal Based on Three EMAs" . To determine the trend, it uses three exponentially smoothed moving averages: FastEMA, MediumEMA and SlowEMA.

The strategy is implemented in the CSignal3EMA class (signal3ema.mqh must be placed in terminal_data_folder\MQL5\Include\Expert\Signal\Signal3EMA.mqh).

MQL5 Wizard - Trade Signals Based on Three Moving Averages - expert for MetaTrader 5

Figure 1. Trading signals based on three moving averages

The trading strategy is implemented in the CSignal3EMA class, which has some protected methods to simplify access to the values ​​of the three moving averages (fast, medium and slow):

 Double fast EMA ( Integer Industrial) // Returns the fast EMA value of the bar
Double the middle EMA ( Integer Industrial) // Returns the middle EMA value of the bar
Double Slow EMA ( Integer Industrial) // Returns the slow EMA value of the bar
The indicator value of the 0th (unfinished) bar may change, so it is necessary to perform a trading condition check using the data of the completed (formed) bar.

1. Open a long position

An uptrend is determined by the following conditions: FastEMA>MediumEMA>SlowEMA:

 //+------------------------------------------------------------------+
//|Check the conditions for opening a long position (Buy) |
//+------------------------------------------------------------------+
Boolean value CSignal3EMA::CheckOpenLong( double & price, double & SL, double & TP, date time and validity period)
  {
Double mid = mid EMA( 1 ); //--- price = 0.0 ;
  sl =m_symbol.Ask()-m_stop_loss*m_adjusted_point;
  tp =m_symbol.Ask()+m_take_profit*m_adjusted_point; //--- Check the uptrend (on the last completed bar): FastEMA(1)>MediumEMA(1)>SlowEMA(1) returns (FastEMA( 1 )>Medium&& Medium>SlowEMA( 1 ));
  }


2. Long position closing

Downtrend is determined by: FastEMA

 //+------------------------------------------------------------------+
//|Check closing conditions|
//+------------------------------------------------------------------+
boolean CSignal3EMA::CheckCloseLong( double &price)
  {
Double mid = mid EMA( 1 ); //--- price = 0.0 ; //--- Check the downtrend (on the last completed bar): FastEMA(1)  return (fastEMA( 1 )<middle&&middle 1 ));
  }
You can improve long closing: you do not have to wait for a downtrend, you can close your position when it is closed, which can be determined by: FastEMA SlowEMA.


3. Open a short position

//+------------------------------------------------------------------+
//|Check the conditions for opening a short position (sell) |
//+------------------------------------------------------------------+
Boolean value CSignal3EMA::CheckOpenShort ( double & price, double & SL, double & TP, date time and validity period)
  {
Double mid = mid EMA( 1 ); //--- price = 0.0 ;
  sl =m_symbol.Bid()+m_stop_loss*m_adjusted_point;
  tp =m_symbol.Bid()-m_take_profit*m_adjusted_point; //--- Check the downtrend (on the last completed bar): FastEMA(1)  return (fastEMA( 1 )<middle&&middle 1 ));

  }

4. Close position

 //+------------------------------------------------------------------+
//|Check short position closing conditions|
//+------------------------------------------------------------------+
boolean CSignal3EMA::CheckCloseShort( double &price)
  {
Doubled Medium = MediumEMA( 1 ); //--- Price = 0.0 ; //--- Check the uptrend (on the last completed bar): FastEMA(1)>MediumEMA(1)>SlowEMA(1) returns (FastEMA( 1 )>Medium && Medium>SlowEMA( 1 ));
  }

You can improve closing: you do not have to wait for the uptrend, you can close the position when it is closed, which can be determined by: FastEMA>MediumEMA

Create an Expert Advisor using the MQL5 Wizard

To create a trading robot based on a strategy, you need to select the signal property as " Signal based on three EMAs " in the "Create ready-made Expert Advisor" option in the MQL5 Wizard :

MQL5 Wizard - Trade Signals Based on Three Moving Averages - expert for MetaTrader 5

Figure 2. Selecting "Signal based on three 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.

Test results

Let's consider back testing the Expert Advisor based on historical data (EURUSD H1, testing period: January 1, 2010 to January 5, 2011, FastPeriod=5, MediumPeriod=12, SlowPeriod=24, StopLoss=400, TakeProfit=900).

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 Three Moving Averages - expert for MetaTrader 5

Figure 3. Historical backtest results of EA trading (based on three EMAs)

Attachment: Signal3EMA.mqh with CSignal3EMA class must be placed into terminal_data_folder\MQL5\Include\Expert\Signal.

The Threeema.mq5 file contains the code of the Expert Advisor created using the MQL5 Wizard.


Attachment download

📎 signal3ema.mqh (12.94 KB)

📎 threeema.mq5 (6.19 KB)

Source: MQL5 #250

Verification code Refresh