Thursday 10 November 2011

NoSQL and the IB API

So, I must admit that designing database schema is not my favourite activity. I'd much rather be working on modelling and trade monitoring than constant auditing and maintenance.

I've switched from my previous SQL schema to using NoSQL as provided by MongoDB. Some may argue that this is a dangerous approach (Edit: the rebuttal) considering the requirements, but I enjoy the quick and easy document management and the simple efficient indexing of entries.

As I believe everyone is aware, Interactive Brokers offers a great service for small individual traders, but for those wanting to use their API, it's a bit of a nightmare. I've spent quite a lot of time working on a proprietary wrapper which is resistant to their frequent API updates. So I thought I'd share some code snippets.

Following from the NoSQL introduction, an example of recording data from my IB feed, all I have to do is run code such as the following:

//Connect to IB through API
FuzzIBConnection.get().connect();

//ES December 2012 Futures Contract
Contract contract = new Contract();
contract.m_secType = "FUT";
contract.m_symbol = "ES";
contract.m_localSymbol = "ESZ1";
contract.m_expiry = "201112";
contract.m_currency = "USD";
contract.m_exchange = "GLOBEX";

//Instantiate a handler for the security feed to record to the database
SecurityFeedRecorder sfr = new SecurityFeedRecorder(contract);

//Get the data feed from the IB API, and add the SecurityFeedRecorder object
//as a handler.
SecurityFeed securityFeed = FeedManager.getSecurityFeed(contract);
securityFeed.addHandler(sfr);

This snippet of code will then collect all of the information from the IB API regarding the ES futures contract, and record it to a database which I defined in my MongoDB management class.

In a little more detail: tick data is received live from the IB API in the SecurityFeed object, which then publishes this data to all of the handlers added to its handler list. In this case, there is only one handler, a SecurityFeedRecorder object. This SecurityFeedRecorder encapsulates organisation of where to store the data in the database with information obtained from the Contract object passed to its constructor.

At the moment I chose the system to default to run the database locally with the code, but if I decide to migrate to a dedicated machine, it's a simple case of including the line of code:

FuzzMongo.initMongo("127.0.0.1")

obviously replacing the localhost address with the ip of the host machine.

I don't just record security data, but also information regarding my account, contract details from IB, audit trails, trades made, as much as I can.

It's then incredibly easy and efficient to retrieve this data for manipulation. For quick and dirty analysis I can spit out csv files and start working with them in Excel. Plotting equity curves, sector exposure, contract details reports, etc etc.

1 comment: