OPEN-SOURCE SCRIPT
Aggiornato

Quantitative Linear Regression Channel [Dual Fill + MTF]

1 469
Abstract / Description:
This is a significantly upgraded v6 version of the classic Linear Regression Channel originally created by LonesomeTheBlue.
Key Improvements over the original:

Updated to Pine Script v6 with modern syntax and better performance.
Persistent object pool for lines and labels – eliminates object leakage and improves rendering speed.
Optional dual-zone fill (Upper and Lower) with fully customizable colors for clearer visual separation.
Broken channel visualization that freezes the previous channel in blue dotted style instead of deleting it immediately.
Dynamic MTF Trend Table (5m to 1D) showing slope direction with arrows and color coding. Table position is user-configurable (top_left, top_right, etc.).
Support for calculating the channel on a higher timeframe (MTF mode) while displaying it cleanly on the current chart.
Cleaner code structure, grouped inputs, improved alerts, and overall more stable behavior across all timeframes.

The core regression calculation and broken channel logic remain faithful to the original concept, while adding modern institutional-grade features for better usability and performance.
Credits:
Base concept and original code by © LonesomeTheBlue (2020).
v6 architecture, persistent pool, dual fill, MTF table and optimizations by datapro (2026).
Recommended use: Works on any timeframe. Best results when used with the MTF table for confluence.
Note di rilascio
// ─────────────────────────────────────────────────────────────────────────────
// OVERVIEW
// ─────────────────────────────────────────────────────────────────────────────
This indicator is a hardened, professional-grade upgrade of the classic Linear Regression Channel, fully migrated to Pine Script™ v6. It introduces a Multi-Timeframe (MTF) architecture, a persistent rendering engine for historical accuracy, and dynamic adaptation for irregular timeframes (Monthly, Quarterly, Yearly).

It is designed for traders who require precise statistical channels without repainting issues, offering both a visual trend context and a quantitative MTF dashboard.

// ─────────────────────────────────────────────────────────────────────────────
// ORIGINALITY & CREDITS
// ─────────────────────────────────────────────────────────────────────────────
This script is a significant upgrade based on the foundational Linear Regression Channel logic developed by © LonesomeTheBlue.

**Original Script:**
Linear Regression Channel


While the core mathematical concepts (Linear Regression and Standard Deviation) are classic, this publication introduces significant structural improvements:
1. **Pine v6 Architecture**: Complete migration to v6 standards, including strict typing and syntax compliance.
2. **MTF Transport Layer**: A clean, encapsulated function system to transport channel data across timeframes without repainting (`lookahead=off`).
3. **Irregular Timeframe Support**: Custom logic to handle variable-duration timeframes (M, 3M, Y) which standard time conversions often mishandle.
4. **Persistent Rendering Engine**: Optimized line management using object pooling to maintain clean charts and improve performance.
5. **Enhanced Visuals**: Optional dual-zone fill and a frozen "Broken Channel" visualization for historical context.

// ─────────────────────────────────────────────────────────────────────────────
// HOW IT WORKS
// ─────────────────────────────────────────────────────────────────────────────
The indicator calculates the Linear Regression Line (Best Fit Line) and the Standard Deviation channels above and below it.

• **The Center Line**: Represents the fair value linear regression of the selected length.
• **Channel Bands**: Plotted at user-defined standard deviation multiples (default 2.0).
• **Trend Detection**: The slope of the regression line determines the trend direction (Up/Down). The color dynamically updates based on slope acceleration.

// ─────────────────────────────────────────────────────────────────────────────
// KEY FEATURES
// ─────────────────────────────────────────────────────────────────────────────
🔹 **MTF Trend Table**: A compact dashboard showing the current trend slope direction across 5 timeframes (5m, 15m, 1h, 4h, 1D).
🔹 **Broken Channel Detection**: Visual markers when price forcefully exits the channel bounds, signaling potential volatility expansion.
🔹 **Dual Fill Zones**: Customizable color fills for the upper (bullish) and lower (bearish) zones to enhance visual clarity.
🔹 **Fibonacci Levels**: Optional retracement levels (0.236, 0.382, 0.618, 0.786) plotted within the channel range.

**Example: Analyzing Bearish Alignment**
The MTF Table helps identify confluence. Below is an example interpretation of a bearish setup:

┌──────┬───┐
│ TF │ Direction│
├──────┼───┤
│ 5m │ ⇓ │ ← Red background
│ 15m │ ⇘ │ ← Red
│ 1h │ ⇓ │ ← Red
│ 4h │ ⇗ │ ← Lime (caution: short-term pullback)
│ 1D │ ⇓ │ ← Red
└──────┴───┘

**Interpretation Logic:**
✅ Higher timeframe trend (1h/1D) is bearish, suggesting a preference for short positions.
⚠️ 4h showing ⇗ indicates a possible pullback; patience is required until lower timeframes reconfirm.
✅ **Potential Signal**: A price rejection at the upper band on a lower timeframe could serve as an entry trigger in the direction of the dominant trend.

// ─────────────────────────────────────────────────────────────────────────────
// SETTINGS
// ─────────────────────────────────────────────────────────────────────────────
• **Source**: Price source for calculation (Default: Close).
• **Length**: The lookback period for the regression calculation.
• **Channel Calculation TF**: Calculate the channel on a different timeframe (e.g., Daily channel on 1H chart). Empty defaults to Chart TF.
• **Deviation**: Multiplier for the standard deviation bands.

// ─────────────────────────────────────────────────────────────────────────────
// DISCLAIMER
// ─────────────────────────────────────────────────────────────────────────────
This script is provided for educational and technical analysis purposes only. It does not constitute financial advice or a recommendation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading involves substantial risk of loss and is not suitable for every investor.
Note di rilascio
Changelog:

Dynamic Source Consistency (Critical Fix): Resolved a logic mismatch where alerts and the MTF table were hardcoded to close. They now strictly evaluate the user-selected source input (e.g., hl2, ohlc4), ensuring signals perfectly match the visual channel calculation.
Pine Script™ v6 Architecture: Full migration to v6 standards, featuring strict typing, optimized tuple returns, and syntax compliance for maximum stability and future-proofing.
Performance Optimization: Implemented a persistent object pool for lines and labels, eliminating memory leaks and significantly improving rendering speed on historical charts.
Irregular Timeframe Support: Enhanced time geometry logic to accurately calculate channel start points on variable-duration timeframes (Monthly, Quarterly, Yearly).

Declinazione di responsabilità

Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.