Thursday, November 25, 2010

ActiveX vs Java API what's the difference?

There has been quite a discussion on Chans blog about ActiveX vs Java API. One of the claims presented by Anonymous is that ActiveX is missing ticks when another function is executing. This alarmed me as I rely heavily on the  ActiveX api.
So I've tested this claim: while tws events are handled in the background by the wrapper and listener classes, I start a ~10 second 100% cpu intensive task in the foreground. After this I examine the timestamps of each tick.
The results are in the figure:
On the horizontal axis is the logged tick number and tick timestamp is plotted vertically. The cpu is loaded between the red lines. This graph shows that during cpu load no ticks reach the listener class, however no events are lost as they are all fired up just after the cpu has some available time.
The other claims were quite vague as 'performance', as far as I'm concerned I've never had any performance issues using activeX. And last but not least, if you need 'performance' why do you use Matlab??? I would directly go for some native language and FIX CTCI api.

So I'm sticking with the ActiveX for now.

Monday, November 15, 2010

HOWTO: Wrap Interactive Brokers TWS api in a Matlab class.

There are many ways of interfacing Matlab with Interactive Brokers API. The main choice is of course whether you are going to buy a commercial product (like quant2ib) or go the DIY route (a-la Max Dama). For me it was the second option. The reason is that not only am I short of $600 for an interface, but in most cases a commercial product will have some limitations compared to your own or open-source code.
I've started with the code from Max that really got me a flying start, but his approach to interfacing is far from optimal. One would need a bunch of global variables and separate functions to get the data back and forth. Synchronization to events also becomes very difficult with this approach. Without object oriented design, one would quickly become bogged down in 'spaghetti' code.
So I went another route: wrapping all of the tws stuff in a single class and using events for synchronization. This was not trivial to do, as passing class methods as a callback function proved quite tricky.  With code from (thanks!) this issue was solved and I finally managed to create a descent wrapper.

Here is the base code for the wrapper :
It's a basic skeleton that can be extended further. At this moment only market data subscriptions  are implemented. To add order placing, historical data etc. you'll need to add some code, but from here on most of the heavy lifting is already done.

How to use:
- install tws ActiveX , enable api connection in tws
- start TWS
- open demoScript.m  and step through the code blocks to see what happens.

File description:
CTws - main wrapper class .
CSymbolData - used by CTws as a data container
CListener - basic example of a listener class, listening to tws events.
GenericIbEvent - used for passing symbol data within an event (written by
demoScript - basic tws/listener demo

Have fun!
Any remaks/improvements are welcome!