Code covered by the BSD License  

Highlights from
Display Stock Data

2.61538

2.6 | 13 ratings Rate this file 33 Downloads (last 30 days) File Size: 5.79 KB File ID: #1880

Display Stock Data

by

 

24 Jun 2002 (Updated )

Internet enabled data analysis and visualization.

| Watch this File

File Information
Description

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.

Acknowledgements

This file inspired Read Weather Data and Stock Quote Query.

Required Products Financial Toolbox
MATLAB release MATLAB 6.1 (R12.1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (16)
02 Mar 2009 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...

11 Jan 2008 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

15 Jun 2007 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);

11 Dec 2006 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

08 Feb 2006 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.

10 Jan 2006 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);

28 Feb 2005 Otman Estrada

Has anyone tried it behind a firewall?

05 Jan 2005 chen-chia chuang

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

26 Jul 2004 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))

06 May 2004 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);

>>

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

Excellent

04 Dec 2002 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.

02 Nov 2002 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'

:-(

14 Oct 2002 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);

09 Oct 2002 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.

25 Sep 2002 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
11 Oct 2002

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

11 Oct 2002

More robust month calculation.

18 Nov 2010

Added BSD license

Contact us