Nadaraya Watson Envelope
Forums › ProRealTime foro Español › Soporte ProBuilder › Nadaraya Watson Envelope
- This topic has 1 reply, 2 voices, and was last updated 3 weeks ago by Iván.
Viewing 2 posts - 1 through 2 (of 2 total)
-
-
10/17/2024 at 11:31 PM #239170
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=5indicator(“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’)//StyleupCss = input.color(color.teal, ‘Colors’, inline = ‘inline1’, group = ‘Style’)dnCss = input.color(color.red, ”, inline = ‘inline1’, group = ‘Style’)//—————————————————————————–}//Functions//—————————————————————————–{//Gaussian windowgauss(x, h) => math.exp(-(math.pow(x, 2)/(h * h * 2)))//—————————————————————————–}//Append lines//—————————————————————————–{n = bar_indexvarln=array.new_line(0)if barstate.isfirst and repaintfori=0to499array.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 repaintfori=0to499w=gauss(i,h)coefs.push(w)den:=coefs.sum()out = 0.if not repaintfori=0to499out+=src[i]*coefs.get(i)out /= denmae = ta.sma(math.abs(src – out), 499) * multupper = out + maelower = out – mae//—————————————————————————–}//Compute and display NWE//—————————————————————————–{float y2 = nafloat y1 = nanwe = array.new<float>(0)if barstate.islast and repaintsae=0.//Compute and set NWE pointfori=0tomath.min(499,n-1)sum=0.sumw=0.//Compute weighted meanforj=0tomath.min(499,n-1)w=gauss(i-j,h)sum+=src[j]*wsumw+=wy2:=sum/sumwsae+=math.abs(src[i]-y2)nwe.push(y2)sae:=sae/math.min(499,n-1)*multfori=0tomath.min(499,n-1)ifi%2line.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)+saelabel.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)-saelabel.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 repainttb.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 Arrowsplotshape(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)//—————————————————————————–}10/29/2024 at 10:53 AM #23968812345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091//--------------------------------------------////PRC Nadaraya-Watson Envelope//version = 0//28.02.24//Iván González @ www.prorealcode.com//Sharing ProRealTime knowledge//--------------------------------------------////-----------Inputs---------------------------////--------------------------------------------//h1=8 //Bandwidthmult=3length=499 //Window Sizerepaint=1src=close//--------------------------------------------////----------Repaint ON------------------------////--------------------------------------------//if islastbarupdate and repaint thensae=0for i=0 to min(499,barindex-1) dosum=0sumw=0for j=0 to min(499,barindex-1) dox=i-jw=exp(-(pow(x,2)/(h1*h1*2)))sum=src[j]*w+sumsumw=w+sumwnext$y2[i]=sum/sumwsae=sae+abs(src[i]-$y2[i])nextsae1=sae/min(499,barindex-1)*multfor i=min(499,barindex-1) downto 0 doif i MOD 2 <> 0 thendrawpoint(barindex[i],$y2[i]+sae1,2)coloured("green")drawpoint(barindex[i],$y2[i]-sae1,2)coloured("red")endifif close[i]>$y2[i]+sae1 and close[i+1]<=$y2[i+1]+sae1 thendrawarrowdown(barindex[i],high[i])coloured("red")elsif close[i]<$y2[i]-sae1 and close[i+1]>=$y2[i+1]-sae1 thendrawarrowup(barindex[i],low[i])coloured("green")endifnextendif//--------------------------------------------////----------Repaint OFF-----------------------////--------------------------------------------//if repaint=0 thenonce init = 1If init = 1 thenden = 0For i = 0 to length-1 do$coefs[i]=exp(-(i*i)/(h1*h1*2))den = den + $coefs[i]Nextinit = 0Endifout = 0For i = 0 to length-1 doout = out + src[i]*$coefs[i]Nextout = out/denmae = exponentialAverage[length](abs(src-out)) * multupper = out + maelower = out - maeif close crosses over upper thendrawarrowdown(barindex,high)coloured("red")elsif close crosses under lower thendrawarrowup(barindex,low)coloured("green")endifendif//--------------------------------------------////----------Plot Bands------------------------////--------------------------------------------//if repaint thenup=undefineddw=undefinedelseup=upperdw=lowerendif//--------------------------------------------//return up as "Upper band"style(line,2)coloured("Green"),dw as "Lower band"style(line,2)coloured("red") -
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)