Nadaraya-Watson: Rational Quadratic Kernel (Non-Repainting)
Forums › ProRealTime English forum › ProBuilder support › Nadaraya-Watson: Rational Quadratic Kernel (Non-Repainting)
- This topic has 14 replies, 6 voices, and was last updated 1 year ago by LucasBest.
-
-
08/31/2023 at 7:25 AM #220102
Hi All,
I was wondering if it is possible to replicate an indicator from TradingView.
It is called Nadaraya-Watson: Rational Quadratic Kernel (Non-Repainting). On the charts, it kind of looks like a regular Moving Average. But you can set the lines based on another timefrime
You can find more info and the code on TV
Thank you
08/31/2023 at 7:44 AM #220103Hi,
translated here by Nicolas last year:
https://www.prorealcode.com/topic/conversion-from-trading-view-nadaraya-watson-envelope/#post-196311
08/31/2023 at 8:33 AM #22010508/31/2023 at 8:56 AM #220107Sorry, I recognised the name and gave the forum link assuming you were after the mid-line (the y2 variable of the return line), if what you’re after is not the same as this y2 then I don’t remember other Nadaraya-Watson code already made in the forum, Nicolas will assess whether it can be converted (and if yes will add it to the conversions list).
08/31/2023 at 9:25 AM #22010808/31/2023 at 7:03 PM #22015009/01/2023 at 10:38 AM #22017209/01/2023 at 1:13 PM #220180Done!
NadarayaWatsRationalQuadKernel123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121// © jdehorty// @version=5// A non-repainting implementation of Nadaraya-Watson Regression using a Rational Quadratic Kernel.// Settingsonce bullishR = 0once bullishG = 250once bullishB = 154once bearishR = 255once bearishG = 69once bearishB = 0src = customclose// hh = Lookback Window, minval=3.0, tooltip='The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50r = rr*0.25//rr = Relative Weighting, step=0.25, Relative weighting of time frames. As this value approaches zero, the longer time frames will exert more influence on the estimation. As this value approaches infinity, the behavior of the Rational Quadratic Kernel will become identical to the Gaussian kernel. Recommended range: 0.25-25 => RR from 1 to 100//xx = 25, Start Regression at Bar, Bar index on which to start regression. The first bars of a chart are often highly volatile, and omission of these initial bars often leads to a better overall fit. Recommended range: 5-25//smoothColors = false, Smooth Colors, tooltip=Uses a crossover based mechanism to determine colors. This often results in less color transitions overall//lag = 2, Lag for crossover detection. Lower values result in earlier crossovers. Recommended range: 1-2//size = array.size(array.from(src)) // size of the data seriessize = Min(Barindex,Maxlength)// Further Reading:// The Kernel Cookbook: Advice on Covariance functions. David Duvenaud. Published June 2014.// Estimation of the bandwidth parameter in Nadaraya-Watson kernel non-parametric regression based on universal threshold level. Ali T, Heyam Abd Al-Majeed Hayawi, Botani I. Published February 26, 2021.// EstimationscurrentWeight = 0cumulativeWeight = 0for i = 0 to Size-xx-1 doy = src[i]w = POW(1+(POW(i,2)/(POW(hh,2)*2*r)),-r)currentWeight = currentWeight + y*wcumulativeWeight = cumulativeWeight + wNextyhat1 = currentWeight / cumulativeWeightcurrentWeight = 0cumulativeWeight = 0for i = 0 to Size-xx-1 doy = src[i]w = POW(1+(POW(i,2)/(POW(hh-lag,2)*2*r)),-r)currentWeight = currentWeight + y*wcumulativeWeight = cumulativeWeight + wNextyhat2 = currentWeight / cumulativeWeight// Rates of ChangewasBearish = yhat1[2] > yhat1[1]wasBullish = yhat1[2] < yhat1[1]isBearish = yhat1[1] > yhat1isBullish = yhat1[1] < yhat1isBearishChange = isBearish and wasBullishisBullishChange = isBullish and wasBearish// CrossoversisBullishCross = yhat2 crosses over yhat1isBearishCross = yhat2 crosses under yhat1isBullishSmooth = yhat2 > yhat1isBearishSmooth = yhat2 < yhat1// ColorsIf isBullishSmooth thencolorByCrossR = bullishRcolorByCrossG = bullishGcolorByCrossB = bullishBElsecolorByCrossR = bearishRcolorByCrossG = bearishGcolorByCrossB = bearishBEndifIf isBullish thencolorByRateR = bullishRcolorByRateG = bullishGcolorByRateB = bullishBElsecolorByRateR = bearishRcolorByRateG = bearishGcolorByRateB = bearishBEndifIf smoothColors thenR = colorByCrossRG = colorByCrossGB = colorByCrossBElseR = colorByRateRG = colorByRateGB = colorByRateBEndif// Alert VariablesIf smoothColors thenalertBullish = isBearishCrossElsealertBullish = isBearishChangeEndifIf smoothColors thenalertBearish = isBullishCrossElsealertBearish = isBullishChangeEndif// Alerts for Color Changes//alertcondition(condition=alertBullish, title='Bearish Color Change', message='Nadaraya-Watson: {{ticker}} ({{interval}}) turned Bearish ▼')//alertcondition(condition=alertBearish, title='Bullish Color Change', message='Nadaraya-Watson: {{ticker}} ({{interval}}) turned Bullish ▲')// Non-Displayed Plot Outputs (i.e., for use in other indicators)//plot(alertBearish ? -1 : alertBullish ? 1 : 0, "Alert Stream", display=display.none)Return yhat1 as "Rational Quadratic Kernel Estimate" style(line,4) coloured(R,G,B,255)2 users thanked author for this post.
09/01/2023 at 3:02 PM #22018709/01/2023 at 3:02 PM #22018809/01/2023 at 3:42 PM #22018909/01/2023 at 4:05 PM #22019209/01/2023 at 4:25 PM #22019309/02/2023 at 5:25 PM #22022509/03/2023 at 1:25 PM #220249I added Maxlength because otherwise the indicator is very slow.
I just translate the original code (without improving it to be faster, which is possible) and it seems it uses all barindex for calculation (which is not relevant too because it does not change the result after 200 or 300 bars).
That is why i added Maxlength (too lazzy for improving the code)
But you can change 400 to 2000 or more if you want, you will notice no change in the result exept more time of calculationps : Lamaille is me
-
AuthorPosts
Find exclusive trading pro-tools on