'how to offset 2 moving averages and change the background color whenever they cross each other?

I created an indicator of crossing over moving averages, when ema1 crosses above ema2 it is an uptrend so the background color changes to green and when the ema1 crosses below ema2 it is a downtrend and the background color changes to red but i want to be able to offset/shift the moving averages and still get the same result whenever they crossover, how do i go about it?

//@version=5

indicator(title="Offset Emas Crossover", shorttitle="OEC", overlay=true, timeframe="", timeframe_gaps=true)

// EMA 1
length_of_ema_1 = input.int(50, minval=1, title="Length", group="EMA 1")
source_of_ema_1 = input(close, title="Source", group="EMA 1")
offset_of_ema_1 = input.int(title="Offset", defval=0, minval=-500, maxval=500, group="EMA 1")
ema_1 = ta.ema(source_of_ema_1, length_of_ema_1)

// EMA 2
length_of_ema_2 = input.int(100, minval=1, title="Length", group="EMA 2")
source_of_ema_2 = input(close, title="Source", group="EMA 2")
offset_of_ema_2 = input.int(title="Offset", defval=0, minval=-500, maxval=500, group="EMA 2")
ema_2 = ta.ema(source_of_ema_2, length_of_ema_2)

// TREND
float color = 255
float transparency = 75
uptrend = ema_1 > ema_2
downtrend = ema_1 < ema_2

// BACKGROUND COLOR
background_color = if uptrend
    color.rgb(0, colour, 0, transparency)
else
    color.rgb(colour, 0, 0, transparency)

// PLOTTING
plot(ema_1, color=color.blue, title="MA", offset=offset_of_ema_1)
plot(ema_2, color=color.red, title="MA", offset=offset_of_ema_2)


Sources

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

Source: Stack Overflow

Solution Source