#https://www.tradingview.com/v/6VgJlewW/
#// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla
#// © QuantraAI
#indicator("Triple Confirmation Kernel Regression Oscillator [QuantraAI]", [QuantraAI]"
# Cconverted and mod to upper study by Sam4Cok@Samer800 - 12/2023
#// Kernel Regression Settings
input BarColoring = yes; # "Enable Bar Coloring"
input showSignals = yes; # "Enable Overbought/Oversold Shading"
input source = close; # "Source"
input bandwidth = 45; # "Bandwidth"
input RelativeWeighting = 2.0; # 'Relative weighting of time frames. Recommended range: 0.25-25'
input stdevLength = 150;
input sdevMulti = 3.0; # "Standard Deviation Extreme for OB/OS Border"
input stdevMultiplier = 2.0; #, "Standard Deviation Extreme for OB/OS Border",
def na = Double.NaN;
def last = isNaN(close);
DefineGlobalColor("up", CreateColor(95, 250, 224));
DefineGlobalColor("dn", CreateColor(194, 46, 208));
DefineGlobalColor("dup",CreateColor(3, 106, 89));
DefineGlobalColor("ddn", CreateColor(89, 21, 96));
#// Functions //
#epanechnikov(source, bandwidth) =>
Script epanechnikov {
input source = close;
input bandwidth = 45;
input width = 1;
def sum = fold i = 0 to bandwidth with p do
p + source[i] * (if AbsValue((Sqr(i) / Sqr(bandwidth))/width) <= 1 then
(3/4) * (1 - Sqr((Sqr(i) / Sqr(bandwidth))/width)) else 0);
def sumw = fold j = 0 to bandwidth with q do
q + (if AbsValue((Sqr(j) / Sqr(bandwidth))/width) <= 1 then
(3/4) * (1 - Sqr((Sqr(j) / Sqr(bandwidth))/width)) else 0);
def kernel_regression = (sum / sumw);
plot out = kernel_regression;
}
#logistic(source, bandwidth) =>
Script logistic {
input source = close;
input bandwidth = 45;
input width = 1;
def sum = fold i = 0 to bandwidth with p do
p + source[i] * (1 / (Exp((Sqr(i) / Sqr(bandwidth)) / width) + 2
+ Exp(-(Sqr(i) / Sqr(bandwidth)) / width)));
def sumw = fold j = 0 to bandwidth with q do
q + (1 / (Exp((Sqr(j) / Sqr(bandwidth)) / width) + 2
+ Exp(-(Sqr(j) / Sqr(bandwidth)) / width)));
def kernel_regression = (sum / sumw);
plot out = kernel_regression;
}
#wave(source, bandwidth) =>
Script wave {
input source = close;
input bandwidth = 45;
input width = 1;
def pi = Double.Pi;
def sum = fold i = 0 to bandwidth with p do
p + source[i] * (if (AbsValue((Sqr(i) / Sqr(bandwidth))/width) <= 1) then
(1 - AbsValue((Sqr(i) / Sqr(bandwidth))/width)) * Cos(pi * (Sqr(i) / Sqr(bandwidth)) / width) else 0);
def sumw = fold j = 0 to bandwidth with q do
q + (if (AbsValue((Sqr(j) / Sqr(bandwidth))/width) <= 1) then
(1 - AbsValue((Sqr(j) / Sqr(bandwidth))/width)) * Cos(pi * (Sqr(j) / Sqr(bandwidth)) / width) else 0);
def kernel_regression = (sum / sumw);
plot out = kernel_regression;
}
Script waveCalculation {
input source = close;
input bandwidth = 45;
input width = 2;
def pi = Double.Pi;
def sum = fold i = 0 to bandwidth with p do
p + source[i] * (if ((i * i) / (bandwidth * bandwidth) / width) <= 1 then
(1 - ((i * i) / (bandwidth * bandwidth) / width)) * cos(pi * ((i * i) / (bandwidth * bandwidth) / width)) else 0.0);
def sumw = fold j = 0 to bandwidth with q do
q + (if ((j * j) / (bandwidth * bandwidth) / width) <= 1 then
(1 - ((j * j) / (bandwidth * bandwidth) / width)) * cos(pi * ((j * j) / (bandwidth * bandwidth) / width)) else 0.0);
def kernel_regression = sum / sumw;
plot out = kernel_regression;
}
#// Triple Confirmations
def Wav = waveCalculation(source, bandwidth, RelativeWeighting);
def Ep = epanechnikov(source, bandwidth, RelativeWeighting);
def Lo = logistic(source, bandwidth, RelativeWeighting);
def Wa = wave(source, bandwidth, RelativeWeighting);
def up = Wav>Wav[1]; # 'Wave'
def dn = wav<wav[1];
#// Average
def AV = Average((Ep + Lo + Wa) / 3, 1);
def Mid = Average(AV, stdevLength);
#// Base Plots
plot pAV = Wav;
def Epanechnikov = Ep; # 'Epanechnikov'
def Logistic = Lo; # 'Logistic'
pAV.SetLineWeight(2);
pAV.AssignValueColor(if up then GlobalColor("up") else if dn then GlobalColor("dn") else Color.GRAY);
#// Calculate Dynamic OB/OS Zones
def basis = Average(AV, stdevLength);
def dev = stdev(AV, stdevLength);
def u1 = basis + sdevMulti/2 * dev;
def l1 = basis - sdevMulti/2 * dev;
def u2 = basis + sdevMulti * dev;
def l2 = basis - sdevMulti * dev;
##// Final Plots + Fill
plot avg = basis;
plot pu1 = u1;
plot pl1 = l1;
def pu2 = u2;
def pl2 = l2;
avg.SetDefaultColor(Color.GRAY);
pu1.SetDefaultColor(GlobalColor("dn"));
pl1.SetDefaultColor(GlobalColor("up"));
AssignPriceColor(if !BarColoring then Color.CURRENT else
if up and wav > av then Color.GREEN else #GlobalColor("up") else
if up then Color.DARK_GREEN else
if dn and wav < av then GlobalColor("dn") else
if dn then Color.PLUM else Color.GRAY);
AddCloud(if source > pAV then pAV - (pAV - source)/2 else na,pAV , GlobalColor("dup"));
AddCloud(if source > pAV then na else pAV, pAV + (source - pAV)/2, GlobalColor("ddn"));
AddCloud(pu2, pu1, GlobalColor("ddn"));
AddCloud(pl1, pl2, GlobalColor("dup"));
def sigUp = av > (l1 - (l1-l2)/2);# and av > av[1];
def sigDn = av < (u1 + (u2-u1)/2);# and av < av[1];
def sig = if showSignals then (if sigUp and !sigUp[1] then 1 else
if sigDn and !sigDn[1] then -1 else 0) else na;
# and sig[1]<0;
#def arrUp = Wac > Wac[1];# and !(Wac[1] > Wac[2]);
#def arrDn = Wac < Wac[1];# and !(Wac[1] < Wac[2]);
AddChartBubble(sig>0, low, "B", Color.GREEN, no);
AddChartBubble(sig<0, high, "S", Color.RED);
#-- END of CODE