forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatio.m
32 lines (24 loc) · 1.09 KB
/
Ratio.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
clear;
% 1 minute data on EWA-EWC
load('inputData_ETF', 'tday', 'syms', 'cl');
idxG=find(strcmp('GLD', syms));
idxU=find(strcmp('USO', syms));
x=cl(:, idxG);
y=cl(:, idxU);
lookback=20; % Lookback is set arbitrarily
ratio=y./x;
ratio(1:lookback)=[]; % Removed to have same test set as price spread and log price spread strategies
x(1:lookback)=[];
y(1:lookback)=[];
plot(ratio);
%
% Apply a simple linear mean reversion strategy to GLD-USO
numUnits=-(ratio-movingAvg(ratio, lookback))./movingStd(ratio, lookback); % units invested in the portfolio. movingAvg and movingStd are functions from epchan.com/book2
positions=repmat(numUnits, [1 2]).*[-ones(size(x, 1), 1) ones(size(x, 1), 1)]; % positions in dollar invested
pnl=sum(lag(positions, 1).*([x y]-lag([x y], 1))./lag([x y], 1), 2); % daily P&L of the strategy
ret=pnl./sum(abs(lag(positions, 1)), 2);
ret(isnan(ret))=0;
figure;
plot(cumprod(1+ret)-1); % Cumulative compounded return
fprintf(1, 'APR=%f Sharpe=%f\n', prod(1+ret).^(252/length(ret))-1, sqrt(252)*mean(ret)/std(ret));
% APR=-0.141522 Sharpe=-0.746663