Mel
Mel

Reputation: 19

Pinescript "undeclared Identifier"

I am getting an Undeclared identified 'd' error. This was converted from an indicator that had alerts. But when I had them in TV nothing would happen. I can't seem to figure this out. Can anyone assist me with this error?

strategy("My strategy",overlay=true,max_lines_count=500,max_bars_back=500)
h = input(8.,'Bandwidth')
src = input(close,'Source')
//----
n = bar_index
var ln = array.new_line(0) 
if barstate.isfirst
    for i = 0 to 499
        array.push(ln,line.new(na,na,na,na))
//----
float y2 = na
float y1 = na
float y1_d = na
//----
line l = na
label lb = na
if barstate.islast
    for i = 0 to math.min(499,n-1)
        sum = 0.
        sumw = 0.
        for j = 0 to math.min(499,n-1)
            w = math.exp(-(math.pow(i-j,2)/(h*h*2)))
            sum += src[j]*w
            sumw += w
        y2 := sum/sumw
        d = y2 - y1
        l := array.get(ln,i)
        line.set_xy1(l,n-i+1,y1)
        line.set_xy2(l,n-i,y2)
        line.set_color(l,y2 > y1 ? #ff1100 : #39ff14)
        line.set_width(l,2)
        
        if d > 0 and y1_d < 0
            label.new(n-i+1,src[i],'▲',color=#00000000,style=label.style_label_up,textcolor=#39ff14,textalign=text.align_center) 
            
            
        if d < 0 and y1_d > 0
            label.new(n-i+1,src[i],'▼',color=#00000000,style=label.style_label_down,textcolor=#ff1100,textalign=text.align_center) 
            
        y1 := y2
        y1_d := d
        

Buy = d > 0 and y1_d < 0
Sell = d < 0 and y1_d > 0

longCondition = Buy
if (longCondition)
    strategy.entry("Id", strategy.long)

shortCondition = Sell
if (shortCondition)
    strategy.close("Id")

Upvotes: 0

Views: 2419

Answers (2)

rTJ
rTJ

Reputation: 19

You are telling your script to use d, when your script does not know what d means, because you are using d in two different scopes.

Declare (and initialize) it, just like you did with y2, y1, y1_d, before you use it.

p.ex.

int d = 0

That should fix it.

Upvotes: 0

vitruvius
vitruvius

Reputation: 21342

d is defined in a local scope, inside the for loop. You are trying to access it on the global scope which will not work because it is not visible on the global scope.

Upvotes: 1

Related Questions