TradingView just recently announced the support of arrays in PineScript. And it’s a pretty big deal. Many developers waited for that for years.

I want to explain to you by example how easier life will be in PineScript with arrays. Previously, if you need any arrays functionality, you had to “simulate” the arrays manually on single variables, and it was a nightmare. Script grew to hundreds of lines quickly, and it was a massive headache to develop this script or debug.

Here is an example of a script I developed before arrays were introduced to PineScript. The idea of this script is pretty simple. I just want to calculate the average of the last X pivot points. To implement it without arrays it took me 100+ lines of code for max 25 average pivots supported. And you can see that code is really ugly:

```
//@version=4
strategy("Average Pivot Reversal Strategy", overlay = true)
// Inputs
leftBars = input(4)
rightBars = input(2)
avg_length = input(25, maxval = 25)
swh = pivothigh(leftBars, rightBars)
swl = pivotlow(leftBars, rightBars)
// Defining Highs "Array"
var float H01 = na, var float H02 = na, var float H03 = na, var float H04 = na, var float H05 = na
var float H06 = na, var float H07 = na, var float H08 = na, var float H09 = na, var float H10 = na
var float H11 = na, var float H12 = na, var float H13 = na, var float H14 = na, var float H15 = na
var float H16 = na, var float H17 = na, var float H18 = na, var float H19 = na, var float H20 = na
var float H21 = na, var float H22 = na, var float H23 = na, var float H24 = na, var float H25 = na
// Defining Lows "Array"
var float L01 = na, var float L02 = na, var float L03 = na, var float L04 = na, var float L05 = na
var float L06 = na, var float L07 = na, var float L08 = na, var float L09 = na, var float L10 = na
var float L11 = na, var float L12 = na, var float L13 = na, var float L14 = na, var float L15 = na
var float L16 = na, var float L17 = na, var float L18 = na, var float L19 = na, var float L20 = na
var float L21 = na, var float L22 = na, var float L23 = na, var float L24 = na, var float L25 = na
// Add new element to Highs "Array"
if (not na(swh))
H25 := H24, H24 := H23, H23 := H22, H22 := H21, H21 := H20
H20 := H19, H19 := H18, H18 := H17, H17 := H16, H16 := H15
H15 := H14, H14 := H13, H13 := H12, H12 := H11, H11 := H10
H10 := H09, H09 := H08, H08 := H07, H07 := H06, H06 := H05
H05 := H04, H04 := H03, H03 := H02, H02 := H01, H01 := swh
// Add new element to Lows "Array"
if (not na(swl))
L25 := L24, L24 := L23, L23 := L22, L22 := L21, L21 := L20
L20 := L19, L19 := L18, L18 := L17, L17 := L16, L16 := L15
L15 := L14, L14 := L13, L13 := L12, L12 := L11, L11 := L10
L10 := L09, L09 := L08, L08 := L07, L07 := L06, L06 := L05
L05 := L04, L04 := L03, L03 := L02, L02 := L01, L01 := swl
// Calculating Average Pivot High
pivot_high_avg = ((avg_length > 0 ? H01 : 0) +
(avg_length > 1 ? H02 : 0) +
(avg_length > 2 ? H03 : 0) +
(avg_length > 3 ? H04 : 0) +
(avg_length > 4 ? H05 : 0) +
(avg_length > 5 ? H06 : 0) +
(avg_length > 6 ? H07 : 0) +
(avg_length > 7 ? H08 : 0) +
(avg_length > 8 ? H09 : 0) +
(avg_length > 9 ? H10 : 0) +
(avg_length > 10 ? H11 : 0) +
(avg_length > 11 ? H12 : 0) +
(avg_length > 12 ? H13 : 0) +
(avg_length > 13 ? H14 : 0) +
(avg_length > 14 ? H15 : 0) +
(avg_length > 15 ? H16 : 0) +
(avg_length > 16 ? H17 : 0) +
(avg_length > 17 ? H18 : 0) +
(avg_length > 18 ? H19 : 0) +
(avg_length > 19 ? H20 : 0) +
(avg_length > 20 ? H21 : 0) +
(avg_length > 21 ? H22 : 0) +
(avg_length > 22 ? H23 : 0) +
(avg_length > 23 ? H24 : 0) +
(avg_length > 24 ? H25 : 0) ) / avg_length
// Calculating Average Pivot Low
pivot_low_avg = ((avg_length > 0 ? L01 : 0) +
(avg_length > 1 ? L02 : 0) +
(avg_length > 2 ? L03 : 0) +
(avg_length > 3 ? L04 : 0) +
(avg_length > 4 ? L05 : 0) +
(avg_length > 5 ? L06 : 0) +
(avg_length > 6 ? L07 : 0) +
(avg_length > 7 ? L08 : 0) +
(avg_length > 8 ? L09 : 0) +
(avg_length > 9 ? L10 : 0) +
(avg_length > 10 ? L11 : 0) +
(avg_length > 11 ? L12 : 0) +
(avg_length > 12 ? L13 : 0) +
(avg_length > 13 ? L14 : 0) +
(avg_length > 14 ? L15 : 0) +
(avg_length > 15 ? L16 : 0) +
(avg_length > 16 ? L17 : 0) +
(avg_length > 17 ? L18 : 0) +
(avg_length > 18 ? L19 : 0) +
(avg_length > 19 ? L20 : 0) +
(avg_length > 20 ? L21 : 0) +
(avg_length > 21 ? L22 : 0) +
(avg_length > 22 ? L23 : 0) +
(avg_length > 23 ? L24 : 0) +
(avg_length > 24 ? L25 : 0) ) / avg_length
plot(pivot_low_avg, color = color.red)
plot(pivot_high_avg, color = color.green)
strategy.entry("long", strategy.long, stop = pivot_high_avg + syminfo.mintick)
strategy.entry("short", strategy.short, stop = pivot_low_avg - syminfo.mintick)
```

