OPEN-SOURCE SCRIPT

Statistical Zone Engine [JOAT]

1 310
Statistical Zone Engine [JOAT]

Introduction

Statistical Zone Engine [JOAT] is an open-source overlay indicator that builds pivot-cluster support and resistance zones with walk-forward statistical scoring. Each zone is backed by a full expected value computation: the indicator counts historical touches and bounces from the zone's price range over a configurable lookback, computes a win rate, and derives an EV score in units of R. Zones are tiered into four strength categories — Weak, Moderate, Strong, and Institutional — based on their live touch count, with border thickness and fill opacity scaling proportionally to the EV and tier. Labels display R:R, win rate, EV, and touch count, all updated live each bar.

The core problem this indicator solves is that conventional support and resistance drawing tools are entirely qualitative — the trader decides what is significant by eye. The SZE replaces that subjective judgment with a quantitative framework: zone strength is computed from actual price behavior over the lookback window, not from the visual prominence of the swing. A zone that has been tested eight times with seven bounces carries an objectively different statistical weight from one that was tested twice with one bounce, and the SZE communicates that difference through its tier system, border rendering, and live EV label. Cluster merging prevents adjacent pivots at nearly the same price from spawning overlapping zones that would misrepresent true strength.

snapshot

Core Concepts

1. Pivot Cluster Zones

The indicator uses ta.pivothigh and ta.pivotlow with a configurable swing length. When a new pivot high is confirmed and no existing resistance zone is within ATR * clusterTol of the pivot price, a new zone is created. The cluster merge check prevents nearby pivots from generating duplicate zones at the same structural level — if a zone already exists within the tolerance radius, no new zone is spawned. This means zones represent genuinely distinct price levels, not just the most recent pivot above an existing zone.

2. Walk-Forward Expected Value Computation

For each new zone, the indicator scans the prior lookback bars and counts every bar where the high-low range overlapped with the zone. For each touch, it checks whether the close exited the far side of the zone — if so, it counts as a bounce. Win rate = bounces / touches. EV = winRate * tpRR - (1 - winRate) * slRR. A positive EV means the zone has historically resolved in the bounce direction more often than not, weighted by the configured R:R ratio.

3. Four-Tier Strength System

Zone tier is determined by live touch count:
  • Weak: 1-2 touches — thin border (width 1), low opacity fill
  • Moderate: 3 touches — medium border (width 1), moderate opacity fill
  • Strong: 4-5 touches — thicker border (width 2), more opaque fill
  • Institutional: 6+ touches — widest border (width 3), most opaque fill


Both the border width and the border transparency scale with tier, producing a visual system where the most historically significant zones dominate the chart. The fill opacity also scales with EV — zones with positive EV are more opaque, zones with negative EV are more transparent.

4. Live Label Updates

Each zone carries a label at its right edge displaying: type (RES/SUP), tier name, touch count, win rate percentage, and EV in R units. The label is recalculated and updated every bar when price is inside the zone, ensuring the statistics reflect current behavior. The label text color also scales with tier — more significant zones use brighter text.

5. Sweep Detection

When price closes fully through a zone boundary — above the top for resistance, below the bottom for support — the zone is marked as mitigated. If volume exceeds 1.4x the SMA(20) at the mitigation bar, a BREAK label fires above or below the zone. The total sweep count accumulates in the dashboard. After a break, zone fill fades to near-transparent, clearly communicating that the level has been closed through.

Features

  • Pivot-Cluster Zone Detection: Swing-pivot based zone creation with ATR-cluster merge deduplication — nearby pivots do not spawn overlapping zones
  • Walk-Forward EV Computation: Historical touch/bounce counting over configurable lookback produces win rate and R-unit EV scores for each zone
  • Four-Tier Strength System: Weak / Moderate / Strong / Institutional tiers based on touch count — border width and opacity scale with tier
  • EV-Scaled Fill Opacity: Positive EV zones are more opaque, negative EV zones are more transparent — fill intensity communicates statistical quality
  • Live Label Updates: Type, tier, touch count, win rate %, and EV in R units update every bar when price is inside the zone
  • Sweep Detection with Volume Filter: BREAK label fires on zone close-through when volume exceeds 1.4x SMA(20)
  • Post-Break Zone Fade: Broken zones fade visually, clearly delineating active versus mitigated levels
  • Proximity Markers: Diamond plotchar fires when price first enters a zone neighborhood
  • Min Touches Filter: Only zones with at least the configured minimum historical touches are displayed, eliminating freshly-formed single-touch zones
  • Zone Trim Management: Oldest zones are removed when arrays exceed the maximum zone count, keeping memory bounded
  • 9-Row Dashboard: Active resistance and support zone counts, near-zone states, total sweep count, TP and SL R:R ratios, ATR
  • 4 Alertconditions: Zone entry for resistance and support, new zone creation for both sides


