-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReturnPrediction.m
More file actions
109 lines (82 loc) · 3.05 KB
/
Copy pathReturnPrediction.m
File metadata and controls
109 lines (82 loc) · 3.05 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
clear variables;
close all;
clc;
%% Historical Data
% Get all closing stock prices
[dateRange, stockIndex, P] = DataProcessing(5);
Pinit = P;
% Model parameters
numDays = 21;
numMonthsSimulated = 4;
numSimsPerMonth = 20;
numSims = 100;
investments = 0;
options = optimoptions('quadprog', 'Display', 'off');
n = 20; % number of stocks
for k=1:numSims
P = Pinit;
% Set initial arbitrary alpha
alpha = 0.5;
%alpha = rand;
% Set initial investment value
investment = 10000;
expReturn = zeros(numMonthsSimulated,numSimsPerMonth); % all expected simulated returns
for i=1:numMonthsSimulated
% Historical return ratio between days
R = (P(2:end,:)-P(1:end-1,:))./P(1:end-1,:);
% average return over entire period for all stocks
r = mean(R);
% the covariance of all our return ratios
C = cov(R);
if i ~= 1
wOld = w;
end
% Unlimited optimization
% [w, optVal] = quadprog((1-alpha)*2*C, -alpha*r, [], [], ones(1,n), [1], zeros(n,1), ones(n,1), [], options);
% Limited optimization
[w, optVal] = quadprog((1-alpha)*2*C, -alpha*r, [], [], ones(1,n), [1], zeros(n,1), 0.2*ones(n,1), [], options);
% 5% cost on trades
if i ~= 1
deltaW = w - wOld;
investment = investment - sum(abs(deltaW) * 0.05 * investment);
end
% Repeatedly simulate future stock prices and assess performance
for j=1:numSimsPerMonth
% generate future prices
Po = StockSimulation(P,numDays);
% Historical return ratio between days
R = (Po(2:end,:)-Po(1:end-1,:))./Po(1:end-1,:);
% average return over entire period for all stocks
r = mean(R);
expReturn(i,j) = r * w;
end
% "Real" next month stock prices
Po = StockSimulation(P,numDays);
P = [P(numDays+1:length(P),:)' flip(Po(2:numDays+1,:))' ]';
% Actual return based on simulated price
R = (Po(2:end,:)-Po(1:end-1,:))./Po(1:end-1,:);
r = mean(R);
%Assess performance based on multiple simulations
simReturn = mean(expReturn(1,i));
% Options:
% Don't change alpha
% alpha = alpha;
% Increase alpha if profit, decrease if loss
if simReturn > 0
alpha = min(1, alpha + 0.1);
elseif simReturn < 0
alpha = max(0, alpha - 0.1);
end
% Randomize alpha every month
% alpha = rand;
% Update investment
actualReturn = r * w;
investment = investment * (1 + actualReturn);
end
%figure
%plot(P)
investments = investments + investment;
%fprintf("After %d months, $10000 has been transformed into $%f\n", numMonthsSimulated, investment);
end
investments = investments / numSims;
fprintf("With %d months and %d runs, \n$10000 is on average transformed into $%f\n", numMonthsSimulated, numSims, mean(investments));