]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/private/fetch_yahoo.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / private / fetch_yahoo.m
1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {[@var{data} @var{fields}] =}
18 ##  fetch_yahoo (@var{conn}, @var{symbol}, @var{fromdate}, @var{todate}, @var{period})
19 ##
20 ## Download stock data from yahoo. (Helper for fetch.)
21 ##
22 ## @var{fields} are the data fields returned by Yahoo.
23 ##
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
26 ## data.
27 ##
28 ## @var{period} (default: "d") allows you to select the period for the
29 ## data which can be any of
30 ## @itemize @bullet
31 ## @item 'd': daily
32 ## @item 'w': weekly
33 ## @item 'm': monthly
34 ## @item 'v': dividends
35 ## @end itemize
36 ##
37 ## @seealso{yahoo, fetch}
38 ## @end deftypefn
39
40 ## FIXME: Actually use the proxy info if given in the connection.
41 ## FIXME: Do not ignore the fields input.
42
43 function [data fields] = fetch_yahoo (conn=[], symbol="",
44                                           fromdate, todate, period="d")
45
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&" ...
51                          "ignore=.csv"],
52                          symbol, todate(2)-1, todate(3), todate(1),
53                          period,
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 ());
58     if ! success
59       error ("Could not write Yahoo data to tmp file:\n%s", msg)
60     endif
61     d = csv2cell (f);
62     unlink(f);
63     ## Pull off the header
64     fields = d(1,:);
65     d(1,:) = [];
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))];
71   else
72     error ("Non-yahoo connection passed to yahoo fetch")
73   endif
74
75 endfunction
76
77 %!shared fgood, dgood
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];
84 %!test
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
89 %!test
90 %! [d f] = fetch_yahoo (yahoo(), "yhoo", 732494, 732501);
91 %! assert(d, dgood, eps);
92 %! assert(f, fgood, eps);
93
94 ## The test below fails because yahoo gives a different volume on 732498
95 ##%!xtest
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);