clear all close all clc set(0,'defaulttextfontsize',12); set(0,'defaultaxesfontsize',12); set(0,'defaultlinelinewidth',2); set(0,'defaultlinemarkersize',12); tickers={'^gspc', 'IEF', 'GLD', '^VIX'}; start_val=1; for i0=1:length(tickers) Y0=getMarketDataViaYahoo(char(tickers(i0)),datestr(datenum(char('08/31/2023'))),datetime('today'),'1d'); % Y(i0).Date = Y0{:, 1}; % Y(i0).Open = Y0{:, 2}; % Y(i0).High = Y0{:, 3}; % Y(i0).Low = Y0{:, 4}; % Y(i0).Close = Y0{:, 5}; % Y(i0).AdjClose = Y0{:, 6}; % Y(i0).Volume = Y0{:, 7}; % Y(i0).Ticker = char(ticker(i0)); Y(i0).Date = datenum(Y0{:, 1}); Y(i0).Open = table2array(Y0(:, 2)); Y(i0).High = table2array(Y0(:, 3)); Y(i0).Low = table2array(Y0(:, 4)); Y(i0).Close = table2array(Y0(:, 5)); Y(i0).AdjClose = table2array(Y0(:, 6)); Y(i0).Volume = table2array(Y0(:, 7)); Y(i0).Ticker = char(tickers(i0)); end %% figure(length(findobj('type','figure'))+1) subplot(1,2,1) plot(datenum(Y(1).Date),1000*[Y(1).Close./Y(1).Close(start_val) Y(2).Close./Y(2).Close(start_val) Y(3).Close./Y(3).Close(start_val)],... datenum(Y(1).Date),1000*ones(size(Y(1).Close./Y(1).Close(start_val))),'k') legend('S&P500','US 7-10 Years T-Bond','Gold','Location','SouthWest') ylabel('Return of $1,000 [USD]') grid on datetick('x',6) ROI=100*[Y(1).Close(end)./Y(1).Close(1)-1 Y(2).Close(end)./Y(2).Close(1)-1 Y(3).Close(end)./Y(3).Close(1)-1]; text(datenum(Y(1).Date(end))+1,1000*Y(1).Close(end)./Y(1).Close(1),[num2str(ROI(1),3) ' [%]'],... 'BackgroundColor', [1 1 1]); text(datenum(Y(2).Date(end))+1,1000*Y(2).Close(end)./Y(2).Close(1),[num2str(ROI(2),3) ' [%]'],... 'BackgroundColor', [1 1 1]); text(datenum(Y(3).Date(end))+1,1000*Y(3).Close(end)./Y(3).Close(1),[num2str(ROI(3),3) ' [%]'],... 'BackgroundColor', [1 1 1]); xlim([Y(1).Date(start_val)-5 Y(1).Date(end)+5]) subplot(1,2,2) plot(datenum(Y(4).Date),Y(4).Close) title('Market Volatility') ylabel('VIX [%]') datetick('x',6) ylim([10 40]) grid on x0=0; y0=0; width=1100; height=450; set(gcf,'position',[x0,y0,width,height]) xlim([Y(1).Date(start_val)-5 Y(1).Date(end)+5]) % figure(length(findobj('type','figure'))+1) % plot(datenum(Y(1).Date),1000*[Y(1).Close./Y(1).Close(start_val) Y(2).Close./Y(2).Close(start_val) Y(3).Close./Y(3).Close(start_val)],... % datenum(Y(1).Date),1000*ones(size(Y(1).Close./Y(1).Close(start_val))),'k') % legend('S&P500','US 7-10 Years T-Bond','Gold','Location','SouthWest') % ylabel('Return of $1,000 [USD]') % grid on % datetick('x',6) % ROI=100*[Y(1).Close(end)./Y(1).Close(1)-1 Y(2).Close(end)./Y(2).Close(1)-1 Y(3).Close(end)./Y(3).Close(1)-1]; % text(datenum(Y(1).Date(end))+1,1000*Y(1).Close(end)./Y(1).Close(1)+0.75,[num2str(ROI(1),3) ' [%]'],... % 'BackgroundColor', [1 1 1]); % text(datenum(Y(2).Date(end))+1,1000*Y(2).Close(end)./Y(2).Close(1),[num2str(ROI(2),3) ' [%]'],... % 'BackgroundColor', [1 1 1]); % text(datenum(Y(3).Date(end))+1,1000*Y(3).Close(end)./Y(3).Close(1),[num2str(ROI(3),3) ' [%]'],... % 'BackgroundColor', [1 1 1]); % xlim([Y(1).Date(start_val)-5 Y(1).Date(end)+5]) 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