2.61538

2.6 | 13 ratings Rate this file 55 downloads (last 30 days) File Size: 4.79 KB File ID: #1880

Display Stock Data

by Peter Webb

 

24 Jun 2002 (Updated 05 Nov 2002)

No BSD License  

Internet enabled data analysis and visualization.

Download Now | 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 submission has inspired the following:
ReadWeatherData, STOCK QUOTE QUERY
Required Products Financial Toolbox
MATLAB release MATLAB 6.1 (R12.1)
Zip File Content  
Other Files DisplayStockData.m,
GetStockData.m,
year.m,
day.m,
month.m,
daxis.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (16)
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.

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.

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.&lt;init&gt;(Unknown Source)

at sun.net.www.http.HttpClient.&lt;init&gt;(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);

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'

:-(

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.

12 Mar 2004 Kari Kha

Excellent

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

>>

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

05 Jan 2005 chen-chia chuang

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

28 Feb 2005 Otman Estrada

Has anyone tried it behind a firewall?

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

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.

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

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

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...

Please login to add a comment or rating.
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.

Tag Activity for this File
Tag Applied By Date/Time
data import Peter Webb 22 Oct 2008 06:45:57
data export Peter Webb 22 Oct 2008 06:45:57
news Peter Webb 22 Oct 2008 06:45:57
notes Peter Webb 22 Oct 2008 06:45:57
stock Peter Webb 22 Oct 2008 06:45:57
data Peter Webb 22 Oct 2008 06:45:57
visualiz Peter Webb 22 Oct 2008 06:45:57
candlestick Peter Webb 22 Oct 2008 06:45:57
graph Peter Webb 22 Oct 2008 06:45:57
internet Peter Webb 22 Oct 2008 06:45:57
java Peter Webb 22 Oct 2008 06:45:57
candlestick Pietro 20 Nov 2008 08:14:57
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com