Arrays Pine脚本数组中的历史引用

Arrays Pine脚本数组中的历史引用,arrays,pine-script,Arrays,Pine Script,我一直在尝试使用阵列功能,最近在PineScript 4中引入了该功能,但它可能是我不知道它的局限性,或者可能是实现仍然有缺陷。 以下非常简单的脚本说明了我面临的问题: //@version=4 study("TEST") // A is basically the same as bar_index+1, and it is plotted as expected A=0 A:=nz(A[1])+1 // the same thing implemented usin

我一直在尝试使用阵列功能,最近在PineScript 4中引入了该功能,但它可能是我不知道它的局限性,或者可能是实现仍然有缺陷。 以下非常简单的脚本说明了我面临的问题:

//@version=4

study("TEST")

// A is basically the same as bar_index+1, and it is plotted as expected
A=0
A:=nz(A[1])+1

// the same thing implemented using arrays nevertheless doesn't work as expected
B=array.new_float(1,0)
array.set(B,0,nz(array.get(B,0)[1])+1)

plot(A,color=color.red)
plot(array.get(B,0),color=color.yellow)
根据我的理解,AB数组的第一个元素必须生成相同的图。 然而,B的曲线图在所有条上仅给出1。 这个问题肯定与历史引用操作符[]的使用有关。 有人知道如何克服这种问题吗

注意:为了深入了解问题的本质,我已经尽可能地简化了这个脚本。我正在编写的脚本要复杂得多,它以各种方式使用内部数组进行循环,包括刚才演示的脚本(即历史引用op),因此使用简单变量代替数组对我来说根本不起作用

  • 无法使用Pine的[]历史引用运算符()直接引用数组id或元素的过去实例
  • 您的示例的固定版本:

  • 感谢您的输入,Andrey,但是您修复脚本的方式忽略了数组元素中历史引用问题的整个要点。您是对的,不能使用Pine的[]历史引用操作符直接引用数组id或元素的过去实例。文档中的例子清楚地解释了这一点:不能编写类似于
    array.get(a[1],0)
    的东西。然而,使用
    array.get(a,0)[1]
    是非常好的(请参见您所指的同一章中的示例)。因此,我仍然认为数组中的历史引用实现存在问题。同样,我的测试脚本的目的不是找到一种方法,通过将历史引用操作符全部去掉,使a和B图的第一个元素相同,而是说明数组中的历史引用问题,因为我确实需要在真实脚本中访问数组元素历史记录。我认为,即使表达式
    array.get(a,0)[1]
    编译成功,操作符
    [1]
    在运行时不适用于任何内容。你是对的,它不适用。我认为这是一个错误。否则,他们为什么首先在自己的文档中给出这种构造呢。我会偶尔继续运行这段代码。我已经在tradingvidew网站上提交了几个类似于此的与阵列相关的bug,它们最终已经被修复。
    //@version=4
    
    study("TEST")
    
    // A is basically the same as bar_index+1, and it is plotted as expected
    A=0
    A:=nz(A[1])+1
    
    // the same thing implemented using arrays nevertheless doesn't work as expected
    var B=array.new_float(1,0)
    array.set(B,0,nz(array.get(B,0))+1)
    
    plot(A,color=color.red)
    plot(array.get(B,0),color=color.yellow)