Trading Toolbox - Retrieve Option Chain from Interactive Brokers

5 views (last 30 days)
I'm trying to use the Interactive Brokers functionality below to return an option chain including all strike prices and expires for a given security. Is this possible to do this with Matlab's Trading Toolbox? I can't seem to make it work in Matlab R2017a. Thanks.
https://interactivebrokers.github.io/tws-api/options.html#option_chains

Accepted Answer

qmnjb007
qmnjb007 on 7 Sep 2017
Turns out this is very easy to do with Yahoo Finance...
function d = getchain(ticker)
% DESCRIPTION: This function retrieves all stikes prices and expiry dates
% from yahoo finance for a given ticker input.
%
% REFERENCES
% https://www.mathworks.com/matlabcentral/fileexchange/50455-getyahoooptionchain
% https://stackoverflow.com/questions/38680008/how-can-i-download-option-tables-using-the-yahoo-finance-api/40243903#40243903
data = webread(['https://query1.finance.yahoo.com/v7/finance/options/' ticker]);
expys = datetime(data.optionChain.result.expirationDates,'ConvertFrom','posixtime','Format','MM-dd-yyy')
strks = data.optionChain.result.strikes
end
  2 Comments
lab314
lab314 on 8 Jan 2018
The above works well, thanks for posting. I noticed it only returns 1 option series. The yahoo web page has a selection box for other series. Any ideas on how to read the other option series? Thanks.
David Hanuka
David Hanuka on 6 Dec 2021
Hi,
I am getting the following error:
>> data = webread(['https://query1.finance.yahoo.com/v7/finance/options/NVDA']);
Error using matlab.internal.webservices.HTTPConnector/copyContentToByteArray (line 373)
The server returned the status 403 with message "Forbidden" in response to the request to URL https://query1.finance.yahoo.com/v7/finance/options/NVDA.
Error in readContentFromWebService (line 46)
byteArray = copyContentToByteArray(connection);
Error in webread (line 125)
[varargout{1:nargout}] = readContentFromWebService(connection, options);
Is there a way to resolving this issue?
Thanks,
David

Sign in to comment.

More Answers (1)

Yair Altman
Yair Altman on 4 Jan 2019
Here's how you can fetch the full option chain using the IB-Matlab connector, for example all futures options for the 10-year US Treasury Note (ZN) that have a contract month of March 2019 (which trade on the ECBOT exchange):
>> dataStruct = IBMatlab('action','contract', 'symbol','ZN', 'secType','FOP', 'expiry','201903', 'exchange','ecbot')
dataStruct =
220×1 struct array with fields:
m_conId
m_symbol
m_secType
m_expiry
m_strike
m_right
m_multiplier
m_exchange
m_currency
...
>> dataStruct(1)
ans =
struct with fields:
m_conId: 322823677
m_symbol: 'ZN'
m_secType: 'FOP'
m_expiry: '20190222'
m_strike: 97
m_right: 'C'
m_multiplier: '1000'
m_exchange: 'ECBOT'
m_currency: 'USD'
m_localSymbol: 'C OZN MAR 19 9700'
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: 'OZN'
m_tradingClass: 'OZN'
m_minTick: 0.015625
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: 'ECBOT'
m_underConId: 322458860
m_longName: '10 Year US Treasury Note'
m_contractMonth: '201903'
m_industry: []
m_category: []
m_subcategory: []
m_timeZoneId: 'CST'
m_tradingHours: '20190102:1700-20190103:1600;20190103:1700-20190104:1600;20190105:CLOSED;20190106:1700-20190107:1600;20190107:1700-20190108:1600;20190108:1700-20190109:1600;20190109:1700-20190110:1600;20190110:1700-20190111:1600;20190112:CLOSED;20190113:1700-20190114:1600;20190114:1700-20190115:1600;20190115:1700-20190116:1600;20190116:1700-20190117:1600;20190117:1700-20190118:1600;20190119:CLOSED;20190120:1700-20190121:1600;20190121:1700-20190122:1600;20190122:1700-20190123:1600;20190123:1700-20190124:1600;20190124:1700-20190125:1600;20190126:CLOSED;20190127:1700-20190128:1600;20190128:1700-20190129:1600;20190129:1700-20190130:1600;20190130:1700-20190131:1600;20190131:1700-20190201:1600;20190202:CLOSED;20190203:1700-20190204:1600;20190204:1700-20190205:1600;20190205:1700-20190206:1600'
m_liquidHours: '20190103:0830-20190103:1600;20190104:0830-20190104:1600;20190105:CLOSED;20190106:CLOSED;20190107:0830-20190107:1600;20190108:0830-20190108:1600;20190109:0830-20190109:1600;20190110:0830-20190110:1600;20190111:0830-20190111:1600;20190112:CLOSED;20190113:CLOSED;20190114:0830-20190114:1600;20190115:0830-20190115:1600;20190116:0830-20190116:1600;20190117:0830-20190117:1600;20190118:0830-20190118:1600;20190119:CLOSED;20190120:CLOSED;20190121:0830-20190121:1600;20190122:0830-20190122:1600;20190123:0830-20190123:1600;20190124:0830-20190124:1600;20190125:0830-20190125:1600;20190126:CLOSED;20190127:CLOSED;20190128:0830-20190128:1600;20190129:0830-20190129:1600;20190130:0830-20190130:1600;20190131:0830-20190131:1600;20190201:0830-20190201:1600;20190202:CLOSED;20190203:CLOSED;20190204:0830-20190204:1600;20190205:0830-20190205:1600;20190206:0830-20190206:1600'
...
>> dataStruct(2)
ans =
struct with fields:
m_conId: 322823681
m_symbol: 'ZN'
m_secType: 'FOP'
m_expiry: '20190222'
m_strike: 97.5
m_right: 'C'
m_multiplier: '1000'
m_exchange: 'ECBOT'
m_currency: 'USD'
m_localSymbol: 'C OZN MAR 19 9750'
...
You can filter the results by specifying a combination of the Expiry, Strike, Multiplier and/or Right parameters. For example, to limit ZN options only to Calls that have Strike=100 (i.e., fetch all the 11 active expiration dates):
>> dataStruct = IBMatlab('action','contract', 'symbol','ZN', 'secType','FOP', 'exchange','ecbot', 'Right','Call', 'Strike',100)
dataStruct =
11×1 struct array with fields:
m_conId
m_symbol
m_secType
m_expiry
m_strike
m_right
m_multiplier
m_exchange
m_currency
...
>> {dataStruct.m_expiry}
ans =
1×11 cell array
Columns 1 through 6
{'20190222'} {'20190125'} {'20190104'} {'20190111'} {'20190102'} {'20190109'}
Columns 7 through 11
{'20190118'} {'20190524'} {'20190322'} {'20190426'} {'20190823'}
For additional details, see https://undocumentedmatlab.com/ib-matlab

Products

Community Treasure Hunt

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

Start Hunting!