Upgrade Edition of Keltner Channel trading Strategy
Tags: trading, strategy, keltner, fmzquant
Published: 2024-06-06 08:49:56

## Introduction to the Keltner Channel trading Strategy
The Keltner channel is a trading system invented by Chester W. Keltner in the 1960s. Its core idea is the average line theory. And at that time the system had achieved remarkable results for a very long time. Although the original Keltner channel system was not as effective as it was when it first appeared, its core idea has so far had a profound impact on the trading community.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/663h3a029d3o6613oyld.png)

## The principle of the Keltner channel
Speaking of the channel type strategy, you may think of the famous Bollinger Band (BOLL), but the difference is that the Keltner channel uses the average of the highest price, the lowest price, and the closing price as the base price, and then calculates The N-period average of this base price, which is the middle rail of the Keltner channel. The upper rail is the multiple of the middle rail plus the fluctuation amplitude, and the lower rail is the multiple of the middle rail minus the fluctuation amplitude.

So how is this fluctuation amplitude calculated? That is, the average value of the N period (highest price - lowest price), multiplied by a certain number. In this way, you will find that it is similar to the Bollinger Band (BOLL), there is also the price of the middle rail, and the upper and lower rails calculated according to the price of the middle rail. However, the Keltner channel is smoother than the Bollinger Band (BOLL).

## Calculation formula of Keltner channel
- Base price: (highest price + lowest price + closing price) / 3
- Middle rail: N-period moving average of the base price
- Volatility: highest price - lowest price
- Upper rail: middle rail + fluctuation amplitude * multiple
- Lower rail: middle rail - fluctuation amplitude * multiple

## Upgraded version of Keltner Strategy
Later on, the Keltner channel was improved by Linda Raschke. Linda Raschke is a well-known trader in commodity futures in the US and president of LBR Asset Management. The original Keltner strategy Middle rail is a normal moving average that was changed to an exponential average. In addition, the calculation method of the fluctuation range is also changed to the average true fluctuation range (ATR). Its calculation formula is:

- Base price: (highest price + lowest price + closing price) / 3
- Middle rail: N-period exponential moving average of the base price
- Volatility: Average true fluctuation range (ATR)
- Upper rail: middle rail + fluctuation range
- Lower rail: middle rail - fluctuation range

## Keltner channel trading strategy
We know that prices don't always run in a trend or turbulent way, but in a way that trends and oscillations do not completely alternate randomly. Then Keltner uses the channel as a dividing line to separate the trend market from the turbulent market. When the price runs between the upper and lower rails, we can think of it as a turbulent market. When the price breaks above the upper limit, it shows that a stronger buying pressure has emerged, and the price will continue rise in the future. When the price breaks the lower rail, it shows that there is already a stronger selling pressure, and the price may continue fall in the future.

**Open Position**
- The middle rail is up, and the price rises above the upper rail, opening long position;
- The middle rail is down, and the price falls below the lower rail, opening short position;

**Close Position**
- When holding long position, the price fell below the middle rail, close long position;
- When holding short position, the price rises above the middle rail, close short position;

## Using MyLanguage to write Keltner Strategy
Through the above trading logic, we can build this strategy on the FMZ Quant platform. Let's using the Mylanguage as an example. Following these steps: fmz.com > Login > Dashboard > Strategy Library > New Strategy > Click the drop-down box in the upper left corner to select My language, start writing the strategy, and pay attention to the comments in the code below.
```
// parameter
MAN:=20;
ATRN:=50;

JG:=(HIGH+LOW+CLOSE)/3; // base price
ZG:MA(JG, MAN); // Middle rail
TRUEHIGH1:=IF(HIGH>REF(C,1),HIGH,REF(C,1));
TRUELOW1:=IF(LOW<=REF(C,1), LOW, REF(C,1));
TRUERANGE1:=IF(ISLASTBAR,H-L,TRUEHIGH1-TRUELOW1); // Calculate the true fluctuation range
SG: ZG+MA (TRUERANGE1, ATRN); // Upper rail
XG: ZG-MA (TRUERANGE1, ATRN); // Lower rail

ZG>REF(ZG,1)&&C>SG,BK; // The middle rail is up, and the price rises above the upper rail. open long position
C<ZG, SP; // When holding long position, the price falls below the middle rail, close long position
ZG<REF(ZG,1)&&C<XG,SK; // The middle rail is down, and the price falls below the lower rail, open short position
C>ZG, BP; // When holding short position, the price rises above the middle rail, close short position
AUTOFILTER; // Set the signal filtering method
```

## Keltner Strategy Backtest
In order to get closer to the real trading environment, we used the 2 pips of slippage and 2 times the normal transaction fee to test the pressure during the backtest. The test environment is as follows:

- Exchange: BitMEX
- Trading Target: XBTUSD
- Time: January 01, 2019 ~ July 27, 2019
- Cycle: one hour k-line
- Slippage: 2 pips for opening and closing positions
- Fee: 2 times of the normal exchange transaction fee

**Backtest environment**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d9dlq3e7o9egw9ut6xk6.png)

**Profit report**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6z6rvqfzerpqnjsyu5uk.png)

**Fund curve**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/do7vuvxldvsf7jhx0pdg.png)

The above figures are the backtest results of the XBTUSD perpetual contract on the BitMEX exchange. In the trend market, Keltner strategy still maintains valid. Although its efficiency is not too high, the overall fund curve is upward. Even in the retracement of the market trend in July 2019, the net value curve did not have a large retracement.

## Strategy source code
For the complete source code of this strategy, please click it : https://www.fmz.com/strategy/159285

## Summary
Although Keltner is an old trading method, we have restored its value through coding its logic and improved it. It turns out that this strategy is still valid today. Especially in the field of low and medium frequency CTA strategy, Keltner strategy still has something to dig from, that is, cut off losses and let profits run!

It can be said that most successful trading methods adhere to the trading philosophy of “Less loss when losing, earn a little more when earing”, and then consistently implement this concept. Therefore, as a long-term trading strategy, the short-term loss will inevitably bear the cost, and short-term profit is not our goal.

From: https://blog.mathquant.com/2019/07/31/upgrade-edition-of-keltner-channel-trading-strategy.html