function [sopen,sclose,shigh,slow,svol,sdatenum,sdate] = get_stock(stock_symbol,starttime,endtime)
% [sopen,sclose,shigh,slow,svol,sdatenum,sdate] = get_stock(stock_symbol,starttime,endtime)
% 
% get daily historical stock prices from Yahoo 
%    (use get_stock_plot to plot stock prices and volumes)
%
% inputs:
%    stock_symbol: stock symbol, e.g. 'AAPL'
%    starttime:    year or [year month] or [year month day] or [] (earliest possible)
%    endtime:      year or [year month] or [year month day] or [] (now)
% outputs:
%    sopen:        opening price
%    sclose:       closing price
%    shigh:        highest stock price of day
%    slow:         lowest stock price of day
%    svol:         trading volume
%    sdatenum:     date as number of days since year 0000
%    sdate:        date as string (in format '2002-05-01')
%
% examples for starttime, endtime:
%   get_stock('GE',2003)            % from Jan.1 2003 to now
%   get_stock('GE',2003,2004)       % from Jan.1 2003 to Jan.1 2004
%   get_stock('GE',[2003 5 7],[2003 7]) % from May 7 2003 to July 1 2003
%   get_stock('GE',[],[2003 11])    % from earliest data to Nov.1 2003

% modified from get_hist_stock_data.m from
%   http://luminouslogic.com/how-to-download-historical-stock-data-into-matlab.htm
stock_symbol = upper(stock_symbol);
if nargin==1
  starttime = 2004;
  endtime = [];
elseif nargin==2
  endtime = [];
end
if length(starttime)==0                                          % if starttime=[]
  start_year = []; start_month = []; start_day = [];
else
  start_year = starttime(1);
  if length(starttime)>=2; start_month = starttime(2); else start_month=1; end
  if length(starttime)>=3; start_day = starttime(3); else start_day=1; end
end
if length(endtime)==0                                            % if endtime=[]
  [end_year, end_month, end_day, dummy, dummy] = datevec(date);  % Get current date
else
  end_year = endtime(1);
  if length(endtime)>=2; end_month = endtime(2); else end_month=1; end
  if length(endtime)>=3; end_day = endtime(3); else end_day=1; end
end
% Build URL string
F = 'http://ichart.finance.yahoo.com/table.csv?&s=%s&a=%i&b=%i&c=%i&d=%i&e=%i&f=%i&g=%i&ignore.csv';
url_string = sprintf(F,stock_symbol,start_month-1,start_day,start_year,end_month-1,end_day,end_year);
% Open a connection to the URL and retrieve data into a buffer
buffer      = java.io.BufferedReader(...
              java.io.InputStreamReader(...
              openStream(...
              java.net.URL(url_string))));
% Read the first line (a header) and discard
dummy   = readLine(buffer);
% Read all remaining lines in buffer
ptr = 1;
while 1
    % Read line
    buff_line = char(readLine(buffer));     
    % Break if this is the end
    if length(buff_line)<3, break; end    
    % Find comma delimiter locations
    commas    = find(buff_line== ',');    
    % Extract high, low, open, close, etc. from string
    DATEvar   = buff_line(1:commas(1)-1);
    OPENvar   = str2num( buff_line(commas(1)+1:commas(2)-1) );
    HIGHvar   = str2num( buff_line(commas(2)+1:commas(3)-1) );
    LOWvar    = str2num( buff_line(commas(3)+1:commas(4)-1) );
    CLOSEvar  = str2num( buff_line(commas(4)+1:commas(5)-1) );
    VOLvar    = str2num( buff_line(commas(5)+1:commas(6)-1) );
    adj_close = str2num( buff_line(commas(6)+1:end) );
    %Adjust for dividends, splits, etc.
    DATEtemp{ptr,1} = DATEvar;
    OPENtemp(ptr,1) = OPENvar  * adj_close / CLOSEvar;
    HIGHtemp(ptr,1) = HIGHvar  * adj_close / CLOSEvar;
    LOWtemp (ptr,1) = LOWvar   * adj_close / CLOSEvar;
    CLOSEtemp(ptr,1)= CLOSEvar * adj_close / CLOSEvar;
    VOLtemp(ptr,1)  = VOLvar;
    ptr = ptr + 1;
end
% Reverse to normal chronological order, so 1st entry is oldest data point
sdate  = flipud(DATEtemp);
sopen  = flipud(OPENtemp);
shigh  = flipud(HIGHtemp);
slow   = flipud(LOWtemp);
sclose = flipud(CLOSEtemp);
svol   = flipud(VOLtemp);
sdatenum = datenum(sdate,'yyyy-mm-dd');



