How to create algo for MACD Momentum Trading strategy?
Welcome to Episode # of Algo Trading Series from MarketSecrets.
In this episode , we are going to learn how to code a simple MACD Momentum Trading strategy.
Let’s get into the set-up.
Condition:
The MACD indicator is one of the most popular indicators to determine trending prices. The basic trading rules is to buy when it cuts above zero line or buy when its cuts above signal line. When the direction of trend is not clear, MACD stays close to the zero line.
The trading idea is simple.
We need to buy when
- trend is strong
- there is momentum in market
- the market is not sideways
We use smooth MACD(20,40,50) to determine trend and do no trade when it’s value is between -25 to +25 (close to zero).
When the market is dull, the prices move slowly and seldom go beyond their standard deviation levels. So we use Bollinger Band(20,1) to filter out signals generated when prices are between them.
Buy Condition: MACD>0 and MACD> Signal Line and MACD>25 and Signal candle is beyond Bollinger Band
Sell Condition: When either MACD<0 or MACD< Signal Line
Reverse is applicable for shorting.
Let’s begin with the script set-up now.
Script:
_SECTION_BEGIN( “MACD AFL” );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, “Close”, ParamColor( “Color”, colorBlack ), styleNoTitle | ParamStyle( “Style” ) | GetPriceStyle() );
SetBarsRequired( 100000, 0 );
per1 = Param( “MACD Fast”, 20, 1, 100, 1 );
per2 = Param( “MACD Slow”, 50, 1, 100, 1 );
per3 = Param( “Signal Line”, 40, 1, 100, 1 );
per4 = 20;
per5 = 1;
bbtop = BBandTop( C, per4, per5 );
bbot = BBandBot( C, per4, per5 );
SetPositionSize( 1, spsShares );
MACD1 = MACD( per1, per2 );
sig = Signal( per1, per2, per3 );
Buy = MACD1 > 0 AND MACD1 > sig AND L > bbtop AND MACD1 > 25 ;
Sell = MACD1 < 0 OR MACD1 < sig;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = MACD1 < 0 AND MACD1 < sig AND H < bbot AND MACD1 < -25 ;
Cover = MACD1 > 0 OR MACD1 > sig;
Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );
Title = NumToStr( DateTime(), formatDateTime ) + ” O ” + O + ” H ” + H + ” L ” + L + ” C ” + C;
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -45 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );
PlotShapes( IIf( Cover, shapeUpArrow, shapeNone ), colorBlue, 0, L, Offset = -45 );
_SECTION_END();
First of the script is self explanatory – these are parameters used for backtesting , which we have already discussed in detail in the previous episodes. So I’m skipping this part.
Now comes the actual part of the script.
First, we are setting the parameters for Bollinger bands top and bottom band and also MACD Slow, Fast and Signal line.
per1 = Param( “MACD Fast”, 20, 1, 100, 1 );
per2 = Param( “MACD Slow”, 50, 1, 100, 1 );
per3 = Param( “Signal Line”, 40, 1, 100, 1 );
per4 = 20;
per5 = 1;
bbtop = BBandTop( C, per4, per5 );
bbot = BBandBot( C, per4, per5 );
We use the next couple of lines to smoothen the singal
MACD1 = MACD( per1, per2 );
sig = Signal( per1, per2, per3 );
Next line is the buy condition.
I’ll go long if
- MACD>0
- MACD> Signal Line
- MACD>25
- Signal candle is above Upper Bollinger Band
Buy = MACD1 > 0 AND MACD1 > sig AND L > bbtop AND MACD1 > 25 ;
Sell = MACD1 < 0 OR MACD1 < sig;
Square off the long if MACD goes below 0 or signal line.
I’ll short if
- MACD<0
- MACD< Signal Line
- MACD<-25
- Signal candle is below Lower Bollinger Band
Short = MACD1 < 0 AND MACD1 < sig AND H < bbot AND MACD1 < -25 ;
Cover = MACD1 > 0 OR MACD1 > sig;
Square off the long if MACD goes above 0 or signal line.
Rest of the lines are self explanatory – these are used for plotting the charts , which we have already discussed in detail in the previous episodes. So I’m skipping this part as well.
So if you look at the charts, you can spot the signals, you can also run a backtest on this code.
This was one of the strategies used for the momentum trading, especially if you are trading less actively and prefer positional trades. If you look at the chart, just 3 trades were initiated in the last 1 year and it gave a return of around 15%
Do give this a try and let us know in case of any issues.
For more details and examples, checkout the video: https://youtu.be/nGkVlilg5BU
Related
amibroker tutorial videos Free Afl for Amibroker how to backtest amibroker afl? how to code amibroker afl for RSI Crossover System? How to create algo for RSI Crossover System? how to create algo script for zerodha? how to do algo trading in india? how to get free afl for amibroker how to install amibroker how to learn algo trading? how to start with algo trading in india? how to use afl codes how to use amibroker how to write amibroker afl scripts