Indicator · Pine Script v5 · On the chart

Parallel Bounding Trend Lines

by koleharv

This indicator calculates parallel bounding trend lines based on linear regression of close prices over a specified lookback period.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code115
Settings1inputs
LicenceMIT
Last update3 months ago16 Aug 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

115 lines of Pine Script v5 from new-examples/trend_2(parallel fast).pine.txt, published under MIT. Keep the author's credit and licence lines when you reuse it.

trend_2(parallel fast).pine.txt
//@version=5
indicator("Parallel Bounding Trend Lines", overlay=true)

// Input
lookback = input.int(50, "Lookback Period", minval=2)

// Function to calculate parallel bounding lines for a window
f_calculateParallelBounds(_lookback) =>
    // 1. Calculate Central Tendency Line (using close)
    linregClose = ta.linreg(close, _lookback, 0)
    linregClosePrev = ta.linreg(close, _lookback, 1)
    commonSlope = linregClose - linregClosePrev
    // Calculate intercept at the start of the window (lookback - 1 bars ago)
    baseIntercept = linregClose - commonSlope * (_lookback - 1)

    // 3 & 4. Calculate Deviations and Find Max Deviations
    float maxPosDev = na
    float maxNegDev = na

    for i = 0 to _lookback - 1
        // Central line value at relative index 'i'
        centralLinePoint = baseIntercept + commonSlope * i

        // High price at relative index 'i'
        highPrice = high[_lookback - 1 - i]
        if not na(highPrice) and not na(centralLinePoint)
            posDeviation = highPrice - centralLinePoint
            if na(maxPosDev) or posDeviation > maxPosDev
                maxPosDev := posDeviation

        // Low price at relative index 'i'
        lowPrice = low[_lookback - 1 - i]
        if not na(lowPrice) and not na(centralLinePoint)
            negDeviation = lowPrice - centralLinePoint
            if na(maxNegDev) or negDeviation < maxNegDev
                maxNegDev := negDeviation

    // Default deviations if na
    actualMaxPosDev = na(maxPosDev) ? 0.0 : maxPosDev
    actualMaxNegDev = na(maxNegDev) ? 0.0 : maxNegDev

    // 5. Adjust Intercepts
    upperAdjustedIntercept = baseIntercept + actualMaxPosDev
    lowerAdjustedIntercept = baseIntercept + actualMaxNegDev

    // Return the common slope and adjusted intercepts
    [commonSlope, upperAdjustedIntercept, lowerAdjustedIntercept]


// --- Main ---
// Declare persistent line variables for drawing on last bar
var line upperLine = na
var line lowerLine = na

// Initialize calculation results with na
float slope = na
float upperIntercept = na
float lowerIntercept = na
float currentUpperVal = na
float currentLowerVal = na

// Perform calculations on every bar after initial lookback
if bar_index >= lookback // Need lookback bars for linreg offset 1
    // Calculate bounds for the window ending at the *current* bar
    [_slope, _upperIntercept, _lowerIntercept] = f_calculateParallelBounds(lookback)
    slope := _slope
    upperIntercept := _upperIntercept
    lowerIntercept := _lowerIntercept

    // Calculate the line values at the *current* bar (end of the window)
    // Value at relative index (lookback - 1) is intercept + slope * (lookback - 1)
    currentUpperVal := upperIntercept + slope * (lookback - 1)
    currentLowerVal := lowerIntercept + slope * (lookback - 1)

// Calculate break conditions on every bar using the current bar's line values
breakUp = not na(currentUpperVal) and close > currentUpperVal[1]
breakDown = not na(currentLowerVal) and close < currentLowerVal[1]

// Plot markers for break up
plotshape(breakUp,   style=shape.labelup,  location=location.abovebar, color=color.new(color.green, 0),  text="Break Up")

// Plot markers for break down
plotshape( breakDown,  style=shape.labeldown, location=location.belowbar, color=color.new(color.red, 0),  text="Break Down"   )

// --- Debug Plots ---
// Plot the calculated line values for every bar
plot(currentUpperVal, title="Debug Upper Val", color=color.new(color.aqua, 50), style=plot.style_line)
plot(currentLowerVal, title="Debug Lower Val", color=color.new(color.fuchsia, 50), style=plot.style_line)

