Contract Details

Contents

Initialize session with TWS

Typically, before a call to subscribe to market data, a request for contract details is made to obtain a list of valid exchanges.

Furthermore, note that not all exchanges will provide level II data. Generally, only ARCA, BATS, ISLAND, BEX provide market depth.

% initialize session with TWS
session = TWS.Session.getInstance();

% create local buffer for contract details events
[buf,lh] = TWS.initBufferForEvent(TWS.Events.CONTRACTDETAILS);

% create an empty stock contract
contract = com.tws.ContractFactory.GenericStockContract('SPY');

% connect to TWS
session.eClientSocket.eConnect('127.0.0.1',7496,0);
added interface method: TWSNotification
notification listener has been added
Server Version:75
TWS Time at connection:20150429 19:13:17 EST

Make request for contract details for SPY

session.eClientSocket.reqContractDetails(0,contract); pause(2);

Process contract details events

% Contract details have a lot going on so make sure to look through the references listed below.
% There are instances when contract details returns many details objects therefore a HashSet<ContractDetails> is returned.
buf.get().data

% the hash set of com.tws.ContractDetails objects can be easily converted to a cell array
details = collection2cell(buf.get().data);
 
ans =
 
[conid = 756733
symbol = SPY
secType = STK
expiry = null
strike = 0.0
right = null
multiplier = null
exchange = SMART
primaryExch = ARCA
currency = USD
localSymbol = SPY
tradingClass = SPY
marketName = SPY
minTick = 0.01
price magnifier = 1
orderTypes = ACTIVETIM,ADJUST,ALERT,ALGO,ALLOC,AON,AVGCOST,BASKET,COND,CONDORDER,DARKONLY,DARKPOLL,DAY,DEACT,DEACTDIS,DEACTEOD,DIS,GAT,GTC,GTD,GTT,HID,IBKRATS,ICE,IOC,LIT,LMT,LOC,MIT,MKT,MOC,MTL,NGCOMB,NODARK,NONALGO,OCA,OPG,OPGREROUT,PEGBENCH,POSTONLY,PREOPGRTH,REL,RTH,SCALE,SCALEODD,SCALERST,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,SWEEP,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF
validExchanges = SMART,CBOE,ISE,CHX,ARCA,ISLAND,VWAP,IBSX,DRCTEDGE,BEX,BATS,EDGEA,LAVA,CSFBALGO,JEFFALGO,BYX,IEX,TPLUS2,PSX
underConId = 0
longName = SPDR S&P 500 ETF TRUST
contractMonth = null
industry = null
category = null
subcategory = null
timeZoneId = EST5EDT
tradingHours = 20150429:0400-2000;20150430:0400-2000
liquidHours = 20150429:0930-1600;20150430:0930-1600
evRule = null
evMultiplier = 0.0
secIdList={}
]
 

the buffer can be purged of all events using clear

buf.clear();

Request contract details for Options

In a similar way, contract details for options can be requested.
% create generic contract for IBM
contract = com.tws.ContractFactory.GenericStockContract('IBM');

% configure the contract for options
contract.m_secType = 'OPT'; contract.m_primaryExch = [];

% get all the details for this option
session.eClientSocket.reqContractDetails(0,contract); pause(10)

Note that a contract details object with be returned for each strike price and expiry!

Sometimes can take a long time to get options contract details back so busy wait ...

while(buf.isEmpty); pause(1); end

% turn hashset of ContractDetails into cell array of ContractDetails
details = collection2cell(buf.get().data);

% check out how many contract details events were returned
numel(details)
ans =

   840

% get a list of all expiry dates for this option
expiryDates = cellfun(@(d)char(d.contractDetails.m_summary.m_expiry),details,'UniformOutput',0);

% get list of unique expiry dates for this option
unique(expiryDates)
ans = 

    '20150501'
    '20150508'
    '20150515'
    '20150522'
    '20150529'
    '20150605'
    '20150619'
    '20150717'
    '20151016'
    '20160115'
    '20170120'

See Also

TWSMarketDepthExample | TWSMarketDataExample

References

Interactive Brokers API:

TWS@Github:

Apache Commons: