Friday, July 17, 2009

A free Matlab-to-Interactive Brokers API

For readers who do not want to pay for a commercial Matlab2IB API, Max Dama has put together a free alternative. Domenic has provided some additional sample Matlab codes for trading.

A user of the commercial product that I previously mentioned reports that "My problem with the matlab2ib product was that it did not have a function for all the Active X methods. ( for example the Market Scanners, Real time Bars and Fundamental Data methods are missing). I also had issues when I tried to steam in trades data(I'm not sure if the matlab2ib product allows you to even do this?)." Apparently Max's API has included these methods, though I have not personally tried them.

23 comments:

  1. I have tried the Activex route before purchasing the MATLAB2IB product. There is one major fundamental flaw when it comes to using ActiveX with matlab. Say,you are running an algorithm and you are processing a function, and at the same time TWS fires an Event. If you use ActiveX, MATLAB will NOT update the price until the processing of your function has completed. So Several Events will be missed and the price you would be looking would be a different one. Whereas in JAVA , there is no such problem. As any event fired will be immediately captured by java which is running in background. So when you call getLastPrice, you will get the correct price.

    Another flaw is obviously the fact that you can use ACtiveX ONLY with WINDOWS. Whereas with JAVA you can use it with windows, Mac, Linux etc

    It is NOT a good idea to stream in Live Trades data as it comes in into MATLAB. Imagine, you have 100 symbols, which updates every say 200 msec, so you have a trade happening so quickly and being captured and stored into matlab. due to MATLAB's single-threaded issue, some Trades ticks will be missed and also will eat up your memory. So all that you will be able to do is just to stream in data and not do anything else.

    ReplyDelete
  2. It's good to be a part of this community as a rookie, thanks Ernie!

    ReplyDelete
  3. Hi

    I'm torn between what option to choose.

    I only have Matlab R2007b so I can't use the classdef functions that were introduced in 2008a. Hence events have to be sent to separate M file for processing which is annonying.

    Then as Anonymous points out you can miss events whilst Matlab is processing.

    However, in a similar vein to Ernie's MATLAB2IB example I have been calling the prices from IB via the snapshot method.

    For example set up your stock with:
    c=ib.createContract; c.symbol=char('GOOG'); c.secType=('STK'); c.exchange=('Smart'); c.currency=('USD')

    Then call ,in a time based loop,
    ib.reqMktDataEx(101, c, '', 1) with "1" being a snap shot of last, bid, ask etc.

    This seems to work OK.

    But I haven't written any place order or check cash balance code yet so I'm not sure how that will go.

    I am also currently trialing MATLAB2IB.

    The demo only allows access to certain functions and stocks. It works only for IBM, AAPL and ES symbols. Ironically their TestAllCommands example looks for stock code EEM which isn't available in the demo.

    Also the request historical data function is missing which I would have thought was a key demo function to show.

    However the demo does work as described - also it was very easy to install understand.

    So I suppose I am at the point where I either pay $300 to get a 3rd party module that does what I need now (but later I can see I would like Real Time Bars which MATLAB2IB doesn't have) or pay a couple of thousand and get R2009a and then write a full ActiveX version using classdef to get better event handling.

    But I guess another option is to write a JAVA interface that does what I want. And when I say write - I mean I'll do the skelton and then hire someone via rentacoder.com to do all the grunt work. I have been very impressed with the quality and price of the work I have had done there to date.

    I will ask MATLAB2IB what their long term plans for the product is- if they are committed to supporting all IB API JAVA functionality I guess I'd stay with them. And yes I would pay more for enhanced functionality.

    Another site where there is some good Matlab ActiveX IB code is http://leptokurtosis.com/main/
    You have to register to see the code but that was no hassle.

    Thanks Ernie for this blog.

    ReplyDelete
  4. Hi

    We are releasing the MATLAB2IB V2 which will have the RealTime Bars, Scanner functions and reuters fundamental data . We could NOT test the reuters fundamental data function as we do not have access to reuters. we Have a tentative date of August 1st.

    Anonymous: We do Provide additional functions inside the demo on a as needed basis. So email at info@exchangeapi.com so that we can send you the functionality.

    Regarding ActiveX: We actually have a COMPLETED version of ActiveX but we never released it due to various reasons. Performance being one of the main reasons.

    ReplyDelete
  5. Hi

    When you do pairs trading, do you analysis min data, hour data or daily data?

    And when you do cointergration, do you always analysis data from the very beginning?

    thx in advance.

    ReplyDelete
  6. Sorry for the late reply; but...

    How fast is the 2IB product for pulling in data? I need to pull in second-by-second bid and ask prices for 50 to 100 liquid stocks, the matrices of which would get run through a matlab routine every minute or so. Trading would occur sporadically but never any more often than ever minute or more. Is the 2IB application (or even the ActiveX methods) and TWS capable of handling this without freezing up, or would I need to look into another data source even if executing through Interactive Brokers?

    ReplyDelete
  7. Todd,
    I think you can get 50 stock prices per second, but not faster than that.
    You can ask IB customer support for their API "message rate".
    Ernie

    ReplyDelete
  8. Ernie, much thanks. I did contact interactive brokers about what I wanted to do and they replied with:

    "... essentially the max ticker limitations is set by the messages you sent not the events fired back. when you send the 100 market data requests to IB. This is what will need to be paced which technically means if you wanted 100 market data request streaming then the fastest rate you can send this request to IB is request 50 contracts, wait one second and request 50 additional contracts. The events fired back and the tickTypes you received is not limited or filtered in anyway"

    So, it seems that I should be able to keep streaming in the hundred stock names and at least have the info available ever second as needed. I'll play around with seeing if it can actually be captured on a second-by-second basis efficiently.

    Thanks again, regardless.

    ReplyDelete
  9. hi Max,
    Thanks for your blog, smart content.

    I dont get why you need to get all that when you can directly call and use the Java API provided by IB, Matlab works beautifully with Java, one of the great quality of Matlab.No need another API.

    ReplyDelete
  10. Sorry not Max, Ernie ;-), I got confused.

    I liked your book by the way.

    ReplyDelete
  11. Dam,
    Yes, you are probably right about calling Java from Matlab. In fact, the API's that I use does exactly that. Many of us are just too lazy to look into the native Java API of IB and figure out how to call it from Matlab.

    Maybe you can ask Max if he agrees with your point?
    Ernie

    ReplyDelete
  12. 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.
    http://finance.jasonstrimpel.com

    ReplyDelete
  13. I don't agree with the 'missed ticks' issue. See my blog for details.

    http://matlab-trading.blogspot.com/2010/11/activex-vs-java-api-whats-difference.html

    ReplyDelete
  14. HI Ernie
    I am trying to connect IB API on Matlab. I am able to connect to tws but when I try to get historical data I am getting following error in this command:
    tws.reqHistoricalDataEx(RequestID,Contract,EndDate,Duration,BarSize,’BID’,1,1);

    Historical Market Data Service error message:Starting time must occur before ending time

    EndDate = ’20110412 12:00:00′;
    Duration = ’7200 S’;
    BarSize = ’15 secs’;

    Can you please figure out and let me know what is the actual problem?

    regards
    Prashant

    ReplyDelete
  15. Hi Prashant,
    Your error does seem curious. You can send you complete Matlab function to the customer support at exchangeapi.com and they should be able to diagnose it.
    Ernie

    ReplyDelete
  16. You may be interested there is now a Matlab toolbox that enables FIX messaging (MATFIX). This toolbox allows you to trade with most brokers directly from Matlab, not just IB! See http://www.agoratron.com/our_products.html

    ReplyDelete
  17. You may be interested that there is now a Matlab toolbox that enables FIX communication (MATFIX). This means you can trade directly from matlab with any broker, not just IB! See http://www.agoratron.com/our_products.html

    ReplyDelete
  18. Anon,
    That is a very useful piece of info. Thanks for the link on MATFIX!
    Ernie

    ReplyDelete
  19. Hi to all

    First of all i'd like to thank Ernie for this really interesting blog.

    I'm looking for an advice and/or a comparison between the commercial MATLAB-IB API's. I'm a Matlab user but not a proffesional developer which means that I do not program in java or C# or C++, but only in Matlab. However, I have a fairly quantitative background and after a considerable research I concluded that IB is the most appropriate platform (in terms of costs, assets, etc) for me, as i would like to trade in my free time on a minute/five minutes frequency. Further, for all the above reasons i wouldn't mind paying for a commercial API.

    Could you please share any comments/experiences you had with:

    (1) IB-Matlab http://undocumentedmatlab.com/ib-matlab/

    (2) Quant2ib
    http://www.exchangeapi.com/freetrial.aspx

    (3) MATTICK
    http://www.agoratron.com/our_products.html

    Has anyone used before

    (4)CQG
    http://www.cqg.com/Support/FAQ/CQG-API-MATLAB-FAQ.aspx

    (5) RTS
    http://www.rtsgroup.net/

    (6) 4th story
    http://www.4thstory.com/

    Thanks
    John.

    ReplyDelete
  20. John,
    Of all the products you listed, I have only tried Quant2ib. It is excellent if you are generating signals only at the end of each bar (1min? 5min?), but not great if your strategy is event driven (e.g. whenever a price change occurs).

    Note that IB's native API is event-driven.
    Ernie

    ReplyDelete
  21. Hi Ernie,

    Great blog. Would you happen to know if there is a link to Max Dama's Matlab API for IB lying around somewhere?

    Cheers
    MM

    ReplyDelete
  22. MM,
    I am afraid Max has taken down his site permanently. However, if you google "Matlab IB API" you will find one that Jev Kuznetsov wrote.
    Ernie

    ReplyDelete