Keltner Channel SE#
This strategy uses the Keltner Channel to identify short entry opportunities. The Keltner Channel consists of an upper band and a lower band around the market price; prices within the channel are considered normal. When the market price crosses below the lower band, the strategy places a sell short stop order one point below the breakout bar’s low.
Source Code#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| [IntrabarOrderGeneration = false]
Inputs: Price(Close), Length(20), NumATRs(1.5);
Variables: var0(0), var1(0), var2(0), var3(false), var4(0);
var0 = AverageFC(Price, Length);
var1 = NumATRs * AvgTrueRange(Length);
var2 = var0 - var1;
Condition1 = CurrentBar > 1 and Price crosses under var2;
If Condition1 Then Begin
var3 = true;
var4 = Low;
End
Else Begin
Condition1 = var3 and (Price > var0 or Low <= var4 - 1 point);
If Condition1 Then
var3 = false;
End;
If var3 Then
Sell Short ("KltChSE") next bar at var4 - 1 point stop;
|
Code Walkthrough#
1
| [IntrabarOrderGeneration = false]
|
Ensures that only one order is generated per bar; orders are placed only when the bar is complete.
1
| Inputs: Price(Close), Length(20), NumATRs(1.5);
|
Price: The price series used for calculations; defaults to the closing price.Length: The number of bars used to calculate the Keltner Channel; defaults to 20.NumATRs: The ATR multiplier that determines channel width; defaults to 1.5.
1
| Variables: var0(0), var1(0), var2(0), var3(false), var4(0);
|
var0: Stores the moving average value.var1: The ATR value multiplied by NumATRs.var2: The lower Keltner Channel band (var0 - var1).var3: Boolean flag tracking whether entry conditions are met.var4: Stores the low of the breakout bar, used to calculate the entry stop price.
1
2
3
| var0 = AverageFC(Price, Length);
var1 = NumATRs * AvgTrueRange(Length);
var2 = var0 - var1;
|
Calculates the moving average, subtracts the ATR distance scaled by NumATRs, and stores the result as the lower channel band.
1
2
3
4
5
| Condition1 = CurrentBar > 1 and Price crosses under var2;
If Condition1 Then Begin
var3 = true;
var4 = Low;
End
|
Checks whether price crossed below the lower band. If so, sets var3 to true and records the current bar’s low in var4.
1
2
3
4
5
| Else Begin
Condition1 = var3 and (Price > var0 or Low <= var4 - 1 point);
If Condition1 Then
var3 = false;
End;
|
On subsequent bars, resets the entry flag if either: price recovers above the moving average (signal invalidated), or the bar’s low reaches var4 - 1 point (the sell stop was triggered). This prevents the order from being re-placed after the entry has fired.
1
2
| If var3 Then
Sell Short ("KltChSE") next bar at var4 - 1 point stop;
|
While var3 is true, places a sell short stop order one point below the breakout bar’s low. The entry triggers when the market falls to that price.