DonBastardo
Pine Script Rookie
Pine Script Rookie
Posts: 8
Joined: February 10th, 2023

PineScript v5 - Detect Label/Shape etc on previous candles

Gents,

I am new to pine script 5 and encountered my first challange.

I want to build a 1min strategy around this newly found indicator "Nadaraya-Watson Envelope" by LuxAlgo:

Code: Select all

// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo
//@version=5

indicator("Nadaraya-Watson Envelope [LuxAlgo]",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) 

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 need help with 2 things.

1. How can I check/verify that the prev candle or within the last x candles 1 or more candles have a signal (red or green triangle) printed? I want to use this information as one of the conditions for entries.

2. How can I check if a candle hast closed above or below the green or red line?

I appreciate any help.

Thanks, DonBastardo
Last edited by DonBastardo on Wed Feb 22, 2023 11:57 am, edited 1 time in total.

DonBastardo
Pine Script Rookie
Pine Script Rookie
Posts: 8
Joined: February 10th, 2023

Re: PineScript v5 - Detect Label/Shape etc on previous candles

Is there no one who can help?

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: PineScript v5 - Detect Label/Shape etc on previous candles

Hi Don,

As you can see in the above script, there is no indenting for if and for loops making it very hard to read as I can't tell if there are embedded if and for loops or whether they are all independant. Can you resend it as the script should be as I can't get it to compile

DonBastardo
Pine Script Rookie
Pine Script Rookie
Posts: 8
Joined: February 10th, 2023

Re: PineScript v5 - Detect Label/Shape etc on previous candles

Hi Steve,

sorry my bad - now the code is shown correctly. Thanks for taking the time to reply.
KR

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: PineScript v5 - Detect Label/Shape etc on previous candles

Thanks,

This script is quite complicated and doesn't have any comments on what it is doing which makes it difficult to read for novices and for those with more experience.

When I run this I don't see any red or green triangles. What market are you running this on and what time frame?

It looks like he already has set alerts if the price crosses over or under the red or green line as follows:

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

DonBastardo
Pine Script Rookie
Pine Script Rookie
Posts: 8
Joined: February 10th, 2023

Re: PineScript v5 - Detect Label/Shape etc on previous candles

OK, thats strange it works on different markets - EURUSD, Gold, US30 etc and different TF´s - the only thing is, that its limited to 500 bars
I wanted to use it on the 1 min TF

https://imgbox.com/VzgqyOUK

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: PineScript v5 - Detect Label/Shape etc on previous candles

Yes, TV labels and other objects have a 500 limit which is appalling especially on Pro+ and higher accounts

This is what I get when I run it on the e-mini or USDEUR https://imgbox.io/ib/0XPQ44nD61

Return to “Pine Script Q&A”