Matlab和IB

Discussion in 'Julia / MATLAB / SAS' started by wj2000, Oct 23, 2009.

  1. http://www.tradingwithmatlab.com/video-tutorials

    Pairs Trading Video Tutorial 6/11/2009 19:19:04 - This tutorial walks you through building, backtesting and optimizing a pairs trading strategy in Matlab. Enjoy!



    Intraday Data Acquisition with IB's TWS6/16/2009 22:21:58 - This tutorial walks you through getting intraday data into your workspace using Interactive Broker's TWS API and Matlab.

    Connecting to Interactive Brokers TWS API
    It is possible to retrieve historical data (and real time data) in Matlab via Interactive Broker’s Trader Work Station API. To do this you will need an Interactive Brokers account. IB currently grants access to up to 6 months of historical intraday data. If this is an option you would like to pursue, you should watch Max Dama’s video tutorial at the link below:

    下面这个站点需要翻墙:p
    http://www.maxdama.com/2008/12/interactive-brokers-via-matlab.html
     
  2. 下面的站点需要翻墙
    Interactive Brokers via Matlab
    http://www.maxdama.com/2008/12/interactive-brokers-via-matlab.html


    More Matlab/IB Code
    http://www.maxdama.com/2009/02/more-matlabib-code.html
    More Matlab/IB Code
    Here is some more Matlab code to interact with Interactive Brokers' API. Check the previous tutorial on connecting Matlab to IB if this is new to you and of interest.

    This script, sys3.m, is what I run to execute all the IB commands. It defines the futures contracts, turns them into IBContract objects, connects to TWS, downloads historical data, and then processes the data into usable periodic return format. This is not intended to trade real time so do not expect that functionality; however adding that yourself would not be difficult with this as an example.

    Here are all the files you need (if something's missing, leave a comment please):
    sys3.m (same as above)
    connecttws.m
    loaddatatws2.m
    preprocess.m
    twsevent.m (very important)

    To test it, download the files into the current Matlab directory, run Matlab, run and login to TWS and wait for it to fully load, then at the prompt type > sys3 and hit enter. Wait until it has said "end" seven times- it's currently configured to download historical data for seven futures, 'VIX' 'ZB' 'YC' 'YM' 'GE' 'ES' 'HE' - "end" signals each data stream download finishing. Historical data from IB will then be in the variable "returndata", ordered by security definitions, so you can play around with it. Look through the code to see what else is going on.

    If it keeps saying "waiting", that means there is a problem receiving the data so you probably need to cut it off. On my computer it does not say waiting more than a few times, if any. For those who are interested, it waits for each data stream to arrive before requesting new ones because it is received asynchroniously and this is the simplest way to keep straight which is which.

    In sys3.m you can see that I had to tediously enter the contract details for each future from IB's website by hand. I have another function which does this automatically, but it has not been integrated into the main script yet. After running the sys3 script, run this one- reqdet.m (abbrev. for "requestdetails.m"). In the variable "secTest", in Matlab, you will find fully completed contract information for the securities. This only requires specifying the symbols for each security you want in a list and the expiration data for all of them. reqdet.m will make it much easier to download data from a huge list of futures.

    I could use help from anyone else working on Matlab/IB in answering two questions:
    1) How do you find out which futures/expiries have decent volume? Futures are relatively new to me and compared to stocks, there is no "volume" number.
    2) How do you create a stream of historical data that goes back a long time, over multiple expiration dates? I've looked up "continuous contract" and it sounds like what I want, but the implementation is not simple because contracts don't all have the same expiration dates. Perhaps you know of another data source that provides this already (does IB?)?

    Also, please share any scripts and funtions you have created to do similar things. I would especially like to see alternative event handler functions. I welcome questions/comments too.
     
  3. 嗯,不错不错,收藏一下
    MATLAB就是我的终极powershouse,
    不过目前还不必动用,Excel在前面已经很不错了
     
  4. 一直用matlab来分析,无比强大,空间无限
     
  5. 链接好像翻墙也登陆不了,请求WJ2000帮助,谢谢