简体   繁体   English

Pine Script 上的循环错误

[英]Loop mistake on Pine Script

I am trying a calculate golden cross with for loop but the code does not work (actually even I really dunno if it is written correctly).我正在尝试使用 for 循环计算黄金交叉,但代码不起作用(实际上,即使我真的不知道它是否正确编写)。 There is 2 gold cross but it says 0. I beg you to help me I am new at coding and I gonna lose my mind.有 2 个金十字,但上面写着 0。我求你帮助我,我是编码新手,我会失去理智。

indicator("Cross count", overlay = true)
v1 = input.int(8, "Short WMA", minval=1)
v2 = input.int(21, "Long WMA", minval=1)
v3 = input.int(500, "Look Back Input", minval=10, step=10)

cp = 0
var label lbl = label.new(na, na, " ", style = label.style_label_left)
wmaS= ta.wma(close, v1)
wmaL = ta.wma(close, v2)

for i = 0 to v3
 if ta.crossover(wmaS,wmaL)
  cp := cp + 1
cp

plot(wmaS, color=color.purple)
plot(wmaL, color=color.orange)
label.set_xy(lbl, bar_index, high)
label.set_text(lbl, str.tostring(cp, "Golden Cross = "))`

This part does not make sense:这部分没有意义:

for i = 0 to v3
    if ta.crossover(wmaS,wmaL)
        cp := cp + 1
cp

You create a loop, but inside the loop, you check the same condition.您创建了一个循环,但在循环内部,您检查相同的条件。 So by default, you just check whether the current bar has a crossover 500 times in a row.因此,默认情况下,您只需检查当前柱是否连续 500 次交叉。 If you want to iterate, you should use the [i] operator to check the status of the crossover in the past:如果你想迭代,你应该使用[i]运算符来检查过去的交叉状态:

crossoverCond = ta.crossover(wmaS,wmaL)
for i = 0 to v3
    if crossoverCond[i]
        cp := cp + 1
    cp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM