Reputation: 3432
I'm creating and indicator and I would like at every indicated colour change as seen in the image below, to also know the immediately previous colour. So at any point I would like to know that I am e.g. in a yellow area, and the previous one was red, or I'm on yellow and the previous one was green.
How can I do that in Pinescript?
Upvotes: 0
Views: 833
Reputation: 21362
You need a var
variable so that its value is kept between each execution.
Then you can have some codes for the colors. If the color is different than previous one, store the previous color in the var
variable.
var int last_color = 0 // 1: Red 2: Yellow 3: Green
last_color := (current_color != current_color[1]) ? current_color[1] : last_color // If the current color is not equal to previous color, assign previous color to this variable. If they are equal, do not change last_color's value
Upvotes: 1