问题
I want to assign a constant value to a variable. This value comes from a series. The value is 10 bars back, and must stay constant unless there is a new bar.
I tried this code and lots of variations on it, but it didn't work.
//@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")
I don't get a straight line, or I get NA. How to resolve the issue?
回答1:
Version 1
Hard to figure out what you want. This may come close. You can use the Inputs to plot only on the last bar:
//@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")
Version 2
This version uses the brilliant Sum()
function from alexgrover found in Functions Allowing Series As Length - PineCoders FAQ, which accepts a series length. Should be closer to your needs:
//@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)
来源:https://stackoverflow.com/questions/62052149/getting-a-constant-value-from-a-series