1

我正在尝试从爱尔兰证券交易所网站提取股票价格。使用该函数的行出现错误datenum()

function get_ise_data(equity_name,equity,start_date,end_date)

% Example:
%   get_ise_data('CandC',22076,'01-Jan-2010','10-Jan-2010')
% Note:

% This program reads data from the ISE website. It creates a vector of
% trading days, and then requests data for each specific day from the
% website

% Specify the equity name and reference number, start and end dates as in
% the example above.

% C&C corresponds to the reference number 22076 and you can check this on ise.ie. Kerry 
% Group and BOI have similar reference numbers.
% The data is saved to a file called "[stock]data.mat". The first
% column is the date in serial number format. You can convert to string
% format using "datestr()" The graph produced is not automatically saved.
% The data is also saved to an excel file by the same name. When viewing in
% Excel we need to change the format of the first column to 'custom'
% and specify format as "hh:mm dd/mm/yy".
% Close the excel file before running the program.

% Convert start and end dates to serial format:
start_date = datenum(start_date, 'dd­-mmm­-yyyy');
end_date = datenum(end_date, 'dd­-mmm-­yyyy');

clear final_data

% Work out relevant trading days:

bdates = busdays(start_date, end_date, 'daily');

% The above assumes the same trading days as NYSE - need to modify this for the ISE. See the
% documentation of "busday" to change the default holidays.

itterations=length(bdates);
count=0;

% Loop over each trading day:

for i=1:itterations
    [year,month,date]=datevec(bdates(i));
    str1='http://www.ise.ie/Prices,-Indices-Stats/Equity­Market-Data/EquityDetails/?equity=';
    str2=sprintf('%d&start_day=%d&start_month=%d&start_year=%d',equity,date,month,year);
    url=sprintf('%s%s',str1,str2);
    raw=urlread(url); % Read in html code from website
    exp='<tr?\w+.*?>([\d]+:[\d]+)</\w+.*?>([%\­\d/:A-­Z\.]+)</\w+.*?>([%\­\d/:A­Z\.]+)</\w+.*?>([%\-\d/:A-Z\.]+)</\w+.*?>([%\-\d/:A­Z\.]+).*?/tr>';
    % The following extracts the parts of the html code that match the
    % format specified by 'exp':
    data = regexp(raw,exp,'tokens','freespacing');
    number_of_quotes=length(data);

    % Print out current day:
    sprintf('Now reading: date: %d, month: %d',date,month)
    % A number of quotes are given each day. Loop through these:

    for j=1:number_of_quotes
% Determine the serial date number of each quote:
time=data{1,j}(1,1);

hour=regexp(time{1,1},'([\d]+):[\d]+','tokens');
minute=regexp(time{1,1},'[\d]+:([\d]+)','tokens');
hour = str2double(cell2mat(hour{1,1}));
minute = str2double(cell2mat(minute{1,1}));
second=0;
time_serial _number=datenum(year,month,date,hour,minute,second);
price=str2double(cell2mat(data{1,j}(1,3)));
% Save the price and date number to temporary file, while ignoring
% missing data:
if ~isnan(price)
count=count+1;
final_data(count,1)=time_serial_number;
final_data(count,2)=price;
end
end % End of inter-­?day loop
end % End of daily loop
str=sprintf('% s_data',equity_name);
% Save data to hard disk:
save(str,'final_data');
xlswrite(str,final_data)
end

我得到的错误是

??? Error using ==> datenum at 182
DATENUM failed.

Error in ==> get_ise_data at 25
start_date = datenum(start_date, 'dd­-mmm-­yyyy');

Caused by:
    Error using ==> dtstr2dtnummx
    Failed on converting date string to date number.

有人有什么主意吗?我想我已经在评论中解释了一切。

露丝

4

1 回答 1

0

该错误是由于您的日期字符串不符合此处指定的格式(因此错误消息说明这一点):

start_date = datenum(start_date, 'dd­-mmm­-yyyy');

检查网站返回的日期字符串格式是否正确。在欧洲国家,通常以以下格式返回财务日期:DD/MM/YY 或 DD/MM/YYYY

在报告错误的行设置断点并检查传递给 datenum 的数据。

于 2013-05-06T13:07:26.620 回答