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_google (@var{conn}, @var{symbol}, @var{fromdate}, @var{todate}, @var{period})
20 ## Download stock data from google. (Helper for fetch.)
22 ## @var{fields} are the data fields returned by Google.
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
35 ## @seealso{google, fetch}
38 ## FIXME: Actually use the proxy info if given in the connection.
39 ## FIXME: Do not ignore the fields input.
41 function [data fields] = fetch_google (conn=[], symbol="",
42 fromdate, todate, period="d")
44 periods = struct("d", "daily", "w", "weekly");
45 if strcmpi (conn.url, "http://finance.google.com")
46 fromdatestr = datestr (fromdate);
47 todatestr = datestr (todate);
48 ## http://finance.google.com/finance/historical?q=T&startdate=Sep+1%2C+2007&enddate=Aug+31%2C+2008&histperiod=weekly&output=csv
49 geturl = sprintf (["http://finance.google.com/finance/" ...
51 "q=%s&startdate=%s&enddate=%s&" ...
52 "histperiod=%s&output=csv"],
53 symbol, fromdatestr, todatestr, periods.(period));
54 ## FIXME: This would be more efficient if csv2cell could work on
55 ## strings instead of files.
56 [f, success, msg] = urlwrite (geturl, tmpnam ());
58 error (["Could not write Google data to tmp file:" ...
59 "\n%s\nURL was:\n%s"], msg, geturl)
62 d{1,1} = d{1,1}(4:end); # Remove byte order mark (BOM)
64 ## Pull off the header
67 ## Put the dates into datenum format
68 data = [datenum(datevec(d(:,1), "dd-mmm-yy")), \
69 cell2mat(d(:,2:end))];
70 ## Note that google appears to have an off-by-one error in
71 ## returning historical data, so make sure that we only return the
72 ## requested data and not what Google sent.
73 data((data(:,1) < fromdate) | (data(:,1) > todate), :) = [];
75 error ("Non-google connection passed to google fetch")
80 %!shared fgood, dgood, wgood
81 %! fgood = {"Date", "Open", "High", "Low", "Close", "Volume"};
82 %! dgood = [732501,34.77,34.87,34.25,34.62,15296900;
83 %! 732500,33.87,34.77,33.72,34.63,16265900;
84 %! 732499,34.64,34.97,34.03,34.12,13451500;
85 %! 732498,34.25,35.08,34.20,34.60,15845100;
86 %! 732494,34.76,34.85,34.22,34.44,9740300];
87 %! wgood = [732501,34.25,35.08,33.72,34.62,60859400;
88 %! 732494,35.88,36.24,34.22,34.44,67132100];
90 %! [d f] = fetch_google (google(), "yhoo", 732494, 732501, "d");
91 %! assert(d, dgood, eps);
92 %! assert(f, fgood, eps);
93 ## test that the automatic period works
95 %! [d f] = fetch_google (google(), "yhoo", 732494, 732501);
96 %! assert(d, dgood, eps);
97 %! assert(f, fgood, eps);
98 ## Test that weekly works
100 %! [d f] = fetch_google (google(), "yhoo", 732494, 732501, "w");
101 %! assert(d, wgood, eps);
102 %! assert(f, fgood, eps);