我想建立多个最高点的集合,并像在数组中一样访问所有这些最高点。我怎么做?我的代码如下:
var float lastHigh = 0
if (highfound)
lastHigh := high
现在我尝试这个:
x := lastHigh[3]
...但是x只是最后的lastHigh值(就像它将是lastHigh [1])。所以lastHigh只是一个“扁平”变量,对吧?我如何收集多于最后的高点?
假设您的highfound
函数未在原始问题中演示,但结果为false或true,则您的代码应通过从:
变量中删除x
来工作:
x = lastHigh[3]
以下内容对我来说很好:
//@version=4
study("My Script")
var float lastHigh = 0
if (close>open)
lastHigh := high
x = lastHigh[3]
plot(x, color=color.red)
我想出了下一种方法:
//@version=4
study("Three last highs", overlay=true)
// here goes the logic for finding the high
// NOTE: if you are updating this function,
// then it should return either new value for the array or n/a.
// Because the non-n/a value will be added to the array and the oldest removed
getNewHighOrNa() =>
pivothigh(high, 3, 3)
newHigh = getNewHighOrNa()
// ======= Array lifecicle =========
ARRAY_LENGTH = 3
arrayCell = label(na)
if bar_index < ARRAY_LENGTH
arrayCell := label.new(0, 0)
label.set_y(arrayCell, 0)
else
if na(newHigh)
val = label.get_y(arrayCell[1])
for i = 2 to ARRAY_LENGTH
v = label.get_y(arrayCell[i])
label.set_y(arrayCell[i-1], v)
arrayCell := arrayCell[ARRAY_LENGTH]
label.set_y(arrayCell, val)
else
arrayCell := arrayCell[ARRAY_LENGTH]
label.set_y(arrayCell, newHigh)
// ==================================
// Array getter by index. Note, that numeration is right to left
// so 0 is the last bar (current) and 1 it's a left to current bar
get(index) => label.get_y(arrayCell[index])
// example of using of the array for calculation average of all elements of the array
mean() =>
sum = 0.0
for i = 0 to ARRAY_LENGTH - 1
sum := sum + get(i) / ARRAY_LENGTH
sum
plot(mean(), color=color.green)
// the rest is just checking that the code works:
plot(get(0))
plot(get(1))
plot(get(2))
// mark the bars where we found new highs
plotshape(not na(newHigh), style=shape.flag)
我试图使它变得更容易,但是由于松木的限制,没有对其进行管理。但是此代码有效,您可以获得最后的3个局部高点,并且可以对其进行迭代。希望我能正确回答你的问题。