Hadi Sharghi
Hadi Sharghi

Reputation: 1005

Fix undeclared identifier self referencing value in Pine Script 5

There is an old script which is written in version 3 of Pine Script and it's working fine. I'm going to change it to version 5 but I'm getting the `Undeclared identifier SmoothedTrueRange' error for the last 3 lines. How to safely convert this code to version 5?

TrueRange = math.max(math.max(high-low, math.abs(high-nz(close[1]))), math.abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? math.max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? math.max(nz(low[1])-low, 0): 0

SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
SmoothedDirectionalMovementPlus = nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus = nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

Upvotes: 0

Views: 635

Answers (1)

Rohit Agarwal
Rohit Agarwal

Reputation: 1368

You will have to declare the three variables before using them and then while using you should use := operator. Example below

//@version=5
indicator(title="SATR",overlay=false)
len=input.int(9)
TrueRange = math.max(math.max(high-low, math.abs(high-nz(close[1]))), math.abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? math.max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? math.max(nz(low[1])-low, 0): 0

var SmoothedTrueRange=TrueRange
var SmoothedDirectionalMovementPlus=DirectionalMovementPlus
var SmoothedDirectionalMovementMinus=DirectionalMovementMinus

SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
SmoothedDirectionalMovementPlus:= nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus:= nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus

Upvotes: 1

Related Questions