clear all close all clc set(0,'DefaultTextFontSize',12); set(0,'DefaultAxesFontSize',12); set(0,'DefaultLineLineWidth',2); set(0,'DefaultLineMarkerSize',12); delete VIX9D.csv delete VIX3M.csv delete VIX6M.csv websave('VIX9D.csv','https://cdn.cboe.com/api/global/us_indices/daily_prices/VIX9D_History.csv','Timeout',Inf); M9D=readtable('VIX9D.csv'); Date9VIX=datenum(table2array(M9D(:,1))); Val9VIX=table2array(M9D(:,5)); websave('VIX3M.csv','https://cdn.cboe.com/api/global/us_indices/daily_prices/VIX3M_History.csv','Timeout',Inf); M3M=readtable('VIX3M.csv'); Date3VIX=datenum(table2array(M3M(:,1))); Val3VIX=table2array(M3M(:,5)); websave('VIX6M.csv','https://cdn.cboe.com/api/global/us_indices/daily_prices/VIX6M_History.csv','Timeout',Inf); M6M=readtable('VIX6M.csv'); Date6VIX=datenum(table2array(M6M(:,1))); Val6VIX=table2array(M6M(:,5)); Y0=getMarketDataViaYahoo('^VIX','04-Jan-2011',datetime('today'),'1d'); DateVIX= datenum(Y0{:, 1}); VIX= table2array(Y0(:, 5)); TF=strcmp(datestr(DateVIX(end)),string(datetime('today'))); if TF==1 DateVIX=DateVIX(1:end-1); VIX=VIX(1:end-1); end Date9VIXsync=[]; Val9VIXsync=[]; Date3VIXsync=[]; Val3VIXsync=[]; Date6VIXsync=[]; Val6VIXsync=[]; Slope=[]; for i0=1:length(VIX) [val,pos]=min(abs(Date9VIX-DateVIX(i0))); Date9VIXsync=[Date9VIXsync; Date9VIX(pos)]; Val9VIXsync=[Val9VIXsync; Val9VIX(pos)/VIX(i0)]; [val,pos]=min(abs(Date3VIX-DateVIX(i0))); Date3VIXsync=[Date3VIXsync; Date3VIX(pos)]; Val3VIXsync=[Val3VIXsync; Val3VIX(pos)/VIX(i0)]; [val,pos]=min(abs(Date6VIX-DateVIX(i0))); Date6VIXsync=[Date6VIXsync; Date6VIX(pos)]; Val6VIXsync=[Val6VIXsync; Val6VIX(pos)/VIX(i0)]; % P = polyfit([1 2 3],[Val9VIXsync(i0) Val3VIXsync(i0) Val6VIXsync(i0)],1); % Slope=[Slope; P(1)]; end Rank9D=100*percentile_rank_calculator(Val9VIXsync); Rank3M=100*percentile_rank_calculator(Val3VIXsync); Rank6M=100*percentile_rank_calculator(Val6VIXsync); figure(length(findobj('type','figure'))+1) plot(DateVIX, [Rank9D Rank3M Rank6M]) ylabel('Percentile Rank [%]') legend(['VIX9D/VIX: ' num2str(Rank9D(end),2) ' [%]'], ['VIX3M/VIX: ' num2str(Rank3M(end),2) ' [%]'], ['VIX6M/VIX: ' num2str(Rank6M(end),2) ' [%]']) datetick('x',10) %%% Functions are defined 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