%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
symbol = getMarketDataViaYahoo(symbol, initDate);
dates = symbol(:,1).Date;
open = symbol(:,2).Open;
symbol = timeseries([symbol.Close], datestr(symbol(:,1).Date));

%Make Return Series
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));

%Calculate MAs etc
P = symbol.Data;
lags = [5, 10, 20, 50, 100, 200];
MA = [];
EMA = [];
BU = [];
BL = [];
for lag = lags
    movAvg = dsp.MovingAverage(lag);
    tempMA = movAvg(P);
    MA = [MA tempMA];
    clear middle upper lower;
    [middle,upper,lower] = bollinger(P,'WindowSize',lag);
    BU = [BU upper];
    BL = [BL lower];
    movAvg = dsp.MovingAverage(lag,'Method','Exponential weighting','ForgettingFactor',1-2/(1+lag));
    EMA = [EMA movAvg(P)];
end

%Plot 200d MAs
figure(1);
plotRatio = 0.5;
plotRange = (round(length(P)*(1-plotRatio))):length(P);
MA200 = MA(:,6);
EMA200 = EMA(:,6);
hold on;
plot(dates(plotRange),P(plotRange),'DisplayName','Price');
plot(dates(plotRange),MA200(plotRange),'DisplayName','200d MA');
plot(dates(plotRange),EMA200(plotRange),'DisplayName','200d EMA');
xlabel('Days');
ylabel('');
set(gca,'ytick',[]);
title('Hang Seng with 200day MA and EMA');
%legend('Price','MA 200d','EMA 200d','Location','best');
legend('Location','best');
legend('boxoff');
box off;
box off;
hold off;

%Plot Bollinger Bands
plotLag = 6;
figure(2);
plotRatio = 0.99;
plotRange = (round(length(P)*(1-plotRatio))):length(P);
BUplot = BU(:,plotLag );
BLplot = BL(:,plotLag );
plot(dates(plotRange),P(plotRange));
title(sprintf('%dD Hang Seng Bollinger Bands', lags(plotLag)));
hold on;
plot(dates(plotRange),BUplot(plotRange));
plot(dates(plotRange),BLplot(plotRange));
hold off;

%Trading Strategy Returns
cost = 0.001;
maProfit = [];
maDelayedProfit = [];
maProfitCosts = [];
emaProfit = [];
BBprofit = [];
for k = 1:length(lags)
    %MA signal
    signal = P > MA(:,k);
    position = signal(1:end-1);
    strategyReturn = ret .* position;
    strategyCumRet = 100 * cumsum(strategyReturn);
    maProfit = [maProfit strategyCumRet];
    %MA with costs
    reversePosition = signal(1:end-1) ~= signal(2:end);
    strategyReturn = ret .* position - reversePosition*cost;
    strategyCumRet = 100 * cumsum(strategyReturn);
    maProfitCosts = [maProfitCosts strategyCumRet];
    %delayed MA profit
    signal = [0.5 ; signal(1:end-1)];
    position = (-1 + signal(1:end-1).*2);
    strategyReturn = ret .* position;
    strategyCumRet = 100 * cumsum(strategyReturn);
    maDelayedProfit = [maDelayedProfit strategyCumRet];
    %EMA signal
    signal = P > EMA(:,k);
    position = signal(1:end-1);
    strategyReturn = ret .* position;
    strategyCumRet = 100 * cumsum(strategyReturn);
    emaProfit = [emaProfit strategyCumRet];
    %Bollinger Band signal
    BBsignal = zeros(length(P),1);
    BBsignal(P > BU(:,k)) = 1;
    BBsignal(P < BL(:,k)) = -1;
    position = BBsignal(1:end-1);
    strategyReturn = ret .* position;
    strategyCumRet = 100 * cumsum(strategyReturn);
    BBprofit = [BBprofit strategyCumRet];
end

%MA strategy PnL plot
figure(3);
plot(dates(1:end-1),maProfit(:,1),'DisplayName',string(lags(1)) + "D");
hold on;
for k = 2:length(lags)
    plot(dates(1:end-1),maProfit(:,k),'DisplayName',string(lags(k)) + "D");
end
title('Profit from Momentum MA Trading Strategies');
legend('Location','best');
legend('boxoff');
ylabel('%');
box off;
hold off; 

%MA with costs strategy PnL plot
figure(4);
plot(dates(1:end-1),maProfitCosts(:,1),'DisplayName',string(lags(1)) + "D");
hold on;
for k = 2:length(lags)
    plot(dates(1:end-1),maProfitCosts(:,k),'DisplayName',string(lags(k)) + "D");
end
title('Profit from Momentum MA Trading Strategies With 10bp Costs');
legend('Location','best');
legend('boxoff');
ylabel('%');
box off;
hold off; 

%MA DELAYED strategy PnL plot
figure(5);
plot(dates(1:end-1),maDelayedProfit(:,1),'DisplayName',string(lags(1)) + "D");
hold on;
for k = 2:length(lags)
    plot(dates(1:end-1),maDelayedProfit(:,k),'DisplayName',string(lags(k)) + "D");
end
title('Profit from Delayed Momentum MA Trading Strategies');
legend('Location','best');
legend('boxoff');
ylabel('%');
box off;
hold off; 

%EMA strategy PnL plot
figure(6);
plot(dates(1:end-1),emaProfit(:,1),'DisplayName',string(lags(1)) + "D");
hold on;
for k = 2:length(lags)
    plot(dates(1:end-1),emaProfit(:,k),'DisplayName',string(lags(k)) + "D");
end
title('Profit from Momentum EMA Trading Strategies');
legend('Location','best');
legend('boxoff');
ylabel('%');
box off;
hold off; 


%BB strategy PnL plot
figure(7);
plot(dates(1:end-1),BBprofit(:,1),'DisplayName',string(lags(1)) + "D");
hold on;
for k = 2:length(lags)
    plot(dates(1:end-1),BBprofit(:,k),'DisplayName',string(lags(k)) + "D");
end
title('Profit from Momentum Bollinger Band Strategies');
legend('Location','best');
legend('boxoff');
ylabel('%');
box off;
hold off; 

    