pine-scriptpine-script-v5pine-script-v4

colour background when condition triggers UNTIL another triggers


I am plotting green dots on an indicator. When all dots turn green i want the background to turn green. The background should remain green (So 1 or more dots can turn red) until ALL dots turn red. then i want the background to turn red until ALL dots turn green again.

right now i have this:

bull = (ha_diff==2 and ha_diff2==2 and ha_diff3==2 and ha_diff4==2 and ha_diff5==2 and ha_diff6==2 and ha_diff7==2 and ha_diff8==2 )
bear = (ha_diff==1 and ha_diff2==1 and ha_diff3==1 and ha_diff4==1 and ha_diff5==1 and ha_diff6==1  and ha_diff7==1 and ha_diff8==1 )

plot(15, title="TF1", color=iff(ha_diff==1, red, iff(ha_diff==2, green, white)), style=circles, linewidth=5, join=true)
plot(14, title="TF2", color=iff(ha_diff2==1, red, iff(ha_diff2==2, green, white)), style=circles, linewidth=5, join=true)
plot(13, title="TF3", color=iff(ha_diff3==1, red, iff(ha_diff3==2, green, white)), style=circles, linewidth=5, join=true)
plot(12, title="TF4", color=iff(ha_diff4==1, red, iff(ha_diff4==2, green, white)), style=circles, linewidth=5, join=true)
plot(11, title="TF5", color=iff(ha_diff5==1, red, iff(ha_diff5==2, green, white)), style=circles, linewidth=5, join=true)
plot(10, title="TF6", color=iff(ha_diff6==1, red, iff(ha_diff6==2, green, white)), style=circles, linewidth=5, join=true)
plot(9, title="TF7", color=iff(ha_diff7==1, red, iff(ha_diff7==2, green, white)), style=circles, linewidth=5, join=true)
plot(8, title="TF8", color=iff(ha_diff8==1, red, iff(ha_diff8==2, green, white)), style=circles, linewidth=5, join=true)


// Colour background
backgroundColour = (bull) ? green : red

bgcolor(color=backgroundColour, transp=40,
     title="Conditionally coloured background")

but what this does is it only paint the background green when all dots are green. as soon as one dot turns red it paints red, and it should continue to paint green until all dots are red...

can anyone help?


Solution

  • As is shown in the example below, assign a color to the backgroundColour variable only when the bull/bear signal trigger, otherwise use the previous value:

    // Colour background
    backgroundColour = white
    backgroundColour := bull ? green : bear ? red : backgroundColour[1]
    

    enter image description here