OPEN-SOURCE SCRIPT
Market Structure Shift and CRT

Here it is to copy directly.
**SHORT DESCRIPTION**
Market structure built from candle bodies instead of wicks, read across two timeframes at once, combined with higher-timeframe liquidity sweep detection that requires the body to close back inside the swept level.
---
**FULL DESCRIPTION**
Two ideas drive this indicator, and both are about ignoring wicks when they lie and paying attention to them when they matter.
**Structure from bodies**
Most structure tools build swings from highs and lows. One wick — a stop run, a thin print during a quiet hour — can define a swing point and drag the entire structure read with it.
This builds swings from candle bodies by default: open and close, not high and low. Levels reflect where price was accepted rather than where it briefly poked. A wick-based mode is included if you want to compare.
The zigzag traces a continuous line through body pivots. At length 1 it puts a vertex at nearly every bar and stretches into longer legs only where price runs without pulling back, so the shape itself shows you where the market trended and where it chopped.
Structure labels run on their own, slower pivot setting so they stay readable while the zigzag stays dense. Every confirmed pivot is compared to the previous pivot in the same direction and labeled HH, HL, LH, or LL. When price breaks a structure level it prints BOS if the break continues the existing bias, or CHoCH if it reverses it.
**Pivots with separate Left and Right bars**
This is where most structure indicators quietly cost you time.
A pivot cannot confirm until bars have printed after it. If left and right are tied to one number, improving quality always costs you speed. Here they are separate: Left decides how significant a swing must be, Right decides how long you wait. Left 5 / Right 2 keeps a meaningful filter while confirming in two bars instead of five.
Evaluate Breaks On removes the second source of lag. Set to Chart Bar, a break of a higher-timeframe level registers the moment price crosses it on your chart rather than waiting for that timeframe's bar to close — worth up to fourteen bars on a 1-minute chart reading 15-minute structure. Set to TF Close for fewer false breaks.
**Dual-timeframe alignment**
Structure TF 1 drives everything drawn on the chart. Structure TF 2 runs the same logic on a higher timeframe and feeds the table. Each has its own pivot settings and its own level colors.
The table compares them row by row — bias, last event, last pivot, active swing high and low — and then reports:
ALIGNED ▲ — both timeframes bullish
ALIGNED ▼ — both timeframes bearish
CONFLICT — they disagree
Conflict is the row worth watching. It means the faster timeframe has flipped while the slower one hasn't confirmed, which is where counter-trend entries tend to get run over.
**Higher-timeframe sweeps**
A sweep is a candle whose wick clears a prior candle's high or low while its body closes back inside. Body top is the close on an up candle and the open on a down candle, so a bar that opens beyond the level and reverses is correctly rejected — a close-only test would pass it.
Higher-timeframe candles are assembled bar by bar from your chart rather than requested. Each HTF high and low keeps the bar where it actually printed, so sweep lines anchor to the wick that made the level instead of to wherever the HTF bar happened to close. Nothing is pulled through request.security for the sweep engine, so there is no repainting question on confirmed sweeps.
Three independent sweep timeframes, each with its own toggle, timeframe and color, fully separate from the structure timeframes. A sweep stays live and extends right until a later candle's body closes through it, at which point it is marked invalidated. Each sweep can carry a 50% zone drawn from the sweeping candle's midpoint to its close.
Show Sweeps While Forming draws a sweep the moment the wick clears the level, styled faintly, before the candle closes. When that candle does close, the sweep is either promoted to its solid style or removed entirely if the body ended up closing back through. Forming sweeps repaint by nature — that is what "forming" means — which is why the option is off by default.
**Settings**
① Structure Timeframes — TF1 and TF2 for the structure read
② ZigZag — length, body or wick, color, width, legs kept
③ Structure Labels — per-timeframe pivot Left/Right, break evaluation and source, four independent level colors, label options
④ Sweep Timeframes — three slots plus how many prior candles each is compared against
⑤ Sweep Detection — highs, lows, forming sweeps and their styling
⑥ Sweep Display — line style, invalidated sweeps, timeframe tags, 50% zones, extension
⑦ Status Table — position, size, alignment tint
**Alerts**
Structure shift on either timeframe, timeframes becoming aligned bullish or bearish, timeframe conflict opening, and a new higher-timeframe sweep forming.
**Suggested starting points**
1-minute futures: Structure TF1 blank with Left 5 / Right 2, TF2 = 15 with Left 4 / Right 2, ZigZag length 1, sweeps on 5 and 15
5-minute: TF1 blank, TF2 = 30, ZigZag length 2, sweeps on 15 and 60
Daily swing trading: TF1 blank, TF2 = W, ZigZag length 3, Left 8 / Right 3, sweeps on 1D and 1W
**Notes on repainting**
Confirmed sweeps do not repaint. Structure pivots do not repaint once confirmed, but by definition cannot confirm until Right bars have printed after them. The final unconfirmed leg of the zigzag updates until its pivot confirms, which is inherent to how zigzags work. Forming sweeps repaint until their candle closes, and the option is off by default for that reason.
Sweep timeframes must be higher than your chart timeframe; slots set at or below it are skipped automatically.
This is an analysis tool. It describes where price has been and which levels are currently in play. It does not predict what happens next, and nothing here is financial advice.
**SHORT DESCRIPTION**
Market structure built from candle bodies instead of wicks, read across two timeframes at once, combined with higher-timeframe liquidity sweep detection that requires the body to close back inside the swept level.
---
**FULL DESCRIPTION**
Two ideas drive this indicator, and both are about ignoring wicks when they lie and paying attention to them when they matter.
**Structure from bodies**
Most structure tools build swings from highs and lows. One wick — a stop run, a thin print during a quiet hour — can define a swing point and drag the entire structure read with it.
This builds swings from candle bodies by default: open and close, not high and low. Levels reflect where price was accepted rather than where it briefly poked. A wick-based mode is included if you want to compare.
The zigzag traces a continuous line through body pivots. At length 1 it puts a vertex at nearly every bar and stretches into longer legs only where price runs without pulling back, so the shape itself shows you where the market trended and where it chopped.
Structure labels run on their own, slower pivot setting so they stay readable while the zigzag stays dense. Every confirmed pivot is compared to the previous pivot in the same direction and labeled HH, HL, LH, or LL. When price breaks a structure level it prints BOS if the break continues the existing bias, or CHoCH if it reverses it.
**Pivots with separate Left and Right bars**
This is where most structure indicators quietly cost you time.
A pivot cannot confirm until bars have printed after it. If left and right are tied to one number, improving quality always costs you speed. Here they are separate: Left decides how significant a swing must be, Right decides how long you wait. Left 5 / Right 2 keeps a meaningful filter while confirming in two bars instead of five.
Evaluate Breaks On removes the second source of lag. Set to Chart Bar, a break of a higher-timeframe level registers the moment price crosses it on your chart rather than waiting for that timeframe's bar to close — worth up to fourteen bars on a 1-minute chart reading 15-minute structure. Set to TF Close for fewer false breaks.
**Dual-timeframe alignment**
Structure TF 1 drives everything drawn on the chart. Structure TF 2 runs the same logic on a higher timeframe and feeds the table. Each has its own pivot settings and its own level colors.
The table compares them row by row — bias, last event, last pivot, active swing high and low — and then reports:
ALIGNED ▲ — both timeframes bullish
ALIGNED ▼ — both timeframes bearish
CONFLICT — they disagree
Conflict is the row worth watching. It means the faster timeframe has flipped while the slower one hasn't confirmed, which is where counter-trend entries tend to get run over.
**Higher-timeframe sweeps**
A sweep is a candle whose wick clears a prior candle's high or low while its body closes back inside. Body top is the close on an up candle and the open on a down candle, so a bar that opens beyond the level and reverses is correctly rejected — a close-only test would pass it.
Higher-timeframe candles are assembled bar by bar from your chart rather than requested. Each HTF high and low keeps the bar where it actually printed, so sweep lines anchor to the wick that made the level instead of to wherever the HTF bar happened to close. Nothing is pulled through request.security for the sweep engine, so there is no repainting question on confirmed sweeps.
Three independent sweep timeframes, each with its own toggle, timeframe and color, fully separate from the structure timeframes. A sweep stays live and extends right until a later candle's body closes through it, at which point it is marked invalidated. Each sweep can carry a 50% zone drawn from the sweeping candle's midpoint to its close.
Show Sweeps While Forming draws a sweep the moment the wick clears the level, styled faintly, before the candle closes. When that candle does close, the sweep is either promoted to its solid style or removed entirely if the body ended up closing back through. Forming sweeps repaint by nature — that is what "forming" means — which is why the option is off by default.
**Settings**
① Structure Timeframes — TF1 and TF2 for the structure read
② ZigZag — length, body or wick, color, width, legs kept
③ Structure Labels — per-timeframe pivot Left/Right, break evaluation and source, four independent level colors, label options
④ Sweep Timeframes — three slots plus how many prior candles each is compared against
⑤ Sweep Detection — highs, lows, forming sweeps and their styling
⑥ Sweep Display — line style, invalidated sweeps, timeframe tags, 50% zones, extension
⑦ Status Table — position, size, alignment tint
**Alerts**
Structure shift on either timeframe, timeframes becoming aligned bullish or bearish, timeframe conflict opening, and a new higher-timeframe sweep forming.
**Suggested starting points**
1-minute futures: Structure TF1 blank with Left 5 / Right 2, TF2 = 15 with Left 4 / Right 2, ZigZag length 1, sweeps on 5 and 15
5-minute: TF1 blank, TF2 = 30, ZigZag length 2, sweeps on 15 and 60
Daily swing trading: TF1 blank, TF2 = W, ZigZag length 3, Left 8 / Right 3, sweeps on 1D and 1W
**Notes on repainting**
Confirmed sweeps do not repaint. Structure pivots do not repaint once confirmed, but by definition cannot confirm until Right bars have printed after them. The final unconfirmed leg of the zigzag updates until its pivot confirms, which is inherent to how zigzags work. Forming sweeps repaint until their candle closes, and the option is off by default for that reason.
Sweep timeframes must be higher than your chart timeframe; slots set at or below it are skipped automatically.
This is an analysis tool. It describes where price has been and which levels are currently in play. It does not predict what happens next, and nothing here is financial advice.
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.
Skrypt open-source
W zgodzie z duchem TradingView twórca tego skryptu udostępnił go jako open-source, aby użytkownicy mogli przejrzeć i zweryfikować jego działanie. Ukłony dla autora. Korzystanie jest bezpłatne, jednak ponowna publikacja kodu podlega naszym Zasadom serwisu.
Wyłączenie odpowiedzialności
Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.