OPEN-SOURCE SCRIPT
CNN Fear and Greed Strategy

Adaptation of the CNN Fear and Greed Index Indicator (Original by EdgeTools)

The following changes have been implemented:
CNN Fear & Greed Backtest Strategy (Adapted)
This script is an adaptation of the popular CNN Fear & Greed Index, originally created by EdgeTools, with significant modifications to optimize it for long-term backtesting on the TradingView platform.
The core function of the Fear & Greed Index is to measure the current emotional state of the stock market, ranging from 0 (Extreme Fear) to 100 (Extreme Greed). It operates on the principle that excessive fear drives prices too low (a potential buying opportunity), and excessive greed drives them too high (a potential selling opportunity).
Key Components of the Index (7 Factors)
The composite index is calculated as a weighted average of seven market indicators, each normalized to a score between 0 and 100:
Market Momentum: S&P 500's current level vs. its 125-day Moving Average.
Stock Price Strength: Stocks hitting 52-week highs vs. those hitting 52-week lows.
Stock Price Breadth: Measured by the McClellan Volume Summation Index (or similar volume/breadth metric).
Put/Call Ratio: The relationship between volume of put options (bearish bets) and call options (bullish bets).
Market Volatility: The CBOE VIX Index relative to its 50-day Moving Average.
Safe Haven Demand: The relative performance of stocks (S&P 500) vs. bonds.
Junk Bond Demand: The spread between high-yield (junk) bonds and U.S. Treasury yields.
Critical Adaptations for Backtesting
To improve the index's utility for quantitative analysis, the following changes were made:
Long-Term Normalization: The original normalization method (ta.stdev over a short LENGTH) has been replaced or adjusted to use longer historical data. This change ensures the index generates consistent and comparable sentiment scores across decades of market history, which is crucial for reliable backtesting results.
Updated Data Sources: Specific ticker requests for the Put/Call Ratio and Bond components (Safe Haven and Junk Bond Demand) have been updated to use the most reliable and long-running data available on TradingView, reducing data gaps and improving chart continuity.
Simplified Visuals: The chart display is streamlined, focusing only on the final Fear & Greed Index line and key threshold levels (25, 50, 75) for quick visual assessment.
Integrated Trading Strategy
This script also includes a simple, rules-based strategy designed to test the counter-trend philosophy of the index:
Entry Logic (Long Position): A long position is initiated when the market shows increasing fear, specifically when the index score is less than or equal to the configurable FEAR_LEVEL (default 25) and the index crosses above its own short-term 5-period Simple Moving Average (SMA). This crossover acts as a confirmation that sentiment may be starting to turn around from peak fear.
Exit Logic (Time-Based): All positions are subject to a time-based exit after holding for 252 trading days (approximately one year). This fixed holding period aims to capture the typical duration of a cyclical market recovery following a major panic event.

The following changes have been implemented:
- Put/Call Ratio Data Source: The data source for the Put/Call Ratio has been updated.
- Bond Data Source: The data sources for the bond components (Safe Haven Demand and Junk Bond Demand) have been updated.
- Normalization Adjustment: The normalization method has been adjusted to allow the CNN Fear and Greed Index to display over a longer historical period, optimizing it for backtesting purposes.
- Style Modification: The display style has been modified for a simpler and cleaner appearance.
- Strategy Logic Addition: Added a new strategy entry condition: index >= 25 AND index crosses over its 5-period Simple Moving Average (SMA), and a corresponding exit condition of holding the position for 252 bars (days).
CNN Fear & Greed Backtest Strategy (Adapted)
This script is an adaptation of the popular CNN Fear & Greed Index, originally created by EdgeTools, with significant modifications to optimize it for long-term backtesting on the TradingView platform.
The core function of the Fear & Greed Index is to measure the current emotional state of the stock market, ranging from 0 (Extreme Fear) to 100 (Extreme Greed). It operates on the principle that excessive fear drives prices too low (a potential buying opportunity), and excessive greed drives them too high (a potential selling opportunity).
Key Components of the Index (7 Factors)
The composite index is calculated as a weighted average of seven market indicators, each normalized to a score between 0 and 100:
Market Momentum: S&P 500's current level vs. its 125-day Moving Average.
Stock Price Strength: Stocks hitting 52-week highs vs. those hitting 52-week lows.
Stock Price Breadth: Measured by the McClellan Volume Summation Index (or similar volume/breadth metric).
Put/Call Ratio: The relationship between volume of put options (bearish bets) and call options (bullish bets).
Market Volatility: The CBOE VIX Index relative to its 50-day Moving Average.
Safe Haven Demand: The relative performance of stocks (S&P 500) vs. bonds.
Junk Bond Demand: The spread between high-yield (junk) bonds and U.S. Treasury yields.
Critical Adaptations for Backtesting
To improve the index's utility for quantitative analysis, the following changes were made:
Long-Term Normalization: The original normalization method (ta.stdev over a short LENGTH) has been replaced or adjusted to use longer historical data. This change ensures the index generates consistent and comparable sentiment scores across decades of market history, which is crucial for reliable backtesting results.
Updated Data Sources: Specific ticker requests for the Put/Call Ratio and Bond components (Safe Haven and Junk Bond Demand) have been updated to use the most reliable and long-running data available on TradingView, reducing data gaps and improving chart continuity.
Simplified Visuals: The chart display is streamlined, focusing only on the final Fear & Greed Index line and key threshold levels (25, 50, 75) for quick visual assessment.
Integrated Trading Strategy
This script also includes a simple, rules-based strategy designed to test the counter-trend philosophy of the index:
Entry Logic (Long Position): A long position is initiated when the market shows increasing fear, specifically when the index score is less than or equal to the configurable FEAR_LEVEL (default 25) and the index crosses above its own short-term 5-period Simple Moving Average (SMA). This crossover acts as a confirmation that sentiment may be starting to turn around from peak fear.
Exit Logic (Time-Based): All positions are subject to a time-based exit after holding for 252 trading days (approximately one year). This fixed holding period aims to capture the typical duration of a cyclical market recovery following a major panic event.
Script de código abierto
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Exención de responsabilidad
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Script de código abierto
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Exención de responsabilidad
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.