pine-scripttradingview-apialerts

Howto trigger multiple alerts in a realtime 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)

Solution

  • 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.