File Exchange

image thumbnail

Display Stock Data

version 1.1 (5.79 KB) by

Internet enabled data analysis and visualization.

2.61538
13 Ratings

3 Downloads

Updated

View License

This is the example code for the News and Notes article entitled "Internet Enabled Data Analysis and Visualization with MATLAB." It demonstrates how to use Java to retrieve and plot data publically available on the Internet, using historical stock data from the Yahoo! servers as an example. Note that the Yahoo! historical stock price server is often very busy during the US business day -- you may have trouble connecting. Try again after 4:30pm Eastern (US) Time.

Comments and Ratings (16)

Greg Freeman

>> DisplayStockData('S', '1/1/2000', 'm', 27)
Contacting server...
Reading data for symbol S...
??? Error using ==> datenum at 174
DATENUM failed.

Error in ==> GetStockData at 47
date = datenum(dates);

Error in ==> DisplayStockData at 18
[open, high, low, close, date] = GetStockData(symbol, startdate, frequency, periods);

Caused by:
Error using ==> datevec at 286
Cannot parse date 9.84.

I entered everything as stated in the file, trying the example, and it fails. I am running MATLAB V 7.6.0.324 R2008a. What's up...

Jacob Abernethy

if (length(stockdata) > 0)
[dates, open, high, low, close] = ...
strread(stockdata,'%s%f%f%f%f%*n%*n', 'delimiter', ',', 'emptyvalue', NaN);
for dateind = 1:length(dates)
thisdate = dates{dateind};
thisdate = [thisdate(6:end) '-' thisdate(1:4)];
dates{dateind} = thisdate;
end

% Convert the string dates into date numbers. The plotting functions
% need these date numbers.
date = datenum(dates);
end

Fredrik Peterson

No matter what I do, I get this error:

>> DisplayStockData('S', '1/1/2000', 'm', 27);
Contacting server...
Reading data for symbol S...
??? DATENUM failed.
Failed to parse date string.
Error using ==> datevec
2003 is too large to be a month.

Error in ==> GetStockData at 47
date = datenum(dates);

Error in ==> DisplayStockData at 18
[open, high, low, close, date] = GetStockData(symbol, startdate, frequency, periods);

Luminous Logic

FYI: I have some additional information re: how to use the "adjusted close" data to normalize the other columns for splits, dividends, etc. on my blog at http://luminouslogic.com/how-to-normalize-historical-data-for-splits-dividends-etc.htm

DJ Gregoire

line 42 in GetStockdata has to be updated to
[dates, open, high, low, close] = ...
strread(stockdata,'%s%f%f%f%f%*n%*n', 'delimiter', ',', 'emptyvalue', NaN);

to reflect Yahoo's current format for it's stock data tables. They now have a 6th column, the adjusted close price, which adjusts the historical close price for any subsequent splits.
Ideally, the whole set of routines should be updated to use the adjusted close price instead of the historical close price.

Gert Van Tonder

I get this error, please any help?!

??? Trouble reading floating point number from file (row 2, field 3) ==> Dec-05,68.95,75.46,68.81,71.89,

Error in ==> C:\MATLAB6p1\toolbox\matlab\iofun\dataread.dll
Error in ==> C:\MATLAB6p1\toolbox\matlab\iofun\strread.m
On line 51 ==> [varargout{1:nlhs}]=dataread('string',varargin{:});

Error in ==> C:\MATLAB6p1\work\retreive_stock\GetStockData.m
On line 42 ==> [dates, open, high, low, close] = ...

Error in ==> C:\MATLAB6p1\work\retreive_stock\DisplayStockData.m
On line 18 ==> [open, high, low, close, date] = GetStockData(symbol, startdate, frequency, periods);

Otman Estrada

Has anyone tried it behind a firewall?

chen-chia chuang

can I get the price of each 5 minntes or obtains the online price???

volker denneberg

i think following patches are required
to run the demo under R14:

GetStockData.m:40
% Parse the string data into MATLAB numeric arrays.
[dates, open, high, low, close] = ...
strread(stockdata,'%s%f%f%f%f%*n', 'delimiter', ',', 'emptyvalue', NaN);

to

