[英]Loop an array push with a function in pine script
I'm using pine script and I'm getting different results while doing an array push inside or outside a while loop.我正在使用 pine 脚本,并且在 while 循环内部或外部执行数组推送时得到不同的结果。
Let's say I have a simple function that returns the sum of two values:假设我有一个简单的 function ,它返回两个值的总和:
myFunction(myValue1, myValue2) =>
myValue3 = myValue1 + myValue2
And an array that will store the function's result:还有一个将存储函数结果的数组:
myArray = array.new_float(0)
If I apply multiple array pushes like this:如果我像这样应用多个数组推送:
array.push(myArray, myFunction(1, 0))
array.push(myArray, myFunction(1, 1))
array.push(myArray, myFunction(1, 2))
array.push(myArray, myFunction(1, 3))
array.push(myArray, myFunction(1, 4))
myArray correctly stores the following values: myArray 正确存储以下值:
myArray = [1, 2, 3, 4, 5]
Then I tried the same thing but with the array push inside a while loop:然后我尝试了同样的事情,但是在 while 循环中使用了数组 push:
myInc = 0
while myInc <= 4
array.push(myArray, myFunction(1, myInc))
myInc += 1
And it did not store the same values in the array:并且它没有在数组中存储相同的值:
myArray = [1, 1, 1, 1, 1]
How can I correctly achieve an array push with a function inside a while loop?如何在 while 循环内使用 function 正确实现数组推送?
//@version=5
indicator(title="TEST", overlay=true)
myFunction(myValue1, myValue2) => myValue3 = myValue1 + myValue2
myArray = array.new_float(0)
// array.push(myArray, myFunction(1, 0))
// array.push(myArray, myFunction(1, 1))
// array.push(myArray, myFunction(1, 2))
// array.push(myArray, myFunction(1, 3))
// array.push(myArray, myFunction(1, 4))
var int myInc = 0
myInc := 0
while myInc <= 4
array.push(myArray, myFunction(1, myInc))
myInc += 1
if barstate.islast
label.new(bar_index, high, array.join(myArray, ',') )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.