Monday – Friday : 9:00 AM – 6:30 PM

MACD Trading

The MACD Trading strategy executed through Amibroker AFL relies on interpreting MACD signals for precise market entry and exit points. amibroker data feed functionalities play a crucial role in identifying trends, crossovers, or divergences, allowing traders to execute trades based on MACD indicator insights. Rigorous backtesting using historical data feeds within Amibroker validates the effectiveness of the MACD Trading strategy across diverse market scenarios.

// TRADING THE MACD Ver 1.0 by Karthik Marar. AFL provided as a part of the Discussion on trading the MACD in the Thread " Experiments in Technical Analysis"
// Afl provided to learn and get an insight for trading using the MACD. The AFL can be freely distributed except for commercial purpose.

r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );
r4 = Param( "Wk slow", 8, 2, 200, 1 );
r5 = Param( "Wk fast", 17, 2, 200, 1 );
GraphXSpace =20;

mycolor=IIf(m1<0 AND m1>s1, 51,IIf(m1>0 AND m1>s1,colorLime,IIf(m1>0 AND m1 0, colorLime, colorRed ); 

TimeFrameSet( inWeekly ); 

kw=TimeFrameExpand( kp, inWeekly ); // expand for display 
khw=TimeFrameExpand( kph, inWeekly ); // expand for display 
mw=TimeFrameExpand( m1w, inWeekly ); // expand for display 
sw=TimeFrameExpand( s1w, inWeekly ); // expand for display 

hcolor=IIf(mw<0 AND mw>sw, 51,IIf(mw>0 AND mw>sw,colorLime,IIf(mw>0 AND mwkhw,IIf(kw>0,colorDarkYellow,colorYellow),IIf(kw>0,colorSkyblue,colorBlue));

Plot( 2,"Wkly MACD ribbon",hcolor,styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
Plot( 2,"Ribbon",gcolor,styleOwnScale|styleArea|styleNoLabel, -0.4, 40,-0.6 );

Plot( m1-s1, "MACD Histogram",  mycolor, styleHistogram | styleThick| styleOwnScale  );


//Zero crossover up

PlotShapes(IIf(j1,shapeDigit1 ,Null),colorPaleGreen,0,Min(0,0),Min(0,0)); 

// crossover above zero

j2=Cross(m1,s1) AND m1>0;
PlotShapes(IIf(j2,shapeDigit2 ,Null),colorYellow,0,0,0); 

//Zero crossover down

j3=Cross(s1,m1) AND m1>0;
PlotShapes(IIf(j3,shapeDigit3 ,Null),colorOrange,0,Min(0,0),0); 

// crossover below zero

PlotShapes(IIf(j4,shapeDigit4 ,Null),colorRed,0,0,0); 

// Histogram peak and troughs
Tp = Ref(pT,-1) == HHV(pT,3);
Vl = Ref(pT,-1)==LLV(pT,3);
PlotShapes(IIf(Vl AND m1>s1 ,shapeSmallCircle+ shapePositionAbove,shapeNone),IIf(m1<0 ,colorYellow,colorLime),0,0,0);
PlotShapes(IIf(Tp AND m1Ref(EMA(PDI(14),3),-5);

//Power Dips - Bullish
PDIp=ADX(14)>MDI(14) AND PDI(14)>MDI(14) AND ap AND Vl AND m1>s1 AND plus ;
PlotShapes(IIf(PDIp,shapeHollowCircle+ shapePositionAbove,shapeNone),colorCustom12,0,0,0);

//power buys
pr2=ADX(14)>20 AND PDI(14)>20 AND ADX(14)>MDI(14) AND PDI(14)>MDI(14) AND plus AND j2;
PlotShapes(IIf(pr2,shapeHollowCircle+ shapePositionAbove,shapeNone),colorCustom12,0,0,20);

//Power Dips - Bearish
PDIm=ADX(14)>PDI(14) AND MDI(14)>PDI(14) AND ap AND Tp AND m120 AND MDI(14)>20 AND ADX(14)>PDI(14) AND MDI(14)>PDI(14) AND Minus AND j4;
PlotShapes(IIf(sr2,shapeHollowCircle+ shapePositionAbove,shapeNone),colorRed,0,0,-20);

pr2a=ADX(14)>20 AND PDI(14)>20 AND ADX(14)>MDI(14) AND PDI(14)>MDI(14) AND  plus AND j1;
PlotShapes(IIf(pr2a,shapeHollowCircle+ shapePositionAbove,shapeNone),colorCustom12,0,0,20);

Filter = j1 OR j2 OR j3 OR j4 OR PDIp OR PDIm OR pr2 OR sr2 ;

AddColumn(j1,"ZL UP",1);
AddColumn(J2,"MA Up",1);
AddColumn(j3,"MA DN",1);
AddColumn(J4,"ZL DN",1);
AddColumn(PDIp,"PDIP UP",1);
AddColumn(pr2,"PHK UP",1);
AddColumn(PDIm,"PDIP DN",1);
AddColumn(sr2,"PHk UP",1);

_SECTION_BEGIN("Display the Signals");
Title = "Trading the MACD" + " - " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor() +

 "  - " + Date() +" - " +EncodeColor(colorLime)+ "MACD= "+WriteVal(m1)+"--"+EncodeColor(colorYellow)+
WriteIf (j1, " MACD Crossed above zero","")+
WriteIf (j2, " Bullish crossover above zero","")+
WriteIf (j4, " MACD Crossed below Zero","")+
WriteIf (j3, " Bearish crossover above zero","")+
WriteIf (PDIP, "  Bullish Power Dip","")+
WriteIf (pr2, " & Power Buy","")+
WriteIf (sr2, " & Power Short","")+
WriteIf (PDIm, " Bearish Power Dip","")+
WriteIf (Hd1, " & Bullish Hook","")+
WriteIf (Hu1, " & Bearish Hook","")+
WriteIf (zlrd1, " & Bearish zeroline Reject","")+
WriteIf (zlru, " & Bullish Zeroline Reject","");

Open chat
Hi, how can I help you?