PROTECTED SOURCE SCRIPT

Adaptive Moving Average

30
This is a sophisticated Adaptive Moving Average (AMA) indicator written in Pine Script v6 for TradingView. It's designed to automatically adjust its smoothing period based on market conditions, becoming more responsive in trending markets and more stable during choppy/sideways periods.
Core Concept & Philosophy
The fundamental idea is that traditional moving averages use fixed periods, which creates a trade-off: short periods are responsive but noisy, while long periods are smooth but lag significantly. This AMA solves that by dynamically adjusting its length based on real-time market analysis.
How the Adaptive Mechanism Works
1. Multi-Indicator Market Analysis
The script analyzes market conditions using 11 different technical indicators, each measuring different aspects of market behavior:
Trend Strength Indicators:

RSI (Relative Strength Index): Measures momentum and overbought/oversold conditions
MACD: Detects trend changes and momentum shifts
ADX (Average Directional Index): Quantifies trend strength
Aroon Oscillator: Identifies trend direction and strength

Volatility Indicators:

Bollinger Bands Width: Measures price volatility expansion/contraction
Choppiness Index: Determines if market is trending or ranging
Rate of Change (ROC): Measures price momentum
Andean Oscillator: A volatility-based trend indicator

Market Flow Indicators:

Money Flow Index (MFI): Volume-weighted RSI
Chande Momentum Oscillator: Alternative momentum measure

Price Reference Points:

TWAP (Time-Weighted Average Price): Daily volume-weighted benchmark
VWAP (Volume-Weighted Average Price): Intraday volume benchmark
TRAMA (Triangular Moving Average): Adaptive trend-following average




2. Normalization Process
Each indicator is normalized to a 0-1 scale to ensure equal weighting:

// Example: RSI normalized from 30-70 range to 0-1
rsi_norm = (rsi - 30) / 40

// Price deviation as percentage
vwap_dev = math.abs(src - vwap) / src


This prevents any single indicator from dominating the adaptive calculation.
3. Composite Scoring System
The script creates three key scores:

Volatility Score: Average of all 11 normalized indicators
Trend Strength: Combination of ADX, Aroon, and MACD
Momentum: Blend of RSI, MFI, and Chande Momentum

4. Adaptive Length Calculation
The final adaptive factor combines these scores:

adaptive_factor = (volatility_score + volatility + (1 - trend_strength)) / 3

Key Logic:

High volatility + weak trend → Higher adaptive_factor → Longer MA period (more smoothing)
Low volatility + strong trend → Lower adaptive_factor → Shorter MA period (more responsive)

The adaptive length is then calculated:

adaptive_length = min_length + (max_length - min_length) * adaptive_factor


Market Condition Responses
Trending Markets

Strong directional movement detected by ADX and Aroon
Low choppiness and clear MACD signals
Result: Shorter MA period for quick trend following

Choppy/Sideways Markets

High choppiness index values
Conflicting signals from trend indicators
Wide Bollinger Bands indicating volatility
Result: Longer MA period to filter out noise

Volatile Markets

Large price deviations from VWAP/TWAP
High ROC and Andean oscillator readings
Result: Increased smoothing to avoid false signals

Advanced Smoothing Options

After calculating the base adaptive MA, users can apply additional smoothing filters:

SMA/EMA/RMA/WMA: Standard moving averages
ALMA (Arnaud Legoux MA): Low-lag, customizable smoothing
Hull MA: Reduces lag while maintaining smoothness
T3: Triple exponential smoothing for ultra-smooth results

Practical Implementation Benefits
1. Reduced Whipsaws

In choppy markets, the longer periods prevent false breakout signals that plague fixed-period MAs.
2. Faster Trend Recognition

In clear trends, shorter periods allow quicker entry/exit signals without excessive lag.
3. Market Regime Adaptation

The indicator automatically adjusts to changing market conditions without manual intervention.
4. Multi-Timeframe Relevance

Works across different timeframes as it adapts to the specific volatility and trending characteristics of each period.
Technical Robustness

The script includes several protective measures:

NaN Protection: Handles missing data gracefully
Bounds Checking: Ensures adaptive_factor stays between 0-1
Manual ADX Implementation: Avoids built-in function limitations
Proper Variable Initialization: Uses var declarations for stateful calculations

Usage Strategy

This AMA is particularly effective for:

Trend Following: Provides cleaner trend signals than fixed MAs
Support/Resistance: Dynamic levels that adapt to market volatility
Entry/Exit Timing: Reduces lag in trending markets, filters noise in ranging markets
Multi-Asset Trading: Automatically adjusts to different asset volatility profiles

The indicator essentially acts as an intelligent moving average that "thinks" about current market conditions and adjusts its behavior accordingly, making it a powerful tool for both systematic and discretionary trading approaches.



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.