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.
The logic is simple but effective. Instead of defining a lookback period, you define a fixed starting point in the past.
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.The primary benefit of this indicator is adding historical context and objectivity to your trend analysis.
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).
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.//------------------------------------------------------
// 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