如何在实时栏中触发多个警报?

Howto trigger multiple alerts in a realtime bar?

我有一个指标可以检查前 2 个蜡烛柱是否相同(不是 current/realtime 柱)。最大和最小价格值是根据它们各自的高值和低值计算得出的。当实时柱线穿过这些点时,就会触发买入或卖出警报。这部分工作正常。

我的问题是它只发生一次。如果实时条中突然出现尖峰(跨越设定点 - 可以是多次),则不会生成进一步的警报。

这是我的代码:

// This source code is PRIVATE
// © WaxBill2k 2204053

//@version=5
indicator("BarAlerts", overlay = true, max_labels_count = 500)

// INITIATIONS =================================================================

// Initialize boolean for plotshape series
var bool buy = false
var bool sell = false

// Initialize first Buy and Sell levels
var float buy_level = 0
var float sell_level = 0

// Initialize 2Bars
bool upBar = false
bool dnBar = false
bool twoUpBars = false
bool twoDnBars = false

// CALCULATOR ==================================================================

// Find 2Bars
upBar := barstate.isconfirmed and open < close
dnBar := barstate.isconfirmed and open > close

// Set 2Bars
twoUpBars := upBar and upBar[1]
twoDnBars := dnBar and dnBar[1]

// Calc bars
if twoUpBars or twoDnBars
    buy_level := math.max(high[1], high)
    sell_level := math.min(low[1], low)

// CONDITIONS ==================================================================

// goto SELL
if low < sell_level[1]
    buy := false
    sell := true
// goto BUY
else if high > buy_level[1]
    buy := true
    sell := false

// DISPLAY =====================================================================

// Buy and Sell points ---------------------------------------------------------

lblb = label.new(x = bar_index, y = na, size = size.normal, color = color.new(color.white, 90))
lbls = label.new(x = bar_index, y = na, size = size.normal, color = color.new(color.white, 90))

bpic = "◀ "
spic = "◀ "
npic = "."

if sell and buy[1]
    label.set_text(lbls, bpic)
    label.set_y(lbls, sell_level)
    label.set_yloc(lbls, yloc.price)
    label.set_style(lbls, label.style_label_left)
else if buy and sell[1]
    label.set_text(lblb, spic)
    label.set_y(lblb, buy_level)
    label.set_yloc(lblb, yloc.price)
    label.set_style(lblb, label.style_label_left)
else
    lbln = label.new(x = bar_index, y = na, yloc = yloc.price, size = size.tiny, color = color.new(color.green, 90), style = label.style_none)
    
// Buy and Sell text values ----------------------------------------------------

bval = "Sell"
sval = "Buy"

if sell and buy[1]
    lblsv = label.new(x = bar_index, y = sell_level, size = size.normal, color = color.white, style = label.style_label_up)
    label.set_text(lblsv, bval)
    label.set_y(lblsv, sell_level)
    label.set_yloc(lblsv, yloc.belowbar)
else if buy and sell[1]
    lblbv = label.new(x = bar_index, y = buy_level, size = size.normal, color = color.white, style = label.style_label_down)
    label.set_text(lblbv, sval)
    label.set_y(lblbv, buy_level)
    label.set_yloc(lblbv, yloc.abovebar)
else
    lblnv = label.new(x = bar_index, y = na, yloc = yloc.price, size = size.tiny, color = color.new(color.red, 90), style = label.style_none)

// ALERTS =====================================================================

if buy and sell[1]
    alert('buy: ' + 'price=' + str.tostring(sell_level), alert.freq_once_per_bar)
else if sell and buy[1]
    alert('sell: ' + 'price=' + str.tostring(buy_level), alert.freq_once_per_bar)

那是因为您在 alert() 函数中使用了 alert.freq_once_per_bar。将其更改为 alert.freq_all.

此外,您的代码中有 barstate.isconfirmed。如果脚本正在计算当前柱的最后(收盘)更新,它只会是 true。不确定在这种情况下这是否是您真正想要的。