05-30-2018, 03:16 AM
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:
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);
}
//+------------------------------------------------------------------+
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);
}
//+------------------------------------------------------------------+