// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("Gauss Kernel");

SetChartBkColor(ParamColor("Panel Color",colorBlack));
SetChartBkGradientFill(ParamColor("Upper Chart",colorBlack),ParamColor("Lower Chart",colorBlack));

function Normalize(array, arraylen)
// Figure 1.7 on p. 7
{
MaxH = HHV(array, arraylen);
MinL = LLV(array, arraylen);
Value1[0] = array[0]; // Initialize as array

for(i = 1; i < BarCount; i++)
{
Value1[i]=.5*2*((array[i]-MinL[i])/IIf(MaxH[i]-MinL[i]==0,1,MaxH[i]-MinL[i])-.5)+.5*Value1[i-1];

if (Value1[i] > .9999) Value1[i] = .9999;
if (Value1[i] < -.9999) Value1[i] = -.9999;
}
return Value1;
}


PI = 3.1415926; 

Data = (H+L)/2; 



Med = (H+L+C)/3;
MidPoint=0;

function Kernel(Input, Length)
{

Norm = 0;
sigma = (Length+1)/4.0;

for (i = 0; i <= Length; i++)
		
	{
		Norm = Norm + exp(-((Length/2 - i)*(Length/2 - i))/(2*sigma*sigma));
	}

array = Input;

for (j = Length; j < BarCount; j++)
	{
		Filtered = 0;

		for (k = 0; k <= Length; k++)

			{
				Filtered = Filtered + exp(-((Length/2 - k)*(Length/2 - k))/(2*sigma*sigma))*Input[j - k];
			}

		array[j] = Filtered/Norm;

	}
return array;
}

function Kernel_HMA(Input, N)
{
	f = Kernel(2 * Kernel(Input,round(N/2)) - Kernel(Input,N), round(sqrt(N)));
return f;
}

//Length1 = Param("Length1", 8, 2, 50, 1);
Length2 = Param("Length2", 13, 2, 50, 1);
Length3 = Param("Length3", 21, 2, 50, 1);
Length4 = Param("Length4", 5, 2, 50, 1);
//Length5 = Param("Length5", 34, 2, 50, 1);


//Smoothx = Kernel(C, Length1);
Smoothy = Kernel(C, Length2);
//Smoothz = Kernel(C, Length3);
Smooth4 = Kernel(C, Length4);
//Smooth5 = Kernel(C, Length5);


//x = C - Smoothx;
y = C - Smoothy;
z = C - Smooth4;
//f4 = C - Smooth4;
//f5 = C - Smooth5;


//Normx = Kernel_HMA(x/sqrt(Kernel(x^2,Length1)),2);
Normy = Kernel_HMA(y/sqrt(Kernel(y^2,Length2)),2);
Normz = Kernel_HMA(z/sqrt(Kernel(z^2,Length4)),2);
// Norm4 = Kernel_HMA(f4/sqrt(Kernel(f4^2,Length4)),2);
//Norm5 = Kernel_HMA(f5/sqrt(Kernel(f5^2,Length5)),2);


//Buy = ( Cross(Normx,0) OR Cross(Normy,0) OR Cross(Normz,0)) AND (Normx > Ref(Normx,-1) AND Normy > Ref(Normy,-1) AND Normz > Ref(Normz,-1));
//Sell = ( Cross(0,Normx) OR Cross(0,Normy) OR Cross(0,Normz) ) AND (Normx < Ref(Normx,-1) AND Normy < Ref(Normy,-1) AND Normz < Ref(Normz,-1));
// FisherXform = Fisher(Normalize(Med, 20)); 

barvisible = Status("barvisible");



//Buy = ExRem(Buy,Sell);
//Sell = ExRem(Sell,Buy);

//shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
//PlotShapes( shape, IIf( Buy, colorBrightGreen, colorRed ), 0, IIf( Buy, 0 , 0));

//Plot(Normx, "Normx", colorYellow, styleThick);
Plot(Normy, "Normy", colorAqua, styleThick);
Plot(Normz, "Normz", colorRed, styleThick);
//Plot(Norm4, "Normz", colorOrange, styleThick);
//Plot(Norm5, "Normz", colorOrange, styleThick);


Plot(0, "", colorWhite, styleThick);
Plot(4, "", colorWhite, styleThick);
Plot(-4, "", colorWhite, styleThick);

Plot(3 ,"", colorWhite, styleThick);
Plot(-3, "", colorWhite, styleThick);

_SECTION_END();


PlusDM= IIf(High>Ref(High,-1) AND Low>=Ref(Low,-1), High-Ref(High,-1),
                 IIf(High>Ref(High,-1) AND Low<Ref(Low,-1)
                 AND High-Ref(High,-1)>Ref(Low,-1)-Low,
                High-Ref(High,-1),0));
 
Vm=log(EMA(V,3));
  
b= (C-L)*Vm+ (H-O)*Vm + 
         IIf(C>O,(C-O)*Vm,0) + PlusDM*Vm + 
         IIf(Ref(C,-1)<O,(O-Ref(C,-1))*Vm,0);
 
MinDM = IIf(Low<Ref(Low,-1)   AND  High<=Ref(High,-1), Ref(Low,-1)-Low,
               IIf(High>Ref(High,-1) AND Low<Ref(Low,-1)
               AND High-Ref(High,-1)<Ref(Low,-1)-Low, Ref(Low,-1)-Low, 0));
 
s= (H-C)*Vm + (O-L)*Vm + 
         IIf(C<O,(O-C)*Vm,0) + MinDM*Vm + 
         IIf(Ref(C,-1)>O,(Ref(C,-1)-O)*Vm,0);
 
Wm=Wilders(Wilders(b,3)-Wilders(s,3),3);
Wm1=MA(Wm,8);
Color=IIf(Wm >10,colorBlue,IIf(Wm < -10,colorRed,7));
// Plot(Wm,"Volume",Color,2|styleThick);
// Plot(Wm1,"Wm(8P)",11,styleLine);
//  Plotlinewidth( Wm1 ,"",colorwhite,styleLine,Null,Null,0,3,1);
 //Plot(0,"",ParamColor("Zero Line",colorGrey50),styleLine|styleNoLabel);

f4 = Wm;


Norm4 = Kernel_HMA(f4/sqrt(Kernel(f4^2,Length4)),2);
//Norm5 = Kernel_HMA(f5/sqrt(Kernel(f5^2,Length4)),2);


Plot(Norm4, "NormVal", coloryellow, styleThick);