Kernel Regression Toolkit For ThinkOrSwim

apdusp

Active member
VIP
Mini Kernel Regression Toolkit derived from @veryfid's indicator

Hi, to our esteemed scripters, this is a reduced version of late @veryfid's (RIP) Kernel Regression Toolkit indicator (https://www.tradingview.com/script/ARPbTa5N-Kernel-Regression-Toolkit/) where I only left the 3 more relevant plots.

I could have left only the fast one as it the one I find more useful at this point, but leaving 3 allows for further exploring in the future, like Didi did with 3 simple moving averages in his Didi Index (just an idea).

The imported functions do use arrays, but maybe there is a way to circumvent the obstacles by assuming some reasonable restrictions?

In any case, thanks for trying
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Mini Kernel Regression Toolkit derived from @veryfid's indicator

Hi, to our esteemed scripters, this is a reduced version of late @veryfid's (RIP) Kernel Regression Toolkit indicator (https://www.tradingview.com/script/ARPbTa5N-Kernel-Regression-Toolkit/) where I only left the 3 more relevant plots.

I could have left only the fast one as it the one I find more useful at this point, but leaving 3 allows for further exploring in the future, like Didi did with 3 simple moving averages in his Didi Index (just an idea).

The imported functions do use arrays, but maybe there is a way to circumvent the obstacles by assuming some reasonable restrictions?

In any case, thanks for trying



// 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='Mini Kernel Regression Toolkit', shorttitle='mini-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/...son-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')

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

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)
check the below

CSS:
# https://www.tradingview.com/v/ARPbTa5N/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © jdehorty © veryfid
#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
# Converted by Sam4Cok@Samer800    - 11/2023
input src = close;
input showRegLine1 = yes;
input RegressionType1 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType1 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow1 = 3;         # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting1 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level1 = 2;                  # 'Bar index on which to start regression. Recommended range: 2-25'
input lag1 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine2 = yes;
input RegressionType2 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType2 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow2 = 8;         # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting2 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level2 = 20;                 # 'Bar index on which to start regression. Recommended range: 2-25'
input lag2 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine3 = yes;
input RegressionType3 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType3 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow3 = 200;       # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting3 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level3 = 500;                # 'Bar index on which to start regression. Recommended range: 2-25'
input lag3 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine4 = yes;
input RegressionType4 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType4 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow4 = 30;        # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting4 = 8.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level4 = 35;                 # 'Bar index on which to start regression. Recommended range: 2-25'
input lag4 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine5 = yes;
input RegressionType5 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType5 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow5 = 69;        # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting5 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level5 = 500;                # 'Bar index on which to start regression. Recommended range: 2-25'
input lag5 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine6 = yes;
input RegressionType6 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType6 = {"No Filter", "Smooth",default "Zero Lag"};
input LookbackWindow6 = 200;       # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting6 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level6 = 500;                # 'Bar index on which to start regression. Recommended range: 2-25'
input lag6 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input ShowJLine1 = no;    # "Show J line 1"
input jLine1Src1 = {"Line 1",Default "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close"};
input jLine1Src2 = {"Line 1", "Line 2", "Line 3",Default "Line 4", "Line 5", "Line 6","Close"};
input ShowJLine2 = no;    # "Show J line 2"
input jline2src1 = {"Line 1", "Line 2",Default "Line 3", "Line 4", "Line 5", "Line 6","Close", "J Line 1"};
input jline2src2 = {"Line 1", "Line 2", "Line 3", "Line 4",Default "Line 5", "Line 6","Close", "J Line 1"};
input useBlend = no;      # "Blend Two Lines"
input BlendTwoLinesWeight = 50;    # "Blend Two Lines"
input blendSrc1 = {"Line 1",Default "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close", "J Line 2"}; #"Blend Two Lines"
input blendsrc2 = {"Line 1", "Line 2", "Line 3",Default "Line 4", "Line 5", "Line 6","Close", "J Line 2"}; #"Blend Two Lines"


def na = Double.NaN;
DefineGlobalColor("up", CreateColor(0,213,213));
DefineGlobalColor("dn", CreateColor(255,82,82));
def h1 = LookbackWindow1;
def r1 = RelativeWeighting1;
def x1 = Level1;
def h2 = LookbackWindow2;
def r2 = RelativeWeighting2;
def x2 = Level2;
def h3 = LookbackWindow3;
def r3 = RelativeWeighting3;
def x3 = Level3;
def h4 = LookbackWindow4;
def r4 = RelativeWeighting4;
def x4 = Level4;
def h5 = LookbackWindow5;
def r5 = RelativeWeighting5;
def x5 = Level5;
def h6 = LookbackWindow6;
def r6 = RelativeWeighting6;
def x6 = Level6;

