OPEN-SOURCE SCRIPT
Dynamic Gap Probability Tool

Dynamic Gap Probability Tool measures the percentage gap between price and a chosen moving average, then analyzes your chart history to estimate the likelihood of the next candle moving up or down. It dynamically adjusts its sample size to ensure statistical robustness while focusing on the exact deviation level.
Originality and Value:
• Combines gap-based analysis with dynamic sample aggregation to balance precision and reliability.
• Automatically extends the sample when exact matches are scarce, avoiding misleading signals on rare extreme moves.
• Provides real “next-candle” probabilities based on historical occurrences rather than fixed thresholds or untested heuristics.
• Adds value by giving traders an evidence-based edge: you see how similar past deviations actually played out.
How It Works:
1. Calculate gap = (close – moving average) / moving average * 100.
2. Round the absolute gap to nearest percent (X%).
3. Count historical bars where gap ≥ X% above or ≤ –X% below.
4. If exact X% count is below the minimum occurrences threshold, include gaps at X+1%, X+2%, etc., until threshold is reached.
5. Compute “next-candle” green vs. red probabilities from the aggregated sample.
6. Display current gap, sample size, green probability, and red probability in a table.
Inputs:
• Moving Average Type (SMA, EMA, WMA, VWMA, HMA, SMMA, TMA)
• Moving Average Period (default 200)
• Minimum Occurrences Threshold (default 50)
• Table position and styling options
Examples:
• If price is 3% above the 200-period SMA and 120 occurrences ≥3% are found, with 84 green next candles (70%) and 36 red (30%), the script displays “3% | 120 | 70% green | 30% red.”
• If price is 8% below the SMA but only 20 exact matches exist, the script will include 9% and 10% gaps until it reaches 50 samples, then calculate probabilities from that broader set.
Why It’s Useful:
• Mean-reversion traders see green-probability signals at extreme overbought or oversold levels.
• Trend-followers identify continuation likelihood when red probability is high.
• Risk managers gauge reliability by inspecting sample size before acting on any signal.
Limitations:
• Historical probabilities do not guarantee future performance.
• Results depend on timeframe and symbol, backtest with your data before trading.
• Use realistic slippage and commission when overlaying on strategy scripts.
Originality and Value:
• Combines gap-based analysis with dynamic sample aggregation to balance precision and reliability.
• Automatically extends the sample when exact matches are scarce, avoiding misleading signals on rare extreme moves.
• Provides real “next-candle” probabilities based on historical occurrences rather than fixed thresholds or untested heuristics.
• Adds value by giving traders an evidence-based edge: you see how similar past deviations actually played out.
How It Works:
1. Calculate gap = (close – moving average) / moving average * 100.
2. Round the absolute gap to nearest percent (X%).
3. Count historical bars where gap ≥ X% above or ≤ –X% below.
4. If exact X% count is below the minimum occurrences threshold, include gaps at X+1%, X+2%, etc., until threshold is reached.
5. Compute “next-candle” green vs. red probabilities from the aggregated sample.
6. Display current gap, sample size, green probability, and red probability in a table.
Inputs:
• Moving Average Type (SMA, EMA, WMA, VWMA, HMA, SMMA, TMA)
• Moving Average Period (default 200)
• Minimum Occurrences Threshold (default 50)
• Table position and styling options
Examples:
• If price is 3% above the 200-period SMA and 120 occurrences ≥3% are found, with 84 green next candles (70%) and 36 red (30%), the script displays “3% | 120 | 70% green | 30% red.”
• If price is 8% below the SMA but only 20 exact matches exist, the script will include 9% and 10% gaps until it reaches 50 samples, then calculate probabilities from that broader set.
Why It’s Useful:
• Mean-reversion traders see green-probability signals at extreme overbought or oversold levels.
• Trend-followers identify continuation likelihood when red probability is high.
• Risk managers gauge reliability by inspecting sample size before acting on any signal.
Limitations:
• Historical probabilities do not guarantee future performance.
• Results depend on timeframe and symbol, backtest with your data before trading.
• Use realistic slippage and commission when overlaying on strategy scripts.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
The trend is your friend, until it's not...
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
The trend is your friend, until it's not...
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.