]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/fetch.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / fetch.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} =} 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{})
23 ##
24 ## Download stock data from a connection.
25 ##
26 ## @var{fields} are the data fields to download and must come from the
27 ## set
28 ## @itemize @bullet
29 ## @item "Symbol"
30 ## @item "Last"
31 ## @item "Date"
32 ## @item "Time"
33 ## @item "Change"
34 ## @item "Open"
35 ## @item "High",
36 ## @item "Low"
37 ## @item "Volume"
38 ## @end itemize
39 ##
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
43 ## validation.
44 ##
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.
48 ##
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.
52 ## @itemize @bullet
53 ## @item 'd': daily
54 ## @item 'w': weekly
55 ## @item 'm': monthly (Yahoo only)
56 ## @item 'v': dividends (Yahoo only)
57 ## @end itemize
58 ##
59 ## @seealso{yahoo, google}
60 ## @end deftypefn
61
62 ## FIXME: Actually use the proxy info if given in the connection.
63 ## FIXME: Do not ignore the fields input.
64
65 function [data fields] = fetch (conn=[], symbol="", varargin)
66
67   fields   = {"Symbol", "Last", "Date", "Time", "Change", "Open", ...
68               "High", "Low", "Volume"};
69   fromdate = [];
70   todate   = [];
71   period   = "d";
72
73   firstdate = datenum (1900, 1, 1);
74   lastdate  = today ();
75
76   if isempty (conn)
77     ## By default, use yahoo now since it's the only connection
78     ## currently available.
79     conn = yahoo ();
80   endif
81   if isempty (symbol)
82     error ("The ticker symbol must be given")
83   elseif ! ischar (symbol)
84     error ("The symbol must be either a string")
85   endif
86   for i = 1:numel (varargin)
87     if ischar (varargin{i}) && (length (varargin{i}) == 1)
88       period = varargin{i};
89     elseif iscellstr (varargin{i}) || ischar (varargin{i})
90       ## if it's a character and it's a valid date, make it into our
91       ## dates
92       if ischar (varargin{i})
93         thisdate = [];
94         try
95           thisdate = datenum (varargin{i});
96           if isempty (fromdate)
97             fromdate = thisdate;
98           endif
99           todate = thisdate;
100         end_try_catch
101       endif
102       if isempty (thisdate)
103         fields = varargin{i};
104         warning ("Fields are currently ignored and all data is returned")
105       endif
106       thisdate = [];
107     elseif isnumeric (varargin{i})
108       ## it must be our dates
109       if isempty (fromdate)
110         fromdate = varargin{i};
111       endif
112       todate = varargin{i};
113     else
114       error ("Invalid input for argument %d", i + 2)
115     endif
116   endfor
117
118   if isempty (fromdate)
119     fromdate = firstdate;
120     todate   = lastdate;
121   endif
122
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);
127   else
128     error ("Unrecgonized connection type")
129   endif
130
131 endfunction
132
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];
140 %!test
141 %! [d f] = fetch(yahoo(), "yhoo", "01-Jul-2005", "10-Jul-2005");
142 %! assert(d, dgood, eps);
143 %! assert(f, fgood, eps);
144
145 ## The test below fails because yahoo gives a different volume on 732498
146 ##%!xtest
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);