PineScript v2 to v4 conversion, Cant Convert with Self Referencing Variable

Viewed 106

I'm trying to convert my Pine script version 2 indicator to version 4 so I publish it, but it uses a self-referencing variable which they got rid of in version 3. To solve it it needs to be wrapped as stated in the reference page I will link at the bottom, but I cant figure out how to properly wrap it. If anyone could help me wrap this formula it would be greatly appreciated. Thank you in advance. https://www.tradingview.com/pine-script-docs/en/v5/migration_guides/To_Pine_version_3.html#

Here is the code. The self-referencing variable is on line 33 (BSsum) and uses line 9 as part of its formula.

// © OasisTrading

//@version=4
study("Rotation Factor: Buy/Sell Pressure", shorttitle="Rotation Factor")

interval = security(syminfo.tickerid, input("1D", title="Reset Interval"), time)

newSession = iff(change(interval), 1, 0)

one = (high > high[1]) and (low > low[1])
two = (high < high[1]) and (low < low[1])
three = (high > high[1]) and (low < low[1])
four = (high < high[1]) and (low > low[1])
five = (high == high[1]) and (low > low[1])
six = (high > high[1]) and (low == low[1])
seven = (high < high[1]) and (low == low[1])
eight = (high == high[1]) and (low < low[1])

//formula
onex = +2 
twox = -2
threex = 0
fourx = 0
fivex = +1
sixx = +1
sevenx = -1 
eightx = -1

formula = (high > high[1]) and (low > low[1]) ? onex : (high < high[1]) and (low < low[1]) ? twox : (high > high[1]) and (low < low[1]) ? threex : (high < high[1]) and (low > low[1]) ? fourx : (high == high[1]) and (low > low[1]) ? fivex : (high > high[1]) and (low == low[1]) ? sixx : (high < high[1]) and (low == low[1]) ? sevenx : (high == high[1]) and (low < low[1]) ? eightx : 0
BuySell = sum(formula, 1)

BSsum : iff(newSession, BuySell, BSsum[1]+BuySell)

Kolor1 = (BSsum > 0)==true
Kolor2 = (BSsum < 0)==true

plot(BSsum, style=plot.style_columns, color=Kolor1?color.green:Kolor2?color.red:na, linewidth=3, transp=0, title="Buy/Sell Pressure")```
1 Answers

It doesn't have to be wrapped, wrapping variables inside functions is necessary if you want to pass these mutable variables inside the security(). Your variable is based on data from security, but it isn't passed to it.

To make the script work in v4, you need to first declare the variable, and then assign it a new value via the := operator:

// © OasisTrading

//@version=4
study("Rotation Factor: Buy/Sell Pressure", shorttitle="Rotation Factor")

interval = security(syminfo.tickerid, input("1D", title="Reset Interval"), time)

newSession = iff(change(interval), 1, 0)

one = (high > high[1]) and (low > low[1])
two = (high < high[1]) and (low < low[1])
three = (high > high[1]) and (low < low[1])
four = (high < high[1]) and (low > low[1])
five = (high == high[1]) and (low > low[1])
six = (high > high[1]) and (low == low[1])
seven = (high < high[1]) and (low == low[1])
eight = (high == high[1]) and (low < low[1])

//formula
onex = +2 
twox = -2
threex = 0
fourx = 0
fivex = +1
sixx = +1
sevenx = -1 
eightx = -1

formula = (high > high[1]) and (low > low[1]) ? onex : (high < high[1]) and (low < low[1]) ? twox : (high > high[1]) and (low < low[1]) ? threex : (high < high[1]) and (low > low[1]) ? fourx : (high == high[1]) and (low > low[1]) ? fivex : (high > high[1]) and (low == low[1]) ? sixx : (high < high[1]) and (low == low[1]) ? sevenx : (high == high[1]) and (low < low[1]) ? eightx : 0
BuySell = sum(formula, 1)

BSsum = 0.0
BSsum := newSession ? BuySell : BSsum[1] + BuySell

Kolor1 = (BSsum > 0)==true
Kolor2 = (BSsum < 0)==true

plot(BSsum, style=plot.style_columns, color=Kolor1?color.green:Kolor2?color.red:na, linewidth=3, transp=0, title="Buy/Sell Pressure")
Related