本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Breakout Quality Analyzer (Wyckoff VSA)", shorttitle="BQ_Wyckoff_v3.2", overlay=false)
// BREAKOUT QUALITY ANALYZER v3.2
// v3.1: VSA column rebuilt with bars-ago recency tracking
// v3.2: Added HIGHER-TIMEFRAME (HTF) confirmation using the MTF
// stable-snapshot technique (non-repainting request.security
// wrapper: force _commit=true inside a *_HTF() function, offset
// every returned field by [1], call via lookahead_on). All
// original v3.1 logic below is unchanged. New material is in
// section "8. HIGHER TIMEFRAME CONTEXT" and everything tagged
// "NEW in v3.2" below.
// INPUTS
grp_bo = "1. BREAKOUT DETECTION"
bo_lookback = input.int(20, "Pivot Lookback", group=grp_bo, minval=5, maxval=50)
bo_vol_mult = input.float(1.3, "Min Breakout Volume (x avg)", group=grp_bo, minval=1.0, maxval=5.0, step=0.1)
grp_vsa = "2. VSA SETTINGS"
vol_avg_len = input.int(50, "Volume Average Length", group=grp_vsa, minval=10, maxval=200)
absorption_len = input.int(20, "Pre-Breakout Absorption Window", group=grp_vsa, minval=5, maxval=50)
grp_cvd = "3. CVD DIVERGENCE"
cvd_lookback = input.int(3, "CVD Slope Lookback (bars)", group=grp_cvd, minval=2, maxval=10)
grp_hidden = "4. HIDDEN SIGNAL"
hidden_buy_thresh = input.float(0.55, "Hidden Buy: Min DV Ratio on red bar", group=grp_hidden, minval=0.51, maxval=0.75, step=0.01)
hidden_sell_thresh = input.float(0.45, "Hidden Sell: Max DV Ratio on green bar", group=grp_hidden, minval=0.25, maxval=0.49, step=0.01)
grp_follow = "5. FOLLOW-THROUGH"
ft_days = input.int(3, "Follow-Through Days to Track", group=grp_follow, minval=1, maxval=10)
grp_tw = "6. TOM WILLIAMS VSA ENGINE"
tw_vol_low_mult = input.float(0.7, "Low Volume Threshold (x avg)", group=grp_tw, minval=0.3, maxval=0.95, step=0.05)
tw_vol_high_mult = input.float(1.5, "High Volume Threshold (x avg)", group=grp_tw, minval=1.1, maxval=3.0, step=0.1)
tw_spread_narrow = input.float(0.5, "Narrow Spread Threshold (ATR mult)", group=grp_tw, minval=0.2, maxval=1.0, step=0.05)
tw_spread_wide = input.float(1.2, "Wide Spread Threshold (ATR mult)", group=grp_tw, minval=0.8, maxval=3.0, step=0.1)
tw_close_high = input.float(0.6, "Close-High Threshold (pct of range)", group=grp_tw, minval=0.5, maxval=0.9, step=0.05)
tw_close_low = input.float(0.4, "Close-Low Threshold (pct of range)", group=grp_tw, minval=0.1, maxval=0.5, step=0.05)
tw_support_lb = input.int(10, "Support/Resistance Lookback", group=grp_tw, minval=5, maxval=30)
tw_weakness_lb = input.int(5, "Prior Weakness Lookback (No Demand gate)", group=grp_tw, minval=2, maxval=15)
show_vsa_labels = input.bool(true, "Show VSA Labels on Chart", group=grp_tw)
show_vsa_bg = input.bool(true, "Show VSA Background Shading", group=grp_tw)
grp_disp = "7. DISPLAY"
show_bo_table = input.bool(true, "Show Breakout Scorecard", group=grp_disp)
show_labels = input.bool(true, "Show Verdict Labels on Chart", group=grp_disp)
// NEW in v3.2 -------------------------------------------------------
grp_htf = "8. HIGHER TIMEFRAME CONTEXT (MTF)"
htf_enable = input.bool(true, "Enable HTF Confirmation", group=grp_htf)
htf_tf = input.timeframe("W", "Higher Timeframe", group=grp_htf, tooltip="Must be strictly higher than the chart timeframe. Uses a non-repainting stable snapshot: reads only the last CLOSED higher-timeframe bar, never the still-forming one.")
htf_bo_lookback = input.int(20, "HTF Pivot Lookback", group=grp_htf, minval=5, maxval=50)
htf_vol_mult = input.float(1.3, "HTF Min Breakout Volume (x avg)", group=grp_htf, minval=1.0, maxval=5.0, step=0.1)
show_htf_table = input.bool(true, "Show HTF Context Table", group=grp_htf)
// ---------------------------------------------------------------------
// CORE CALCULATIONS
vol_avg = ta.sma(volume, vol_avg_len)
spread = high - low
avg_spread = ta.sma(spread, vol_avg_len)
pivot_high = ta.highest(high, bo_lookback)[1]
is_breakout = (close > pivot_high) and (close[1] <= pivot_high)
bo_volume_ok = volume > (vol_avg * bo_vol_mult)
breakout_bar = is_breakout and bo_volume_ok
// MODULE 1: EFFORT vs RESULT
effort = volume / vol_avg
result = spread / avg_spread
evr_ratio = effort > 0 ? (result / effort) : 0.0
evr_score = evr_ratio > 1.2 ? 3 : (evr_ratio > 0.9 ? 2 : (evr_ratio > 0.6 ? 1 : 0))
// MODULE 2: CLOSE POSITION
close_pct = spread > 0 ? ((close - low) / spread) : 0.5
close_score = close_pct > 0.75 ? 3 : (close_pct > 0.60 ? 2 : (close_pct > 0.40 ? 1 : 0))
// MODULE 3: PRE-BREAKOUT ABSORPTION
avg_vol_pre = ta.sma(volume, absorption_len)[1]
vol_ratio_pre = vol_avg > 0 ? (avg_vol_pre / vol_avg) : 1.0
vol_dryup_before = vol_ratio_pre < 0.85
avg_spread_pre = ta.sma(spread, absorption_len)[1]
spread_ratio_pre = avg_spread > 0 ? (avg_spread_pre / avg_spread) : 1.0
range_contracted = spread_ratio_pre < 0.85
accum_day = (close > close[1]) and (volume > vol_avg) ? 1.0 : 0.0
distrib_day = (close < close[1]) and (volume > vol_avg) ? 1.0 : 0.0
pre_accum = ta.sma(accum_day, absorption_len) * absorption_len
pre_distrib = ta.sma(distrib_day, absorption_len) * absorption_len
pre_ad_ratio = pre_distrib > 0 ? (pre_accum / pre_distrib) : 2.0
pre_ad_healthy = pre_ad_ratio > 1.0
abs_s1 = vol_dryup_before ? 1 : 0
abs_s2 = range_contracted ? 1 : 0
abs_s3 = pre_ad_healthy ? 1 : 0
absorption_score = abs_s1 + abs_s2 + abs_s3
// MODULE 4: VOLUME CHARACTER CHANGE
vol_surge_magnitude = avg_vol_pre > 0 ? (volume / avg_vol_pre) : 1.0
vol_char_score = vol_surge_magnitude > 2.5 ? 3 : (vol_surge_magnitude > 1.8 ? 2 : (vol_surge_magnitude > 1.3 ? 1 : 0))
// MODULE 5: VOLUME RANK
highest_vol_20 = ta.highest(volume, bo_lookback)
vol_is_highest = volume >= highest_vol_20
highest_vol_10_prev = ta.highest(volume, 10)[1]
vol_rank_score = vol_is_highest ? 2 : (volume > highest_vol_10_prev ? 1 : 0)
// MODULE 6: SPREAD QUALITY
spread_vs_avg = avg_spread > 0 ? (spread / avg_spread) : 1.0
spread_score = spread_vs_avg > 1.5 ? 2 : (spread_vs_avg > 1.0 ? 1 : 0)
// MODULE 7: CVD DIVERGENCE
bar_spread_cvd = high - low
bar_buy_vol = bar_spread_cvd > 0 ? (volume * (close - low) / bar_spread_cvd) : (volume * 0.5)
bar_sell_vol = bar_spread_cvd > 0 ? (volume * (high - close) / bar_spread_cvd) : (volume * 0.5)
bar_delta = bar_buy_vol - bar_sell_vol
cvd_raw = ta.cum(bar_delta)
cvd_change = cvd_raw - nz(cvd_raw[cvd_lookback], cvd_raw)
price_change = close - nz(close[cvd_lookback], close)
cvd_div_bull = (cvd_change > 0) and (price_change < 0)
cvd_div_bear = (cvd_change < 0) and (price_change > 0)
cvd_confirms = (cvd_change > 0) and (price_change > 0)
cvd_diverges = cvd_div_bear
cvd_score = cvd_confirms ? 2 : (cvd_diverges ? 0 : 1)
cvd_chg_pre = nz(cvd_raw[1]) - nz(cvd_raw[6], cvd_raw[1])
price_chg_pre = nz(close[1]) - nz(close[6], close[1])
cvd_stealth_accum = (cvd_chg_pre > 0) and (price_chg_pre <= 0)
cvd_bonus = cvd_stealth_accum ? 1 : 0
cvd_total_score = cvd_score + cvd_bonus
// MODULE 8: HIDDEN SIGNAL DV FLOW
dv_buy_vol = spread > 0 ? (volume * (close - low) / spread) : (volume * 0.5)
dv_sell_vol = spread > 0 ? (volume * (high - close) / spread) : (volume * 0.5)
dv_total = dv_buy_vol + dv_sell_vol
dv_ratio = dv_total > 0 ? (dv_buy_vol / dv_total) : 0.5
is_green_bar = close > open
is_red_bar = close < open
hidden_buy = (dv_ratio > hidden_buy_thresh) and is_red_bar
hidden_sell = (dv_ratio < hidden_sell_thresh) and is_green_bar
hidden_score = dv_ratio > 0.65 ? 3 : (dv_ratio > 0.55 ? 2 : (dv_ratio > 0.45 ? 1 : 0))
hb_1 = hidden_buy[1] == true ? 1.0 : 0.0
hb_2 = hidden_buy[2] == true ? 1.0 : 0.0
hb_3 = hidden_buy[3] == true ? 1.0 : 0.0
recent_hidden_buys = hb_1 + hb_2 + hb_3
hidden_base_accum = recent_hidden_buys >= 1
// MODULE 9: TOM WILLIAMS VSA ENGINE
atr_val = ta.atr(14)
tw_bar_spread = high - low
tw_spread_ratio = tw_bar_spread / (atr_val > 0.0 ? atr_val : 1.0)
tw_close_pos = tw_bar_spread > 0.0 ? (close - low) / tw_bar_spread : 0.5
tw_vol_ratio = volume / (vol_avg > 0.0 ? vol_avg : 1.0)
tw_bar_bull = close >= open
tw_bar_bear = close < open
tw_vol_is_low = tw_vol_ratio < tw_vol_low_mult
tw_vol_is_high = tw_vol_ratio > tw_vol_high_mult
tw_spread_is_narrow = tw_spread_ratio < tw_spread_narrow
tw_spread_is_wide = tw_spread_ratio > tw_spread_wide
tw_close_near_hi = tw_close_pos >= tw_close_high
tw_close_near_lo = tw_close_pos <= tw_close_low
local_support = ta.lowest(low, tw_support_lb)[1]
local_resistance = ta.highest(high, tw_support_lb)[1]
// Buying Climax detection for No Demand gate
var bool bc_detected = false
bc_detected := false
for w = 1 to tw_weakness_lb
bc_bar_spread = high[w] - low[w]
bc_close_pos = bc_bar_spread > 0.0 ? (close[w] - low[w]) / bc_bar_spread : 0.5
bc_spread_rat = atr_val[w] > 0.0 ? bc_bar_spread / atr_val[w] : 0.0
if close[w] > open[w] and volume[w] > vol_avg[w] * 2.0 and bc_spread_rat > 1.2 and bc_close_pos < 0.40
bc_detected := true
// End of Rising detection for No Demand gate
var bool eor_detected = false
eor_detected := false
for w = 1 to tw_weakness_lb
eor_bar_spread = high[w] - low[w]
eor_prior_spread = high[w+1] - low[w+1]
eor_close_pos = eor_bar_spread > 0.0 ? (close[w] - low[w]) / eor_bar_spread : 0.5
if close[w] > open[w] and volume[w] > vol_avg[w] * 1.5 and eor_bar_spread < eor_prior_spread and eor_close_pos < 0.50
eor_detected := true
prior_weakness = bc_detected or eor_detected
no_supply = tw_bar_bear and tw_vol_is_low and tw_spread_is_narrow and tw_close_near_hi
no_demand = tw_bar_bull and tw_vol_is_low and tw_spread_is_narrow and tw_close_near_lo and (bc_detected or eor_detected)
shakeout = low < local_support and close > local_support and tw_vol_is_high and tw_close_near_hi and tw_spread_is_wide
upthrust = high > local_resistance and close < local_resistance and tw_close_near_lo and tw_spread_is_wide
tw_score = 0
tw_score := tw_score + (no_supply ? 2 : 0)
tw_score := tw_score + (shakeout ? 3 : 0)
tw_score := tw_score - (no_demand ? 2 : 0)
tw_score := tw_score - (upthrust ? 3 : 0)
tw_score := math.max(tw_score, -5)
// GAP-AWARE VSA SIGNS OF STRENGTH AND WEAKNESS
vsa_gap_size = math.abs(open - close[1])
vsa_total_spread = tw_bar_spread + vsa_gap_size
vsa_spread_ratio = vsa_total_spread / (atr_val > 0.0 ? atr_val : 1.0)
vsa_total_hi = math.max(high, close[1])
vsa_total_lo = math.min(low, close[1])
vsa_total_rng = vsa_total_hi - vsa_total_lo
vsa_full_pos = vsa_total_rng > 0.0 ? (close - vsa_total_lo) / vsa_total_rng : tw_close_pos
vsa_spread_contracting = vsa_total_spread < (math.abs(close[1] - close[2]) + (high[1] - low[1]))
// Signs of Strength
vsa_sc = tw_bar_bear and tw_vol_ratio > 1.3 and vsa_spread_ratio > 0.9 and vsa_full_pos > 0.55
vsa_sv_bar1_spd = high[2] - low[2]
vsa_sv_bar2_spd = high[1] - low[1]
vsa_sv_bar3_spd = high - low
vsa_sv_spread_dec = vsa_sv_bar1_spd > vsa_sv_bar2_spd and vsa_sv_bar2_spd > vsa_sv_bar3_spd
vsa_sv_vol_inc = volume[2] < volume[1] and volume[1] < volume
vsa_sv_all_dn = close[2] < open[2] and close[1] < open[1] and tw_bar_bear
vsa_sv = vsa_sv_all_dn and vsa_sv_spread_dec and vsa_sv_vol_inc and vsa_full_pos > 0.40 and not vsa_sc
vsa_br = tw_bar_bear and tw_vol_ratio > 1.1 and vsa_full_pos > 0.60 and not vsa_sc and not vsa_sv
vsa_prior_flush = (close[1] < open[1]) and (volume[1] > vol_avg[1] * 1.3) and ((close[1] - low[1]) / math.max(high[1] - low[1], 0.001) > 0.40)
vsa_bh = tw_bar_bull and tw_vol_ratio > 1.2 and vsa_full_pos > 0.60 and vsa_prior_flush
vsa_sa = tw_bar_bull and tw_vol_ratio > 1.3 and vsa_spread_ratio < 1.1 and vsa_full_pos >= 0.30 and vsa_full_pos <= 0.80
// Signs of Weakness
vsa_bc = tw_bar_bull and tw_vol_ratio > 1.3 and vsa_spread_ratio > 0.9 and vsa_full_pos < 0.45
vsa_eor = tw_bar_bull and tw_vol_ratio > 1.3 and vsa_spread_contracting and vsa_full_pos < 0.50 and not vsa_bc
vsa_tr = tw_bar_bull and tw_vol_ratio > 1.1 and vsa_full_pos < 0.40 and not vsa_bc and not vsa_eor
vsa_sod = tw_bar_bear and volume > volume[1] * 1.1 and close[1] >= open[1] and vsa_full_pos < 0.40
vsa_da = tw_bar_bear and tw_vol_ratio > 1.3 and vsa_spread_ratio < 1.1 and vsa_full_pos >= 0.20 and vsa_full_pos <= 0.70
// ========================================
// BARS-AGO TRACKING (NEW in v3.1)
// Tracks recency of every VSA signal
// ========================================
var int ago_sc = 999
var int ago_sv = 999
var int ago_br = 999
var int ago_bh = 999
var int ago_sa = 999
var int ago_bc = 999
var int ago_eor = 999
var int ago_tr = 999
var int ago_sod = 999
var int ago_da = 999
var int ago_ns = 999
var int ago_shk = 999
var int ago_ut = 999
var int ago_nd = 999
var int ago_hb = 999
var int ago_hs = 999
ago_sc := vsa_sc ? 0 : ago_sc + 1
ago_sv := vsa_sv ? 0 : ago_sv + 1
ago_br := vsa_br ? 0 : ago_br + 1
ago_bh := vsa_bh ? 0 : ago_bh + 1
ago_sa := vsa_sa ? 0 : ago_sa + 1
ago_bc := vsa_bc ? 0 : ago_bc + 1
ago_eor := vsa_eor ? 0 : ago_eor + 1
ago_tr := vsa_tr ? 0 : ago_tr + 1
ago_sod := vsa_sod ? 0 : ago_sod + 1
ago_da := vsa_da ? 0 : ago_da + 1
ago_ns := no_supply ? 0 : ago_ns + 1
ago_shk := shakeout ? 0 : ago_shk + 1
ago_ut := upthrust ? 0 : ago_ut + 1
ago_nd := no_demand ? 0 : ago_nd + 1
ago_hb := hidden_buy ? 0 : ago_hb + 1
ago_hs := hidden_sell ? 0 : ago_hs + 1
// Best recent strength signal
best_sos_ago = math.min(ago_sc, math.min(ago_sv, math.min(ago_br, math.min(ago_bh, math.min(ago_sa, math.min(ago_ns, ago_shk))))))
best_sos_name = ago_sc == best_sos_ago ? "SC" : (ago_sv == best_sos_ago ? "SV" : (ago_shk == best_sos_ago ? "SHK" : (ago_br == best_sos_ago ? "BR" : (ago_bh == best_sos_ago ? "BH" : (ago_sa == best_sos_ago ? "SA" : (ago_ns == best_sos_ago ? "NS" : "---"))))))
// Best recent weakness signal
best_sow_ago = math.min(ago_bc, math.min(ago_eor, math.min(ago_tr, math.min(ago_sod, math.min(ago_da, math.min(ago_ut, ago_nd))))))
best_sow_name = ago_bc == best_sow_ago ? "BC" : (ago_eor == best_sow_ago ? "EoR" : (ago_ut == best_sow_ago ? "UT" : (ago_tr == best_sow_ago ? "TR" : (ago_sod == best_sow_ago ? "SoD" : (ago_da == best_sow_ago ? "DA" : (ago_nd == best_sow_ago ? "ND" : "---"))))))
// COMPOSITE SCORE (28 pts max)
raw_score = evr_score + close_score + absorption_score + vol_char_score + vol_rank_score + spread_score + cvd_total_score + hidden_score + tw_score
raw_score := math.max(raw_score, 0)
max_score = 28
bq_pct = raw_score * 100.0 / max_score
is_accumulation = bq_pct >= 70
is_likely_accum = (bq_pct >= 50) and (bq_pct < 70)
is_uncertain = (bq_pct >= 35) and (bq_pct < 50)
is_exhaustion = bq_pct < 35
// LATCHED SCORES (captured at breakout bar)
var float latch_bq_pct = 0.0
var int latch_raw_score = 0
var float latch_evr_ratio = 0.0
var int latch_evr_score = 0
var float latch_close_pct = 0.0
var int latch_close_score = 0
var int latch_absorption = 0
var float latch_vol_surge = 0.0
var int latch_vol_char = 0
var bool latch_vol_highest = false
var int latch_vol_rank = 0
var float latch_spread_vs_avg = 0.0
var int latch_spread_score = 0
var bool latch_cvd_confirms = false
var bool latch_cvd_diverges = false
var bool latch_cvd_stealth = false
var int latch_cvd_total = 0
var float latch_dv_ratio = 0.0
var int latch_hidden_score = 0
var bool latch_hidden_buy = false
var bool latch_hidden_sell = false
var int latch_tw_score = 0
var bool latch_no_supply = false
var bool latch_shakeout = false
var bool latch_no_demand = false
var bool latch_upthrust = false
var bool latch_bc = false
var bool latch_eor = false
var bool latch_is_accum = false
var bool latch_is_likely = false
var bool latch_is_uncertain = false
var bool latch_is_exhaust = false
var string latch_date = "No breakout yet"
if breakout_bar
latch_bq_pct := bq_pct
latch_raw_score := raw_score
latch_evr_ratio := evr_ratio
latch_evr_score := evr_score
latch_close_pct := close_pct
latch_close_score := close_score
latch_absorption := absorption_score
latch_vol_surge := vol_surge_magnitude
latch_vol_char := vol_char_score
latch_vol_highest := vol_is_highest
latch_vol_rank := vol_rank_score
latch_spread_vs_avg := spread_vs_avg
latch_spread_score := spread_score
latch_cvd_confirms := cvd_confirms
latch_cvd_diverges := cvd_diverges
latch_cvd_stealth := cvd_stealth_accum
latch_cvd_total := cvd_total_score
latch_dv_ratio := dv_ratio
latch_hidden_score := hidden_score
latch_hidden_buy := hidden_buy
latch_hidden_sell := hidden_sell
latch_tw_score := tw_score
latch_no_supply := no_supply
latch_shakeout := shakeout
latch_no_demand := no_demand
latch_upthrust := upthrust
latch_bc := bc_detected
latch_eor := eor_detected
latch_is_accum := is_accumulation
latch_is_likely := is_likely_accum
latch_is_uncertain := is_uncertain
latch_is_exhaust := is_exhaustion
latch_date := str.format("{0,date,MMM dd yyyy}", time)
// FOLLOW-THROUGH
var int bars_since_bo = 100
bars_since_bo := breakout_bar ? 0 : (bars_since_bo + 1)
in_followthrough = (bars_since_bo <= ft_days) and (bars_since_bo > 0)
ft_holding = close > pivot_high
ft_vol_sustained = volume > (vol_avg * 0.8)
ft_close_strong = close_pct > 0.50
ft_s1 = ft_holding ? 1 : 0
ft_s2 = ft_vol_sustained ? 1 : 0
ft_s3 = ft_close_strong ? 1 : 0
ft_score = ft_s1 + ft_s2 + ft_s3
// =====================================================================
// NEW in v3.2 — HTF CONTEXT ENGINE (MTF stable-snapshot technique)
//
// A condensed, self-contained engine mirroring Modules 1, 2, 4 and 9
// of the chart-timeframe engine above. Module 9 here is built GAP-AWARE
// (matching the fix already applied to the vsa_* signals above, after
// SNOW's earnings gap-up bars failed to trigger signals) since HTF bars
// are, if anything, MORE likely than daily bars to span an earnings
// gap. The bc/eor lookback loop that gates No Demand is left
// non-gap-aware, matching the original tw_* implementation, to keep
// this addition narrowly scoped to the specific current-bar problem
// that was already documented and fixed elsewhere in this script.
// Modules 3 (CVD), 7 (absorption) and 8 (hidden signal) are
// intentionally left out of the HTF read to keep it fast and
// self-contained — extend f_bqEngineHTF_core below if you want fuller
// parity later.
//
// _commit exists purely so this function can be reused for the
// non-repainting HTF snapshot below: it is always called with
// _commit = true, and the wrapper's [1] offset (not _commit) is what
// actually prevents repainting.
// =====================================================================
f_bqEngineHTF_core(bool _commit) =>
htf_vol_avg = ta.sma(volume, vol_avg_len)
htf_spread = high - low
htf_avg_spread = ta.sma(htf_spread, vol_avg_len)
htf_pivot_high = ta.highest(high, htf_bo_lookback)[1]
htf_is_breakout = (close > htf_pivot_high) and (close[1] <= htf_pivot_high)
htf_bo_vol_ok = volume > (htf_vol_avg * htf_vol_mult)
htf_breakout_bar = htf_is_breakout and htf_bo_vol_ok
// Module 1: Effort vs Result
htf_effort = volume / htf_vol_avg
htf_result = htf_spread / htf_avg_spread
htf_evr_ratio = htf_effort > 0 ? (htf_result / htf_effort) : 0.0
htf_evr_score = htf_evr_ratio > 1.2 ? 3 : (htf_evr_ratio > 0.9 ? 2 : (htf_evr_ratio > 0.6 ? 1 : 0))
// Module 2: Close Position (non-gap, matches original Module 2)
htf_close_pct = htf_spread > 0 ? ((close - low) / htf_spread) : 0.5
htf_close_score = htf_close_pct > 0.75 ? 3 : (htf_close_pct > 0.60 ? 2 : (htf_close_pct > 0.40 ? 1 : 0))
// Module 4: Volume Character Change
htf_avg_vol_pre = ta.sma(volume, absorption_len)[1]
htf_vol_surge = htf_avg_vol_pre > 0 ? (volume / htf_avg_vol_pre) : 1.0
htf_vc_score = htf_vol_surge > 2.5 ? 3 : (htf_vol_surge > 1.8 ? 2 : (htf_vol_surge > 1.3 ? 1 : 0))
// Module 9: Tom Williams VSA Engine (gap-aware)
htf_atr = ta.atr(14)
htf_gap_size = math.abs(open - close[1])
htf_total_spread = htf_spread + htf_gap_size
htf_gap_spread_ratio = htf_total_spread / (htf_atr > 0.0 ? htf_atr : 1.0)
htf_total_hi = math.max(high, close[1])
htf_total_lo = math.min(low, close[1])
htf_total_rng = htf_total_hi - htf_total_lo
htf_gap_close_pos = htf_total_rng > 0.0 ? (close - htf_total_lo) / htf_total_rng : 0.5
htf_vol_ratio = volume / (htf_vol_avg > 0.0 ? htf_vol_avg : 1.0)
htf_bar_bull = close >= open
htf_bar_bear = close < open
htf_vol_is_low = htf_vol_ratio < tw_vol_low_mult
htf_vol_is_high = htf_vol_ratio > tw_vol_high_mult
htf_spread_is_narrow = htf_gap_spread_ratio < tw_spread_narrow
htf_spread_is_wide = htf_gap_spread_ratio > tw_spread_wide
htf_close_near_hi = htf_gap_close_pos >= tw_close_high
htf_close_near_lo = htf_gap_close_pos <= tw_close_low
htf_local_support = ta.lowest(low, tw_support_lb)[1]
htf_local_resistance = ta.highest(high, tw_support_lb)[1]
htf_bc_detected = false
for w = 1 to tw_weakness_lb
w_bar_spread = high[w] - low[w]
w_close_pos = w_bar_spread > 0.0 ? (close[w] - low[w]) / w_bar_spread : 0.5
w_spread_rat = htf_atr[w] > 0.0 ? w_bar_spread / htf_atr[w] : 0.0
if close[w] > open[w] and volume[w] > htf_vol_avg[w] * 2.0 and w_spread_rat > 1.2 and w_close_pos < 0.40
htf_bc_detected := true
htf_eor_detected = false
for w = 1 to tw_weakness_lb
w_bar_spread2 = high[w] - low[w]
w_prior_spread = high[w+1] - low[w+1]
w_close_pos2 = w_bar_spread2 > 0.0 ? (close[w] - low[w]) / w_bar_spread2 : 0.5
if close[w] > open[w] and volume[w] > htf_vol_avg[w] * 1.5 and w_bar_spread2 < w_prior_spread and w_close_pos2 < 0.50
htf_eor_detected := true
htf_no_supply = htf_bar_bear and htf_vol_is_low and htf_spread_is_narrow and htf_close_near_hi
htf_no_demand = htf_bar_bull and htf_vol_is_low and htf_spread_is_narrow and htf_close_near_lo and (htf_bc_detected or htf_eor_detected)
htf_shakeout = low < htf_local_support and close > htf_local_support and htf_vol_is_high and htf_close_near_hi and htf_spread_is_wide
htf_upthrust = high > htf_local_resistance and close < htf_local_resistance and htf_close_near_lo and htf_spread_is_wide
htf_tw_score = 0
htf_tw_score := htf_tw_score + (htf_no_supply ? 2 : 0)
htf_tw_score := htf_tw_score + (htf_shakeout ? 3 : 0)
htf_tw_score := htf_tw_score - (htf_no_demand ? 2 : 0)
htf_tw_score := htf_tw_score - (htf_upthrust ? 3 : 0)
htf_tw_score := math.max(htf_tw_score, -5)
// Composite (0-14): EVR(0-3) + Close(0-3) + VolChar(0-3) + max(TW,0)(0-5)
htf_raw_score_pre = htf_evr_score + htf_close_score + htf_vc_score + math.max(htf_tw_score, 0)
htf_raw_score = math.max(htf_raw_score_pre, 0)
htf_max_score = 14
htf_bq_pct = htf_raw_score * 100.0 / htf_max_score
htf_is_accum = htf_bq_pct >= 70
htf_is_likely = (htf_bq_pct >= 50) and (htf_bq_pct < 70)
htf_is_uncertain = (htf_bq_pct >= 35) and (htf_bq_pct < 50)
htf_verdict = htf_is_accum ? 1 : (htf_is_likely ? 2 : (htf_is_uncertain ? 3 : 4))
// Persistent recency tracking - only advances when _commit
var int htf_ago_shk = 999
var int htf_ago_ns = 999
var int htf_ago_ut = 999
var int htf_ago_nd = 999
var int htf_bars_since_bo = 999
if _commit
htf_ago_shk := htf_shakeout ? 0 : htf_ago_shk + 1
htf_ago_ns := htf_no_supply ? 0 : htf_ago_ns + 1
htf_ago_ut := htf_upthrust ? 0 : htf_ago_ut + 1
htf_ago_nd := htf_no_demand ? 0 : htf_ago_nd + 1
htf_bars_since_bo := htf_breakout_bar ? 0 : htf_bars_since_bo + 1
[htf_breakout_bar, htf_evr_ratio, htf_evr_score, htf_close_pct, htf_close_score, htf_vc_score, htf_no_supply, htf_shakeout, htf_no_demand, htf_upthrust, htf_tw_score, htf_raw_score, htf_bq_pct, htf_verdict, htf_ago_shk, htf_ago_ns, htf_ago_ut, htf_ago_nd, htf_bars_since_bo]
// The MTF stable-snapshot wrapper itself: force _commit = true, then
// offset EVERY returned field by [1]. This is the entire trick.
f_bqEngineHTF() =>
[bo, evrR, evrS, cp, cS, vcS, ns, shk, nd, ut, tw, rs, bqp, verdict, agoShk, agoNs, agoUt, agoNd, bsb] = f_bqEngineHTF_core(true)
[bo[1], evrR[1], evrS[1], cp[1], cS[1], vcS[1], ns[1], shk[1], nd[1], ut[1], tw[1], rs[1], bqp[1], verdict[1], agoShk[1], agoNs[1], agoUt[1], agoNd[1], bsb[1]]
// Guard against an HTF input that isn't actually higher than the
// chart timeframe (request.security with tf <= chart tf is a
// different, unintended use). Falls back to the chart timeframe
// itself (a harmless no-op reading) rather than erroring, and the
// table below flags it clearly.
htf_seconds = timeframe.in_seconds(htf_tf)
chart_seconds = timeframe.in_seconds(timeframe.period)
htf_valid = htf_seconds > chart_seconds
htf_tf_safe = htf_valid ? htf_tf : timeframe.period
[htf_breakout_bar, htf_evr_ratio, htf_evr_score, htf_close_pct, htf_close_score, htf_vc_score, htf_no_supply, htf_shakeout, htf_no_demand, htf_upthrust, htf_tw_score, htf_raw_score, htf_bq_pct, htf_verdict, htf_ago_shk, htf_ago_ns, htf_ago_ut, htf_ago_nd, htf_bars_since_bo] = request.security(syminfo.tickerid, htf_tf_safe, f_bqEngineHTF(), lookahead = barmerge.lookahead_on)
// Alignment check: does the higher timeframe corroborate the LAST
// evaluated breakout's verdict? This is Panel-2-internal confluence,
// one level below the "all three panels must align" rule.
// 0 = HTF off/invalid, 1 = confirms bullish, 2 = conflicts,
// 3 = confirms bearish/avoid, 4 = neutral/mixed
htf_bull_read = htf_verdict == 1 or htf_verdict == 2
htf_bear_read = htf_verdict == 4
chart_bull_latch = latch_is_accum or latch_is_likely
chart_bear_latch = latch_is_exhaust
htf_alignment = (not htf_enable or not htf_valid) ? 0 : (chart_bull_latch and htf_bull_read) ? 1 : (chart_bull_latch and htf_bear_read) ? 2 : (chart_bear_latch and htf_bear_read) ? 3 : 4
// ================== END NEW v3.2 HTF ENGINE BLOCK ===================
// PLOTTING
bq_show = breakout_bar ? bq_pct : na
bq_color = is_accumulation ? color.new(#00E676, 0) : (is_likely_accum ? color.new(#2196F3, 0) : (is_uncertain ? color.new(#FF9800, 0) : color.new(#F44336, 0)))
plot(bq_show, "Breakout Quality %", color=bq_color, style=plot.style_columns, linewidth=4)
ft_show = in_followthrough ? ft_score * 20.0 : na
ft_color = ft_score >= 2 ? color.new(#00E676, 40) : (ft_score == 1 ? color.new(#FF9800, 40) : color.new(#F44336, 40))
plot(ft_show, "Follow-Through", color=ft_color, style=plot.style_columns, linewidth=2)
h75 = hline(75, "Accumulation Zone", color=color.new(#00E676, 70), linestyle=hline.style_dotted)
h50 = hline(50, "Neutral Zone", color=color.new(#FF9800, 70), linestyle=hline.style_dotted)
h35 = hline(35, "Exhaustion Zone", color=color.new(#F44336, 70), linestyle=hline.style_dotted)
hline(0, "", color=color.new(#333333, 80))
fill(h75, h50, color=color.new(#00E676, 93))
fill(h50, h35, color=color.new(#FF9800, 95))
evr_plot = evr_ratio * 25.0
plot(evr_plot, "Effort vs Result", color=color.new(#E040FB, 50), linewidth=1)
dv_plot = dv_ratio * 50.0
plot(dv_plot, "DV Ratio (x50)", color=color.new(#00BCD4, 60), linewidth=1)
cvd_norm = bar_spread_cvd > 0 ? ((close - low) / bar_spread_cvd - 0.5) * 50.0 : 0.0
plot(cvd_norm + 25.0, "CVD Bar Delta", color=color.new(#FFEB3B, 60), linewidth=1)
// VERDICT + VSA LABELS
if show_labels and breakout_bar
v_str = is_accumulation ? "ACCUM" : (is_likely_accum ? "LIKELY" : (is_uncertain ? "UNSURE" : "EXHAUST"))
v_clr = is_accumulation ? color.new(#00E676, 0) : (is_likely_accum ? color.new(#2196F3, 0) : (is_uncertain ? color.new(#FF9800, 0) : color.new(#F44336, 0)))
label.new(bar_index, bq_pct + 8, v_str, color=v_clr, textcolor=color.white, style=label.style_label_down, size=size.small)
// NEW in v3.2: small HTF confirmation tag above the verdict label
if show_labels and breakout_bar and htf_enable and htf_valid and htf_alignment != 0
htf_tag = htf_alignment == 1 ? "HTF: OK" : htf_alignment == 2 ? "HTF: NO" : htf_alignment == 3 ? "HTF: OK (avoid)" : ""
if htf_tag != ""
htf_tag_clr = htf_alignment == 1 ? color.new(#00E676, 0) : color.new(#F44336, 0)
label.new(bar_index, bq_pct + 16, htf_tag, color=htf_tag_clr, textcolor=color.white, style=label.style_label_down, size=size.tiny)
if show_labels and hidden_buy
label.new(bar_index, 10, "HB", color=color.new(#00E676, 30), textcolor=color.white, style=label.style_label_up, size=size.tiny)
if show_labels and hidden_sell
label.new(bar_index, 90, "HS", color=color.new(#F44336, 30), textcolor=color.white, style=label.style_label_down, size=size.tiny)
if show_vsa_labels
if vsa_sc
label.new(bar_index, 8, "SC", color=color.new(#00E676, 0), textcolor=color.white, style=label.style_label_up, size=size.normal)
if vsa_sv
label.new(bar_index, 15, "SV", color=color.new(#00E676, 20), textcolor=color.white, style=label.style_label_up, size=size.small)
if vsa_br
label.new(bar_index, 22, "BR", color=color.new(#00BCD4, 10), textcolor=color.white, style=label.style_label_up, size=size.small)
if vsa_bh
label.new(bar_index, 29, "BH", color=color.new(#00BCD4, 20), textcolor=color.white, style=label.style_label_up, size=size.small)
if vsa_sa
label.new(bar_index, 36, "SA", color=color.new(#76FF03, 10), textcolor=color.white, style=label.style_label_up, size=size.small)
if vsa_bc
label.new(bar_index, 92, "BC", color=color.new(#F44336, 0), textcolor=color.white, style=label.style_label_down, size=size.normal)
if vsa_eor
label.new(bar_index, 85, "EoR", color=color.new(#F44336, 20), textcolor=color.white, style=label.style_label_down, size=size.small)
if vsa_tr
label.new(bar_index, 78, "TR", color=color.new(#FF6D00, 10), textcolor=color.white, style=label.style_label_down, size=size.small)
if vsa_sod
label.new(bar_index, 71, "SoD", color=color.new(#FF6D00, 20), textcolor=color.white, style=label.style_label_down, size=size.small)
if vsa_da
label.new(bar_index, 64, "DA", color=color.new(#FF6D00, 30), textcolor=color.white, style=label.style_label_down, size=size.small)
if no_supply
label.new(bar_index, 3, "NS", color=color.new(#00E676, 30), textcolor=color.white, style=label.style_label_up, size=size.tiny)
if shakeout
label.new(bar_index, 3, "SHK", color=color.new(#00C853, 0), textcolor=color.white, style=label.style_label_up, size=size.normal)
if no_demand
label.new(bar_index, 97, "ND", color=color.new(#F44336, 30), textcolor=color.white, style=label.style_label_down, size=size.tiny)
if upthrust
label.new(bar_index, 97, "UT", color=color.new(#D50000, 0), textcolor=color.white, style=label.style_label_down, size=size.normal)
bgcolor(show_vsa_bg and shakeout ? color.new(#00E676, 88) : na, title="Shakeout BG")
bgcolor(show_vsa_bg and no_supply ? color.new(#00E676, 93) : na, title="No Supply BG")
bgcolor(show_vsa_bg and upthrust ? color.new(#F44336, 85) : na, title="Upthrust BG")
bgcolor(show_vsa_bg and no_demand ? color.new(#F44336, 93) : na, title="No Demand BG")
// =========================================================
// SCORECARD TABLE (3-column layout with rebuilt VSA column)
// =========================================================
var tbl = table.new(position.top_right, 6, 13, bgcolor=color.new(#1a1a2e, 0), border_width=1, border_color=color.new(#333355, 0), frame_width=2, frame_color=color.new(#4444aa, 0))
// Helper: format bars-ago as string
f_ago_str(int bars) =>
bars == 0 ? "NOW" : bars < 100 ? (str.tostring(bars) + "d ago") : "---"
// Helper: color by recency (bullish signal)
f_ago_bull(int bars) =>
bars <= 2 ? #00E676 : (bars <= 5 ? #4CAF50 : (bars <= 10 ? #FF9800 : #555577))
// Helper: color by recency (bearish signal)
f_ago_bear(int bars) =>
bars <= 2 ? #F44336 : (bars <= 5 ? #E57373 : (bars <= 10 ? #FF9800 : #555577))
if show_bo_table and barstate.islast
// --- COLUMN HEADERS (row 0) ---
table.cell(tbl, 0, 0, "LIVE TAPE READER", text_color=#FFD700, text_size=size.tiny, bgcolor=color.new(#0d0d1a, 0), text_halign=text.align_left)
table.cell(tbl, 1, 0, "NOW", text_color=#FFD700, text_size=size.tiny, bgcolor=color.new(#0d0d1a, 0))
table.cell(tbl, 2, 0, "LAST BREAKOUT", text_color=#00E676, text_size=size.tiny, bgcolor=color.new(#0d0d1a, 0), text_halign=text.align_left)
table.cell(tbl, 3, 0, latch_date, text_color=#00E676, text_size=size.tiny, bgcolor=color.new(#0d0d1a, 0))
table.cell(tbl, 4, 0, "VSA RECENCY", text_color=#E040FB, text_size=size.tiny, bgcolor=color.new(#0d0d1a, 0), text_halign=text.align_left)
table.cell(tbl, 5, 0, "BARS AGO", text_color=#E040FB, text_size=size.tiny, bgcolor=color.new(#0d0d1a, 0))
// =========================================================
// COLUMN 1: LIVE TAPE READER (rows 1-9)
// =========================================================
live_bar_dir = close >= open ? "UP" : "DOWN"
live_vol_ch = tw_vol_is_high ? "HIGH vol" : (tw_vol_is_low ? "LOW vol" : "avg vol")
live_spd_ch = tw_spread_is_wide ? "WIDE" : (tw_spread_is_narrow ? "NARROW" : "avg spd")
live_cls_ch = tw_close_near_hi ? "close HI" : (tw_close_near_lo ? "close LO" : "close MID")
live_bar_clr = (close >= open and tw_close_near_hi) ? #00E676 : ((close < open and tw_close_near_lo) ? #F44336 : #FF9800)
table.cell(tbl, 0, 1, "Bar", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 1, 1, live_bar_dir + " " + live_vol_ch, text_color=live_bar_clr, text_size=size.tiny)
table.cell(tbl, 0, 2, "Spread/Close", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 1, 2, live_spd_ch + " " + live_cls_ch, text_color=live_bar_clr, text_size=size.tiny)
live_evr_str = str.tostring(evr_ratio, "#.##") + " (" + str.tostring(evr_score) + "/3)"
live_evr_clr = evr_score >= 2 ? #00E676 : (evr_score >= 1 ? #FF9800 : #F44336)
table.cell(tbl, 0, 3, "Effort/Result", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 1, 3, live_evr_str, text_color=live_evr_clr, text_size=size.tiny)
live_vr_str = str.tostring(tw_vol_ratio, "#.##") + "x avg"
live_vr_clr = tw_vol_is_high ? #00E676 : (tw_vol_is_low ? #F44336 : #FF9800)
table.cell(tbl, 0, 4, "Volume", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 1, 4, live_vr_str, text_color=live_vr_clr, text_size=size.tiny)
live_cp_str = str.tostring(close_pct * 100, "#.0") + "% range"
live_cp_clr = tw_close_near_hi ? #00E676 : (tw_close_near_lo ? #F44336 : #FF9800)
table.cell(tbl, 0, 5, "Close Pos", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 1, 5, live_cp_str, text_color=live_cp_clr, text_size=size.tiny)
live_dv_str = str.tostring(dv_ratio * 100, "#.0") + "% buy"
live_dv_flag = hidden_sell ? " HS!" : (hidden_buy ? " HB!" : "")
live_dv_clr = hidden_sell ? #F44336 : (hidden_buy ? #00E676 : (dv_ratio > 0.55 ? #00E676 : (dv_ratio > 0.45 ? #FF9800 : #F44336)))
table.cell(tbl, 0, 6, "DV Flow", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 1, 6, live_dv_str + live_dv_flag, text_color=live_dv_clr, text_size=size.tiny)
live_cvd_str = cvd_confirms ? "CONFIRMS" : (cvd_diverges ? "DIVERGES!" : "Neutral")
live_cvd_clr = cvd_confirms ? #00E676 : (cvd_diverges ? #F44336 : #FF9800)
table.cell(tbl, 0, 7, "CVD", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 1, 7, live_cvd_str, text_color=live_cvd_clr, text_size=size.tiny)
live_ctx_str = (bc_detected or eor_detected) ? "BC/EoR ARMED" : "Neutral ctx"
live_ctx_clr = (bc_detected or eor_detected) ? #FF9800 : #666688
table.cell(tbl, 0, 8, "Background", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 1, 8, live_ctx_str, text_color=live_ctx_clr, text_size=size.tiny)
bo_live_str = breakout_bar ? "BREAKOUT!" : (bars_since_bo <= 5 ? ("FT Day " + str.tostring(bars_since_bo)) : "---")
bo_live_clr = breakout_bar ? #00E676 : (bars_since_bo <= 5 ? #2196F3 : #555577)
table.cell(tbl, 0, 9, "Breakout", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 1, 9, bo_live_str, text_color=bo_live_clr, text_size=size.tiny)
// =========================================================
// COLUMN 2: LAST BREAKOUT SCORECARD (rows 1-12)
// =========================================================
v_str2 = latch_is_accum ? "ACCUMULATION" : (latch_is_likely ? "LIKELY ACCUM" : (latch_is_uncertain ? "UNCERTAIN" : (latch_is_exhaust ? "EXHAUSTION" : "No BO yet")))
v_clr2 = latch_is_accum ? #00E676 : (latch_is_likely ? #2196F3 : (latch_is_uncertain ? #FF9800 : #F44336))
table.cell(tbl, 2, 1, "Verdict", text_color=#ccccdd, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 1, v_str2, text_color=v_clr2, text_size=size.tiny)
comp_str = str.tostring(latch_raw_score) + "/28 (" + str.tostring(latch_bq_pct, "#") + "%)"
comp_clr = latch_is_accum ? #00E676 : (latch_is_likely ? #2196F3 : (latch_is_uncertain ? #FF9800 : #F44336))
table.cell(tbl, 2, 2, "COMPOSITE", text_color=#ffffff, text_size=size.tiny, text_halign=text.align_left, bgcolor=color.new(#0d0d1a, 0))
table.cell(tbl, 3, 2, comp_str, text_color=comp_clr, text_size=size.tiny, bgcolor=color.new(#0d0d1a, 0))
evr_str = str.tostring(latch_evr_ratio, "#.##") + " (" + str.tostring(latch_evr_score) + "/3)"
evr_clr = latch_evr_score >= 2 ? #00E676 : (latch_evr_score >= 1 ? #FF9800 : #F44336)
table.cell(tbl, 2, 3, "Effort/Result", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 3, evr_str, text_color=evr_clr, text_size=size.tiny)
cp_str = str.tostring(latch_close_pct * 100, "#.0") + "% (" + str.tostring(latch_close_score) + "/3)"
cp_clr = latch_close_score >= 2 ? #00E676 : (latch_close_score >= 1 ? #FF9800 : #F44336)
table.cell(tbl, 2, 4, "Close Pos", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 4, cp_str, text_color=cp_clr, text_size=size.tiny)
abs_str = str.tostring(latch_absorption) + "/3"
abs_clr = latch_absorption >= 2 ? #00E676 : (latch_absorption >= 1 ? #FF9800 : #F44336)
table.cell(tbl, 2, 5, "Absorption", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 5, abs_str, text_color=abs_clr, text_size=size.tiny)
vc_str = str.tostring(latch_vol_surge, "#.#") + "x (" + str.tostring(latch_vol_char) + "/3)"
vc_clr = latch_vol_char >= 2 ? #00E676 : (latch_vol_char >= 1 ? #FF9800 : #F44336)
table.cell(tbl, 2, 6, "Vol Surge", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 6, vc_str, text_color=vc_clr, text_size=size.tiny)
vr_str = latch_vol_highest ? "HIGHEST 20d" : "Not highest"
vr_clr = latch_vol_rank >= 2 ? #00E676 : (latch_vol_rank >= 1 ? #FF9800 : #F44336)
table.cell(tbl, 2, 7, "Vol Rank", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 7, vr_str, text_color=vr_clr, text_size=size.tiny)
sq_str = str.tostring(latch_spread_vs_avg, "#.##") + "x avg"
sq_clr = latch_spread_score >= 2 ? #00E676 : (latch_spread_score >= 1 ? #FF9800 : #F44336)
table.cell(tbl, 2, 8, "Spread", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 8, sq_str, text_color=sq_clr, text_size=size.tiny)
cvd_str = latch_cvd_confirms ? "CONFIRMS" : (latch_cvd_diverges ? "DIVERGES" : "Neutral")
cvd_extra = latch_cvd_stealth ? " +stealth" : ""
cvd_display = cvd_str + cvd_extra + " (" + str.tostring(latch_cvd_total) + "/3)"
cvd_clr = latch_cvd_total >= 2 ? #00E676 : (latch_cvd_total >= 1 ? #FF9800 : #F44336)
table.cell(tbl, 2, 9, "CVD", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 9, cvd_display, text_color=cvd_clr, text_size=size.tiny)
dv_pct_str = str.tostring(latch_dv_ratio * 100, "#.0") + "% buy"
hs_flag = latch_hidden_sell ? " HS" : (latch_hidden_buy ? " HB" : "")
hidden_display = dv_pct_str + hs_flag + " (" + str.tostring(latch_hidden_score) + "/3)"
hidden_clr = latch_hidden_score >= 2 ? #00E676 : (latch_hidden_score >= 1 ? #FF9800 : #F44336)
hidden_bg = latch_hidden_sell ? color.new(#F44336, 70) : (latch_hidden_buy ? color.new(#00E676, 70) : color.new(#1a1a2e, 0))
table.cell(tbl, 2, 10, "Hidden Signal", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 10, hidden_display, text_color=hidden_clr, text_size=size.tiny, bgcolor=hidden_bg)
ft_day_str = str.tostring(bars_since_bo)
ft_str = bars_since_bo <= 10 ? (str.tostring(ft_score) + "/3 D" + ft_day_str) : "N/A"
ft_clr2 = ft_score >= 2 ? #00E676 : (ft_score >= 1 ? #FF9800 : #F44336)
ft_tbl_clr = bars_since_bo <= ft_days ? ft_clr2 : #666688
table.cell(tbl, 2, 11, "Follow-Thru", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 11, ft_str, text_color=ft_tbl_clr, text_size=size.tiny)
tw_net_str = (latch_tw_score > 0 ? "+" : "") + str.tostring(latch_tw_score) + " pts"
tw_net_clr = latch_tw_score > 0 ? #00E676 : (latch_tw_score < 0 ? #F44336 : #666688)
table.cell(tbl, 2, 12, "TW VSA Net", text_color=#ccccdd, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 3, 12, tw_net_str, text_color=tw_net_clr, text_size=size.tiny)
// =========================================================
// COLUMN 3: VSA RECENCY (REBUILT in v3.1)
// Shows bars-ago for every signal with color-coded recency
// =========================================================
// Row 1: Best recent SoS
sos_ago_str = best_sos_name + " " + f_ago_str(best_sos_ago)
sos_ago_clr = f_ago_bull(best_sos_ago)
table.cell(tbl, 4, 1, "Best Strength", text_color=#ccccdd, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 5, 1, sos_ago_str, text_color=sos_ago_clr, text_size=size.tiny, bgcolor=best_sos_ago <= 2 ? color.new(#00E676, 80) : color.new(#1a1a2e, 0))
// Row 2: Best recent SoW
sow_ago_str = best_sow_name + " " + f_ago_str(best_sow_ago)
sow_ago_clr = f_ago_bear(best_sow_ago)
table.cell(tbl, 4, 2, "Best Weakness", text_color=#ccccdd, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 5, 2, sow_ago_str, text_color=sow_ago_clr, text_size=size.tiny, bgcolor=best_sow_ago <= 2 ? color.new(#F44336, 80) : color.new(#1a1a2e, 0))
// Row 3: SC | SV
table.cell(tbl, 4, 3, "SC | SV", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 5, 3, f_ago_str(ago_sc) + " | " + f_ago_str(ago_sv), text_color=f_ago_bull(math.min(ago_sc, ago_sv)), text_size=size.tiny)
// Row 4: BR | BH | SA
table.cell(tbl, 4, 4, "BR | BH | SA", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 5, 4, f_ago_str(ago_br) + " | " + f_ago_str(ago_bh) + " | " + f_ago_str(ago_sa), text_color=f_ago_bull(math.min(ago_br, math.min(ago_bh, ago_sa))), text_size=size.tiny)
// Row 5: NS | SHK
table.cell(tbl, 4, 5, "NS | SHK", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 5, 5, f_ago_str(ago_ns) + " | " + f_ago_str(ago_shk), text_color=f_ago_bull(math.min(ago_ns, ago_shk)), text_size=size.tiny)
// Row 6: BC | EoR
table.cell(tbl, 4, 6, "BC | EoR", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 5, 6, f_ago_str(ago_bc) + " | " + f_ago_str(ago_eor), text_color=f_ago_bear(math.min(ago_bc, ago_eor)), text_size=size.tiny)
// Row 7: TR | SoD | DA
table.cell(tbl, 4, 7, "TR | SoD | DA", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 5, 7, f_ago_str(ago_tr) + " | " + f_ago_str(ago_sod) + " | " + f_ago_str(ago_da), text_color=f_ago_bear(math.min(ago_tr, math.min(ago_sod, ago_da))), text_size=size.tiny)
// Row 8: UT | ND
table.cell(tbl, 4, 8, "UT | ND", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 5, 8, f_ago_str(ago_ut) + " | " + f_ago_str(ago_nd), text_color=f_ago_bear(math.min(ago_ut, ago_nd)), text_size=size.tiny)
// Row 9: HB | HS
table.cell(tbl, 4, 9, "HB | HS", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
hb_clr = ago_hb < ago_hs ? f_ago_bull(ago_hb) : f_ago_bear(ago_hs)
table.cell(tbl, 5, 9, f_ago_str(ago_hb) + " | " + f_ago_str(ago_hs), text_color=hb_clr, text_size=size.tiny)
// Row 10: Bias summary
sos_recent = best_sos_ago <= 5
sow_recent = best_sow_ago <= 5
bias_str = (sos_recent and not sow_recent) ? "BULL BIAS" : ((sow_recent and not sos_recent) ? "BEAR BIAS" : ((sos_recent and sow_recent) ? "CONTESTED" : "NEUTRAL"))
bias_clr = (sos_recent and not sow_recent) ? #00E676 : ((sow_recent and not sos_recent) ? #F44336 : ((sos_recent and sow_recent) ? #FF9800 : #666688))
bias_bg = (sos_recent and not sow_recent) ? color.new(#00E676, 80) : ((sow_recent and not sos_recent) ? color.new(#F44336, 80) : color.new(#1a1a2e, 0))
table.cell(tbl, 4, 10, "VSA BIAS", text_color=#ccccdd, text_size=size.tiny, text_halign=text.align_left)
table.cell(tbl, 5, 10, bias_str, text_color=bias_clr, text_size=size.tiny, bgcolor=bias_bg)
// =========================================================
// NEW in v3.2 — HTF CONTEXT TABLE (MTF stable-snapshot readout)
// =========================================================
var tblHtf = table.new(position.bottom_left, 2, 9, bgcolor=color.new(#1a1a2e, 0), border_width=1, border_color=color.new(#333355, 0), frame_width=2, frame_color=color.new(#4444aa, 0))
f_verdict_str(int v) =>
v == 1 ? "ACCUMULATION" : v == 2 ? "LIKELY ACCUM" : v == 3 ? "UNCERTAIN" : v == 4 ? "EXHAUSTION" : "---"
f_verdict_clr(int v) =>
v == 1 ? #00E676 : v == 2 ? #2196F3 : v == 3 ? #FF9800 : v == 4 ? #F44336 : #666688
f_htf_ago_str(int bars) =>
bars == 0 ? "NOW" : bars < 900 ? (str.tostring(bars) + " bars ago") : "none seen"
if show_htf_table and barstate.islast
table.cell(tblHtf, 0, 0, "HTF CONTEXT (" + htf_tf + ")", text_color=#E040FB, text_size=size.tiny, bgcolor=color.new(#0d0d1a, 0), text_halign=text.align_left)
table.cell(tblHtf, 1, 0, htf_valid ? "stable snapshot" : "INVALID TF", text_color=htf_valid ? #E040FB : #F44336, text_size=size.tiny, bgcolor=color.new(#0d0d1a, 0))
table.cell(tblHtf, 0, 1, "Verdict", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tblHtf, 1, 1, f_verdict_str(htf_verdict), text_color=f_verdict_clr(htf_verdict), text_size=size.tiny)
htf_comp_str = str.tostring(htf_raw_score) + "/14 (" + str.tostring(htf_bq_pct, "#") + "%)"
table.cell(tblHtf, 0, 2, "Composite", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tblHtf, 1, 2, htf_comp_str, text_color=f_verdict_clr(htf_verdict), text_size=size.tiny)
htf_tw_str = (htf_tw_score > 0 ? "+" : "") + str.tostring(htf_tw_score) + " pts"
htf_tw_clr = htf_tw_score > 0 ? #00E676 : (htf_tw_score < 0 ? #F44336 : #666688)
table.cell(tblHtf, 0, 3, "TW VSA Net", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tblHtf, 1, 3, htf_tw_str, text_color=htf_tw_clr, text_size=size.tiny)
htf_best_strength_ago = math.min(htf_ago_shk, htf_ago_ns)
htf_best_strength_name = htf_ago_shk <= htf_ago_ns ? "SHK" : "NS"
table.cell(tblHtf, 0, 4, "Best Strength", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tblHtf, 1, 4, htf_best_strength_name + " " + f_htf_ago_str(htf_best_strength_ago), text_color=htf_best_strength_ago <= 2 ? #00E676 : #666688, text_size=size.tiny)
htf_best_weak_ago = math.min(htf_ago_ut, htf_ago_nd)
htf_best_weak_name = htf_ago_ut <= htf_ago_nd ? "UT" : "ND"
table.cell(tblHtf, 0, 5, "Best Weakness", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tblHtf, 1, 5, htf_best_weak_name + " " + f_htf_ago_str(htf_best_weak_ago), text_color=htf_best_weak_ago <= 2 ? #F44336 : #666688, text_size=size.tiny)
table.cell(tblHtf, 0, 6, "HTF Breakout", text_color=#aaaacc, text_size=size.tiny, text_halign=text.align_left)
table.cell(tblHtf, 1, 6, htf_bars_since_bo == 0 ? "THIS BAR" : f_htf_ago_str(htf_bars_since_bo), text_color=#aaaacc, text_size=size.tiny)
align_str = htf_alignment == 1 ? "CONFIRMS" : htf_alignment == 2 ? "CONFLICTS!" : htf_alignment == 3 ? "CONFIRMS (avoid)" : htf_alignment == 4 ? "NEUTRAL" : "OFF"
align_clr = htf_alignment == 1 ? #00E676 : htf_alignment == 2 ? #F44336 : htf_alignment == 3 ? #F44336 : #666688
align_bg = htf_alignment == 2 ? color.new(#F44336, 70) : (htf_alignment == 1 ? color.new(#00E676, 80) : color.new(#1a1a2e, 0))
table.cell(tblHtf, 0, 7, "Alignment", text_color=#ccccdd, text_size=size.tiny, text_halign=text.align_left)
table.cell(tblHtf, 1, 7, align_str, text_color=align_clr, text_size=size.tiny, bgcolor=align_bg)
table.cell(tblHtf, 0, 8, "vs Last Breakout", text_color=#666688, text_size=size.tiny, text_halign=text.align_left)
table.cell(tblHtf, 1, 8, latch_date, text_color=#666688, text_size=size.tiny)
// ALERTS
alertcondition(breakout_bar and is_accumulation, title="Accumulation Breakout", message="BQ WYCKOFF: Accumulation breakout on {{ticker}}")
alertcondition(breakout_bar and is_exhaustion, title="Exhaustion Breakout", message="BQ WYCKOFF: Exhaustion breakout on {{ticker}} - avoid")
alertcondition(breakout_bar and hidden_sell, title="Hidden Sell on Breakout", message="BQ WYCKOFF: HIDDEN SELL on breakout of {{ticker}}")
alertcondition(breakout_bar and cvd_diverges, title="CVD Divergence on Breakout", message="BQ WYCKOFF: CVD divergence on breakout of {{ticker}}")
alertcondition(in_followthrough and (ft_score < 2), title="Weak Follow-Through", message="BQ WYCKOFF: Weak follow-through on {{ticker}}")
alertcondition(hidden_buy, title="Hidden Buy Detected", message="BQ WYCKOFF: Hidden Buy on {{ticker}}")
alertcondition(hidden_sell, title="Hidden Sell Detected", message="BQ WYCKOFF: Hidden Sell on {{ticker}}")
alertcondition(shakeout, title="Shakeout Detected", message="BQ WYCKOFF v3: SHAKEOUT on {{ticker}}")
alertcondition(no_supply, title="No Supply Detected", message="BQ WYCKOFF v3: NO SUPPLY on {{ticker}}")
alertcondition(upthrust, title="Upthrust Detected", message="BQ WYCKOFF v3: UPTHRUST on {{ticker}}")
alertcondition(breakout_bar and upthrust, title="Upthrust on Breakout Bar", message="BQ WYCKOFF v3: UPTHRUST on breakout of {{ticker}}")
alertcondition(no_demand, title="No Demand Detected", message="BQ WYCKOFF v3: NO DEMAND on {{ticker}}")
// NEW in v3.2
alertcondition(breakout_bar and is_accumulation and htf_enable and htf_valid and (htf_verdict == 1 or htf_verdict == 2), title="HTF-Confirmed Accumulation Breakout", message="BQ WYCKOFF: Accumulation breakout on {{ticker}} CONFIRMED by HTF context")
// END