当 pinescript 中出现条件时如何存储值?只有一次

how do you store a value when a condition happens in pinescript ? ONLY ONCE

假设我想要 2 个 EMA 交叉时的价格,并使用该价格创建另一个变量作为止损,但实时价格总是在实时蜡烛中变化,所以我的止损和止盈总是变化。 我知道我必须使用 varip 但我不知道如何将它与 valuewhen

一起使用

请帮我更正我的代码。谢谢:)

varip float Current_Close = na

/////
Long_Cond = crossover(ema1 , ema2)

////
Current_Close := valuewhen(Long_Cond , close , 0)

///
Stop_loss = Current_Close * 0.995

// "Risk Ratio 1/1"

takeprofit__long := Current_Close * (1 + (1 * ((Current_Close - Stop_loss) / Current_Close)))

我不知道如何在这段代码中将 varipvaluewhen 一起使用。

示例:

//@version=5
indicator("My Script")

captureValueOnce(cond, float value)=>
    var float store = na
    if cond and na(store)
        store := value
    store

Long_Cond = ta.crossover(ta.ema(close, 50) , ta.ema(close, 200))
plot( captureValueOnce(Long_Cond, bar_index) )