How to create algo for Keltner Channel trading System?

How to create algo for Keltner Channel trading System?

Welcome to Episode 16 of Algo Trading Series from MarketSecrets.

In this episode , we are going to learn how to code Keltner Channel trading System.

Let’s get into the set-up.

 

Condition:

This is a slightly complicated strategy, we will be using Keltner Band Code to take buying, selling, shorting and short covering decisions. Keltner Band is kind of similar to Bollinger band in structure, but parameters are different.

Buy: When high of the candle crosses Keltner Top Band

Short: When Keltner Bottom Band crosses low of the candle

Sell: When Keltner Mid Band crosses low of the candle

Cover: When high of the candle crosses Keltner Mid Band

 

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

Script:

_SECTION_BEGIN(“Keltner Channel trading System”);

SetChartOptions(0,chartShowArrows|chartShowDates);

_N(Title = StrFormat(“{{FULLNAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}”, O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

//Initial Parameters

SetTradeDelays( 0,0,0, 0 );

SetOption( “InitialEquity”, 200000);

SetOption(“FuturesMode” ,True);

SetOption(“MinShares”,1);

SetOption(“CommissionMode”,2);

SetOption(“CommissionAmount”,50);

SetOption(“AccountMargin”,10);

SetOption(“RefreshWhenCompleted”,True);

SetPositionSize(150,spsShares);

SetOption( “AllowPositionShrinking”, True );

Plot( Close, “Price”, colorWhite, styleCandle );

//Keltner Band Code

KMid = MA (Close, 20);    //Middle Line

ATRPeriod = 10;         //ATR Period

KFactor = 2;            //Multiplier

KValue = ATR(ATRPeriod) * KFactor;

KTop = KMid + KValue;    //Upper Band

KBottom = KMid – KValue;    //Lower Ban

printf(“\nKTop : ” + KTop ); 

printf(“\nKBottom : ” + KBottom ); 

printf(“\nKMid : ” + KMid ); 

Plot(KTop,”KTop”,colorBlue,styleLine);

Plot(KMid,”KMid”,colorGreen,styleLine);

Plot(KBottom,”KBottom”,colorRed,styleLine);

Buy=Cross(High,KTop);

Short=Cross(KBottom,Low);

Sell=Cross(KMid,Low);

Cover=Cross(High,KMid);

BuyPrice=KTop;

SellPrice=KMid;

ShortPrice=KBottom;

CoverPrice=KMid;

Buy = ExRem(Buy,Sell);

Sell = ExRem(Sell,Buy);

Short=ExRem(Short,Cover);

Cover=ExRem(Cover,Short);

printf(“\nBuy : ” + Buy ); 

printf(“\nSell : ” + Sell ); 

printf(“\nShort : ” + Short ); 

printf(“\nCover : ” + Cover ); 

/* Plot Buy and Sell Signal Arrows */

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-25);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-35);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-30);

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=25);

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=35);

PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-30);

PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15);

PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15);

_SECTION_END();

 

 

First of the script is self explanatory – these are used for setting up the chart title and assign values to the parameters to be used for backtesting , which we have already discussed in detail in the previous episodes. So I’m skipping this part.

SetChartOptions(0,chartShowArrows|chartShowDates);

_N(Title = StrFormat(“{{FULLNAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}”, O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

//Initial Parameters

SetTradeDelays( 0,0,0, 0 );

SetOption( “InitialEquity”, 200000);

SetOption(“FuturesMode” ,True);

SetOption(“MinShares”,1);

SetOption(“CommissionMode”,2);

SetOption(“CommissionAmount”,50);

SetOption(“AccountMargin”,10);

SetOption(“RefreshWhenCompleted”,True);

SetPositionSize(150,spsShares);

SetOption( “AllowPositionShrinking”, True );

Plot( Close, “Price”, colorWhite, styleCandle );

 

Next 6 lines are used for assigning parameters for Keltner Band Code, This is simple direct Assignment using Moving Average, ATR Value and Multiplier, which inturn will be used to create the top and bottom bands.

KMid = MA (Close, 20);    //Middle Line

ATRPeriod = 10;         //ATR Period

KFactor = 2;            //Multiplier

KValue = ATR(ATRPeriod) * KFactor;

KTop = KMid + KValue;    //Upper Band

KBottom = KMid – KValue;    //Lower Ban

 

Next 3 lines are used to print the current value of all 3 Keltner Bands in the chart. This will be visible at the top of the chart.

printf(“\nKTop : ” + KTop ); 

printf(“\nKBottom : ” + KBottom ); 

printf(“\nKMid : ” + KMid );

 

Next 3 lines are used to plot all 3 Keltner Bands in the chart.

Plot(KTop,”KTop”,colorBlue,styleLine);

Plot(KMid,”KMid”,colorGreen,styleLine);

Plot(KBottom,”KBottom”,colorRed,styleLine);

 

The next part is crucial and we will be using Keltner Band values to take buying, selling, shorting and short covering decisions.

Buy: When high of the candle crosses Keltner Top Band

Short: When Keltner Bottom Band crosses low of the candle

Sell: When Keltner Mid Band crosses low of the candle

Cover: When high of the candle crosses Keltner Mid Band

Buy=Cross(High,KTop);

Short=Cross(KBottom,Low);

Sell=Cross(KMid,Low);

Cover=Cross(High,KMid);

 

Next 4 lines are used to assign the buying, selling, shorting and short covering prices.

BuyPrice=KTop;

SellPrice=KMid;

ShortPrice=KBottom;

CoverPrice=KMid;

 

Next 4 lines are used to remove excess buying , selling, shorting and short covering signals.

Buy = ExRem(Buy,Sell);

Sell = ExRem(Sell,Buy);

Short=ExRem(Short,Cover);

Cover=ExRem(Cover,Short);

 

Next 4 lines are used to print the buying, selling, shorting and short covering prices.

printf(“\nBuy : ” + Buy ); 

printf(“\nSell : ” + Sell ); 

printf(“\nShort : ” + Short ); 

printf(“\nCover : ” + Cover );

 

Next Part is used to plot the shapes like stars, up and down arrow in the chart, whenever there is a buy, sell, shorting and short covering signals.

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-25);

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-35);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-30);

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=25);

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=35);

PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-30);

PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15);

PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15);

If you look at the charts, you can spot the buy, sell, shorting and short covering signals using the stars, 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 15.5Lakh profits on a capital of 1L in 6 months with annual return of 5000%.

 

When we execute the backtest of same set-up in the 15 Min Timeframe, this strategy generates approximately 14.5L profits on a capital of 1L in just 6 months with annual return of 4400%.

 

Numbers are mind blowing once again, isn’t it? Well, again., That’s power of Algo-Trading. You can play around the parameters we have used in this strategy to optimize the returns further. Do give this a try and let us know in case of any issues.

 

For more details and examples, checkout the video:

 

 

 

Leave a Reply

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

error: Content is protected !!