How to address "Java exception occurred" error when pulling data from web

3 views (last 30 days)
Hi everyone, I am using MATLAB R2009a, and pulling in stock data from the web. I used Luminous Logic's get_hist_stock_data.m file (<http://luminouslogic.com/how-to-download-historical-stock-data-into-matlab.htm>) as inspiration to build a script to download dividend data from yahoo finance.
I am having particular trouble running it with stock symbol "EMM". I can run this script and 80% of time, it returns the correct data from the web, but 20% of time, I get the following error:
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
I manually enter the url in my web browser, and the table downloads successfully every time. I am by no means whatsoever familiar with the java interface in MATLAB. Can anyone help diagnose my issue?
Thanks so much in advance, Shawn
P.S. If anyone has any tips to get the script running faster (the "while" loop is pretty slow), I will surely appreciate it! __
script I am trying to run that gives errors (in reality, this is a function, but I wanted to make it a script so easy for folks to try out): ___________________________
% Define starting year (the further back in time, the longer it takes to download)
start_year = '2000';
stock_symbol='EMM';
% Get current date
[this_year, this_month, this_day, dummy, dummy] = datevec(date); % Build URL string
url_string = strcat('http://ichart.finance.yahoo.com/table.csv?s=',upper(stock_symbol),'&a=01&b=1&c=',start_year,'&d=',num2str(this_month-1),'&e=',num2str(this_day),'&f=',num2str(this_year),'&g=v&ignore=.csv');
% 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 date and dividend amount from string
DATEvar = buff_line(1:commas(1)-1);
DIVvar = str2num( buff_line(commas(1)+1:end) );
DATEtemp{ptr,1} = DATEvar;
DIVtemp(ptr,1) = DIVvar;
ptr = ptr + 1;
end
if ptr==1 %only if header is returned, implying no data exist in remaining buffer
%In case company never issued dividends, return blank values
hist_div_date={};
hist_div= [];
else %if company did issue dividends
% Reverse to normal chronological order, so 1st entry is oldest data point
hist_div_date = flipud(DATEtemp);
hist_div = flipud(DIVtemp);
end
_______________ UPDATE: I believe I may have found what the issue is: It appears Yahoo has some spotty coverage of this ticker symbol. When I manually enter the URL that the script would call:
Sometimes it would download the csv file, and sometimes I will get a 404 error.
Is there a way to repeat trying to get the csv file for a set number of attempts, and a way to abort if the csv is not returned in the max number of attempts?
______________ UPDATE: I think I found a workaround: I use the try-catch syntax to inside of a for loop to attempt pulling down the data.
However, The buffer read operation is still very slow, I could definitely use the help!

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!