Channel Trailing LX#
This strategy closes a long position with a trailing stop once the position’s profit reaches a defined threshold. It uses the lowest low over a recent lookback period as the exit price, locking in gains while allowing the trade room to run. The Length, FloorAmt, and PositionBasis parameters let traders adapt the strategy to different market conditions.
Source Code#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [IntrabarOrderGeneration = false]
Inputs: Length(3), FloorAmt(1), PositionBasis(false);
Variables: var0(0), var1(0);
var0 = LowestFC(Low, Length);
If PositionBasis = false Then
var1 = CurrentShares * FloorAmt
Else
var1 = FloorAmt;
Condition1 = MarketPosition = 1 and MaxPositionProfit >= var1;
If Condition1 Then
Sell ("ChTrLX") next bar at var0 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: Length(3), FloorAmt(1), PositionBasis(false);
|
Length: Number of past bars used to calculate the lowest low.FloorAmt: The minimum profit that must be reached before the exit order is placed.PositionBasis: Determines how profit is measured. false calculates profit per share/contract; true calculates profit for the entire position. Combined with FloorAmt, this lets you set the profit floor on either a per-unit or whole-position basis.
1
| Variables: var0(0), var1(0);
|
var0: Stores the lowest low over the past Length bars.var1: The calculated profit threshold required before the trailing stop activates.
1
| var0 = LowestFC(Low, Length);
|
Calculates the lowest low over the past Length bars using LowestFC.
1
2
3
4
| If PositionBasis = false Then
var1 = CurrentShares * FloorAmt
Else
var1 = FloorAmt;
|
If PositionBasis is false, the threshold is CurrentShares × FloorAmt (per-unit basis). If true, the threshold is simply FloorAmt (whole-position basis).
1
2
3
| Condition1 = MarketPosition = 1 and MaxPositionProfit >= var1;
If Condition1 Then
Sell ("ChTrLX") next bar at var0 stop;
|
Checks whether the strategy is in a long position (MarketPosition = 1) and whether the maximum open profit has reached or exceeded var1. When both conditions are met, a sell stop order is placed at var0 (the recent lowest low). The exit triggers when the market falls to that price.