1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {[@var{data} @var{fields}] =}
18 ## fetch_yahoo (@var{conn}, @var{symbol}, @var{fromdate}, @var{todate}, @var{period})
20 ## Download stock data from yahoo. (Helper for fetch.)
22 ## @var{fields} are the data fields returned by Yahoo.
24 ## @var{fromdate} and @var{todate} is the date datenum for the requested
25 ## date range. If you enter today's date, you will get yesterday's
28 ## @var{period} (default: "d") allows you to select the period for the
29 ## data which can be any of
34 ## @item 'v': dividends
37 ## @seealso{yahoo, fetch}
40 ## FIXME: Actually use the proxy info if given in the connection.
41 ## FIXME: Do not ignore the fields input.
43 function [data fields] = fetch_yahoo (conn=[], symbol="",
44 fromdate, todate, period="d")
46 if strcmpi (conn.url, "http://quote.yahoo.com")
47 fromdate = datevec (fromdate);
48 todate = datevec (todate);
49 geturl = sprintf (["http://ichart.finance.yahoo.com/table.csv" ...
50 "?s=%s&d=%d&e=%d&f=%d&g=%s&a=%d&b=%d&c=%d&" ...
52 symbol, todate(2)-1, todate(3), todate(1),
54 fromdate(2)-1, fromdate(3), fromdate(1));
55 ## FIXME: This would be more efficient if csv2cell could work on
56 ## strings instead of files.
57 [f, success, msg] = urlwrite (geturl, tmpnam ());
59 error ("Could not write Yahoo data to tmp file:\n%s", msg)
63 ## Pull off the header
66 dates = strvcat (d(:,1));
67 dates = datenum(str2num(dates(:,1:4)),
68 str2num(dates(:,6:7)),
69 str2num(dates(:,9:10)));
70 data = [dates, cell2mat(d(:,2:end))];
72 error ("Non-yahoo connection passed to yahoo fetch")
78 %! fgood = {"Date", "Open", "High", "Low", "Close", "Volume", "Adj Close"};
79 %! dgood = [732501,34.77,34.87,34.25,34.62,15515400,34.62;
80 %! 732500,33.87,34.77,33.72,34.63,16354300,34.63;
81 %! 732499,34.64,34.97,34.03,34.12,13585700,34.12;
82 %! 732498,34.25,35.08,34.20,34.60,16086700,34.60;
83 %! 732494,34.76,34.85,34.22,34.44,9861600,34.44];
85 %! [d f] = fetch_yahoo (yahoo(), "yhoo", 732494, 732501, "d");
86 %! assert(d, dgood, eps);
87 %! assert(f, fgood, eps);
88 ## test that the automatic period works
90 %! [d f] = fetch_yahoo (yahoo(), "yhoo", 732494, 732501);
91 %! assert(d, dgood, eps);
92 %! assert(f, fgood, eps);
94 ## The test below fails because yahoo gives a different volume on 732498
96 ##%! [d f] = fetch(yahoo(), "yhoo", "01-Jul-2005", "10-Jul-2005", "w");
97 ##%! assert(d, dgood(4:5,:), eps);
98 ##%! assert(f, fgood, eps);