The Magic Trend line in your script is derived using the Commodity Channel Index (CCI), along with adjustments based on the Average True Range (ATR) to create a dynamic trend-following indicator. The key idea behind this is to provide a smoothed trend line that responds to both market volatility (using ATR) and the underlying price action (via CCI).
Here’s a step-by-step breakdown of how the Magic Trend line is calculated:
1. CCI Calculation The Commodity Channel Index (CCI) is a momentum-based indicator that measures the deviation of the current price from its average over a specified period. In this script:
The CCI period is set to 20 bars by default, controlled by the lengthCCI input.
The CCI formula is:
𝐶 𝐶 𝐼 = Close − SMA(Close, lengthCCI) 0.015 × Mean Deviation CCI= 0.015×Mean Deviation Close−SMA(Close, lengthCCI)
where the Mean Deviation is the average of the absolute differences between the close price and the SMA of the close.
2. ATR-Based Trend Bands The Average True Range (ATR) is a measure of volatility, and it's used here to define dynamic upper and lower trend bands. These bands adjust based on the volatility of the market, providing a flexible trend filter.
The ATR period is set to 5 bars by default, controlled by the AP input. The ATR multiplier (coeff) is set to 1.0 by default. Using the ATR, two trend bands are calculated:
Upper Trend Band (upT): low - ATR * coeff Lower Trend Band (downT): high + ATR * coeff These bands help to determine whether the market is in an uptrend or a downtrend, and they dynamically adjust to market conditions.
3. Magic Trend Line Adjustment Once the CCI is calculated and the ATR-based bands are defined, the Magic Trend line (MagicTrend) is determined as follows:
If the CCI is greater than or equal to 0 (indicating a bullish market condition), the Magic Trend is pushed upwards towards the upper trend band, ensuring the trend does not dip below the upward ATR-based boundary. The line is adjusted to be the maximum of the previous Magic Trend value and the upper trend boundary.
MagicTrend = max ( upT , MagicTrend[1] ) MagicTrend=max(upT,MagicTrend[1]) If the CCI is less than 0 (indicating a bearish market condition), the Magic Trend is pushed downwards towards the lower trend band, ensuring the trend does not rise above the downward ATR-based boundary. The line is adjusted to be the minimum of the previous Magic Trend value and the lower trend boundary.
MagicTrend = min ( downT , MagicTrend[1] ) MagicTrend=min(downT,MagicTrend[1]) This approach smooths the CCI values while constraining them within dynamically adjusted trend bands based on market volatility.
4. Trend Coloring The Magic Trend line is colored blue when the trend is bullish (CCI ≥ 0). It is colored red when the trend is bearish (CCI < 0). In Summary: The Magic Trend is essentially a dynamic trend line that adapts to both price momentum (via CCI) and market volatility (via ATR). It is designed to smooth out the market's fluctuations and provide a clear visual indication of the trend direction. The line itself is adjusted to never exceed certain volatility boundaries defined by the ATR, ensuring it remains in sync with the market's natural fluctuations. The trend direction is color-coded for easy interpretation: blue for bullish trends and red for bearish trends.
Dalam semangat sebenar TradingView, penulis telah menerbitkan kod Pine ini sebagai sumber terbuka supaya pedagang dapat memahami dan mengesahkannya. Sorakan kepada penulis! Anda boleh menggunakan perpustakaan ini secara percuma, tetapi penggunaan semula kod dalam penerbitan ini adalah dikawal oleh Peraturan dalaman. Anda boleh menyukainya untuk menggunakannya pada carta.
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.