// Plot the breakout conditions in a separate pane
plot(breakUp ? 1 : 0, title="BreakUp Condition", style=plot.style_histogram, color=color.green)
plot(breakDown ? 1 : 0, title="BreakDown Condition", style=plot.style_histogram, color=color.red)
// --- End Debug Plots ---

// Only draw the lines visually on the last bar
if barstate.islast and bar_index >= lookback
    // Use the slope/intercepts calculated specifically for this last bar
    x1 = bar_index - lookback + 1
    x2 = bar_index

    // y-coords for the line drawn on the last bar
    y1_upper_draw = upperIntercept // Intercept is value at start (relative index 0)
    y2_upper_draw = currentUpperVal  // Value at end (relative index lookback-1)
    y1_lower_draw = lowerIntercept
    y2_lower_draw = currentLowerVal

    // Delete old visual lines
    line.delete(upperLine[1])
    line.delete(lowerLine[1])

    // Draw new visual lines
    upperLine := line.new(x1=x1, y1=y1_upper_draw, x2=x2, y2=y2_upper_draw, color=color.blue, style=line.style_solid)
    lowerLine := line.new(x1=x1, y1=y1_lower_draw, x2=x2, y2=y2_lower_draw, color=color.red, style=line.style_solid)

// --- End Main ---

Overview

Parallel Bounding Trend Lines finds the maximum high and low deviations from the central linear regression slope to form parallel channel bounds around price action. When price closes above or below the previous bar's upper or lower channel boundary, the script plots breakout shape markers with labels on the main price chart. Traders can adjust the lookback period setting to change the calculation window size.

Details

Published by

koleharv on GitHub

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

antlrcompilerestreelangiumlanguage-serverpinescripttradingviewtranspiler

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Lookback Periodint50lookback
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Sideway Zones Live

by dearvn
257 stars

Sideway Zones Live is an overlay indicator that estimates inner and outer sideways bands from recent highs and lows with a rational quadratic kernel. It tracks swing pivots over a loopback period and counts bars that remain between the outer high and low. Afte

Indicator v5 TrendSupport & resistance
Updated 2 months ago · None stated GitHub repository of Sideway Zones Live
56 stars

This indicator calculates dynamic upper and lower trendlines from pivot highs and pivot lows across a specified bar length. Trendline slopes are derived using ATR, standard deviation, or linear regression methods. It plots trendlines and breakout signal labels

Indicator v5 TrendSupport & resistance Repaints
Updated 3 months ago · None stated GitHub repository of Trendlines With Breaks [LUX]

Reversal Breakout System

by TamaraKGreen
47 stars

This indicator detects inside candles inside larger parent candles on the price chart. It plots colored boxes around inside and parent candles, fills them with user-adjustable transparency, draws Fibonacci retracement lines and mother candle range lines, and d

Indicator v6 Candlestick patternsBreakout
Updated 2 months ago · None stated GitHub repository of Reversal Breakout System

The Mathematical Indicator calculates session bias by evaluating previous daily and 4-hour closed candles. It draws timeframe support and resistance levels using candle bodies, highlights clean trading zones, and identifies setup patterns involving consolidati

Indicator v6 TrendSupport & resistance Repaints
Updated 3 days ago · MIT GitHub repository of The Mathematical Indicator · 2-Candle Bias
34 stars

Calculates a trailing stop line based on the highest high and lowest low over a specified swing period. When the price breaks above the previous swing high or below the previous swing low, the script updates its directional state and adjusts the trailing stop

Indicator v4 TrendSupport & resistance
Updated 2 weeks ago · MIT GitHub repository of Accurate Swing Trading System
28 stars

This indicator maps Smart Money Concepts (SMC) market structure directly onto the price chart. It detects internal and swing structure breaks (BOS and CHoCH), tracks bullish and bearish order blocks, identifies fair value gaps (FVG), highlights equal highs and

Indicator v5 SMC / ICT / price actionSupport & resistance Repaints
Updated 5 weeks ago · Custom licence GitHub repository of Smart Money Concepts [LuxAlgo] Test B

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers