OPEN-SOURCE SCRIPT
Smart Structure Breaks & Order Blocks

Overview (What it does)
The indicator “Smart Structure Breaks & Order Blocks” detects market structure using swing highs and lows, identifies Break of Structure (BOS) events, and automatically draws order blocks (OBs) from the origin candle. These zones extend to the right and change color/outline when mitigated or invalidated. By formalizing and automating part of discretionary analysis, it provides consistent zone recognition.
Main Components
Input Parameters
Signal Definition & Logic
Chart Appearance
Practical Use (Trading Assistance)
Parameter Tuning (per market/timeframe)
Strengths
Limitations
Workflow Example
FAQ
Customization Tips (Code perspective)
Summary
This indicator formalizes the cycle Swing → BOS → OB creation → Mitigation/Invalidation, providing consistent structure analysis and zone tracking. By tuning sensitivity and strictness, and combining with higher timeframe context, it enhances pullback/continuation trading setups. Always combine with proper risk management.
The indicator “Smart Structure Breaks & Order Blocks” detects market structure using swing highs and lows, identifies Break of Structure (BOS) events, and automatically draws order blocks (OBs) from the origin candle. These zones extend to the right and change color/outline when mitigated or invalidated. By formalizing and automating part of discretionary analysis, it provides consistent zone recognition.
Main Components
- []Swing Detection: ta.pivothigh/ta.pivotlow identify confirmed swing points.
[]BOS Detection: Determines if the recent swing high/low is broken by close (strict mode) or crossover.
[]OB Creation: After a BOS, the opposite candle (bearish for bullish BOS, bullish for bearish BOS) is used to generate an order block zone.
[]Zone Management: Limits the number of zones, extends them to the right, and tracks tagged (mitigated) or invalidated states.
Input Parameters
- []Left/Right Pivot (default 6/6): Number of bars required on each side to confirm a swing. Higher values = smoother swings.
[]Max Zones (default 4): Maximum zones stored per direction (bull/bear). Oldest zones are overwritten.
[]Zone Confirmation Lookback (default 3): Ensures OB origin candle validity by checking recent highs/lows.
[]Show Swing Points (default ON): Displays triangles on swing highs/lows.
[]Require close for BOS? (default ON): Strict BOS (close required) vs loose BOS (line crossover).
[]Use candle body for zones (default OFF): Zones drawn from candle body (ON) or wick (OFF).
Signal Definition & Logic
- []Swing Updates: Latest confirmed pivots update lastHighLevel / lastLowLevel.
[]BOS (Break of Structure):
Bullish – close breaks last swing high.
Bearish – close breaks last swing low.
Only one valid BOS per swing (avoids duplicates).
[]OB Detection:
Bullish BOS → previous bearish candle with lowest low forms the OB.
Bearish BOS → previous bullish candle with highest high forms the OB.
Zones: Bull = green, Bear = red, semi-transparent, extended to the right.
[]Zone States:
Mitigated: Price touches the zone → border highlighted.
Invalidated:
Bull zone → close below → turns red.
Bear zone → close above → turns green.
Chart Appearance
- []Swing High: red triangle above bar
[]Swing Low: green triangle below bar
[]Bull OB: green zone (border highlighted on touch)
[]Bear OB: red zone (border highlighted on touch) - Invalid Zones: Bull zones turn reddish, Bear zones turn greenish
Practical Use (Trading Assistance)
- []Trend Following Entries: Buy pullbacks into green OBs in uptrends, sell rallies into red OBs in downtrends.
[]Focus on First Touch: First mitigation after BOS often has higher reaction probability.
[]Confluence: Combine with higher timeframe trend, volume, session levels, key price levels (previous highs/lows, VWAP, etc.).
[]Stops/Targets:
Bull – stop below zone, partial take profit at swing high or resistance.
Bear – stop above zone, partial take profit at swing low or support.
Parameter Tuning (per market/timeframe)
- []Pivot (6/6 → 4/4/8/8): Lower for scalping (3–5), medium for day trading (5–8), higher for swing trading (8–14). Increase to reduce noise.
[]Strict Break: ON to reduce false breaks in ranging markets; OFF for earlier signals.
[]Body Zones: ON for assets with long wicks, OFF for cleaner OBs in liquid instruments.
[]Zone Confirmation (default 3): Increase for stricter OB origin, fewer zones. - Max Zones (default 4 → 6–10): Increase for higher volatility, decrease to avoid clutter.
Strengths
- []Standardizes BOS and OB detection that is usually subjective.
[]Tracks mitigation and invalidation automatically. - Adaptable: allows body/wick zone switching for different instruments.
Limitations
- []Pivot-based: Signals appear only after pivots confirm (slight lag).
[]Zones reflect past balance: Can fail after new events (news, earnings, macro data).
[]Range-heavy markets: More false BOS; consider stricter settings.
[]Backtesting: This script is for drawing/visual aid; trading rules must be defined separately.
Workflow Example
- []Identify higher timeframe trend (4H/Daily).
[]On lower TF (15–60m), wait for BOS and new OB.
[]Enter on first mitigation with confirmation candle.
[]Stop beyond zone; targets based on R multiples and swing points.
FAQ
- []Q: Why are zones invalidated quickly?
A: Flow reversal after BOS. Adjust pivots higher, enable Strict mode, or switch to Body zones to reduce noise.
[]Q: What does “tagged” mean?
A: Price touched the zone once = mitigated. Implies some orders in that zone may have been filled. - Q: Body or Wick zones?
A: Wick zones are fine in clean markets. For volatile pairs with long wicks, body zones provide more realistic areas.
Customization Tips (Code perspective)
- []Zone storage: Currently ring buffer ((idx+1) % zoneLimit). Could prioritize keeping unmitigated zones.
[]Automated testing: Add strategy.entry/exit for rule-based backtests.
[]Multi-timeframe: Use request.security() for higher timeframe swings/BOS.
[]Visualization: Add labels for BOS bars, tag zones with IDs, count touches.
Summary
This indicator formalizes the cycle Swing → BOS → OB creation → Mitigation/Invalidation, providing consistent structure analysis and zone tracking. By tuning sensitivity and strictness, and combining with higher timeframe context, it enhances pullback/continuation trading setups. Always combine with proper risk management.
Skrypt open-source
W duchu TradingView twórca tego skryptu udostępnił go jako open-source, aby traderzy mogli analizować i weryfikować jego funkcjonalność. Brawo dla autora! Możesz korzystać z niego za darmo, ale pamiętaj, że ponowna publikacja kodu podlega naszym Zasadom Społeczności.
Wyłączenie odpowiedzialności
Informacje i publikacje przygotowane przez TradingView lub jego użytkowników, prezentowane na tej stronie, nie stanowią rekomendacji ani porad handlowych, inwestycyjnych i finansowych i nie powinny być w ten sposób traktowane ani wykorzystywane. Więcej informacji na ten temat znajdziesz w naszym Regulaminie.
Skrypt open-source
W duchu TradingView twórca tego skryptu udostępnił go jako open-source, aby traderzy mogli analizować i weryfikować jego funkcjonalność. Brawo dla autora! Możesz korzystać z niego za darmo, ale pamiętaj, że ponowna publikacja kodu podlega naszym Zasadom Społeczności.
Wyłączenie odpowiedzialności
Informacje i publikacje przygotowane przez TradingView lub jego użytkowników, prezentowane na tej stronie, nie stanowią rekomendacji ani porad handlowych, inwestycyjnych i finansowych i nie powinny być w ten sposób traktowane ani wykorzystywane. Więcej informacji na ten temat znajdziesz w naszym Regulaminie.