Downloading yahoo stock data using hist_stock_data

31 Ansichten (letzte 30 Tage)
maor yehoshua
maor yehoshua am 7 Mai 2020
Kommentiert: Mario Trevino am 17 Jul. 2020
Hello,
I'm using hist_stock_data to download historical data from yahoo.
For most of the stocks it work fine, but for some stock (about 10-15% of the stock, e.g. ALGN), the command line:
[temp, status] = urlread(url,'post',{'matlabstockdata@yahoo.com', 'historical stocks'},'Timeout',10);
returns:
temp =
0×0 empty char array
But if I check at yahoo website the historical data is fine.
Does anyone familiar with this problem?
Thank you,
Maor
  3 Kommentare
maor yehoshua
maor yehoshua am 7 Mai 2020
Bearbeitet: Rik am 7 Mai 2020
Hello Rik,
Thanks for your help.
I ran this lines, for example:
stock_data1 = hist_stock_data('01012015', '08052020', 'AMAT'); %- this line will return data
stock_data1 = hist_stock_data('01012015', '08052020', 'NFLX'); %- this line will not reutrn any data
I'm using the following function:
function stocks = hist_stock_data(start_date, end_date, varargin)
% NOTE
% This is a modified version of the original file.
% Please download the file from Matlab file exchange:
% https://la.mathworks.com/matlabcentral/fileexchange/18458-hist-stock-data-start-date--end-date--varargin-
% HIST_STOCK_DATA Obtain historical stock data
% hist_stock_data(X,Y,'Ticker1','Ticker2',...) retrieves historical stock
% data for the ticker symbols Ticker1, Ticker2, etc... between the dates
% specified by X and Y. X and Y can either be strings in the format
% ddmmyyyy or Matlab datenums, where X is the beginning date and Y is the
% ending date. The program returns the stock data in a structure giving
% the Date, Open, High, Low, Close, Volume, and Adjusted Close price
% adjusted for dividends and splits.
%
% hist_stock_data(X,Y,'tickers.txt') retrieves historical stock data
% using the ticker symbols found in the user-defined text file. Ticker
% symbols must be separated by line feeds.
%
% hist_stock_data(X,Y,{'Ticker1' 'Ticker2'}) combined the ticker symbols
% into a single cell array when calling hist_stock_data (sometimes easier
% for calling the funtion with a cell array of ticker symbols).
%
% hist_stock_data(X,Y,'Ticker1','frequency',FREQ) retrieves historical
% stock data using the frequency specified by FREQ, which must be either
% 'd' for daily, 'wk' for weekly, or 'mo' for monthly.
%
% hist_stock_data(X,Y,'Ticker1','type','div') retrieves dividend data. If
% anything but 'div' is specified then it will default to retrieving
% historical prices.
%
% EXAMPLES
% stocks = hist_stock_data('23012003','15042008','GOOG','C');
% Returns the structure array 'stocks' that holds historical
% stock data for Google and CitiBank for dates from January
% 23, 2003 to April 15, 2008.
%
% stocks = hist_stock_data('12101997','18092001','tickers.txt');
% Returns the structure arrary 'stocks' which holds historical
% stock data for the ticker symbols listed in the text file
% 'tickers.txt' for dates from October 12, 1997 to September 18,
% 2001. The text file must be a column of ticker symbols
% separated by new lines.
%
% stocks = hist_stock_data(now-10, now, {'GOOG' 'C'});
% Get stock data for approximately the last 10 days for the two
% tickers specified in the cell array.
%
% stocks = hist_stock_data('12101997','18092001','C','frequency','w')
% Returns historical stock data for Citibank using the date range
% specified with a frequency of weeks. Possible values for
% frequency are d (daily), wk (weekly), or mo (monthly). If not
% specified, the default frequency is daily.
%
% stocks = hist_stock_data('12101997','18092001','C','type','div')
% Returned historical dividend data for Citibank between Oct 12,
% 1997 and September 18, 2001.
%
% DATA STRUCTURE
% INPUT DATA STRUCTURE FORMAT
% X (start date) ddmmyyyy String
% Y (end date) ddmmyyyy String
% Ticker NA String
% ticker.txt NA Text file
% FREQ NA String; 'd', 'wk', or 'mo'
% TYPE NA String; 'div'
%
% OUTPUT FORMAT
% All data is output in the structure 'stocks'. Each structure
% element will contain the ticker name, then vectors consisting of
% the organized data sorted by date, followed by the Open, High, Low,
% Close, Volume, then Adjusted Close prices.
%
% DATA FEED
% The historical stock data is obtained using Yahoo! Finance website.
% By using Yahoo! Finance, you agree not to redistribute the
% information found therein. Therefore, this program is for personal
% use only, and any information that you obtain may not be
% redistributed.
%
% NOTE
% This program uses the Matlab command urlread in a very basic form.
% If the program gives you an error and does not retrieve the stock
% information, it is most likely because there is a problem with the
% urlread command. You may have to tweak the code to let the program
% connect to the internet and retrieve the data.
% Created by Josiah Renfree
% January 25, 2008
stocks = struct([]); % initialize data structure
%% Parse inputs
% Format start and end dates into Posix times. This is the number of
% seconds since Jan 1, 1970. This previously used the posixtime function,
% but since that is relatively new, it now does the calculation using
% Matlab datenum's, which are in units of days, then converting to seconds.
origDate = datenum('01-Jan-1970 00:00:00', 'dd-mmm-yyyy HH:MM:SS');
% Convert input dates to Matlab datenums, if necessary
if ischar(start_date)
startDate = (datenum(start_date, 'ddmmyyyy') - origDate) * 24 * 60 * 60;
else
startDate = (floor(start_date) - origDate) * 24 * 60 * 60;
end
if ischar(end_date)
endDate = (datenum(end_date, 'ddmmyyyy') - origDate) * 24 * 60 * 60;
else
endDate = (floor(end_date) - origDate) * 24 * 60 * 60;
end
% determine if user specified frequency
temp = find(strcmp(varargin,'frequency') == 1); % search for frequency
if isempty(temp) % if not given
freq = 'd'; % default is daily
else % if user supplies frequency
freq = varargin{temp+1}; % assign to user input
varargin(temp:temp+1) = []; % remove from varargin
end
clear temp
% determine if user specified event type
temp = find(strcmp(varargin,'type') == 1); % search for frequency
if isempty(temp) % if not given
event = 'history'; % default is historical prices
else % if user supplies frequency
event = varargin{temp+1}; % assign to user input
varargin(temp:temp+1) = []; % remove from varargin
end
clear temp
% If the first cell of varargin is itself a cell array, assume it is a cell
% array of ticker symbols
if iscell(varargin{1})
tickers = varargin{1};
% Otherwise, check to see if it's a .txt file
elseif ~isempty(strfind(varargin{1},'.txt'))
fid = fopen(varargin{1}, 'r');
tickers = textscan(fid, '%s'); tickers = tickers{:};
fclose(fid);
% Otherwise, assume it's either a single ticker or a list of tickers
else
tickers = varargin;
end
%% Get historical data
%h = waitbar(0, 'Please Wait...'); % create waitbar
idx = 1; % idx for current stock data
% Cycle through each ticker symbol and retrieve historical data
for i = 1:length(tickers)
% Update waitbar to display current ticker
% waitbar((i-1)/length(tickers), h, ...
% sprintf('Retrieving stock data for %s (%0.2f%%)', ...
% tickers{i}, (i-1)*100/length(tickers)))
% Create url string for retrieving data
url = sprintf(['https://query1.finance.yahoo.com/v7/finance/download/', ...
'%s?period1=%d&period2=%d&interval=1%s&events=%s'], ...
tickers{i}, startDate, endDate, freq, event);
% Call data from Yahoo Finance
[temp, status] = urlread(url,'post',{'matlabstockdata@yahoo.com', 'historical stocks'},'Timeout',10);
% If data was downloaded successfully, then proceed to process it.
% Otherwise, ignore this ticker symbol
if status
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
%Seccin modificada de la funcin original
[date, op, high, low, cl, adj_close, volume] = ...
strread(temp(43:end),'%s%s%s%s%s%s%s','delimiter',',');
stocks(idx).Ticker = tickers{i}; % get ticker symbol
stocks(idx).Date = date; % save date data
stocks(idx).Open = str2double(op); % save opening price data
stocks(idx).High = str2double(high); % save high price data
stocks(idx).Low = str2double(low); % save low price data
stocks(idx).Close = str2double(cl); % save closing price data
stocks(idx).AdjClose = str2double(adj_close); % save adjustied close data
stocks(idx).Volume = str2double(volume); % save volume data
%Busca ndice cuando el volumen es 0 y elimina el ndice de cada lista
% posicion=find(isnan(stocks(idx).Volume));
% stocks(idx).Volume(posicion)=[];
% stocks(idx).Date(posicion)=[];
% stocks(idx).Open(posicion)=[];
% stocks(idx).High(posicion)=[];
% stocks(idx).Low(posicion)=[];
% stocks(idx).Close(posicion)=[];
% stocks(idx).AdjClose(posicion)=[];
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
%Esta seccin comentada es la que se sustituy por la seccin anterior
% Put data into appropriate variables
% % % if strcmp(event, 'history') % If historical prices
% % %
% % % % Parse out the historical data
% % % data = textscan(temp, '%s%f%f%f%f%f%f', 'delimiter', ',', ...
% % % 'Headerlines', 1);
% % %
% % % [stocks(idx).Date, stocks(idx).Open, stocks(idx).High, ...
% % % stocks(idx).Low, stocks(idx).Close, ...
% % % stocks(idx).AdjClose, stocks(idx).Volume] = deal(data{:});
% % %
% % % % If dividends
% % % else
% % %
% % % % Parse out the dividend data
% % % data = textscan(temp, '%s%f', 'delimiter', ',', ...
% % % 'Headerlines', 1);
% % %
% % % [stocks(idx).Date, stocks(idx).Dividend] = deal(data{:});
% end
% % % stocks(idx).Ticker = tickers{i}; % Store ticker symbol
idx = idx + 1; % Increment stock index
end
% update waitbar
%waitbar(i/length(tickers),h)
end
%close(h) % close waitbar
Mario Trevino
Mario Trevino am 17 Jul. 2020
I just posted the same question....
something is wrong when reading the url string.. but it works fine when copy-pasting it in a browser... right?
Did you find the solution?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by