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: TWS auto-export Executions-file parser
Author/Uploader: Herman van den Bergen - (email hidden)
Date/Time added: 2005-04-10 18:00:26
Origin:
Keywords: TWS trade-executions converter and trade-plotter
Level: advanced
Flags: function

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:

Intended for real-time traders using the Interactive Brokers TWS. This function converts the auto-exported trade executions file from the TWS to csv format, creates an AmiBroker compatible format for further processing, and plots real trades at execution prices on your chart. This is experimental code provided for experienced afl programmers only.

Formula:

// Important: This is experimental, as-is, code for you to play with and
improve. 
// Converts and Plots trades listed in the auto-exported TWS Trade-Executions
file.
// This code is intended for Real-Time trading, I use it in the 3-min
timeframe.
// Configure your TWS to auto-export the trade-executions list (TWSTrades.txt)
each Minute.
// Change the destination path/name for your csv output file for your setup.
// One array is returned by the function: negative prices are Shorts, pos
prices are Long.
// Note that you must have real executed trades exported to plot them
// The program doesn't concatenate trading days yet but creates timestamped csv
files.
// The trade list is copied to StaticVarSetText("TWSTrades") for further
processing or 
// display in the interpretation window. Coded by Herman van den Bergen -
10APR2005.

function PlotTWSExecutions()
	{
	InputPath = "C:\\Jts\\TWSTrades.txt";
	OutPutFilename = "TWSTrades"+NumToStr(Now(3),1.0,False)+".csv";
	OutputPath = "C:\\Program Files\\AmiBroker\\TWSTrades\\"+OutPutFilename;
	fdelete( OutPutPath );
	fh1 = fopen( InputPath, "r"); 
	fh2 = fopen( OutputPath, "a");

	if( fh1 AND fh2 ) 
		{ 
		TradeList= ""; TradeNum = 0;
  		while( ! feof( fh1 ) ) 
   		{ 
      	Line1 = fgets( fh1 );
			Line2 = "";
			Len = StrLen(Line1);
			for(p=0; p<=Len; p++)
				{
				Char = StrMid(Line1,p,1);
				if( Char == ";" ) Char = ",";
				Line2 = Line2 + Char;
				} 

			if( Line2 != "" )
				{
				TradeNum++;
				fputs( Line2, fh2);
				Ticker 	= StrExtract(Line2,0);
				Action 	= StrExtract(Line2,1); if( Action=="SLD" ) ActionNum = -1; else
if(Action=="BOT") ActionNum=1; 
				Shares	= StrExtract(Line2,2); SharesS	= StrToNum(Shares);
				Price		= StrExtract(Line2,3); PriceNum	= StrToNum(Price);
				TimeStr	= StrExtract(Line2,4);
				DateStr	= StrExtract(Line2,5);
				ABDateNum = 10000*(StrToNum(StrLeft(DateStr,4))-1900)+
									100*StrToNum(StrMid(DateStr,4,2))+
										 StrToNum(StrRight(DateStr,2)); 
				ABDateNumStr = NumToStr(ABDateNum ,1.0,False);
				TradeNumStr = NumToStr(TradeNum,1.0,False);

				ABTime = "";TimeOffSet = 040000;	// Adjust to your location
				for(t=0; t<=StrLen(TimeStr); t++)
					{
					Char = StrMid(TimeStr,t,1);
					if( Char == ":" ) Char="";
					ABTime = ABTime + Char;
					}

				ABTimeNum = StrToNum(ABTime)-TimeOffSet;
				VarSet("TradeTime"+TradeNum, ABTimeNum);
				VarSet("TradePrice"+TradeNum, ActionNum * PriceNum);
				VarSet("TradeDate"+TradeNum, ABDateNum);
				ABTimeStr = NumToStr(ABTimeNum,1.0,False);

				DateStr	= StrExtract(Line2,5);
				Trade = TradeNumStr+" ,"+ABTimeStr+", "+ABDateNumStr+" "+Action + ", "+
SharesS+", "+Price +"\n";
				TradeList= TradeList + Ticker + ", "+ Trade;
				}
 			}
		StaticVarSetText("TWSTrades",TradeList);
		fclose(fh1);
		fclose(fh2);
		} 
	else TradeList = "TradeList could not be converted";

	FirstVisibleBar = Status( "FirstVisibleBar");
	Lastvisiblebar = Status("LastVisibleBar");
	TN = TimeNum();
	PriceArray = Null;
	TradeNum = 1;
	TTS = VarGet("TradeTime"+TradeNum);
	DN=DateNum();

	for( b = Firstvisiblebar; b < Lastvisiblebar; b++)
		{
		TDN = VarGet("TradeDate"+TradeNum);
		if( TDN == DN[b] )
			{
			TTSs = NumToStr(TTS,1.0,False);
			while( TTS > TN[b-1] AND TTS <= TN[b] )
				{
				PriceNum = VarGet("TradePrice"+TradeNum);
				if( PriceNum <0 ) ActionNum = -1; else ActionNum = 1;
				PriceArray[b] = PriceNum;
				TTS = VarGet("TradeTime"+(++TradeNum));
				}
			}
		}
	return PriceArray;
	}

