forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kentdaniel.m
61 lines (48 loc) · 1.92 KB
/
kentdaniel.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
clear;
load('inputDataOHLCDaily_stocks_20120424');
lookback=252;
holddays=25;
topN=50;
% idxStart=find(tday==20100104);
% idxEnd=find(tday==20120424);
idxStart=find(tday==20070515);
idxEnd=find(tday==20071231);
% idxStart=find(tday==20080102);
% idxEnd=find(tday==20091231);
% tday=tday(idxStart:idxEnd);
% cl=cl(idxStart:idxEnd, :);
% op=op(idxStart:idxEnd, :);
% cl is a TxN array of closing prices, where T is the number of trading
% days, and N is the number of stocks in the S&P 500
ret=(cl- backshift(lookback,cl))./backshift(lookback,cl); % daily returnslongs=false(size(ret));
shorts=false(size(ret));
positions=zeros(size(ret));
for t=lookback+1:length(tday)
[foo idx]=sort(ret(t, :), 'ascend');
nodata=find(isnan(ret(t, :)));
idx=setdiff(idx, nodata, 'stable');
longs(t, idx(end-topN+1:end))=true;
shorts(t, idx(1:topN))=true;
end
for h=0:holddays-1
long_lag=backshift(h, longs);
long_lag(isnan(long_lag))=false;
long_lag=logical(long_lag);
short_lag=backshift(h, shorts);
short_lag(isnan(short_lag))=false;
short_lag=logical(short_lag);
positions(long_lag)=positions(long_lag)+1;
positions(short_lag)=positions(short_lag)-1;
end
dailyret=smartsum(backshift(1, positions).*(cl-lag(cl))./lag(cl), 2)/(2*topN)/holddays;
dailyret(isnan(dailyret))=0;
cumret=cumprod(1+dailyret(idxStart:idxEnd))-1;
plot(cumret);
tday=tday([idxStart:idxEnd]);
fprintf(1, 'Avg Ann Ret=%7.4f Sharpe ratio=%4.2f \n',252*smartmean(dailyret(idxStart:idxEnd)), sqrt(252)*smartmean(dailyret(idxStart:idxEnd))/smartstd(dailyret(idxStart:idxEnd)));
fprintf(1, 'APR=%10.4f\n', prod(1+dailyret(idxStart:idxEnd)).^(252/length(dailyret(idxStart:idxEnd)))-1);
[maxDD maxDDD]=calculateMaxDD(cumret);
fprintf(1, 'Max DD =%f Max DDD in days=%i\n\n', maxDD, round(maxDDD));
% Avg Ann Ret= 0.0315 Sharpe ratio=0.40
% APR= 0.0288
% Max DD =-0.066923 Max DDD in days=182