{"id":1556,"date":"2008-02-18T14:48:19","date_gmt":"2008-02-18T14:48:19","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2008\/02\/18\/readingbacking-up-the-tws-exported-execution-report\/"},"modified":"2008-02-18T15:01:16","modified_gmt":"2008-02-18T15:01:16","slug":"readingbacking-up-the-tws-exported-execution-report","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2008\/02\/18\/readingbacking-up-the-tws-exported-execution-report\/","title":{"rendered":"Reading\/Backing-up the TWS Exported Execution Report"},"content":{"rendered":"

Please be sure you set up the TWS according to the instructions provided in Setting Up your TWS<\/a> before you test this code. The code presented here reads the execution report, converts it to a .csv format, date-stamps it, backs it up for later use, and optionally displays it in the chart Title. The code doesn’t do anything important besides displaying the information in the Title. The idea is to show you how to read the file so that you can extract real execution prices, use them in your calculations, and plot them on your chart. The Param options are self-explanatory:<\/h4>\n

\"clip_image002\"<\/a><\/p>\n

The name used for the execution report generated by the TWS is not date-stamped. For example, if you set up the TWS to export executions under the name Simulated.Trades,<\/i> this same name will be used on successive days. If the TWS finds a tradelist from the previous day, it will simply overwrite it. To prevent losing this AFL readable file it is important to back up the tradelist at the end of the day. The format of the execution report exported by the TWS looks like this:<\/p>\n

\nATVI;SLD;75;26.19;21:29:33;20080125;ARCA;DU1195;;;DEMO;
\nALTR;BOT;100;18.54;21:53:12;20080125;ARCA;DU1195;;;DEMO;
\nALTR;BOT;100;18.58;21:55:59;20080125;ISLAND;DU1195;;;DEMO;
\nALTR;BOT;100;18.55;21:56:00;20080125;ARCA;DU1195;;;DEMO;
\nALTR;BOT;100;18.58;21:58:47;20080125;ISLAND;DU1195;;;DEMO;\n<\/p>\n

The .csv format of the backup file produced by the code below can be directly imported into Excel and looks like this (note the semicolons have been replaced by commas):<\/p>\n

\nATVI,SLD,75,26.19,21:29:33,20080125,ARCA,DU1195,,,DEMO,
\nALTR,BOT,100,18.54,21:53:12,20080125,ARCA,DU1195,,,DEMO,
\nALTR,BOT,100,18.58,21:55:59,20080125,ISLAND,DU1195,,,DEMO,
\nALTR,BOT,100,18.55,21:56:00,20080125,ARCA,DU1195,,,DEMO,
\nALTR,BOT,100,18.58,21:58:47,20080125,ISLAND,DU1195,,,DEMO,\n<\/p>\n

In Excel, the file will look like this after activating Text to columns:<\/p>\n

\"clip_image004\"<\/a><\/p>\n

Please be aware that the minimum update interval that the TWS exports the execution report is approximately one-minute. This means it will take some time for your trades to show up in the list.<\/p>\n

Before tackling the main backup function, there are a few helper functions you will need. While these are available elsewhere on this site, they are repeated below for your convenience. To prevent conflicts between static variables used in different programs, you should key their names with those charted; see Keying Static Variables<\/a> for more information on this. The DateNumToStr() converts DateNumbers to a standard date string. <\/p>\n

The TWSBackupTradeList( TWSInputPath )<\/i> listed below reads the TWS tradelist, extracts the date, converts it to the .csv format, saves it in a different location, and optionally displays both tradelists in the chart Title. To test this function, Apply it to a new Indicator, open the Param window, set up the parameters, and click BACKUP. The backup file is saved in the path defined by the TradebackupFolder <\/i>variable. If the function finds the execution report and its display is turned on in the Param window, this should look like this in the Title (only a few lines shown):<\/p>\n

\"clip_image006\"<\/a><\/p>\n

And, when displayed, the backup file should look like that below:<\/p>\n

\"clip_image008\"<\/a><\/p>\n

\rInIndicator     <\/span>= <\/span>Status<\/span>( <\/span>"Action" <\/span>) == <\/span>1<\/span>;\r<\/span>StaticVarKey     <\/span>= <\/span>GetChartID<\/span>();\r\r<\/span>procedure xStaticVarSet<\/span>( <\/span>SName<\/span>, <\/span>SValue <\/span>)\r{\r    global <\/span>StaticVarKey<\/span>;\r\r    if ( <\/span>InIndicator <\/span>)\r        <\/span>StaticVarSet<\/span>( <\/span>Sname <\/span>+ <\/span>StaticVarKey<\/span>, <\/span>Svalue <\/span>);\r}\r\rfunction <\/span>xStaticVarGet<\/span>( <\/span>SName <\/span>)\r{\r    global <\/span>StaticVarKey<\/span>;\r    return <\/span>StaticVarGet<\/span>( <\/span>Sname <\/span>+ <\/span>StaticVarKey <\/span>);\r}\r\r<\/span>procedure xStaticVarSetText<\/span>( <\/span>SName<\/span>, <\/span>SValue <\/span>)\r{\r    global <\/span>StaticVarKey<\/span>;\r\r    if ( <\/span>InIndicator <\/span>)\r        <\/span>StaticVarSetText<\/span>( <\/span>Sname <\/span>+ <\/span>StaticVarKey<\/span>, <\/span>Svalue <\/span>);\r}\r\rfunction <\/span>xStaticVarGetText<\/span>( <\/span>SName <\/span>)\r{\r    global <\/span>StaticVarKey<\/span>;\r    return <\/span>StaticVarGetText<\/span>( <\/span>Sname <\/span>+ <\/span>StaticVarKey <\/span>);\r}\r\rfunction <\/span>DateNumToStr<\/span>( <\/span>DtNum <\/span>)\r{\r    <\/span>DayNm <\/span>= <\/span>round<\/span>( <\/span>frac<\/span>( <\/span>DtNum <\/span>\/ <\/span>100 <\/span>) * <\/span>100 <\/span>);\r    <\/span>MthNm <\/span>= <\/span>round<\/span>( <\/span>frac<\/span>( <\/span>DtNum <\/span>\/ <\/span>10000 <\/span>) * <\/span>100 <\/span>);\r    <\/span>YrNm <\/span>= <\/span>int<\/span>( <\/span>DtNum <\/span>\/ <\/span>10000 <\/span>) + <\/span>1900<\/span>;\r    return <\/span>NumToStr<\/span>( <\/span>MthNm<\/span>, <\/span>1.0 <\/span>) + <\/span>"\/" <\/span>+ <\/span>NumToStr<\/span>( <\/span>DayNm<\/span>, <\/span>1.0 <\/span>) + <\/span>"\/" <\/span>+ <\/span>NumToStr<\/span>( <\/span>YrNm<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>);\r}\r\r<\/span>procedure TWSBackupTradeList<\/span>( <\/span>TWSInputPath <\/span>)\r{\r    global <\/span>TradebackupFolder<\/span>, <\/span>AccountType<\/span>;\r    <\/span>fh1 <\/span>= <\/span>fopen<\/span>( <\/span>TWSInputPath<\/span>, <\/span>"r" <\/span>);\r\r    if ( <\/span>fh1 <\/span>)\r    {\r        <\/span>Line                 <\/span>= <\/span>StrReplace<\/span>( <\/span>fgets<\/span>( <\/span>fh1 <\/span>), <\/span>";"<\/span>, <\/span>"," <\/span>);\r        <\/span>DateStr             <\/span>= <\/span>StrExtract<\/span>( <\/span>Line<\/span>, <\/span>5 <\/span>);\r        <\/span>YearNum            <\/span>= <\/span>StrToNum<\/span>( <\/span>StrLeft<\/span>( <\/span>DateStr<\/span>, <\/span>4 <\/span>) );\r        <\/span>MonthNum            <\/span>= <\/span>StrToNum<\/span>( <\/span>StrMid<\/span>( <\/span>DateStr<\/span>, <\/span>4<\/span>, <\/span>2 <\/span>) );\r        <\/span>DayNum            <\/span>= <\/span>StrToNum<\/span>( <\/span>StrRight<\/span>( <\/span>DateStr<\/span>, <\/span>2 <\/span>) );\r        <\/span>DateNumber         <\/span>= ( <\/span>YearNum <\/span>- <\/span>1900 <\/span>) * <\/span>10000 <\/span>+ <\/span>100 <\/span>* <\/span>MonthNum <\/span>+ <\/span>DayNum<\/span>;\r        <\/span>DateNumStr         <\/span>= <\/span>NumToStr<\/span>( <\/span>DateNumber<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>);\r        <\/span>BackupFilename <\/span>= <\/span>AccountType <\/span>+ <\/span>DateNumStr <\/span>+ <\/span>".csv"<\/span>;\r        <\/span>BackupPath         <\/span>= <\/span>TradebackupFolder <\/span>+ <\/span>BackupFilename<\/span>;\r        <\/span>fclose<\/span>( <\/span>fh1 <\/span>);\r    }\r\r    <\/span>fh1 <\/span>= <\/span>fopen<\/span>( <\/span>TWSInputPath<\/span>, <\/span>"r" <\/span>);\r\r    <\/span>fdelete<\/span>( <\/span>BackupPath <\/span>);\r    <\/span>fh2 <\/span>= <\/span>fopen<\/span>( <\/span>BackupPath<\/span>, <\/span>"a" <\/span>);\r    <\/span>LineNum <\/span>= <\/span>0<\/span>;\r    <\/span>TWSTradeList <\/span>= <\/span>CSVTradelist <\/span>= <\/span>""<\/span>;\r\r    if ( <\/span>fh1 <\/span>)\r    {\r        if ( <\/span>fh2 <\/span>)\r        {\r            while ( ! <\/span>feof<\/span>( <\/span>fh1 <\/span>) )\r            {\r                <\/span>Line <\/span>= <\/span>fgets<\/span>( <\/span>fh1 <\/span>);\r                <\/span>TWSTradeList <\/span>= <\/span>TWSTradeList <\/span>+ <\/span>Line<\/span>;\r                <\/span>Line <\/span>= <\/span>StrReplace<\/span>( <\/span>Line<\/span>, <\/span>";"<\/span>, <\/span>"," <\/span>);\r                <\/span>CSVTradelist <\/span>= <\/span>CSVTradelist <\/span>+ <\/span>Line<\/span>;\r                <\/span>LineNum<\/span>++;\r\r                if ( <\/span>Line <\/span>!= <\/span>"" <\/span>)\r                {\r                    <\/span>fputs<\/span>( <\/span>Line<\/span>, <\/span>fh2 <\/span>);\r                }\r            }\r        }\r\r        <\/span>xStaticVarSetText<\/span>( <\/span>"TWSTradelist"<\/span>, <\/span>TWSTradelist <\/span>);\r\r        <\/span>xStaticVarSetText<\/span>( <\/span>"CSVTradelist"<\/span>, <\/span>CSVTradelist <\/span>);\r    }\r    else\r    {\r        if ( <\/span>fh1 <\/span>== <\/span>0 <\/span>)\r        {\r            <\/span>PopupWindow<\/span>( <\/span>"Could NOT Open InputPath: " <\/span>+ <\/span>TWSInputPath<\/span>,\r                         <\/span>"TWS EXPORTED TRADELIST"<\/span>, <\/span>timeout <\/span>= <\/span>5<\/span>, <\/span>left <\/span>= -<\/span>1<\/span>, <\/span>top <\/span>= -<\/span>1 <\/span>);\r        }\r\r        if ( <\/span>fh2 <\/span>== <\/span>0 <\/span>)\r        {\r            <\/span>PopupWindow<\/span>( <\/span>"Could not open OutputPath: " <\/span>+ <\/span>OutputPath<\/span>,\r                         <\/span>"TWS EXPORTED TRADELIST"<\/span>, <\/span>timeout <\/span>= <\/span>5<\/span>, <\/span>left <\/span>= -<\/span>1<\/span>, <\/span>top <\/span>= -<\/span>1 <\/span>);\r        }\r    }\r\r    if ( <\/span>fh1 <\/span>)\r        <\/span>fclose<\/span>( <\/span>fh1 <\/span>);\r\r    if ( <\/span>fh2 <\/span>)\r        <\/span>fclose<\/span>( <\/span>fh2 <\/span>);\r\r    <\/span>Caption <\/span>= <\/span>"TWS EXPORTED TRADELIST"<\/span>;\r\r    <\/span>Message <\/span>= <\/span>"The TWS Tradelist: \\n   " <\/span>+ <\/span>TWSInputPath <\/span>+ <\/span>" [" <\/span>+ <\/span>NumToStr<\/span>( <\/span>LineNum<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>) +\r              <\/span>" Trades\/" <\/span>+ <\/span>DateNumToStr<\/span>( <\/span>DateNumber <\/span>) + <\/span>"]" <\/span>+\r              <\/span>" \\nHas been saved in csv format as:\\n   " <\/span>+ <\/span>BackupPath<\/span>;\r\r    <\/span>PopupWindow<\/span>( <\/span>Message<\/span>, <\/span>Caption<\/span>, <\/span>timeout <\/span>= <\/span>20<\/span>, <\/span>left <\/span>= -<\/span>1<\/span>, <\/span>top <\/span>= -<\/span>1 <\/span>);\r}\r\r<\/span>_SECTION_BEGIN<\/span>( <\/span>"BACKUP TWS TRADELIST" <\/span>);\r<\/span>TWSInputPath                <\/span>= <\/span>ParamStr<\/span>( <\/span>"TWS Tradelist (Folder)"<\/span>, <\/span>"C:\\\\Jts\\\\" <\/span>);\r<\/span>AccountType                    <\/span>= <\/span>ParamList<\/span>( <\/span>"TWS Account Type (Filename)"<\/span>, <\/span>"Real|Simulated|Demo"<\/span>, <\/span>1 <\/span>);\r<\/span>TWSInputFilename            <\/span>= <\/span>AccountType <\/span>+ <\/span>".Trades"<\/span>;\r<\/span>TWSInputPath                 <\/span>= <\/span>TWSInputPath <\/span>+ <\/span>TWSInPutFilename<\/span>;\r<\/span>TradebackupFolder            <\/span>= <\/span>ParamStr<\/span>( <\/span>"Backup Destination Folder"<\/span>, <\/span>"C:\\\\Jts\\\\TWSTrades\\\\" <\/span>);\r<\/span>BackupTWSTradeList        <\/span>= <\/span>ParamTrigger<\/span>( <\/span>"Create Backup Tradelist"<\/span>, <\/span>"BACKUP" <\/span>);\r<\/span>ShowTWSTradeList            <\/span>= <\/span>ParamToggle<\/span>( <\/span>"TWS Tradelist"<\/span>, <\/span>"HIDE|SHOW"<\/span>, <\/span>1 <\/span>);\r<\/span>ShowCSVTradeList            <\/span>= <\/span>ParamToggle<\/span>( <\/span>"CSV Tradelist"<\/span>, <\/span>"HIDE|SHOW"<\/span>, <\/span>1 <\/span>);\r<\/span>_SECTION_END<\/span>();\r\rif ( <\/span>BackupTWSTradeList <\/span>)\r    <\/span>TWSBackupTradeList<\/span>( <\/span>TWSInputPath <\/span>);\r\r<\/span>TWSStr <\/span>= <\/span>WriteIf<\/span>( <\/span>ShowTWSTradeList<\/span>, <\/span>"\\nTWS Exported Tradelist: \\n" <\/span>+ <\/span>xStaticVarGetText<\/span>( <\/span>"TWSTradelist" <\/span>) + <\/span>"\\n"<\/span>, <\/span>"" <\/span>);\r\r<\/span>CSVStr <\/span>= <\/span>WriteIf<\/span>( <\/span>ShowCSVTradelist<\/span>, <\/span>"\\nCSV Exported Tradelist: \\n" <\/span>+ <\/span>xStaticVarGetText<\/span>( <\/span>"CSVTradelist" <\/span>), <\/span>"" <\/span>);\r\r<\/span>Title <\/span>= <\/span>TWSStr <\/span>+ <\/span>CSVStr<\/span>;\r<\/span><\/pre>\n

Edited by Al Venosa.<\/p>\n","protected":false},"excerpt":{"rendered":"

Please be sure you set up the TWS according to the instructions provided in Setting Up your TWS before you test this code. The code presented here reads the execution report, converts it to a .csv format, date-stamps it, backs it up for later use, and optionally displays it in the chart Title. The code […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[64],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1556"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=1556"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1556\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=1556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=1556"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=1556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}