简体   繁体   中英

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). 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.

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. If you want to iterate, you should use the [i] operator to check the status of the crossover in the past:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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