Search code examples
pine-scripttradingview-apipine-script-v5

Need help converting Tradingview pine script from version 2 to version 5


I am new to TradingView Pine scripting. I am seeking for help with a pine script which is on version 2 right now but I am trying convert it into Version 5 and is having so many compilation errors.

Below is the complete script I am trying to convert from V2 to V5 and error I am facing. i'm getting these errors while converting Conversion failed, reason: Source pine is incorrect. line 12: Variable sum was declared with integer type. Cannot assign it expression of type series '''

//@version=2
study("Trend Signal", overlay=true)
RISK= input(title="RISK", type=integer, defval=14)
value10=3+RISK*2
value11=value10
x1=67+RISK
x2=33-RISK

range(length) =>
    sum = 0
    for i = 1 to length-1
        sum := sum + abs(high[i]-low[i])
    sum / length

MRO1(length, range) =>
    true_count = 0
    counter = 0
    for i = 1 to length-1
        x = if abs(open[i]-close[i+1]) >= range*2.0
            true_count := true_count + 1
            counter = i
            break
    mro = if true_count >= 1
        counter
    else
        -1  
mro
MRO2(length, range) =>
    true_count = 0
    counter = 0
    for i = 1 to length-1
        x = if abs(close[i+3]-close[i]) >= range*4.6
            true_count := true_count + 1
            counter = i
            break
    mro = if true_count >= 1
        counter
    else
        -1  
    mro

wpr(length) =>
    upper = highest(length)
    lower = lowest(length)
    out = 100 * (close - upper) / (upper - lower)
    out
    
rng = range(10)
mro1=MRO1(10,rng)
mro2=MRO2(7,rng)

rez = if mro1>-1
    3
else
    value10
    
value11:=rez
rez1 = if mro2>-1
    4
else
    value10
value11:=rez1

value2 = 100-abs(wpr(value10))

Table_value2=value2

notset=false
ii1 = 1
for i1 = 1 to 300
    if ((Table_value2[i1] < x2 or Table_value2[i1] > x1) and notset==false)
        notset:=true
        ii1 := i1

z=Table_value2[ii1]
up = if value2 < x2
    if Table_value2[ii1] > x1
        ii1
    else
        0
else
    0


plotshape(up, style=shape.labeldown, location=location.abovebar, size=size.normal, color=aqua, text="Sell")

dn = if value2 > x1
    if Table_value2[ii1] < x2
        ii1
    else
        0
else
    0


plotshape(dn, style=shape.labelup, location=location.belowbar, size=size.normal, color=aqua, text="Buy")

'''


Solution

  • Convert it to v3 first. Just change //@version=2 to //@version=3 and it should compile.

    After that use the auto converter that is provided by Tradingview to upgrade it to v4 and then same thing for v5.

    Before converting to v4, make sure to make the following changes:

    sum=0 to sum=0.0:

    range(length) =>
        sum = 0.0
        for i = 1 to length - 1 by 1
            sum := sum + abs(high[i] - low[i])
            sum
        sum / length
    

    Do the following in both MRO functions. x should be removed because nothing is assigned to it.

    if abs(open[i] - close[i + 1]) >= range * 2.0
        true_count := true_count + 1
        counter = i
        break