From: <ws...@us...> - 2008-08-31 15:59:51
|
Revision: 5285 http://octave.svn.sourceforge.net/octave/?rev=5285&view=rev Author: wsloand Date: 2008-08-31 15:59:55 +0000 (Sun, 31 Aug 2008) Log Message: ----------- Updated fetch to modularize based on the source (for better coding and in preparation for objects) Modified Paths: -------------- trunk/octave-forge/main/financial/inst/fetch.m trunk/octave-forge/main/financial/inst/yahoo.m Added Paths: ----------- trunk/octave-forge/main/financial/inst/__fetch_yahoo__.m trunk/octave-forge/main/financial/inst/google.m Copied: trunk/octave-forge/main/financial/inst/__fetch_yahoo__.m (from rev 5284, trunk/octave-forge/main/financial/inst/fetch.m) =================================================================== --- trunk/octave-forge/main/financial/inst/__fetch_yahoo__.m (rev 0) +++ trunk/octave-forge/main/financial/inst/__fetch_yahoo__.m 2008-08-31 15:59:55 UTC (rev 5285) @@ -0,0 +1,102 @@ +## Copyright (C) 2008 Bill Denney +## +## This software is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## This software is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this software; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {[@var{data} @var{fields}] =} +## __fetch_yahoo__ (@var{conn}, @var{symbol}, @var{fromdate}, @var{todate}, @var{period}) +## +## Download stock data from yahoo. (Helper for fetch.) +## +## @var{fields} are the data fields returned by Yahoo. +## +## @var{fromdate} and @var{todate} is the date datenum for the requested +## date range. If you enter today's date, you will get yesterday's +## data. +## +## @var{period} (default: "d") allows you to select the period for the +## data which can be any of +## @itemize @bullet +## @item 'd': daily +## @item 'w': weekly +## @item 'm': monthly +## @item 'v': dividends +## @end itemize +## +## @seealso{yahoo, fetch} +## @end deftypefn + +## FIXME: Actually use the proxy info if given in the connection. +## FIXME: Do not ignore the fields input. + +## Author: Bill Denney <bi...@de...> +## Created: 17 Aug 2008 + +function [data fields] = __fetch_yahoo__ (conn=[], symbol="", + fromdate, todate, period="d") + + if strcmpi (conn.url, "http://quote.yahoo.com") + fromdate = datevec (fromdate); + todate = datevec (todate); + geturl = sprintf (["http://ichart.finance.yahoo.com/table.csv" ... + "?s=%s&d=%d&e=%d&f=%d&g=%s&a=%d&b=%d&c=%d&" ... + "ignore=.csv"], + symbol, todate(2)-1, todate(3), todate(1), + period, + fromdate(2)-1, fromdate(3), fromdate(1)); + ## FIXME: This would be more efficient if csv2cell could work on + ## strings instead of files. + [f, success, msg] = urlwrite (geturl, tmpnam ()); + if ! success + error ("Could not write Yahoo data to tmp file:\n%s", msg) + endif + d = csv2cell (f); + unlink(f); + ## Pull off the header + fields = d(1,:); + d(1,:) = []; + dates = strvcat (d(:,1)); + dates = datenum(str2num(dates(:,1:4)), + str2num(dates(:,6:7)), + str2num(dates(:,9:10))); + data = [dates, cell2mat(d(:,2:end))]; + else + error ("Non-yahoo connection passed to yahoo fetch") + endif + +endfunction + +%!shared fgood, dgood +%! fgood = {"Date", "Open", "High", "Low", "Close", "Volume", "Adj Close"}; +%! dgood = [732501,34.77,34.87,34.25,34.62,15515400,34.62; +%! 732500,33.87,34.77,33.72,34.63,16354300,34.63; +%! 732499,34.64,34.97,34.03,34.12,13585700,34.12; +%! 732498,34.25,35.08,34.20,34.60,16086700,34.60; +%! 732494,34.76,34.85,34.22,34.44,9861600,34.44]; +%!test +%! [d f] = __fetch_yahoo__ (yahoo(), "yhoo", 732494, 732501, "d"); +%! assert(d, dgood, eps); +%! assert(f, fgood, eps); +## test that the automatic period works +%!test +%! [d f] = __fetch_yahoo__ (yahoo(), "yhoo", 732494, 732501); +%! assert(d, dgood, eps); +%! assert(f, fgood, eps); + +## The test below fails because yahoo gives a different volume on 732498 +##%!xtest +##%! [d f] = fetch(yahoo(), "yhoo", "01-Jul-2005", "10-Jul-2005", "w"); +##%! assert(d, dgood(4:5,:), eps); +##%! assert(f, fgood, eps); \ No newline at end of file Property changes on: trunk/octave-forge/main/financial/inst/__fetch_yahoo__.m ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/octave-forge/main/financial/inst/fetch.m =================================================================== --- trunk/octave-forge/main/financial/inst/fetch.m 2008-08-31 13:01:26 UTC (rev 5284) +++ trunk/octave-forge/main/financial/inst/fetch.m 2008-08-31 15:59:55 UTC (rev 5285) @@ -124,30 +124,7 @@ endif if strcmpi (conn.url, "http://quote.yahoo.com") - fromdate = datevec (fromdate); - todate = datevec (todate); - geturl = sprintf (["http://ichart.finance.yahoo.com/table.csv" ... - "?s=%s&d=%d&e=%d&f=%d&g=%s&a=%d&b=%d&c=%d&" ... - "ignore=.csv"], - symbol, todate(2)-1, todate(3), todate(1), - period, - fromdate(2)-1, fromdate(3), fromdate(1)); - ## FIXME: This would be more efficient if csv2cell could work on - ## strings instead of files. - [f, success, msg] = urlwrite (geturl, tmpnam ()); - if ! success - error ("Could not write Yahoo data to tmp file:\n%s", msg) - endif - d = csv2cell (f); - unlink(f); - ## Pull off the header - fields = d(1,:); - d(1,:) = []; - dates = strvcat (d(:,1)); - dates = datenum(str2num(dates(:,1:4)), - str2num(dates(:,6:7)), - str2num(dates(:,9:10))); - data = [dates, cell2mat(d(:,2:end))]; + [data fields] = __fetch_yahoo__ (conn, symbol, fromdate, todate, period); else error ("Unrecgonized connection type") endif Copied: trunk/octave-forge/main/financial/inst/google.m (from rev 5284, trunk/octave-forge/main/financial/inst/yahoo.m) =================================================================== --- trunk/octave-forge/main/financial/inst/google.m (rev 0) +++ trunk/octave-forge/main/financial/inst/google.m 2008-08-31 15:59:55 UTC (rev 5285) @@ -0,0 +1,48 @@ +## Copyright (C) 2008 Bill Denney +## +## This software is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## This software is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this software; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{conn} =} google () +## @deftypefnx {Function File} {@var{conn} =} google (@var{URL}, @var{ipaddress}, @var{port}) +## +## Prepare a Google connection for the fetch command to get Google +## historical quote data. +## +## If given, the @var{URL} must be "http://finance.google.com". The +## @var{ipaddress} and @var{port} is the proxy ipaddress and port. These +## parameters are currently ignored (with a warning if given). +## +## @seealso{fetch, yahoo} +## @end deftypefn + +## FIXME: Actually use the proxy info if given. + +## Author: Bill Denney <bi...@de...> +## Created: 31 Aug 2008 + +function conn = google (url="http://finance.google.com", ipaddr="", port=[]) + + if ! strcmpi (url, "http://finance.google.com") + error ("url must be 'http://finance.google.com'") + elseif ! (isempty (ipaddr) && isempty (port)) + warning ("Proxy information is currently ignored") + endif + + conn.url = url; + conn.ip = ipaddr; + conn.port = port; + +endfunction \ No newline at end of file Property changes on: trunk/octave-forge/main/financial/inst/google.m ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/octave-forge/main/financial/inst/yahoo.m =================================================================== --- trunk/octave-forge/main/financial/inst/yahoo.m 2008-08-31 13:01:26 UTC (rev 5284) +++ trunk/octave-forge/main/financial/inst/yahoo.m 2008-08-31 15:59:55 UTC (rev 5285) @@ -25,7 +25,7 @@ ## @var{ipaddress} and @var{port} is the proxy ipaddress and port. These ## parameters are currently ignored (with a warning if given). ## -## @seealso{fetch} +## @seealso{fetch, google} ## @end deftypefn ## FIXME: Actually use the proxy info if given. @@ -45,4 +45,4 @@ conn.ip = ipaddr; conn.port = port; -endfunction \ No newline at end of file +endfunction This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |