本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// © jan80hansen
//@version=6
indicator("Order Block Delta Engine", overlay = true, max_boxes_count = 500)
// -----------------------------------------------------------------------------
// 1. INPUTS
// -----------------------------------------------------------------------------
gp_ob = "Order Block Logic"
swing_len = input.int(10, "Swing Lookback", minval = 2, group = gp_ob, tooltip = "Bars needed to confirm a swing point.")
max_obs = input.int(5, "Max Active OBs per side", minval = 1, group = gp_ob)
del_broken = input.bool(true, "Delete Broken Order Blocks", group = gp_ob)
gp_style = "Visuals & Data"
bull_bg = input.color(color.new(#00e676, 85), "Bullish OB", group = gp_style)
bear_bg = input.color(color.new(#ff1744, 85), "Bearish OB", group = gp_style)
border_col = input.color(color.new(#ffffff, 50), "Border Color", group = gp_style)
// -----------------------------------------------------------------------------
// 2. TYPES & DATA STRUCTURES
// -----------------------------------------------------------------------------
type OrderBlock
box ob_box
float top
float bottom
bool is_bullish
bool mitigated
var OrderBlock[] active_obs = array.new<OrderBlock>()
// Volum-akkumulatorer (Nullstilles ved hver nye OB)
var float accum_bull = 0.0
var float accum_bear = 0.0
// Format numbers for clean display (e.g., 1.2M, 450K)
format_vol(float v) =>
if v >= 1000000
str.format("{0,number,#.##}M", v / 1000000)
else if v >= 1000
str.format("{0,number,#.##}K", v / 1000)
else
str.format("{0,number,#}", v)
// -----------------------------------------------------------------------------
// 3. ORDER BLOCK DETECTION & INTRA-BAR DELTA LOGIC
// -----------------------------------------------------------------------------
p_high = ta.pivothigh(high, swing_len, swing_len)
p_low = ta.pivotlow(low, swing_len, swing_len)
// Nullstill volum-telleren når en ny struktur (OB) er bekreftet
if not na(p_low) or not na(p_high)
accum_bull := 0.0
accum_bear := 0.0
// EKTE DELTA-BEREGNING (Fordeler volumet INNAD i candlen)
float range_total = high - low
float buy_vol_bar = 0.0
float sell_vol_bar = 0.0
if range_total > 0
buy_vol_bar := volume * ((close - low) / range_total)
sell_vol_bar := volume * ((high - close) / range_total)
else
buy_vol_bar := close >= open ? volume : 0.0
sell_vol_bar := close < open ? volume : 0.0
accum_bull += buy_vol_bar
accum_bear += sell_vol_bar
// --- BULLISH OB LOGIKK ---
if not na(p_low)
int ob_index = swing_len
if close[swing_len] > open[swing_len] and close[swing_len + 1] < open[swing_len + 1]
ob_index := swing_len + 1
float top_price = math.max(open[ob_index], close[ob_index])
float btm_price = low[ob_index]
box b = box.new(left = bar_index - ob_index, top = top_price, right = bar_index, bottom = btm_price, bgcolor = bull_bg, border_color = border_col)
OrderBlock new_ob = OrderBlock.new(b, top_price, btm_price, true, false)
array.unshift(active_obs, new_ob)
// --- BEARISH OB LOGIKK ---
if not na(p_high)
int ob_index = swing_len
if close[swing_len] < open[swing_len] and close[swing_len + 1] > open[swing_len + 1]
ob_index := swing_len + 1
float top_price = high[ob_index]
float btm_price = math.min(open[ob_index], close[ob_index])
box b = box.new(left = bar_index - ob_index, top = top_price, right = bar_index, bottom = btm_price, bgcolor = bear_bg, border_color = border_col)
OrderBlock new_ob = OrderBlock.new(b, top_price, btm_price, false, false)
array.unshift(active_obs, new_ob)
// Clean up old boxes
if array.size(active_obs) > (max_obs * 2)
OrderBlock old_ob = array.pop(active_obs)
box.delete(old_ob.ob_box)
// -----------------------------------------------------------------------------
// 4. MITIGATION & INVALIDATION (Live Tracking)
// -----------------------------------------------------------------------------
if array.size(active_obs) > 0
for i = array.size(active_obs) - 1 to 0 by 1
OrderBlock ob = array.get(active_obs, i)
box.set_right(ob.ob_box, bar_index)
bool touched = ob.is_bullish ? low <= ob.top : high >= ob.bottom
if touched and not ob.mitigated
ob.mitigated := true
color faded_bg = ob.is_bullish ? color.new(bull_bg, 92) : color.new(bear_bg, 92)
box.set_bgcolor(ob.ob_box, faded_bg)
box.set_border_color(ob.ob_box, color.new(border_col, 90))
bool broken = ob.is_bullish ? close < ob.bottom : close > ob.top
if del_broken and broken
box.delete(ob.ob_box)
array.remove(active_obs, i)
// -----------------------------------------------------------------------------
// 5. ELEGANT REAL-TIME VOLUME DASHBOARD
// -----------------------------------------------------------------------------
var table flow_tbl = table.new(position.top_right, 2, 4, bgcolor = color.new(#131722, 10), border_color = color.new(#363a45, 50), border_width = 1)
if barstate.islast
float net_delta = accum_bull - accum_bear
string bias_txt = net_delta > 0 ? "BULLISH ⬆" : net_delta < 0 ? "BEARISH ⬇" : "NEUTRAL"
color bias_col = net_delta > 0 ? color.new(#00e676, 0) : net_delta < 0 ? color.new(#ff1744, 0) : color.gray
table.cell(flow_tbl, 0, 0, "FLOW SINCE LAST OB", text_color = color.gray, text_size = size.small, text_halign = text.align_left)
table.cell(flow_tbl, 1, 0, "", text_color = color.gray, text_size = size.small)
table.cell(flow_tbl, 0, 1, "Buy Contracts:", text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(flow_tbl, 1, 1, format_vol(accum_bull), text_color = color.new(#00e676, 0), text_size = size.small, text_halign = text.align_right)
table.cell(flow_tbl, 0, 2, "Sell Contracts:", text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(flow_tbl, 1, 2, format_vol(accum_bear), text_color = color.new(#ff1744, 0), text_size = size.small, text_halign = text.align_right)
table.cell(flow_tbl, 0, 3, "Current Bias:", text_color = color.white, text_size = size.small, text_halign = text.align_left)
table.cell(flow_tbl, 1, 3, bias_txt, text_color = bias_col, text_size = size.small, text_halign = text.align_right)