As you can see every simple operation with arrays like define it or add an element took me a dozen lines of code and it’s not pleasant to work with this kind of code.

So I took this code and rewrote it using arrays and you can see that now it is much shorter and readable that it was before:

```
//@version=4
strategy("Average Pivot Reversal Strategy", overlay = true)
// Inputs
leftBars = input(4)
rightBars = input(2)
avg_length = input(25, maxval = 25)
swh = pivothigh(leftBars, rightBars)
swl = pivotlow(leftBars, rightBars)
var H = array.new_float(0)
var L = array.new_float(0)
if (not na(swh))
array.unshift(H, swh)
if (not na(swl))
array.unshift(L, swl)
pivot_low_avg = array.size(L) >= avg_length ? array.avg(array.slice(L, 0, avg_length)) : na
pivot_high_avg = array.size(H) >= avg_length ? array.avg(array.slice(H, 0, avg_length)) : na
plot(pivot_low_avg, color = color.red)
plot(pivot_high_avg, color = color.green)
strategy.entry("long", strategy.long, stop = pivot_high_avg + syminfo.mintick)
strategy.entry("short", strategy.short, stop = pivot_low_avg - syminfo.mintick)
```

So with arrays, your life coding in PineScript can become much easier.

This is just an example, I will probably create more examples/tutorials about alerts in PineScript. This topic is pretty big, pine script has 30+ arrays related functions.

If you want to learn PineScript deeper, check out my PineScript Programming Course. I’m currently working on a big section about arrays, where I will explain to you all aspects of arrays in detail.

Hi there,

just wondering if your earlier screeners could be re-written using arrays? I’m not asking you to do it – I’ll try to do that – but I’m wondering if there are constraints that make the effort not even worthwhile attempting?

Cheers,

Pete

Hi Pete.

I don’t see many benefits in rewriting scripts with arrays. I have to get all the symbols explicitly with the security function. I can’t do that in a loop.

Hi, is it now possible to loop through arrays? For example, in python: (l – x) for x in array

I created a small article about it: https://quantnomad.com/faq-how-to-loop-through-arrays-in-pine-script/

Hi, I am working with a script which uses an array and a resolution input to populate it. I’m now trying to avoid having to input the resolution manually when I switch between securities by finding the most detailed resolution which doesn’t go over the maximum array size (100,000).

Is there any way to catch that error within the script or to limit the max array size to within 100,000? Any pointer would be much appreciated, cheers!

Hi Phil, I’m not sure I understand your issue.