Page 1 of 1

Undeclared identifiers

Posted: Sun Sep 26, 2021 12:03 pm
by grdnryn
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])

Re: Undeclared identifiers

Posted: Thu Sep 30, 2021 12:39 am
by Matthew
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])

Re: Undeclared identifiers

Posted: Fri Oct 08, 2021 2:49 pm
by grdnryn
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.