-
Notifications
You must be signed in to change notification settings - Fork 20
/
getMarketDataViaYahoo.m
71 lines (67 loc) · 2.6 KB
/
getMarketDataViaYahoo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function data = getMarketDataViaYahoo(symbol, startdate, enddate, interval)
% Downloads market data from Yahoo Finance for a specified symbol and
% time range.
%
% INPUT:
% symbol - is a ticker symbol i.e. 'AMD', 'BTC-USD'
% startdate - the date from which the market data will be requested
% enddate - the market data will be requested till this date
% interval - the market data will be returned in this intervals
% supported intervals are '1d', '5d', '1wk', '1mo', '3mo'
%
% OUTPUT:
% data - is a retrieved dataset returned as a table
%
% Example:
% data = getMarketDataViaYahoo('AMD', '1-Jan-2018', datetime('today'), '5d');
%
% Author: Artem Lenskiy, PhD
% Version: 1.13
%
% Special thanks to Patryk Dwórznik (https://github.com/dworznik) for
% a hint on JavaScript processing.
%
% Alternative approach is given here
% https://stackoverflow.com/questions/50813539/user-agent-cookie-workaround-to-web-scraping-in-matlab
%
% Another approach taken form WFAToolbox is to send a post request as
% follows:
% urlread(url, 'post',{'matlabstockdata@yahoo.com', 'historical stocks'})
if(nargin() == 1)
startdate = posixtime(datetime('1-Jan-2018'));
enddate = posixtime(datetime()); % now
interval = '1d';
elseif (nargin() == 2)
startdate = posixtime(datetime(startdate));
enddate = posixtime(datetime()); % now
interval = '1d';
elseif (nargin() == 3)
startdate = posixtime(datetime(startdate));
enddate = posixtime(datetime(enddate));
interval = '1d';
elseif(nargin() == 4)
startdate = posixtime(datetime(startdate));
enddate = posixtime(datetime(enddate));
else
error('At least one parameter is required. Specify ticker symbol.');
data = [];
return;
end
%% Send a request for data
% Construct an URL for the specific data
uri = matlab.net.URI(['https://query1.finance.yahoo.com/v7/finance/download/', upper(symbol)],...
'period1', num2str(int64(startdate), '%.10g'),...
'period2', num2str(int64(enddate), '%.10g'),...
'interval', interval,...
'events', 'history',...
'frequency', interval,...
'guccounter', 1,...
'includeAdjustedClose', 'true');
options = weboptions('ContentType','table', 'UserAgent', 'Mozilla/5.0');
try
data = rmmissing(webread(uri.EncodedURI, options));
catch ME
data = [];
warning(['Identifier: ', ME.identifier, 'Message: ', ME.message])
end
end