[DMcL] Nadaraya-Watson Kernel v2

beta
By dmaclennan in Trend Published June 2023 👁 1,595 views 💬 1 comments

Description

-- original indicator by jdehorty -- https://www.tradingview.com/script/AWNvbPRM-Nadaraya-Watson-Rational-Quadratic-Kernel-Non-Repainting/
HaasScript
-- original script by jdehorty on tradingview
-- Author: DMcL
EnableHighSpeedUpdates(true)

DefineCommand('Nadaraya_Watson_Kernel_v2', 'Signals long or short when kernel changes bullish/bearish')

OptimizedForInterval(0, 
    function()

-- Parameters

local alpha = DefineParameter(NumberType, 'Alpha', 'NQK Alpha', true, 2)
local h = DefineParameter(NumberType, 'Lookback Window', 'Lookback Window', true, 41)
local r = DefineParameter(NumberType, 'Relative Weighting', 'Relative Weighting', true, 6)
local x_0 = DefineParameter(NumberType, "Start Regression at Bar", "Start Regression at Bar", true, 19)


-- Data
local close = ClosePrices()
local ohlc = OHLCPrices()
local high = HighPrices()
local low = LowPrices()



-- Kernel function

local function kernel_regression (_src, _alpha, _r, _h)
    local _currentWeight = 0
    local _cumulativeWeight = 0
    for i = 1, _alpha + x_0 do
        local y = _src[i]
        local w = Pow( 1 + (Pow(i, 2) / (Pow(_h, 2) * 2 * _r )), -_r)
         _currentWeight = _currentWeight + (y*w)
         _cumulativeWeight = _cumulativeWeight + w
    end
    return  _currentWeight / _cumulativeWeight
end

-- Estimations

local yhat1 = CC_IndicatorMemory(
                function(i, mem)
                return kernel_regression(ohlc, alpha, r, h)
                end)
local yhat2 = CC_IndicatorMemory(
                function(i, mem)
                return kernel_regression(ohlc, alpha, r, h - 2)
                end)



-- Rates of Change
local wasBearish = yhat1[3] > yhat1[2]
local wasBullish = yhat1[3] < yhat1[2]
local isBearish = yhat1[2] > yhat1[1]
local isBullish = yhat1[2] < yhat1[1]
local isBearishChange = isBearish and wasBullish
local isBullishChange = isBullish and wasBearish

-- Crossovers
local isBullishCross = CrossOver(yhat2, yhat1)
local isBearishCross = CrossUnder(yhat2, yhat1) 
local isBullishSmooth = yhat2[1] > yhat1[1]
local isBearishSmooth = yhat2[1] < yhat1[1]


-- Plot
Plot(0, "Rational Quadratic Kernel Estimate", yhat1, {color=Aqua(50), width=2})



-- // Alert Variables
local alertBullish = isBullishChange
local alertBearish = isBearishChange


local signal = SignalNone


if alertBullish then
    signal = SignalLong
    PlotShape(0, ShapeTriangleUp, DarkGreen, 3, false)
end

if alertBearish then
    signal = SignalShort
    PlotShape(0, ShapeTriangleDown, Red, 3, true)
end


DefineOutput(EnumType, signal)

end)

1 Comment

Sign in to leave a comment.

A
Anom over 2 years ago

Hey dmaclennan, Thank you for the script. I'd love to get in touch with you abt getting Funding Rates and Open Interest in Haasbot from an external API.
I'm a noob on the scripting part, unfortunately.
Send you a friend request. Hope we get in touch! Br, thisuser