File Exchange

image thumbnail

Get stock option chains

version 1.3 (35.8 KB) by

Get stock option chains.

4.16667
7 Ratings

3 Downloads

Updated

View License

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)

Comments and Ratings (14)

Zeke

Zeke (view profile)

I'm getting the following error:

Struct contents reference from a non-struct array object.

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

Error in getOptionChainYQL (line 42)
optionChain = getOptionChainYQLCore(tickers2{i});

Beaver

Beaver (view profile)

Running in to an error. Seen it before and it seems to go away on its own eventually.

input={'aapl'}
data = getOptionChainYQL(input)

~~~

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});

~~~

Haven't been able to pull data since yesterday evening. Any idea if the YQL is dysfunctional presently or has something in the query changed?

Tom

Tom (view profile)

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.

ted p teng

ted p teng (view profile)

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

Tom

Tom (view profile)

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!

Tom

Tom (view profile)

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});

Sam Da

Sam Da (view profile)

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.

ted p teng

ted p teng (view profile)

@G, thanks for the excellent suggestion!

G

G (view profile)

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);

Beaver

Beaver (view profile)

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});

Mirko

Mirko (view profile)

Super tool. This is what I was looking for.

Updates

1.3

corrected grammar in Description

1.2

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

MATLAB Release
MATLAB 7.13 (R2011b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video