Dynamic Anchored Regression Channel

Category: Indicators By: Iván González Created: November 14, 2025, 11:16 AM
November 14, 2025, 11:16 AM
Indicators
2 Comments

Regression channels are a powerful tool for analyzing trends. They plot a central “mean” line of a trend and upper/lower bands to measure volatility, helping traders identify overbought or oversold conditions.

However, most standard regression channels are based on a “rolling” fixed period (e.g., the last 100 bars). This can be limiting. What if you want to analyze a trend not from “100 bars ago,” but from its specific starting point—like a major market bottom or a key news event?

This indicator solves that problem. It allows you to anchor a regression channel to a specific date and time, creating a dynamic tool that grows and adapts with your chart.

How It Works & Key Features

The logic is simple but effective. Instead of defining a lookback period, you define a fixed starting point in the past.

  1. Anchoring: You set a StartDate and StartTime in the code’s configuration. The indicator finds this single bar on your chart and uses it as the permanent “anchor” (Bar 0) for all its calculations.
  2. Dynamic Recalculation: The “dynamic” part is key. The channel is always calculated from that single anchor point up to the current, most recent bar.
  3. Growing Channel: As each new bar forms, the indicator automatically recalculates the entire channel’s slope, intercept, and standard deviation. This means you can watch the trend’s path and volatility evolve in real-time, all based on its original starting point.

Main Use Cases

The primary benefit of this indicator is adding historical context and objectivity to your trend analysis.

  • Analyze Trends from a Specific Event: This is the most powerful feature. You can anchor the channel’s start to:
    • A major market bottom or top.
    • A significant news release (e.g., an earnings report or central bank decision).
    • The beginning of a new year, quarter, or month.
  • Objective Trend Analysis: It removes the guesswork of picking the “right” lookback period (e.g., 50, 100, or 200). Your analysis is based on the trend’s entire life since the anchor point you selected.
  • Visualize Volatility: By watching the standard deviation bands, you can see if a trend is expanding (volatility increasing, bands widening) or contracting (volatility decreasing, bands narrowing) as it matures.

Configuration

You can easily configure the channel by editing the “USER INPUTS” section at the top of the code.

  • StartDate: Enter the starting date for the channel. The format must be YYYYMMDD (e.g., 20250128 for January 28, 2025).
  • StartTime: Enter the starting time. The format must be HHMMSS (e.g., 150000 for 3:00:00 PM).
    • Important: This time is based on your chart’s time (the time displayed on the x-axis), not necessarily your local computer time.
  • StdMultiplier: (Default: 2). This controls the width of the upper and lower bands. A value of 2 is standard (capturing ~95% of price action), but you can use 1 for a tighter channel or 3 for a wider one.

ProBuilder Code

//------------------------------------------------------
// PRT_Anchored Regression Channel
// Description: Draws an anchored regression channel based on a
// user-specified start date and time.
// The channel recalculates dynamically on each new bar.
// version = 0
// 14.11.2025
// Iván González @ www.prorealcode.com
// Sharing ProRealTime knowledge
//------------------------------------------------------
DEFPARAM DRAWONLASTBARONLY = TRUE
//------------------------------------------------------
// --- USER INPUTS ---
//------------------------------------------------------
// The user must enter the start date and time here
StartDate = 20250501 // Format YYYYMMDD
StartTime = 150000   // Format HHMMSS
StdMultiplier = 2    // Standard Deviation Multiplier (e.g., 2)
expand = 150          // Number of bars to project into the future
//------------------------------------------------------
// --- 1. Find the starting BarIndex (only once) ---
//------------------------------------------------------
ONCE FoundStart = 0
ONCE StartBarIndex = -1
MyDateTime = Date * 1000000 + Time
InputDateTime = StartDate * 1000000 + StartTime

IF NOT FoundStart AND MyDateTime >= InputDateTime THEN
   StartBarIndex = barindex
   FoundStart = 1
ENDIF
//------------------------------------------------------
// --- 2. Manual Linear Regression Calculation ---
//------------------------------------------------------
// N = Number of bars from the start to the current bar
N = barindex - StartBarIndex + 1

if islastbarupdate then
   // Initialize sums
   SumX = 0
   SumY = 0
   SumXY = 0
   SumX2 = 0
   
   // Loop to calculate the sums
   // (x = relative index 0, 1, 2...N-1; y = closing price)
   FOR i = 0 TO N - 1 DO
      x = i
      y = close[N - 1 - i] // close[N-1]...close[0]
      
      SumX = SumX + x
      SumY = SumY + y
      SumXY = SumXY + (x * y)
      SumX2 = SumX2 + (x * x)
   NEXT
   
   // Calculate Slope and Intercept
   Slope = 0
   Intercept = 0
   Denominator = (N * SumX2 - SumX * SumX)
   
   IF Denominator <> 0 THEN
      Slope = (N * SumXY - SumX * SumY) / Denominator
      Intercept = (SumY - Slope * SumX) / N
   ENDIF
   //------------------------------------------------------
   // --- 3. Standard Deviation of Residuals Calculation ---
   //------------------------------------------------------
   SumResidualsSq = 0 // Sum of squared residuals
   
   FOR i = 0 TO N - 1 DO
      x = i
      y = close[N - 1 - i]
      
      // Value of the regression line at that point 'x'
      RegressionValue = Intercept + Slope * x
      
      // Residual (difference between actual price and the line)
      Residual = y - RegressionValue
      
      // Sum the square of the residual
      SumResidualsSq = SumResidualsSq + (Residual * Residual)
   NEXT
   
   // Standard deviation of the residuals
   StdDev = SQRT(SumResidualsSq / N)
   //------------------------------------------------------
   // --- 4. Calculate channel start and end points ---
   //------------------------------------------------------
   // Start points (at relative index x=0)
   RegStart = Intercept
   UpperStart = RegStart + StdMultiplier * StdDev
   LowerStart = RegStart - StdMultiplier * StdDev
   
   // Future End points
   // Relative index of the current bar is (N - 1)
   // Relative index of the future bar is (N - 1) + expand
   FutureRelativeIndex = (N - 1) + expand
   
   RegEnd = Intercept + Slope * FutureRelativeIndex
   UpperEnd = RegEnd + StdMultiplier * StdDev
   LowerEnd = RegEnd - StdMultiplier * StdDev
   
   // Absolute bar index for the future end point
   FutureAbsoluteIndex = barindex + expand
   //------------------------------------------------------
   // --- 5. Draw the Channel ---
   //------------------------------------------------------
   // (Using absolute BarIndex: StartBarIndex and barindex)
   DRAWSEGMENT(StartBarIndex, RegStart, FutureAbsoluteIndex, RegEnd) COLOURED(0, 0, 255) STYLE(DottedLine)
   DRAWSEGMENT(StartBarIndex, UpperStart, FutureAbsoluteIndex, UpperEnd) COLOURED(255, 0, 0)
   DRAWSEGMENT(StartBarIndex, LowerStart, FutureAbsoluteIndex, LowerEnd) COLOURED(0, 150, 0)
endif
//------------------------------------------------------
RETURN

Download
Filename: PRT_Anch.Regression-Channel.itf
Downloads: 56
Iván González Master
I usually let my code do the talking, which explains why my bio is as empty as a newly created file. Bio to be initialized...
Author’s Profile

Comments

Logo Logo
Loading...