OPEN-SOURCE SCRIPT

TASC 2025.05 Trading The Channel

4 295
█ OVERVIEW


This script implements channel-based trading strategies based on the concepts explained by Perry J. Kaufman in the article "A Test Of Three Approaches: Trading The Channel" from the May 2025 edition of TASC's Traders' Tips. The script explores three distinct trading methods for equities and futures using information from a linear regression channel. Each rule set corresponds to different market behaviors, offering flexibility for trend-following, breakout, and mean-reversion trading styles.



█ CONCEPTS


Linear regression

Linear regression is a model that estimates the relationship between a dependent variable and one or more independent variables by fitting a straight line to the observed data. In the context of financial time series, traders often use linear regression to estimate trends in price movements over time.

The slope of the linear regression line indicates the strength and direction of the price trend. For example, a larger positive slope indicates a stronger upward trend, and a larger negative slope indicates the opposite. Traders can look for shifts in the direction of a linear regression slope to identify potential trend trading signals, and they can analyze the magnitude of the slope to support trading decisions.

One caveat to linear regression is that most financial time series data does not follow a straight line, meaning a regression line cannot perfectly describe the relationships between values. Prices typically fluctuate around a regression line to some degree. As such, analysts often project ranges above and below regression lines, creating channels to model the expected extent of the data's variability. This strategy constructs a channel based on the method used in Kaufman's article. It measures the maximum distances from points on the linear regression line to historical price values, then adds those distances and the current slope to the regression points.

Depending on the trading style, traders might look for prices to move outside an established channel for breakout signals, or they might look for price action to reach extremes within the channel for potential mean reversion opportunities.



█ STRATEGY CALCULATIONS


Primary trade rules

This strategy implements three distinct sets of rules for trend, breakout, and mean-reversion trades based on the methods Kaufman describes in his article:
  • Trade the trend (Rule 1): Open new positions when the sign of the slope changes, indicating a potential trend reversal. Close short trades and enter a long trade when the slope changes from negative to positive, and do the opposite when the slope changes from positive to negative.
  • Trade channel breakouts (Rule 2): Open new positions when prices cross outside the linear regression channel for the current sample. Close short trades and enter a long trade when the price moves above the channel, and do the opposite when the price moves below the channel.
  • Trade within the channel (Rule 3): Open new positions based on price values within the channel's range. Close short trades and enter a long trade when the price is near the channel's low, within a specified percentage of the channel's range, and do the opposite when the price is near the channel's high. With this rule, users can also filter the trades based on the channel's slope. When the filter is active, long positions are allowed only when the slope is positive, and short positions are allowed only when it is negative.


Position sizing

Kaufman's strategy uses specific trade sizes for equities and futures markets:
  • For an equities symbol, the number of shares traded is $10,000 divided by the current price.
  • For a futures symbol, the number of contracts traded is based on a volatility-adjusted formula that divides $25,000 by the product of the 20-bar average true range and the instrument's point value.

By default, this script automatically uses these sizes for its trade simulation on equities and futures symbols and does not simulate trading on other symbols. However, users can control position sizes from the "Settings/Properties" tab and enable trade simulation on other symbol types by selecting the "Manual" option in the script's "Position sizing" input.


Stop-loss

This strategy includes the option to place an accompanying stop-loss order for each trade, which users can enable from the "SL %" input in the "Settings/Inputs" tab. When enabled, the strategy places a stop-loss order at a specified percentage distance from the closing price where the entry order occurs, allowing users to compare how the strategy performs with added loss protection.



█ USAGE


This strategy adapts its display logic for the three trading approaches based on the rule selected in the "Trade rule" input:
  • For all rules, the script plots the linear regression slope in a separate pane. The plot is color-coded to indicate whether the current slope is positive or negative.
  • When the selected rule is "Trade the trend", the script plots triangles in the separate pane to indicate when the slope's direction changes from positive to negative or vice versa. Additionally, it plots a color-coded SMA on the main chart pane, allowing visual comparison of the slope to directional changes in a moving average.
  • When the rule is "Trade channel breakouts" or "Trade within the channel", the script draws the current period's linear regression channel on the main chart pane, and it plots bands representing the history of the channel values from the specified start time onward.
  • When the rule is "Trade within the channel", the script plots overbought and oversold zones between the bands based on a user-specified percentage of the channel range to indicate the value ranges where new trades are allowed.

Users can customize the strategy's calculations with the following additional inputs in the "Settings/Inputs" tab:
  • Start date: Sets the date and time when the strategy begins simulating trades. The script marks the specified point on the chart with a gray vertical line. The plots for rules 2 and 3 display the bands and trading zones from this point onward.
  • Period: Specifies the number of bars in the linear regression channel calculation. The default is 40.
  • Linreg source: Specifies the source series from which to calculate the linear regression values. The default is "close".
  • Range source: Specifies whether the script uses the distances from the linear regression line to closing prices or high and low prices to determine the channel's upper and lower ranges for rules 2 and 3. The default is "close".
  • Zone %: The percentage of the channel's overall range to use for trading zones with rule 3. The default is 20, meaning the width of the upper and lower zones is 20% of the range.
  • SL%: If the checkbox is selected, the strategy adds a stop-loss to each trade at the specified percentage distance away from the closing price where the entry order occurs. The checkbox is deselected by default, and the default percentage value is 5.
  • Position sizing: Determines whether the strategy uses Kaufman's predefined trade sizes ("Auto") or allows user-defined sizes from the "Settings/Properties" tab ("Manual"). The default is "Auto".
  • Long trades only: If selected, the strategy does not allow short positions. It is deselected by default.
  • Trend filter: If selected, the strategy filters positions for rule 3 based on the linear regression slope, allowing long positions only when the slope is positive and short positions only when the slope is negative. It is deselected by default.


NOTE: Because of this strategy's trading rules, the simulated results for a specific symbol or channel configuration might have significantly fewer than 100 trades. For meaningful results, we recommend adjusting the start date and other parameters to achieve a reasonable number of closed trades for analysis.

Additionally, this strategy does not specify commission and slippage amounts by default, because these values can vary across market types. Therefore, we recommend setting realistic values for these properties in the "Cost simulation" section of the "Settings/Properties" tab.

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.