History referencing in Pine Script arrays

送分小仙女□ 提交于 2020-12-15 06:22:26

问题


I have been trying to use array feature, which recently has been introduced in PineScript 4, but it seams that either I'm not aware of its limitations, or , possibly the implementation is still buggy. The problem I'm faced with is illustrated by the following very simple script:

//@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)

According to my understanding both A and the first element of B array must produce identical graphs. Nevertheless plot of B simply gives 1 on all bars. The problem is definitely related to the usage of history referencing operator []. Does anybody know how to overcome this kind of issue?

Note: I've made this script as simple as possible in order to get to the guts of the problem. The script I'm working on is much more complex, and it uses arrays inside for-loops in various ways, including the one that has been just illustrated (i.e. history referencing op), so using simple variables in place of array simply doesn't work for me.


回答1:


  1. Past instances of array id’s or elements cannot be referenced directly using Pine’s [ ] history-referencing operator (https://www.tradingview.com/pine-script-docs/en/v4/essential/Arrays.html?highlight=array#history-referencing)
  2. Fixed version of your example:
//@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)


来源:https://stackoverflow.com/questions/64638670/history-referencing-in-pine-script-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!