clear all close all clc set(0,'defaulttextfontsize',12); set(0,'defaultaxesfontsize',12); set(0,'defaultlinelinewidth',2); set(0,'defaultlinemarkersize',12); tickers={'SPY', 'DIA', 'QQQ', 'IWM'}; start_val=1; for i0=1:length(tickers) Y0=getMarketDataViaYahoo(char(tickers(i0)),datetime('today')-365/12,datetime('today'),'1d'); 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) Y(4).Close./Y(4).Close(start_val)]) legend('SPY', 'DIA', 'QQQ', 'IWM','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 Y(4).Close(end)./Y(4).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]); text(datenum(Y(4).Date(end))+1,1000*Y(4).Close(end)./Y(4).Close(1),[num2str(ROI(4),3) ' [%]'],... 'BackgroundColor', [1 1 1]); xlim([Y(1).Date(start_val)-5 Y(1).Date(end)+5]) %% tickers_mtx={'spy','dia','qqq','iwm','tlt','lqd','mub','mbb','hyg','ezu','ita',... 'icln','ixj','ibb','iyh','iyr','itb','iyk','ifra','iyf','iye',... 'idu','iyt','iym','iyc','ihe'}; Y_MTX=[]; RSI_MTX=[]; for i0=1:length(tickers_mtx) Y0=getMarketDataViaYahoo(char(tickers_mtx(i0)),datetime('today')-365/12,datetime('today'),'1d'); 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_mtx(i0)); Y(i0).ROI = Y(i0).Close(end)./Y(i0).Close(1); Y_MTX=[Y_MTX Y(i0).Close]; RSI=rsindex(Y(i0).Close); RSI_MTX=[RSI_MTX; RSI(end)]; end Y_MTX=Y_MTX./Y_MTX(1,:); %% ROI_EoM=Y_MTX(end,:)'; [ROI_EoM_Sorted, Index]=sort(ROI_EoM); TableROI_EoM=table(tickers_mtx(Index)',ROI_EoM_Sorted, RSI_MTX(Index)); TableROI_EoM = renamevars(TableROI_EoM,["Var1","ROI_EoM_Sorted","Var3"], ... ["Ticker","1M Return","RSI"]); openvar('TableROI_EoM') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Functions are define below function percentile_rank=percentile_rank_calculator(X) X_steps=linspace(min(X),max(X),1000)'; percentaile_rank=[]; for i0=1:length(X_steps) counter=0; for i1=1:length(X) if X(i1)<=X_steps(i0) counter=counter+1; end end percentaile_rank=[percentaile_rank; counter/length(X)]; end X_rank=interp1(X_steps,percentaile_rank,X); percentile_rank=X_rank; end 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