% housekeeping:
clear all
addpath('./market-data-functions-master');
%Chose Asset:
initDate = '1-Jan-1987';
symbol = '^HSI';
name = 'Hang Seng';

% Scrape Yahoo and Plot Price Series
symb = getMarketDataViaYahoo(symbol, initDate);
dates = symb(:,1).Date;
symbol = timeseries([symb.Close], datestr(symb(:,1).Date));

%Make Return Series and Print Statistics
ret = (symbol.Data(2:end) - symbol.Data(1:end-1)) ./ (symbol.Data(1:end-1));
logRet = log(symbol.Data(2:end) ./ symbol.Data(1:end-1));

% cycle through dates
mn = [];
sd = [];
s = [];
k = [];
acf1 = [];
acf2 = [];
acf5 = [];
acf20 = [];
for i = 253:length(ret)
    trailingRet = 100*ret(i-252:i);
    mn = [mn; mean(trailingRet)];
    sd = [sd; std(trailingRet)];
    s = [s; skewness(trailingRet)];
    k = [k; kurtosis(trailingRet)];
    acf = autocorr(trailingRet,20);
    acf1 = [acf1; acf(2)];
    acf2 = [acf2; acf(3)];
    acf5 = [acf5; acf(6)];
    acf20 = [acf20; acf(21)];
end
N = length(ret) - 252;
dts = dates(254:end);

figure(1);
plot(dts,100*mn);
hold on;
plot(dts,zeros(N,1),'color',[0 0 0],'lineStyle',':');
title('Trailing Mean of Hang Seng');
ylabel('Basis Points');
xlabel('days');
box off;
hold off;

figure(2);
plot(dts,100*sd);
hold on;
title('Trailing Standard Error of Hang Seng');
ylabel('Basis Points');
xlabel('days');
box off;
hold off;


figure(3);
plot(dts,100*s);
hold on;
plot(dts,zeros(N,1),'color',[0 0 0],'lineStyle',':');
title('Trailing Skew of Hang Seng');
ylabel('Basis Points');
xlabel('days');
box off;
hold off;

figure(4);
plot(dts,100*k);
hold on;
title('Trailing Kurtosis of Hang Seng');
ylabel('Basis Points');
xlabel('days');
box off;
hold off;

% ACF plot
figure(5);
plot(dts,acf1,'DisplayName','ACF1');
hold on;
plot(dts,acf2,'DisplayName','ACF2');
plot(dts,acf5,'DisplayName','ACF5');
plot(dts,acf20,'DisplayName','ACF20');
title('Trailing AutoCorrelations');
legend('Location','best');
legend('boxoff');
xlabel('days');
box off;
plot(dts,ones(N,1).*[1.96/sqrt(252)],'color',[0.5 0.5 0.5],'HandleVisibility','off','LineWidth',1);
plot(dts,ones(N,1).*[-1.96/sqrt(252)],'color',[0.5 0.5 0.5],'HandleVisibility','off','LineWidth',1);
hold off; 


figure(6);
plot(symbol);
xtickformat('yyyy');
ylabel(name);
title( sprintf('%s 2006 - Present' , name) );
box off