BB Signal v2.1 [ABA Invest]
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © abainvest

//@version=5
indicator(shorttitle="BB Signal v2.1 [ABA Invest]", title="BB Signal v2.1 [ABA Invest]", overlay=true)
length = input.int(20, minval=1)
source = close
multiplier = input.float(2, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(source, length)
bb_deviation = multiplier * ta.stdev(source, length)
upper_band = basis + bb_deviation
lower_band = basis - bb_deviation
offset = 0
p1 = plot(upper_band, "Upper", color=color.navy, offset = offset)
p2 = plot(lower_band, "Lower", color=color.navy, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))

ma1 = ta.sma(close, 1)
ma20 = ta.sma(close, 20)
ema50 = ta.ema(close, 50)
ema200 = ta.ema(close, 200)

plot(ma20, "Mid BB", color=color.navy)
plot(ema50, "Ema 50", color=color.red, display=display.none)
plot(ema200, "Ema 200", color=color.white, display=display.none)

upTrend = ma20 > ema50 and ema50 > ema200 and close > ema200 and close[1] > ema200[1]
downTrend = ma20 < ema50 and ema50 < ema200 and close < ema200 and close[1] < ema200[1]
downCross = ma1 > ema200 and ma1[1] <= ema200[1]
upCross = ma1 < ema200 and ma1[1] >= ema200[1]

buySignal = upTrend and close >= upper_band and close[1] >= upper_band[1] and close >= open and close[1] >= open[1]
sellSignal = downTrend and close <= lower_band and close[1] <= lower_band[1] and close <= open and close[1] <= open[1]

var labelsArr = array.new_label(0)
var labelsString = array.new_string(0) 

if (high >= ma20 and downTrend) or downCross
    array.push(labelsString, "UP")

if low <= ma20 and upTrend or upCross
    array.push(labelsString, "DOWN")

if buySignal
    if array.size(labelsString) > 0
        prevLabel = array.get(labelsString, math.max(0, array.size(labelsString) -1))
        if prevLabel == "DOWN" or prevLabel == "RESET_SELL"
            array.push(labelsString, "UP")
            array.push(labelsArr, label.new(x = bar_index, y = math.max(high, high[1]), text = "BUY",color = color.green, textcolor = color.white,style = label.style_label_up, size=size.tiny, yloc = yloc.belowbar ))
    if array.size(labelsString) <= 0
        array.push(labelsString, "UP")
        array.push(labelsArr, label.new(x = bar_index, y = math.max(high, high[1]), text = "BUY",color = color.green, textcolor = color.white,style = label.style_label_up, size=size.tiny, yloc = yloc.belowbar ))
   
if sellSignal
    if array.size(labelsString) > 0
        prevLabel = array.get(labelsString, math.max(0, array.size(labelsString) -1))
        if prevLabel == "UP" or prevLabel == "RESET_BUY"
            array.push(labelsString, "DOWN")
            array.push(labelsArr, label.new(x = bar_index, y = math.min(low, low[1]), text = "SELL",color = color.red,textcolor = color.white,style = label.style_label_down, size=size.tiny, yloc = yloc.abovebar ))
    if array.size(labelsString) <= 0
        array.push(labelsString, "DOWN")
        array.push(labelsArr, label.new(x = bar_index, y = math.min(low, low[1]), text = "SELL",color = color.red, textcolor = color.white,style = label.style_label_down, size=size.tiny, yloc = yloc.abovebar ))
