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.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
🔵 Access our best trading & investing tools, get a 30-day free trial today: whop.com/nordic-alpha-lab/
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
🔵 Access our best trading & investing tools, get a 30-day free trial today: whop.com/nordic-alpha-lab/
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.