在全局变量中保存 last swinghigh 和 last swinglow

Persist last swinghigh and last swinglow in global variable

试图将最后一个摆动高点和摆动低点保存在一个全局变量中。根据我的理解,食谱是:

到目前为止,我尝试以这种方式识别和存储最后 red/green 支蜡烛:

var last_green_candle = (close > open)
var last_red_candle = (open > close)

我的想法是我可以存储这个“对象”或者它的 high/low 值,但看起来这个表达式只是 returns 事件发生的栏上的一个布尔值,但后来我无法访问high/low.

的值

我正在浏览文档,但我可能遗漏了一些解释如何访问特定条形数据并存储它的重要部分。非常感谢任何意见

(close > open)(open > close) 都是比较结果 booleanclose 大于 open 或不大于 (true/false)。

您需要做的是,使用三元运算符来更新值。

var last_green_candle = 0.0
var last_red_candle = 0.0

last_green_candle := (close > open) ? high : last_green_candle  // If it's a green candle, update the value with the new high, else, keep the old value
last_red_candle := (open > close) ? low : last_red_candle   // If it's a redcandle, update the value with the new low, else, keep the old value

您可以使用ta.valuewhen()函数。

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

last_green_candle = ta.valuewhen(close > open, high, 0)
last_red_candle =  ta.valuewhen(open > close, low, 0)

plot(last_green_candle, color = color.green)
plot(last_red_candle, color = color.red)