TWSTradePrices= PlotTWSExecutions();
Tradecolor = IIf(TWSTradePrices<0,4,5);
Plot(C,"",1,128);
PlotShapes(IIf(TWSTradePrices,shapeSmallCircle,shapeNone),TradeColor,0,abs(TWSTradePrices),0);

TWSTradeList = StaticVarGetText("TWSTrades");

Comments:

Robert
robert [at] hoemke.de
2006-05-18 10:08:43
Hello Herman,
i get an error message:

for( b = Firstvisiblebar; b < Lastvisiblebar; b++)
{
TDN = VarGet("TradeDate"+TradeNum);
if( TDN == DN[b] )

Error 10.
Subscript out of range
You must not access array elements outside 0 .. (BarCount-1) range


Please help. I don't know where to change the formula.

Regards
Robert
Rene Nederhand
rene [at] nederhand.net
2008-01-09 18:31:12
Change this line:

for( b = Firstvisiblebar; b < Lastvisiblebar; b++)

in:

for( b = FirstVisibleBar; b < LastVisibleBar AND b < BarCount; b++)

This will solve the error, but I still can't get trade shapes plotted in my graph.
sogodo

2008-11-29 21:14:20
> This will solve the error, but I still can\\\'t
> get trade shapes plotted in my graph.

That\\\'s because you need to adjust the time zone.
in my version, I have time to be 20:52:11 for YM (e-mini Dow), for example, and my time (EST) is really was 15:52:11. When I adjusted the following statement:

TimeOffSet = 050000; // 5 hours difference

the trade shapes appeared on my graph.

It is kinda slow.

I added the verification to filter out only the Name() (ticker in the chart, actually, better StrLeft(Name(),8) and not all other symbols in the file.

Then I get rid of the code, copying data into another file, and removed the spooling.

For me, it\\\'s easy to copy (or autocopy) the whole file and parse it without any spooling, especially, if you need to check your trades at the end of the day, and not during trading session, where permanent I/O operations deteriorate performance, especially if you make thousand transactions per day, and executions.txt file keeps tens and more days.
sogodo

2008-11-29 21:14:35
> This will solve the error, but I still can\\\'t
> get trade shapes plotted in my graph.

That\\\'s because you need to adjust the time zone.
in my version, I have time to be 20:52:11 for YM (e-mini Dow), for example, and my time (EST) is really was 15:52:11. When I adjusted the following statement:

TimeOffSet = 050000; // 5 hours difference

the trade shapes appeared on my graph.

It is kinda slow.

I added the verification to filter out only the Name() (ticker in the chart, actually, better StrLeft(Name(),8) and not all other symbols in the file.

Then I get rid of the code, copying data into another file, and removed the spooling.

For me, it\\\'s easy to copy (or autocopy) the whole file and parse it without any spooling, especially, if you need to check your trades at the end of the day, and not during trading session, where permanent I/O operations deteriorate performance, especially if you make thousand transactions per day, and executions.txt file keeps tens and more days.
sogodo

2008-11-29 21:14:50
> This will solve the error, but I still can\\\'t
> get trade shapes plotted in my graph.

That\\\'s because you need to adjust the time zone.
in my version, I have time to be 20:52:11 for YM (e-mini Dow), for example, and my time (EST) is really was 15:52:11. When I adjusted the following statement:

TimeOffSet = 050000; // 5 hours difference

the trade shapes appeared on my graph.

It is kinda slow.

I added the verification to filter out only the Name() (ticker in the chart, actually, better StrLeft(Name(),8) and not all other symbols in the file.

Then I get rid of the code, copying data into another file, and removed the spooling.

For me, it\\\'s easy to copy (or autocopy) the whole file and parse it without any spooling, especially, if you need to check your trades at the end of the day, and not during trading session, where permanent I/O operations deteriorate performance, especially if you make thousand transactions per day, and executions.txt file keeps tens and more days.


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