OPEN-SOURCE SCRIPT
Cập nhật CAP Channel Trading (Auto-Deviation & Analysis Dashboard)

This indicator is a highly precise and comprehensive replica of the well-known "MeetAlgo CAP Channel Trading" indicator, originally developed for MetaTrader 4 and 5. It has been built from the ground up in Pine Script v6 and expanded with exclusive TradingView features (such as Auto-Deviation calculation and dynamic themes).
The indicator is based on the "Advanced Envelope Theory." This theory assumes that market prices naturally oscillate within dynamically calculated boundaries (envelopes). If the price breaks out of these boundaries or touches them, it signals a statistical anomaly and provides an excellent opportunity for reversal trades.
Unlike classic Bollinger Bands, which often lag behind volatility, this system reacts extremely sensitively to current fluctuations and keeps the channel width much more constant.

🧠 How does the indicator work?
The Center Line (TMA): The heart of the channel is a double-smoothed Triangular Moving Average. This filters out short-term market noise and forms a static center of gravity for the price.
The Outer Bands (ATR): The channel boundaries are calculated using the Average True Range (ATR) as a proxy for true market volatility.
Auto-Deviation (Exclusive): Since highly volatile instruments (like XAUUSD on the 5M chart) have more extreme outliers than quiet Forex pairs, this indicator features an Auto-Deviation logic. It analyzes the last 200 candles and calculates the 85th percentile of volatility to stretch the channel exactly enough to capture normal price action, while immediately providing razor-sharp signals during real breakouts.
✨ Main Features & Visualization (See Screenshots)
Dual-Color Smart Bands: The channel visually displays the market state. If the price remains safely within the channel, the bands are red with a yellow edge. If the price touches the outer boundary and enters the reversal zone (Overbought/Oversold), the bands instantly switch to black with a green edge.
Precise Entry Signals: As soon as extreme zones are reached, the indicator plots colored arrows (including the exact entry price as text) directly above or below the corresponding candle. The text color automatically adapts to your TradingView theme (Light/Dark).
Live Analysis Dashboard: In the top right corner of the chart, there is an info table that automatically simulates trades in the background and evaluates how profitable the signals currently are (including Win-Rate, accumulated Points/Pips, and the Live Spread).
⚙️ Settings (Parameters)
1. Auto-Calculate Optimal Deviation
(Default: Enabled) Allows the indicator to independently calculate the optimal channel width (Deviations) for the currently open instrument and selected timeframe. Ideal for dynamic markets like Gold.
2. Auto-Dev Percentile (Sensitivity)
(Default: 85) Controls the sensitivity of the automatic calculation. A lower value (e.g., 80) ignores more historical spikes, makes the channel tighter, and generates signals more frequently. A higher value (e.g., 95) makes the channel wider and filters signals more strictly.
3. Auto-Dev Smoothing Length
(Default: 14) Smooths the dynamically calculated channel width using an Exponential Moving Average (EMA). This prevents the channel from becoming erratic during a sudden, unnatural price spike.
4. Static Deviations
(Default: 2.0) The manual multiplier for the distance of the bands to the center line. This value is only used if the "Auto-Calculate" function is disabled.
5. Trade Exit Level
Determines at which point the dashboard considers a trade "closed" to calculate the Win-Rate.
Middle Line (Default): A trade is opened at the outer band and closed exactly at the TMA center line.
Opposite Band: A trade is only closed when the price has traveled completely through the channel and touches the opposite band.
6. Show Analysis Report
Shows or hides the performance dashboard (Info-Box) in the top right corner of the chart.
7. Signal Bar (Notification)
ClosedBar (Default): Signals and dashboard evaluations are only confirmed when the current candle has completely closed. This prevents "repainting" (arrows disappearing later).
LiveBar: Evaluates every tick in real-time. Can enable faster entries but carries the risk of false signals if the price pulls back before the candle closes.
The indicator is based on the "Advanced Envelope Theory." This theory assumes that market prices naturally oscillate within dynamically calculated boundaries (envelopes). If the price breaks out of these boundaries or touches them, it signals a statistical anomaly and provides an excellent opportunity for reversal trades.
Unlike classic Bollinger Bands, which often lag behind volatility, this system reacts extremely sensitively to current fluctuations and keeps the channel width much more constant.
🧠 How does the indicator work?
The Center Line (TMA): The heart of the channel is a double-smoothed Triangular Moving Average. This filters out short-term market noise and forms a static center of gravity for the price.
The Outer Bands (ATR): The channel boundaries are calculated using the Average True Range (ATR) as a proxy for true market volatility.
Auto-Deviation (Exclusive): Since highly volatile instruments (like XAUUSD on the 5M chart) have more extreme outliers than quiet Forex pairs, this indicator features an Auto-Deviation logic. It analyzes the last 200 candles and calculates the 85th percentile of volatility to stretch the channel exactly enough to capture normal price action, while immediately providing razor-sharp signals during real breakouts.
✨ Main Features & Visualization (See Screenshots)
Dual-Color Smart Bands: The channel visually displays the market state. If the price remains safely within the channel, the bands are red with a yellow edge. If the price touches the outer boundary and enters the reversal zone (Overbought/Oversold), the bands instantly switch to black with a green edge.
Precise Entry Signals: As soon as extreme zones are reached, the indicator plots colored arrows (including the exact entry price as text) directly above or below the corresponding candle. The text color automatically adapts to your TradingView theme (Light/Dark).
Live Analysis Dashboard: In the top right corner of the chart, there is an info table that automatically simulates trades in the background and evaluates how profitable the signals currently are (including Win-Rate, accumulated Points/Pips, and the Live Spread).
⚙️ Settings (Parameters)
1. Auto-Calculate Optimal Deviation
(Default: Enabled) Allows the indicator to independently calculate the optimal channel width (Deviations) for the currently open instrument and selected timeframe. Ideal for dynamic markets like Gold.
2. Auto-Dev Percentile (Sensitivity)
(Default: 85) Controls the sensitivity of the automatic calculation. A lower value (e.g., 80) ignores more historical spikes, makes the channel tighter, and generates signals more frequently. A higher value (e.g., 95) makes the channel wider and filters signals more strictly.
3. Auto-Dev Smoothing Length
(Default: 14) Smooths the dynamically calculated channel width using an Exponential Moving Average (EMA). This prevents the channel from becoming erratic during a sudden, unnatural price spike.
4. Static Deviations
(Default: 2.0) The manual multiplier for the distance of the bands to the center line. This value is only used if the "Auto-Calculate" function is disabled.
5. Trade Exit Level
Determines at which point the dashboard considers a trade "closed" to calculate the Win-Rate.
Middle Line (Default): A trade is opened at the outer band and closed exactly at the TMA center line.
Opposite Band: A trade is only closed when the price has traveled completely through the channel and touches the opposite band.
6. Show Analysis Report
Shows or hides the performance dashboard (Info-Box) in the top right corner of the chart.
7. Signal Bar (Notification)
ClosedBar (Default): Signals and dashboard evaluations are only confirmed when the current candle has completely closed. This prevents "repainting" (arrows disappearing later).
LiveBar: Evaluates every tick in real-time. Can enable faster entries but carries the risk of false signals if the price pulls back before the candle closes.
Phát hành các Ghi chú
Authentic Visuals: Accurately replicated the chart aesthetics from the original version, including the dual-color bands (Yellow/Red for normal, Green/Black for active) and the highly visible Blue (Buy) and Pink (Sell) entry arrows seen in your charts.Analysis Dashboard: Integrated the on-chart report panel to track Win/Loss percentages and accumulated "Green Points" exactly as displayed in the original interface.
Smart Auto-Deviation: Upgraded legacy static parameters like "Minimum Bar Redraw" and "Signal Bar" with a dynamic, auto-adjusting volatility multiplier optimized for highly volatile assets like Gold (XAUUSD).
Phát hành các Ghi chú
The copyright notice has been updated to reflect the company's new name.Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.