forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimateFuturesReturns.m
55 lines (42 loc) · 1.72 KB
/
estimateFuturesReturns.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
clear;
% load('inputDataDaily_VX_20120507', 'tday', 'contracts', 'cl');
load('//dellquad/Futures_data/inputDataDaily_CL_20120813', 'tday', 'contracts', 'cl');
% load('//dellquad/Futures_data/inputDataDaily_TU_20120813', 'tday', 'contracts', 'cl');
% load('//dellquad/Futures_data/inputDataDaily_BR_20120813', 'tday', 'contracts', 'cl');
% load('//dellquad/Futures_data/inputDataDaily_HG_20120813', 'tday', 'contracts', 'cl');
% load('//dellquad/Futures_data/inputDataDaily_C2_20120813', 'tday', 'contracts', 'cl');
% load('inputDataDaily_C2_20120813', 'tday', 'contracts', 'cl');
% load('//dellquad/Futures_data/inputDataDaily_HO2_20120813', 'tday', 'contracts', 'cl');
% Find spot prices
spotIdx=find(strcmp(contracts, '0000$'));
spot=cl(:, spotIdx);
cl(:, spotIdx)=[];
contracts(spotIdx)=[];
T=[1:length(spot)]';
isBadData=~isfinite(spot);
spot(isBadData)=[];
T(isBadData)=[];
res=ols(log(spot), [T ones(size(T, 1), 1)]);
fprintf(1, 'Average annualized spot return=%f\n', 252*smartmean(res.beta(1)));
% Fitting gamma to forward curve
gamma=NaN(size(tday));
for t=1:length(tday)
FT=cl(t, :)';
idx=find(isfinite(FT));
idxDiff=fwdshift(1, idx)-idx; % ensure consecutive months futures
if (length(idx) >= 5 && all(idxDiff(1:4)==1))
FT=FT(idx(1:5)); % only uses the nearest 5 contracts
T=[1:length(FT)]';
% scatter(T, log(FT));
res=ols(log(FT), [T ones(size(T, 1), 1)]);
gamma(t)=-12*res.beta(1);
end
end
isBadData=find(isnan(gamma));
gamma(isBadData)=[];
tday(isBadData)=[];
plot(gamma);
%print -r300 -djpeg fig5_4
% hold on;
fprintf(1, 'Average annualized roll return=%f\n', smartmean(gamma));
% save('C2_gamma', 'tday', 'gamma');