You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

盈利型MSB指标三角标记延迟显示问题求助

MSB指标三角标记延迟显示的解决方法

问题根源

三角标记延迟10-14根K线显示的核心原因是market变量的趋势翻转判断依赖ZigZag的滞后确认特性:

  • ZigZag的to_up/to_down信号需要等待zigzag_len根K线走完,才能确认当前K线是高低点,这直接导致l0/h0等关键变量的更新滞后。
  • market变量的翻转逻辑(从-1到1或1到-1)基于这些滞后的高低点数据,因此market == 1 and market[1] == -1的条件只会在ZigZag确认高低点后的K线才触发,而非实际突破的K线。

解决方案

通过记录MSB信号触发的Bar索引,直接在对应K线上绘制标记,绕过market变量的滞后问题:

修改步骤

  1. 添加变量存储MSB触发的Bar索引:
var int msb_bullish_bar = na
var int msb_bearish_bar = na
  1. 在ta.change(market) != 0的条件块中,记录触发MSB的Bar索引:
if ta.change(market) != 0
    if market == 1
        // 记录看涨MSB的Bar索引
        msb_bullish_bar := bar_index
        // 原有代码...
    if market == -1
        // 记录看跌MSB的Bar索引
        msb_bearish_bar := bar_index
        // 原有代码...
  1. 修改plotshape逻辑,直接在记录的Bar索引位置绘制标记:
// ———— TRIANGLES ON STRUCTURE-BREAK BAR ————
plotshape(bar_index == msb_bullish_bar, title="Bullish MSB", style=shape.triangleup, location=location.abovebar, color=color.green, size=size.small)
plotshape(bar_index == msb_bearish_bar, title="Bearish MSB", style=shape.triangledown, location=location.belowbar, color=color.red, size=size.small)

注:将看跌标记的location改为location.belowbar可避免与看涨标记重叠,更符合视觉逻辑。

完整修改后的代码

//@version=5
indicator("Market Structure Break & Order Block + MSB Triangles", "MSB-OB+", overlay=true, max_lines_count=500, max_bars_back=4900, max_boxes_count=500)

settings = "Settings"
zigzag_len = input.int(9, "ZigZag Length", group=settings)
show_zigzag = input.bool(true, "Show Zigzag", group=settings)
fib_factor = input.float(0.33, "Fib Factor for breakout confirmation", 0, 1, 0.01, group=settings)

text_size = input.string(size.tiny, "Text Size", [size.tiny, size.small, size.normal, size.large, size.huge], group=settings)
delete_boxes = input.bool(true, "Delete Old/Broken Boxes", group=settings)

bu_ob_inline_color = "Bu-OB Colors"
be_ob_inline_color = "Be-OB Colors"
bu_bb_inline_color = "Bu-BB Colors"
be_bb_inline_color = "Be-BB Colors"

bu_ob_display_settings = "Bu-OB Display Settings"
bu_ob_color = input.color(color.new(color.green, 70), "Color", group=bu_ob_display_settings, inline=bu_ob_inline_color)
bu_ob_border_color = input.color(color.green, "Border Color", group=bu_ob_display_settings, inline=bu_ob_inline_color)
bu_ob_text_color = input.color(color.green, "Text Color", group=bu_ob_display_settings, inline=bu_ob_inline_color)

be_ob_display_settings = "Be-OB Display Settings"
be_ob_color = input.color(color.new(color.red, 70), "Color", group=be_ob_display_settings, inline=be_ob_inline_color)
be_ob_border_color = input.color(color.red, "Border Color", group=be_ob_display_settings, inline=be_ob_inline_color)
be_ob_text_color = input.color(color.red, "Text Color", group=be_ob_display_settings, inline=be_ob_inline_color)

bu_bb_display_settings = "Bu-BB & Bu-MB Display Settings"
bu_bb_color = input.color(color.new(color.green, 70), "Color", group=bu_bb_display_settings, inline=bu_bb_inline_color)
bu_bb_border_color = input.color(color.green, "Border Color", group=bu_bb_display_settings, inline=bu_bb_inline_color)
bu_bb_text_color = input.color(color.green, "Text Color", group=bu_bb_display_settings, inline=bu_bb_inline_color)

