// Downloaded From https://www.WiseStockTrader.com

_SECTION_BEGIN("5 sec Bad Ticks"); 
FilterType = ParamList("Filter type","None|TEMAOC4|MedianOHLC9]|UnSpike",1);

if(FilterType=="UnSpike" )
{
	CH=C==H; CL=C==L; OH=O==H; OL=O==L;
	MH=Median(H,4); ML=Median(L,4);
	MH[0]=MH[1]=MH[2]=MH[3]=MH[4]; //workaround for bug?
	ML[0]=ML[1]=ML[2]=ML[3]=ML[4]; //workaround for bug?
	H=IIf(CH,MH,H); //trim glitch High close
	C=IIf(CH,MH,C); //trim glitch Close high
	O=IIf(OH,MH,O); //trim glitch Open high
	H=IIf(OH,MH,H); //trim glitch High open
	L=IIf(CL,ML,L); //trim glitch Low close
	C=IIf(CL,ML,C); //trim glitch Close low
	O=IIf(OL,ML,O); //trim glitch Open low
	L=IIf(OL,ML,L); //trim glitch Low open
	H=Min(H,Min(MH*2-L,H)); //trim out of range H
	L=Max(L,Max(ML*2-H,L)); //trim out of range L
	O=Min(H,Max(L,O)); //trim out of range O
	C=Min(H,Max(L,C)); //trim out of range C
}

if(FilterType=="MedianOHLC9" )
{
	H=Median(H,9);
	L=Median(L,9);
	O=Min(H,Max(L,O));
	C=Min(H,Max(L,C));
}

if(FilterType=="TEMAOC4" ){O=H=L=C=TEMA((O+C)/2,4);}

AddToComposite(O ,"~SA_Clean" ,"O",atcFlagEnableInIndicator|atcFlagDeleteValues);
AddToComposite(H ,"~SA_Clean" ,"H",atcFlagEnableInIndicator);
AddToComposite(L ,"~SA_Clean" ,"L",atcFlagEnableInIndicator);
AddToComposite(C ,"~SA_Clean" ,"C",atcFlagEnableInIndicator);
AddToComposite(V ,"~SA_Clean" ,"V",atcFlagEnableInIndicator);

_SECTION_END(); // end of bad tick removal routine


_SECTION_BEGIN("Test Timeframes"); //Explore timeframe functions

Chartframe = ParamList("Chart Type","None|Native|Time bars|Volume bars|$Range bars|%Range bars",0);
Timebar = 60*Param("Time (minutes)",1,1,10,1);
Volumebar = 12*Param("Volume (avg rate)",1,.25,10,.25); //finds a volume with an average bar rate in minutes
Rangebar = Param("$ Range",.1,.01,10,.01);
RangebarP = Param("% Range",.1,.01,2,.01)/100;

if(Chartframe=="Time bars")
{
	Plot(Timebar/60 ,"Bar Minutes",colorBlue,styleNoLabel|styleNoLine|styleOwnScale); //just for the number
	TimeFrameMode(0);
	TimeFrameSet(Timebar ); //force n*5 second bars now
}

if(Chartframe=="Volume bars")
{
	// first total all the volume for the last 4680 bars between 9:30 and 4:00 only to get a volume per 5 sec bar
	Totalv=0; totalbars=0;
	bartimes = TimeNum(); //time of day to gather volume
	for(i=BarCount-1;totalbars<4680;i--)
	{
		if(bartimes[i]>=93000 AND bartimes[i]<160000){Totalv += V[i]; totalbars++;}
	}
	avevol = Volumebar*Totalv/4860;
	ex = int(log10(avevol))-1; //2 significant digits to keep
	barvol = ((10^ex)*(round((avevol)/(10^ex)))); //truncate significance to keep from dithering
	Plot(barvol ,"Bar Vol",colorBlue,styleNoLabel|styleNoLine|styleOwnScale); //just for the number
	TimeFrameMode(2);
	TimeFrameSet(barvol);
}

if(Chartframe=="$Range bars") //this does not work right, Bug???
{
	Plot(Rangebar ,"Bar range",colorBlue,styleNoLabel|styleNoLine|styleOwnScale); //just for the number
	TimeFrameMode(3);
	TimeFrameSet(Rangebar );
} 

if(Chartframe=="%Range bars") //this does not work right either, Bug???
{
	// first total all the prices for the last 4680 bars between 9:30 and 4:00 only to get an average price
	Totalp=0; totalbars=0;
	bartimes = TimeNum(); //time of day to gather volume
	for(i=BarCount-1;totalbars<4680;i--)
	{
		if(bartimes[i]>=93000 AND bartimes[i]<160000){Totalp += C[i]; totalbars++;}
	}
	range = RangebarP*Totalp/4860;
	ex = int(log10(range))-1; //2 significant digits to keep
	barP = ((10^ex)*(round((range)/(10^ex)))); //truncate significance to keep from dithering
	Plot(barP ,"Bar range",colorBlue,styleNoLabel|styleNoLine|styleOwnScale); //just for the number
	TimeFrameMode(3);
	TimeFrameSet(barP );
} 

if(Chartframe!="None") //use none for high speed
{
	Plot(C ,"Price",colorBlue,styleBar);
	// quick and dirty trading day start and end markers
	Plot( TimeNum()>=160000 AND Ref(TimeNum(),-1)<160000 , "", colorRed,styleOwnScale|styleNoLabel|styleDashed|styleHistogram);
	Plot( TimeNum()>=93000 AND Ref(TimeNum(),-1)<93000, "", colorGreen,styleOwnScale|styleNoLabel|styleDashed|styleHistogram);
}
_SECTION_END();