#https://www.tradingview.com/script/WpVY7GKW-Gaussian-Channel-DW/
#//@version=4
#study(title="Gaussian Channel [DW]", shorttitle="GC [DW]", overlay=true)
# Request from https://usethinkscript.com/ memeber
# Converted and mod by Sam4Cok@Samer800 - 09/2022
#//------------------------------------------------------
#//Inputs
#//------------------------------------------------------
input BarColor = no;
input ShowChannel = no;
input ShowCloud = yes;
#//Source
input src = hlc3;
#//Poles
input Poles = 4;
#//Period
input SmaplePeriod = 144; # "Sampling Period"
#//True Range Multiplier
input mult = 1.414; # "Filtered True Range Multiplier"
#//Lag Reduction
input ReducedLagMode = no; # "Reduced Lag Mode"
input FastResponseMode = no; # "Fast Response Mode"
def na = Double.NaN;
########### Color ################
DefineGlobalColor("Exup" , CreateColor(10, 255, 104));
DefineGlobalColor("Weakup" , CreateColor(10, 255, 27));
DefineGlobalColor("up" , CreateColor(0, 117, 45));
DefineGlobalColor("Exdn" , CreateColor(255, 10, 90));
DefineGlobalColor("weakdn" , CreateColor(255, 10, 17));
DefineGlobalColor("dn" , CreateColor(153, 0, 50)); #
#//Filter function
#f_filt9x (_a, _s, _i) =>
script f_filt9x {
input _a = close;
input _s = close;
input _i = close;
def na = Double.NaN;
def _x = (1 - _a);
def _m2;
def _m3;
def _m4;
def _m5;
def _m6;
def _m7;
def _m8;
def _m9;
# // Initial weight _m1 is a pole number and equal to _i
_m2 = if IsNaN(_m2[1]) then 0 else
if _i == 9 then 36 else if _i == 8 then 28 else if _i == 7 then 21 else
if _i == 6 then 15 else if _i == 5 then 10 else if _i == 4 then 6 else
if _i == 3 then 3 else if _i == 2 then 1 else 0;
_m3 = if IsNaN(_m3[1]) then 0 else
if _i == 9 then 84 else if _i == 8 then 56 else if _i == 7 then 35 else
if _i == 6 then 20 else if _i == 5 then 10 else if _i == 4 then 4 else
if _i == 3 then 1 else 0;
_m4 = if IsNaN(_m4[1]) then 0 else
if _i == 9 then 126 else if _i == 8 then 70 else if _i == 7 then 35 else
if _i == 6 then 15 else if _i == 5 then 5 else if _i == 4 then 1 else 0;
_m5 = if IsNaN(_m5[1]) then 0 else
if _i == 9 then 126 else if _i == 8 then 56 else if _i == 7 then 21 else
if _i == 6 then 6 else if _i == 5 then 1 else 0;
_m6 = if IsNaN(_m6[1]) then 0 else
if _i == 9 then 84 else if _i == 8 then 28 else if _i == 7 then 7 else
if _i == 6 then 1 else 0;
_m7 = if IsNaN(_m7[1]) then 0 else
if _i == 9 then 36 else if _i == 8 then 8 else if _i == 7 then 1 else 0;
_m8 = if IsNaN(_m8[1]) then 0 else
if _i == 9 then 9 else if _i == 8 then 1 else 0;
_m9 = if IsNaN(_m9[1]) then 0 else if _i == 9 then 1 else 0;
# // filter
def _f = if IsNaN(_f[10*2]) then na else
Power(_a, _i) * (_s) + _i * _x * (_f[1]) - (if _i >= 2 then
_m2 * Power(_x, 2) * (_f[2]) else 0) + (if _i >= 3 then
_m3 * Power(_x, 3) * (_f[3]) else 0) - (if _i >= 4 then
_m4 * Power(_x, 4) * (_f[4]) else 0) + (if _i >= 5 then
_m5 * Power(_x, 5) * (_f[5]) else 0) - (if _i >= 6 then
_m6 * Power(_x, 6) * (_f[6]) else 0) + (if _i >= 7 then
_m7 * Power(_x, 7) * (_f[7]) else 0) - (if _i >= 8 then
_m8 * Power(_x, 8) * (_f[8]) else 0) + (if _i == 9 then
_m9 * Power(_x, 9) * (_f[9]) else 0);
plot return = _f;
}
#//9 var declaration fun
#f_pole (_a, _s, _i) =>
script f_pole {
input _a = 0;
input _s = 0;
input _i = 0;
def _f1 = f_filt9x(_a, _s, 1);
def _f2 = (if _i >= 2 then f_filt9x(_a, _s, 2) else 0);
def _f3 = (if _i >= 3 then f_filt9x(_a, _s, 3) else 0);
def _f4 = (if _i >= 4 then f_filt9x(_a, _s, 4) else 0);
def _f5 = (if _i >= 5 then f_filt9x(_a, _s, 5) else 0);
def _f6 = (if _i >= 6 then f_filt9x(_a, _s, 6) else 0);
def _f7 = (if _i >= 2 then f_filt9x(_a, _s, 7) else 0);
def _f8 = (if _i >= 8 then f_filt9x(_a, _s, 8) else 0);
def _f9 = (if _i == 9 then f_filt9x(_a, _s, 9) else 0);
def _fn = if _i == 1 then _f1 else if _i == 2 then _f2 else if _i == 3 then _f3 else
if _i == 4 then _f4 else if _i == 5 then _f5 else if _i == 6 then _f6 else
if _i == 7 then _f7 else if _i == 8 then _f8 else if _i == 9 then _f9 else Double.NaN;
plot filtn = _fn;
plot filt1 = _f1;
}
# Variables
script g {
input data = hlc3;
def w = (2 * Double.Pi / 8);
def beta = (1 - Cos(w)) / (Power(1.414, 2.0 / 4) - 1 );
def alpha = (-beta + Sqrt(beta * beta + 2 * beta));
def G = Power(alpha, 4) * data +
4 * (1 – alpha) * G[1] – 6 * Power( 1 - alpha, 2 ) * G[2] +
4 * Power( 1 - alpha, 3 ) * G[3] - Power( 1 - alpha, 4 ) * G[4];
plot Line = G;
}
#/------------------------------------------
#//Definitions
#//-----------------------------------------
#//Beta and Alpha Components
def beta = (1 - Cos(4 * ASin(1) / SmaplePeriod)) / (Power(1.414, 2 / Poles) - 1);
def alpha = - beta + Sqrt(Power(beta, 2) + 2 * beta);
#//Lag
def lag = (SmaplePeriod - 1) / (2 * Poles);
def tr = TrueRange(high, close, low);
#//Data
def srcdata = if ReducedLagMode then src + (src - GetValue(src, lag)) else src;
def trdata = if ReducedLagMode then tr + tr - GetValue(tr, lag) else tr;
#//Filtered Values
def filtn = f_pole(alpha, srcdata, Poles).filtn;
def filt1 = f_pole(alpha, srcdata, Poles).filt1;
def filtntr = f_pole(alpha, trdata, Poles).filtn;
def filt1tr = f_pole(alpha, trdata, Poles).filt1;
#//Lag Reduction
def filt = if FastResponseMode then (filtn + filt1) / 2 else filtn;
def filttr = if FastResponseMode then (filtntr + filt1tr) / 2 else filtntr;
#//Bands
def hband = filt + filttr * mult;
def lband = filt - filttr * mult;
def fcolor = if filt > filt[1] then 1 else if filt < filt[1] then -1 else 0;
#//line Plots
plot filter = if IsNaN(close) then na else filt; #"Filter"
filter.SetLineWeight(2);
filter.AssignValueColor(if fcolor > 0 then GlobalColor("Exup") else
if fcolor < 0 then GlobalColor("Exdn") else Color.GRAY);
#//Band Plots
plot hbandplot = if ShowChannel and !IsNaN(close) then hband else na; # "Filtered True Range High Band"
hbandplot.AssignValueColor(if fcolor > 0 then GlobalColor("EXup") else
if fcolor < 0 then GlobalColor("Exdn") else Color.GRAY);
plot lbandplot = if ShowChannel and !IsNaN(close) then lband else na; # "Filtered True Range Low Band"
lbandplot.AssignValueColor(if fcolor > 0 then GlobalColor("Exup") else
if fcolor < 0 then GlobalColor("Exdn") else Color.GRAY);
AddCloud(if ShowCloud and !IsNaN(close) and fcolor > 0 then hband else
if ShowCloud and !IsNaN(close) and fcolor < 0 then lband else na,
if fcolor > 0 then lband else if fcolor < 0 then hband else na,
GlobalColor("up"), GlobalColor("dn"));
#//price color Plots
AssignPriceColor(if BarColor then
if (src > src[1]) and (src > filt) and (src < hband) then GlobalColor("Exup") else
if (src > src[1]) and (src >= hband) then GlobalColor("weakup") else
if (src <= src[1]) and (src > filt) then GlobalColor("up") else
if (src < src[1]) and (src < filt) and (src > lband) then GlobalColor("Exdn") else
if (src < src[1]) and (src <= lband) then GlobalColor("weakdn") else
if (src >= src[1]) and (src < filt) then GlobalColor("dn") else Color.DARK_GRAY
else Color.CURRENT);
#### END