OPEN-SOURCE SCRIPT

Uptrick: Dynamic Z-Score Deviation

3 711
Overview
Uptrick: Dynamic Z‑Score Deviation is a trading indicator built in Pine Script that combines statistical filters and adaptive smoothing to highlight potential reversal points in price action. It combines a hybrid moving average, dual Z‑Score analysis on both price and RSI, and visual enhancements like slope‑based coloring, ATR‑based shadow bands, and dynamically scaled reversal signals.

Introduction
Statistical indicators like Z‑Scores measure how far a value deviates from its average relative to the typical variation (standard deviation). Standard deviation quantifies how dispersed a set of values is around its mean. A Z‑Score of +2 indicates a value two standard deviations above the mean, while -2 is two below. Traders use Z‑Scores to spot unusually high or low readings that may signal overbought or oversold conditions.

Moving averages smooth out price data to reveal trends. The Arnaud Legoux Moving Average (ALMA) reduces lag and noise through weighted averaging. A Zero‑Lag EMA (approximated here using a time‑shifted EMA) seeks to further minimize delay in following price. The RSI (Relative Strength Index) is a momentum oscillator that measures recent gains against losses over a set period.

ATR (Average True Range) gauges market volatility by averaging the range between high and low over a lookback period. Shadow bands built using ATR give a visual mood of volatility around a central trend line. Together, these tools inform a dynamic but statistically grounded view of market extremes.

Purpose
The main goal of this indicator is to help traders spot short‑term reversal opportunities on lower timeframes. By requiring both price and momentum (RSI) to exhibit statistically significant deviations from their norms, it filters out weak setups and focuses on higher‑probability mean‑reversion zones. Reversal signals appear when price deviates far enough from its hybrid moving average and RSI deviates similarly in the same direction. This makes it suitable for discretionary traders seeking clean entry cues in volatile environments.

Originality and Uniqueness
Uptrick: Dynamic Z‑Score Deviation distinguishes itself from standard reversal or mean‑reversion tools by combining several elements into a single framework:
  • A composite moving average (ALMA + Zero‑Lag EMA) for a smooth yet responsive baseline

  • Dual Z‑Score filters on price and RSI rather than relying on a single measure

  • Adaptive visual elements, including slope‑aware coloring, multi‑layer ATR shadows, and signal sizing based on combined Z‑Score magnitude


Most indicators focus on one aspect—price envelopes or RSI thresholds—whereas Uptrick: Dynamic Z‑Score Deviation requires both layers to align before signaling. Its visual design aids quick interpretation without overwhelming the chart.

Why these indicators were merged
Every component in Uptrick: Dynamic Z‑Score Deviation has a purpose:

• ALMA: provides a smooth moving average with reduced lag and fewer false crossovers than a simple SMA or EMA.

• Zero‑Lag EMA (ZLMA approximation): further reduces the delay relative to price by applying a time shift to EMA inputs. This keeps the composite MA closer to current price action.

• RSI and its EMA filter: RSI measures momentum. Applying an EMA filter on RSI smooths out false spikes and confirms genuine overbought or oversold momentum.

• Dual Z‑Scores: computing Z‑Scores on both the distance between price and the composite MA, and on smoothed RSI, ensures that signals only fire when both price and momentum are unusually stretched.

• ATR bands: using ATR‑based shadow layers visualizes volatility around the MA, guiding traders on potential support and resistance zones.

At the end, these pieces merge into a single indicator that detects statistically significant mean reversions while staying adaptive to real‑time volatility and momentum.

Calculations

1. Compute ALMA over the chosen MA length, offset, and sigma.

2. Approximate ZLMA by applying EMA to twice the price minus the price shifted by the MA length.

3. Calculate the composite moving average as the average of ALMA and ZLMA.

4. Compute raw RSI and smooth it with ALMA. Apply an EMA filter to raw RSI to reduce noise.

5. For both price and smoothed RSI, calculate the mean and standard deviation over the Z‑Score lookback period.

