Search This Blog

Wednesday, August 18, 2010

Matlab IB Historical Data

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.

Tuesday, August 17, 2010

Matlab and MySQL

Today I wanted to talk about how to configure MySQL and Matlab Database Toolbox so you can access an SQL server directly from Matlab. I am posting these instructions because when I was doing this I had to look through about 40 web sites to find the information I needed to configure the Database Toolbox because Matlab didn't feel like writing decent documentation. Just to let everyone know, I mainly use Windows, so these instructions may not be very useful if you are interested in using another OS, but I hope they help.

1. Install Matlab, including the Database toolbox on your computer.

2. Install MySQL on your computer. The latest release can be downloaded from If you are unfamiliar with MySQL, install using the default settngs. When you are prompted to set a passoword, this is for the root (administrator) account.  You can use the root account as your primary login for MySQL, which will give you every possible privilige.  However, it is generally considered good practice to only use the root account for administrative purposes, and to set up and use a separate login with limited privileges for everyday use.

3. Make sure Java is installed on your computer, as this is how Matlab will communicate with MySQL. If it is not installed, it can be downloaded from

4. Download the MySQL Connector/J from This is the MySQL Java driver. The driver can be stored anywhere you want, but I recommend navigating to the Matlab installation files, and placing the driver in the Java directory. On my computer, the file is located in the following location:  C:\Program Files\MATLAB\R2009a\java\mysql-connector-java-5.1.12-bin.jar.

5. Associate the driver with Matlab. This is done by adding the location of the driver to the classpath file. To do this, in the Matlab command line type 'edit classpath.txt'. On the classpath file, type the following on a separate line: '$matlabroot/java/mysql-connector-java-5.1.12-bin.jar'. $matlabroot is Matlab's way of abbreviating the folder that matlab is installed in, so for example, on my computer, $matlabroot is the same as C:\Program Files\MATLAB\R2009a\. Using $matlabroot is better than typing the entire path because Windows allows spaces in path names, but sometimes they can cause problems in Matlab. If you are installing a different version of the driver than I am, remember to put the correct name of the driver in the classpath file.

6. Restart your computer.

7. If you do not have a database setup in MySQL, create a dummy database and table for testing your connection. The following commands can be typed into the MySQL command line to make the dummy database and variable:

     create database testdb;
     create table testtbl ( var1 varchar(8),var2 int);
     insert into testtbl VALUES ( 'hello',7);

8. In the Matlab command line, type 'confds'. This command brings up a window that will allow you to configure a data source. Select the 'Create New File...' option and name the configuration file that will store the connection information, then click 'OK'. In the Name field, designate the name you want for the data source. In the driver field, type 'com.mysql.jdbc.Driver'. In the URL field, type 'jdbc:mysql://localhost/testdb'. Click on 'Add/Update'. If you have a database that is located on a different computer, replace 'localhost' in the URL field with the location of the database.  I have only used a database located on the same machine as the Matlab installation, so If anyone can verify this last bit about connecting to a database located on a different computer I would appreciate the feed back.

9. Test the connection by clicking 'Test'.  You will be prompted to type the username and password. If you are logging in using the root account, use 'root' as username, and whatever password you set.

10. Open the Visual Query Builder by typing 'querybuilder' into the Matlab command line. If the name of your database does not show up in your in the data sources, click Query -> Load, and load the configuration file. You may be asked to enter your username and password for the database again.  From here you can play around with VQB and see how it works.

Now that the connection has been set up between Matlab and MySQL, you can either use the VQB, or use the database functions. I recommend either using the database function, or building your own GUI that does what you want it do do, as the VGB GUI has a number of limitations. I will probably post more later on how define tables in the database and exactly how to use the database toolbox to import/export data to/from  the database. Good luck!