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} =} fetch (@var{conn}, @var{symbol})
18 ## @deftypefnx {@var{data} =} fetch (@dots{}, @var{fields})
19 ## @deftypefnx {@var{data} =} fetch (@dots{}, @var{date})
20 ## @deftypefnx {@var{data} =} fetch (@dots{}, @var{fromdate}, @var{todate})
21 ## @deftypefnx {@var{data} =} fetch (@dots{}, @var{period})
22 ## @deftypefnx {[@var{data}, @var{fields}] =} fetch (@dots{})
24 ## Download stock data from a connection.
26 ## @var{fields} are the data fields to download and must come from the
40 ## As an output, @var{fields} may be different than your request. This
41 ## is because there is mapping of field names from the data source to
42 ## the output, and what is returned is the source mapping to allow
45 ## @var{date} is the date string or datenum for the requested data. If
46 ## you enter today's date, you will get yesterday's data. @var{fromdate}
47 ## and @var{todate} allow you to specify a date range for the data.
49 ## @var{period} (default: "d") allows you to select the period for the
50 ## data which can be any of the below as long as they are supported by
51 ## the associated backend.
55 ## @item 'm': monthly (Yahoo only)
56 ## @item 'v': dividends (Yahoo only)
59 ## @seealso{yahoo, google}
62 ## FIXME: Actually use the proxy info if given in the connection.
63 ## FIXME: Do not ignore the fields input.
65 function [data fields] = fetch (conn=[], symbol="", varargin)
67 fields = {"Symbol", "Last", "Date", "Time", "Change", "Open", ...
68 "High", "Low", "Volume"};
73 firstdate = datenum (1900, 1, 1);
77 ## By default, use yahoo now since it's the only connection
78 ## currently available.
82 error ("The ticker symbol must be given")
83 elseif ! ischar (symbol)
84 error ("The symbol must be either a string")
86 for i = 1:numel (varargin)
87 if ischar (varargin{i}) && (length (varargin{i}) == 1)
89 elseif iscellstr (varargin{i}) || ischar (varargin{i})
90 ## if it's a character and it's a valid date, make it into our
92 if ischar (varargin{i})
95 thisdate = datenum (varargin{i});
102 if isempty (thisdate)
103 fields = varargin{i};
104 warning ("Fields are currently ignored and all data is returned")
107 elseif isnumeric (varargin{i})
108 ## it must be our dates
109 if isempty (fromdate)
110 fromdate = varargin{i};
112 todate = varargin{i};
114 error ("Invalid input for argument %d", i + 2)
118 if isempty (fromdate)
119 fromdate = firstdate;
123 if strcmpi (conn.url, "http://quote.yahoo.com")
124 [data fields] = fetch_yahoo (conn, symbol, fromdate, todate, period);
125 elseif strcmpi (conn.url, "http://finance.google.com")
126 [data fields] = fetch_google (conn, symbol, fromdate, todate, period);
128 error ("Unrecgonized connection type")
133 %!shared fgood, dgood
134 %! fgood = {"Date", "Open", "High", "Low", "Close", "Volume", "Adj Close"};
135 %! dgood = [732501,34.77,34.87,34.25,34.62,15515400,34.62;
136 %! 732500,33.87,34.77,33.72,34.63,16354300,34.63;
137 %! 732499,34.64,34.97,34.03,34.12,13585700,34.12;
138 %! 732498,34.25,35.08,34.20,34.60,16086700,34.60;
139 %! 732494,34.76,34.85,34.22,34.44,9861600,34.44];
141 %! [d f] = fetch(yahoo(), "yhoo", "01-Jul-2005", "10-Jul-2005");
142 %! assert(d, dgood, eps);
143 %! assert(f, fgood, eps);
145 ## The test below fails because yahoo gives a different volume on 732498
147 ##%! [d f] = fetch(yahoo(), "yhoo", "01-Jul-2005", "10-Jul-2005", "w");
148 ##%! assert(d, dgood(4:5,:), eps);
149 ##%! assert(f, fgood, eps);