Indicator · Pine Script v5 · On the chart

Kernel Regression Toolkit

by regalouisei· code credited to jdehorty © veryfid

The Kernel Regression Toolkit calculates and plots non-linear regression estimations directly on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code99
Settings40inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago25 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

99 lines of Pine Script v5 from pinescript/editors_picks/kernel-regression-toolkit.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

kernel-regression-toolkit.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jdehorty © veryfid

//@version=5

indicator(title='Kernel Regression Toolkit', shorttitle='kreg', overlay=true)
import veryfid/KernelFunctionsFilters/1 as kreg

// For more information on this technique refer to to the original open source indicator by @jdehorty located here: 
// https://www.tradingview.com/script/AWNvbPRM-Nadaraya-Watson-Rational-Quadratic-Kernel-Non-Repainting/

// Nadaraya-Watson Kernel Regression Settings
src = input(close)

filt1 = input.string("Smooth", options = ["No Filter", "Smooth", "Zero Lag"],title = "",group ="Kernel Regression 1 - Fast", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
type1 = input.string("Rational Quadratic", options = ["Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"],title = "",group ="Kernel Regression 1 - Fast", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
h = input.int(3, 'Lookback Window', minval=3, group="Kernel Regression 1 - Fast", inline="k")
r = input.float(1., 'Weighting', step=0.25, tooltip='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', group="Kernel Regression 1 - Fast", inline="k1")
x = input.int(2, "Level", tooltip='Bar index on which to start regression. Controls how tightly fit the kernel estimate is to the data. Smaller values are a tighter fit. Larger values are a looser fit. Recommended range: 2-25', group="Kernel Regression 1 - Fast", inline="k1")
lag = input.int(2, "Lag", tooltip="Lag for crossover detection. Lower values result in earlier crossovers. Recommended range: 1-2", inline='k1', group="Kernel Regression 1 - Fast")

filt2 = input.string("Smooth", options = ["No Filter", "Smooth", "Zero Lag"],title = "",group ="Kernel Regression 2 - Medium", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
type2 = input.string("Rational Quadratic", options = ["Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"],title = "",group ="Kernel Regression 2 - Medium", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
h2 = input.int(8, 'Lookback Window', minval=3, tooltip='The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50', group="Kernel Regression 2 - Medium", inline="k")
r2 = input.float(1., 'Weighting', step=0.25, tooltip='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', group="Kernel Regression 2 - Medium", inline="kernel2")
x2 = input.int(20, "Level", tooltip='Bar index on which to start regression. Controls how tightly fit the kernel estimate is to the data. Smaller values are a tighter fit. Larger values are a looser fit. Recommended range: 2-25', group="Kernel Regression 2 - Medium", inline="kernel2")
lag2 = input.int(2, "Lag", tooltip="Lag for crossover detection. Lower values result in earlier crossovers. Recommended range: 1-2", inline='kernel2', group='Kernel Regression 2 - Medium')

filt3 = input.string("Smooth", options = ["No Filter", "Smooth", "Zero Lag"],title = "",group ="Kernel Regression 3 - Slow", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
type3 = input.string("Rational Quadratic", options = ["Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"],title = "",group ="Kernel Regression 3 - Slow", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
h3 = input.int(200, 'Lookback Window', minval=3, tooltip='The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50', group="Kernel Regression 3 - Slow", inline="k")
r3 = input.float(1., 'Weighting', step=0.25, tooltip='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', group="Kernel Regression 3 - Slow", inline="kernel3")
x3 = input.int(500, "Level", tooltip='Bar index on which to start regression. Controls how tightly fit the kernel estimate is to the data. Smaller values are a tighter fit. Larger values are a looser fit. Recommended range: 2-25', group="Kernel Regression 3 - Slow", inline="kernel3")
lag3 = input.int(2, "Lag", tooltip="Lag for crossover detection. Lower values result in earlier crossovers. Recommended range: 1-2", inline='kernel3', group='Kernel Regression 3 - Slow')

filt4 = input.string("Smooth", options = ["No Filter", "Smooth", "Zero Lag"],title = "",group ="Kernel Regression 4 - Mono", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
type4 = input.string("Rational Quadratic", options = ["Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"],title = "",group ="Kernel Regression 4 - Mono", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
h4 = input.int(30, 'Lookback Window', minval=3, group="Kernel Regression 4 - Mono", inline="k")
r4 = input.float(8., 'Weighting', step=0.25, tooltip='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', group="Kernel Regression 4 - Mono", inline="k1")
x4 = input.int(35, "Level", tooltip='Bar index on which to start regression. Controls how tightly fit the kernel estimate is to the data. Smaller values are a tighter fit. Larger values are a looser fit. Recommended range: 2-25', group="Kernel Regression 4 - Mono", inline="k1")
lag4 = input.int(2, "Lag", tooltip="Lag for crossover detection. Lower values result in earlier crossovers. Recommended range: 1-2", inline='k1', group="Kernel Regression 4 - Mono")

filt5 = input.string("Smooth", options = ["No Filter", "Smooth", "Zero Lag"],title = "",group ="Kernel Regression 5 - Mono", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
type5 = input.string("Rational Quadratic", options = ["Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"],title = "",group ="Kernel Regression 5 - Mono", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
h5 = input.int(69, 'Lookback Window', minval=3, group="Kernel Regression 5 - Mono", inline="k")
r5 = input.float(1, 'Weighting', step=0.25, tooltip='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', group="Kernel Regression 5 - Mono", inline="k1")
x5 = input.int(500, "Level", tooltip='Bar index on which to start regression. Controls how tightly fit the kernel estimate is to the data. Smaller values are a tighter fit. Larger values are a looser fit. Recommended range: 2-25', group="Kernel Regression 5 - Mono", inline="k1")
lag5 = input.int(2, "Lag", tooltip="Lag for crossover detection. Lower values result in earlier crossovers. Recommended range: 1-2", inline='k1', group="Kernel Regression 5 - Mono")

filt6 = input.string("Zero Lag", options = ["No Filter", "Smooth", "Zero Lag"],title = "",group ="Kernel Regression 6 - Mono", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
type6 = input.string("Rational Quadratic", options = ["Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"],title = "",group ="Kernel Regression 6 - Mono", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
h6 = input.int(200, 'Lookback Window', minval=3, group="Kernel Regression 6 - Mono", inline="k")
r6 = input.float(1., 'Weighting', step=0.25, tooltip='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', group="Kernel Regression 6 - Mono", inline="k1")
x6 = input.int(500, "Level", tooltip='Bar index on which to start regression. Controls how tightly fit the kernel estimate is to the data. Smaller values are a tighter fit. Larger values are a looser fit. Recommended range: 2-25', group="Kernel Regression 6 - Mono", inline="k1")
lag6 = input.int(2, "Lag", tooltip="Lag for crossover detection. Lower values result in earlier crossovers. Recommended range: 1-2", inline='k1', group="Kernel Regression 6 - Mono")

ksrc = kreg.rationalQuadratic(close, 3, 1, 1, "No FIlter")
line1 = type1 == "Rational Quadratic" ? kreg.rationalQuadratic(src, h, r, x, filt1) : type1 == "Gaussian" ? kreg.gaussian(src, h-lag, x, filt1) : type1 == "Periodic" ? kreg.periodic(src, h, lag, x, filt1) : type1 == "Locally Periodic" ? kreg.locallyPeriodic(src, h, lag, x, filt1) : na
line2 = type2 == "Rational Quadratic" ? kreg.rationalQuadratic(src, h2, r2, x2, filt2) : type2 == "Gaussian" ? kreg.gaussian(src, h2-lag2, x2, filt2) : type2 == "Periodic" ? kreg.periodic(src, h2, lag2, x2, filt2) : type2 == "Locally Periodic" ? kreg.locallyPeriodic(src, h2, lag2, x2, filt2) : na
line3 = type3 == "Rational Quadratic" ? kreg.rationalQuadratic(src, h3, r3, x3, filt3) : type3 == "Gaussian" ? kreg.gaussian(src, h3-lag3, x3, filt3) : type3 == "Periodic" ? kreg.periodic(src, h3, lag3, x3, filt3) : type3 == "Locally Periodic" ? kreg.locallyPeriodic(src, h3, lag3, x3, filt3) : na
line4 = type4 == "Rational Quadratic" ? kreg.rationalQuadratic(src, h4, r4, x4, filt4) : type4 == "Gaussian" ? kreg.gaussian(src, h4-lag4, x4, filt4) : type4 == "Periodic" ? kreg.periodic(src, h4, lag4, x4, filt4) : type4 == "Locally Periodic" ? kreg.locallyPeriodic(src, h4, lag4, x4, filt4) : na
line5 = type5 == "Rational Quadratic" ? kreg.rationalQuadratic(src, h5, r5, x5, filt5) : type5 == "Gaussian" ? kreg.gaussian(src, h5-lag5, x5, filt5) : type5 == "Periodic" ? kreg.periodic(src, h5, lag5, x5, filt5) : type5 == "Locally Periodic" ? kreg.locallyPeriodic(src, h5, lag5, x5, filt5) : na
line6 = type6 == "Rational Quadratic" ? kreg.rationalQuadratic(src, h6, r6, x6, filt6) : type6 == "Gaussian" ? kreg.gaussian(src, h6-lag6, x6, filt6) : type6 == "Periodic" ? kreg.periodic(src, h6, lag6, x6, filt6) : type6 == "Locally Periodic" ? kreg.locallyPeriodic(src, h6, lag6, x6, filt6) : na

usej1 = input(false,"Show J line 1",group ="J Line 1", inline = "k")
jline1src1 = input.string("Line 2", options = ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close"],title = "",group ="J Line 1", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
jline1src2 = input.string("Line 4", options = ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close"],title = "",group ="J Line 1", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
jline1a = jline1src1 == "Line 1" ? line1 : jline1src1 == "Line 2" ? line2 : jline1src1 == "Line 3" ? line3 : jline1src1 == "Line 4" ? line4 : jline1src1 == "Line 5" ? line5 : jline1src1 == "Line 6" ? line6 : jline1src1 == "Close" ? close : jline1src1 == "Ksrc" ? ksrc : ksrc 
jline1b = jline1src2 == "Line 1" ? line1 : jline1src2 == "Line 2" ? line2 : jline1src2 == "Line 3" ? line3 : jline1src2 == "Line 4" ? line4 : jline1src2 == "Line 5" ? line5 : jline1src2 == "Line 6" ? line6 : jline1src2 == "Close" ? close : jline1src2 == "Ksrc" ? ksrc : ksrc 
jline1 = kreg.j(jline1a,jline1b)

usej2 = input(false,"Show J line 2",group ="J Line 2", inline = "k")
jline2src1 = input.string("Line 3", options = ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close", "J Line 1"],title = "",group ="J Line 2", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
jline2src2 = input.string("Line 5", options = ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close", "J Line 1"],title = "",group ="J Line 2", inline = "k", tooltip = "Select Kernel Regression type from dropdown box. The Lookback Window is the number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50 ") 
jline2a = jline2src1 == "Line 1" ? line1 : jline2src1 == "Line 2" ? line2 : jline2src1 == "Line 3" ? line3 : jline2src1 == "Line 4" ? line4 : jline2src1 == "Line 5" ? line5 : jline2src1 == "Line 6" ? line6 : jline2src1 == "Close" ? close : jline2src1 == "J Line 1" ? jline1 : ksrc 
jline2b = jline2src2 == "Line 1" ? line1 : jline2src2 == "Line 2" ? line2 : jline2src2 == "Line 3" ? line3 : jline2src2 == "Line 4" ? line4 : jline2src2 == "Line 5" ? line5 : jline2src2 == "Line 6" ? line6 : jline2src2 == "Close" ? close : jline2src2 == "J Line 1" ? jline1 : ksrc 

jline2 = kreg.j(jline2a,jline2b)

useblend = input(false,"Show",group ="Blend Two Lines", inline = "k")
blendsrc1 = input.string("Line 2", options = ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close"],title = "",group ="Blend Two Lines", inline = "k", tooltip = "Select two Kernel Regression lines you would like to blend from the dropdown box. Adjust the percentage of the blend from 0-100%. It is also possible to overdrive the blend by pushing it over 100") 
blendsrc2 = input.string("Line 4", options = ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close"],title = "",group ="Blend Two Lines", inline = "k")
blenda = blendsrc1 == "Line 1" ? line1 : blendsrc1 == "Line 2" ? line2 : blendsrc1 == "Line 3" ? line3 : blendsrc1 == "Line 4" ? line4 : blendsrc1 == "Line 5" ? line5 : blendsrc1 == "Line 6" ? line6 : blendsrc1 == "Close" ? close : blendsrc1 == "Ksrc" ? ksrc : ksrc 
blendb = blendsrc2 == "Line 1" ? line1 : blendsrc2 == "Line 2" ? line2 : blendsrc2 == "Line 3" ? line3 : blendsrc2 == "Line 4" ? line4 : blendsrc2 == "Line 5" ? line5 : blendsrc2 == "Line 6" ? line6 : blendsrc2 == "Close" ? close : blendsrc2 == "Ksrc" ? ksrc : ksrc 
weight = input(50,"",group ="Blend Two Lines", inline = "k")
weightcalc = weight/100
blend = blenda*weightcalc + blendb * (1 - weightcalc)

plot(line1,"Line1", color= line1 > line1[1] ? color.teal : color.red, linewidth=2)
plot(line2,"Line2", color= line2 > line2[1] ? color.teal : color.red , linewidth=2)
plot(line3,"Line3", color= line3 > line3[1] ? color.teal : color.red , linewidth=2)
plot(line4,"Line4", color= color.gray, linewidth=1)
plot(line5,"Line5", color= color.gray, linewidth=1)
plot(line6,"Line6", color= color.gray, linewidth=1)

plot(usej1 ? jline1 : na,"j1",color = color.white)
plot(usej2 ? jline2 : na,"j2",color = color.white)

plot(useblend ? blend : na,"Blend")

Overview

Kernel Regression Toolkit provides multiple configurable kernel regression lines using functions such as Rational Quadratic, Gaussian, Periodic, and Locally Periodic kernels. Users can apply filtering methods like Smooth or Zero Lag, adjust lookback windows, weighting parameters, level fitting, and lag to tailor the responsiveness and trend-filtering capabilities.

Details

Published by

regalouisei on GitHub

Code credit

jdehorty © veryfid, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
srcsourceclosesrc
filt1stringSmoothfilt1
type1stringRational Quadratictype1
Lookback Windowint3h
Weightingfloat1.r
Levelint2x
Lagint2lag
filt2stringSmoothfilt2
type2stringRational Quadratictype2
Lookback Windowint8h2
Weightingfloat1.r2
Levelint20x2
Show all 40 settings
SettingTypeDefaultVariable
Lagint2lag2
filt3stringSmoothfilt3
type3stringRational Quadratictype3
Lookback Windowint200h3
Weightingfloat1.r3
Levelint500x3
Lagint2lag3
filt4stringSmoothfilt4
type4stringRational Quadratictype4
Lookback Windowint30h4
Weightingfloat8.r4
Levelint35x4
Lagint2lag4
filt5stringSmoothfilt5
type5stringRational Quadratictype5
Lookback Windowint69h5
Weightingfloat1r5
Levelint500x5
Lagint2lag5
filt6stringZero Lagfilt6
type6stringRational Quadratictype6
Lookback Windowint200h6
Weightingfloat1.r6
Levelint500x6
Lagint2lag6
Show J line 1boolfalseusej1
jline1src1stringLine 2jline1src1
jline1src2stringLine 4jline1src2
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

947 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 12 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B

Indicator v5 Moving averagesTrend
Updated 2 months ago · MIT GitHub repository of Multiple Moving Averages

NVDA Trend Ribbon

by longsurf-ai
280 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated 3 days ago · MIT GitHub repository of NVDA Trend Ribbon
257 stars

Donald SPX - Options Trading is an overlay indicator designed for intraday SPX analysis. It plots a 200-period Weighted Moving Average (WMA) on the price chart while evaluating multi-timeframe market structure across Daily, 15-minute, 5-minute, and 2-minute ba

Indicator v5 Moving averagesTrend Repaints
Updated 7 weeks ago · None stated GitHub repository of Donald SPX - Options Trading
228 stars

This indicator computes moving average and RSI bias signals across the current timeframe and four higher timeframes. It sums biases from the MA and RSI to derive an overall bullish or bearish signal. The results appear in a configurable table that displays eac

Indicator v6 TrendMoving averages
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe Analysis Template

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers