-
Notifications
You must be signed in to change notification settings - Fork 195
/
monteCarloOptimLeverage.m
81 lines (58 loc) · 2.82 KB
/
monteCarloOptimLeverage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
clear;
load('../Data/AUDCAD_unequal_ret', 'ret');
moments={mean(ret), std(ret), skewness(ret), kurtosis(ret)};
[ret_sim, type]=pearsrnd(moments{:}, 100000, 1);
g=inline('sum(log(1+f*R))/length(R)', 'f', 'R');
% g=inline('prod(1+f*R)^(1/length(R))-1', 'f', 'R');
myf=0:23;
myg=NaN(24, 1);
for f=myf
myg(f+1)=g(f, ret_sim);
end
plot(myf, myg);
minusG=@(f)-g(f, ret);
minusGsim=@(f)-g(f, ret_sim);
optimalF=fminbnd(minusGsim, 0, 20); % optimal leverage based on simulated returns
fprintf(1, 'Optimal leverage=%f optimal growth rate=%f\n', optimalF, -minusGsim(optimalF));
minR=min(ret_sim); % minimum return in simulated series
fprintf(1, 'minR=%f\n', minR);
maxDD=calculateMaxDD(cumprod(1+optimalF*ret_sim)-1); % max drawdown with optimal leverage
fprintf(1, 'f=%i maxDD with optimal leverage=%f\n', optimalF, maxDD);
maxDD=calculateMaxDD(cumprod(1+optimalF/2*ret_sim)-1); % max drawdown with half of optimal leverage
fprintf(1, 'f=%i maxDD with half of optimal leverage=%f\n', optimalF/2, maxDD);
maxDD=calculateMaxDD(cumprod(1+optimalF/7*ret_sim)-1); % max drawdown with 1/7 of optimal leverage
fprintf(1, 'f=%i maxDD with 1/7 of optimal leverage=%f\n', optimalF/7, maxDD);
maxDD=calculateMaxDD(cumprod(1+optimalF/1.4*ret)-1); % max drawdown with 1/1.4 of optimal leverage for historical returns
fprintf(1, 'f=%i maxDD for historical returns=%f\n', optimalF/1.4, maxDD);
D=0.5;
fprintf(1, 'Growth rate on simulated returns using D=%3.1f of optimal leverage on full account=%f\n', D, -minusGsim(optimalF*D));
fprintf(1, 'MaxDD on simulated returns using D of optimal leverage on full account=%f\n', calculateMaxDD(cumprod(1+optimalF*D*ret_sim)-1));
% CPPI
g_cppi=0;
% g_debug=0;
drawdown=0;
for t=1:length(ret_sim)
g_cppi=g_cppi+log(1+ret_sim(t)*D*optimalF*(1+drawdown));
% g_cppi=(1+g_cppi)*(1+r(t)*D*optimalF*(1+drawdown))-1;
% g_debug=g_debug+log(1+r(t)*D*optimalF)
% if (g_cppi/t >= 1)
% keyboard;
% end
drawdown=min(0, (1+drawdown)*(1+ret_sim(t))-1);
end
g_cppi=g_cppi/length(ret_sim);
% g_cppi=(1+g_cppi)^(1/length(r))-1;
fprintf(1, 'Growth rate on simulated returns using CPPI=%f\n', g_cppi);
fprintf(1, 'Growth rate on historical returns using D of optimal leverage on full account=%f\n', -minusG(optimalF*D));
fprintf(1, 'MaxDD on historical returns using D of optimal leverage on full account=%f\n', calculateMaxDD(cumprod(1+optimalF*D*ret)-1));
% CPPI
g_cppi=0;
drawdown=0;
for t=1:length(ret)
% g_cppi=(1+g_cppi)*(1+ret(t)*D*optimalF*(1+drawdown))-1;
g_cppi=g_cppi+log(1+r(t)*D*optimalF*(1+drawdown));
drawdown=min(0, (1+drawdown)*(1+ret(t))-1);
end
g_cppi=g_cppi/length(ret);
% g_cppi=(1+g_cppi)^(1/length(ret))-1;
fprintf(1, 'Growth rate on historical returns using CPPI=%f\n', g_cppi);