1

I'm new to programming in Pine Script language and I'm trying to combine the RSI indicator with the Nadaraya-Watson Envelope by LuxAlgo. The strategy basically is that when the price crosses the upper channel and the RSI indicates overbought, it creates a sell alert, and when the price crosses the lower channel and the RSI indicates oversold, it creates a buy alert. Specifically, where I'm having trouble is when I try to modify the conditional in the local scope to add the RSI, the variable "rsiBuy" and "rsiSell".


//@version=5

indicator("NWE by luxAlgo+RSI",overlay=true,max_bars_back=1000,max_lines_count=500,max_labels_count=500)
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')
disclaimer = input(false, 'Hide Disclaimer')
//----
n = bar_index
var k = 2
var upper = array.new_line(0) 
var lower = array.new_line(0) 

//RSI-->
per_rsi = input.int(7, "Periodos RSI")
rsi_sc = input.int(70, "Sobrecompra")
rsi_sv = input.int(30, "Sobreventa")
rsi = ta.rsi(close, per_rsi)
plot(rsi, color = color.white)

h1 = hline(rsi_sc, color = color.gray)
h2 = hline(rsi_sv, color = color.gray)

fill(h1,h2, color = color.new(color.purple, 90))

rsiBuy = rsi < rsi_sv
rsiSell = rsi > rsi_sc
//<--RSI

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 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

alertcondition(ta.crossover(src,cross_up),'Down','Down')
alertcondition(ta.crossunder(src,cross_dn),'Up','Up')

//----
var tb = table.new(position.top_right, 1, 1
  , bgcolor = #35202b)

if barstate.isfirst and not disclaimer
    table.cell(tb, 0, 0, 'Nadaraya-Watson Envelope [LUX] Repaints'
      , text_size = size.small
      , text_color = #cc2f3c)

I tried to modify the alertcondition at the end, but through trial and error, it seems that the conditional in the local scope is what actually generates the arrows, and that's where I'm stuck because I can't test it without being able to use the variables rsiBuy and rsiSell that were declared earlier.

1 Answers1

0

You should first declare global variable at the top of your indicator :

SignalUp = false
SignalDown = false

Then update those variable in the local scope

if src[i] > y1 + mae and src[i+1] < y1 + mae
    SignalUp := true
    label.new(n-i,src[i],'▼',color=#00000000,style=label.style_label_down,textcolor=dn_col,textalign=text.align_center)
if src[i] < y1 - mae and src[i+1] > y1 - mae
    SignalDown := true
    label.new(n-i,src[i],'▲',color=#00000000,style=label.style_label_up,textcolor=up_col,textalign=text.align_center)

Then use those variables for your alerts.

G.Lebret
  • 2,826
  • 2
  • 16
  • 27
  • Hi!! thank you very much for the quick answer but I dont follow what you are trying to explain. What I have in mind is to use this boolean variables in the condition: `rsiBuy = rsi < rsi_sv; rsiSell = rsi > rsi_sc` like this: `if src[i] > y1 + mae and src[i+1] < y1 + mae and rsiSell .................. if src[i] < y1 - mae and src[i+1] > y1 - mae and rsiBuy ...........` – Leandro Martin Bado Mar 23 '23 at 21:17