% Parse the string data into MATLAB numeric arrays ( xx=data that we ignore here).

[dates, open, high, low, close, xx,xx] = strread(stockdata,'%s%f%f%f%f%f%f', 'delimiter', ',', 'emptyvalue', NaN);

%remove tailing comment
dates(end)=[];
----------------------------------------
change GetStockData.m:143
from

endYear = endYear + (mod(periods, 12));

to

endYear = endYear + (floor(periods/ 12))

Dave Holtschlag

Great concept, but seems to have problems.

>> DisplayStockData('S', '1/1/2000', 'm', 27)
Contacting server...
Reading data for symbol S...
??? Trouble reading floating point number from file (row 2, field 3) ==> Apr-03,24.15,29.19,23.76,28.34,

Error in ==> c:\usgs\matlab13\toolbox\matlab\iofun\dataread.dll
Error in ==> c:\usgs\matlab13\toolbox\matlab\iofun\strread.m
On line 51 ==> [varargout{1:nlhs}]=dataread('string',varargin{:});

Error in ==> C:\usgs\Matlab13\demos\JavaInternetGetData\GetStockData.m
On line 42 ==> [dates, open, high, low, close] = ...

Error in ==> C:\usgs\Matlab13\demos\JavaInternetGetData\DisplayStockData.m
On line 18 ==> [open, high, low, close, date] = GetStockData(symbol, startdate, frequency, periods);

>>

<a href="http://www.sapkota.com.np">Kari Kha

Excellent

Chad Frost

This is a nice demonstration of the use of Java classes in Matlab, but it is still pretty buggy and rather dependent on the financial toolbox for the most interesting results. The author's attempt to supply surrogate functions for those that might be in the F.T. (e.g. daxis in lieu of dateaxis) is a step in the right direction, but still needs some cleanup/debugging work.

Florian Maurer

This package is a good idea, BUT IT DOES NOT WORK. Calling the function GetStockData results in an error message:
??? Unknown parameter 'emptyvalue'

:-(

Marco Culonea

it would be nice if it worked

» DisplayStockData('S', '1/1/2000', 'm', 27)
Contacting server...
??? Java exception occurred:
java.net.UnknownHostException: table.finance.yahoo.com

at java.net.InetAddress.getAllByName0(Unknown Source)

at java.net.InetAddress.getAllByName0(Unknown Source)

at java.net.InetAddress.getByName(Unknown Source)

at java.net.Socket.<init>(Unknown Source)

at sun.net.NetworkClient.doConnect(Unknown Source)

at sun.net.www.http.HttpClient.openServer(Unknown Source)

at sun.net.www.http.HttpClient.openServer(Unknown Source)

at sun.net.www.http.HttpClient.<init>(Unknown Source)

at sun.net.www.http.HttpClient.<init>(Unknown Source)

at sun.net.www.http.HttpClient.New(Unknown Source)

at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

at java.net.URL.openStream(Unknown Source)
.

Error in ==> c:\matlab6p5\toolbox\finance\finance\GetStockData.m (QueryServerForData)
On line 67 ==> stream = openStream(url);

Error in ==> c:\matlab6p5\toolbox\finance\finance\GetStockData.m
On line 25 ==> stockdata = QueryServerForData(symbol, startdate, frequency, periods);

Error in ==> c:\matlab6p5\toolbox\finance\finance\DisplayStockData.m
On line 18 ==> [open, high, low, close, date] = GetStockData(symbol, startdate, frequency, periods);

Nick Wooder

Well it might look OK in the "News and Notes" but maybe these samples should actually work when downloaded.
This gives errors about missing functions!
Not a good example of anything.

j. helffrich

I like the lesson about querying a database over the web, but I think you left out a couple of functions from the zip archive: day.m month.m year.m

I couldn't run it without huge revisions, but then I don't have the financial toolbox.

Updates

1.1

Added BSD license

More robust month calculation.

Will now run properly even in the absense of the Financial Toolbox (better error checking, new functions added).

MATLAB Release
MATLAB 6.1 (R12.1)
Acknowledgements

Inspired: STOCK QUOTE QUERY, ReadWeatherData

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

» Watch video