Reputation: 99
Arrays are not available in PineScript.
Is there any workaround? Has anyone developed a code, which works as array?
What do I need it for? I would like to count number of touches to each trendline or S/R level.
Upvotes: 3
Views: 11471
Reputation: 307
recently they add arrays and you can use it like this: reference
levels = array.new_int(size = 5, initial_value = na)
for i = 1 to 5
array.push(levels, i)
there is no direct access to array element and you must use it like this
array.get(levels, 0)
Upvotes: 3
Reputation: 1121
RicardoSantos create a pseudo array function. See his Pseudo Array Example.
Upvotes: 0
Reputation: 536
I do not see an official method to create array in Pine Script. However, you can simulate it by walk backward at every new bar/tick/calculation and see how many touches in the past. I'd go for other languages such as C# with separate data feed for this work.
Upvotes: -2
Reputation: 21219
To implement a counter, you can create a variable and then modify its value by using the History Referencing Operator []
.
Below example counts number of crossover/crossunder occurrences in a typical rsi diagram.
//@version=3
study("counter", overlay=false)
rsi_max = 70
rsi_min = 30
cnt_up = 0
cnt_dwn = 0
cnt_up := crossover(rsi(close, 14), rsi_max) ? nz(cnt_up[1]) + 1 : nz(cnt_up[1])
cnt_dwn := crossunder(rsi(close, 14), rsi_min) ? nz(cnt_dwn[1]) + 1 : nz(cnt_dwn[1])
plot(rsi(close, 14), color=orange, title='RSI')
plot(series=cnt_up, title="Up counter", color=green)
plot(series=cnt_dwn, title="Down counter", color=red)
band0 = hline(30)
band1 = hline(70)
fill(band1, band0, color=purple, transp=90)
Green line is the "cnt_up" in my example. After that point, rsi line crosses over the "overbought" zone 7 more times.
And here, you can see that "cnt_up" indeed counted up 7 times.
Unfortunately, this is all you can do.
Upvotes: 4