Million Dollar Challenge

Full Version: MACD Trading Strategy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is a simple MACD Trading Strategy. We first determine market direction on daily timeframe then we use M5 or M15 timeframe to trade in the direction of daily trend. Below is the screenshot of this MACD Trading Strategy:
[Image: MACDStrategy.png]

Below arrow is showing the sell signal. Below is the MQL4 code for this MACD Trading Strategy:

//+------------------------------------------------------------------+
//|                                                                     Z2.mq4 |
//|                                                          Ahmad Hassam |
//|                                      https://www.doubledoji.com/ |
//+------------------------------------------------------------------+
#property copyright "Ahmad Hassam"
#property link      "https://www.doubledoji.com/"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_width1 2
#property indicator_width2 2

//Buffer Arrays
double buySignal[];
double sellSignal[];

//Higher Timeframe
int higherTimeframe=1440; //daily timeframe=1440, Weekly timeframe=10880
int higherTimeframeBar=0;

//Trend direction on the Higher Timeframe
int trendDirection = 0;

//MACD Higher Timeframe
double macdHigher1=0.0;
double macdHigher2=0.0;
double macdHigher3=0.0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
 
//custom indicator buffers mapping
IndicatorBuffers(2);

//initialize the custom indicator settings
SetIndexBuffer(0,buySignal);
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,233);
SetIndexEmptyValue(0, 0.0);
SetIndexLabel(0,"Buy Signal");
SetIndexBuffer(1,sellSignal);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,234);
SetIndexEmptyValue(1,0.0);
SetIndexLabel(1,"Sell_Signal");
     
//--- indicator buffers mapping

IndicatorShortName("Trend Trader");


      
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,      // size of input time series
                 const int prev_calculated,  // bars handled in previous call
                 const datetime& time[],     // Time
                 const double& open[],       // Open
                 const double& high[],       // High
                 const double& low[],        // Low
                 const double& close[],      // Close
                 const long& tick_volume[],  // Tick Volume
                 const long& volume[],       // Real Volume
                 const int& spread[]         // Spread
   )
   
   
  {
 
      
        ArraySetAsSeries(buySignal,true);
      ArraySetAsSeries(sellSignal,true);
      
      int bars = rates_total - 1;
      if (prev_calculated > 0) bars =  rates_total - (prev_calculated - 1);
      
      for ( int i =  bars; i >= 0; i--)
      {

      //determine the main trend on the daily timeframe
        higherTimeframeBar= i/96;
          
          macdHigher1=iMACD(NULL,PERIOD_D1,12,26,9,
          PRICE_CLOSE,MODE_MAIN,higherTimeframeBar+1);
          macdHigher2=iMACD(NULL,PERIOD_D1,12,26,9,
          PRICE_CLOSE,MODE_MAIN,higherTimeframeBar+2);
          macdHigher3=iMACD(NULL,PERIOD_D1,12,26,9,
          PRICE_CLOSE,MODE_MAIN,higherTimeframeBar+3);
      //downtrend on higher timeframe
      if (macdHigher3 < macdHigher2 &&
      macdHigher1 < macdHigher2)  trendDirection =-1;
      //else trendDirection =0;
      if (macdHigher3 > macdHigher2 && macdHigher2 > macdHigher1)
      trendDirection =-1;
      //else trendDirection =0;
      //uptrend on higher timeframe
      if (macdHigher3 > macdHigher2 &&
      macdHigher1 > macdHigher2)  trendDirection =+1;
     // else trendDirection =0;
      if (macdHigher3 < macdHigher2 && macdHigher2 < macdHigher1)
      trendDirection =1;
     // else trendDirection =0;    
      //determine the local trend on MACD M30
      double macdM15_1=iMACD(NULL,PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_MAIN,i+1);
      double macdM15_2=iMACD(NULL,PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_MAIN,i+2);
      double macdM15_3=iMACD(NULL,PERIOD_M15,12,26,9,PRICE_CLOSE,MODE_MAIN,i+3);
      //Generate buy signals
      if ( trendDirection==1 && macdM15_3 > macdM15_2 && macdM15_1 > macdM15_2 &&
      macdM15_1 < 0 && macdM15_2 < 0 && macdM15_3 < 0)
         buySignal[i]= Low[i] - 50*Point;
      
      //Generate sell signals
      if ( trendDirection==-1 && macdM15_3 < macdM15_2 && macdM15_1 < macdM15_2 &&
      macdM15_1 > 0 && macdM15_2 > 0 && macdM15_3 > 0)
         sellSignal[i]= High[i] + 50*Point;
            
      }
      //return value of prev_calculated for the next call
      return(rates_total);
  }
//+------------------------------------------------------------------+
I wanted to backtest this MACD Trading Strategy. I did the backtest using R. You can read this thread where I have posted the R code for backtesting this MACD Trading Strategy. The winrate is just 50%. So this MACD Trading Strategy is nothing more than flipping a coin. Now winrate is not always important. If we can catch big moves with a small stop loss we can achieve good results. For example, suppose we have a trading strategy that makes 100 pips with 10 pips stop loss. The winrate of our trading strategy is only 50%. So in 10 trades on average we will win 500 pips and lose 50 pips so we make net 450 pips. This is what I plan to do. Test this MACD Trading Strategy on M5 timeframe by adding a few filters that can reduce the number of false signals. Below is the code for MACD Trading Strategy  on M5 timeframe.


