如何在使用for循环时正确将值保存到数组中。

huangapple go评论51阅读模式
英文:

How to properly save values into an array when using for loop

问题

我试图在每次循环迭代中将`cumDistance`推入`cumDistances`数组中,但我遇到了问题。

    var inTrade = false
    var entry = 0.
    var exit = 0.
    distance = 0.
    
    entryCon = ta.crossover(ta.rsi(close, 10), 20) and not inTrade
    exitCon = ta.crossunder(ta.rsi(close, 10), 80) and inTrade
    
    cumDistances = array.new_float()
    for i=1 to 5
        if entryCon[1]
            entry := close[1]
            inTrade := true
    
        if exitCon[1]
            exit := close[1]
            inTrade := false
            distance := exit - entry*i
    
        cumDistance = ta.cum(distance)
        array.push(cumDistances, cumDistance)

在`cumDistances`数组中,我得到的值都是相同的:
 1. -43.41588
 2. -43.41588
 3. -43.41588
 4. -43.41588
 5. -43.41588


    if entryCon[1]
        entry := close[1]
        inTrade := true
    
    if exitCon[1]
        exit := close[1]
        inTrade := false
        distance := exit - entry*i //将 i 改为 1,2,3,4,5
    
    cumDistance = ta.cum(distance)

如果我运行没有for循环的代码(就像上面的代码),这是我得到的`cumDistance`值,对应它们的迭代:

 1. 0.02016
 2. -10.83885
 3. -21.69786
 4. -32.55687
 5. -43.41588

看起来`cumDistances`数组只保存了for循环的最后一次迭代的`cumDistance`值到所有数组索引中。

我该如何修复这个问题?

**编辑:**
回应 elod008:

    cumDistances = array.new_float()
    for i=1 to 5
        var distances = array.new_float()
        if entryCon[1]
            entry := close[1]
            inTrade := true
    
        if exitCon[1]
            exit := close[1]
            inTrade := false
            distance := exit - entry*i
            array.push(distances, distance)

    
        cumDistance = array.sum(distances)
        array.push(cumDistances, cumDistance)
        //array.clear(distances) <---- 将此放在这里,我会得到 NaN,NaN,NaN,NaN,Nan 作为结果
    
    plot(array.size(cumDistances), "索引号", display=display.data_window)
    
    table1 = table.new(position.top_right, 10, 10)
    table.cell(table1, 0, 0, str.tostring(array.get(cumDistances, 0)) + ", "
                             + str.tostring(array.get(cumDistances, 1)) + ", "
                             + str.tostring(array.get(cumDistances, 2)) + ", "
                             + str.tostring(array.get(cumDistances, 3)) + ", "
                             + str.tostring(array.get(cumDistances, 4)))
英文:

I'm trying to push cumDistance into cumDistances array for every iteration of the for loop but I am having trouble.

var inTrade = false
var entry = 0.
var exit = 0.
distance = 0.

entryCon = ta.crossover(ta.rsi(close, 10), 20) and not inTrade
exitCon = ta.crossunder(ta.rsi(close, 10), 80) and inTrade

cumDistances = array.new_float()
for i=1 to 5
    if entryCon[1]
        entry := close[1]
        inTrade := true

    if exitCon[1]
        exit := close[1]
        inTrade := false
        distance := exit - entry*i

    cumDistance = ta.cum(distance)
    array.push(cumDistances, cumDistance)

The values inside the cumDistances array that I get are all the same:

  1. -43.41588
  2. -43.41588
  3. -43.41588
  4. -43.41588
  5. -43.41588

.

if entryCon[1]
    entry := close[1]
    inTrade := true

if exitCon[1]
    exit := close[1]
    inTrade := false
    distance := exit - entry*i //changed i to 1,2,3,4,5

cumDistance = ta.cum(distance)

If I run the code without for loops(like the code just above), these are the values of cumDistance I get for their respective iteration.

  1. 0.02016
  2. -10.83885
  3. -21.69786
  4. -32.55687
  5. -43.41588

It seems that the array cumDistances only saves the value of cumDistance for the last iteration of the for loop to all index of the array.

How do I fix this?

EDIT:
Response to elod008:

cumDistances = array.new_float()
for i=1 to 5
    var distances = array.new_float()
    if entryCon[1]
        entry := close[1]
        inTrade := true

    if exitCon[1]
        exit := close[1]
        inTrade := false
        distance := exit - entry*i
        array.push(distances, distance)


    cumDistance = array.sum(distances)
    array.push(cumDistances, cumDistance)
    //array.clear(distances) <---- placing this here, I will get NaN,NaN,NaN,NaN,Nan as result

plot(array.size(cumDistances), "no. of index", display=display.data_window)

table1 = table.new(position.top_right, 10, 10)
table.cell(table1, 0, 0, str.tostring(array.get(cumDistances, 0)) + ", "
                         + str.tostring(array.get(cumDistances, 1)) + ", "
                         + str.tostring(array.get(cumDistances, 2)) + ", "
                         + str.tostring(array.get(cumDistances, 3)) + ", "
                         + str.tostring(array.get(cumDistances, 4)))

答案1

得分: 1

我运行了你的代码。计算中似乎没有错误。你遇到的问题是 ta.cum() 在一个浮点数系列上工作,对你来说它按照预期计算了绝对总和。<br><br>然而,如果在一些迭代中条件不满足将新值添加到数组中,ta.cum() 将接收 distance 默认的 0.0,这不符合你的期望。于是你的数组会受到内置函数改变的累积距离系列的影响。<br><br>
在这种情况下,我不建议使用 ta.cum()。我更愿意收集并在 var 变量/对象/矩阵中根据你的需要汇总相关结果。

英文:

I ran your code. There seems to be no error in the calculation. What you're experiencing is that ta.cum() works on a series float and sums for you the absolute total as it is supposed to.<br><br>However if your conditional that adds new values to the array is false in some iterations, ta.cum() receives n times the default 0.0 of distance, what does not meet your expectations. Your array gets pushed the series cumDistance altered by the built-in function then.<br><br>
I would not recommend using ta.cum() in this case. I'd rather collect and sum up the relevant results in a var variable/object/matrix etc. depending on your needs.

答案2

得分: 0

你将entryexit都声明为close[1],这意味着你只是进行相同的计算五次,并将其推送到数组中(也是五次)。我假设你想循环遍历过去的最后5个柱子;在这种情况下,你需要将close[1]更改为close[i],这样在每次循环迭代中,它会请求过去更远的close值。

英文:

You declare both entry and exit as close[1], which means that you just do the same calculation five times and push it into array (also five times). I assume you want to cycle over the last 5 bars; in this case, you need to change close[1] to close[i] so that on each loop iteration, it requests the close value farther into the past.

huangapple
  • 本文由 发表于 2023年6月29日 09:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定