OPEN-SOURCE SCRIPT
Order Block Detector Pro

Order Block Detector Pro — FVG + Volume + Breaker Blocks
━━━━━━━━━━━━━━━━━━━━
CREDIT & OPEN-SOURCE STATEMENT
━━━━━━━━━━━━━━━━━━━━
Zone detection concept inspired by "Market Structure Break & Orderblock" by EmreKb (TradingView open-source). The codebase has been fully rewritten with significant original additions: Fair Value Gap overlap detection, volume confirmation filter, Breaker Block conversion logic, anti-clustering system, unrespected zone cancellation, and FVG-tagged labels. Published open-source in compliance with TradingView reuse rules.
━━━━━━━━━━━━━━━━━━━━
WHY THESE COMPONENTS WORK TOGETHER
━━━━━━━━━━━━━━━━━━━━
Order blocks identify WHERE institutional orders were placed. Volume confirms WHETHER those orders had genuine institutional size. Fair Value Gaps identify WHETHER an imbalance exists inside the zone that price is likely to return and fill. None of these three factors alone is sufficient — an order block without volume may be retail noise; volume without structure has no location context; FVGs without a structural anchor have no directional bias. Combined, they produce a filtered, high-probability zone with multiple layers of institutional confirmation.
━━━━━━━━━━━━━━━━━━━━
WHAT THIS INDICATOR DOES
━━━━━━━━━━━━━━━━━━━━
Detects four zone types based on market structure breaks:
- Bu OB — Bullish Order Block (demand)
- Be OB — Bearish Order Block (supply)
- Bu BB — Bullish Breaker Block (former supply flipped to demand)
- Be BB — Bearish Breaker Block (former demand flipped to supply)
Each zone is volume-confirmed, FVG-tagged when applicable, anti-clustered, and auto-mitigated when price returns.
━━━━━━━━━━━━━━━━━━━━
MARKET STRUCTURE — SWING PIVOTS + BOS
━━━━━━━━━━━━━━━━━━━━
Swing highs and lows detected using ta.pivothigh() and ta.pivotlow() with a configurable pivot length (default 5 bars each side).
Break of Structure (BOS) is confirmed when:
- BOS Up: close crosses above a previously unbroken swing high
- BOS Down: close crosses below a previously unbroken swing low
Each swing point is tracked in a typed array (Swing type: price, bar, broken flag). Once broken, the swing is marked and triggers order block search.
━━━━━━━━━━━━━━━━━━━━
ORDER BLOCK DETECTION
━━━━━━━━━━━━━━━━━━━━
On BOS Up: script searches backwards up to N bars (default 25) for the most recent bearish candle (close < open). That candle is the Bullish Order Block — the last point where sellers were active before institutional buyers took control.
On BOS Down: searches backwards for the most recent bullish candle (close > open). That candle is the Bearish Order Block.
Zone boundaries:
- Full Range mode: top = high, bottom = low of OB candle
- Body Only mode: top = max(open, close), bottom = min(open, close)
━━━━━━━━━━━━━━━━━━━━
VOLUME CONFIRMATION
━━━━━━━━━━━━━━━━━━━━
When enabled, an order block only forms if:
- volume[offset] > SMA(volume, 20) × multiplier (default 1.5×)
This filters out low-participation structural moves that are more likely to fail on retest. The volume is read at the OB candle's bar offset, not the current bar.
━━━━━━━━━━━━━━━━━━━━
FAIR VALUE GAP (FVG) DETECTION
━━━━━━━━━━━━━━━━━━━━
After an order block is identified, the script checks for a Fair Value Gap within the zone's price range.
Bullish FVG condition (within OB range):
- low[m−1] > high[m+1] — gap between candle m−1's low and candle m+1's high
- Both gap boundaries fall within the OB top/bottom range
Bearish FVG: inverse — high[m−1] < low[m+1].
When a FVG is detected inside the zone, a ★ symbol is appended to the zone label. These zones represent compounded institutional imbalances — price is statistically more likely to react at them.
━━━━━━━━━━━━━━━━━━━━
ANTI-CLUSTERING SYSTEM
━━━━━━━━━━━━━━━━━━━━
Prevents redundant overlapping zones using two filters:
Spacing filter:
- New zone rejected if midpoint is within ATR(14) × spacing multiplier of any existing active zone of the same type
Max active cap:
- Each zone type (Bu OB, Be OB, Bu BB, Be BB) capped at N active zones (default 5)
- When exceeded, oldest zone of that type is removed first
━━━━━━━━━━━━━━━━━━━━
MITIGATION LOGIC
━━━━━━━━━━━━━━━━━━━━
Two mitigation modes:
Body Close (default):
- Bu OB mitigated when close < zone bottom
- Be OB mitigated when close > zone top
Wick Touch:
- Bu OB mitigated when low < zone bottom
- Be OB mitigated when high > zone top
━━━━━━━━━━━━━━━━━━━━
BREAKER BLOCK CONVERSION
━━━━━━━━━━━━━━━━━━━━
When a mitigated zone was previously touched (price entered the zone at least once before violation):
- Bu OB violated → converts to Be BB (Bearish Breaker Block)
- Be OB violated → converts to Bu BB (Bullish Breaker Block)
Breaker Blocks represent zones where institutional orders have been fully absorbed — price is expected to use them as opposition on return.
Unrespected zone cancellation: if a zone is violated without ever being touched (price skipped through it), it is removed entirely rather than converted. This avoids phantom breaker blocks from low-quality structural moves.
━━━━━━━━━━━━━━━━━━━━
ZONE RENDERING
━━━━━━━━━━━━━━━━━━━━
Each zone is drawn as a box extending N bars to the right (default 50, updated each bar via box.set_right). Zone type visual identifiers:
- Bu OB — teal fill, solid border
- Be OB — red fill, solid border
- Bu BB — blue fill, dashed border
- Be BB — orange fill, dashed border
Volume of the OB candle is displayed inside the box (formatted as K/M/B). Labels show zone type and FVG tag (★) when applicable.
━━━━━━━━━━━━━━━━━━━━
HOW TO USE
━━━━━━━━━━━━━━━━━━━━
1. Wait for a fresh Bu OB or Be OB to form after a BOS
2. Prioritize zones marked with ★ (FVG overlap = stronger imbalance)
3. Enter when price returns to the zone on lower timeframe confirmation
4. Bu BB and Be BB are counter-trend zones — treat as resistance/support levels after a failed OB
5. Mitigated zones auto-remove — no manual cleanup needed
Entry logic suggestion:
- Long: price enters Bu OB zone → bullish rejection candle → entry above that candle
- Short: price enters Be OB zone → bearish rejection candle → entry below that candle
Stop loss: opposite side of the zone box
Target: next opposing zone or structural high/low
━━━━━━━━━━━━━━━━━━━━
RECOMMENDED SETTINGS
━━━━━━━━━━━━━━━━━━━━
- Scalping (1m–5m): Swing Length 3, OB Search 15, Volume ON 1.5×
- Intraday (15m–1H): Swing Length 5, OB Search 25, Volume ON 1.5×
- Swing (4H–Daily): Swing Length 7, OB Search 40, Volume OFF
- Forex/Gold: Volume OFF (broker tick volume unreliable)
- Stocks/Crypto/Futures: Volume ON
━━━━━━━━━━━━━━━━━━━━
No repainting. Pivot detection is inherently offset by pivot length bars by design. All zone creation and mitigation runs on confirmed price data only.
━━━━━━━━━━━━━━━━━━━━
CREDIT & OPEN-SOURCE STATEMENT
━━━━━━━━━━━━━━━━━━━━
Zone detection concept inspired by "Market Structure Break & Orderblock" by EmreKb (TradingView open-source). The codebase has been fully rewritten with significant original additions: Fair Value Gap overlap detection, volume confirmation filter, Breaker Block conversion logic, anti-clustering system, unrespected zone cancellation, and FVG-tagged labels. Published open-source in compliance with TradingView reuse rules.
━━━━━━━━━━━━━━━━━━━━
WHY THESE COMPONENTS WORK TOGETHER
━━━━━━━━━━━━━━━━━━━━
Order blocks identify WHERE institutional orders were placed. Volume confirms WHETHER those orders had genuine institutional size. Fair Value Gaps identify WHETHER an imbalance exists inside the zone that price is likely to return and fill. None of these three factors alone is sufficient — an order block without volume may be retail noise; volume without structure has no location context; FVGs without a structural anchor have no directional bias. Combined, they produce a filtered, high-probability zone with multiple layers of institutional confirmation.
━━━━━━━━━━━━━━━━━━━━
WHAT THIS INDICATOR DOES
━━━━━━━━━━━━━━━━━━━━
Detects four zone types based on market structure breaks:
- Bu OB — Bullish Order Block (demand)
- Be OB — Bearish Order Block (supply)
- Bu BB — Bullish Breaker Block (former supply flipped to demand)
- Be BB — Bearish Breaker Block (former demand flipped to supply)
Each zone is volume-confirmed, FVG-tagged when applicable, anti-clustered, and auto-mitigated when price returns.
━━━━━━━━━━━━━━━━━━━━
MARKET STRUCTURE — SWING PIVOTS + BOS
━━━━━━━━━━━━━━━━━━━━
Swing highs and lows detected using ta.pivothigh() and ta.pivotlow() with a configurable pivot length (default 5 bars each side).
Break of Structure (BOS) is confirmed when:
- BOS Up: close crosses above a previously unbroken swing high
- BOS Down: close crosses below a previously unbroken swing low
Each swing point is tracked in a typed array (Swing type: price, bar, broken flag). Once broken, the swing is marked and triggers order block search.
━━━━━━━━━━━━━━━━━━━━
ORDER BLOCK DETECTION
━━━━━━━━━━━━━━━━━━━━
On BOS Up: script searches backwards up to N bars (default 25) for the most recent bearish candle (close < open). That candle is the Bullish Order Block — the last point where sellers were active before institutional buyers took control.
On BOS Down: searches backwards for the most recent bullish candle (close > open). That candle is the Bearish Order Block.
Zone boundaries:
- Full Range mode: top = high, bottom = low of OB candle
- Body Only mode: top = max(open, close), bottom = min(open, close)
━━━━━━━━━━━━━━━━━━━━
VOLUME CONFIRMATION
━━━━━━━━━━━━━━━━━━━━
When enabled, an order block only forms if:
- volume[offset] > SMA(volume, 20) × multiplier (default 1.5×)
This filters out low-participation structural moves that are more likely to fail on retest. The volume is read at the OB candle's bar offset, not the current bar.
━━━━━━━━━━━━━━━━━━━━
FAIR VALUE GAP (FVG) DETECTION
━━━━━━━━━━━━━━━━━━━━
After an order block is identified, the script checks for a Fair Value Gap within the zone's price range.
Bullish FVG condition (within OB range):
- low[m−1] > high[m+1] — gap between candle m−1's low and candle m+1's high
- Both gap boundaries fall within the OB top/bottom range
Bearish FVG: inverse — high[m−1] < low[m+1].
When a FVG is detected inside the zone, a ★ symbol is appended to the zone label. These zones represent compounded institutional imbalances — price is statistically more likely to react at them.
━━━━━━━━━━━━━━━━━━━━
ANTI-CLUSTERING SYSTEM
━━━━━━━━━━━━━━━━━━━━
Prevents redundant overlapping zones using two filters:
Spacing filter:
- New zone rejected if midpoint is within ATR(14) × spacing multiplier of any existing active zone of the same type
Max active cap:
- Each zone type (Bu OB, Be OB, Bu BB, Be BB) capped at N active zones (default 5)
- When exceeded, oldest zone of that type is removed first
━━━━━━━━━━━━━━━━━━━━
MITIGATION LOGIC
━━━━━━━━━━━━━━━━━━━━
Two mitigation modes:
Body Close (default):
- Bu OB mitigated when close < zone bottom
- Be OB mitigated when close > zone top
Wick Touch:
- Bu OB mitigated when low < zone bottom
- Be OB mitigated when high > zone top
━━━━━━━━━━━━━━━━━━━━
BREAKER BLOCK CONVERSION
━━━━━━━━━━━━━━━━━━━━
When a mitigated zone was previously touched (price entered the zone at least once before violation):
- Bu OB violated → converts to Be BB (Bearish Breaker Block)
- Be OB violated → converts to Bu BB (Bullish Breaker Block)
Breaker Blocks represent zones where institutional orders have been fully absorbed — price is expected to use them as opposition on return.
Unrespected zone cancellation: if a zone is violated without ever being touched (price skipped through it), it is removed entirely rather than converted. This avoids phantom breaker blocks from low-quality structural moves.
━━━━━━━━━━━━━━━━━━━━
ZONE RENDERING
━━━━━━━━━━━━━━━━━━━━
Each zone is drawn as a box extending N bars to the right (default 50, updated each bar via box.set_right). Zone type visual identifiers:
- Bu OB — teal fill, solid border
- Be OB — red fill, solid border
- Bu BB — blue fill, dashed border
- Be BB — orange fill, dashed border
Volume of the OB candle is displayed inside the box (formatted as K/M/B). Labels show zone type and FVG tag (★) when applicable.
━━━━━━━━━━━━━━━━━━━━
HOW TO USE
━━━━━━━━━━━━━━━━━━━━
1. Wait for a fresh Bu OB or Be OB to form after a BOS
2. Prioritize zones marked with ★ (FVG overlap = stronger imbalance)
3. Enter when price returns to the zone on lower timeframe confirmation
4. Bu BB and Be BB are counter-trend zones — treat as resistance/support levels after a failed OB
5. Mitigated zones auto-remove — no manual cleanup needed
Entry logic suggestion:
- Long: price enters Bu OB zone → bullish rejection candle → entry above that candle
- Short: price enters Be OB zone → bearish rejection candle → entry below that candle
Stop loss: opposite side of the zone box
Target: next opposing zone or structural high/low
━━━━━━━━━━━━━━━━━━━━
RECOMMENDED SETTINGS
━━━━━━━━━━━━━━━━━━━━
- Scalping (1m–5m): Swing Length 3, OB Search 15, Volume ON 1.5×
- Intraday (15m–1H): Swing Length 5, OB Search 25, Volume ON 1.5×
- Swing (4H–Daily): Swing Length 7, OB Search 40, Volume OFF
- Forex/Gold: Volume OFF (broker tick volume unreliable)
- Stocks/Crypto/Futures: Volume ON
━━━━━━━━━━━━━━━━━━━━
No repainting. Pivot detection is inherently offset by pivot length bars by design. All zone creation and mitigation runs on confirmed price data only.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.