3

How can I use Matlab to get a list of a particular future symbol's individual contracts (in terms of expiration dates) from Interactive Broker's API? For instance, by providing the contract symbol for "ES" I would want a list of the contracts IB has for ES (not just the front contract).

This question has been answered for python (see second answer in thread), but I am looking for a Matlab solution.

Community
  • 1
  • 1
MikeD
  • 171
  • 2
  • 13
  • You can actually incorporate some java code into your m-files and call from matlab. I did it with javasocket in an m-file for example. – vindarmagnus Nov 20 '16 at 12:39

1 Answers1

1

If you use the IB-Matlab connector, then the answer is simple. For example, to get the list of all Futures-Options (FOPs) Call-side contracts that expire on 12/2018:

>> data = IBMatlab('action','contract', 'symbol','ES', 'secType','FOP', 'exchange','GLOBEX', 'expiry','201812', 'right','Call')
data = 
  567×1 struct array with fields:
    m_conId
    m_symbol
    m_secType
    m_expiry
    m_strike
    m_right
    m_multiplier
    m_exchange
    m_currency
    m_localSymbol
    m_primaryExch
    m_includeExpired
    m_secIdType
    m_secId
    ...

>> data(1)
ans = 
  struct with fields:
            m_conId: 300287365
           m_symbol: 'ES'
          m_secType: 'FOP'
           m_expiry: '20181221'
           m_strike: 1100
            m_right: 'C'
       m_multiplier: '50'
         m_exchange: 'GLOBEX'
         m_currency: 'USD'
      m_localSymbol: 'ESZ8 C1100'
      m_primaryExch: []
   m_includeExpired: 0
        m_secIdType: []
            m_secId: []
 m_comboLegsDescrip: []
        m_comboLegs: [0 java.util.Vector]
        m_underComp: []
          m_summary: [1×1 com.ib.client.Contract]
       m_marketName: 'ES'
     m_tradingClass: 'ES'
          m_minTick: 0.05
   m_priceMagnifier: 1
       m_orderTypes: 'ACTIVETIM,ADJUST,ALERT,ALLOC,AVGCOST,BASKET,COND,CONDORDER,DAY,DEACT,DEACTDIS,DEACTEOD,GAT,GTC,GTD,GTT,HID,IOC,LIT,LMT,LTH,MIT,MKT,MTL,NGCOMB,NONALGO,OCA,SCALE,SCALERST,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,VOLAT,WHATIF'
   m_validExchanges: 'GLOBEX'
       m_underConId: 289128563
         m_longName: 'E-mini S&P 500'
    m_contractMonth: '201812'
         m_industry: []
         m_category: []
      m_subcategory: []
       m_timeZoneId: 'CST'
     m_tradingHours: ';20180729:1700-20180730:1515;20180730:1530-20180730:1600;20180730:1700-20180731:1515;20180731:1530-20180731:1600;20180731:1700-20180801:1515;20180801:1530-20180801:1600;20180801:1700-20180802:1515;20180802:1530-20180802:1600;20180802:1700-20180803:1515;20180803:1530-20180803:1600;20180804:CLOSED;20180805:1700-20180806:1515;20180806:1530-20180806:1600;20180806:1700-20180807:1515;20180807:1530-20180807:1600;20180807:1700-20180808:1515;20180808:1530-20180808:1600;20180808:1700-20180809:1515;20180809:1530-20180809:1600;20180809:1700-20180810:1515;20180810:1530-20180810:1600;20180811:CLOSED;20180812:1700-20180813:1515;20180813:1530-20180813:1600;20180813:1700-20180814:1515;20180814:1530-20180814:1600;20180814:1700-20180815:1515;20180815:1530-20180815:1600;20180815:1700-20180816:1515;20180816:1530-20180816:1600;20180816:1700-20180817:1515;20180817:1530-20180817:1600;20180818:CLOSED;20180819:1700-20180820:1515;20180820:1530-20180820:1600;20180820:1700-20180821:1515;20180821:1530-20180821:1600;20180821:1700-20180822:1515;20180822:1530-20180822:1600;20180822:1700-20180823:1515;20180823:1530-20180823:1600;20180823:1700-20180824:1515;20180824:1530-20180824:1600;20180825:CLOSED;20180826:1700-20180827:1515;20180827:1530-20180827:1600;20180827:1700-20180828:1515;20180828:1530-20180828:1600;20180828:1700-20180829:1515;20180829:1530-20180829:1600;20180829:1700-20180830:1515;20180830:1530-20180830:1600;20180830:1700-20180831:1515;20180831:1530-20180831:1600;20180901:CLOSED'
      m_liquidHours: '20180729:CLOSED;20180730:0830-20180730:1515;20180730:1530-20180730:1600;20180731:0830-20180731:1515;20180731:1530-20180731:1600;20180801:0830-20180801:1515;20180801:1530-20180801:1600;20180802:0830-20180802:1515;20180802:1530-20180802:1600;20180803:0830-20180803:1515;20180803:1530-20180803:1600;20180804:CLOSED;20180805:CLOSED;20180806:0830-20180806:1515;20180806:1530-20180806:1600;20180807:0830-20180807:1515;20180807:1530-20180807:1600;20180808:0830-20180808:1515;20180808:1530-20180808:1600;20180809:0830-20180809:1515;20180809:1530-20180809:1600;20180810:0830-20180810:1515;20180810:1530-20180810:1600;20180811:CLOSED;20180812:CLOSED;20180813:0830-20180813:1515;20180813:1530-20180813:1600;20180814:0830-20180814:1515;20180814:1530-20180814:1600;20180815:0830-20180815:1515;20180815:1530-20180815:1600;20180816:0830-20180816:1515;20180816:1530-20180816:1600;20180817:0830-20180817:1515;20180817:1530-20180817:1600;20180818:CLOSED;20180819:CLOSED;20180820:0830-20180820:1515;20180820:1530-20180820:1600;20180821:0830-20180821:1515;20180821:1530-20180821:1600;20180822:0830-20180822:1515;20180822:1530-20180822:1600;20180823:0830-20180823:1515;20180823:1530-20180823:1600;20180824:0830-20180824:1515;20180824:1530-20180824:1600;20180825:CLOSED;20180826:CLOSED;20180827:0830-20180827:1515;20180827:1530-20180827:1600;20180828:0830-20180828:1515;20180828:1530-20180828:1600;20180829:0830-20180829:1515;20180829:1530-20180829:1600;20180830:0830-20180830:1515;20180830:1530-20180830:1600;20180831:0830-20180831:1515;20180831:1530-20180831:1600;20180901:CLOSED'
                ...

Note: you can remove the expiry and/or right parameters to get a larger list, but be warned that you might need to wait for quite a long time and get many thousands of results, so it's best to filter the results. You can also use the strike parameter to filter.

Yair Altman
  • 5,704
  • 31
  • 34
  • Is there a way to identify the current contract? – shashwat Mar 20 '20 at 04:16
  • @shashwat - assuming you mean the continuous (lead-edge) contract, simply set the 'SecType' parameter to 'CONTFUT' (not 'FUT'), as explained here: https://interactivebrokers.github.io/tws-api/basic_contracts.html – Yair Altman Mar 20 '20 at 08:18