PROTECTED SOURCE SCRIPT
Linear Regression Channel with Crossover Signals

Linear Regression Channel with Crossover Signals is an overlay indicator that plots a statistically derived **linear regression channel** around price and optionally generates **crossover / re-entry signals**, labels, alerts, and a small on-chart dashboard. It is designed to help visualize trend direction (via regression slope), typical price “containment” (via deviation bands), and potential breakout/mean-reversion events when price interacts with the channel.
---
## What this indicator shows
### 1) Linear Regression Midline (trend estimate)
The midline is the **linear regression value** of `close` over the selected *Regression Length*. It represents the “best fit” straight line through the last *N* bars, providing:
- **Trend direction** (via slope: rising/falling/flat)
- A dynamic reference level for midline cross signals (optional)
### 2) Upper / Lower Deviation Bands (channel)
A channel is built around the regression midline using the **standard deviation of price from the regression line**:
- Upper Band = midline + (stdDev × Deviation Multiplier)
- Lower Band = midline − (stdDev × Deviation Multiplier)
This creates a volatility-scaled envelope that adapts to how tightly or loosely price has been tracking the regression line.
### 3) Optional visual aids
You can independently toggle:
- Channel lines (upper/lower)
- Midline
- Channel fill (shaded area between bands)
- Background coloring to reflect whether price is **above / inside / below** the channel
A dashboard (optional) displays live values and context such as band values, distance from bands, current position, and trend direction.
---
## How it works (calculation overview)
### Regression calculation (manual)
The script calculates slope and intercept using a least-squares approach across the last `lengthInput` bars:
- It iterates over the window, builds sums (X, Y, XY, X²), and computes:
- **Slope** and **Intercept** of the regression line
- The midline value plotted is the regression value at the most recent point in the window.
### Standard deviation from the regression line
Instead of using standard deviation of closes directly, it measures the **deviation of each close from the regression line** and computes:
- variance = average of squared deviations
- stdDev = sqrt(variance)
This is what makes the channel represent “how far price typically strays from the trend line” over the lookback period.
---
## Signal logic (what triggers markers)
All signals are optional and controlled by the **Signal Types** toggles.
### A) Upper band cross signals (optional)
- **Upper Breakout (OB)**: price crosses **above** the Upper Band (`crossover(close, upperBand)`)
- **Upper Re-entry**: price crosses **back below** the Upper Band (`crossunder(close, upperBand)`)
These are often interpreted as “extension above channel” and “return from extension,” but interpretation depends on your style (breakout vs mean reversion).
### B) Lower band cross signals (optional)
- **Lower Breakdown (OS)**: price crosses **below** the Lower Band (`crossunder(close, lowerBand)`)
- **Lower Re-entry**: price crosses **back above** the Lower Band (`crossover(close, lowerBand)`)
### C) Midline cross signals (optional)
- **Mid Bullish**: price crosses above the regression midline
- **Mid Bearish**: price crosses below the regression midline
### D) Re-entry-to-channel signals (enabled by default)
The script tracks whether price was previously:
- **above** the channel (close > upperBand)
- **below** the channel (close < lowerBand)
- **inside** the channel
It then triggers “re-entry” when price returns inside the channel:
- **Bullish Re-entry (Bounce Support)**: price was below and crosses back above the Lower Band
- **Bearish Re-entry (Rejection Resistance)**: price was above and crosses back below the Upper Band
These are intended to highlight potential mean-reversion acceptance back into the “normal” channel range.
---
## Plot markers and labels (what you’ll see)
If enabled, the indicator can plot:
- Triangles and circles for band/midline crosses (with short text such as **OB**, **OS**, ▲, ▼)
- Diamond markers for re-entry confirmations (✓ / ✗)
- Optional text labels describing the event (e.g., “OVERBOUGHT Breakout”, “REJECTION Resistance”, etc.)
Note: this script sets high label/line limits (`max_labels_count` / `max_lines_count`) to reduce the chance of hitting platform limits when many signals occur.
---
## Alerts (optional)
When “Enable Alerts” is ON, the script can fire alerts for:
- Upper breakout / upper re-entry
- Lower breakdown / lower re-entry
- Re-entry bounce / re-entry rejection
- Midline bullish / bearish cross
Alerts are triggered **once per bar** when conditions occur. They are informational and should be validated within your trading plan.
---
## Inputs (how to tune it)
### Channel Settings
- **Regression Length**: larger = smoother and slower to react; smaller = more responsive but noisier
- **Deviation Multiplier**: larger = wider channel (fewer band touches); smaller = tighter channel (more touches)
- **Extend Forward**: present as an input, but this version does not draw forward-projected lines; it primarily affects nothing visually unless you add line objects later
### Signal Settings / Types
- Enable only the signal types you actually use to keep charts clean.
- Re-entry signals are ON by default because they depend on context (previously outside → back inside), which many traders find more meaningful than a single cross alone.
### Visual Settings
- Show/hide channel, midline, fill, background coloring
- Customize colors for upper/lower/mid and fill for readability
---
## Limitations / notes (important for public descriptions)
- This is an **indicator**, not a strategy; it does not backtest performance.
- Linear regression channels are **context-dependent**: strong trends can “ride” a band, and ranging markets can produce frequent crosses.
- Signals are based on **price crossing computed bands** and do not predict direction by themselves.
- Standard deviation bands reflect past dispersion around the regression line; volatility regime shifts can change behavior quickly.
- Always apply risk management and confirm with additional context (structure, volume, higher timeframe bias, session conditions, etc.).
---
## What this indicator shows
### 1) Linear Regression Midline (trend estimate)
The midline is the **linear regression value** of `close` over the selected *Regression Length*. It represents the “best fit” straight line through the last *N* bars, providing:
- **Trend direction** (via slope: rising/falling/flat)
- A dynamic reference level for midline cross signals (optional)
### 2) Upper / Lower Deviation Bands (channel)
A channel is built around the regression midline using the **standard deviation of price from the regression line**:
- Upper Band = midline + (stdDev × Deviation Multiplier)
- Lower Band = midline − (stdDev × Deviation Multiplier)
This creates a volatility-scaled envelope that adapts to how tightly or loosely price has been tracking the regression line.
### 3) Optional visual aids
You can independently toggle:
- Channel lines (upper/lower)
- Midline
- Channel fill (shaded area between bands)
- Background coloring to reflect whether price is **above / inside / below** the channel
A dashboard (optional) displays live values and context such as band values, distance from bands, current position, and trend direction.
---
## How it works (calculation overview)
### Regression calculation (manual)
The script calculates slope and intercept using a least-squares approach across the last `lengthInput` bars:
- It iterates over the window, builds sums (X, Y, XY, X²), and computes:
- **Slope** and **Intercept** of the regression line
- The midline value plotted is the regression value at the most recent point in the window.
### Standard deviation from the regression line
Instead of using standard deviation of closes directly, it measures the **deviation of each close from the regression line** and computes:
- variance = average of squared deviations
- stdDev = sqrt(variance)
This is what makes the channel represent “how far price typically strays from the trend line” over the lookback period.
---
## Signal logic (what triggers markers)
All signals are optional and controlled by the **Signal Types** toggles.
### A) Upper band cross signals (optional)
- **Upper Breakout (OB)**: price crosses **above** the Upper Band (`crossover(close, upperBand)`)
- **Upper Re-entry**: price crosses **back below** the Upper Band (`crossunder(close, upperBand)`)
These are often interpreted as “extension above channel” and “return from extension,” but interpretation depends on your style (breakout vs mean reversion).
### B) Lower band cross signals (optional)
- **Lower Breakdown (OS)**: price crosses **below** the Lower Band (`crossunder(close, lowerBand)`)
- **Lower Re-entry**: price crosses **back above** the Lower Band (`crossover(close, lowerBand)`)
### C) Midline cross signals (optional)
- **Mid Bullish**: price crosses above the regression midline
- **Mid Bearish**: price crosses below the regression midline
### D) Re-entry-to-channel signals (enabled by default)
The script tracks whether price was previously:
- **above** the channel (close > upperBand)
- **below** the channel (close < lowerBand)
- **inside** the channel
It then triggers “re-entry” when price returns inside the channel:
- **Bullish Re-entry (Bounce Support)**: price was below and crosses back above the Lower Band
- **Bearish Re-entry (Rejection Resistance)**: price was above and crosses back below the Upper Band
These are intended to highlight potential mean-reversion acceptance back into the “normal” channel range.
---
## Plot markers and labels (what you’ll see)
If enabled, the indicator can plot:
- Triangles and circles for band/midline crosses (with short text such as **OB**, **OS**, ▲, ▼)
- Diamond markers for re-entry confirmations (✓ / ✗)
- Optional text labels describing the event (e.g., “OVERBOUGHT Breakout”, “REJECTION Resistance”, etc.)
Note: this script sets high label/line limits (`max_labels_count` / `max_lines_count`) to reduce the chance of hitting platform limits when many signals occur.
---
## Alerts (optional)
When “Enable Alerts” is ON, the script can fire alerts for:
- Upper breakout / upper re-entry
- Lower breakdown / lower re-entry
- Re-entry bounce / re-entry rejection
- Midline bullish / bearish cross
Alerts are triggered **once per bar** when conditions occur. They are informational and should be validated within your trading plan.
---
## Inputs (how to tune it)
### Channel Settings
- **Regression Length**: larger = smoother and slower to react; smaller = more responsive but noisier
- **Deviation Multiplier**: larger = wider channel (fewer band touches); smaller = tighter channel (more touches)
- **Extend Forward**: present as an input, but this version does not draw forward-projected lines; it primarily affects nothing visually unless you add line objects later
### Signal Settings / Types
- Enable only the signal types you actually use to keep charts clean.
- Re-entry signals are ON by default because they depend on context (previously outside → back inside), which many traders find more meaningful than a single cross alone.
### Visual Settings
- Show/hide channel, midline, fill, background coloring
- Customize colors for upper/lower/mid and fill for readability
---
## Limitations / notes (important for public descriptions)
- This is an **indicator**, not a strategy; it does not backtest performance.
- Linear regression channels are **context-dependent**: strong trends can “ride” a band, and ranging markets can produce frequent crosses.
- Signals are based on **price crossing computed bands** and do not predict direction by themselves.
- Standard deviation bands reflect past dispersion around the regression line; volatility regime shifts can change behavior quickly.
- Always apply risk management and confirm with additional context (structure, volume, higher timeframe bias, session conditions, etc.).
สคริปต์ที่ได้รับการป้องกัน
สคริปต์นี้ถูกเผยแพร่เป็นแบบ closed-source อย่างไรก็ตาม คุณสามารถใช้ได้อย่างอิสระและไม่มีข้อจำกัดใดๆ – เรียนรู้เพิ่มเติมได้ที่นี่
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน
สคริปต์ที่ได้รับการป้องกัน
สคริปต์นี้ถูกเผยแพร่เป็นแบบ closed-source อย่างไรก็ตาม คุณสามารถใช้ได้อย่างอิสระและไม่มีข้อจำกัดใดๆ – เรียนรู้เพิ่มเติมได้ที่นี่
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน