OPEN-SOURCE SCRIPT
SPMA Supertrend | NAL

SPMA Supertrend is a trend-following framework that replaces the conventional Supertrend center with a Shock Percentile Moving Average (SPMA).
Unlike a traditional moving average that updates on every bar, SPMA only accepts a new baseline value when the current one-bar return ranks sufficiently high relative to its recent distribution. When that percentile condition is not met, the baseline remains unchanged.
The result is an event-driven trend structure designed to respond selectively to meaningful upside price displacement rather than continuously following every movement in price.
1. Shock Percentile Moving Average
The SPMA begins by calculating the current one-bar price change and ranking that observation against its recent history:
Pine Script®
The percentile gate determines whether the underlying EMA is permitted to update.
Pine Script®
When the gate is satisfied, SPMA adopts the current EMA value. Otherwise, it preserves its previous level.
This creates a baseline whose evolution is determined by relative return significance rather than the passage of time alone.
Higher percentile settings make the model increasingly selective, while lower settings allow the baseline to update more frequently.
2. SPMA Supertrend
The SPMA is then used as the center of a conventional trailing-band Supertrend architecture.
Pine Script®
ATR defines the volatility distance around the event-driven baseline, while the trailing-band logic converts the structure into persistent bullish and bearish regimes.
This produces a hybrid model combining percentile-ranked price displacement, selective baseline updating, volatility normalization, and Supertrend state persistence.
Visuals & Interpretation
Only the active Supertrend boundary is displayed.
A gradient extends between the hidden SPMA baseline and the active Supertrend level, making the relationship between the event-driven baseline and current trend boundary visible without adding unnecessary chart clutter.
Candles are also colored according to the active regime.
Three visual presets are included:
Practical Application
SPMA Supertrend is designed for identifying directional market structure while reducing the influence of routine price movement on the underlying trend baseline.
The key distinction from a standard Supertrend is simple:
The volatility bands still respond continuously, but the underlying baseline only evolves when price movement is sufficiently significant relative to its recent distribution.
Users can independently control the baseline horizon, percentile-ranking window, percentile gate, ATR horizon, and Supertrend volatility multiplier.
Automated Alerts
Built-in alerts are included for both directional transitions:
For closed-bar confirmation, configure TradingView alerts to trigger Once Per Bar Close.
Unlike a traditional moving average that updates on every bar, SPMA only accepts a new baseline value when the current one-bar return ranks sufficiently high relative to its recent distribution. When that percentile condition is not met, the baseline remains unchanged.
The result is an event-driven trend structure designed to respond selectively to meaningful upside price displacement rather than continuously following every movement in price.
1. Shock Percentile Moving Average
The SPMA begins by calculating the current one-bar price change and ranking that observation against its recent history:
The percentile gate determines whether the underlying EMA is permitted to update.
When the gate is satisfied, SPMA adopts the current EMA value. Otherwise, it preserves its previous level.
This creates a baseline whose evolution is determined by relative return significance rather than the passage of time alone.
Higher percentile settings make the model increasingly selective, while lower settings allow the baseline to update more frequently.
2. SPMA Supertrend
The SPMA is then used as the center of a conventional trailing-band Supertrend architecture.
ATR defines the volatility distance around the event-driven baseline, while the trailing-band logic converts the structure into persistent bullish and bearish regimes.
- Bullish — price has cleared the upper trailing structure and the lower band becomes active.
- Bearish — price has cleared the lower trailing structure and the upper band becomes active.
This produces a hybrid model combining percentile-ranked price displacement, selective baseline updating, volatility normalization, and Supertrend state persistence.
Visuals & Interpretation
Only the active Supertrend boundary is displayed.
A gradient extends between the hidden SPMA baseline and the active Supertrend level, making the relationship between the event-driven baseline and current trend boundary visible without adding unnecessary chart clutter.
Candles are also colored according to the active regime.
Three visual presets are included:
- Standard
- Nordic
- Simple
Practical Application
SPMA Supertrend is designed for identifying directional market structure while reducing the influence of routine price movement on the underlying trend baseline.
The key distinction from a standard Supertrend is simple:
The volatility bands still respond continuously, but the underlying baseline only evolves when price movement is sufficiently significant relative to its recent distribution.
Users can independently control the baseline horizon, percentile-ranking window, percentile gate, ATR horizon, and Supertrend volatility multiplier.
Automated Alerts
Built-in alerts are included for both directional transitions:
- Bullish Flip — triggers when SPMA Supertrend changes from bearish to bullish.
- Bearish Flip — triggers when SPMA Supertrend changes from bullish to bearish.
For closed-bar confirmation, configure TradingView alerts to trigger Once Per Bar Close.
Açık kaynak kodlu komut dosyası
Gerçek TradingView ruhuyla, bu komut dosyasının mimarı, yatırımcıların işlevselliğini inceleyip doğrulayabilmesi için onu açık kaynaklı hale getirdi. Yazarı tebrik ederiz! Ücretsiz olarak kullanabilseniz de, kodu yeniden yayınlamanın Topluluk Kurallarımıza tabi olduğunu unutmayın.
🔵 Access our best trading & investing tools, get a 30-day free trial today: whop.com/nordic-alpha-lab/
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, alım satım veya diğer türden tavsiye veya öneriler anlamına gelmez ve teşkil etmez. Kullanım Koşulları bölümünde daha fazlasını okuyun.
Açık kaynak kodlu komut dosyası
Gerçek TradingView ruhuyla, bu komut dosyasının mimarı, yatırımcıların işlevselliğini inceleyip doğrulayabilmesi için onu açık kaynaklı hale getirdi. Yazarı tebrik ederiz! Ücretsiz olarak kullanabilseniz de, kodu yeniden yayınlamanın Topluluk Kurallarımıza tabi olduğunu unutmayın.
🔵 Access our best trading & investing tools, get a 30-day free trial today: whop.com/nordic-alpha-lab/
Feragatname
Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, alım satım veya diğer türden tavsiye veya öneriler anlamına gelmez ve teşkil etmez. Kullanım Koşulları bölümünde daha fazlasını okuyun.