我想抓住这一刻并努力,即使它在未来再次发生(CA)

I want to catch this moment and work on it even if it happens again in the future (CA)

em20 = ta.ema(close, 20)
em50 = ta.ema(close, 50)

Signal = ta.crossover(em20, em50)

CA = ta.valuewhen(Signal, close, 0)

var 关键字可让您保存一个值,而不会一遍又一遍地重置它。

来自 documentation:

When the var keyword is used, the variable is only initilized once, on the first bar if the declaration is in the global scope, or the first time the local block is executed if the declaration is inside a local block. After that, it will preserve its last value on successive bars, until we reassign a new value to it. This behavior is very useful in many cases where a variable’s value must persist through the iterations of a script across successive bars.

您可以使用此功能来仅在第一个 ta.crossover() 上保留 close 的值,前提是您要先保存叉数,然后再保存该值。这是一个例子:

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

em20 = ta.ema(close, 20) 
em50 = ta.ema(close, 50)

var numberOfTimesCrossed = 0

Signal = ta.crossover(em20, em50)

if Signal
    numberOfTimesCrossed += 1

var CA = float(na)

if numberOfTimesCrossed == 1
    CA := ta.valuewhen(Signal, close, 0)

plot(nz(CA))