OB EmaCross + BB
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © OdirBrasil (07-dec-2021)

//@version=5
indicator("OB EmaCross + BB", overlay=true)

bcolorbars = input(true, "Colored Bars?")
iEma1 = input(9, "Fast EMA")
iEma2 = input(21, "Fast EMA")
iEma3 = input(200, "Slow EMA")

length = input.int(20, "Lenght BB", minval=1)
src = input(close, title="Source BB")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev BB")


ema1 = ta.ema(close, iEma1)
ema2 = ta.ema(close, iEma2)
ema3 = ta.ema(close, iEma3)

fColorUp = color.rgb(77, 60, 235)
fColorDown = color.rgb(139, 4, 99)

//fColorUp = ta.crossover(ema3, sma1) ? color.yellow : color.green
//fColorDown = ta.crossover(sma1, ema3) ? color.yellow : color.red

p1 = plot(ema1, color=ema1 > ema2 ? fColorUp : fColorDown, linewidth=2)
p2 = plot(ema2, color=ema1 > ema2 ? fColorUp : fColorDown, linewidth=3)
p3 = plot(ema3, color=color.gray, linewidth=5)
fill(p1, p2, title = "Background", color=ema1 > ema2 ? fColorUp : fColorDown)




bColor = close > ema1 and close > ema2 ? color.green : close < ema1 and close < ema2 ? color.red : color.white

plotcandle(open, high, low, close, color=bcolorbars ? bColor : na, wickcolor=bcolorbars ? bColor : na,  bordercolor=bcolorbars ? bColor : na)

// *** ALERTAS ***

//bLong = high > ema1 and low > ema1 and ema1 > ema2 and ema2 > ema3 and ( high[1] < ema1 or low[1] < ema1 or ema1 < ema2 or ema2 < ema3)
//bShort = high < ema1 and low < ema1 and ema1 < ema2 and ema2 < ema3 and ( high[1] > ema1 or low[1] > ema1 or ema1 > ema2 or ema2 > ema3)

alertcondition(ta.crossover(ema1, ema2), title="Medias em Tendencia de ALTA.", message="{{ticker}} - price: {{close}} - Tendência de Alta")
alertcondition(ta.crossover(ema2, ema1), title="Medias em Tendencia de BAIXA.", message="{{ticker}} - price: {{close}} - Tendência de Baixa")

//alertcondition(bLong, title="Medias em Tendencia de ALTA.", message="{{ticker}} 1H - price: {{close}} - Tendência de Alta")
//alertcondition(bShort,title="Medias em Tendencia de BAIXA.", message="{{ticker}} 1H - price: {{close}} - Tendência de BAIXA")


basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
//plot(basis, "Basis", color=#FF6D00, offset = offset)
p100 = plot(upper, "Upper", color=#2962FF, offset = offset)
p200 = plot(lower, "Lower", color=#2962FF, offset = offset)
fill(p100, p200, title = "Background", color=color.rgb(81, 28, 20, 95))