-
Notifications
You must be signed in to change notification settings - Fork 1
/
Kalman_IO.m
144 lines (120 loc) · 3.26 KB
/
Kalman_IO.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
%% Importing Data
% [Data,txt,~]=xlsread('BTC-ETH-Data.xlsx','A1:C240861');
% [Data,txt,~]=xlsread('BTC-ETH-Data.xlsx','A100001:C200000');
% [Data,txt,~]=xlsread('BTC-ETH-Data.xlsx','A200001:C240861');
% [Data,txt,~]=xlsread('BTC-ETH-Data.xlsx','A1:C100000');
% [Data,txt,~]=xlsread('BTC-ETH-Data.xlsx','A1:C40000');
% [Data,txt,~]=xlsread('tullow-data.xlsx','A40000:C57000');
% [Data,txt,~]=xlsread('tullow-data.xlsx','A1:C40000');
% [Data,txt,~]=xlsread('tullow-data.xlsx');
% [Data,txt,~]=xlsread('BTC-ETH-17-1-1-18-2-3.csv');
[Data,txt,~]=xlsread('BTC-ETH-BCH-17-8-1-18-1-24.csv');
% [Data,txt,~]=xlsread('BTC-ETH-BCH-17-8-1-18-1-24.csv','A1:C200000');
% [Data,txt,~]=xlsread('BTC-ETH-BCH-17-8-1-18-1-24.csv','A200000:C253327');
% Data=Data(150000:200000,:);
Y=Data(:,1);
X=Data(:,2);
Index=txt(2:end,1);
T=length(Y);
%% Kalman Filter
[beta,ZScore]=Kalman_Filter(Y,X);
% Kalman coefficients
figure(1);
subplot(2,1,1);
plot(beta(1,:));
subplot(2,1,2);
plot(beta(2,:));
figure(3);
plot(ZScore);
axis([0 T -std(ZScore) std(ZScore) ]);
%% Optimize Thresholds
thPer=5000;
thRelax=1;
thWin=10000;
[thY,thX,thYcl,thXcl]=Parameter_Optimizer(Y,X,ZScore,beta(1,:),thPer,thRelax,thWin);
%% Bollinger Band Trading
K=0.002; %Transaction Cost
T=length(Y);
val=100;
pos=zeros(T,2);
longY=[];
shortY=[];
close=[];
PnL=zeros(T,1);
for t=2:T
if (ZScore(t)<thY(t))&&(ZScore(t-1)>=thY(t))&&(pos(t-1,1)<=0)
pos(t,:)=[val/Y(t) , -val*beta(1,t)./X(t)];
longY=[longY , t];
elseif (ZScore(t)>thX(t))&&(ZScore(t-1)<=thX(t))&&(pos(t-1,1)>=0)
pos(t,:)=[-val/Y(t) , val*beta(1,t)./X(t)];
shortY=[shortY , t];
elseif (ZScore(t)<thYcl(t))&&(pos(t-1,1)>0)
pos(t,:)=[0 , 0];
close=[close , t];
elseif (ZScore(t)>thXcl(t))&&(pos(t-1,1)<0)
pos(t,:)=[0 , 0];
close=[close , t];
else
pos(t,:)=pos(t-1,:);
end
end
% Profit and Loss
PnL(2:end)=pos(1:end-1,1).*(Y(2:end)-Y(1:end-1)) + pos(1:end-1,2).*(X(2:end)-X(1:end-1))...
-K/2*abs(pos(2:end,1)-pos(1:end-1,1)).*Y(1:end-1)-K/2*abs(pos(2:end,2)-pos(1:end-1,2)).*X(1:end-1);
netVal=cumsum(PnL);
BnH=Y-Y(1);
lev=1; %Leverage
margin=1/lev*(abs(pos(:,1)).*Y+abs(pos(:,2)).*X)-min(netVal,0);
totMargin=max(margin);
APR=netVal(end)/totMargin;
[netVal(end) totMargin APR]
%Number of Transactions
sum(~((pos(2:end,1)==pos(1:end-1,1))&(pos(2:end,2)==pos(1:end-1,2)))) %Number of Transactions
%% Figures
% Kalman coefficients
figure(1);
subplot(2,1,1);
plot(beta(1,:));
subplot(2,1,2);
plot(beta(2,:));
%
figure(2);
subplot(2,1,1);
plot(netVal);
subplot(2,1,2);
plot(margin);
%
figure(3);
%subplot(3,1,3);
plot(ZScore);
axis([0 T -std(ZScore) std(ZScore) ]);
hold on;
plot(thY);
hold on;
plot(thX);
hold on;
plot(thYcl);
hold on;
plot(thXcl);
hold off;
%
figure(4);
subplot(2,1,1);
plot(Y);
hold on;
plot(longY,Y(longY),'.','markers',12);
hold on;
plot(shortY,Y(shortY),'.','markers',12);
hold on;
plot(close,Y(close),'.','markers',12);
hold off;
subplot(2,1,2);
plot(X);
hold on;
plot(longY,X(longY),'.','markers',12);
hold on;
plot(shortY,X(shortY),'.','markers',12);
hold on;
plot(close,X(close),'.','markers',12);
hold off;
[netVal(end) totMargin APR]