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 Crossing of the MACD Indicator's Main Line and Signal Line - MetaTrader 5 Expert

author EAcpu | 5 reads | 0 comments |

MQL5 Wizard - Trade Signals Based on Crossover of Main and Signal lines of MACD indicator - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Crossover of Main and Signal lines of MACD indicator - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Crossover of Main and Signal lines of MACD indicator - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Crossover of Main and Signal lines of MACD indicator - expert for MetaTrader 5

MQL5 Wizard - Trade Signals Based on Crossover of Main and Signal lines of MACD indicator - expert for MetaTrader 5

MQL5 Wizard provides automatic creation of Expert Advisors (see MQL5 Wizard: Creating Expert Advisors without Programming ).

The main line of the MACD indicator is calculated from the difference between the fast EMA and the slow EMA. The MACD signal line is calculated as the main line and smoothed with the PeriodSignal period.

Trade signals:

This strategy is implemented in the C-Signal MACD category trading strategy course MQL5 standard library (located in MQL5\Include\Expert\Signal\SignalMACD.mqh).

MQL5 Wizard - Trade Signals Based on Crossover of Main and Signal lines of MACD indicator - expert for MetaTrader 5

Figure 1. Trading signal based on the crossing of the main MACD line and the signal MACD line

The implementation of the trading strategy is in the C-Signal MACD class, which has some protected methods to simplify access to the indicator values:

 Doubled Major MACD ( Integer Industrials) // Returns the value of the main MACD line of the bar
Double Signal MACD ( Integer Industrial) // Returns the value of the bar's Signal MACD line
Double State MACD ( Integer Industrial) // Returns the difference between the main MACD line and the signal MACD line
Integer external state MACD ( integer industrial); // Returns the number of sign changes of the difference between the main line and the signal line


1. Open a long position

Conditions for opening a long position:

 //+------------------------------------------------------------------+
//|Check the conditions for opening a long position (Buy) |
//+------------------------------------------------------------------+
Boolean value CSignalMACD::CheckOpenLong( double & price, double & SL, double & TP, date time and validity period)
  {
  price = 0.0 ;
  sl =m_symbol.Ask()-m_stop_loss*m_adjusted_point;
  tp =m_symbol.Ask()+m_take_profit*m_adjusted_point; //--- return (external state MACD( 1 )== 1 );
  }


2. Long position closing

Long position closing conditions:

//+------------------------------------------------------------------+
//|Check closing conditions|
//+------------------------------------------------------------------+
Boolean value CSignalMACD::CheckCloseLong( double and price)
  {
  Price = 0.0 ; //--- return (external state MACD( 1 )==- 1 );
  }


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 CSignalMACD::CheckOpenShort( double &price, double &SL, double &TP, datetime &validity)
  {
  price = 0.0 ;
  sl =m_symbol.Bid()+m_stop_loss*m_adjusted_point;
  tp =m_symbol.Bid()-m_take_profit*m_adjusted_point; //--- return (external state MACD( 1 )==- 1 );
  }


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 CSignalMACD::CheckCloseShort( double and price)
  {
  Price = 0.0 ; //--- return (external state MACD( 1 )== 1 );
  }

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 the main MACD line and the signal line " in the "Create ready-made Expert Advisor" option in the MQL5 Wizard :

MQL5 Wizard - Trade Signals Based on Crossover of Main and Signal lines of MACD indicator - expert for MetaTrader 5

Figure 2. Select "Signal based on cross of main line and signal MACD line" in 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 the EA's backtesting of historical data (EURUSD H1, test period: January 1, 2010 - January 5, 2011, PeriodFast=12, PeriodSlow=24, PeriodSignal=9, StopLoss=20, TakeProfit=80).

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 Main and Signal lines of MACD indicator - expert for MetaTrader 5

Attachment: SignalMACD.mqh with the C signal MACD class (included in the MQL5 standard library ) is located in the MQL5\Include\Expert\Signal folder. testmacd.mq5 contains the code of the Expert Advisor created using the MQL5 Wizard.


Attachment download

📎 signalmacd.mqh (10.5 KB)

📎 testmacd.mq5 (6.12 KB)

Source: MQL5 #262

Verification code Refresh