Nadaraya Watson Envelope

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #239170 quote
    Azkune
    Participant
    New

    Hola,

    He visto que hay un código parecido de esto en el foro en inglés, pero el resultado no es el mismo. ¿Alguien podría traducir esto a ProRealCode para poder usarlo en el ProRealTime?

    Gracias

    Un saludo

    // 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]”, “LuxAlgo – Nadaraya-Watson Envelope”, overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back=500)
    //——————————————————————————
    //Settings
    //—————————————————————————–{
    h = input.float(8.,’Bandwidth’, minval = 0)
    mult = input.float(3., minval = 0)
    src = input(close, ‘Source’)
    repaint = input(true, ‘Repainting Smoothing’, tooltip = ‘Repainting is an effect where the indicators historical output is subject to change over time. Disabling repainting will cause the indicator to output the endpoints of the calculations’)
    //Style
    upCss = input.color(color.teal, ‘Colors’, inline = ‘inline1’, group = ‘Style’)
    dnCss = input.color(color.red, ”, inline = ‘inline1’, group = ‘Style’)
    //—————————————————————————–}
    //Functions
    //—————————————————————————–{
    //Gaussian window
    gauss(x, h) => math.exp(-(math.pow(x, 2)/(h * h * 2)))
    //—————————————————————————–}
    //Append lines
    //—————————————————————————–{
    n = bar_index
    varln=array.new_line(0)
    if barstate.isfirst and repaint
    fori=0to499
    array.push(ln,line.new(na,na,na,na))
    //—————————————————————————–}
    //End point method
    //—————————————————————————–{
    var coefs = array.new_float(0)
    var den = 0.
    if barstate.isfirst and not repaint
    fori=0to499
    w=gauss(i,h)
    coefs.push(w)
    den:=coefs.sum()
    out = 0.
    if not repaint
    fori=0to499
    out+=src[i]*coefs.get(i)
    out /= den
    mae = ta.sma(math.abs(src – out), 499) * mult
    upper = out + mae
    lower = out – mae
    //—————————————————————————–}
    //Compute and display NWE
    //—————————————————————————–{
    float y2 = na
    float y1 = na
    nwe = array.new<float>(0)
    if barstate.islast and repaint
    sae=0.
    //Compute and set NWE point
    fori=0tomath.min(499,n-1)
    sum=0.
    sumw=0.
    //Compute weighted mean
    forj=0tomath.min(499,n-1)
    w=gauss(i-j,h)
    sum+=src[j]*w
    sumw+=w
    y2:=sum/sumw
    sae+=math.abs(src[i]-y2)
    nwe.push(y2)
    sae:=sae/math.min(499,n-1)*mult
    fori=0tomath.min(499,n-1)
    ifi%2
    line.new(n-i+1,y1+sae,n-i,nwe.get(i)+sae,color=upCss)
    line.new(n-i+1,y1-sae,n-i,nwe.get(i)-sae,color=dnCss)
    ifsrc[i]>nwe.get(i)+saeandsrc[i+1]<nwe.get(i)+sae
    label.new(n-i,src[i],’▼’,color=color(na),style=label.style_label_down,textcolor=dnCss,textalign=text.align_center)
    ifsrc[i]<nwe.get(i)-saeandsrc[i+1]>nwe.get(i)-sae
    label.new(n-i,src[i],’▲’,color=color(na),style=label.style_label_up,textcolor=upCss,textalign=text.align_center)
    y1:=nwe.get(i)
    //—————————————————————————–}
    //Dashboard
    //—————————————————————————–{
    var tb = table.new(position.top_right, 1, 1
    ,bgcolor=#1e222d
    ,border_color=#373a46
    ,border_width=1
    ,frame_color=#373a46
    ,frame_width=1)
    if repaint
    tb.cell(0,0,’Repainting Mode Enabled’,text_color=color.white,text_size=size.small)
    //—————————————————————————–}
    //Plot
    //—————————————————————————–}
    plot(repaint ? na : out + mae, ‘Upper’, upCss)
    plot(repaint ? na : out – mae, ‘Lower’, dnCss)
    //Crossing Arrows
    plotshape(ta.crossunder(close, out – mae) ? low : na, “Crossunder”, shape.labelup, location.absolute, color(na), 0 , text = ‘▲’, textcolor = upCss, size = size.tiny)
    plotshape(ta.crossover(close, out + mae) ? high : na, “Crossover”, shape.labeldown, location.absolute, color(na), 0 , text = ‘▼’, textcolor = dnCss, size = size.tiny)
    //—————————————————————————–}
    #239688 quote
    Iván González
    Moderator
    Master
    //--------------------------------------------//
    //PRC Nadaraya-Watson Envelope
    //version = 0
    //28.02.24
    //Iván González @ www.prorealcode.com
    //Sharing ProRealTime knowledge
    //--------------------------------------------//
    //-----------Inputs---------------------------//
    //--------------------------------------------//
    h1=8 //Bandwidth
    mult=3
    length=499 //Window Size
    repaint=1
    src=close
    //--------------------------------------------//
    //----------Repaint ON------------------------//
    //--------------------------------------------//
    if islastbarupdate and repaint then
    sae=0
    for i=0 to min(499,barindex-1) do
    sum=0
    sumw=0
    for j=0 to min(499,barindex-1) do
    x=i-j
    w=exp(-(pow(x,2)/(h1*h1*2)))
    sum=src[j]*w+sum
    sumw=w+sumw
    next
    $y2[i]=sum/sumw
    sae=sae+abs(src[i]-$y2[i])
    next
    
    sae1=sae/min(499,barindex-1)*mult
    
    for i=min(499,barindex-1) downto 0 do
    if i MOD 2 <> 0 then
    drawpoint(barindex[i],$y2[i]+sae1,2)coloured("green")
    drawpoint(barindex[i],$y2[i]-sae1,2)coloured("red")
    endif
    if close[i]>$y2[i]+sae1 and close[i+1]<=$y2[i+1]+sae1 then
    drawarrowdown(barindex[i],high[i])coloured("red")
    elsif close[i]<$y2[i]-sae1 and close[i+1]>=$y2[i+1]-sae1 then
    drawarrowup(barindex[i],low[i])coloured("green")
    endif
    next
    endif
    //--------------------------------------------//
    //----------Repaint OFF-----------------------//
    //--------------------------------------------//
    if repaint=0 then
    once init = 1
    
    If init = 1 then
    den = 0
    For i = 0 to length-1 do
    $coefs[i]=exp(-(i*i)/(h1*h1*2))
    den = den + $coefs[i]
    Next
    init = 0
    Endif
    
    out = 0
    
    For i = 0 to length-1 do
    out = out + src[i]*$coefs[i]
    Next
    
    out = out/den
    mae = exponentialAverage[length](abs(src-out)) * mult
    
    upper = out + mae
    lower = out - mae
    
    if close crosses over upper then
    drawarrowdown(barindex,high)coloured("red")
    elsif close crosses under lower then
    drawarrowup(barindex,low)coloured("green")
    endif
    endif
    //--------------------------------------------//
    //----------Plot Bands------------------------//
    //--------------------------------------------//
    if repaint then
    up=undefined
    dw=undefined
    else
    up=upper
    dw=lower
    endif
    //--------------------------------------------//
    return up as "Upper band"style(line,2)coloured("Green"),dw as "Lower band"style(line,2)coloured("red")
    
    dkatle thanked this post
    #241412 quote
    Armand2020
    Participant
    Senior

    bonjour,

    comment pourrait t on faire pour qu en mode repaint on est des point verts quand on monte et rouge quand on descent?

    #241459 quote
    Iván González
    Moderator
    Master

    En el bucle de las líneas 35 a 45 tendría que introducir un condicional indicando que cuando $y2[i]+sae1 > $y2[i-1]+sae1 entonce dibuje los puntos verdes y en caso contrario rojos.

    #241495 quote
    Armand2020
    Participant
    Senior
    
    
    //Iván González @ www.prorealcode.com
    //Sharing ProRealTime knowledge
    //--------------------------------------------//
    //-----------Inputs---------------------------//
    //--------------------------------------------//
    //h1=8 //Bandwidth
    //mult=3
    length=499 //Window Size
    //repaint=1
    src=close
    //--------------------------------------------//
    //----------Repaint ON------------------------//
    //--------------------------------------------//
    if islastbarupdate and repaint then
    sae=0
    for i=0 to min(499,barindex-1) do
    sum=0
    sumw=0
    for j=0 to min(499,barindex-1) do
    x=i-j
    w=exp(-(pow(x,2)/(h1*h1*2)))
    sum=src[j]*w+sum
    sumw=w+sumw
    next
    $y2[i]=sum/sumw
    sae=sae+abs(src[i]-$y2[i])
    next
     
    sae1=sae/min(499,barindex-1)*mult
     
    
    for i=min(499,barindex-1) downto 0 do
    IF ($y2[i]+sae1)<($y2[i-1]+sae1) THEN
    RA=200
    GA=0
    BA=0
    ENDIF
    if i MOD 2 <> 0 then
    
    
    drawpoint(barindex[i],$y2[i]+sae1,1)coloured("RA,gA,bA")
    drawpoint(barindex[i],$y2[i]-sae1,1)coloured("red")
    endif
    
    if close[i]>$y2[i]+sae1 and close[i+1]<=$y2[i+1]+sae1 then
    drawarrowdown(barindex[i],high[i])coloured("red")
    elsif close[i]<$y2[i]-sae1 and close[i+1]>=$y2[i+1]-sae1 then
    drawarrowup(barindex[i],low[i])coloured("green")
    endif
    
    next
    
    endif
    
    //--------------------------------------------//
    //----------Repaint OFF-----------------------//
    //--------------------------------------------//
    if repaint=0 then
    once init = 1
     
    If init = 1 then
    den = 0
    For i = 0 to length-1 do
    $coefs[i]=exp(-(i*i)/(h1*h1*2))
    den = den + $coefs[i]
    Next
    init = 0
    Endif
     
    out = 0
     
    For i = 0 to length-1 do
    out = out + src[i]*$coefs[i]
    Next
     
    out = out/den
    mae = exponentialAverage[length](abs(src-out)) * mult
     
    upper = out + mae
    lower = out - mae
     
    if close crosses over upper then
    drawarrowdown(barindex,high)coloured(200,200,0)
    elsif close crosses under lower then
    drawarrowup(barindex,low)coloured(0,200,200)
    endif
    endif
    //--------------------------------------------//
    //----------Plot Bands------------------------//
    //--------------------------------------------//
    if repaint then
    up=undefined
    dw=undefined
    else
    up=upper
    dw=lower
    endif
    IF UP>UP[1] THEN
    R=0
    G=220
    B=220
    ELSIF UP<UP[1] THEN
    R=220
    G=220
    B=0
    ENDIF
    //--------------------------------------------//
    return up as "Upper band"style(DOTTEDLINE4,1)coloured(r,g,b),dw as "Lower band"style(DOTTEDLINE4,1)coloured(r,g,b)
    

    no se por que pero no funciona

    #241498 quote
    Iván González
    Moderator
    Master
    for i=min(499,barindex-1) downto 0 do
    if i MOD 2 <> 0 then
    if $y2[i]+sae1>$y2[i-1]+sae1 then
    drawpoint(barindex[i],$y2[i]+sae1,2)coloured("red")
    drawpoint(barindex[i],$y2[i]-sae1,2)coloured("red")
    else
    drawpoint(barindex[i],$y2[i]+sae1,2)coloured("green")
    drawpoint(barindex[i],$y2[i]-sae1,2)coloured("green")
    endif
    endif
    if close[i]>$y2[i]+sae1 and close[i+1]<=$y2[i+1]+sae1 then
    drawarrowdown(barindex[i],high[i])coloured("red")
    elsif close[i]<$y2[i]-sae1 and close[i+1]>=$y2[i+1]-sae1 then
    drawarrowup(barindex[i],low[i])coloured("green")
    endif
    next
    Armand2020 thanked this post
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Nadaraya Watson Envelope


ProBuilder: Indicadores y Herramientas

New Reply
Author
author-avatar
Azkune @azkune Participant
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by Iván González
1 year, 1 month ago.

Topic Details
Forum: ProBuilder: Indicadores y Herramientas
Language: Spanish
Started: 10/17/2024
Status: Active
Attachments: 1 files
Logo Logo
Loading...