Cannot call 'operator <' with argument 'expr0'='p2'. An argument of 'plot' type was used but a 'simple float' is expected

Viewed 56

I want to display a label when the Bollinger band goes out of the envelope(up or dn), but I don't think it's possible because one is a plot type(BB) and another one is a line type(ND). The error message is as follows.

"line 79: Cannot call 'operator <' with argument 'expr0'='p2'. An argument of 'plot' type was used but a 'simple float' is expected line 79: Cannot call 'operator <' with argument 'expr1'='dn'. An argument of 'series line' type was used but a 'simple float' is expected"

indicator("ND+BB",overlay=true,max_bars_back=1000,max_lines_count=500,max_labels_count=500)

//BB
Bsrc = input(close, title="Source")
Bbasis = ta.sma(close, 20)
dev = 2 * ta.stdev(close, 20)
Bupper = Bbasis + dev
Blower = Bbasis - dev
//plot(Bbasis, color=color.aqua)
p1 = plot(Bupper, color=#e91e63)
p2 = plot(Blower, color=#e91e63)


//ND
length = input.float(500,'Window Size',maxval=500,minval=0)
h      = input.float(8.,'Bandwidth')
mult   = input.float(3.) 
src    = input.source(close,'Source')

up_col = input.color(#39ff14,'Colors',inline='col')
dn_col = input.color(#ff1100,'',inline='col')
//----
n = bar_index
var k = 2
var upper = array.new_line(0) 
var lower = array.new_line(0) 

lset(l,x1,y1,x2,y2,col)=>
    line.set_xy1(l,x1,y1)
    line.set_xy2(l,x2,y2)
    line.set_color(l,col)
    line.set_width(l,2)

if barstate.isfirst
    for i = 0 to length/k-1
        array.push(upper,line.new(na,na,na,na))
        array.push(lower,line.new(na,na,na,na))
//----
line up = na
line dn = na
//----
cross_up = 0.
cross_dn = 0.
if barstate.islast
    y = array.new_float(0)
    
    sum_e = 0.
    for i = 0 to length-1
        sum = 0.
        sumw = 0.
        
        for j = 0 to length-1
            w = math.exp(-(math.pow(i-j,2)/(h*h*2)))
            sum += src[j]*w
            sumw += w
        
        y2 = sum/sumw
        sum_e += math.abs(src[i] - y2)
        array.push(y,y2)

    mae = sum_e/length*mult
    
    for i = 1 to length-1
        y2 = array.get(y,i)
        y1 = array.get(y,i-1)
        
        up := array.get(upper,i/k)
        dn := array.get(lower,i/k)
        
        lset(up,n-i+1,y1 + mae,n-i,y2 + mae,up_col)
        lset(dn,n-i+1,y1 - mae,n-i,y2 - mae,dn_col)
        
        if src[i] > y1 + mae and src[i+1] < y1 + mae
            label.new(n-i,src[i],'▼',color=#00000000,style=label.style_label_down,textcolor=dn_col,textalign=text.align_center)
        if  p2<dn//src[i] < y1 - mae and src[i+1] > y1 - mae 
            label.new(n-i,src[i],'▲',color=#00000000,style=label.style_label_up,textcolor=up_col,textalign=text.align_center)
    
    cross_up := array.get(y,0) + mae
    cross_dn := array.get(y,0) - mae
1 Answers
  1. The plot function doesn't return a value but it returns plot reference. You need use variable Bupper or Blower instead of p1 or p2
  2. To get price value of the line for some bar_index you need to use line.get_price() function. The fixed code is:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov

//@version=5
indicator("ND+BB",overlay=true,max_lines_count=500,max_labels_count=500)

//BB
Bsrc = input(close, title="Source")
Bbasis = ta.sma(close, 20)
dev = 2 * ta.stdev(close, 20)
Bupper = Bbasis + dev
Blower = Bbasis - dev
//plot(Bbasis, color=color.aqua)
p1 = plot(Bupper, color=#e91e63)
p2 = plot(Blower, color=#e91e63)


//ND
length = input.float(500,'Window Size',maxval=500,minval=0)
h      = input.float(8.,'Bandwidth')
mult   = input.float(3.) 
src    = input.source(close,'Source')

up_col = input.color(#39ff14,'Colors',inline='col')
dn_col = input.color(#ff1100,'',inline='col')
//----
n = bar_index
var k = 2
var upper = array.new_line(0) 
var lower = array.new_line(0) 

lset(l,x1,y1,x2,y2,col)=>
    line.set_xy1(l,x1,y1)
    line.set_xy2(l,x2,y2)
    line.set_color(l,col)
    line.set_width(l,2)

if barstate.isfirst
    for i = 0 to length/k-1
        array.push(upper,line.new(na,na,na,na))
        array.push(lower,line.new(na,na,na,na))
//----
line up = na
line dn = na
//----
cross_up = 0.
cross_dn = 0.
if barstate.islast
    y = array.new_float(0)
    
    sum_e = 0.
    for i = 0 to length-1
        sum = 0.
        sumw = 0.
        
        for j = 0 to length-1
            w = math.exp(-(math.pow(i-j,2)/(h*h*2)))
            sum += src[j]*w
            sumw += w
        
        y2 = sum/sumw
        sum_e += math.abs(src[i] - y2)
        array.push(y,y2)

    mae = sum_e/length*mult
    
    for i = 1 to length-1
        y2 = array.get(y,i)
        y1 = array.get(y,i-1)
        
        up := array.get(upper,i/k)
        dn := array.get(lower,i/k)
        
        lset(up,n-i+1,y1 + mae,n-i,y2 + mae,up_col)
        lset(dn,n-i+1,y1 - mae,n-i,y2 - mae,dn_col)
        
        if src[i] > y1 + mae and src[i+1] < y1 + mae
            label.new(n-i,src[i],'▼',color=#00000000,style=label.style_label_down,textcolor=dn_col,textalign=text.align_center)
        if  Blower<line.get_price(dn, bar_index)//src[i] < y1 - mae and src[i+1] > y1 - mae 
            label.new(n-i,src[i],'▲',color=#00000000,style=label.style_label_up,textcolor=up_col,textalign=text.align_center)
    
    cross_up := array.get(y,0) + mae
    cross_dn := array.get(y,0) - mae
Related