Watch how to download trading robots for free
Find us on Twitter!
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 Main and Signal lines of MACD indicator - expert for MetaTrader 5

Views:
25051
Rating:
(34)
Published:
2011.01.14 12:55
Updated:
2016.11.22 07:32
signalmacd.mqh (10.5 KB) view
testmacd.mq5 (6.12 KB) view
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

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

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

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

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.

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

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 Two Exponentially Smoothed Moving Averages MQL5 Wizard - Trade Signals Based on Crossover of Two Exponentially Smoothed Moving Averages

Trade signals based on price crossover of two exponentially smoothed moving averages 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 Lines of  the Alligator Indicator MQL5 Wizard - Trade Signals Based on Crossover of Lines of the Alligator Indicator

Trade signals based on crossover of lines of the Alligator technical indicator is considered. The code of the Expert Advisor based on this strategy can be generated automatically using the MQL5 Wizard.

Professional ZigZag Professional ZigZag

The improved version of the ZigZag indicator.