TradingView
jdehorty
2022년 10월 16일 오전 9시 31분

KernelFunctions 

ETHUSDT SPOTBybit

설명

Library "KernelFunctions"
This library provides non-repainting kernel functions for Nadaraya-Watson estimator implementations. This allows for easy substitution/comparison of different kernel functions for one another in indicators. Furthermore, kernels can easily be combined with other kernels to create newer, more customized kernels. Compared to Moving Averages (which are really just simple kernels themselves), these kernel functions are more adaptive and afford the user an unprecedented degree of customization and flexibility.

rationalQuadratic(_src, _lookback, _relativeWeight, _startAtBar)
  Rational Quadratic Kernel - An infinite sum of Gaussian Kernels of different length scales.
  Parameters:
    _src: <float series> The source series.
    _lookback: <simple int> The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars.
    _relativeWeight: <simple float> Relative weighting of time frames. Smaller values result in a more stretched-out curve, and larger values will result in a more wiggly curve. 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.
    _startAtBar: <simple int> Bar index on which to start regression. The first bars of a chart are often highly volatile, and omitting these initial bars often leads to a better overall fit.
  Returns: yhat <float series> The estimated values according to the Rational Quadratic Kernel.

gaussian(_src, _lookback, _startAtBar)
  Gaussian Kernel - A weighted average of the source series. The weights are determined by the Radial Basis Function (RBF).
  Parameters:
    _src: <float series> The source series.
    _lookback: <simple int> The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars.
    _startAtBar: <simple int> Bar index on which to start regression. The first bars of a chart are often highly volatile, and omitting these initial bars often leads to a better overall fit.
  Returns: yhat <float series> The estimated values according to the Gaussian Kernel.

periodic(_src, _lookback, _period, _startAtBar)
  Periodic Kernel - The periodic kernel (derived by David Mackay) allows one to model functions that repeat themselves exactly.
  Parameters:
    _src: <float series> The source series.
    _lookback: <simple int> The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars.
    _period: <simple int> The distance between repititions of the function.
    _startAtBar: <simple int> Bar index on which to start regression. The first bars of a chart are often highly volatile, and omitting these initial bars often leads to a better overall fit.
  Returns: yhat <float series> The estimated values according to the Periodic Kernel.

locallyPeriodic(_src, _lookback, _period, _startAtBar)
  Locally Periodic Kernel - The locally periodic kernel is a periodic function that slowly varies with time. It is the product of the Periodic Kernel and the Gaussian Kernel.
  Parameters:
    _src: <float series> The source series.
    _lookback: <simple int> The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars.
    _period: <simple int> The distance between repititions of the function.
    _startAtBar: <simple int> Bar index on which to start regression. The first bars of a chart are often highly volatile, and omitting these initial bars often leads to a better overall fit.
  Returns: yhat <float series> The estimated values according to the Locally Periodic Kernel.

릴리즈 노트

v2

Updated:
Allow float for relativeWeight of the Rational Quadratic Kernel
rationalQuadratic(_src, _lookback, _relativeWeight, _startAtBar)
  Rational Quadratic Kernel - An infinite sum of Gaussian Kernels of different length scales.
  Parameters:
    _src: <float series> The source series.
    _lookback: <simple int> The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars.
    _relativeWeight: <simple float> Relative weighting of time frames. Smaller values resut in a more stretched out curve and larger values will result in a more wiggly curve. 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.
    _startAtBar: <simple int> 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.
  Returns: yhat <float series> The estimated values according to the Rational Quadratic Kernel.
코멘트
v3j3v
Awesome good work
davewantsmoore
Thanks for sharing!

I have a question about _size = array.size(array.from(_src))
This is always = 1 (?!)
What is the purpose of using array.from here?
jdehorty
@davewantsmoore, Great question. I do not believe it is necessary, and if you were to fork this and modify it into your own version, you are probably safe not to include it. In the past, I encountered a strange error on some charts that prevented the kernel estimate from completing the for-loop. For some reason, the loop was getting short-circuited, and the error I was receiving was to the effect of out-of-bounds indexing, as I recall. To be honest, I had a hard time isolating why exactly this was happening, but after trying many different things, this particular one seemed to solve the issues I was having, but I think any syntax that ensures the existence of the series prior to iterating would work. But in any case, this was a rather rare occurrence and likely isn't needed at all, so feel free not to include it. Since it doesn't hurt anything, I have decided to leave it in this particular library out of an abundance of caution.
davewantsmoore
@jdehorty, Thanks :)
omryzci737
I am trying to convert into python code. Do you have any python code of rationalQuadratic ?
lcastilloe
Great job! one question, in order to determine a potential reversal level/zone, have you considered to add another 3rd zone on the envelope (beyond the upper/lower levels)? Does it meake sense for handling those really big market movements?
Dahikino
Hello Justin, great job !!!
edzim
can you write a kernel for the levy distribution?
tkarolak
@edzim,

levyKernel(series float _src, simple int _lookback, simple float _mu, simple float _c, simple int _startAtBar) => float _currentWeight = 0. float _cumulativeWeight = 0. _size = array.size(array.from(_src)) for i = 0 to _size + _startAtBar y = _src x = i + _mu // Adjusting for the location parameter if x > _mu w = math.sqrt(_c / (2 * math.pi)) * math.exp(-_c / (2 * (x - _mu))) / math.pow((x - _mu), 1.5) _currentWeight += y * w _cumulativeWeight += w else w = 0 yhat = _cumulativeWeight != 0 ? _currentWeight / _cumulativeWeight : na yhat // Example Usage yhat = levyKernel(close, 14, 0, 1, 25) plot(yhat, color=color.red, title="Levy Kernel Estimate")


Here are some suggestions:

_lookback (simple int):

Description: This parameter defines the number of bars to look back from the current bar for the kernel calculation.
Typical Values: Between 10 and 50. A smaller lookback period makes the kernel more responsive to recent changes, while a larger period smoothens the output by considering more historical data.
Default Suggestion: 20.
_mu (simple float):

Description: In the context of the Lévy distribution, mu is the location parameter. It shifts the distribution along the X-axis.
Typical Values: Often close to zero, but it can be adjusted based on the desired offset.
Default Suggestion: 0.0.
_c (simple float):

Description: The scale parameter c affects the "width" of the Lévy distribution. It influences the dispersion of the distribution.
Typical Values: Between 0.1 and 2.0. A smaller c results in a more "spread out" distribution, while a larger c concentrates it more tightly.
Default Suggestion: 1.0.
_startAtBar (simple int):

Description: This parameter determines the starting point of the kernel calculation in terms of bar index. It's useful for omitting initial bars which may contain volatile data.
Typical Values: Between 5 and 30. The choice depends on how much of the initial data you want to exclude from the calculation.
Default Suggestion: 10.
philippe_nadeau
@tkarolak, thanks for sharing this, 2 questions : 1. _lookback is not part of the code, but I see 2 times "x" should these be replaced by _lookback? 2. applying a lag on x does not work for the crossovers, do you have an idea? (similar to yhat1 and yhat2 jdehorty is referering there tradingview.com/script/AWNvbPRM-Nadaraya-Watson-Rational-Quadratic-Kernel-Non-Repainting/)
더보기