#j(float line1,float line2) =>
script j {
    input line1 = close;
    input line2 = close;
    def j = 3 * line1 - 2 * line2;
    plot out = j;
}
#rationalQuadratic(_src, _lookback, _relativeWeight, startAtBar,string _filter) =>
script rationalQuadratic {
    input _src = close;
    input _lookback = 8;
    input _relativeWeight = 1;
    input startAtBar = 25;
    input _filter = "No Filter";
    def _size = 2;
    def factor = Sqr(_lookback) * 2 * _relativeWeight;
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
        p + _src[i] * Power(1 + (Sqr(i) / factor), -_relativeWeight);
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
        p1 + Power(1 + (Sqr(i1) / factor), -_relativeWeight);
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
        q + yhatdelt[j] * Power(1 + (Sqr(j) / factor), -_relativeWeight);
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
#gaussian(_src, _lookback, startAtBar, _filter) =>
script gaussian {
    input _src = close;
    input _lookback = 16;
    input startAtBar = 25;
    input _filter = "Smooth";
    def _size = 2;
    def factor = 2 * Sqr(_lookback);
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
            p + _src[i] * (Exp(- Sqr(i) / factor));
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
            p1 + (Exp(- Sqr(i1) / factor));
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
            q + yhatdelt[j] * (Exp(- Sqr(j) / factor));
    def _cumulativeWeight2 = fold j1 = 0 to _size + startAtBar with q1 do
            q1 + (Exp(- Sqr(j1) / factor));
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight2;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
#periodic(_src, _lookback, _period, startAtBar, _filter) =>
script periodic {
    input _src = close;
    input _lookback = 8;
    input _period = 100;
    input startAtBar = 25;
    input _filter = "Zero Lag";
    def _size = 2;
    def pi = Double.Pi;
    def factor = Sqr(_lookback);
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
            p + _src[i] * (Exp(-2 * Sqr(Sin(pi * i / _period)) / factor));
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
            p1 + (Exp(-2 * Sqr(Sin(pi * i1 / _period)) / factor));
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
            q + yhatdelt[j] * (Exp(-2 * Sqr(Sin(pi * j / _period)) / factor));
    def _cumulativeWeight2 = fold j1 = 0 to _size + startAtBar with q1 do
            q1 + (Exp(-2 * Sqr(Sin(pi * j1 / _period)) / factor));
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight2;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
#locallyPeriodic(_src, _lookback, _period, startAtBar, _filter) =>
script locallyPeriodic {
    input _src = close;
    input _lookback = 8;
    input _period = 100;
    input startAtBar = 25;
    input _filter = "Zero Lag";
    def _size = 2;
    def pi = Double.Pi;
    def factor = Sqr(_lookback);
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
        p + GetValue(_src, i) * (Exp(-2 * Sqr(Sin(pi * i / _period)) /
                              factor) * Exp(-Sqr(i) / (2 * factor)));
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
        p1 + (Exp(-2 * Sqr(Sin(pi * i1 / _period)) / factor) * Exp(-Sqr(i1) / (2 * factor)));
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
        q + GetValue(yhatdelt, j) * (Exp(-2 * Sqr(Sin(pi * j / _period)) /
                             factor) * Exp(-Sqr(j) / (2 * factor)));
    def _cumulativeWeight2 = fold j1 = 0 to _size + startAtBar with q1 do
            q1 + (Exp(-2 * Sqr(Sin(pi * j1 / _period)) / factor) * Exp(-Sqr(j1) / (2 * factor)));
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight2;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
def rq1 = rationalQuadratic(src, h1, r1, x1, filterType1);
def ga1 = gaussian(src, h1 - lag1, x1, filterType1);
def pe1 = periodic(src, h1, lag1, x1, filterType1);
def lp1 = locallyPeriodic(src, h1, lag1, x1, filterType1);

def rq2 = rationalQuadratic(src, h2, r2, x2, filterType2);
def ga2 = gaussian(src, h2 - lag2, x2, filterType2);
def pe2 = periodic(src, h2, lag2, x2, filterType2);
def lp2 = locallyPeriodic(src, h2, lag2, x2, filterType2);

def rq3 = rationalQuadratic(src, h3, r3, x3, filterType3);
def ga3 = gaussian(src, h3 - lag3, x3, filterType3);
def pe3 = periodic(src, h3, lag3, x3, filterType3);
def lp3 = locallyPeriodic(src, h3, lag3, x3, filterType3);

def rq4 = rationalQuadratic(src, h4, r4, x4, filterType4);
def ga4 = gaussian(src, h4 - lag4, x4, filterType4);
def pe4 = periodic(src, h4, lag4, x4, filterType4);
def lp4 = locallyPeriodic(src, h4, lag4, x4, filterType4);

def rq5 = rationalQuadratic(src, h5, r5, x5, filterType5);
def ga5 = gaussian(src, h5 - lag5, x5, filterType5);
def pe5 = periodic(src, h5, lag5, x5, filterType5);
def lp5 = locallyPeriodic(src, h5, lag5, x5, filterType5);

def rq6 = rationalQuadratic(src, h6, r6, x6, filterType6);
def ga6 = gaussian(src, h6 - lag6, x6, filterType6);
def pe6 = periodic(src, h6, lag6, x6, filterType6);
def lp6 = locallyPeriodic(src, h6, lag6, x6, filterType6);

def regLine1;
Switch (RegressionType1) {
Case "Gaussian" :         regLine1 = ga1;
Case "Periodic" :         regLine1 = pe1;
Case "Locally Periodic" : regLine1 = lp1;
Default :                 regLine1 = rq1;
}
def regLine2;
Switch (RegressionType2) {
Case "Gaussian" :         regLine2 = ga2;
Case "Periodic" :         regLine2 = pe2;
Case "Locally Periodic" : regLine2 = lp2;
Default :                 regLine2 = rq2;
}
def regLine3;
Switch (RegressionType3) {
Case "Gaussian" :         regLine3 = ga3;
Case "Periodic" :         regLine3 = pe3;
Case "Locally Periodic" : regLine3 = lp3;
Default :                 regLine3 = rq3;
}
def regLine4;
Switch (RegressionType4) {
Case "Gaussian" :         regLine4 = ga4;
Case "Periodic" :         regLine4 = pe4;
Case "Locally Periodic" : regLine4 = lp4;
Default :                 regLine4 = rq4;
}
def regLine5;
Switch (RegressionType5) {
Case "Gaussian" :         regLine5 = ga5;
Case "Periodic" :         regLine5 = pe5;
Case "Locally Periodic" : regLine5 = lp5;
Default :                 regLine5 = rq5;
}
def regLine6;
Switch (RegressionType6) {
Case "Gaussian" :         regLine6 = ga6;
Case "Periodic" :         regLine6 = pe6;
Case "Locally Periodic" : regLine6 = lp6;
Default :                 regLine6 = rq6;
}
def line1 = regLine1;
def line2 = regLine2;
def line3 = regLine3;
def line4 = regLine4;
def line5 = regLine5;
def line6 = regLine6;

def jSrc1a;
Switch (jLine1Src1) {
Case "Line 1" : jSrc1a = line1;
Case "Line 3" : jSrc1a = line3;
Case "Line 4" : jSrc1a = line4;
Case "Line 5" : jSrc1a = line5;
Case "Line 6" : jSrc1a = line6;
Case "Close" : jSrc1a = close;
Default : jSrc1a = line2;
}
def jSrc1b;
Switch (jLine1Src2) {
Case "Line 1" : jSrc1b = line1;
Case "Line 2" : jSrc1b = line2;
Case "Line 3" : jSrc1b = line3;
Case "Line 5" : jSrc1b = line5;
Case "Line 6" : jSrc1b = line6;
Case "Close" : jSrc1b = close;
Default : jSrc1b = line4;
}
def jline1a = jSrc1a;
def jline1b = jSrc1b;
def jline1 = j(jline1a,jline1b);

def jSrc2a;
Switch (jline2src1) {
Case "Line 1" : jSrc2a = line1;
Case "Line 2" : jSrc2a = line2;
Case "Line 4" : jSrc2a = line4;
Case "Line 5" : jSrc2a = line5;
Case "Line 6" : jSrc2a = line6;
Case "Close" : jSrc2a = close;
Case "J Line 1" : jSrc2a = jline1;
Default : jSrc2a = line3;
}
def jSrc2b;
Switch (jline2src2) {
Case "Line 1" : jSrc2b = line1;
Case "Line 2" : jSrc2b = line2;
Case "Line 3" : jSrc2b = line3;
Case "Line 4" : jSrc2b = line4;
Case "Line 6" : jSrc2b = line6;
Case "Close" : jSrc2b = close;
Case "J Line 1" : jSrc2b = jline1;
Default : jSrc2b = line5;
}
def jline2a = jSrc2a;
def jline2b = jSrc2b;
def jline2 = j(jline2a,jline2b);

def jblnd1;
Switch (blendSrc1) {
Case "Line 1" : jblnd1 = line1;
Case "Line 3" : jblnd1 = line3;
Case "Line 4" : jblnd1 = line4;
Case "Line 5" : jblnd1 = line5;
Case "Line 6" : jblnd1 = line6;
Case "Close" : jblnd1 = close;
Case "J Line 2" : jblnd1 = jline2;
Default : jblnd1 = line2;
}
def jblnd2;
Switch (blendsrc2) {
Case "Line 1" : jblnd2 = line1;
Case "Line 2" : jblnd2 = line2;
Case "Line 3" : jblnd2 = line3;
Case "Line 5" : jblnd2 = line5;
Case "Line 6" : jblnd2 = line6;
Case "Close" : jblnd2 = close;
Case "J Line 2" : jblnd2 = jline2;
Default : jblnd2 = line4;
}
def blenda = jblnd1;
def blendb = jblnd2;

def weightcalc = BlendTwoLinesWeight / 100;
def blend = blenda * weightcalc + blendb * (1 - weightcalc);

plot Line11 = if !showRegLine1 then na else line1;        # "Line1"
plot Line22 = if !showRegLine2 then na else line2;        # "Line2"
plot Line33 = if !showRegLine3 then na else line3;        # "Line3"
plot Line44 = if !showRegLine4 then na else line4;        # "Line4"
plot Line55 = if !showRegLine5 then na else line5;        # "Line5"
plot Line66 = if !showRegLine6 then na else line6;        # "Line6"
plot j1 = if !ShowJLine1 then na else jline1;
plot j2 = if !ShowJline2 then na else jline2;
plot blendLine = if !useblend then na else blend;         # "Blend"

Line11.AssignValueColor(if line1 > line1[1] then GlobalColor("up") else GlobalColor("dn"));
Line22.AssignValueColor(if line2 > line2[1] then GlobalColor("up") else GlobalColor("dn"));
Line33.AssignValueColor(if line3 > line3[1] then GlobalColor("up") else GlobalColor("dn"));
Line44.SetDefaultColor(Color.GRAY);
Line55.SetDefaultColor(Color.GRAY);
Line66.SetDefaultColor(Color.GRAY);
j1.SetDefaultColor(Color.WHITE);
j2.SetDefaultColor(Color.WHITE);


#-- END of CODE
 
check the below

CSS:
# https://www.tradingview.com/v/ARPbTa5N/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © jdehorty © veryfid
#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
# Converted by Sam4Cok@Samer800    - 11/2023
input src = close;
input showRegLine1 = yes;
input RegressionType1 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType1 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow1 = 3;         # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting1 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level1 = 2;                  # 'Bar index on which to start regression. Recommended range: 2-25'
input lag1 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine2 = yes;
input RegressionType2 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType2 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow2 = 8;         # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting2 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level2 = 20;                 # 'Bar index on which to start regression. Recommended range: 2-25'
input lag2 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine3 = yes;
input RegressionType3 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType3 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow3 = 200;       # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting3 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level3 = 500;                # 'Bar index on which to start regression. Recommended range: 2-25'
input lag3 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine4 = yes;
input RegressionType4 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType4 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow4 = 30;        # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting4 = 8.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level4 = 35;                 # 'Bar index on which to start regression. Recommended range: 2-25'
input lag4 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine5 = yes;
input RegressionType5 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType5 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow5 = 69;        # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting5 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level5 = 500;                # 'Bar index on which to start regression. Recommended range: 2-25'
input lag5 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input showRegLine6 = yes;
input RegressionType6 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType6 = {"No Filter", "Smooth",default "Zero Lag"};
input LookbackWindow6 = 200;       # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting6 = 1.0;    # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level6 = 500;                # 'Bar index on which to start regression. Recommended range: 2-25'
input lag6 = 2;                    # 'Lower values result in earlier crossovers. Recommended range: 1-2"
input ShowJLine1 = no;    # "Show J line 1"
input jLine1Src1 = {"Line 1",Default "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close"};
input jLine1Src2 = {"Line 1", "Line 2", "Line 3",Default "Line 4", "Line 5", "Line 6","Close"};
input ShowJLine2 = no;    # "Show J line 2"
input jline2src1 = {"Line 1", "Line 2",Default "Line 3", "Line 4", "Line 5", "Line 6","Close", "J Line 1"};
input jline2src2 = {"Line 1", "Line 2", "Line 3", "Line 4",Default "Line 5", "Line 6","Close", "J Line 1"};
input useBlend = no;      # "Blend Two Lines"
input BlendTwoLinesWeight = 50;    # "Blend Two Lines"
input blendSrc1 = {"Line 1",Default "Line 2", "Line 3", "Line 4", "Line 5", "Line 6","Close", "J Line 2"}; #"Blend Two Lines"
input blendsrc2 = {"Line 1", "Line 2", "Line 3",Default "Line 4", "Line 5", "Line 6","Close", "J Line 2"}; #"Blend Two Lines"


def na = Double.NaN;
DefineGlobalColor("up", CreateColor(0,213,213));
DefineGlobalColor("dn", CreateColor(255,82,82));
def h1 = LookbackWindow1;
def r1 = RelativeWeighting1;
def x1 = Level1;
def h2 = LookbackWindow2;
def r2 = RelativeWeighting2;
def x2 = Level2;
def h3 = LookbackWindow3;
def r3 = RelativeWeighting3;
def x3 = Level3;
def h4 = LookbackWindow4;
def r4 = RelativeWeighting4;
def x4 = Level4;
def h5 = LookbackWindow5;
def r5 = RelativeWeighting5;
def x5 = Level5;
def h6 = LookbackWindow6;
def r6 = RelativeWeighting6;
def x6 = Level6;

#j(float line1,float line2) =>
script j {
    input line1 = close;
    input line2 = close;
    def j = 3 * line1 - 2 * line2;
    plot out = j;
}
#rationalQuadratic(_src, _lookback, _relativeWeight, startAtBar,string _filter) =>
script rationalQuadratic {
    input _src = close;
    input _lookback = 8;
    input _relativeWeight = 1;
    input startAtBar = 25;
    input _filter = "No Filter";
    def _size = 2;
    def factor = Sqr(_lookback) * 2 * _relativeWeight;
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
        p + _src[i] * Power(1 + (Sqr(i) / factor), -_relativeWeight);
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
        p1 + Power(1 + (Sqr(i1) / factor), -_relativeWeight);
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
        q + yhatdelt[j] * Power(1 + (Sqr(j) / factor), -_relativeWeight);
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
#gaussian(_src, _lookback, startAtBar, _filter) =>
script gaussian {
    input _src = close;
    input _lookback = 16;
    input startAtBar = 25;
    input _filter = "Smooth";
    def _size = 2;
    def factor = 2 * Sqr(_lookback);
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
            p + _src[i] * (Exp(- Sqr(i) / factor));
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
            p1 + (Exp(- Sqr(i1) / factor));
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
            q + yhatdelt[j] * (Exp(- Sqr(j) / factor));
    def _cumulativeWeight2 = fold j1 = 0 to _size + startAtBar with q1 do
            q1 + (Exp(- Sqr(j1) / factor));
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight2;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
#periodic(_src, _lookback, _period, startAtBar, _filter) =>
script periodic {
    input _src = close;
    input _lookback = 8;
    input _period = 100;
    input startAtBar = 25;
    input _filter = "Zero Lag";
    def _size = 2;
    def pi = Double.Pi;
    def factor = Sqr(_lookback);
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
            p + _src[i] * (Exp(-2 * Sqr(Sin(pi * i / _period)) / factor));
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
            p1 + (Exp(-2 * Sqr(Sin(pi * i1 / _period)) / factor));
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
            q + yhatdelt[j] * (Exp(-2 * Sqr(Sin(pi * j / _period)) / factor));
    def _cumulativeWeight2 = fold j1 = 0 to _size + startAtBar with q1 do
            q1 + (Exp(-2 * Sqr(Sin(pi * j1 / _period)) / factor));
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight2;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
#locallyPeriodic(_src, _lookback, _period, startAtBar, _filter) =>
script locallyPeriodic {
    input _src = close;
    input _lookback = 8;
    input _period = 100;
    input startAtBar = 25;
    input _filter = "Zero Lag";
    def _size = 2;
    def pi = Double.Pi;
    def factor = Sqr(_lookback);
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
        p + GetValue(_src, i) * (Exp(-2 * Sqr(Sin(pi * i / _period)) /
                              factor) * Exp(-Sqr(i) / (2 * factor)));
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
        p1 + (Exp(-2 * Sqr(Sin(pi * i1 / _period)) / factor) * Exp(-Sqr(i1) / (2 * factor)));
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
        q + GetValue(yhatdelt, j) * (Exp(-2 * Sqr(Sin(pi * j / _period)) /
                             factor) * Exp(-Sqr(j) / (2 * factor)));
    def _cumulativeWeight2 = fold j1 = 0 to _size + startAtBar with q1 do
            q1 + (Exp(-2 * Sqr(Sin(pi * j1 / _period)) / factor) * Exp(-Sqr(j1) / (2 * factor)));
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight2;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
def rq1 = rationalQuadratic(src, h1, r1, x1, filterType1);
def ga1 = gaussian(src, h1 - lag1, x1, filterType1);
def pe1 = periodic(src, h1, lag1, x1, filterType1);
def lp1 = locallyPeriodic(src, h1, lag1, x1, filterType1);

def rq2 = rationalQuadratic(src, h2, r2, x2, filterType2);
def ga2 = gaussian(src, h2 - lag2, x2, filterType2);
def pe2 = periodic(src, h2, lag2, x2, filterType2);
def lp2 = locallyPeriodic(src, h2, lag2, x2, filterType2);

def rq3 = rationalQuadratic(src, h3, r3, x3, filterType3);
def ga3 = gaussian(src, h3 - lag3, x3, filterType3);
def pe3 = periodic(src, h3, lag3, x3, filterType3);
def lp3 = locallyPeriodic(src, h3, lag3, x3, filterType3);

def rq4 = rationalQuadratic(src, h4, r4, x4, filterType4);
def ga4 = gaussian(src, h4 - lag4, x4, filterType4);
def pe4 = periodic(src, h4, lag4, x4, filterType4);
def lp4 = locallyPeriodic(src, h4, lag4, x4, filterType4);

def rq5 = rationalQuadratic(src, h5, r5, x5, filterType5);
def ga5 = gaussian(src, h5 - lag5, x5, filterType5);
def pe5 = periodic(src, h5, lag5, x5, filterType5);
def lp5 = locallyPeriodic(src, h5, lag5, x5, filterType5);

def rq6 = rationalQuadratic(src, h6, r6, x6, filterType6);
def ga6 = gaussian(src, h6 - lag6, x6, filterType6);
def pe6 = periodic(src, h6, lag6, x6, filterType6);
def lp6 = locallyPeriodic(src, h6, lag6, x6, filterType6);

def regLine1;
Switch (RegressionType1) {
Case "Gaussian" :         regLine1 = ga1;
Case "Periodic" :         regLine1 = pe1;
Case "Locally Periodic" : regLine1 = lp1;
Default :                 regLine1 = rq1;
}
def regLine2;
Switch (RegressionType2) {
Case "Gaussian" :         regLine2 = ga2;
Case "Periodic" :         regLine2 = pe2;
Case "Locally Periodic" : regLine2 = lp2;
Default :                 regLine2 = rq2;
}
def regLine3;
Switch (RegressionType3) {
Case "Gaussian" :         regLine3 = ga3;
Case "Periodic" :         regLine3 = pe3;
Case "Locally Periodic" : regLine3 = lp3;
Default :                 regLine3 = rq3;
}
def regLine4;
Switch (RegressionType4) {
Case "Gaussian" :         regLine4 = ga4;
Case "Periodic" :         regLine4 = pe4;
Case "Locally Periodic" : regLine4 = lp4;
Default :                 regLine4 = rq4;
}
def regLine5;
Switch (RegressionType5) {
Case "Gaussian" :         regLine5 = ga5;
Case "Periodic" :         regLine5 = pe5;
Case "Locally Periodic" : regLine5 = lp5;
Default :                 regLine5 = rq5;
}
def regLine6;
Switch (RegressionType6) {
Case "Gaussian" :         regLine6 = ga6;
Case "Periodic" :         regLine6 = pe6;
Case "Locally Periodic" : regLine6 = lp6;
Default :                 regLine6 = rq6;
}
def line1 = regLine1;
def line2 = regLine2;
def line3 = regLine3;
def line4 = regLine4;
def line5 = regLine5;
def line6 = regLine6;

def jSrc1a;
Switch (jLine1Src1) {
Case "Line 1" : jSrc1a = line1;
Case "Line 3" : jSrc1a = line3;
Case "Line 4" : jSrc1a = line4;
Case "Line 5" : jSrc1a = line5;
Case "Line 6" : jSrc1a = line6;
Case "Close" : jSrc1a = close;
Default : jSrc1a = line2;
}
def jSrc1b;
Switch (jLine1Src2) {
Case "Line 1" : jSrc1b = line1;
Case "Line 2" : jSrc1b = line2;
Case "Line 3" : jSrc1b = line3;
Case "Line 5" : jSrc1b = line5;
Case "Line 6" : jSrc1b = line6;
Case "Close" : jSrc1b = close;
Default : jSrc1b = line4;
}
def jline1a = jSrc1a;
def jline1b = jSrc1b;
def jline1 = j(jline1a,jline1b);

def jSrc2a;
Switch (jline2src1) {
Case "Line 1" : jSrc2a = line1;
Case "Line 2" : jSrc2a = line2;
Case "Line 4" : jSrc2a = line4;
Case "Line 5" : jSrc2a = line5;
Case "Line 6" : jSrc2a = line6;
Case "Close" : jSrc2a = close;
Case "J Line 1" : jSrc2a = jline1;
Default : jSrc2a = line3;
}
def jSrc2b;
Switch (jline2src2) {
Case "Line 1" : jSrc2b = line1;
Case "Line 2" : jSrc2b = line2;
Case "Line 3" : jSrc2b = line3;
Case "Line 4" : jSrc2b = line4;
Case "Line 6" : jSrc2b = line6;
Case "Close" : jSrc2b = close;
Case "J Line 1" : jSrc2b = jline1;
Default : jSrc2b = line5;
}
def jline2a = jSrc2a;
def jline2b = jSrc2b;
def jline2 = j(jline2a,jline2b);

def jblnd1;
Switch (blendSrc1) {
Case "Line 1" : jblnd1 = line1;
Case "Line 3" : jblnd1 = line3;
Case "Line 4" : jblnd1 = line4;
Case "Line 5" : jblnd1 = line5;
Case "Line 6" : jblnd1 = line6;
Case "Close" : jblnd1 = close;
Case "J Line 2" : jblnd1 = jline2;
Default : jblnd1 = line2;
}
def jblnd2;
Switch (blendsrc2) {
Case "Line 1" : jblnd2 = line1;
Case "Line 2" : jblnd2 = line2;
Case "Line 3" : jblnd2 = line3;
Case "Line 5" : jblnd2 = line5;
Case "Line 6" : jblnd2 = line6;
Case "Close" : jblnd2 = close;
Case "J Line 2" : jblnd2 = jline2;
Default : jblnd2 = line4;
}
def blenda = jblnd1;
def blendb = jblnd2;

def weightcalc = BlendTwoLinesWeight / 100;
def blend = blenda * weightcalc + blendb * (1 - weightcalc);

plot Line11 = if !showRegLine1 then na else line1;        # "Line1"
plot Line22 = if !showRegLine2 then na else line2;        # "Line2"
plot Line33 = if !showRegLine3 then na else line3;        # "Line3"
plot Line44 = if !showRegLine4 then na else line4;        # "Line4"
plot Line55 = if !showRegLine5 then na else line5;        # "Line5"
plot Line66 = if !showRegLine6 then na else line6;        # "Line6"
plot j1 = if !ShowJLine1 then na else jline1;
plot j2 = if !ShowJline2 then na else jline2;
plot blendLine = if !useblend then na else blend;         # "Blend"

Line11.AssignValueColor(if line1 > line1[1] then GlobalColor("up") else GlobalColor("dn"));
Line22.AssignValueColor(if line2 > line2[1] then GlobalColor("up") else GlobalColor("dn"));
Line33.AssignValueColor(if line3 > line3[1] then GlobalColor("up") else GlobalColor("dn"));
Line44.SetDefaultColor(Color.GRAY);
Line55.SetDefaultColor(Color.GRAY);
Line66.SetDefaultColor(Color.GRAY);
j1.SetDefaultColor(Color.WHITE);
j2.SetDefaultColor(Color.WHITE);


#-- END of CODE
@samer800 , Fantastic work !! It will take some time but I will check it.

out of curiosity, what did you do with the array stuff?

not in the direct script, but there is in the imported library functions it uses, unless I am confused here, which would not be a surprise to anybody, specially my wife.

1701117659535.png
 
Last edited by a moderator:
not in the direct script, but there is in the imported library functions it uses, unless I am confused here, which would not be a surprise to anybody, specially my wife.
(@apdusp sorry for my deaf, dumb, and blind moment; too much turkey tryptophan 🤪 )

The code that you highlighted didn't need the use of an array.
The incomparable @samer800 ingeniously determined that all that was required was a custom script routine.

In ThinkScript, the script{} routine can be used to create custom functions.
script{} routines cannot replace real arrays as it still can only manipulate one value at a time, not an array of values.
Ruby:
#rationalQuadratic(_src, _lookback, _relativeWeight, startAtBar,string _filter) =>
script rationalQuadratic {
    input _src = close;
    input _lookback = 8;
    input _relativeWeight = 1;
    input startAtBar = 25;
    input _filter = "No Filter";
    def _size = 2;
    def factor = Sqr(_lookback) * 2 * _relativeWeight;
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
        p + _src[i] * Power(1 + (Sqr(i) / factor), -_relativeWeight);
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
        p1 + Power(1 + (Sqr(i1) / factor), -_relativeWeight);
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
        q + yhatdelt[j] * Power(1 + (Sqr(j) / factor), -_relativeWeight);
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
 
Last edited:
(@apdusp sorry for my deaf, dumb, and blind moment; too much turkey tryptophan 🤪 )

The code that you highlighted didn't need the use of an array.
The incomparable @samer800 ingeniously determined that all that was required was a custom script routine.

In ThinkScript, the script{} routine can be used to create custom functions.
script{} routines cannot replace real arrays as it still can only manipulate one value at a time, not an array of values.
Ruby:
#rationalQuadratic(_src, _lookback, _relativeWeight, startAtBar,string _filter) =>
script rationalQuadratic {
    input _src = close;
    input _lookback = 8;
    input _relativeWeight = 1;
    input startAtBar = 25;
    input _filter = "No Filter";
    def _size = 2;
    def factor = Sqr(_lookback) * 2 * _relativeWeight;
    def _currentWeight = fold i = 0 to _size + startAtBar with p do
        p + _src[i] * Power(1 + (Sqr(i) / factor), -_relativeWeight);
    def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
        p1 + Power(1 + (Sqr(i1) / factor), -_relativeWeight);
    def yhatdelt = _currentWeight / _cumulativeWeight;
    def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
        q + yhatdelt[j] * Power(1 + (Sqr(j) / factor), -_relativeWeight);
    def yhatdelt2 = _currentWeight2 / _cumulativeWeight;
    def yhat = yhatdelt2;
    def zlagcalc = 2 * yhatdelt - yhat;
    def lineout = if _filter == "No Filter" then yhatdelt else
                  if _filter == "Smooth" then yhat else
                  if _filter == "Zero Lag" then zlagcalc else yhatdelt;
    plot out = lineout;
}
@MerryDay , you just pointed to the right place in a very simple way. thank you to clear my confusion.

As I mentioned initially, it looks to me that single kregs can be played as we would with more conventional moving averages, like crossings, parallels, etc
 
just as a further spinoff of @samer800 code, since I am trying to focus on a single regression I constructed this version that includes trend coloring and scanning ability

##############################################################

# https://www.tradingview.com/v/ARPbTa5N/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © jdehorty © veryfid
#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/...son-Rational-Quadratic-Kernel-Non-Repainting/
#// Nadaraya-Watson Kernel Regression Settings
# Converted by Sam4Cok@Samer800 - 11/2023
# this version displays just one regression as it can be trated as a single moving average with trend coloring and includes simple scanning posssiblity as TOS does not consider it TOO COMPLEX

input scan = no;
input src = close;

input showRegLine1 = yes;
input RegressionType1 = {default "Rational Quadratic", "Gaussian", "Periodic", "Locally Periodic"};
input filterType1 = {"No Filter", default "Smooth", "Zero Lag"};
input LookbackWindow1 = 3; # 'Lookback Window', Recommended range: 3-50
input RelativeWeighting1 = 1.0; # 'Relative weighting of time frames. Recommended range: 0.25-25'
input Level1 = 2; # 'Bar index on which to start regression. Recommended range: 2-25'
input lag1 = 2; # 'Lower values result in earlier crossovers. Recommended range: 1-2"


def na = Double.NaN;
DefineGlobalColor("up", CreateColor(0,213,213));
DefineGlobalColor("dn", CreateColor(255,82,82));
def h1 = LookbackWindow1;
def r1 = RelativeWeighting1;
def x1 = Level1;


#rationalQuadratic(_src, _lookback, _relativeWeight, startAtBar,string _filter) =>
script rationalQuadratic {
input _src = close;
input _lookback = 8;
input _relativeWeight = 1;
input startAtBar = 25;
input _filter = "No Filter";
def _size = 2;
def factor = Sqr(_lookback) * 2 * _relativeWeight;
def _currentWeight = fold i = 0 to _size + startAtBar with p do
p + _src * Power(1 + (Sqr(i) / factor), -_relativeWeight);
def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
p1 + Power(1 + (Sqr(i1) / factor), -_relativeWeight);
def yhatdelt = _currentWeight / _cumulativeWeight;
def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
q + yhatdelt[j] * Power(1 + (Sqr(j) / factor), -_relativeWeight);
def yhatdelt2 = _currentWeight2 / _cumulativeWeight;
def yhat = yhatdelt2;
def zlagcalc = 2 * yhatdelt - yhat;
def lineout = if _filter == "No Filter" then yhatdelt else
if _filter == "Smooth" then yhat else
if _filter == "Zero Lag" then zlagcalc else yhatdelt;
plot out = lineout;
}


#gaussian(_src, _lookback, startAtBar, _filter) =>
script gaussian {
input _src = close;
input _lookback = 16;
input startAtBar = 25;
input _filter = "Smooth";
def _size = 2;
def factor = 2 * Sqr(_lookback);
def _currentWeight = fold i = 0 to _size + startAtBar with p do
p + _src * (Exp(- Sqr(i) / factor));
def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
p1 + (Exp(- Sqr(i1) / factor));
def yhatdelt = _currentWeight / _cumulativeWeight;
def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
q + yhatdelt[j] * (Exp(- Sqr(j) / factor));
def _cumulativeWeight2 = fold j1 = 0 to _size + startAtBar with q1 do
q1 + (Exp(- Sqr(j1) / factor));
def yhatdelt2 = _currentWeight2 / _cumulativeWeight2;
def yhat = yhatdelt2;
def zlagcalc = 2 * yhatdelt - yhat;
def lineout = if _filter == "No Filter" then yhatdelt else
if _filter == "Smooth" then yhat else
if _filter == "Zero Lag" then zlagcalc else yhatdelt;
plot out = lineout;
}

#periodic(_src, _lookback, _period, startAtBar, _filter) =>
script periodic {
input _src = close;
input _lookback = 8;
input _period = 100;
input startAtBar = 25;
input _filter = "Zero Lag";
def _size = 2;
def pi = Double.Pi;
def factor = Sqr(_lookback);
def _currentWeight = fold i = 0 to _size + startAtBar with p do
p + _src * (Exp(-2 * Sqr(Sin(pi * i / _period)) / factor));
def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
p1 + (Exp(-2 * Sqr(Sin(pi * i1 / _period)) / factor));
def yhatdelt = _currentWeight / _cumulativeWeight;
def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
q + yhatdelt[j] * (Exp(-2 * Sqr(Sin(pi * j / _period)) / factor));
def _cumulativeWeight2 = fold j1 = 0 to _size + startAtBar with q1 do
q1 + (Exp(-2 * Sqr(Sin(pi * j1 / _period)) / factor));
def yhatdelt2 = _currentWeight2 / _cumulativeWeight2;
def yhat = yhatdelt2;
def zlagcalc = 2 * yhatdelt - yhat;
def lineout = if _filter == "No Filter" then yhatdelt else
if _filter == "Smooth" then yhat else
if _filter == "Zero Lag" then zlagcalc else yhatdelt;
plot out = lineout;
}

#locallyPeriodic(_src, _lookback, _period, startAtBar, _filter) =>
script locallyPeriodic {
input _src = close;
input _lookback = 8;
input _period = 100;
input startAtBar = 25;
input _filter = "Zero Lag";
def _size = 2;
def pi = Double.Pi;
def factor = Sqr(_lookback);
def _currentWeight = fold i = 0 to _size + startAtBar with p do
p + GetValue(_src, i) * (Exp(-2 * Sqr(Sin(pi * i / _period)) /
factor) * Exp(-Sqr(i) / (2 * factor)));
def _cumulativeWeight = fold i1 = 0 to _size + startAtBar with p1 do
p1 + (Exp(-2 * Sqr(Sin(pi * i1 / _period)) / factor) * Exp(-Sqr(i1) / (2 * factor)));
def yhatdelt = _currentWeight / _cumulativeWeight;
def _currentWeight2 = fold j = 0 to _size + startAtBar with q do
q + GetValue(yhatdelt, j) * (Exp(-2 * Sqr(Sin(pi * j / _period)) /
factor) * Exp(-Sqr(j) / (2 * factor)));
def _cumulativeWeight2 = fold j1 = 0 to _size + startAtBar with q1 do
q1 + (Exp(-2 * Sqr(Sin(pi * j1 / _period)) / factor) * Exp(-Sqr(j1) / (2 * factor)));
def yhatdelt2 = _currentWeight2 / _cumulativeWeight2;
def yhat = yhatdelt2;
def zlagcalc = 2 * yhatdelt - yhat;
def lineout = if _filter == "No Filter" then yhatdelt else
if _filter == "Smooth" then yhat else
if _filter == "Zero Lag" then zlagcalc else yhatdelt;
plot out = lineout;
}


def rq1 = rationalQuadratic(src, h1, r1, x1, filterType1);
def ga1 = gaussian(src, h1 - lag1, x1, filterType1);
def pe1 = periodic(src, h1, lag1, x1, filterType1);
def lp1 = locallyPeriodic(src, h1, lag1, x1, filterType1);


def regLine1;
Switch (RegressionType1) {
Case "Gaussian" : regLine1 = ga1;
Case "Periodic" : regLine1 = pe1;
Case "Locally Periodic" : regLine1 = lp1;
Default : regLine1 = rq1;
}


def line1 = regLine1;


plot Line11 = if !showRegLine1 then na else line1; # "Line1"

Line11.AssignValueColor(if line1 > line1[1] then GlobalColor("up") else GlobalColor("dn"));

#-- END of CODE

# =================== COLOR TREND IN PRICE GRAPH====================================

input back_color = yes;
def up = line1 > line1[1] ;

def pos = if back_color then Double.POSITIVE_INFINITY else Double.NaN;
def neg = if back_color then Double.NEGATIVE_INFINITY else Double.NaN;

DefineGlobalColor("dgreen" , CreateColor(0,59,0));
DefineGlobalColor("dred" , CreateColor(79,0,0));
AddCloud(if up then pos else neg, if up then neg else pos, GlobalColor("dgreen"), GlobalColor("dred"));


# ====================== S C A N ====================================================

plot bullish = if up and scan then 1 else if scan then -1 else Double.NaN;
plot new_trend = if up and !(up[1]) and scan then 1 else if !up and (up[1]) and scan then -1 else Double.NaN;
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
267 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top