Here's an example og toggle study
Inside the script you can find the toggle function that can help you to create series that maintain value or change value
each time a source crosses some level
In the example I applied the function to RSI plot changing the color each time a fund an overbuy or underbuy level
Please, don't exitate to contact me if you need more help.