获取Series中上一个不同的值

Get the previous different value in Series

我正在创建和指示器,我希望在下图中看到的每个指示颜色变化时,也能知道前一个颜色。所以在任何时候我都想知道我是在黄色区域,前一个是红色,或者我是黄色,前一个是绿色。

我如何在 Pinescript 中做到这一点?

您需要一个 var 变量,以便在每次执行之间保留其值。

然后你可以有一些颜色代码。如果颜色与之前的颜色不同,则将之前的颜色存储在 var 变量中。

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