-
Notifications
You must be signed in to change notification settings - Fork 190
/
example7_4.m
60 lines (42 loc) · 1.85 KB
/
example7_4.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
clear;
lookback=252; % use lookback days as estimation (training) period for determining factor exposures.
numFactors=5;
topN=50; % for trading strategy, long stocks with topN expected 1-day returns.
% test on SP600 smallcap stocks. (This MATLAB binary input file
% contains tday, stocks, op, hi, lo, cl arrays.
load('IJR_20080114');
mycls=fillMissingData(cl);
positionsTable=zeros(size(cl));
% note the rows of dailyret are the observations at different time
% periods
dailyret=(mycls-lag1(mycls))./lag1(mycls);
for t=lookback+1:length(tday)
% here the columns of R are the different observations.
R=dailyret(t-lookback+1:t,:)';
% avoid any stocks with missing returns
hasData=find(all(isfinite(R), 2));
R=R(hasData, :);
avgR=smartmean(R, 2);
% subtract mean from returns
R=R-repmat(avgR, [1 size(R, 2)]);
% compute covariance matrix, with observations in rows.
covR=smartcov(R');
% X is the factor exposures matrix, B the variances of factor returns
[X, B]=eig(covR);
X(:, 1:size(X, 2)-numFactors)=[]; % Retain only numFactors
% b are the factor returns for time period t-1 to t.
results=ols(R(:, end), X);
b=results.beta;
% Rexp is the expected return for next period assuming factor
% returns remain constant.
Rexp=avgR+X*b;
[foo idxSort]=sort(Rexp, 'ascend');
positionsTable(t, hasData(idxSort(1:topN)))=-1; % short topN stocks with lowest expected returns
positionsTable(t, hasData(idxSort(end-topN+1:end)))=1; % buy topN stocks with highest expected returns
end
ret=smartsum(backshift(1, positionsTable).*dailyret, 2); % compute daily returns of trading strategy
avgret=smartmean(ret)*252 % compute annualized average return of trading strategy
% A very poor return!
% avgret =
%
% -1.8099