Interactive Brokers and Matlab

Discussion in 'Automated Trading' started by maxdama, Dec 28, 2008.

  1. maxdama


    Hi all,

    I wrote a tutorial on connecting Matlab to Interactive Brokers. I was wondering if anyone has experience using Matlab through IB's ActiveX API, and if so, could you share your TWS-event handler file. I would like to check my overall method of linking the two with someone else.

    I think Matlab is a good environment to develop strategies because a functional style is easy to debug and it has premade libraries so you can write code quickly. But unfortunately I haven't found anyone else who's actually gotten it to work with IB besides me so it's slow progress.

  2. estim


    Somewhere on ET you can find these two links which got me started:

    I register the events I am using as follows:

    ibtws.registerevent({'errMsg' 'event_errMsg'; ...
    'tickPrice' 'event_tickPrice'; ...
    'tickSize' 'event_tickSize'});

    And then writes the function for each:

    function event_tickPrice(varargin)

    eventstruct = varargin{end-1};

    if eventstruct.tickType == 4,
    ' id=',int2str(, ...
    ' lastPrice=',num2str(eventstruct.price), ...
    ' tickType=',int2str(eventstruct.tickType)])

  3. maxdama



    Thanks, this is exactly what I was hoping for. I also started with MatlabTrader and looked at ExchangeAPI.

    I would like to see the details of each event handler and other support code you've written (such as for creating contracts, etc.). As you may have seen near the bottom on the blog post, my twsevents.m file (which I use to handle all events) is not very complete. Do you think it would work to have the system's buy/sell logic in the tickprice event handler, so it analyzes each new data as it arrives?
    I will be putting my system's code on the blog in a few days if I can finalize it by then and I'd be interested in your criticism. Thanks again.

  4. estim


    Yes, just as NinjaTrader does with the CalculateOnBarClose() method which can be set to false/true depending if you run your strategy on tick data or bar data.
    I avoided to have everything in one file. Instead I created event files to match different strategies. So for the tick event I might have 5 different versions depending on the strategy I am running.

    However I am now running everything in NinjaTrader because I was using to much time on developing stuff in matlab.
  5. maxdama


    Ok thanks.
  6. domenic


    I'm trying to create a central repository for files/info for those trying to trade with matlab.

    It seems like most of the other sites have stale data......

    check it out: