How to create algo for Stochastic DK strategy?
In this episode , we are going to learn how to code Stochastic DK strategy.
Let’s get into the set-up.
Condition:
We have 2 conditions for Buying/Selling. Stoch should be above/below specific level and there should be a stoch crossover
Buy: When Stoch K is less than 15 and Stoch K crosses above Stoch D
Sell: When Stoch K is above 85 and Stoch K crosses below Stoch D
Limit of 15 or 85 is just a placeholder for oversold and overbought conditions and can be adjusted dynamically.
Let’s begin with the script set-up now.
Script:
_SECTION_BEGIN(“Stochastic %D”);
SetChartOptions(0, 0, chartGrid20 | chartGrid80);
GraphXSpace = Param(“GraphXSpace”, 5, -10, 20, 1);
OBthreshold = Param(“OBthreshold”, 85, 75, 100, 5);
OSthreshold = Param(“OSthreshold”, 15, 0, 25, 5);
PlotGrid(OBthreshold, colorRed, 8, 1, False);
PlotGrid(OSthreshold, colorGreen, 8, 1, False);
periods = Param(“Periods”, 15, 1, 200, 1);
Ksmooth = Param(“%K avg”, 3, 1, 200, 1);
Dsmooth = Param(“%D avg”, 3, 1, 200, 1);
Plot(StochD(periods, Ksmooth, DSmooth), _DEFAULT_NAME(), ParamColor(“Color”, colorCycle), ParamStyle(“Style”));
_SECTION_END();
_SECTION_BEGIN(“Stochastic %K”);
periods = Param(“Periods”, 15, 1, 200, 1);
Ksmooth = Param(“%K avg”, 3, 1, 200, 1);
Plot(StochK(periods, Ksmooth), _DEFAULT_NAME(), ParamColor(“Color”, colorCycle), ParamStyle(“Style”));
Buy = StochK(periods, Ksmooth) < OSthreshold AND Cross(StochK(periods, Ksmooth), StochD(periods, Ksmooth, DSmooth));
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorBlue);
Sell = StochK(periods, Ksmooth) > OBthreshold AND Cross(StochD(periods, Ksmooth, DSmooth), StochK(periods, Ksmooth));
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed);
_SECTION_END();
First, we are setting up the graph for Stoch and then defining and plotting the threshold for buying and selling limit, which are 15 & 85.
SetChartOptions(0, 0, chartGrid20 | chartGrid80);
GraphXSpace = Param(“GraphXSpace”, 5, -10, 20, 1);
OBthreshold = Param(“OBthreshold”, 85, 75, 100, 5);
OSthreshold = Param(“OSthreshold”, 15, 0, 25, 5);
PlotGrid(OBthreshold, colorRed, 8, 1, False);
PlotGrid(OSthreshold, colorGreen, 8, 1, False);
Next, we are defining the periods, Ksmooth and Dsmooth parameters and then plotting the StochD indicator.
periods = Param(“Periods”, 15, 1, 200, 1);
Ksmooth = Param(“%K avg”, 3, 1, 200, 1);
Dsmooth = Param(“%D avg”, 3, 1, 200, 1);
Plot(StochD(periods, Ksmooth, DSmooth), _DEFAULT_NAME(), ParamColor(“Color”, colorCycle), ParamStyle(“Style”));
In the next step, we are defining the periods and Ksmooth parameters and then plotting the StochK indicator.
periods = Param(“Periods”, 15, 1, 200, 1);
Ksmooth = Param(“%K avg”, 3, 1, 200, 1);
Plot(StochK(periods, Ksmooth), _DEFAULT_NAME(), ParamColor(“Color”, colorCycle), ParamStyle(“Style”));
Now comes the buy and sell conditions and plotting the corresponding Buy/Sell signals in the chart.
Buy = StochK(periods, Ksmooth) < OSthreshold AND Cross(StochK(periods, Ksmooth), StochD(periods, Ksmooth, DSmooth));
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorBlue);
Sell = StochK(periods, Ksmooth) > OBthreshold AND Cross(StochD(periods, Ksmooth, DSmooth), StochK(periods, Ksmooth));
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed);
We’ll go long if
· Stoch is less than 15
· When StochK crosses above StochD
We will Square off the long & can short back if,
· Stoch is above than 85
· When StochK crosses below StochD
If you look at the charts, you can spot the buy and sell signals using the up and down arrows.
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 9K profits on a capital of 1L.
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: