Categories
Uncategorized

How to use the Keltner Channel for Algorithmic Trading

An analysis of the Keltner Channel based algorithmic trading system


How to use the Keltner Channel for Algorithmic Trading

An analysis of the Keltner Channel based algorithmic trading system

The construction of a Keltner Channel begins with the calculation of a moving average and an average true range. Then an upper band is plotted at the moving average plus a multiple of the average true range, and a lower band is plotted at the moving average minus a multiple of the average true range. Generally, the length selected for the moving average is also the length selected for the number of bars included for the average true range. The multiple of the average true range that is added to and subtracted from the moving average determines the sensitivity of the Keltner Channel.

Adding 1x the average true range to the moving average to determine the top band of the channel, and subtracting 1x the average true range to determine the bottom of the channel results in a relatively narrow channel. Adding 5x the average true range to the moving average to determine the top band and subtracting 5x the average true range to determine the bottom band produces a very wide channel. For this system, we selected a 10-bar simple moving average of closes, a 10-bar average true range, and a 1.2 multiple of the average true range.

(I remind you that you can download the complete code in my telgram channel, link at the end of the article)

Our Keltner Channel System attempts to profit from trending price moves while limiting “whipsaw” losses (“whipsaw” losses occur when a sensitive entry indicator is triggered repeatedly causing several relatively small losses within a short period of time). Buying when a market closes above a sensitive moving average (for example, a 10-bar average) and selling short when a market closes below the moving average usually results in an unacceptable number of “whipsaw” losses. Our Keltner Channel attempts to mitigate that problem by requiring prices to rally above the top band of the channel for a buy and to decline below the lower band of the channel for a sell.

Of course, the advantage of filtering out many of the “whipsaw” losses comes with a disadvantage. Our buys will be executed at a higher price (above the top band), and our sells will be executed at a lower price (below the bottom band) than they would be if we bought just above or sold just below the moving average. Our goal is to save more money by reducing “whipsaw” losses than we spend by buying higher and selling lower.

Our setup to buy is a close above the top line of the channel, and our setup to sell short is a close below the bottom line of the channel.

With a buy setup in place, our next step is to determine our entry point. First we measure the distance between the top band of the channel and the moving average. Then we add 50% of that distance to the high of the setup bar for our buy point. When we have a setup to sell short, we first measure the distance between the moving average and the bottom band of the channel.

Then we subtract 50% of that distance from the low of the setup bar to determine our sell point.

We’ll keep the entry orders active for 5 bars; if the entry price is not reached within 5 bars of the cross over, the orders are cancelled and we will wait for another setup to occur.

After we enter a position, we’ll set a money management stop to limit our loss to a specified dollar amount if the market moves against us. To manage the trade, we’ll set a trailing stop at the 4-bar extreme. Our exit, if we are not stopped out by our money management stop or our trailing stop, will be on a close below the moving average (in the case of a long position) and on a close above the moving average (for a short position).

Defining your Trading Rules

In this system, we defined both long entries and short entries as well as exit orders. We also did some setup work to calculate the channel. The setup, entries and exits are described next:

Setup

a) Calculate a channel using the 10-bar simple moving average of closing prices and a 10-bar average true range with a 1.2 multiple.

b) Calculate the channel’s range.

Long Entries

a) Check for a close above the top line of the channel.

b) Once a close is above the top line, measure the distance between the top band of the channel and the moving average and add 50% of that distance to the high of the setup bar. This is our buy point, buy when the price reaches this point.

c) Keep the order active for 5 bars.

Short Entries

a) Check for a close below the bottom line of the channel.

b) Once a close is below the bottom line, measure the distance between the moving average and the bottom band of the channel. Then, subtract 50% of that distance from the low of the setup bar. This is our sell point, sell when the price reaches this point.

c) Keep the order active for 5 bars.

Exit Orders

a) Once we enter a position, set a trailing stop at the 4-bar extreme. We’ll exit our long position when the close penetrates the lowest low of the last 4 bars and we’ll exit our short position when the close penetrates the highest high of the last 4 bars.

b) Also, exit a long position on a close below the moving average and exit a short position on a close above the moving average (for a short position).

Designing & Formatting

This section presents the EasyLanguage instructions and formatting for the system, with the EasyLanguage instructions broken down and explained line by line.

Setup

We calculate the moving average of the closing price as well as of the true range for the last 10 bars, and store the resulting values in the variables AvgVal and AvgRange, respectively.

Then, we multiply AvgRange by the multiplier and add the resulting value to AvgVal. This gives us the upper band of the Keltner Channel. Likewise, we perform the same multiplication again and this time subtract the resulting value from AvgVal. This gives us the lower band of the Keltner Channel. Finally, we calculate the channel range by subtracting the lower band from the top band and dividing the resulting value by 2:

We use two counters, CountL and CountS. We increment these with each bar and reset them each time a buy setup or sell setup occurs, respectively. This way, we can keep a count and use them to keep buy and sell orders active for 5 bars:

Long Entries

Whenever the close (represented here by the input Price) crosses above the upper Keltner Channel band, we will save the high of the bar in the variable SetBar and we will reset the variable CountL to 1. This variable will be incremented on every bar thereafter by the above instructions.

If the close is above the upper Keltner Channel band, and it has been five or less bars since the cross over, we will place a buy stop order at the high of the set up bar, which is the bar on which the close crossed over the upper Keltner Channel band (this value is stored in the variable SetBar) plus 50% of the channel range.

Short Entries

Whenever the close crosses under the lower Keltner Channel band, we store the low in the variable SetBar and reset the variable CountS to 1. As with the variable CountL, this variable is incremented on every bar thereafter by the previous instructions, CountS = CountS + 1.

If the price is less than the lower Keltner Channel band, and it has been five or less bars since the cross under, we will place a sell stop order at the low of the set up bar, which is the bar on which the close crossed under the lower Keltner Channel band (this value is stored in the variable SetBar) minus 50% of the channel range.

Long & Short Exits

We will exit any long position if the close crosses under the moving average of the close. Also, we will cover any short positions if the close crosses over the moving average of the close.

Finally, we will place a trailing stop for long positions at the lowest low of the last four bars. Likewise, we will place a trailing stop to cover our short positions at the highest high of the last four bars.

General System Format

When we apply a system to a chart, we normally use the options in the Format dialog box to format costs, stops, and properties. However, in this system we did not enter an amount for slippage and commission although those costs must certainly be taken into account before a system is traded. We did not specify a default number of shares to trade per order. Instead, we want to focus on what we believe to be a much improved method for determining the number of shares to trade. Please see the section at the end of this chapter, titled, “Investing a Fixed Dollar Amount,” for an explanation of how we are calculating the number of shares to trade.

Note: Remember that Commissions are calculated on a per contract/share basis. When you are trading stocks, you would enter the average commission you are charged divided by the number of shares the system is buying and selling.

We also did not enable a money management stop or a trailing stop for this system. Our initial protective stop for a long position is set at the low of the setup bar plus one point; our initial stop for a short position is set at the high of the setup bar plus one point.

Rather than enabling a trailing stop, we decided to demonstrate a system that exits on signals generated by indicators. We’ll exit our positions with one of two possible strategies. When ADX reaches 30 and ticks down, we’ll exit on the next open. Alternatively, we’ll exit a long position when DMI+ crosses below DMI- and exit a short position when DMI- crosses below DMI+ (we’ll exit with whichever strategy occurs first).

Conclusion

Now I leave it up to you to test this strategy on as many underlyings as you want, I did it but I don’t want to spoil anything! Have fun and let me know your results with a comment on this article! I remind you that you can find the complete system on my Telgram channel.

Bye🤘


If you want discounts for the platform I use contact me privately or buy via this 👉 MultiCharts