如何在实时栏中触发多个警报?
我有一个指标可以检查前 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您在
alert()
函数中使用了alert.freq_once_per_bar
。将其更改为alert.freq_all
。另外,您的代码中有
barstate.isconfirmed
。仅当脚本正在计算当前柱的最后(收盘)更新时,它才会为true
。不确定在这种情况下这是否是您真正想要的。That is because you are using
alert.freq_once_per_bar
in youralert()
function. Change it toalert.freq_all
.Also, you have
barstate.isconfirmed
in your code. It will only betrue
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.