How to write algo for Stoch Crossover System?

How to write algo for Stoch Crossover System?

Welcome to Episode 10 of Algo Trading Series from MarketSecrets. In this episode , we are going to learn how to code and use Stoch Crossover System. Let’s get into the set-up.

 

Condition:

Stochastic is an oscillator that measures the position of a stock compared with its recent trading range indicating overbought OR oversold conditions. It displays current Day price at a percentage relative to it’s trading range over the specified period of time. In a Slow Stochastic, the highs AND lows are averaged over a slowing period. The default is usually 3 for slow AND 1 for fast. The line can then be smoothed using an exponential moving average, Weighted, OR simple moving average. Confirming Buy/Sell signals can be read at intersections of the %D with the %K as well.

The Stochastic Oscillator always ranges between 0% AND 100%.

  • A reading of 0% shows that the security’s Close was the Lowest price that the security has traded during the preceding x-time periods.
  • A reading of 100% shows that the security’s Close was the Highest price that the security has traded during the preceding x-time periods.
  • When the closing price is near the top of the recent trading range (above 80%), the security is in an overbought condition AND may Signal for a possible correction.
  • Oversold condition exists at a point below %20. The security is in an oversold condition AND may Signal for a possible upmove.
  • Whenever stoch makes a crossover we will initiate the trades accordingly.

 

Let’s begin with the script set-up now.

Script:

_SECTION_BEGIN( “Stoch” );

periods = Param( “Periods”, 15, 1, 200, 1 );

Ksmooth = Param( “%K Avg”, 3, 1, 200, 1 );

a = StochK( periods , Ksmooth);

Plot( a, _DEFAULT_NAME(), ParamColor( “Color”, colorOrange ), ParamStyle(“Style”) );

Dsmooth = Param( “%D Avg”, 3, 1, 200, 1 );

b = StochD( periods , Ksmooth, DSmooth );

Plot( b, _DEFAULT_NAME(), ParamColor( “Color”, colorOrange ), ParamStyle(“Style”) );

PlotOHLC( a,a,b,a,””, IIf(a>b, colorBrightGreen, colorYellow), styleCloud);

 

Overbought= 80; Oversold= 20;

Plot(Overbought,”OB”,colorRed);

Plot(Oversold,”OS”,colorGreen);

Buy = Cross( a, b );

Sell = Cross( b, a );

PlotShapes( shapeSmallUpTriangle * Buy + shapeSmallDownTriangle * Sell, IIf( Buy, colorWhite, colorWhite ) );

_SECTION_END();

 

First, we are setting the time period for Stoch, which is 15 candles. This will calculate the current Day price at a percentage relative to it’s trading range over the last 15 days.

periods = Param( “Periods”, 15, 1, 200, 1 );

Ksmooth = Param( “%K Avg”, 3, 1, 200, 1 );

a = StochK( periods , Ksmooth);

Plot( a, _DEFAULT_NAME(), ParamColor( “Color”, colorBrightGreen ), ParamStyle(“Style”) );

Dsmooth = Param( “%D Avg”, 3, 1, 200, 1 );

b = StochD( periods , Ksmooth, DSmooth );

Plot( b, _DEFAULT_NAME(), ParamColor( “Color”, colorRed ), ParamStyle(“Style”) );

Then we are smoothening signal using the moving average of the price we generated and we are plotting it in the chart.

 

Next we are plotting both the slow and fast signals.

PlotOHLC( a,a,b,a,””, IIf(a>b, colorBrightGreen, colorRed), styleCloud);

Next line is used to color the area between the 2 stock signals we have plotted.

Overbought= 80; Oversold= 20;

Plot(Overbought,”OB”,colorRed);

Plot(Oversold,”OS”,colorGreen);

Now, we are plotting 2 horizontal lines to mark OverSold and OverBought area.

 

Buy = Cross( a, b );

Sell = Cross( b, a );

PlotShapes( shapeSmallUpTriangle * Buy + shapeSmallDownTriangle * Sell, IIf( Buy, colorWhite, colorWhite ) );

 

So when it comes to trade condition,

We will go long When short-term stoch goes above long-term stoch

And we will square off the long position:  When short-term stoch goes below long-term stoch

Reverse is applicable for shorting.

 

So if you look at the charts, you can spot the signals using the triangles.

You can also run a backtest on this code. In this case, we are running the backtest on NIFTY. I’m setting the timeframe as 15 Minutes and we are taking only long trades.

And when it comes to report, this strategy is producing the returns of around 30%, which is extremely good.

 

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/LMejvxcpGSY

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!