[Giankam] Nadaraya-Watson: Envelope (Non-Repainting)

0 2295 Views 2 Comments 100 Tipped Credits 1 year ago
HTS CLOUDSTABLE
  • This is an envelope implementation of non-repainting Nadaraya-Watson indicator using the Rational Quadratic Kernel: Nadaraya-Watson

    What is Nadaraya–Watson Regression?
    Nadaraya–Watson Regression is a type of Kernel Regression, which is a non-parametric method for estimating the curve of best fit for a dataset. Unlike Linear Regression or Polynomial Regression, Kernel Regression does not assume any underlying distribution of the data. For estimation, it uses a kernel function, which is a weighting function that assigns a weight to each data point based on how close it is to the current point. The computed weights are then used to calculate the weighted average of the data points.

    How is this different from using a Moving Average?
    A Simple Moving Average is actually a special type of Kernel Regression that uses a Uniform (Retangular) Kernel function. This means that all data points in the specified lookback window are weighted equally. In contrast, the Rational Quadratic Kernel function used in this indicator assigns a higher weight to data points that are closer to the current point. This means that the indicator will react more quickly to changes in the data.

    Why use the Rational Quadratic Kernel over the Gaussian Kernel?
    The Gaussian Kernel is one of the most commonly used Kernel functions and is used extensively in many Machine Learning algorithms due to its general applicability across a wide variety of datasets. The Rational Quadratic Kernel can be thought of as a Gaussian Kernel on steroids; it is equivalent to adding together many Gaussian Kernels of differing length scales. This allows the user even more freedom to tune the indicator to their specific needs.
    The formula for the Rational Quadratic function is:
    K(x, x’) = (1 + ||x – x’||^2 / (2 * alpha * h^2))^(-alpha)
    where x and x’ data are points, alpha is a hyperparameter that controls the smoothness (i.e. overall “wiggle”) of the curve, and h is the band length of the kernel.

    Does this Indicator Repaint?
    No, this indicator has been intentionally designed to NOT repaint. This means that once a bar has closed, the indicator will never change the values in its plot. This is useful for backtesting and for trading strategies that require a non-repainting indicator.

    Example:

    
    local alpha = 2
    local nearFactor = 1.5
    local farFactor = 8
    local atr_length = 60
    local relative_weight = 8
    local lookback_w = 8
    
    local nqk = CC_NQK(alpha, nearFactor, farFactor, atr_length, relative_weight,lookback_w)
    
    Plot(0, 'nqk', nqk.kernel_avg , {color=White, width=4})
    PlotBBandsChart(0, 'upper', nqk.up_far, nqk.up_avg, nqk.up_near)
    PlotBBandsChart(0, 'lower', nqk.low_far, nqk.low_avg, nqk.low_near)
    
    • This topic was modified 1 year, 1 month ago by Giankam.
    • This topic was modified 1 year, 1 month ago by pshai. Reason: fixed example code
    • This topic was modified 1 year, 1 month ago by pshai.
    • This topic was modified 1 year, 1 month ago by Giankam.
    • This topic was modified 1 year, 1 month ago by Giankam.
    • This topic was modified 6 months, 3 weeks ago by Team HaasScripts.
    HaasScript Code
    Sign in or Register to download for free
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free