-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairtrading.m
83 lines (61 loc) · 1.69 KB
/
pairtrading.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
79
80
81
82
83
% load your Stock prices as Pricemat in matrix form and Dates as dates in
% vector form.
NumStocks = size(PriceMat,2);
CointMatrix = zeros(NumStocks);
% Pair selection part.
% create a Cointegration matrix
% Go through the list of stocks and test for cointegration and eliminate
% pairs that fail the test.
for idx = 1:NumStocks;
for jdx=idx+1:NumStocks
CointMatrix(idx,jdx)=ForCoint(PriceMat(:,idx),PriceMat(:,jdx));
end
end
[rows,cols]=find(CointMatrix);
CointPairs = [rows cols];
cf=(CointPairs(:,1)-CointPairs(:,2))==0;
% Testing for Tradability, residual series would be stationary ; pairs
% trading is a bet that the residual series will revert to its mean. WE
% take the pairs R-squared from the regression is above 0,90 exceeding the
% two standar deviation.
CointPairs(cf,:)=[];
Bhat=[];
r=[];
stats=[];
limit=0.90; % limit for Regression R square
k=[];
for P = 1:size(CointPairs,1)
% Look at the regression founded
Y = log(PriceMat(:,CointPairs(P,1)));
X = log(PriceMat(:,CointPairs(P,2)));
% get coeficients of the regression statistics
[B,BINT,R,RINT,STATS] = regress(Y,[ones(length(X),1) X(1:end)]);
r=[R r];
stats=[STATS(1,1)' stats];
Bhat=[B(2)' Bhat];
% find the values that exceed the limit we set.
Z=(stats>limit);
if any(Z)
K=find(Z);
else
K=0;
end
k=[K k];
end
figure % set up dates ticks
h=plot(Dates,Dates);
a=get(gca,'XTick');
XTick = [];
years = year(Dates(1)):year(Dates(end));
for n = years
XTick = [XTick datenum(n,1,1)];
end
a=min(Dates);
b=max(Dates);
X_Lim=[a-.01*(b-a) b+.01*(b-a)];
close
Pairres=r(:,K);
Sigma=std(Pairres);
Uppersigma=Sigma.*2;
Lowersigma=Sigma.*(-2);
[S1 S2]=size(Pairres);