forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCL_rev.m
78 lines (59 loc) · 2.36 KB
/
CL_rev.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
clear;
lookback1=30;
lookback2=40;
load('inputDataOHLCDaily_20120504', 'syms', 'tday', 'cl');
cl=cl(:, strcmp('CL', syms));
% longs= cl < lag(cl, lookback1) & cl > lag(movingAvg(cl, lookback2), 1);
% shorts=cl > lag(cl, lookback1) & cl < lag(movingAvg(cl, lookback2), 1);
% longs= cl > lag(cl, lookback2);
% shorts= cl < lag(cl, lookback2);
longs= cl < backshift(lookback1, cl) & cl > backshift(lookback2, cl);
shorts=cl > backshift(lookback1, cl) & cl < backshift(lookback2, cl);
% longs= cl < lag(cl, lookback1) ;
% shorts=cl > lag(cl, lookback1) ;
positions=zeros(size(cl));
positions(longs)=1;
positions(shorts)=-1;
% ret=lag(positions, 1).*(cl-lag(cl, 1))./lag(cl, 1);
ret=backshift(1, positions).*(cl-backshift(1, cl))./backshift(1, cl);
ret(isnan(ret))=0;
plot(cumprod(1+ret)-1, 'r'); % Cumulative compounded return
dateaxis('X', 12, datenum(num2str(tday(1)), 'yyyymmdd'));
hold on;
fprintf(1, 'APR=%f Sharpe=%f\n', prod(1+ret).^(252/length(ret))-1, sqrt(252)*mean(ret)/std(ret));
% APR=0.117600 Sharpe=1.100368
% Momentum only
longs= cl > backshift(lookback2, cl);
shorts=cl < backshift(lookback2, cl);
positions=zeros(size(cl));
positions(longs)=1;
positions(shorts)=-1;
ret=backshift(1, positions).*(cl-backshift(1, cl))./backshift(1, cl);
ret(isnan(ret))=0;
plot(cumprod(1+ret)-1, 'g'); % Cumulative compounded return
% Reversal only
longs= cl < backshift(lookback1, cl);
shorts=cl > backshift(lookback1, cl);
positions=zeros(size(cl));
positions(longs)=1;
positions(shorts)=-1;
ret=backshift(1, positions).*(cl-backshift(1, cl))./backshift(1, cl);
ret(isnan(ret))=0;
plot(cumprod(1+ret)-1, 'k'); % Cumulative compounded return
legend('Combo', 'Momentum', 'Reversal');
longs= cl < backshift(lookback1, cl) | cl > backshift(lookback2, cl);
shorts=cl > backshift(lookback1, cl) | cl < backshift(lookback2, cl);
% longs= cl < lag(cl, lookback1) ;
% shorts=cl > lag(cl, lookback1) ;
positions=zeros(size(cl));
positionsL=zeros(size(cl));
positionsS=zeros(size(cl));
positionsL(longs)=1;
positionsS(shorts)=-1;
positions=positionsL+positionsS;
% ret=lag(positions, 1).*(cl-lag(cl, 1))./lag(cl, 1);
ret=backshift(1, positions).*(cl-backshift(1, cl))./backshift(1, cl);
ret(isnan(ret))=0;
plot(cumprod(1+ret)-1, 'c'); % Cumulative compounded return
legend('ComboAND', 'Momentum', 'Reversal', 'ComboOR');
hold off;