OPEN-SOURCE SCRIPT

21DMA Structure Counter (EMA/SMA Option)

177
21DMA Structure Counter (EMA/SMA Option)

Overview
The 21DMA Structure Counter is an advanced technical indicator that tracks consecutive periods where price action remains above a 21-period moving average structure. This indicator helps traders identify momentum phases and potential trend exhaustion points using statistical analysis.

Key Features

Moving Average Structure
- Configurable MA Type: Choose between EMA (Exponential Moving Average) or SMA (Simple Moving Average)
- 21-Period Default: Optimized for the widely-watched 21-period moving average
- Triple MA Structure: Tracks high, close, and low moving averages for comprehensive analysis

Statistical Analysis
- Cycle Counting: Automatically counts consecutive periods above the MA structure
- Historical Data: Maintains up to 2,500 historical cycles (approximately 10 years of daily data)
- Z-Score Calculation: Provides statistical context using mean and standard deviation
- Multiple Standard Deviation Levels: Displays +1, +2, and +3 standard deviation thresholds

Visual Indicators
Color-Coded Bars:
- Gray: Below 10-year average
- Yellow: Between average and +1 standard deviation
- Orange: Between +1 and +2 standard deviations
- Red: Between +2 and +3 standard deviations
- Fuchsia: Above +3 standard deviations (extreme readings)

Breadth Integration
- Multiple Breadth Options: NDFI, NDTH, NDTW (NASDAQ breadth indicators), or VIX
- Background Shading: Visual alerts when breadth reaches extreme levels
- High/Low Thresholds: Customizable levels for breadth analysis
- Real-time Display: Current breadth value shown in data table

Smart Reset Logic
- High Below Structure Reset: Automatically resets count when daily high falls below the lowest MA
- Flexible Hold Period: Continues counting during temporary weakness as long as structure isn't violated
- Precise Entry/Exit: Strict criteria for starting cycles, flexible for maintaining them

How to Use

Trend Identification
- Rising Counts: Indicate sustained momentum above key moving average structure
- Extreme Readings: Z-scores above +2 or +3 suggest potential trend exhaustion
- Historical Context: Compare current cycles to 10-year statistical averages

Risk Management
- Breadth Confirmation: Use breadth shading to confirm market-wide strength/weakness
- Statistical Extremes: Exercise caution when readings reach +3 standard deviations
- Reset Signals: Pay attention to structure violations for potential trend changes

Multi-Timeframe Application
- Daily Charts: Primary timeframe for swing trading and position management
- Weekly/Monthly: Longer-term trend analysis
- Intraday: Shorter-term momentum assessment (adjust MA period accordingly)

Settings

Moving Average Options
- Type: EMA or SMA selection
- Period: Default 21 (customizable)
- Reset Days: Days below structure required for reset

Visual Customization
- Standard Deviation Lines: Toggle and customize colors for +1, +2, +3 SD
- Breadth Selection: Choose from NDFI, NDTH, NDTW, or VIX
- Threshold Levels: Set custom high/low breadth thresholds
- Table Styling: Customize text colors, background, and font size

Technical Notes
- Data Retention: Maintains 2,500 historical cycles for robust statistical analysis
- Real-time Updates: Calculations update with each new bar
- Breadth Integration: Uses security() function to pull external breadth data
- Performance Optimized: Efficient array management prevents memory issues

Best Practices
1. Combine with Price Action: Use alongside support/resistance and chart patterns
2. Monitor Breadth Divergences: Watch for breadth weakness during strong readings
3. Respect Statistical Extremes: Exercise caution at +2/+3 standard deviation levels
4. Context Matters: Consider overall market environment and sector rotation
5. Risk Management: Use appropriate position sizing, especially at extreme readings

Disclaimer
This indicator is for educational and informational purposes only. It should not be used as the sole basis for trading decisions. Always combine with other forms of analysis and proper risk management techniques.

Compatible with Pine Script v6 | Optimized for daily timeframes | Best used on major indices and liquid stocks

Feragatname

Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.