How to create algo for Wilders Moving Average Crossover Trading strategy?
Welcome to Episode 14 of Algo Trading Series from MarketSecrets.
In this episode , we are going to learn how to code Wilders Moving Average Crossover Trading strategy.
Let’s get into the set-up.
Condition:
This is simple strategy, we use wilders indicator and using Moving Average crossover of 2 wilders indicator to take buying or selling decisions.
Buy: When 7.5 period Wilders Moving Average crosses above 11 Period Wilders Moving Average
Sell: When 7.5 period Wilders Moving Average crosses below 11 Period Wilders Moving Average
Wilders is a very complicated strategy in itself and we won’t be getting into the details of this strategy. Since it is algo, we can use just 1 line of code to pull the Wilders data and it is enough for us. If you are interested in learning Wilders strategy, let us know in the comments section, we will cover it as part of “Trading Strategy” series if there is enough interest,
Let’s begin with the script set-up now.
Script:
_SECTION_BEGIN(“CandleColor”);
SetChartOptions( 0, chartShowDates | chartShowArrows );
Plot( C, “Close”, ParamColor(“Color”, colorDefault ), styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN(“Wilders”);
M1 = Wilders(Close,7.5);
M2 = Wilders(Close,11);
Plot(M1, “”, colorBrightGreen,styleLine);
Plot(M2, “”, colorRed,styleLine);
A = Cross(M1,M2);
B = Cross(M2,M1);
PlotShapes(IIf(A,shapeUpArrow,shapeNone),colorBrightGreen,0,L,Offset= -30);
PlotShapes(IIf(B,shapeDownArrow,shapeNone),colorRed,0,H,Offset = -30);
Buy= A;
Sell = B;
_SECTION_END();
_SECTION_BEGIN(“Swing Trend”);
no=20;
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
supres=IIf(avn==1,sup,res);
supres_col=IIf(avn==1,colorGreen,colorOrange);
Plot(supres,””,supres_col,styleStaircase|styleThick);
_SECTION_END();
First, we are setting up the chart to plot the prices
SetChartOptions( 0, chartShowDates | chartShowArrows );
Plot( C, “Close”, ParamColor(“Color”, colorDefault ), styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );
The next part is crucial and the first 2 lines represent the 7.5 & 11 point moving averages of Wilders indicator. We are pulling and storing this value in 2 variables.
M1 = Wilders(Close,7.5);
M2 = Wilders(Close,11);
Next 2 lines are used to plot these moving averages in the charts.
Plot(M1, “”, colorBrightGreen,styleLine);
Plot(M2, “”, colorRed,styleLine);
Next 2 lines are used to find the crossover points of these 2 moving averages.
A = Cross(M1,M2);
B = Cross(M2,M1);
Next 4 lines represent the Buy & Sell statements and in addition, we are plotting a small shapes to indicate the buy and sell signals in the chart.
PlotShapes(IIf(A,shapeUpArrow,shapeNone),colorBrightGreen,0,L,Offset= -30);
PlotShapes(IIf(B,shapeDownArrow,shapeNone),colorRed,0,H,Offset = -30);
Buy= A;
Sell = B;
We will Buy: When 7.5 period Wilders Moving Average crosses above 11 Period Wilders Moving Average
We will Sell: When 7.5 period Wilders Moving Average crosses below 11 Period Wilders Moving Average
Next Part is used to plot the current swing direction. We are using typical Swing highs and swing lows to create a supertrend like staircase structure in our chart. But it is not used by the strategy in anyway. This is just used to plot the staircase in the charts.
_SECTION_BEGIN(“Swing Trend”);
no=20;
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
supres=IIf(avn==1,sup,res);
supres_col=IIf(avn==1,colorGreen,colorOrange);
Plot(supres,””,supres_col,styleStaircase|styleThick);
_SECTION_END();
If you look at the charts, you can spot the buy and sell signals using the up and down arrows and also the staircase and moving averages.
You can also run a backtest on this code, I am running the backtest now on Nifty and BankNifty for 6 months. This is being executed on 1 hour Timeframe and we are taking only long positions. This generates approximately 32K profits on a capital of 1L.
When we execute the backtest on the 15 Min Timeframe, this strategy generates approximately 39K profits on a capital of 1L. This strategy generates 80% returns per annum.
You can play around the parameters we have used in this strategy to optimize the returns. Do give this a try and let us know in case of any issues.
For more details and examples, checkout the video:
Related
how to backtest amibroker afl? how to code amibroker afl for swing trading strategy? how to code amibroker afl for Wilder strategy? How to create algo for swing trading strategy? How to create algo for Wilder strategy? how to create algo script for zerodha? how to do algo trading in india? how to install amibroker how to learn algo trading? how to start with algo trading in india? how to use afl codes how to write amibroker afl scripts