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

发布于 2025-01-19 10:29:17 字数 3266 浏览 4 评论 0原文

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

我的问题是它只发生一次。如果实时条突然出现峰值(与设定点交叉 - 可能多次),则不会生成进一步的警报。

这是我的代码:

// 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)

I have an indicator that check if the previous 2 candle bars are the same (not the current/realtime bar). The maximum and minumum price value is calculated from their respective high and low values. A buy or sell alert is then triggered when the realtime bar crossover these points. This part works fine.

My problem is that it only happens once. If there is a sudden spike in the realtime bar (that crossover the setpoints - which can be multiple times) no further alerts are generated.

This is my code:

// 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2025-01-26 10:29:17

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

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

That is because you are using alert.freq_once_per_bar in your alert() function. Change it to alert.freq_all.

Also, you have barstate.isconfirmed in your code. It will only be true if the script is calculating the last (closing) update of the current bar. Not sure if that's what you really want in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文