Buy/Sell EMA Crossover
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AleSaira

//@version=5
indicator(title = "Buy/Sell ema crossover", shorttitle="Buy/Sell ema crossover", overlay=false)

emaLengthInput = input.int(200, title="EMA Length")
ema200 = ta.ema(close, emaLengthInput)


ema13LengthInput = input.int(13, title="EMA 13 Length")
ema21LengthInput = input.int(21, title="EMA 21 Length")
ema13 = ta.ema(close, ema13LengthInput)
ema21 = ta.ema(close, ema21LengthInput)

// Calculate crossover and crossunder conditions
ema21_cross_over_ema200 = ta.crossover(ema21, ema200)
ema21_cross_under_ema200 = ta.crossunder(ema21, ema200)
ema13_cross_over_ema21 = ta.crossover(ema13, ema21)
ema13_cross_under_ema21 = ta.crossunder(ema13, ema21)

// Calculate src and pos
src = close
pos = 0
pos := src[1] < nz(ema21[1], src) ? 1 :
       src[1] > nz(ema21[1], src) ? -1 :
       nz(pos[1], 0)

// Define colors for background
color colorBg = na
if (ema200 > src)
    colorBg := pos == -1 ? color.rgb(192, 13, 237) : na
else
    colorBg := pos == 1 ? color.rgb(13, 222, 241) : na

// Apply the background color
bgcolor(colorBg)

// Plot triangles only on crossovers and crossunders
plotshape(ema21_cross_over_ema200, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(ema21_cross_under_ema200, style=shape.triangledown, location=location.abovebar, color=color.rgb(166, 10, 10), size=size.small)
plotshape(ema13_cross_over_ema21, style=shape.triangleup, location=location.belowbar, color=color.white, size=size.small)
plotshape(ema13_cross_under_ema21, style=shape.triangledown, location=location.abovebar, color=color.purple, size=size.small)

// Debugging Plots
plot(ema200, color=color.blue, title="EMA 200")
plot(ema13, color=color.rgb(248, 246, 244), title="EMA 13")
plot(ema21, color=color.purple, title="EMA 21")
