Hello again. Today I wanted to talk about how to use Matlab to connect to Interactive Brokers via their IB API, and then download historical data. The setup for this is pretty straight-forward, and I will talk about that in a minute, but first a little bit about setting up an account. If you have $10,000 just sitting around, use it to fund a cash account at IB. There are plenty of perks that come with it. If you don't have that kind of cash, but are a high school or college student or professor, set up a free paper-trading account through their Student Trading Lab program. This gives you full access to their trading platforms, API platforms, and historical data. These are some incredible perks which normally are not accessible to the general public until they set up an account and fund it with at least $10K. If you don't have the cash, and you aren't in school, sorry but you won't be able to actually connect to IB. Don't let that stop you from learning what to do when you do have the cash though.
Let's get down to business. First of all, I just want to let everyone know that I am running Windows XP, and I will be using ActiveX controls, which only work in Windows. If you are using a different OS, they do have other ways to connect to their API, but I haven't used them yet. I may look into them at some point, but right that it's not very high on my priority list. With that said, here are the instructions for connecting Matlab to IB:
1. Make sure Matlab is installed on your machine. No toolboxes are required, just Matlab.
2. Download and install Trader Work Station (TWS) and IB API from Interactive Broker's web site.
3. Check to make sure that ActiveX is installed on your machine. This is the method we will be using to communicate between Matlab and the IB software.
4. Login to TWS and click on the Configuration Menu. In the API tab there should be an option to allow ActiveX and Socket Clients. This box needs to be checked.
5. Logout of TWS and restart your computer.
6. Login to TWS, and start Matlab.
7. Download this set of functions and place them in your current matlab directory. The demo file uses all of the functions to connect to TWS through the IB API, download some historical data for a single stock, and then disconnect. The data should will be stored in a struct called HistData. Here is a brief description of each of the functions:
tws_Connect.m - Creates an invisible figure, places an ActiveX component in it, and uses the ActiveX component to create a connection to TWS via the API.
tws_Disconnect.m - Closes the connection between Matlab and TWS.
tws_CreateRequestID.m - Uses the name of the security you want to request historical data for and creates a Request ID.
tws_CreateContract.m - Uses the information you specify about the stock of interest to create an ActiveX object used in the data request.
tws_RequestHistory.m - Sends the request to TWS to download the specified historical data, recieves, the data, and stores it in a convenient format.
tws_Event.m - You will never call this function directly, but it is very important. It coordinates the event handling.
tws_conn_hdata_demo.m - script that demos all of the above functions.
That's it! Now you can use the demo script as a starting point to write your own script that meets your needs. Notice I didn't save the data anywhere. I'll leave that up to you. My last post was on how to connect Matlab to a MySQL database, and I think my next post will be how to create tables in MySQL and store downloaded data in those tables using Matlab. I hope you enjoy this. As always, let me know if you have any suggestions on how to improve my methods. I am always interested in learning new tricks.