'Alert function for order blocks

I recently started trying to code in Pine on my own and built my own OB indicator with help from some other indicators. I know want to add an alert, that is triggered when Price comes back to an orderblock, but only one time. I played around a bit with the alert function, but it always triggered with every new candle. Can someone tell me, what I did wrong?

//@version=5
indicator("Orderblock Alerts", overlay=true, max_boxes_count=500, max_lines_count=500)

//Decalring Box Arrays//
var box[] _bullBoxesOB = array.new_box()
var box[] _bearBoxesOB = array.new_box()

//Functions//
isUp(index) =>
    close[index] > open[index]
    
isDown(index) =>
    close[index] < open[index]
    
isOBUp(index) =>
    isDown(index + 1) and isUp(index) and close[index] > high[index + 1]
    
isOBDown(index) =>
    isUp(index + 1) and isDown(index) and close[index] < low[index + 1]

//Drawing Boxes//

if isOBUp(1)
    _bullBoxOB = box.new(left=bar_index - 2, top=high[2], right=bar_index, bottom=math.min(low[2],low[1]), border_color=color.new(color.green, 55), border_style=line.style_solid, border_width=1, bgcolor=color.new(color.green, 55))
    array.push(_bullBoxesOB, _bullBoxOB)
    
if array.size(_bullBoxesOB) > 0
    for i = array.size(_bullBoxesOB) - 1 to 0 by 1
        sbox = array.get(_bullBoxesOB, i)
        top = box.get_top(sbox)
        bot = box.get_bottom(sbox)
        right = box.get_right(sbox)
        if na or (bar_index == right and not((high > bot and low < bot) or (high > top and low < top)))
            box.set_right(sbox, bar_index + 1)
        if array.size(_bullBoxesOB) > 10
            box.delete(array.shift(_bullBoxesOB))
        if low < top
            alert("Price inside Bull OB", alert.freq_once_per_bar)
            
if isOBDown(1)
    _bearBoxOB = box.new(left=bar_index - 2, top=math.max(high[2],high[1]), right=bar_index, bottom=low[2], border_color=color.new(color.red, 55), border_style=line.style_solid, border_width=1, bgcolor=color.new(color.red, 55))
    array.push(_bearBoxesOB, _bearBoxOB)
    
if array.size(_bearBoxesOB) > 0
    for i = array.size(_bearBoxesOB) - 1 to 0 by 1
        sbox = array.get(_bearBoxesOB, i)
        top = box.get_top(sbox)
        bot = box.get_bottom(sbox)
        right = box.get_right(sbox)
        if na or (bar_index == right and not((high > bot and low < bot) or (high > top and low < top)))
            box.set_right(sbox, bar_index + 1)
        if array.size(_bearBoxesOB) > 10
            box.delete(array.shift(_bearBoxesOB))
        if high > bot
            alert("Price inside Bear OB", alert.freq_once_per_bar)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source