How do I use trading toolbox to get IB option chain?

Hi, I thought I might try getting the option chain for the RUT index (Russell 2000) via a partially completed ibContract as described in the documentation for the IB API interface here: http://interactivebrokers.github.io/tws-api/contract_details.html#gsc.tab=0
I coded the following:
optContract = ib.Handle.createContract;
optContract.symbol = 'RUT';
optContract.secType = 'OPT';
optContract.exchange = 'CBOE';
optContract.currency = 'USD';
optionChain = contractdetails(ib, optContract);
disp('Option contract details');
disp(optionChain)
The display of the optionChain shows:
Option contract details
marketName: 'RUT'
minTick: 0.0500
priceMagnifier: 1
orderTypes: 'ACTIVETIM,ADJUST,ALERT,ALLOC,AON,AVGCOST,BASKET,CON…'
validExchanges: 'SMART,CBOE,CBOE2'
underConId: 416888
longName: 'Russell 2000 Stock Index'
contractMonth: '201612'
industry: 'Indices'
category: 'Broad Range Equity Index'
subcategory: '*'
timeZoneId: 'CST'
tradingHours: '20160705:0830-1515;20160706:0830-1515'
liquidHours: '20160705:0830-1515;20160706:0830-1515'
summary: [1x1 Interface.AE6A66F3_8FA9_4076_9C1F_3728B10A4CC7]
secIdList: []
cusip: ''
ratings: ''
descAppend: ''
bondType: ''
couponType: ''
callable: 0
putable: 0
coupon: 0
convertible: 0
maturity: ''
issueDate: ''
nextOptionDate: ''
nextOptionType: ''
nextOptionPartial: 0
notes: ''
evRule: ''
evMultiplier: 0
Seems that the request recognized the RUT, and I do see a contract month for 201612, but nothing else that shows the various option contract details for the various strikes and expirations.
I was hoping that I would get back an array of option contract details so that I could then iterate over them and use the name of each contract (e.g., 'RUT 160819C01180000') on a getdata request to obtain the bid and ask price for each option. Any ideas on how I can do this?
If the Matlab trading toolbox contractdetails function does not support this, what other method could anyone suggest to get the option chain?

6 Comments

OK, I'm starting to understand how this works. I need to use an event handler which gets called for each option contract in the chain, and I also need to retrieve the option contract in the handler. So, the contractdetails call is now:
optionChain = contractdetails(ib, optContract,@ibSbtEventHandler);
And my simple event handler for now is:
function ibSbtEventHandler(varargin)
%IBSBTEVENTHANDLER Interactive Brokers' Trader Workstation SBT event handler.
myCD = get(varargin{5}.contractDetails);
disp('myCD:')
disp(myCD)
myCdSummary = get(myCD.summary);
disp('myCdSummary')
disp(myCdSummary)
end
The output from the disp has:
myCD:
marketName: 'RUTW'
minTick: 0.0500
priceMagnifier: 1
orderTypes: 'ACTIVETIM,ADJUST,ALERT,ALLOC,AON,AVGCOST,BASKET,CON…'
validExchanges: 'SMART,CBOE,CBOE2'
underConId: 416888
longName: 'Russell 2000 Stock Index'
contractMonth: '201608'
industry: 'Indices'
category: 'Broad Range Equity Index'
subcategory: '*'
timeZoneId: 'CST'
tradingHours: '20160705:0830-1515;20160706:0830-1515'
liquidHours: '20160705:0830-1515;20160706:0830-1515'
summary: [1x1 Interface.AE6A66F3_8FA9_4076_9C1F_3728B10A4CC7]
secIdList: []
cusip: ''
ratings: ''
descAppend: ''
bondType: ''
couponType: ''
callable: 0
putable: 0
coupon: 0
convertible: 0
maturity: ''
issueDate: ''
nextOptionDate: ''
nextOptionType: ''
nextOptionPartial: 0
notes: ''
evRule: ''
evMultiplier: 0
myCdSummary
conId: 239011698
symbol: 'RUT'
secType: 'OPT'
expiry: '20160812'
strike: 990
right: 'P'
multiplier: '100'
exchange: 'CBOE'
primaryExchange: ''
currency: 'USD'
localSymbol: 'RUTW 160812P00990000'
tradingClass: 'RUTW'
includeExpired: 0
comboLegs: []
underComp: []
comboLegsDescrip: ''
secIdType: ''
secId: ''
The trick was to realize that the contract details contains a field called summary which is an interface to get the actual contract.
So, I made good progress and my next step will be to use some filtering on the contractdetails request to limit the number of contracts returned to something more manageable. After that, I will need to change the event handler to build an array of the filtered option contracts which I can use later to obtain the prices.
Can you briefly talk about what is an event handler?
I have the same question raised by Scott Tuttle and I don't know how to extract option chain data.
Hi KAI YIN CHAN,
When you make a request to the ib API for information via the contractdetails method, the API will send the information back to you by calling an event handler. The event handler is simply a method that receives the returned information and places it into variables as needed. The trading toolbox has its own event handlers, but you can override those to use your own custom event handler in case you need that. I needed my own event handler because I made a request that the default trading toolbox event handler does not support.
You can open the trading toolbox contractdetails file to see how the default event handler code works (it's in the contractdetails file as function ibBuiltInContractDetailsEventHandler).
To get the option chain you will need to write your own event handler. As part of doing that, you will probably want to also register the contractDetailsEnd event and handle that event in your event handler. It tells you that the information is now complete, meaning that all of the option chains have now been returned.
can you explain how to register the contractDetailsEnd event?
Would also like to see this question answered!
G_Ib.Handle.registerevent({'contractDetailsEnd',@SbtContractDetailsEventHandler});

Sign in to comment.

 Accepted Answer

Using the help of @Scott Tuttle i wrote a function to download the option chain. It can be downloaded at FEX.

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Products

Asked:

on 5 Jul 2016

Answered:

on 7 Mar 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!