MQL5 Wizard provides the automatic creation of Expert Advisors (see MQL5 Wizard: Creating Expert Advisors without Programming).
Here we will consider the trading signals, based on crossover of MACD indicator lines. The strategy called “Signals based on crossover of main and signal MACD lines” (when creating EA automatically in MQL5 Wizard).
The main line of MACD indicator is calculated as a difference of fast EMA and slow EMA. The signal line of MACD is calculated as the main line, smoothed with PeriodSignal period.
The trade signals:
- Buy: upward crossover of main and signal lines of MACD indicator.
- Sell: downward crossover of main and signal lines of MACD indicator.
This strategy is implemented in CSignalMACDÂ class of the Trading Strategy classes of MQL5 Standard Library (located in MQL5\Include\Expert\Signal\SignalMACD.mqh).
Figure 1. Trade signals, based on crossover of main and signal MACD lines
Trade signals
The trading strategy is implemented in CSignalMACD class, it has some protected methods to simplify access to indicator values:
double MainMACD(int ind)     // returns the value of main MACD line of the bar double SignalMACD(int ind)   // returns the value of signal MACD line of the bar double StateMACD(int ind)    // returns the difference between the main and signal MACD lines int    ExtStateMACD(int ind); // returns the number of sign changes of the main and signal lines difference
1. Open long position
Conditions to open long position:
- ExtStateMACD(1)==1; it means that main line has crossed upward the signal MACD line
//+------------------------------------------------------------------+ //| Checks conditions to open long position (buy)                    | //+------------------------------------------------------------------+ bool CSignalMACD::CheckOpenLong(double& price,double& sl,double& tp,datetime& expiration)   {   price=0.0;   sl  =m_symbol.Ask()-m_stop_loss*m_adjusted_point;   tp  =m_symbol.Ask()+m_take_profit*m_adjusted_point; //---   return(ExtStateMACD(1)==1);   }
2. Close long position
Conditions to close long position:
- ExtStateMACD(1)==1; it means that main line has crossed downward the signal MACD line
//+------------------------------------------------------------------+ //| Checks conditions to close long position | //+------------------------------------------------------------------+ bool CSignalMACD::CheckCloseLong(double& price) Â Â { Â Â price=0.0; //--- Â Â return(ExtStateMACD(1)==-1); Â Â }
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 CSignalMACD::CheckOpenShort(double& price,double& sl,double& tp,datetime& expiration)   {   price=0.0;   sl  =m_symbol.Bid()+m_stop_loss*m_adjusted_point;   tp  =m_symbol.Bid()-m_take_profit*m_adjusted_point; //---   return(ExtStateMACD(1)==-1);   }
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 CSignalMACD::CheckCloseShort(double& price) Â Â { Â Â price=0.0; //--- Â Â return(ExtStateMACD(1)==1); Â Â }
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 main and signal MACD lines” in “Creating Ready-Made Expert Advisors” option of MQL5 Wizard:
Figure 2. Select “Signals based on crossover of main and signal MACD lines” 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.
Testing Results
Let’s consider backtesting of the Expert Advisor on historical data (EURUSD H1, testing period: 1.1.2010-05.01.2011, PeriodFast=12, PeriodSlow=24, PeriodSignal=9, StopLoss=20, TakeProfit=80).
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 3. Testing Results of the Expert Advisor with trading signals, based on crossover MACD lines
Attachments: The SignalMACD.mqh with CSignalMACD class (included in MQL5 Standard Library) is located at MQL5\Include\Expert\Signal folder. The testmacd.mq5 contains the code of the Expert Advisor, created using MQL5 Wizard.