Code:
//+------------------------------------------------------------------+
//|                                                           Z1.mq4 |
//|                                                     Ahmad Hassam |
//|                                      https://www.doubledoji.com/ |
//+------------------------------------------------------------------+
#property copyright "Ahmad Hassam"
#property link      "https://www.doubledoji.com/"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_width1 2
#property indicator_width2 2

//Buffer Arrays
double buySignal[];
double sellSignal[];

//Higher Timeframe
int higherTimeframe=1440; //daily timeframe=1440, Weekly timeframe=10880
int higherTimeframeBar=0;
//Trend direction on the Higher Timeframe
int trendDirection = 0;
//MACD Higher Timeframe
double macdHigher1=0.0;
double macdHigher2=0.0;
double macdHigher3=0.0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
 {
 
//custom indicator buffers mapping
IndicatorBuffers(2);

//initialize the custom indicator settings
SetIndexBuffer(0,buySignal);
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,233);
SetIndexEmptyValue(0, 0.0);
SetIndexLabel(0,"Buy Signal");
SetIndexBuffer(1,sellSignal);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,234);
SetIndexEmptyValue(1,0.0);
SetIndexLabel(1,"Sell_Signal");
   
//--- indicator buffers mapping

IndicatorShortName("Trend Trader");


     
//---
  return(0);
 }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate (const int rates_total,      // size of input time series
                const int prev_calculated,  // bars handled in previous call
                const datetime& time[],     // Time
                const double& open[],       // Open
                const double& high[],       // High
                const double& low[],        // Low
                const double& close[],      // Close
                const long& tick_volume[],  // Tick Volume
                const long& volume[],       // Real Volume
                const int& spread[]         // Spread
  )
 
 
 {
 
     ArraySetAsSeries(buySignal,true);
     ArraySetAsSeries(sellSignal,true);
     
     int bars = rates_total - 1;
     if (prev_calculated > 0) bars =  rates_total - (prev_calculated - 1);
     
     for ( int i =  bars; i >= 0; i--)
     {

     //determine the main trend on the daily timeframe
       higherTimeframeBar= i/288;
         
         macdHigher1=iMACD(NULL,PERIOD_D1,12,26,9,
         PRICE_CLOSE,MODE_MAIN,higherTimeframeBar+1);
         macdHigher2=iMACD(NULL,PERIOD_D1,12,26,9,
         PRICE_CLOSE,MODE_MAIN,higherTimeframeBar+2);
         macdHigher3=iMACD(NULL,PERIOD_D1,12,26,9,
         PRICE_CLOSE,MODE_MAIN,higherTimeframeBar+3);
     //downtrend on higher timeframe
     if (macdHigher3 < macdHigher2 && macdHigher1 < macdHigher2)  
     trendDirection =-1;
     else trendDirection =0;
     if (macdHigher3 > macdHigher2 && macdHigher2 > macdHigher1)
     trendDirection =-1;
     else trendDirection =0;
     //uptrend on higher timeframe
     if (macdHigher3 > macdHigher2 && macdHigher1 > macdHigher2)  
     trendDirection =1;
     else trendDirection =0;
     if (macdHigher3 < macdHigher2 && macdHigher2 < macdHigher1)
     trendDirection =1;
     else trendDirection =0;  
     //determine the local trend on MACD M30
     double macdM5_1=iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,i+1);
     double macdM5_2=iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,i+2);
     double macdM5_3=iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,i+3);
     //Generate buy signals
     if ( trendDirection==1 && macdM5_3 > macdM5_2 && macdM5_1 > macdM5_2 &&
     macdM5_1 < 0 && macdM5_2 < 0 && macdM5_3 < 0)
        buySignal[i]= Low[i] - 50*Point;
     
     //Generate sell signals
     if ( trendDirection==-1 && macdM5_3 < macdM5_2 && macdM5_1 < macdM5_2 &&
     macdM5_1 > 0 && macdM5_2 > 0 && macdM5_3 > 0)
        sellSignal[i]= High[i] + 50*Point;
           
     }
     //return value of prev_calculated for the next call
     return(rates_total);
 }
//+------------------------------------------------------------------+
MACD is a simple indicator. The concept behind MACD is powerful. It is just the difference of two moving averages. When the two moving averages are moving apart it means the momentum in the market is becoming strong. On the other hand when the moving averages are getting closer it means the momentum in the market is becoming weak. We can make use of this powerful concept in a much better manner on the higher timeframes like D1, H12, H8, H6 and H4 as compared to lower timeframes like M15, M30 and H1. Watch the video below where I explain how I use MACD to find the market direction on higher timeframe and then use that information in finding a low risk entry on the lower timeframe. The idea is to catch a big wave in the market with a small stop loss something like a  100 pip move with a 10 pip stop loss.




I have modified the default MACD indicator on MT4 and MT5 and given it a color. When MACD value increases it changes color to green and when the MACD value decreases it changes color to red. The color of MACD gives us the market direction. MACD_Color is just the default MACD available on MT4 and MT5 with color added to it.