3 Fib EMAs To Scalp Them All
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JohannCoffee


//@version=5
indicator("3 Fib EMAs To Scalp Them All", shorttitle="Fib EMAs", overlay=true)

// EMA inputs
input_ema1 = input(21, title="Micro EMA", group = "Choose Your EMA")
input_ema2 = input(55, title="Mid EMA", group = "Choose Your EMA")
input_ema3 = input(233, title="Macro EMA", group = "Choose Your EMA")

// Bollinger Bands
length = input(200, title = "Bands length")
src = input(hlc3, title="Source")
mult = input.float(defval = 3.0, title = "Mult", minval=0.001, maxval=50)

basis = ta.vwma(src, length)
dev = mult * ta.stdev(src, length)
upper= basis + (1*dev)
lower= basis - (1*dev)

ema1 = ta.ema(close, input_ema1)
ema2 = ta.ema(close, input_ema2)
ema3 = ta.ema(close, input_ema3)

var color color_ema1 = na
var color color_prev = na

color_ema1 := ema1 < ema2 and ema2 < ema3 ? color.rgb(255, 0, 0) : ema1 > ema2 and ema2 > ema3 ? color.rgb(0, 255, 8) : color.new(#f7fff7, 0)

redDiamondCondition = na(color_ema1[1]) ? na : color_ema1 == color.rgb(255, 0, 0) and color_prev != color.rgb(255, 0, 0)
greenDiamondCondition = na(color_ema1[1]) ? na : color_ema1 == color.rgb(0, 255, 8) and color_prev != color.rgb(0, 255, 8)
redCrossCondition = na(color_ema1[1]) ? na : color_prev == color.rgb(255, 0, 0) and color_ema1 != color.rgb(255, 0, 0)
greenCrossCondition = na(color_ema1[1]) ? na : color_prev == color.rgb(0, 255, 8) and color_ema1 != color.rgb(0, 255, 8)

color_prev := color_ema1

// Plotting EMAs
line1 = plot(ema1, color=color_ema1 ,title="Micro EMA")
line2 = plot(ema2, color=color.new(#4ecdc4,0), title="Mid EMA")
line3 = plot(ema3, color=color.new(#1a535c,0), title="Macro EMA")

fill(line1, line2, color=color.new(#4ecdc4,70))
fill(line2, line3, color=color.new(#1a535c,90))

// Plot RS Fib Bands
plot(upper, color=color.rgb(14,100,84), linewidth=2, title="Upper Band")
plot(lower, color=color.rgb(14,100,84), linewidth=2, title="Lower Band")

// Plot signals
plotshape(series=redDiamondCondition, location=location.abovebar, color=color.rgb(255, 0, 0), style=shape.diamond, offset = -1, size = size.small, title="Downtrend")
plotshape(series=redCrossCondition, location=location.abovebar, color=color.rgb(255, 0, 0), style=shape.xcross, offset = -1, size = size.small, title="Downtrend end")
plotshape(series=greenDiamondCondition, location=location.belowbar, color=color.rgb(0, 255, 8), style=shape.diamond, offset = -1, size = size.small, title="Uptrend")
plotshape(series=greenCrossCondition, location=location.belowbar, color=color.rgb(0, 255, 8), style=shape.xcross, offset = -1, size = size.small, title="Uptrend end")


// Alerts
alertcondition(redDiamondCondition, title="Downtrend", message="Red Diamond: Time to short!")
alertcondition(redCrossCondition, title="Downtrend Stopped", message="Red Cross: End of downtrend, exit short positions!")
alertcondition(greenDiamondCondition, title="Uptrend", message="Green Diamond: Time to go long!")
alertcondition(greenCrossCondition, title="Uptrend Stopped", message="Green Cross: End of uptrend, exit long positions!")
//




