// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("FVE");
/* Volatility modified FVE chart 
** plus Volatility color-coded Volume chart + trading system
** TJ for S&C Traders Tips Sep 2003,
*/
Period = Param("Period for FVE", 24, 5, 80, 1 );
Coeff = Param("Coeff for Cutoff", 0.1, 0, 2, 0.01 );
intra=log(H)-log(L);
Vintra = StDev(intra, period );
inter = log(Avg)-log(Ref(Avg,-1));
Vinter = StDev(inter,period);
Cutoff = Coeff * (Vinter+Vintra)*C;
MF = C- (H+L)/2 + Avg - Ref( Avg, -1 );
VC = IIf( MF > Cutoff, V,
IIf( MF < -Cutoff, -V, 0 ));
FVE = 100 * Sum( VC, Period )/(MA( V, Period ) * Period );
//Plot( FVE, "Modified FVE", colorRed, styleThick );
// volatility color-coded volume bars:
BarColor =
IIf( MF > Cutoff,  colorGreen, /* up volume */
IIf( MF < -Cutoff, colorRed, /* down volume */
                   colorBlue /*otherwise*/ ));
//Plot( Volume, "Volume", BarColor, styleHistogram | styleThick | styleOwnScale );
/* Trading system */
SetTradeDelays(0,0,0,0);
PositionSize = 10000; // fixed position size - remove to enable compounding
RoundLotSize = 10;
BuyPrice = Close; SellPrice = Close;
RAD_TO_DEG = 180/3.1415926; // radians to degrees
LinRegAngleFVE = RAD_TO_DEG * atan( LinRegSlope( FVE, 20 ) );
Buy = FVE < 10 AND
FVE > -20 AND
LinRegAngleFVE > 30 AND
FVE > EMA( FVE, 40 ) AND
LinRegSlope( C, 30 ) < Ref(C, -30 ) *0.6/100 AND
LinRegSlope( C, 30 ) > -Ref( C, -30 ) * 0.3/100;
Sell = LinRegAngleFVE < -30;

Short = Sell;
Cover = Buy;
_SECTION_END();

/*Plot( 60, "", colorwhite, styleThick );
Plot( 0, "", colorwhite, styleThick );
Plot( -60, "", colorwhite, styleThick );
*/
//SetChartBkColor(ParamColor("Panel Color",colorBlack));
//SetChartBkGradientFill(ParamColor("Upper Chart",colorBlack),ParamColor("Lower Chart",colorBlack));


NDX_period          = Param("NDX Period",75,40,375);
NDX_SmLen           = round(NDX_Period*20/40);

NST_period          = round(NDX_Period*20/40);
NST_SmLen           = round(NDX_Period*10/40);


SumWght = 0;
SumDnom = 0;

    for(i = 1; i < NDX_period+1; i++)
      {
      DifAry = ln(Ref(Close,-i+1)) - ln(Ref(Close,-i));
      if(i == 1) 
		DnomAry = abs(DifAry); 
		else 
		DnomAry =DNomAry+ abs(DifAry);
	  FracAry =IIf(DNomAry==0, 0,(ln(Close) - ln(Ref(Close,-i))) / DnomAry);
      TimeAry = 1 / (i^( 0.5));
      WghtAry = FracAry * TimeAry;
      SumWght =SumWght+ WghtAry;
      SumDnom =SumDnom+ TimeAry;
      }
    RawNDX = 100 * SumWght / SumDnom;

    TemaNDX= TEMA(RawNDX,NDX_SmLen); 

	 SumWgt = 0;
		 SumDnm = 0;
 
    for(i = 0; i <NST_period+1; i++)
      {
      if(i < 2) 
		{
		HghAry = High;
		LowAry = Low; 
		} 
		HghAry = HHV(High, i);
		LowAry=LLV(Low,i);
	
      RngAry = HghAry - LowAry;
     
	   FrcAry =IIf(RngAry==0, 0,(Close- LowAry) / RngAry);
      TmeAry = 1 / ((i+1)^(0.5));
      WgtAry = TmeAry * FrcAry;
      SumWgt =SumWgt+ WgtAry;
      SumDnm =SumDnm+ TmeAry;
      }
    RawNST = Nz((200 * SumWgt / SumDnm) - 100);

    TemaNST= TEMA(RawNST,NST_SmLen); 

 
      NAvg = (abs(TemaNDX) * TemaNST + abs(TemaNST) * TemaNDX) / 2;
	   Nsign= IIf(NAvg>0,1,IIf(NAVG<0,-1,0));
 
      NXC =round(Nsign * sqrt(abs(NAvg)));


// Plot(NXC,"NXC",coloryellow,styleLine|styleThick);
//===============================================================================================

_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;
// Fisher Transform


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", 8, 2, 50, 1);
//Length5 = Param("Length5", 34, 2, 50, 1);


//Smoothx = Kernel(C, Length1);
Smoothy = Kernel(C, Length3);
//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(wm/sqrt(Kernel(f4^2,Length3)),2);
//Norm5 = Kernel_HMA(f5/sqrt(Kernel(f5^2,Length4)),2);

Plot(Norm4, "NormVal", coloryellow, styleThick);
//Plot(Norm5,"Wm(8P)",11,styleThick);
 
 
 f5 = NXC;
 Norm5 = Kernel_HMA(f5/sqrt(Kernel(f5^2,Length3)),2);

 Plot(Norm5, "NormNXC", colorgreen, styleThick); 
 f6 = FVE;
 
 Norm6 = Kernel_HMA(f6/sqrt(Kernel(f5^2,Length3)),2);
 
 Plot(Norm6, "NormFVE", coloraqua, styleThick);