-
Notifications
You must be signed in to change notification settings - Fork 39
/
Quantitivity.R
41 lines (35 loc) · 944 Bytes
/
Quantitivity.R
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
require("vars")
require("tawny")
btcdHedge <- function(p, start=1, interval=4)
{
# Generate hedge using BTCD method, as defined by de Prado [2011].
#
# Args:
# p: matrix of instrument price data
# start: index into p, which to begin generating hedge
# interval: number of periods used to calibrate hedge
#
# Returns: BTCD hedge ratio vector
end <- start+interval
pvar <- VAR(p[start:end,], p=1, type="none")
varfit <- fitted(pvar)
B <- cov.shrink(p) # shrink covariance
A <- t(varfit) %*% varfit
C <- chol(B)
CInv <- solve(C)
D <- t(CInv) %*% A %*% CInv
eigens <- eigen(D)
z <- eigens$vectors[,length(eigens$values)]
x <- CInv %*% z
hedge <- x/x[1]
# perform sanity check
tx <- t(x)
num <- tx %*% A %*% x
denom <- tx %*% B %*% x
check <- num / denom
if ((check - eigens$values[length(eigens$values)]) > 0.0001)
{
message("failed sanity check")
}
return (hedge)
}