be_bb_display_settings = "Be-BB & Be-MB Display Settings"
be_bb_color = input.color(color.new(color.red, 70), "Color", group=be_bb_display_settings, inline=be_bb_inline_color)
be_bb_border_color = input.color(color.red, "Border Color", group=be_bb_display_settings, inline=be_bb_inline_color)
be_bb_text_color = input.color(color.red, "Text Color", group=be_bb_display_settings, inline=be_bb_inline_color)

var float[] high_points_arr = array.new_float(5)
var int[]   high_index_arr  = array.new_int(5)
var float[] low_points_arr  = array.new_float(5)
var int[]   low_index_arr   = array.new_int(5)

var box[] bu_ob_boxes = array.new_box(5)
var box[] be_ob_boxes = array.new_box(5)
var box[] bu_bb_boxes = array.new_box(5)
var box[] be_bb_boxes = array.new_box(5)

// 新增:存储MSB触发的Bar索引
var int msb_bullish_bar = na
var int msb_bearish_bar = na

to_up = high >= ta.highest(zigzag_len)
to_down = low <= ta.lowest(zigzag_len)

trend = 1
trend := nz(trend[1], 1)
trend := trend == 1 and to_down ? -1 : trend == -1 and to_up ? 1 : trend

last_trend_up_since = ta.barssince(to_up[1])
low_val = ta.lowest(nz(last_trend_up_since > 0 ? last_trend_up_since : 1, 1))
low_index = bar_index - ta.barssince(low_val == low)

last_trend_down_since = ta.barssince(to_down[1])
high_val = ta.highest(nz(last_trend_down_since > 0 ? last_trend_down_since : 1, 1))
high_index = bar_index - ta.barssince(high_val == high)

if ta.change(trend) != 0
    if trend == 1
        array.push(low_points_arr, low_val)
        array.push(low_index_arr, low_index)
    if trend == -1
        array.push(high_points_arr, high_val)
        array.push(high_index_arr, high_index)

f_get_high(ind) =>
    [ array.get(high_points_arr, array.size(high_points_arr) - 1 - ind), array.get(high_index_arr, array.size(high_index_arr) - 1 - ind) ]

f_get_low(ind) =>
    [ array.get(low_points_arr, array.size(low_points_arr) - 1 - ind), array.get(low_index_arr, array.size(low_index_arr) - 1 - ind) ]

[h0, h0i] = f_get_high(0)
[h1, h1i] = f_get_high(1)
[l0, l0i] = f_get_low(0)
[l1, l1i] = f_get_low(1)

if ta.change(trend) != 0 and show_zigzag
    if trend == 1
        line.new(h0i, h0, l0i, l0)
    if trend == -1
        line.new(l0i, l0, h0i, h0)

market = 1
market := nz(market[1], 1)
last_l0 = ta.valuewhen(ta.change(market) != 0, l0, 0)
last_h0 = ta.valuewhen(ta.change(market) != 0, h0, 0)
market := last_l0 == l0 or last_h0 == h0 ? market : market == 1 and l0 < l1 and l0 < l1 - math.abs(h0 - l1) * fib_factor ? -1 : market == -1 and h0 > h1 and h0 > h1 + math.abs(h1 - l0) * fib_factor ? 1 : market

bu_ob_index = bar_index
bu_ob_index := nz(bu_ob_index[1], bar_index)
for i = h1i to l0i[zigzag_len]
    index = bar_index - i 
    if open[index] > close[index]
        bu_ob_index := bar_index[index]
bu_ob_since = bar_index - bu_ob_index

be_ob_index = bar_index
be_ob_index := nz(be_ob_index[1], bar_index)
for i = l1i to h0i[zigzag_len]
    index = bar_index - i 
    if open[index] < close[index]
        be_ob_index := bar_index[index]
be_ob_since = bar_index - be_ob_index

be_bb_index = bar_index
be_bb_index := nz(be_bb_index[1], bar_index)
for i = h1i - zigzag_len to l1i
    index = bar_index - i
    if open[index] > close[index]
        be_bb_index := bar_index[index]
be_bb_since = bar_index - be_bb_index

bu_bb_index = bar_index
bu_bb_index := nz(bu_bb_index[1], bar_index)
for i = l1i - zigzag_len to h1i
    index = bar_index - i
    if open[index] < close[index]
        bu_bb_index := bar_index[index]
bu_bb_since = bar_index - bu_bb_index

