我可以在 Pine Script 的 IF 条件下保存变量吗?

Can i save variable in IF condation in Pinescript?

如何在 IF 条件下保存变量并稍后显示?

变量在 IF 条件外显示 0

我想显示 v1 和 v2

Click to show the image

//@version=4
study("Test2", shorttitle = "Test2", overlay=true)

v1 = 0.0
v2 = 0.0


r = high - low
large_candle = rising(r,50)

if large_candle
    v1 := high
    v2 := low
    target = "high = " + tostring(v1) + "\n low = " + tostring(v2)
//  label.new(bar_index, high, target, yloc = yloc.abovebar, color = color.red, style = label.style_arrowdown)

if (high-open) > 0.7 * (high-low) and open > close
    target = "Target 1 = " + tostring(v1) + "\n Target 2 = " + tostring(v2)
    label.new(bar_index, high, target, yloc = yloc.abovebar, color = color.black, style = label.style_arrowdown)

barcolor( large_candle ? color.yellow : na)

注意:我真的需要为另一个代码保存那个变量(我把这段代码编码得很简单)。 有什么想法吗?

我认为这是完成工作

如果有更好的答案就好了:)

//@version=4
study("Test2", shorttitle = "Test2", overlay=true)

v1 = 0.0
v2 = 0.0
v1 := nz(v1[1])
v2 := nz(v2[1])


r = high - low
large_candle = rising(r,50)

if large_candle
    v1 := high
    v2 := low
    target = "high = " + tostring(v1) + "\n low = " + tostring(v2)
    label.new(bar_index, high, target, yloc = yloc.abovebar, color = color.red, style = label.style_arrowdown)

if (high-open) > 0.7 * (high-low) and open > close
    target = "Target 1 = " + tostring(v1) + "\n Target 2 = " + tostring(v2)
    label.new(bar_index, high, target, yloc = yloc.abovebar, color = color.black, style = label.style_arrowdown)

barcolor( large_candle ? color.yellow : na)