'How to convert this self-referenced variable in pine v2 to pine v3?

//@version=2
study("Support and Resistance", shorttitle="SR", overlay=true)

//////////////////////////1H
tf1 = input(title="Resolution 7", type=resolution, defval = "60")

vamp = input(title="VolumeMA", type=integer, defval=6)
vam = sma(volume, vamp)

//////////////////////////
up = high[3]>high[4] and high[4]>high[5] and high[2]<high[3] and high[1]<high[2] and volume[3]>vam[3]
down = low[3]<low[4] and low[4]<low[5] and low[2]>low[3] and low[1]>low[2] and volume[3]>vam[3]

fractalup =  up ? high[3] : fractalup[1]
fractaldown = down ? low[3] : fractaldown[1]

/////////////////////////////////////1
fuptf1 = security(tickerid,tf1 == "current" ? period : tf1, fractalup)
fdowntf1 = security(tickerid,tf1 == "current" ? period : tf1, fractaldown)

/////////////////////////////////////1
plot(fuptf1, "FractalUp 1", color=red, linewidth=1, style=circles, transp=0, offset =-3, join=false)
plot(fdowntf1, "FractalDown 1", color=#d6fbbb, linewidth=1, style=circles, transp=0, offset=-3, join=false)

It's a quiet simple script but I am not able to figure out how to convert it to version 3. I have read the migration guide and still don't understand how to deal with the fractalup variable.

When I tried changing it the way it was suggested in the guide I will get the error message:

"line 25: Variable fractalup was declared with series[integer] type. Cannot assign it expression of type series"



Solution 1:[1]

To convert this script to v3, you need to not just declare the variables as na before assigning them values, but also to wrap them in a function (otherwise the compiler will throw an error related to using a mutable variable with the security() function.

The working v3 code equivalent to your current v2 code looks like this:

//@version=3
study("Support and Resistance", shorttitle="SR", overlay=true)

//////////////////////////1H 
tf1 = input(title="Resolution 7", type=resolution, defval = "60")

vamp = input(title="VolumeMA", type=integer, defval=6) 
vam = sma(volume, vamp)

////////////////////////// 
up = high[3]>high[4] and high[4]>high[5] and high[2]<high[3] and high[1]<high[2] and volume[3]>vam[3] 
down = low[3]<low[4] and low[4]<low[5] and low[2]>low[3] and low[1]>low[2] and volume[3]>vam[3]

fractalup() =>
    fractalup = na
    fractalup := up ? high[3] : fractalup[1]

fractaldown() =>
    fractaldown = na
    fractaldown := down ? low[3] : fractaldown[1]

/////////////////////////////////////1 
fuptf1 = security(tickerid,tf1 == "current" ? period : tf1, fractalup(), lookahead=barmerge.lookahead_on) 
fdowntf1 = security(tickerid,tf1 == "current" ? period : tf1, fractaldown(), lookahead=barmerge.lookahead_on)

/////////////////////////////////////1 
plot(fuptf1, "FractalUp 1", color=red, linewidth=1, style=circles, transp=0, offset =-3, join=false) 
plot(fdowntf1, "FractalDown 1", color=#d6fbbb, linewidth=1, style=circles, transp=0, offset=-3, join=false)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 beeholder