Page 1 of 1

PineScript v5 - Detect Label/Shape etc on previous candles

Posted: Fri Feb 10, 2023 3:34 am
by DonBastardo
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

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

Posted: Mon Feb 13, 2023 4:46 am
by DonBastardo
Is there no one who can help?

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

Posted: Tue Feb 21, 2023 2:31 am
by Steve Burman
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

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

Posted: Wed Feb 22, 2023 11:58 am
by DonBastardo
Hi Steve,

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

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

Posted: Thu Feb 23, 2023 2:12 am
by Steve Burman
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')

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

Posted: Thu Feb 23, 2023 4:18 pm
by DonBastardo
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

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

Posted: Fri Feb 24, 2023 5:21 am
by Steve Burman
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