在Tradingview下使用数组时在输入'['处获取语法错误

时间:2020-05-13 21:38:17

标签: arrays pine-script

我在Tradingview下使用Pinescript。

我正在尝试将项目列表分配给数组。

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

保存文档时,出现此错误:

输入'['

时出现

语法错误

问题:为什么会出现此错误

1 个答案:

答案 0 :(得分:1)

pine-script中不存在数组。

您认为数组称为history reference operator,它用于访问先前柱线的历史值。

如果要使用数字值,可以查看this的解决方法。

我也有自己的解决方法,该方法使用for循环(因此效率不高),使我更容易理解。

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

我的解决方法背后的想法是,如果满足条件,则仅将有效值分配给“数组”。否则,您分配na。然后,当您想从数组中获取一个值时,只需遍历“数组”的每个历史值,然后查找非na值即可。

在此示例中,我们只希望在close越过smma行时跟踪值。因此,如果满足条件,则在下面的行中将进行检查并将close值添加到数组中。否则,它将添加一个na

coArr := crossover(close, smma) ? close : na

然后,您可以使用getValFromArr()函数从数组的最后一个获取第n个值。