-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bayesian_IO.m
208 lines (171 loc) · 5.16 KB
/
Bayesian_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
%% 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('../Data/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(190000:200000,:);
Y=Data(:,1);
X1=Data(:,2);
X2=Data(:,3);
Index=txt(2:end,1);
T=length(Y);
y=log(Y);
x1=log(X1);
x2=log(X2);
%% Generate sample
b0=cumsum(randn(100,1));
b1=cumsum(randn(100,1));
x1=cumsum(randn(100,1));
y=b0+b1.*x1;
%% BSTS
T=length(x1);
data = struct('T',T,'x',x1,'y',y);
fit = stan('file','BSTS.stan','data',data,'verbose',true);
%%
print(fit);
samples = fit.extract('permuted',true);
fprintf('\n\tmean sd\n')
fprintf('u\t%1.3f\t%1.3f\n',mean(samples.u),std(samples.u));
fprintf('v\t%1.3f\t%1.3f\n',mean(samples.v),std(samples.v));
% fprintf('sigma\t%1.3f\t%1.3f\n',mean(samples.sigma_y),std(samples.sigma_y));
plot(y);
hold on;
plot(mean(samples.u)+mean(samples.v).*x1');
hold off;
corrplot([samples.u(:,T) samples.v(:,T) samples.s_obs samples.s_level samples.s_slope],...
'varNames', {'u','v','std_obs','std_level','std_slope'});
%% Kalman Filter
[beta,ZScore]=Kalman_Filter3(Y,X1,X2);
% Kalman coefficients
figure(1);
subplot(3,1,1);
plot(beta(1,:));
subplot(3,1,2);
plot(beta(2,:));
subplot(3,1,3);
plot(beta(3,:));
figure(3);
plot(ZScore);
axis([0 T -std(ZScore) std(ZScore) ]);
%% Optimize Thresholds
thPer=5000;
thRelax=1;
thWin=10000;
[thY,thX,thYcl,thXcl]=Parameter_Optimizer3(Y,X1,X2,ZScore,beta(1,:),beta(2,:),thPer,thRelax,thWin);
%% Bollinger Band Trading
K=0.002; %Transaction Cost
T=length(Y);
% Position (BTC,ETH)
% thY=thYOpt*ones(T,1); %threshold to buy BTC
% thX=thXOpt*ones(T,1); %threshold to buy ETH
% thYcl=thYclOpt*ones(T,1); %threshold to close BTC position
% thXcl=thXclOpt*ones(T,1); %%threshold to close ETH position
% thY=-3*ones(T,1); %threshold to buy BTC
% thX=3*ones(T,1); %threshold to buy ETH
% thYcl=-9*ones(T,1); %threshold to close BTC position
% thXcl=9*ones(T,1); %%threshold to close ETH position
val=100;
pos=zeros(T,3);
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)./X1(t), -val*beta(2,t)./X2(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)./X1(t) , val*beta(2,t)./X2(t)];
shortY=[shortY , t];
elseif (ZScore(t)<thYcl(t))&&(pos(t-1,1)>0)
pos(t,:)=[0 , 0 , 0];
close=[close , t];
elseif (ZScore(t)>thXcl(t))&&(pos(t-1,1)<0)
pos(t,:)=[0 , 0 , 0];
close=[close , t];
else
pos(t,:)=pos(t-1,:);
end
% PnL(t)=pos(t-1,1).*(Y(t)-Y(t-1)) + pos(t-1,2).*(X(t)-X(t-1))...
% -K/2*abs(pos(t,1)-pos(t-1,1)).*Y(t-1)-K/2*abs(pos(t,2)-pos(t-1,2)).*X(t-1);
% curPosPnL=curPosPnL+PnL(t);
end
PnL(2:end)=pos(1:end-1,1).*(Y(2:end)-Y(1:end-1)) + pos(1:end-1,2).*(X1(2:end)-X1(1:end-1))...
+ pos(1:end-1,3).*(X2(2:end)-X2(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)).*X1(1:end-1) -K/2*abs(pos(2:end,3)-pos(1:end-1,3)).*X2(1:end-1);
netVal=cumsum(PnL);
BnH=Y-Y(1);
lev=1; %Leverage
margin=[0;1/lev*(abs(pos(1:end-1,1)).*Y(2:end)+abs(pos(1:end-1,2)).*X1(2:end)...
+abs(pos(1:end-1,3)).*X2(2:end))-min(netVal(2:end),0)];
totMargin=max(margin);
APR=netVal(end)/totMargin;
[netVal(end) totMargin APR]
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(3,1,1);
plot(beta(1,:));
subplot(3,1,2);
plot(beta(2,:));
subplot(3,1,3);
plot(beta(3,:));
%
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(3,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(3,1,2);
plot(X1);
hold on;
plot(longY,X1(longY),'.','markers',12);
hold on;
plot(shortY,X1(shortY),'.','markers',12);
hold on;
plot(close,X1(close),'.','markers',12);
hold off;
subplot(3,1,3);
plot(X2);
hold on;
plot(longY,X2(longY),'.','markers',12);
hold on;
plot(shortY,X2(shortY),'.','markers',12);
hold on;
plot(close,X2(close),'.','markers',12);
hold off;
[netVal(end) totMargin APR]