Having descent historical data is essential for developing a good strategy. But access to (free) intraday data is almost non-existent. However, if you have an account at interactive brokers, they let you download historical data for free.
In the last couple of weeks I've been very busy with implementing an intraday arbitrage strategy. The strategy itself is very simple with zero (!!!) parameters, but the amount of code needed for data and error handling is much more than I've anticipated. Have been coding data handling and conversion for a couple of weeks now with one clear conclusion: I hate programming. So this is how it feels to be the low-level software engineer aka code-monkey ;-).
However, during this process I managed to get Matlab to talk to TWS through their API and written a historic data downloader that could spare some boring work for other people.
The code is based on tutorial written by Max Dama (thanks Max!). You should follow the steps described on his page to install and configure the tws api. After that just fire it up:
hDataIntraday = getHistoryTws(tickers, filename_copy ,period, barsize);
Keep in mind that IB imposes restrictions on data download : Historical Data Limitations
The data is downloaded to a structure:
hDataIntraday :
stocks: [1x20 struct]
ticker: 'AEE'
history: [1x1 struct]
dates: [253x1 double]
open: [253x1 double]
close: [253x1 double]
volume: [253x1 double]
ticker: 'AEP'
history: [1x1 struct]
dates: [253x1 double]
open: [253x1 double]
close: [253x1 double]
volume: [253x1 double]
last_update: '29-May-2010 16:07:21'
The code: tws_history_downloader.zip
I wrote a Web based application to download multiple stock price series on one spreadsheet. It is extremely useful for getting data into MATLAB, etc. where you need historical prices for 10, 15, 20 stocks on one sheet. This application allows the user to input many tickers to get the prices on one sheet without having to download separate price series and combining them. This can be useful to students or practitioners that do not have commercial software to do the same thing.
ReplyDeletehttp://finance.jasonstrimpel.com
Hey Thanks for posting your code. Can you give me a hand with a couple of errors I am getting? First, when I run the demo, when line 20 of getHistoryTws runs
ReplyDelete20- (tws.connect('', 7496, 1);)
I get the following message:
Unknown event: openOrderEnd
Market data farm connection is OK:usfarm
Then everything seems to execute until line 37
37- tws.reqHistoricalDataEx(requestId,contract,[datestr(today,'yyyymmdd') ' 22:00:00'],lookback,barsize,'TRADES',1,1);
which gives the following error:
??? Undefined function or variable 'today'
I have TWS and the IB API installed on my computer, and I get the prompt that asks me to accept the incoming connection. Any idea what isn't working? Thanks. Ryan
Just replace 'today' by 'date', then it should work ok.
ReplyDeleteDo you know if there are any problems with IB's reqHistoricalDateEx()? When I request one day of '5 min', '3 mins', '2 mins', '1 mins', and '30 secs' barsize their API sends me the data no problem, but if I try to request one day of '15 secs' barsize it throws the following error: Historical Market Data Service error message:invalid step: 15. Do you have any idea why I am getting this error?
ReplyDeleteHey, you can disregard my last post. I didn't realize that when you specify a barsize of 1 - 15 seconds, you have to specify the durationStr as seconds, not days, so you could use '23400 S', but not '1 D', even though there are 23400 seconds in 1 day.
ReplyDeleteHey Jev, I wanted to let you know that I took the demo code you posted for connecting to IB and downloading historical data via matlab and re-wrote parts of it to fit my needs. It is now 6 functions that are very easy to write a script with (Connect, Event, CreateRequestID, CreateContract, RequestHistory, and Disconnect), which are all on my blog (matlab-ib-trading.blogspot.com). I plan on posting most of the code I write and how to configure things as I figure it out. My blog is centered on the mechanics of how to use Matlab, MySQL, and IB API together to build a trading platform and manage historical data.
ReplyDelete@Riggster: Great! I was hoping that someone would take things further from here. I'll certainly take a look at your code, especially the SQL part as it is high on my wishlist.
ReplyDelete