Getting Historical Data from Yahoo
8 views (last 30 days)
Show older comments
Hi all,
Could you please help me to fix code, which is taken partly from a book, below. It gives an error "The server returned the status 401 with message "Unauthorized" in response to the request to URL https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=%EF%BF%BF&period2=%EF%BF%BF&interval=1devents=history&crumb=GAAvT/4ue2V."
Thanks a lot.
function [date, open, high, low, close, vol, adjclose]= downloadyahoo(symbol, period, start_date, end_date)
% download the data from finance.yahoo.com
% period can be '1d' for daily data, '1wk' for weekly data, '1mo' for monthly
% data
origDate = datenum('01-Jan-1970 00:00:00', 'dd-mmm-yyyy HH:MM:SS');
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
% Create URL string and download csv file
url_string = ['https://query1.finance.yahoo.com/v7/finance/download/' upper(symbol) '?period1=' startDate '&period2=' end_date '&interval=' period '&events=history&crumb=GAAvT/4ue2V'];
urlwrite(url_string,['data_' upper(symbol) '.csv']);
import=importdata(['data_' upper(symbol) '.csv']);
delete(['data_' upper(symbol) '.csv']);
% Reverse to normal chronological order, so 1st entry is oldest data point
open = flipud(import.data(:,1));
high = flipud(import.data(:,2));
low = flipud(import.data(:,3));
close = flipud(import.data(:,4));
vol = flipud(import.data(:,5));
adjclose = flipud(import.data(:,6));
for a=length(open):-1:1
date(length(open)-a+1)=datenum((import.textdata{a+1}),'yyyy-mm-dd');
end
end
0 Comments
Answers (0)
See Also
Categories
Find more on Web Services in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!