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.


  1. hi
    well explained article. But I am facing a problem while running the demo file. I am getting this error message:

    Matlab connected to TWS.
    Unknown event: openOrderEnd
    Market data farm connection is OK:ibdemo
    HMDS data farm connection is OK:demohmds
    Historical Market Data Service error message:Starting time must occur before ending time

    Is this the bug or am doing something wrong. Plz reply here or

  2. Try adding breakpoints in the demo file and just executing one portion at a time. It may be that it is trying to download data before the market data service is properly configured, which would cause an error.

  3. ??? Error using ==> actxcontrol at 183
    Control creation failed. Invalid ProgID 'TWS.TwsCtrl.1'

    i keep getting this error, any idea?

  4. One likely thing that could be a problem is the version of matlab you are using. Matlab is notorious for having scripts break when changing versions. This code is not actively being maintained by myself or the community where I found it. I am now using a java based matlab-interactive brokers program. See my IbMatlab API Functions post to get some step by step instructions on how to set it up. This code came from an individual that maintains, and it is getting pretty good, definitely a lot more stable than the ActiveX stuff. Make sure to download the latest version.

  5. Hi,

    Do you get any OPT (option) data out with this script? I always get the same error: Error validating request:-'gc' : cause - When the local symbol field is empty, please fill all option fields (right, strike, expiry). However, all of these parameters ARE filled and with the same parameters, IB to Excel interface (TwsActiveX.xls) works just fine for OPT. Also, your code runs just fine if STK is chosen instead of OPT. ANY ADVICE?

    ***Data used in tws_conn_hdata_demo***
    TickerSymbol = 'RIMM'; % Ticker Symbol for security being requested
    SecurityType = 'OPT'; % What kind of security to request, e.g. stock, option, future, forex, comodity
    contract.expiry = '201107'; %The expiration date. Format: YYYYMM (string)
    contract.strike = 30; % The strike price (double)
    contract.right = 'P'; %Valid values are: 'P' - PUT, 'C' - CALL; string.
    contract.multiplier = ''; % allows you to specify a future or option contract multiplier. This is only necessary when multiple possibilities exist
    Exchange = 'SMART'; % Which exchange to request data for - For stocks usually use SMART routing option
    Currency = 'USD'; % Which currency to request quotes in - necessary because some securities trade in several currencies
    %contract.localSymbol = 'RIMM'; %This is the local exchange symbol of the underlying asset
    EndDate = '201106216 21:30:00 GMT'; % Last timepoint to be downloaded
    Duration = '1 W'; % How long to look back from EndDate (S,D,W,M,Y)
    BarSize = '1 hour'; % Bar size can be 1,5,15,or 30 sec(s); 1,2,3,5,15, or 30 min(s); 1 hour; 1 day; etc
    %contract.primaryExch = 'NYSE'; % String Identifies the listing exchange for the contract*



  6. I've added Expiry, Strike, Right and Multiplier parameters in the Contract function and also the correspondin parameters in the run file. Still, I get the same error as in the previous post. Can anyone get OPT data out (IB/TWS to Matlab)? If yes, please respond.

    function Contract = tws_CreateContract(TickerSymbol,SecurityType,Expiry, Strike, Right,Multiplier,Exchange,Currency,tws)
    Contract = tws.createContract();

    Contract.symbol = TickerSymbol;
    Contract.secType = SecurityType;
    Contract.expiry = Expiry;
    Contract.strike = Strike;
    Contract.right = Right;
    Contract.multiplier=Multiplier; = Exchange;
    Contract.currency = Currency;

  7. Thank you so much for sharing your great work. Finally, I connected to IB using Matlab in a 32-bit OS. It is really helpful to me.

  8. I am in the middle of looking at backtesting options since I no longer have Easy Language available to me--Tradestation made me close my account when their employee shut me out of my own account and I complained and asked for the $755 I lost while the account was closed back. I have an IB account, but have heard they do not have continuous contracts to test futures on. How do you get around that? Is IB data sufficient to backtest, or are there better options? I am willing to try learning any programming language as long as I can afford it! Thanks!

  9. HI,

    I can not download the files, can somebody explain how I can get the files??

    Many thanks

  10. Hey,

    I also cannot see the files? Any chance on finding these somewhere else?

    Any help is much appreciated!