1

Let's say I have two emas, ema15 and ema50 both in red.

ema15 = ta.ema(close,15)
ema50 = ta.ema(close,50)
plot(ema15, color = color.red)
plot(ema50, color = color.red)

Now ema15 crossunder ema50 30 bars ago and the price is below ema15.

I want to change the ema color to yellow when the price is greater then ema50 until price is less than ema15 or ema15 crossover ema50, then change the color back.

I tried close>ema50 and ta.crossover(close,ema50), both didn't work.

Only the part exceed ema50 will change color, once price below ema50 and higher than ema15 the color will not change.

Need help desperatly.

Thanks a lot.

Luke Li
  • 15
  • 2

1 Answers1

0

Please check this code:

//@version=5
indicator("My script", overlay = true)


ema15 = ta.ema(close,15)
ema50 = ta.ema(close,50)

turnYellow = close > ema50 
turnRed = close < ema15 or ta.crossover(ema15, ema50)

color emaColor = na
emaColor := turnYellow ? color.yellow : turnRed ? color.red : emaColor

plot(ema15, color = emaColor)
plot(ema50, color = emaColor, linewidth = 3)
Moebius
  • 329
  • 1
  • 7