6. Compute Z‑Scores:
• z_price = (current price − composite MA mean) / standard deviation of price deviations
• z_rsi = (smoothed RSI − mean RSI) / standard deviation of RSI

7. Determine reversal conditions: both Z‑Scores exceed their thresholds in the same direction, RSI EMA is in oversold/overbought zones (below 40 or above 60), and price movement confirms directionality.

8. Compute signal strength as the sum of the absolute Z‑Scores, then classify into weak, medium, or strong.

9. Calculate ATR over the chosen period and multiply by layer multipliers to form shadow widths.

10.Derive slope over the chosen slope length and color the MA line and bars based on direction, optionally smoothing color transitions via EMA on RGB channels.


How this indicator actually works

1. The script begins by smoothing price data with ALMA and approximating a zero‑lag EMA, then averaging them for the main MA.

2. RSI is calculated, then smoothed and filtered.

3. Using a rolling window, the script computes statistical measures for both price deviations and RSI.

4. Z‑Scores tell how far current values lie from their recent norms.

5. When both Z‑Scores cross configured thresholds and momentum conditions align, reversal signals are flagged.

6. Signals are drawn with size and color reflecting strength.

7. The MA is plotted with dynamic coloring; ATR shadows are layered beneath to show volatility envelopes.

8. Bars can be colored to match MA slope, reinforcing trend context.

9. Alert conditions allow automated notifications when signals occur.

Inputs
Main Length: Main MA Length. Sets the period for ALMA and ZLMA.

RSI Length: RSI Length. Determines the lookback for momentum calculations.

Z-Score Lookback: Z‑Score Lookback. Window for mean and standard deviation computations.

Price Z-Score Threshold: Price Z‑Score Threshold. Minimum deviation required for price.

RSI Z-Score threshold: RSI Z‑Score Threshold. Minimum deviation required for momentum.

RSI EMA Filter Length: RSI EMA Filter Length. Smooths raw RSI readings.

ALMA Offset: Controls ALMA’s focal point in the window.

ALMA Sigma: Adjusts ALMA’s smoothing strength.

Show Reversal Signals : Toggle to display reversal signal markers.

Slope Sensitivity: Length for slope calculation. Higher values smooth slope changes.

Use Bar Coloring: Enables coloring of price bars based on MA slope.

Show MA Shadow: Toggle for ATR‑based shadow bands.

Shadow Layer Count: Number of shadow layers (1–4).

Base Shadow ATR Multiplier: Multiplier for ATR when sizing the first band.

Smooth Color Transitions (boolean): Smooths RGB transitions for line and shadows, if enabled.

ATR Length for Shadow: ATR Period for computing volatility bands.

Use Dynamic Signal Size: Toggles dynamic scaling of reversal symbols.

Features
Moving average smoothing: a hybrid of ALMA and Zero‑Lag EMA that balances responsiveness and noise reduction.

Slope coloring: MA line and optionally price bars change color based on trend direction; color transitions can be smoothed for visual continuity.

ATR shadow layers: translucent bands around the MA show volatility envelopes; up to four concentric layers help gauge distance from normal price swings.

snapshot

Dual Z‑Score filters: price and momentum must both deviate beyond thresholds to trigger signals, reducing false positives.

Dynamic signal sizing: reversal markers scale in size based on the combined Z‑Score magnitude, making stronger signals more prominent.

snapshot

Adaptive visuals: optional smoothing of color channels creates gradient effects on lines and fills for a polished look.

Alert conditions: built‑in buy and sell alerts notify traders when reversal setups emerge.

Conclusion
Uptrick: Dynamic Z‑Score Deviation delivers a structured way to identify short‑term reversal opportunities by fusing statistical rigor with adaptive smoothing and clear visual cues. It guides traders through multiple confirmation layers—hybrid moving average, dual Z‑Score analysis, momentum filtering, and volatility envelopes—while keeping the chart clean and informative.

Disclaimer
This indicator is provided for informational and educational purposes only and does not constitute financial advice. Trading carries risk and may not be suitable for all participants. Past performance is not indicative of future results. Always do your own analysis and risk management before making trading decisions.


Disclaimer

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.