Keltner Channel LE#
This strategy uses the Keltner Channel to identify long 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 above the upper band, the strategy places a buy stop order one point above the breakout bar’s high.
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 over var2;
If Condition1 Then Begin
var3 = true;
var4 = High;
End
Else Begin
Condition1 = var3 and (Price < var0 or High >= var4 + 1 point);
If Condition1 Then
var3 = false;
End;
If var3 Then
Buy ("KltChLE") 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 upper Keltner Channel band (var0 + var1).var3: Boolean flag tracking whether entry conditions are met.var4: Stores the high 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, adds the ATR distance scaled by NumATRs, and stores the result as the upper channel band.
1
2
3
4
5
| Condition1 = CurrentBar > 1 and Price crosses over var2;
If Condition1 Then Begin
var3 = true;
var4 = High;
End
|
Checks whether price crossed above the upper band. If so, sets var3 to true and records the current bar’s high in var4.
1
2
3
4
5
| Else Begin
Condition1 = var3 and (Price < var0 or High >= var4 + 1 point);
If Condition1 Then
var3 = false;
End;
|
On subsequent bars, resets the entry flag if either: price falls back below the moving average (signal invalidated), or the bar’s high reaches var4 + 1 point (the buy stop was triggered). This prevents the order from being re-placed after the entry has fired.
1
2
| If var3 Then
Buy ("KltChLE") next bar at var4 + 1 point stop;
|
While var3 is true, places a buy stop order one point above the breakout bar’s high. The entry triggers when the market rises to that price.