Input Parameters

Zone Detection:
  • Swing Length: Pivot confirmation lookback period — higher values detect fewer, more significant pivots (default 10)
  • Cluster ATR Tolerance: Pivots within ATR * this of an existing zone are merged rather than spawning a new zone (default 0.4)
  • Zone ATR Width: Half the zone height as an ATR multiple — controls vertical thickness (default 0.35)
  • Max Active Zones: Maximum concurrent zones per direction before oldest are trimmed (default 12)
  • Min Touches To Show: Minimum historical touches required to display a zone (default 2)


Statistics:
  • EV Lookback (bars): Historical bar window for touch/bounce counting (default 200)
  • TP R:R Ratio: Take-profit distance in R units used for EV calculation (default 2.0)
  • SL R:R Ratio: Stop-loss distance in R units used for EV calculation (default 1.0)


Visuals:
  • Toggles for zone labels, sweep labels, and dashboard
  • Resistance Color (default orange #f97316), Support Color (default sky blue #38bdf8)


How to Use This Indicator

Primary Setup — Statistical Zone Entry:
Look for Institutional or Strong zones with positive EV — these are the levels with the longest bounce history weighted by your R:R parameters. When price enters a zone, the live label shows the current win rate. Enter at the zone edge with a stop beyond the far edge and a target at your TP R:R ratio from entry.

EV as a Selection Filter:
Multiple zones may be on the chart simultaneously. Prioritize zones with positive EV labels (e.g., EV: 1.25R) over zones with negative EV. A zone with 4 touches and 75% win rate at 2R:1R produces an EV of +1.25R per trade — objectively worth trading. A zone with 3 touches and 33% win rate at the same R:R produces EV of -0.33R — not worth trading regardless of how prominent it looks.

Using the Sweep Count:
The total sweep count on the dashboard accumulates every time a zone break is detected with high-volume momentum. Rising sweep counts in one direction indicate the market is consistently breaking through levels on that side — a sign of trending pressure rather than range behavior. Adjust bias accordingly.

Cluster Merge and Fresh Zones:
When a new pivot forms near an existing zone and is merged rather than spawning a new zone, the existing zone's historical statistics remain unchanged. A fresh zone with no historical data will show EV close to 0 — treat these as unproven until more touches accumulate.

snapshot

Indicator Limitations

  • EV computation scans up to the full lookback on every qualifying pivot — on very long lookback settings and active pivot instruments, this can increase calculation time
  • The walk-forward EV uses the same zone size (ATR * width at creation time) for historical counting. If ATR changes significantly between creation time and the historical scan, the touch count may include bars where the equivalent zone boundaries would have been different
  • Cluster merging uses the current ATR at detection time. In periods of sharply rising or falling ATR, two zones that appear to merge at one ATR level may have been distinct at a different level, potentially underrepresenting zone density
  • The touch count displayed on the label is the live count updated each bar. The historical bounce count used for EV is computed at creation time and is not re-scanned dynamically — the label win rate reflects creation-time statistics
  • The minimum touches filter removes zones with fewer historical touches than the threshold. On fresh instruments or small lookbacks, most zones may be filtered out, especially on less-traded timeframes


Originality Statement

This indicator is original in its walk-forward EV scoring framework, four-tier visual strength system driven by live touch counts, and the cluster merging deduplication approach. The publication is justified because:
  • Walk-forward EV computation in R units provides a quantitative quality signal not found in standard support/resistance tools — each zone is backed by a historically derived expected value, enabling objective zone selection
  • The four-tier visual system (border width and opacity scaling with tier and EV) embeds the statistical quality directly into the zone appearance, eliminating the need to read labels to gauge significance
  • ATR-cluster merge deduplication prevents pivot-dense markets from generating overlapping zones at the same structural level, producing a cleaner, more meaningful map than raw pivot-based zone tools
  • Live label updates during zone interaction show the evolving win rate and EV as each new touch is counted, providing real-time statistical feedback not present in static zone indicators
  • The post-break fade combined with the total sweep count dashboard provides a structural memory of how many levels have been invalidated, enabling a directional bias gauge derived from zone lifecycle data


Disclaimer

This indicator is provided for educational and informational purposes only and does not constitute financial advice or a recommendation to buy or sell any financial instrument. Past performance of any pattern or signal does not guarantee future results. All trading involves substantial risk. Always use proper risk management and conduct your own independent analysis.

— Made with passion by officialjackofalltrades

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.