2023-01-21

Unexplained Pinescript error shows when trying to parametrize a variable

This is the working version of the script:

//@version=5
indicator("compare MA", overlay= true)

var finallength = 0
Length1MA = input.int(defval = 10, title= "1 MA length")
Length2MA = input.int(defval = 20, title= "2 MA length")
sma1 = ta.sma(close, Length1MA)
sma2 = ta.sma(close, Length2MA)
var count1 = 0.0
var count2 = 0.0

for i = 1 to 10
    if not na(sma1[i]) and not na(sma2[i])
        count1 := count1 + math.abs(close[i] - sma1[i])
        count2 := count2 + math.abs(close[i] - sma2[i])

if count1 > count2
    finallength := Length1MA
else
    finallength := Length2MA

plot(ta.sma(close, finallength), color= color.red, linewidth=2)

This is a different version that tries to parametrize the amount of Moving Averages, however there is an error i've been stuck to for a long time:

//@version=5
indicator("compare MA", overlay= true)

var finallength = 0
LengthsMA = input(defval=[10,20,30,40], title="MA lengths", type=input.integer)
counts = 0.0

for i = 1 to bar_index
    for j = 0 to len(LengthsMA)-1
        sma = ta.sma(close, LengthsMA[j])
        if not na(sma[i])
            counts[j] := counts[j] + math.abs(close[i] - sma[i])

finallength := LengthsMA[highest(counts)]
plot(ta.sma(close, finallength), color= color.red, linewidth=2)

you can run it to see the error first hand at line 12: counts[j] = counts[j] + math.abs(close[i] - sma[i]) -> gives error -> Syntax error at input '=' (Error at 12:23) changing it to := will not fix it! the goal is to use many Moving Averages and iterate through them all to find which is furthest from the price for the most time.

The goal is to use as many Moving Averages as needed without writing lines individually and cluttering the code with 20 else if statements, and iterate through them all to find which is furthest from the price. the problem is this unexplainable error: Syntax error at input '=' (Error at 12:23)



No comments:

Post a Comment