从系列中获取恒定值

Getting a constant value from a series

我想给一个变量赋一个常量值。这个值来自一个系列。该值是前 10 根柱线,除非有新柱线,否则必须保持不变。

我尝试了这段代码和它的许多变体,但没有成功。

//@version=4
study(title = "X", overlay = false)

var x = 0.0
x := valuewhen(barstate.islast, sum(cht_acum[10], 10), 1)

plot(x, title = "X")

我没有得到一条直线,或者我得到了 NA。如何解决问题?

版本 1

很难弄清楚你想要什么。这可能会接近。您可以使用输入仅在最后一根柱上绘制:

//@version=4
study(title = "X", overlay = false)
cht_acum = close
plotOnLastBarOnly = input(false)
x = sum(cht_acum[10], 10)
plot(barstate.islast or not plotOnLastBarOnly ? x : na, title = "X")

版本 2

此版本使用 Functions Allowing Series As Length - PineCoders FAQ 中 alexgrover 的出色 Sum() 函数,它接受系列长度。应该更贴近您的需求:

//@version=4
study("")
Sum(src,p) => a = cum(src), a - a[max(p,0)]
cond = rising(close, 30)
sourceSeries = 1.
var count = 0.
if cond
    count := 1.
else
    count := min(10, count + 1)
total = Sum(sourceSeries, count)
plot(total)
plotchar(cond, "cond", "•", location.top, size = size.tiny)