{"id":1493,"date":"2007-12-24T18:28:54","date_gmt":"2007-12-24T18:28:54","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/12\/24\/high-frequency-automated-trading-hfat-part-2\/"},"modified":"2007-12-26T21:37:48","modified_gmt":"2007-12-26T21:37:48","slug":"high-frequency-automated-trading-hfat-part-2","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/12\/24\/high-frequency-automated-trading-hfat-part-2\/","title":{"rendered":"High-Frequency Automated Trading (HFAT); part 2"},"content":{"rendered":"
Just like with price data, volume data are subject to delays and BF (Backfill) corrections. Moreover, IB (Interactive Brokers) reports volume data in a manner that could cause major performance differences between backtesting and actual trading. <\/p>\n
This post outlines simple procedures to collect RT and BF data for comparison. No effort is made to explain the differences or to perform statistical analysis. The views expressed here are based on personal experiences and\/or may be anecdotal; not everything that happens in real-time trading is easy to explain. As always, if you have technical insight and\/or see inaccuracies, please comment for the benefit of future readers. <\/p>\n
As expected, IB RT volume data contain the usual bad ticks and delays that are corrected during backfill. However, and this is very important to the RT trader, IB adjusts live volumes at about 30-second intervals. This means that the volumes IB reports during RT trading do not accurately reflect market activity. This means also that volume data may be delayed by up to 30 seconds, instead of by the typical snapshot delay, which is about 300 milliseconds for price data. Comparing backfilled with real-time volume, it appears that the real-time periodic volume adjustments are re-distributed across individual snapshots during backfill. This post is intended to help you perform your own data analysis. The methods outlined below are intended to get you started. <\/p>\n
To collect and save real-time data: <\/p>\n
The first thing that will happen when you connect to the TWS is that AmiBroker backfills approximately 2000 bars of 5-second data. This cannot be prevented and you must be careful to note the time where backfill ends and raw data collection starts. The simplest way is to place a vertical line on your chart and label it \u201cStart of real-time data\u201d. <\/p>\n
To save the database: <\/p>\n
Be sure to set the Database Settings -> Data Source -> Local<\/i> before saving. If you do not do this the database will backfill on the next startup and this may corrupt your RT data sample. <\/p>\n
The next step is to collect a sample of BF data that overlaps the previously collected real-time sample. To do this, you need to create another database. Since IB backfills only about 2000 bars of 5-second data, you should do this as soon as possible after collecting raw data, else the collection periods may not overlap and you will not be able to compare the two types of data. The procedure is the same as above except that you want to embed \u201cBF\u201d (for backfilled data) instead of \u201cRD\u201d in the database name. <\/p>\n
To visually compare the two databases you can open two instances of AmiBroker and load the RT database in one and the BF database in the other. You can then display the two databases at the same time and visually compare the respective charts. You may want to display both a price chart and a volume chart in separate panes, as shown in the captures below. <\/p>\n
You can use the code below to inspect your price chart: <\/p>\n
<\/span><<\/span>b<\/span>><\/span>Plot<\/span><\/<\/span>b<\/span>>(<<\/span>b<\/span>><\/span>C<\/span><\/<\/span>b<\/span>>,<<\/span>b<\/span>><\/span>"Close"<\/span><\/<\/span>b<\/span>>,<<\/span>b<\/span>><\/span>colorBlack<\/span><\/<\/span>b<\/span>>,<<\/span>b<\/span>><\/span>styleBar<\/span><\/<\/span>b<\/span>>); <<\/span>p<\/span>><\/span>TN<\/span>=<<\/span>b<\/span>><\/span>TimeNum<\/span><\/<\/span>b<\/span>>(); <<\/span>p<\/span>><\/span>Cursortime <\/span>= <<\/span>b<\/span>><\/span>SelectedValue<\/span><\/<\/span>b<\/span>>(<\/span>TN<\/span>); <<\/span>p<\/span>><\/span>CumHL <\/span>= <<\/span>b<\/span>><\/span>Cum<\/span><\/<\/span>b<\/span>>(<<\/span>b<\/span>><\/span>IIf<\/span><\/<\/span>b<\/span>>(<\/span>TN<\/span>&<\/span>gt<\/span>;=<\/span>CursorTime<\/span>,<<\/span>b<\/span>><\/span>H<\/span><\/<\/span>b<\/span>>-<<\/span>b<\/span>><\/span>L<\/span><\/<\/span>b<\/span>>,<<\/span>b<\/span>><\/span>0<\/span><\/<\/span>b<\/span>>)); <<\/span>p<\/span>><<\/span>b<\/span>><\/span>Plot<\/span><\/<\/span>b<\/span>>(<\/span>CumHL<\/span>,<<\/span>b<\/span>><\/span>""<\/span><\/<\/span>b<\/span>>,<<\/span>b<\/span>><\/span>4<\/span><\/<\/span>b<\/span>>,<<\/span>b<\/span>><\/span>styleArea<\/span><\/<\/span>b<\/span>>|<<\/span>b<\/span>><\/span>styleOwnScale<\/span><\/<\/span>b<\/span>>); <<\/span>p<\/span>><<\/span>b<\/span>><\/span>Title<\/span><\/<\/span>b<\/span>>=<<\/span>b<\/span>><\/span>Name<\/span><\/<\/span>b<\/span>>()+<<\/span>b<\/span>><\/span>" Interactive Brokers BackFilled price data - "<\/span><\/<\/span>b<\/span>>+<<\/span>b<\/span>><\/span>Interval<\/span><\/<\/span>b<\/span>>(<<\/span>b<\/span>><\/span>2<\/span><\/<\/span>b<\/span>>); <\/span><\/pre>\nAnd this code to inspect your Volume chart: <\/p>\n
<\/span><<\/span>b<\/span>><\/span>Plot<\/span><\/<\/span>b<\/span>>( <<\/span>b<\/span>><\/span>Volume<\/span><\/<\/span>b<\/span>>,<<\/span>b<\/span>><\/span>""<\/span><\/<\/span>b<\/span>>,<<\/span>b<\/span>><\/span>2<\/span><\/<\/span>b<\/span>>,<<\/span>b<\/span>><\/span>styleOwnScale<\/span><\/<\/span>b<\/span>>|<<\/span>b<\/span>><\/span>styleHistogram<\/span><\/<\/span>b<\/span>>|<<\/span>b<\/span>><\/span>styleThick<\/span><\/<\/span>b<\/span>>); <<\/span>p<\/span>><\/span>TN<\/span>=<<\/span>b<\/span>><\/span>TimeNum<\/span><\/<\/span>b<\/span>>(); <<\/span>p<\/span>><\/span>Cursortime <\/span>= <<\/span>b<\/span>><\/span>SelectedValue<\/span><\/<\/span>b<\/span>>(<\/span>TN<\/span>); <<\/span>p<\/span>><\/span>CV <\/span>= <<\/span>b<\/span>><\/span>Cum<\/span><\/<\/span>b<\/span>>(<<\/span>b<\/span>><\/span>IIf<\/span><\/<\/span>b<\/span>>(<\/span>TN<\/span>&<\/span>gt<\/span>;=<\/span>CursorTime<\/span>,<<\/span>b<\/span>