Reputation: 2973
I am using Pinescript under Tradingview.
I am trying to assign a list of items to an array.
//@version=4
study("FTA", overlay=true)
top = input(title="Top", type=input.float, defval=10000, minval=1)
bottom = input(title="Bottom", type=input.integer, defval=5000, minval=0)
plot(bottom , title="bottom" , linewidth=3, color=color.red)
plot(top , title="top" , linewidth=3, color=color.lime)
[ ... snip ...]
res2 = bottom + diff * 2
var ch_2= input(false, "2")
plot( ch_2? res2 : na, title="fib-2", linewidth=3, color=color.lime)
// testing
more_tests = ['Ada', 'Belle', 'Chris'] // LIST OF ITEMS
When saving the document, I get this error:
Syntax Error at input '['
QUESTION: Why am I getting this error
Upvotes: 0
Views: 2374
Reputation: 21294
Arrays do not exist in pine-script
.
What you think an array is called history reference operator and it is used to access historical values of the previous bars.
You can have a look at this workaround if you are working with numeric values.
I also have my own workaround which uses a for loop (therefore not as efficient), which I find easier to understand.
//@version=4
study("Arrays", max_bars_back=1000)
getValFromArr(src, idx, initVal) =>
retval = -1.0 // Return -1.0 in case there is no valid data found
arrIdx = 0
for i = 1 to 10000 // Use a big number for the loop. It is expected that the desired value can be found within this range
if (not na(src[i])) // Check if the source has valid value at this index
if (src[i] == initVal) // Use the initial value to determine if we have looped through each element of the array
retval := -1.0
break // Reached the initial value of the array and couldn't found the desired value, so break
else
arrIdx := arrIdx + 1 // Array has some valid value at this index, so increase the array index
if (arrIdx == idx) // When the array index reaches the desired index
retval := src[i] // Return the value
break // Found the value -> terminate the loop
retval
// Calculate SMMA
smma = 0.0
smma := na(smma[1]) ? sma(close, 7) : (smma[1] * (7 - 1) + close) / 7 // src: close, len: 7
// Write the close value into array when close crosses over smma (condition)
var arrInitVal = -1.0 // Initial value of the array. Should be set to a value that array won't have further on
var coArr = arrInitVal // Declare the array with the initial value
coArr := crossover(close, smma) ? close : na // Only add value to this series, if it satisfies your conditon! Otherwise, write na!
valAtIdx = getValFromArr(coArr, 2, arrInitVal) // Get the second last value of coArr
myCo = valAtIdx == -1 ? na : valAtIdx // Check if the return value is -1, write na or whatever you want in that case
plot(series=coArr, color=color.red, linewidth=2) // Plot the array. This should only have valid values when the condition is satisfied and na otherwise
plot(series=myCo, color=color.green, linewidth=2) // This should have the second last value of coArr and keep its value until the contidition is satisfied again
The idea behind my workaround is, you only assign a valid value to the "array", if your condition is satisfied. Otherwise, you assign na
. Then when you want to get a value from the array, you just loop over each historical values of the "array" and look for non na
values.
In this example, we only want to keep track of values when close
crosses over smma
line. So, below line will check for that and add the close
value to the array, if the condition is met. Otherwise, it will add a na
.
coArr := crossover(close, smma) ? close : na
Then you can use the getValFromArr()
function to get the nth value from last of the array.
Upvotes: 1