if ta.change(market) != 0
    if market == 1
        // 记录看涨MSB的Bar索引
        msb_bullish_bar := bar_index
        line.new(h1i, h1, h0i, h1, color=color.green, width=2)
        label.new(int((h1i + l0i) / 2), h1, "MSB", color=color.new(color.black, 100), style=label.style_label_down, textcolor=color.green, size=size.small)
        bu_ob = box.new(bu_ob_index, high[bu_ob_since], bar_index + 10, low[bu_ob_since], bgcolor=bu_ob_color, border_color=bu_ob_border_color, text="Bu-OB", text_color=bu_ob_text_color, text_halign=text.align_right, text_size=text_size)
        bu_bb = box.new(bu_bb_index, high[bu_bb_since], bar_index + 10, low[bu_bb_since], bgcolor=bu_bb_color, border_color=bu_bb_border_color, text=l0 < l1 ? "Bu-BB" : "Bu-MB", text_color=bu_bb_text_color, text_halign=text.align_right, text_size=text_size)
        array.push(bu_ob_boxes, bu_ob)
        array.push(bu_bb_boxes, bu_bb)
    if market == -1
        // 记录看跌MSB的Bar索引
        msb_bearish_bar := bar_index
        line.new(l1i, l1, l0i, l1, color=color.red, width=2)
        label.new(int((l1i + h0i) / 2), l1, "MSB", color=color.new(color.black, 100), style=label.style_label_up, textcolor=color.red, size=size.small)
        be_ob = box.new(be_ob_index, high[be_ob_since], bar_index + 10, low[be_ob_since], bgcolor=be_ob_color, border_color=be_ob_border_color, text="Be-OB", text_color=be_ob_text_color, text_halign=text.align_right, text_size=text_size)
        be_bb = box.new(be_bb_index, high[be_bb_since], bar_index + 10, low[be_bb_since], bgcolor=be_bb_color, border_color=be_bb_border_color, text=h0 > h1 ? "Be-BB" : "Be-MB", text_color=be_bb_text_color, text_halign=text.align_right, text_size=text_size)
        array.push(be_ob_boxes, be_ob)
        array.push(be_bb_boxes, be_bb)

for bull_ob in bu_ob_boxes
    bottom = box.get_bottom(bull_ob)
    top = box.get_top(bull_ob)
    if close < bottom
        if delete_boxes
            box.delete(array.shift(bu_ob_boxes))
        else
            array.shift(bu_ob_boxes)
    else if close < top
        alert("Price in the BU-OB zone")
    else
        box.set_right(bull_ob, bar_index + 10)

for bear_ob in be_ob_boxes
    top = box.get_top(bear_ob)
    bottom = box.get_bottom(bear_ob)
    if close > top
        if delete_boxes
            box.delete(array.shift(be_ob_boxes))
        else
            array.shift(be_ob_boxes)
    else if close > bottom
        alert("Price in the BE-OB zone")
    else
        box.set_right(bear_ob, bar_index + 10)

for bear_bb in be_bb_boxes
    top = box.get_top(bear_bb)
    bottom = box.get_bottom(bear_bb)
    if close > top
        if delete_boxes
            box.delete(array.shift(be_bb_boxes))
        else
            array.shift(be_bb_boxes)
    else if close > bottom
        alert("Price in the BE-BB zone")
    else
        box.set_right(bear_bb, bar_index + 10)

for bull_bb in bu_bb_boxes
    bottom = box.get_bottom(bull_bb)
    top = box.get_top(bull_bb)
    if close < bottom
        if delete_boxes
            box.delete(array.shift(bu_bb_boxes))
        else
            array.shift(bu_bb_boxes)
    else if close < top
        alert("Price in the BU-BB zone")
    else
        box.set_right(bull_bb, bar_index + 10)

alertcondition(ta.change(market) != 0, "MSB", "MSB")

// ———— TRIANGLES ON STRUCTURE-BREAK BAR ————
// 直接在记录的Bar索引位置绘制标记
plotshape(bar_index == msb_bullish_bar, title="Bullish MSB", style=shape.triangleup, location=location.abovebar, color=color.green, size=size.small)
plotshape(bar_index == msb_bearish_bar, title="Bearish MSB", style=shape.triangledown, location=location.belowbar, color=color.red, size=size.small)

额外说明

  • 该方法通过bar_index精准定位MSB触发的K线,完全规避了ZigZag滞后带来的标记延迟问题。
  • 若需支持多次MSB标记存储,可改用数组存储所有触发的Bar索引,再遍历数组绘制标记。

内容的提问来源于stack exchange,提问作者Keith Power

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 18:45:55