本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mikey369
//
// OBIM - Order Blocks with Imbalance: multi-timeframe order blocks, breakers and stars
//
// Inspired by the teachings of White Phoenix. This is an independent, open-source
// build written from the general smart money concept of order blocks, with breaker
// blocks and a star filter added.
//
// HOW A BLOCK IS FOUND (on every higher timeframe in the active group)
// 1. The last opposite candle before a move away from it (last down candle
// before an up move, last up candle before a down move).
// 2. The move must leave a clean three candle gap (no wick overlap). No gap,
// no block.
// 3. The box spans that candle's full wick, from its start, to the latest bar.
// The wick of the impulse candle that follows is included. The box stops on
// the first wick that touches it (mitigated) and can be dimmed or removed.
// 4. The chart's own timeframe is always scanned. A block fully inside a
// same-direction block from a higher timeframe is hidden while that higher
// block is clean.
//
// STARS
// A block gets a star when it broke external structure (its move closed beyond the
// live external level: the extreme after a real pullback, the first wicked level
// when Staircase Breaks is off, or the protected swing for a ChoCh) and has not
// been pre-mitigated. Pre-mitigated means price hovered
// over it: a second entry, a long stay inside, or a weak exit. Starred blocks get
// a solid border and a stronger fill, plus the mark. Everything else stays plain.
//
// BREAKERS
// A block that price closes through is not simply deleted. If the candle that
// closes through it also leaves a clean gap, the block flips polarity and stays
// on the chart as a breaker.
//@version=6
indicator("OBIM - Order Blocks with Imbalance", shorttitle = "OBIM", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
// ─── Inputs ────────────────────────────────────────────────────────────────
g_logic = "Block Logic"
g_win = "Stars"
g_tf = "Timeframe Groups"
g_brk = "Breakers"
g_vis = "Visuals"
g_lvl = "Entry Lines"
// Structure Rules — locked to match the current Structure_v5 defaults, hidden from the Inputs menu.
st_start = "Auto"
st_inside = false
st_two = true
st_pull = "Wick"
st_brk = "Close"
st_stair = true
max_per_tf = input.int(60, "Max Active Blocks per TF", minval = 5, maxval = 60, group = g_logic, tooltip = "Oldest active block on a timeframe is dropped past this number.")
atr_on = input.bool(true, "ATR Size Filter", group = g_logic, tooltip = "Discard blocks smaller than a percentage of ATR(14) of the block's own timeframe. Filters micro zones.")
atr_pct = input.float(5.0, "Min Block Size (% of ATR)", minval = 0.0, step = 1.0, group = g_logic)
doji_on = input.bool(true, "Count Dojis", group = g_logic, tooltip = "The block candle always has to close the strict opposite way from the move: a down candle before an up move, an up candle before a down move — no exceptions. On: a candle that closes exactly flat (open equals close, so it has no color of its own to be wrong) can also count, for either direction. On by default.")
mit_mode = input.string("Tap", "Mitigation Trigger", options = ["Tap", "Close", "Wick"], group = g_logic, tooltip = "Tap: the first wick that touches the block ends it. Close: a bar must close through the far side. Wick: a wick must pierce the far side. Breakers follow the same trigger: once price comes back and the breaker holds, it is done.")
hide_inside = input.bool(true, "Hide Blocks Inside Higher-TF Blocks", group = g_logic, tooltip = "A block fully inside a same-direction block from a higher timeframe is hidden while that higher block is clean. If the higher block gets pre-mitigated or mitigated, the lower one shows again.")
win_on = input.bool(true, "Highlight Stars", group = g_win, tooltip = "A block gets a star when it broke structure and has not been pre-mitigated: price has not hovered over it. Starred blocks get a solid border, a stronger fill and the mark.")
win_mark = input.string("★", "Star Mark", group = g_win)
q_dwell = input.int(3, "Max Bars Inside A Tap", minval = 1, maxval = 20, group = g_win, tooltip = "Price may enter a block once and must leave within this many chart bars. A longer stay, or a second entry, means the block is pre-mitigated.")
q_force = input.float(0.5, "Exit Candle Body (x Average)", minval = 0.0, step = 0.1, group = g_win, tooltip = "The candle that first leaves the block must close back outside it with a body of at least this multiple of the 14-bar average body.")
grp_scalp = input.bool(true, "Chart 1m - 14m", group = g_tf, tooltip = "When the chart is 1m to 14m, scans 1m, 2m, 3m, 5m, 10m, 15m.")
grp_int = input.bool(true, "Chart 15m - 3h", group = g_tf, tooltip = "When the chart is 15m to 3h, scans 15m, 30m, 45m, 1h, 2h, 3h.")
grp_swing = input.bool(true, "Chart 4h - 11h", group = g_tf, tooltip = "When the chart is 4h to 11h, scans 2h, 3h, 4h, 6h, 8h, 12h.")
grp_super = input.bool(true, "Chart 12h - 1M", group = g_tf, tooltip = "When the chart is 12h to 1M, scans 18h, 1D, 3D, 1W, 1M.")
show_brk = input.bool(true, "Show Breakers", group = g_brk, tooltip = "When a block is closed through by a candle that leaves a clean gap, flip it into a breaker instead of removing it.")
brk_contain = input.bool(true, "Gap Must Cover The Block", group = g_brk, tooltip = "On: the gap left by the breaking candle must fully cover the block (the block sits inside the gap). Off: any overlap with the block is enough.")
mit_view = input.string("Dim last per TF", "Mitigated Blocks", options = ["Remove", "Dim last per TF", "Dim all"], group = g_vis, tooltip = "What happens to a mitigated block. Remove: it disappears. Dim: it stays as a gray box that stops where it was mitigated (last one per timeframe, or all of them).")
c_dem_fill = input.color(color.new(#2962FF, 93), "Demand Fill", group = g_vis)
c_dem_bd = input.color(#2962FF, "Demand Border", group = g_vis)
c_dem_lbl = input.color(#B8B8B8, "Demand Label", group = g_vis, tooltip = "The TF tag on the block (1h, 2h, ...). Its own color, independent of the border.")
c_sup_fill = input.color(color.new(#F23645, 93), "Supply Fill", group = g_vis)
c_sup_bd = input.color(#F23645, "Supply Border", group = g_vis)
c_sup_lbl = input.color(#B8B8B8, "Supply Label", group = g_vis, tooltip = "The TF tag on the block. Its own color, independent of the border.")
c_bb_fill = input.color(color.new(#00BFA5, 93), "Bullish Breaker Fill", group = g_vis)
c_bb_bd = input.color(#00BFA5, "Bullish Breaker Border", group = g_vis)
c_bb_lbl = input.color(#FFF59D, "Bullish Breaker Label", group = g_vis, tooltip = "The TF tag on the block. Its own color, independent of the border.")
c_bs_fill = input.color(color.new(#FF9800, 93), "Bearish Breaker Fill", group = g_vis)
c_bs_bd = input.color(#FF9800, "Bearish Breaker Border", group = g_vis)
c_bs_lbl = input.color(#FFF59D, "Bearish Breaker Label", group = g_vis, tooltip = "The TF tag on the block. Its own color, independent of the border.")
c_mit_fill = input.color(color.new(#787B86, 92), "Mitigated Fill", group = g_vis)
c_mit_bd = input.color(color.new(#464646, 50), "Mitigated Border", group = g_vis)
c_mit_lbl = input.color(#787B86, "Mitigated Label", group = g_vis, tooltip = "The TF tag on a mitigated block. Its own color, independent of the border.")
bd_style = input.string("Solid", "Border Style", options = ["Dotted", "Solid", "Dashed"], group = g_vis, tooltip = "Plain Demand/Supply blocks only. Breakers, Stars and Mitigated each have their own border style below.")
star_bdSty = input.string("Solid", "Star Border Style", options = ["Dotted", "Solid", "Dashed"], group = g_vis)
brk_bdSty = input.string("Dashed", "Breaker Border Style", options = ["Dotted", "Solid", "Dashed"], group = g_vis)
mit_bdSty = input.string("Solid", "Mitigated Border Style", options = ["Dotted", "Solid", "Dashed"], group = g_vis)
bd_w = input.int(1, "Border Width", minval = 1, maxval = 4, group = g_vis)
ext_on = input.bool(false, "Extend Unmitigated Blocks to Right", group = g_vis, tooltip = "Pushes the right edge of every still-active block past the current bar, instead of stopping right at it.")
ext_len = input.int(50, "Extend Length (bars)", minval = 1, maxval = 500, group = g_vis, tooltip = "How far past the current bar an active block's box extends, when the option above is on.")
txt_size = input.string("Small", "Tag Size", options = ["Tiny", "Small", "Normal"], group = g_vis)
lvl_showLine = input.bool(true, "Show Line", group = g_lvl, tooltip = "A ray off the entry trigger of every block still on the chart (the edge price would hit first: bottom of a demand block, top of a supply block), running right off the edge of the chart — not limited to a handful of blocks the way True Price Scale Tags is. Switches look when the block flips to a breaker (its own color/style below); a starred block keeps its Demand / Supply line, the star mark on the box is enough. Gone as soon as the block mitigates — mitigated blocks get no line or tag.")
lvl_w = input.int(1, "Line Width", minval = 1, maxval = 4, group = g_lvl, tooltip = "Shared by all block types below.")
lvl_demCol = input.color(color.new(#2962FF, 70), "Demand Line", group = g_lvl, inline = "lvd")
lvl_demSty = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], group = g_lvl, inline = "lvd")
lvl_supCol = input.color(color.new(#F23645, 70), "Supply Line", group = g_lvl, inline = "lvs")
lvl_supSty = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], group = g_lvl, inline = "lvs")
lvl_bbCol = input.color(color.new(#00BFA5, 70), "Bullish Breaker Line", group = g_lvl, inline = "lvbb")
lvl_bbSty = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], group = g_lvl, inline = "lvbb")
lvl_bsCol = input.color(color.new(#FF9800, 70), "Bearish Breaker Line", group = g_lvl, inline = "lvbs")
lvl_bsSty = input.string("Solid", "", options = ["Solid", "Dashed", "Dotted"], group = g_lvl, inline = "lvbs")
lvl_showTag = input.bool(true, "Show Price Tag", group = g_lvl, inline = "lvt", tooltip = "A label parked past the right edge of the chart, colored to match the block (not a real docked price-scale tag — see True Price Scale Tags below for that, with its own limit). Independent of Show Line, so you can turn the line off and keep just the tag.")
lvl_tagOff = input.int(100, "Offset (bars)", minval = 0, maxval = 500, group = g_lvl, inline = "lvt", tooltip = "How many bars past the latest candle the price tag sits.")
lvl_scaleOn = input.bool(true, "True Price Scale Tags (Nearest 8)", group = g_lvl, tooltip = "Real tags docked to the price scale itself, using plot(), colored to match each block. Pine only gives a fixed, small number of these (one per plot call), so it's capped to the 8 active blocks closest to the current price — not the 8 newest, so a fresh block far from price won't bump a closer, more relevant one off the list. Works alongside Show Price Tag; turn that off if two tags per block is too busy.")
// ─── Types and shared state ────────────────────────────────────────────────
type OBox
box bx
int uid
int dir
float top
float bot
int tf
int mins
string tag
bool active
bool isBrk
int wait
bool bos
int visits
int inBars
bool messy
int hiddenBy
line lvLn
label lvLb
var array<OBox> obs = array.new<OBox>()
var array<int> lastEv = array.new<int>(7, 0)
var int nextId = 0
// ─── External structure engine (same rules as the Structure indicator) ─────
// Swings alternate. A pullback only counts once it is real: inside bars skipped, two
// consecutive pullback candles (the second trading beyond the first) and a close out
// of the mother bar. A pullback swing registers only when price makes a new extreme.
// A wick through the extreme that closes back inside is a sweep: the wick becomes the
// new extreme. With Staircase Breaks off, the level that was first wicked still breaks
// structure on the first close beyond it.
// Returns the two live break levels after this bar:
// lvH = level a close above would break (protected high in a downtrend = ChoCh,
// the extreme in an uptrend once a real pullback exists = BOS)
// lvL = the mirror for a close below.
stCore(string sm, bool inOn, bool twoOn, string pm, string bm, bool stairOn) =>
int nNeed = twoOn ? 2 : 1
var int trend = 0
var float ext = na
var float extOpp = na
var float pbX = na
var float pbXo = na
var bool pbOk = false
var float refH = na
var float refL = na
var int pb = 0
var float prot = na
var float hi0 = na
var float lo0 = na
var float brkLvl = na
if trend == 0
int d0 = 0
float pv = na
if sm == "Start Bullish"
d0 := 1
pv := low
else if sm == "Start Bearish"
d0 := -1
pv := high
else
if na(hi0)
hi0 := high
lo0 := low
else
if close > hi0
d0 := 1
pv := lo0
else if close < lo0
d0 := -1
pv := hi0
else
hi0 := math.max(hi0, high)
lo0 := math.min(lo0, low)
if d0 != 0
trend := d0
prot := pv
ext := d0 == 1 ? high : low
extOpp := d0 == 1 ? low : high
refH := high
refL := low
pb := 0
pbOk := false
pbX := na
pbXo := na
else
int s = trend
// staircase off: a close beyond the first wicked level is the break
if not stairOn and not na(brkLvl) and (s == 1 ? close > brkLvl : close < brkLvl)
brkLvl := na
bool chHit = bm == "Close" ? (s == 1 ? close < prot : close > prot) : (s == 1 ? low < prot : high > prot)
if chHit
float nx = s == 1 ? low : high
float nxo = s == 1 ? high : low
if not na(pbX) and (s == 1 ? pbX < nx : pbX > nx)
nx := pbX
nxo := pbXo
prot := ext
ext := nx
extOpp := nxo
refH := math.max(nx, nxo)
refL := math.min(nx, nxo)
pb := 0
pbOk := false
pbX := na
pbXo := na
brkLvl := na
trend := -s
else
bool beyond = s == 1 ? high > ext : low < ext
if beyond
if pbOk
bool cb = bm == "Close" ? (s == 1 ? close > ext : close < ext) : true
if cb
brkLvl := na
else if not stairOn and na(brkLvl)
brkLvl := ext
prot := pbX
ext := s == 1 ? high : low
extOpp := s == 1 ? low : high
refH := high
refL := low
pb := 0
pbOk := false
pbX := na
pbXo := na
else
if na(pbX) or (s == 1 ? low < pbX : high > pbX)
pbX := s == 1 ? low : high
pbXo := s == 1 ? high : low
bool inside = inOn and high <= refH and low >= refL
if not inside
bool counter = pm == "Close" ? (s == 1 ? close < refL : close > refH) : (s == 1 ? low < refL : high > refH)
if counter
pb += 1
else
pb := 0
refH := high
refL := low
bool deep = pm == "Close" ? (s == 1 ? close < extOpp : close > extOpp) : (s == 1 ? low < extOpp : high > extOpp)
if pb >= nNeed and deep
pbOk := true
bool wk = not stairOn and not na(brkLvl)
float lvH = trend == -1 ? prot : (trend == 1 ? (wk ? brkLvl : (pbOk ? ext : na)) : na)
float lvL = trend == 1 ? prot : (trend == -1 ? (wk ? brkLvl : (pbOk ? ext : na)) : na)
[lvH, lvL]
// ─── Higher timeframe engine ───────────────────────────────────────────────
// Runs in the requested timeframe. Candle k = two bars back, impulse candle =
// one bar back, gap candle = this bar. Emits at most one new block per bar
// through evId. evBos = 1 when the move closed beyond the last swing.
obCore(bool useAtr, float atrPct, bool dojiOn, string sm, bool inOn, bool twoOn, string pm, string bm, bool stairOn) =>
var int evId = 0
var int evDir = 0
var float evTop = na
var float evBot = na
var int evLeft = 0
var int evBos = 0
float atrV = ta.atr(14)
[lvH, lvL] = stCore(sm, inOn, twoOn, pm, bm, stairOn)
// levels as they stood before the impulse candle (state after the bar two back)
float lastSH = lvH[2]
float lastSL = lvL[2]
float ko = open[2]
float kc = close[2]
float kh = high[2]
float kl = low[2]
float io = open[1]
float ic = close[1]
// A doji here is a candle that closed exactly flat (no color of its own to be wrong). Every other
// candle must still close the strict opposite way from the move: Demand always needs a down candle,
// Supply always needs an up candle, no exceptions.
bool kDoji = dojiOn and kc == ko
bool sizeK = not useAtr or na(atrV) or (kh - kl) >= atrPct / 100.0 * atrV
if not na(kh) and sizeK
if (kc < ko or kDoji) and ic > io and low > kh
evId += 1
evDir := 1
evTop := kh
evBot := math.min(kl, low[1])
evLeft := time[2]
evBos := (not na(lastSH) and lastSH > kh and math.max(ic, close) > lastSH) ? 1 : 0
else if (kc > ko or kDoji) and ic < io and high < kl
evId += 1
evDir := -1
evTop := math.max(kh, high[1])
evBot := kl
evLeft := time[2]
evBos := (not na(lastSL) and lastSL < kl and math.min(ic, close) < lastSL) ? 1 : 0
[evId, evDir, evTop, evBot, evLeft, evBos]
// Closed bars only: every value is shifted one HTF bar, so nothing repaints.
obShifted(bool useAtr, float atrPct, bool dojiOn, string sm, bool inOn, bool twoOn, string pm, string bm, bool stairOn) =>
[a, b, c, d, e, f] = obCore(useAtr, atrPct, dojiOn, sm, inOn, twoOn, pm, bm, stairOn)
[a[1], b[1], c[1], d[1], e[1], f[1]]
// ─── Timeframe groups ──────────────────────────────────────────────────────
int chartSec = timeframe.in_seconds()
int barMs = chartSec * 1000
// A monthly chart reports 28 to 31 days; treat any of them as exactly one month
int chartSecN = chartSec >= 2419200 ? 2592000 : chartSec
bool gScalp = grp_scalp and chartSec >= 60 and chartSec < 900
bool gInt = grp_int and chartSec >= 900 and chartSec < 14400
bool gSwing = grp_swing and chartSec >= 14400 and chartSec < 43200
bool gSuper = grp_super and chartSec >= 43200 and (chartSec <= 604800 or chartSec >= 2419200)
// Minutes for each of the six slots in the active group (0 = unused)
f_min(int s) =>
int r = 0
if gScalp
r := s == 0 ? 1 : s == 1 ? 2 : s == 2 ? 3 : s == 3 ? 5 : s == 4 ? 10 : s == 5 ? 15 : 0
else if gInt
r := s == 0 ? 15 : s == 1 ? 30 : s == 2 ? 45 : s == 3 ? 60 : s == 4 ? 120 : s == 5 ? 180 : 0
else if gSwing
r := s == 0 ? 120 : s == 1 ? 180 : s == 2 ? 240 : s == 3 ? 360 : s == 4 ? 480 : s == 5 ? 720 : 0
else if gSuper
r := s == 0 ? 1080 : s == 1 ? 1440 : s == 2 ? 4320 : s == 3 ? 10080 : s == 4 ? 43200 : 0
r
f_tfStr(int m) =>
string r = timeframe.period
if m >= 43200
r := "1M"
else if m >= 10080
r := "1W"
else if m >= 1440
r := str.tostring(int(m / 1440)) + "D"
else if m > 0
r := str.tostring(m)
r
f_tag(int m) =>
string r = ""
if m >= 43200
r := "1M"
else if m >= 10080
r := "1w"
else if m >= 1440
r := str.tostring(int(m / 1440)) + "d"
else if m >= 60 and m % 60 == 0
r := str.tostring(int(m / 60)) + "h"
else
r := str.tostring(m) + "m"
r
int m0 = f_min(0)
int m1 = f_min(1)
int m2 = f_min(2)
int m3 = f_min(3)
int m4 = f_min(4)
int m5 = f_min(5)
int chartMin = int(chartSec / 60)
int m6 = chartSec >= 2419200 ? 43200 : chartSec >= 60 and chartSec <= 604800 ? chartMin : 0
// A slot is live when it is in use and not lower than the chart timeframe
bool on0 = m0 > 0 and m0 * 60 >= chartSecN
bool on1 = m1 > 0 and m1 * 60 >= chartSecN
bool on2 = m2 > 0 and m2 * 60 >= chartSecN
bool on3 = m3 > 0 and m3 * 60 >= chartSecN
bool on4 = m4 > 0 and m4 * 60 >= chartSecN
bool on5 = m5 > 0 and m5 * 60 >= chartSecN
// The chart's own timeframe is always scanned, unless a group slot already covers it
bool dup6 = (on0 and m0 == m6) or (on1 and m1 == m6) or (on2 and m2 == m6) or (on3 and m3 == m6) or (on4 and m4 == m6) or (on5 and m5 == m6)
bool on6 = m6 > 0 and not dup6
bool anyLive = on0 or on1 or on2 or on3 or on4 or on5 or on6
string tf0 = on0 ? f_tfStr(m0) : timeframe.period
string tf1 = on1 ? f_tfStr(m1) : timeframe.period
string tf2 = on2 ? f_tfStr(m2) : timeframe.period
string tf3 = on3 ? f_tfStr(m3) : timeframe.period
string tf4 = on4 ? f_tfStr(m4) : timeframe.period
string tf5 = on5 ? f_tfStr(m5) : timeframe.period
string tf6 = timeframe.period
[id0, dr0, tp0, bt0, lf0, bs0] = request.security(syminfo.tickerid, tf0, obShifted(atr_on, atr_pct, doji_on, st_start, st_inside, st_two, st_pull, st_brk, st_stair), lookahead = barmerge.lookahead_on)
[id1, dr1, tp1, bt1, lf1, bs1] = request.security(syminfo.tickerid, tf1, obShifted(atr_on, atr_pct, doji_on, st_start, st_inside, st_two, st_pull, st_brk, st_stair), lookahead = barmerge.lookahead_on)
[id2, dr2, tp2, bt2, lf2, bs2] = request.security(syminfo.tickerid, tf2, obShifted(atr_on, atr_pct, doji_on, st_start, st_inside, st_two, st_pull, st_brk, st_stair), lookahead = barmerge.lookahead_on)
[id3, dr3, tp3, bt3, lf3, bs3] = request.security(syminfo.tickerid, tf3, obShifted(atr_on, atr_pct, doji_on, st_start, st_inside, st_two, st_pull, st_brk, st_stair), lookahead = barmerge.lookahead_on)
[id4, dr4, tp4, bt4, lf4, bs4] = request.security(syminfo.tickerid, tf4, obShifted(atr_on, atr_pct, doji_on, st_start, st_inside, st_two, st_pull, st_brk, st_stair), lookahead = barmerge.lookahead_on)
[id5, dr5, tp5, bt5, lf5, bs5] = request.security(syminfo.tickerid, tf5, obShifted(atr_on, atr_pct, doji_on, st_start, st_inside, st_two, st_pull, st_brk, st_stair), lookahead = barmerge.lookahead_on)
[id6, dr6, tp6, bt6, lf6, bs6] = request.security(syminfo.tickerid, tf6, obShifted(atr_on, atr_pct, doji_on, st_start, st_inside, st_two, st_pull, st_brk, st_stair), lookahead = barmerge.lookahead_on)
// ─── Drawing helpers ───────────────────────────────────────────────────────
f_bdStyle() =>
bd_style == "Solid" ? line.style_solid : bd_style == "Dashed" ? line.style_dashed : line.style_dotted
f_styleOf(string s) =>
s == "Solid" ? line.style_solid : s == "Dotted" ? line.style_dotted : line.style_dashed
f_txtSize() =>
txt_size == "Tiny" ? size.tiny : txt_size == "Normal" ? size.normal : size.small
string bdSty = f_bdStyle()
string txtSz = f_txtSize()
float avgBody = ta.sma(math.abs(close - open), 14)
f_newBox(int dir, float top, float bot, int leftT, string tag) =>
color fillC = dir == 1 ? c_dem_fill : c_sup_fill
color bdC = dir == 1 ? c_dem_bd : c_sup_bd
color lblC = dir == 1 ? c_dem_lbl : c_sup_lbl
box.new(left = leftT, top = top, right = time + barMs, bottom = bot, xloc = xloc.bar_time, border_color = bdC, border_style = bdSty, border_width = bd_w, bgcolor = fillC, text = tag, text_color = lblC, text_size = txtSz, text_halign = text.align_right, text_valign = text.align_center)
// The block's identifying color (border-type color), used by the entry line's price tag. na while hidden.
f_curColor(OBox o) =>
color r = na
if o.hiddenBy < 0 and o.active
r := o.isBrk ? (o.dir == 1 ? c_bb_bd : c_bs_bd) : (o.dir == 1 ? c_dem_bd : c_sup_bd)
r
// The entry line's own color and style for this block's current type. na color while hidden or mitigated
// (mitigated blocks get no line or tag at all).
f_lvlLook(OBox o) =>
color c = na
string st = line.style_solid
if o.hiddenBy < 0 and o.active
if o.isBrk
c := o.dir == 1 ? lvl_bbCol : lvl_bsCol
st := o.dir == 1 ? f_styleOf(lvl_bbSty) : f_styleOf(lvl_bsSty)
else
c := o.dir == 1 ? lvl_demCol : lvl_supCol
st := o.dir == 1 ? f_styleOf(lvl_demSty) : f_styleOf(lvl_supSty)
[c, st]
// Applies the right look to a block: hidden, mitigated, breaker, starred or plain
f_style(OBox o) =>
if o.hiddenBy >= 0
box.set_bgcolor(o.bx, na)
box.set_border_color(o.bx, na)
box.set_text_color(o.bx, na)
else if not o.active
box.set_bgcolor(o.bx, c_mit_fill)
box.set_border_color(o.bx, c_mit_bd)
box.set_text_color(o.bx, c_mit_lbl)
box.set_text(o.bx, o.tag)
box.set_border_style(o.bx, f_styleOf(mit_bdSty))
box.set_border_width(o.bx, bd_w)
else if o.isBrk
color fcB = o.dir == 1 ? c_bb_fill : c_bs_fill
color bcB = o.dir == 1 ? c_bb_bd : c_bs_bd
color lcB = o.dir == 1 ? c_bb_lbl : c_bs_lbl
bool cleanB = win_on and not o.messy
box.set_bgcolor(o.bx, cleanB ? color.new(bcB, 78) : fcB)
box.set_border_color(o.bx, bcB)
box.set_text_color(o.bx, lcB)
box.set_text(o.bx, o.tag + (cleanB ? " BRK " + win_mark : " BRK"))
box.set_border_style(o.bx, f_styleOf(brk_bdSty))
box.set_border_width(o.bx, cleanB ? bd_w + 1 : bd_w)
else if win_on and o.bos and not o.messy
color bcW = o.dir == 1 ? c_dem_bd : c_sup_bd
color lcW = o.dir == 1 ? c_dem_lbl : c_sup_lbl
box.set_bgcolor(o.bx, color.new(bcW, 78))
box.set_border_color(o.bx, bcW)
box.set_text_color(o.bx, lcW)
box.set_text(o.bx, o.tag + " " + win_mark)
box.set_border_style(o.bx, f_styleOf(star_bdSty))
box.set_border_width(o.bx, bd_w + 1)
else
color fcN = o.dir == 1 ? c_dem_fill : c_sup_fill
color bcN = o.dir == 1 ? c_dem_bd : c_sup_bd
color lcN = o.dir == 1 ? c_dem_lbl : c_sup_lbl
box.set_bgcolor(o.bx, fcN)
box.set_border_color(o.bx, bcN)
box.set_text_color(o.bx, lcN)
box.set_text(o.bx, o.tag)
box.set_border_style(o.bx, bdSty)
box.set_border_width(o.bx, bd_w)
// the entry line / tag follow the near-edge price (swaps side on a breaker flip), color and style.
// x-position (the connector stub, off the block, and the tag offset) is kept current separately, every bar, in
// section 3 below — same as how the box's own right edge is kept current.
float nearPx = o.dir == 1 ? o.top : o.bot
color tagC = f_curColor(o)
[lnC, lnSt] = f_lvlLook(o)
if not na(o.lvLn)
line.set_y1(o.lvLn, nearPx)
line.set_y2(o.lvLn, nearPx)
line.set_color(o.lvLn, lvl_showLine ? lnC : na)
line.set_style(o.lvLn, lnSt)
line.set_width(o.lvLn, lvl_w)
if not na(o.lvLb)
label.set_y(o.lvLb, nearPx)
label.set_textcolor(o.lvLb, na(tagC) ? na : tagC)
label.set_text(o.lvLb, lvl_showTag and not na(tagC) ? str.tostring(nearPx, format.mintick) : "")
0
// Deletes a block's box, entry line and price tag together (the line/tag never outlive the box)
f_delOb(OBox o) =>
box.delete(o.bx)
if not na(o.lvLn)
line.delete(o.lvLn)
if not na(o.lvLb)
label.delete(o.lvLb)
0
// uid of a clean, higher-timeframe, same-direction block that fully covers b (or -1)
f_cover(OBox b) =>
int r = -1
if array.size(obs) > 0
for i = 0 to array.size(obs) - 1
OBox a = array.get(obs, i)
if a.uid != b.uid and a.active and not a.isBrk and not a.messy and a.hiddenBy < 0 and a.dir == b.dir and a.mins > b.mins and a.bot <= b.bot and a.top >= b.top
r := a.uid
r
// A block stopped covering others (pre-mitigated, mitigated, flipped or dropped): release what it hid
f_release(int uid) =>
if array.size(obs) > 0
for i = 0 to array.size(obs) - 1
OBox x = array.get(obs, i)
if x.hiddenBy == uid
x.hiddenBy := -1
x.hiddenBy := f_cover(x)
f_style(x)
0
// ─── Chart side: create, grade, mitigate, flip ─────────────────────────────
array<int> evA = array.from(nz(id0), nz(id1), nz(id2), nz(id3), nz(id4), nz(id5), nz(id6))
array<int> dirA = array.from(nz(dr0), nz(dr1), nz(dr2), nz(dr3), nz(dr4), nz(dr5), nz(dr6))
array<float> topA = array.from(tp0, tp1, tp2, tp3, tp4, tp5, tp6)
array<float> botA = array.from(bt0, bt1, bt2, bt3, bt4, bt5, bt6)
array<int> leftA = array.from(nz(lf0), nz(lf1), nz(lf2), nz(lf3), nz(lf4), nz(lf5), nz(lf6))
array<int> bosA = array.from(nz(bs0), nz(bs1), nz(bs2), nz(bs3), nz(bs4), nz(bs5), nz(bs6))
array<bool> onA = array.from(on0, on1, on2, on3, on4, on5, on6)
array<string> tagA = array.from(f_tag(m0), f_tag(m1), f_tag(m2), f_tag(m3), f_tag(m4), f_tag(m5), f_tag(m6))
array<int> minA = array.from(m0, m1, m2, m3, m4, m5, m6)
if barstate.isconfirmed
// 1) New blocks from each live timeframe slot
for s = 0 to 6
int id = array.get(evA, s)
if bar_index == 0
array.set(lastEv, s, id)
else if id > array.get(lastEv, s)
array.set(lastEv, s, id)
if array.get(onA, s)
int d = array.get(dirA, s)
float tpv = array.get(topA, s)
float btv = array.get(botA, s)
int ltv = array.get(leftA, s)
if (d == 1 or d == -1) and not na(tpv) and not na(btv) and ltv > 0
string tg = array.get(tagA, s)
int mn = array.get(minA, s)
int uid = nextId
nextId += 1
box nb = f_newBox(d, tpv, btv, ltv, tg)
float nearPx0 = d == 1 ? tpv : btv
color lnCol0 = d == 1 ? lvl_demCol : lvl_supCol
string lnSty0 = f_styleOf(d == 1 ? lvl_demSty : lvl_supSty)
line lvLn0 = line.new(bar_index, nearPx0, bar_index + 1, nearPx0, xloc = xloc.bar_index, color = lnCol0, style = lnSty0, width = lvl_w, extend = extend.right)
label lvLb0 = label.new(bar_index + lvl_tagOff, nearPx0, "", xloc = xloc.bar_index, yloc = yloc.price, style = label.style_label_left, color = color.new(#000000, 100), textcolor = na, size = txtSz)
OBox nob = OBox.new(nb, uid, d, tpv, btv, s, mn, tg, true, false, -1, array.get(bosA, s) == 1, 0, 0, false, -1, lvLn0, lvLb0)
array.push(obs, nob)
if hide_inside
nob.hiddenBy := f_cover(nob)
if nob.hiddenBy < 0
for c = 0 to array.size(obs) - 2
OBox qc = array.get(obs, c)
if qc.active and not qc.isBrk and qc.hiddenBy < 0 and qc.dir == d and qc.mins < mn and qc.bot >= btv and qc.top <= tpv
qc.hiddenBy := uid
f_style(qc)
f_style(nob)
int cnt = 0
for k = 0 to array.size(obs) - 1
OBox qk = array.get(obs, k)
if qk.active and qk.tf == s
cnt += 1
int j = 0
while cnt > max_per_tf and j < array.size(obs)
OBox qj = array.get(obs, j)
if qj.active and qj.tf == s
int dj = qj.uid
f_delOb(qj)
array.remove(obs, j)
f_release(dj)
cnt -= 1
else
j += 1
while array.size(obs) > 480
OBox qo = array.get(obs, 0)
int dq = qo.uid
f_delOb(qo)
array.remove(obs, 0)
f_release(dq)
// 2) Existing blocks (a new block is checked on its own bar): breaker check, mitigation, tap grading
int wi = array.size(obs) - 1
while wi >= 0
OBox o = array.get(obs, wi)
if o.active
bool toMit = false
if o.wait >= 0
if bar_index == o.wait + 1
bool gapOk = false
if o.dir == 1
float gTop = low[2]
float gBot = high
gapOk := gBot < gTop and (brk_contain ? (gBot <= o.bot and gTop >= o.top) : (gBot < o.top and gTop > o.bot))
else
float gBot2 = high[2]
float gTop2 = low
gapOk := gTop2 > gBot2 and (brk_contain ? (gBot2 <= o.bot and gTop2 >= o.top) : (gBot2 < o.top and gTop2 > o.bot))
if gapOk
o.dir := -o.dir
o.isBrk := true
o.wait := -1
o.messy := false
o.visits := 0
o.inBars := 0
o.hiddenBy := -1
f_style(o)
f_release(o.uid)
else
toMit := true
else
string mm = mit_mode
bool mit = false
if o.dir == 1
mit := mm == "Tap" ? low <= o.top : mm == "Wick" ? low < o.bot : close < o.bot
else
mit := mm == "Tap" ? high >= o.bot : mm == "Wick" ? high > o.top : close > o.top
if mit
bool cand = show_brk and not o.isBrk and (o.dir == 1 ? close < o.bot : close > o.top)
if cand
o.wait := bar_index
else
toMit := true
else
// Tap grading: one quick V in and out is fine, hovering is not
bool ov = low <= o.top and high >= o.bot
bool nowMessy = false
if ov
if o.inBars == 0
o.visits += 1
o.inBars += 1
if o.visits >= 2 or o.inBars > q_dwell
nowMessy := true
else if o.inBars > 0
bool forced = (o.dir == 1 ? close > o.top : close < o.bot) and math.abs(close - open) >= q_force * avgBody
if not forced
nowMessy := true
o.inBars := 0
if nowMessy and not o.messy
o.messy := true
f_style(o)
f_release(o.uid)
if toMit
int dead = o.uid
if mit_view != "Remove" and o.hiddenBy < 0
if mit_view == "Dim last per TF"
// keep only the last mitigated block per timeframe
int di = array.size(obs) - 1
while di >= 0
OBox q = array.get(obs, di)
if di != wi and not q.active and q.tf == o.tf
f_delOb(q)
array.remove(obs, di)
if di < wi
wi -= 1
di -= 1
o.active := false
o.wait := -1
box.set_right(o.bx, time)
f_style(o)
else
f_delOb(o)
array.remove(obs, wi)
f_release(dead)
wi -= 1
// 3) Keep active boxes reaching the latest bar
if barstate.islast and array.size(obs) > 0
for e = 0 to array.size(obs) - 1
OBox oe = array.get(obs, e)
if oe.active
box.set_right(oe.bx, time + (ext_on ? ext_len + 1 : 1) * barMs)
if not na(oe.lvLb)
label.set_x(oe.lvLb, bar_index + lvl_tagOff)
// True Price Scale Tags: Pine only gives one axis label per plot() call, so this is capped to 8. Picked by
// distance to the current close, nearest first, not by recency — the ones actually worth watching right now.
var float sv1 = na
var float sv2 = na
var float sv3 = na
var float sv4 = na
var float sv5 = na
var float sv6 = na
var float sv7 = na
var float sv8 = na
var color sc1 = na
var color sc2 = na
var color sc3 = na
var color sc4 = na
var color sc5 = na
var color sc6 = na
var color sc7 = na
var color sc8 = na
if barstate.islast and lvl_scaleOn and array.size(obs) > 0
sv1 := na
sv2 := na
sv3 := na
sv4 := na
sv5 := na
sv6 := na
sv7 := na
sv8 := na
array<float> apx = array.new<float>(0)
array<color> acl = array.new<color>(0)
array<float> adi = array.new<float>(0)
for i = 0 to array.size(obs) - 1
OBox so = array.get(obs, i)
if so.active and so.hiddenBy < 0
float sPx = so.dir == 1 ? so.top : so.bot
array.push(apx, sPx)
array.push(acl, f_curColor(so))
array.push(adi, math.abs(sPx - close))
int sCnt = 0
while sCnt < 8 and array.size(adi) > 0
float mv = array.get(adi, 0)
int mIdx = 0
for j = 1 to array.size(adi) - 1
float v = array.get(adi, j)
if v < mv
mv := v
mIdx := j
float sPx2 = array.get(apx, mIdx)
color sCl2 = array.get(acl, mIdx)
sCnt += 1
if sCnt == 1
sv1 := sPx2
sc1 := sCl2
else if sCnt == 2
sv2 := sPx2
sc2 := sCl2
else if sCnt == 3
sv3 := sPx2
sc3 := sCl2
else if sCnt == 4
sv4 := sPx2
sc4 := sCl2
else if sCnt == 5
sv5 := sPx2
sc5 := sCl2
else if sCnt == 6
sv6 := sPx2
sc6 := sCl2
else if sCnt == 7
sv7 := sPx2
sc7 := sCl2
else if sCnt == 8
sv8 := sPx2
sc8 := sCl2
array.remove(apx, mIdx)
array.remove(acl, mIdx)
array.remove(adi, mIdx)
plot(lvl_scaleOn ? sv1 : na, "Entry Scale Tag 1", sc1, display = display.price_scale)
plot(lvl_scaleOn ? sv2 : na, "Entry Scale Tag 2", sc2, display = display.price_scale)
plot(lvl_scaleOn ? sv3 : na, "Entry Scale Tag 3", sc3, display = display.price_scale)
plot(lvl_scaleOn ? sv4 : na, "Entry Scale Tag 4", sc4, display = display.price_scale)
plot(lvl_scaleOn ? sv5 : na, "Entry Scale Tag 5", sc5, display = display.price_scale)
plot(lvl_scaleOn ? sv6 : na, "Entry Scale Tag 6", sc6, display = display.price_scale)
plot(lvl_scaleOn ? sv7 : na, "Entry Scale Tag 7", sc7, display = display.price_scale)
plot(lvl_scaleOn ? sv8 : na, "Entry Scale Tag 8", sc8, display = display.price_scale)
// Note when this chart timeframe has no enabled timeframe
var table warn = table.new(position.bottom_right, 1, 1)
if barstate.islast
table.cell(warn, 0, 0, anyLive ? "" : "No block timeframe is active for this chart", text_color = color.gray, text_size = size.small)