grdnryn
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: September 26th, 2021
Contact: TradingView Profile

Undeclared identifiers

I am trying to write a simple indicator in pine-script but I can't get rid of the undeclared identifier error when I add the line.delete() function. I couldn't resolve this with all the previous suggestions for this error message. To be honest, mine is probably a simpler problem!


line 20: Undeclared identifier 'WkIBL';
line 21: Undeclared identifier 'WkIBH';
line 22: Undeclared identifier 'WkIBHx';
line 23: Undeclared identifier 'WkIBLx';
line 24: Undeclared identifier 'WkIBM'

I have been over this article, but none of the solutions seem to apply.
https://kodify.net/tradingview/errors/u ... dentifier/

Below is the code.

Code: Select all

//@version=4
study(title=" Weekly Initial Balance", shorttitle="WkIB", overlay=true)
    
TuesC = (dayofweek == dayofweek.tuesday)
Mon = (dayofweek == dayofweek.monday)
TwodayHigh = highest(high, 2)
TwodayLow = lowest(low, 2)
IBHx = TwodayHigh + ((TwodayHigh-TwodayLow)/2)
IBLx = TwodayLow - ((TwodayHigh-TwodayLow)/2)
Mid = (TwodayHigh + TwodayLow) / 2
    
if(TuesC)
    WkIBM = line.new(x1=bar_index[1], y1=Mid, x2=bar_index, y2=Mid, extend=extend.right, color=#F2E8DC, style=line.style_dotted)
	WkIBL = line.new(x1=bar_index[1], y1=TwodayLow, x2=bar_index, y2=TwodayLow, extend=extend.right, color=#F2E8DC)
	WkIBH = line.new(x1=bar_index[1], y1=TwodayHigh, x2=bar_index, y2=TwodayHigh, extend=extend.right, color=#F2E8DC)
	WkIBHx = line.new(x1=bar_index[1], y1=IBHx, x2=bar_index, y2=IBHx, extend=extend.right, color=#F2E8DC, style=line.style_dashed)
	WkIBLx = line.new(x1=bar_index[1], y1=IBLx, x2=bar_index, y2=IBLx, extend=extend.right, color=#F2E8DC, style=line.style_dashed)
if(Mon)
    line.delete(WkIBL[1])
    line.delete(WkIBH[1])
    line.delete(WkIBHx[1])
    line.delete(WkIBLx[1])
    line.delete(WkIBM[1])

User avatar
Matthew
Site Admin
Site Admin
Posts: 92
Joined: July 1st, 2020
Location: Australia
Contact: Website Facebook Twitter TradingView Profile

Re: Undeclared identifiers

Hi grdnryn!

The problem here is due to the if statement scope. Under "if(TuesC)" you create the label variables and they only exist within that scope. What you need to do is remove them from the scope first, like this:

Code: Select all

//@version=4
study(title=" Weekly Initial Balance", shorttitle="WkIB", overlay=true)
    
TuesC = (dayofweek == dayofweek.tuesday)
Mon = (dayofweek == dayofweek.monday)
TwodayHigh = highest(high, 2)
TwodayLow = lowest(low, 2)
IBHx = TwodayHigh + ((TwodayHigh-TwodayLow)/2)
IBLx = TwodayLow - ((TwodayHigh-TwodayLow)/2)
Mid = (TwodayHigh + TwodayLow) / 2

// Declare blank line variables
WkIBM = line.new(x1=na, y1=na, x2=na, y2=na)
WkIBL = line.new(x1=na, y1=na, x2=na, y2=na)
WkIBH = line.new(x1=na, y1=na, x2=na, y2=na)
WkIBHx = line.new(x1=na, y1=na, x2=na, y2=na)
WkIBLx = line.new(x1=na, y1=na, x2=na, y2=na)

if(TuesC and not drawn)
    WkIBM := line.new(x1=bar_index[1], y1=Mid, x2=bar_index, y2=Mid, extend=extend.right, color=#F2E8DC, style=line.style_dotted)
	WkIBL := line.new(x1=bar_index[1], y1=TwodayLow, x2=bar_index, y2=TwodayLow, extend=extend.right, color=#F2E8DC)
	WkIBH := line.new(x1=bar_index[1], y1=TwodayHigh, x2=bar_index, y2=TwodayHigh, extend=extend.right, color=#F2E8DC)
	WkIBHx := line.new(x1=bar_index[1], y1=IBHx, x2=bar_index, y2=IBHx, extend=extend.right, color=#F2E8DC, style=line.style_dashed)
	WkIBLx := line.new(x1=bar_index[1], y1=IBLx, x2=bar_index, y2=IBLx, extend=extend.right, color=#F2E8DC, style=line.style_dashed)
if(Mon)
    line.delete(WkIBL[1])
    line.delete(WkIBH[1])
    line.delete(WkIBHx[1])
    line.delete(WkIBLx[1])
    line.delete(WkIBM[1])

grdnryn
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: September 26th, 2021
Contact: TradingView Profile

Re: Undeclared identifiers

Thanks very much Matthew.

you added 'and not drawn' to the if(TuesC) function.
This is resulting in 'drawn' now being classified as an undeclared identifier.

Return to “Pine Script Q&A”