Code covered by the BSD License  

Highlights from
Get stock option chains

4.4

4.4 | 5 ratings Rate this file 44 Downloads (last 30 days) File Size: 35.8 KB File ID: #37981
image thumbnail

Get stock option chains

by

 

31 Aug 2012 (Updated )

Get stock option chains.

| Watch this File

File Information
Description

Pulls stock option chains from finance.yahoo.com with Yahoo! Query Language.
--
Updates
09JUL2014 1) now supports miniOption, implemented by G (matlab central author # 379241).
09JUL2014 2) now supports index options, implemented by Tom (matlab central author # 483995)

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
10 Jul 2014 Tom

Thanks for the quick implementation!

One more problem I noticed is that numbers with commas in it are converted into a vector, e.g. 1,001.5 becomes [1,1.5].
A quick solution is to remove all commas in strings before converting them to numbers. Following code should be placed on line 214 (just before converting strings to numbers):

%Remove commas from strings that contain numbers
data(i,:)= strrep(data(i,:), ',', '');

This problem occurs e.g. when looking at S&P 500 options.

09 Jul 2014 ted p teng

Thank you for your contributions Tom and G. Your suggestions are now implemented!

09 Jul 2014 Tom

The problem with index options is that there is the ^ symbol in the ticker.

I updated the code to also handle this symbol. The code should be placed before the for-loop in the getOptionChainYQL(tickers) function:

%Replace ^ by hexadecimal ASCII value with % in front
%Note that %25 is % and %5E is ^
tickers2=strrep(tickers, '^', '%255E');

%Add ^ (in hexadecimal ASCII format) for VIX, DJX and S&P 500
ind=find(ismember(tickers,{'VIX','DJX','SPXPM'})==1);
if ~isempty(ind)
for j = 1:length(ind)
tickers2{ind(j)}=['%255E',tickers{ind(j)}];
end
end

The first part replaces the ^ symbol by its hexadecimal ASCII value which is used in URLs.
The second-part adds the ^ symbol in correct format to tickers (without the ^ symbol) for some indices and one can of course add more index tickers if wanted.

In the for-loop, use tickers2 instead of tickers when calling getOptionChainYQLCore to have tickers with correct handling of the ^ symbol.
The original names in tickers are still used in the output structure.

Adding the piece of code gives problems when extracting the expiry from the option symbol because "%5E" is present in the symbol variable but not in the optionSymbol variable, so we should remove it again:

if strcmpi(symbol(1:3),'%5E')
symbol=symbol(4:length(symbol));
end

which should be placed after initialising the symbol variable in the getOptionChainYQLCore function.

Hope this helps and thanks for the excellent function!

09 Jul 2014 Tom  
19 May 2014 Muhammad Hasnat

works for most of stocks but I get this error for options like BMW and few others:
Attempt to reference field of non-structure array.
Error in getOptionChainYQL>getOptionChainYQLCore (line 107)
symbol = char(nodeList.item(0).getAttribute('symbol'));

Error in getOptionChainYQL (line 23)
optionChain = getOptionChainYQLCore(tickers{i});

05 Feb 2014 Sam Da

It does not work for index options like ^VIX. I am not familiar with Java so it will be nice if someone can update the code to pull index options as well.

17 Nov 2013 ted p teng

@G, thanks for the excellent suggestion!

24 Aug 2013 G

Consider updating to handle the miniOptions that are now available for a few stocks such as AAPL.

I modified to the following:
%get expiration from symbol
miniOption = ~cellfun(@isempty, regexp(optionSymbol, ['^',symbol,'7']));
expiration = zeros(numel(miniOption),1);
expiration(~miniOption) = datenum(cellfun(@(x) x(numel(symbol)+1:numel(symbol)+6),optionSymbol(~miniOption),'UniformOutput',false),'yymmdd');
if sum(miniOption) > 0
expiration(miniOption) = datenum(cellfun(@(x) x(numel(symbol)+2:numel(symbol)+7),optionSymbol(miniOption),'UniformOutput',false),'yymmdd');
end
expirationStr = datestr(expiration);

22 Aug 2013 Beaver

Any idea about the following error? it was working one minute and not working the next... Yhoo didn't change their query paths I hope?

Attempt to reference field of non-structure array.

Error in getOptionChainYQL>getOptionChainYQLCore (line 107)
symbol = char(nodeList.item(0).getAttribute('symbol'));

Error in getOptionChainYQL (line 23)
optionChain = getOptionChainYQLCore(tickers{i});

21 Apr 2013 Mirko

Super tool. This is what I was looking for.

Updates
09 Jul 2014

1) now supports miniOption, implemented by G (matlab central authors # 379241)
2) now supports index options, implemented by Tom (matlab central authors # 483995)

10 Jul 2014

corrected grammar in Description

Contact us