Welcome Features News Download Registration Support FAQ Wish list Links
Advanced stock charting and analysis program

AFL Library

This is read-only version of AFL library entry. Ability to add user formulas and comment is only available from members-only area.

Details:

Formula name: Bad Tick Trim on 5 sec database
Author/Uploader: Dennis Brown - (email hidden)
Date/Time added: 2007-08-11 19:58:46
Origin: Original
Keywords: Bad Ticks
Level: medium
Flags: indicator

DISCLAIMER: Most formulas present in AFL on-line library are submitted by the users and are provided here on an "as is" and "as available" basis. AmiBroker.com makes no representations or warranties of any kind to the contents or the operation of material presented here. We do not maintain nor provide technical support for 3rd party formulas.
Description:

This chart is used to generate clean data for another chart.
Link this chart to another one.
This runs on a 5 second database to trim bad ticks.
Clean data is saved to "~SA_Clean" ATC ticker every pass.

Linked chart must replace OHLCV arrays. Place the following AFL at the beginning of the linked chart:
SetForeign("~SA_Clean);

Try out your own high resolution data filtering algorithms.

The Test Timeframes Section allows you to see the effect of different settings using the TimeFrameSet and TimeFrameMode settings --with a few twists.
For instance you can see how much bar volume is required to get a 5 minute rate or compare different settings --however, studies will not work in this window.

Formula:

///////////////////////////////////////////////////////////////////////////
// Bad Tick Trim on 5 sec database (by Dennis Brown 8/11/2007)
//
// This chart is used to generate clean data for another chart.
// Link this chart to another one.
// This runs on a 5 second database to trim bad ticks.
// Clean data is saved to "~SA_Clean" ATC ticker every pass.
//
// Linked chart must replace OHLCV arrays.
// Place the following AFL at the beginning of the linked chart:
// SetForeign("~SA_Clean);
//
// Try out your own high resolution data filtering algorithms.
//
// The Test Timeframes Section allows you to see the effect of different
settings
// using the TimeFrameSet and TimeFrameMode settings --with a few twists.
// For instance you can see how much bar volume is required to get a 5 minute
rate 
// or compare different settings --however, studies will not work in this
window.
//

Version(4.99); //minimum version required

_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();

Comments:


About | Privacy | Terms of Use | Contact information
Copyright © 2001 AMIBROKER.COM