OPEN-SOURCE SCRIPT
Breakout Hazard Surface [JAYADEV RANA]

Overview
This script is designed to quantify breakout risk around a defined range (highest high / lowest low over a lookback) and express it as:
Directional breakout probabilities (Up% and Dn%), and
A hazard surface across the next Forecast Horizon bars that estimates when the breakout risk is concentrated.
It’s not a price target tool. It’s a regime/context tool: “How likely is a range break, in which direction, and is that risk near-term?”
Usage
Bull Watch / Bear Watch: When one side’s breakout probability exceeds the other, but doesn’t meet the strict “expansion” trigger, the script flags a directional watch state.
Bull Expansion / Bear Expansion (signal markers): A signal prints when:
One side’s probability is dominant (≥ Dominance Threshold),
The net edge versus the opposite side is meaningful (≥ Minimum Edge), and
The expected timing is front-loaded (expected breakout bar ≤ Front-Loaded Cutoff).
Net Edge histogram: The column plot shows the difference between Up% and Dn% (in percentage points). Positive values indicate bullish skew; negative values indicate bearish skew.
Hazard Table (timing distribution): The table shows the probability mass allocated to each future bar (1…Horizon) for both directions. Higher values early in the horizon imply “pressure building now”; flatter distributions imply “risk exists but timing is less urgent.”
Details (what it’s doing)
1) Range context and proximity
The “range” is defined by Range Length: highest high and lowest low.
Proximity measures how close price is to the range high (bull) or range low (bear), normalized by range size (with ATR as a floor to avoid tiny ranges).
2) Volatility compression / expansion
Uses fast vs slow ATR (ATR Fast Length vs ATR Slow Length) to estimate compression:
Compression rises when ATR_fast is well below ATR_slow.
Expansion is treated as (1 − compression) and feeds the urgency/timing model.
3) Drift and efficiency
Drift is approximated via an EMA of log returns normalized by return stdev (Drift Length), converted into a bull/bear drift score.
Efficiency estimates directional “trend efficiency” over the drift window (net move divided by total absolute movement), assigned to bull or bear based on direction.
4) Volume normalisation
Volume is z-scored over Volume Length and mapped into a bounded 0–1 feature to avoid extreme influence.
5) Score blending → bucket calibration
A Bull Score and Bear Score are blended from proximity, drift, compression, efficiency, and volume.
Each score is discretised into Calibration Bins.
For each bin, the script records:
Trials: how often that bin occurred historically
Hits: how often an up-break (future high > past range high) or down-break (future low < past range low) occurred within the forecast horizon
Probabilities are computed with Laplace smoothing (the “Laplace Prior”) and optional Neighbor Smoothing across adjacent bins to stabilize estimates when data is sparse.
6) Hazard surface (probability by future bar)
The script takes the calibrated breakout probability (Up% / Dn%) and distributes it across the next Horizon bars using an exponential-style timing model driven by an urgency factor (proximity + drift tilt + expansion + volume tilt).
Result: you get both total probability and a timing profile (front-loaded vs back-loaded).
Settings
Calculation
Range Length: Defines the consolidation/range boundaries.
Forecast Horizon: Bars ahead used for calibration and hazard table.
Calibration Bins: Coarseness of the probability model; more bins = more granular, but needs more data.
Drift Length: Lookback for drift and efficiency calculations.
ATR Fast/Slow Length: Drives compression/expansion regime.
Volume Length: Lookback for volume normalization.
Neighbour Smoothing: Averages probability estimates across nearby bins for stability.
Laplace Prior: Controls smoothing strength; higher values reduce overfitting in low-sample bins.
Signals
Dominance Threshold: Minimum probability required to consider one side “dominant.”
Minimum Edge: Minimum difference between Up% and Dn% to avoid weak/ambiguous triggers.
Front-Loaded Cutoff: Requires expected breakout timing to be early in the horizon for expansion signals.
Dashboard / Style
Show Hazard Table and Table Position control the on-chart table.
Bull/Bear/Neutral colors customize visuals.
Limitations
Warmup requirement: The model needs a meaningful history before calibration is stable (especially with higher bin counts). Early readings can be noisy.
Probabilities are conditional and path-dependent: They reflect how similar historical conditions resolved on the same symbol/timeframe; regime shifts can degrade reliability.
Intrabar variability: Display values can change during a forming candle; historical calibration updates on confirmed bars, but the current context can still move until close.
Not a standalone trading system: Treat signals as context. Confirm with structure, liquidity, and risk controls.
This script is designed to quantify breakout risk around a defined range (highest high / lowest low over a lookback) and express it as:
Directional breakout probabilities (Up% and Dn%), and
A hazard surface across the next Forecast Horizon bars that estimates when the breakout risk is concentrated.
It’s not a price target tool. It’s a regime/context tool: “How likely is a range break, in which direction, and is that risk near-term?”
Usage
Bull Watch / Bear Watch: When one side’s breakout probability exceeds the other, but doesn’t meet the strict “expansion” trigger, the script flags a directional watch state.
Bull Expansion / Bear Expansion (signal markers): A signal prints when:
One side’s probability is dominant (≥ Dominance Threshold),
The net edge versus the opposite side is meaningful (≥ Minimum Edge), and
The expected timing is front-loaded (expected breakout bar ≤ Front-Loaded Cutoff).
Net Edge histogram: The column plot shows the difference between Up% and Dn% (in percentage points). Positive values indicate bullish skew; negative values indicate bearish skew.
Hazard Table (timing distribution): The table shows the probability mass allocated to each future bar (1…Horizon) for both directions. Higher values early in the horizon imply “pressure building now”; flatter distributions imply “risk exists but timing is less urgent.”
Details (what it’s doing)
1) Range context and proximity
The “range” is defined by Range Length: highest high and lowest low.
Proximity measures how close price is to the range high (bull) or range low (bear), normalized by range size (with ATR as a floor to avoid tiny ranges).
2) Volatility compression / expansion
Uses fast vs slow ATR (ATR Fast Length vs ATR Slow Length) to estimate compression:
Compression rises when ATR_fast is well below ATR_slow.
Expansion is treated as (1 − compression) and feeds the urgency/timing model.
3) Drift and efficiency
Drift is approximated via an EMA of log returns normalized by return stdev (Drift Length), converted into a bull/bear drift score.
Efficiency estimates directional “trend efficiency” over the drift window (net move divided by total absolute movement), assigned to bull or bear based on direction.
4) Volume normalisation
Volume is z-scored over Volume Length and mapped into a bounded 0–1 feature to avoid extreme influence.
5) Score blending → bucket calibration
A Bull Score and Bear Score are blended from proximity, drift, compression, efficiency, and volume.
Each score is discretised into Calibration Bins.
For each bin, the script records:
Trials: how often that bin occurred historically
Hits: how often an up-break (future high > past range high) or down-break (future low < past range low) occurred within the forecast horizon
Probabilities are computed with Laplace smoothing (the “Laplace Prior”) and optional Neighbor Smoothing across adjacent bins to stabilize estimates when data is sparse.
6) Hazard surface (probability by future bar)
The script takes the calibrated breakout probability (Up% / Dn%) and distributes it across the next Horizon bars using an exponential-style timing model driven by an urgency factor (proximity + drift tilt + expansion + volume tilt).
Result: you get both total probability and a timing profile (front-loaded vs back-loaded).
Settings
Calculation
Range Length: Defines the consolidation/range boundaries.
Forecast Horizon: Bars ahead used for calibration and hazard table.
Calibration Bins: Coarseness of the probability model; more bins = more granular, but needs more data.
Drift Length: Lookback for drift and efficiency calculations.
ATR Fast/Slow Length: Drives compression/expansion regime.
Volume Length: Lookback for volume normalization.
Neighbour Smoothing: Averages probability estimates across nearby bins for stability.
Laplace Prior: Controls smoothing strength; higher values reduce overfitting in low-sample bins.
Signals
Dominance Threshold: Minimum probability required to consider one side “dominant.”
Minimum Edge: Minimum difference between Up% and Dn% to avoid weak/ambiguous triggers.
Front-Loaded Cutoff: Requires expected breakout timing to be early in the horizon for expansion signals.
Dashboard / Style
Show Hazard Table and Table Position control the on-chart table.
Bull/Bear/Neutral colors customize visuals.
Limitations
Warmup requirement: The model needs a meaningful history before calibration is stable (especially with higher bin counts). Early readings can be noisy.
Probabilities are conditional and path-dependent: They reflect how similar historical conditions resolved on the same symbol/timeframe; regime shifts can degrade reliability.
Intrabar variability: Display values can change during a forming candle; historical calibration updates on confirmed bars, but the current context can still move until close.
Not a standalone trading system: Treat signals as context. Confirm with structure, liquidity, and risk controls.
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または承認する金融、投資、取引、またはその他の種類の助言もしくは推奨であることを意図したものではなく、またこれらに該当するものでもありません。詳細は利用規約をご覧ください。