OPEN-SOURCE SCRIPT

Market Structure Break & Order Block - Improved By SPARTAN

9 524
This indicator combines market structure break detection with Order Block and Breaker Block zone mapping into a single, cohesive tool. It is built on the open-source MSB-OB concept by EmreKb (Mozilla Public License 2.0, tradingview.com/v/DkE7UniD/) and has been substantially rewritten with a focus on accuracy, practicality, and removing several logical flaws present in similar tools.


HOW IT WORKS

Market structure is identified through a zigzag algorithm that tracks swing highs and lows over a user-defined lookback period. A bullish Market Structure Break (MSB) is confirmed when price takes out the previous significant swing high by a minimum threshold expressed as a fraction of the prior swing range — the Fib Factor setting. A bearish MSB requires the equivalent to the downside. All structure breaks are confirmed only on bar close to prevent repainting.

Once a structure break is confirmed, the indicator identifies the relevant Order Block: the last candle of the opposite colour in the swing that preceded the break. For a bullish MSB, this is the last bearish candle in the prior downswing. For a bearish MSB, it is the last bullish candle in the prior upswing. This candle's high-to-low range is drawn as a zone and tracked going forward.

Breaker Blocks are also identified: these are zones from a prior swing that price has since broken through and is now returning to, flipping the original supply or demand area to the opposite role. The indicator distinguishes between Breaker Blocks (full structural flip) and Mitigation Blocks (partial), and labels them accordingly.


ZONE MANAGEMENT

Each zone is tracked individually. The indicator monitors whether price closes inside, outside, or through each zone every bar. Zones are removed from the chart when price closes beyond them, keeping the chart clean and relevant.

A cap on the maximum number of active zones per zone type can be set by the user. When the cap is reached, the oldest zone is removed automatically. This prevents the indicator from silently hitting TradingView's hard object limits on long charts or active markets.

The zone extension mode can be set to either dynamic (zones stretch to the right until broken) or fixed-length (zones extend a set number of bars and stop).


ZONE QUALITY FEATURES

Each zone is scored at creation using the ratio of the forming candle's body size to the 14-period ATR. Zones are classified as Strong, Moderate, or Weak based on user-adjustable thresholds. Stronger candles produce more opaque zone fills so higher-quality zones are visually prominent without cluttering the chart.

Zones formed on above-average volume (relative to the 20-bar simple moving average) are flagged with a Vol+ tag. High-volume Order Blocks tend to represent periods of significant institutional activity and are generally more likely to hold on a return.

Each zone displays a tap count that tracks how many times price has entered it. The count increments only on an outside-to-inside transition based on closing price, not on every bar that price happens to spend inside the zone. A zone that has never been entered is marked as pristine. First touches of untested zones are generally the highest-probability setups.

When price wicks into a zone — including cases where the wick passes entirely through the zone — but the bar closes on the correct side, the zone border switches to dashed. This signals that the zone has been tested without being invalidated, which is a meaningful distinction from a zone that has never been touched.

A dashed CE line (Consequent Encroachment) is drawn at the exact midpoint of each zone. This is the 50% level of the Order Block range, which is commonly used as a precise entry reference within the zone.

When an Order Block and a Breaker Block of the same direction overlap, a compact confluence marker is placed at the left edge of the Order Block zone. Overlapping supply or demand areas are generally considered stronger than isolated ones. The marker fires only once per zone pair regardless of how long both zones remain active.


FAILED MSB DETECTION

When a structure break occurs and price reverses within a configurable number of bars, the indicator can flag this as a Failed MSB. To reduce false positives, the reversal must also exceed a minimum magnitude expressed as a fraction of the prior swing range. A reversal that retraces less than this threshold — which may just be normal noise after the break — is ignored. Failed MSBs are a common setup for counter-trend moves and fakeout entries.


HIGHER TIMEFRAME REFERENCE

An optional higher timeframe reference line can be enabled. This draws a horizontal line at the midpoint of the most recent bar on the selected higher timeframe. The line is extended across the chart and can be used as a broad directional bias reference. The line is redrawn only when the higher timeframe midpoint changes, not on every bar, avoiding unnecessary flickering on live charts.


ALERTS

Alert conditions are available for: any MSB, bullish MSB only, bearish MSB only, Failed MSB, and individual zone entries (Bu-OB, Be-OB, Bu-BB, Be-BB). Zone entry alerts fire once per bar close on first touch only.


SETTINGS OVERVIEW

Core Settings: zigzag length, fib factor threshold, label text size, broken zone deletion toggle.
Block Display: separate toggles for Order Blocks and Breaker Blocks, fixed vs dynamic extension, block length in bars, max active zones per type.
OB Strength: strength label toggle, volume flag toggle, adjustable strong and weak body/ATR thresholds.
Zone Tap Counter: toggle for pristine and tap count display.
Mitigation Visuals: toggle for dashed border on wick-tested zones.
CE Level: toggle and color for the 50% midpoint line.
Confluence: toggle, color, text color, and size for the confluence marker.
MSB Labels: toggle for MSB labels, Failed MSB toggle, lookback bars, and minimum reversal percentage.
Multi-Timeframe: HTF level toggle, timeframe selection, line style, color, width, and bars back.
Colors: fully customizable fill, border, and text colors for each of the four zone types.


NOTES

This script is published open-source under the Mozilla Public License 2.0. It is a derivative of EmreKb's original MSB-OB indicator and credit is given accordingly. The original concept has been significantly reworked at the logic level: zone deletion direction for Breaker Blocks has been corrected, tap counting has been rebuilt to track transitions rather than bar presence, the Failed MSB filter now requires a magnitude condition alongside the bar count, confluence labeling is guarded against duplicate firing, and higher timeframe line rendering no longer redraws unnecessarily every bar.

This script is intended as an analytical tool and does not constitute financial advice. Past zone reactions do not guarantee future behaviour.

Aviso legal

As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.