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
|
require(RBloomberg)
conn <- blpConnect()
data <- c()
for(ticker in sp500.tickers.extended){
data <- cbind(data,blpGetData(conn,paste(ticker,"US Equity"),"PX_LAST",start=as.chron("01/01/2000")))
}
spy <- blpGetData(conn,"spy US Equity","PX_LAST",start=as.chron("01/01/2000"))
spdr <- c("XLY","XLP","XLE","XLF","XLV","XLI","XLB","XLK","XLU")
data <- c()
for(ticker in spdr){
data <- cbind(data,blpGetData(conn,paste(ticker,"US Equity"),"PX_LAST",start=as.chron("01/01/2000")))
}
colnames(data) <- spdr
n.stocks <- ncol(P)
N <- nrow(P)
current.wealth <- 1
w <- rep(1/n.stocks,n.stocks)
dP <- apply(P,2,diff)
L <- rep(0,n.stocks)
V <- 0
W <- w
pnl <- 0
for(i in 1:(N-1)){
r <- dP[i,]/as.numeric(P[i,])
#r <- c(r,-r)
pnl <- cbind(pnl,current.wealth*crossprod(w,r))
current.wealth <- 1+sum(pnl)
L <- rbind(L,-r)
V <- V+crossprod(w,r^2)
T <- 2/3*sqrt(log(N)/V)
#w <- exp(-T*(apply(1+L,2,prod)-1))
w <- exp(-T*colSums(L))
w <- w/sum(w)
W <- rbind(W,w)
if(i%%10==0){
cat(current.wealth,sep="\n")
}
}
price2return <- function(x){
diff(x)/x[-length(x)]
}
#number of shares implementations
tc <- 0.005+0.02
days <- nrow(data.bus)
init.capital <- 1000000
tickers <- memb(sp500.tickers,add,as.Date(time(data.bus)[1]))
tickers.index <- which(sp500.tickers.extended%in%tickers)
n.stocks <- length(tickers)
w <- rep(0,length(sp500.tickers.extended))
w[tickers.index] <- rep(1/n.stocks,n.stocks)
N <- round((capital*w)/as.numeric(data.bus[1,tickers.index]))
dP <- apply(data.bus,2,diff)
L <- rep(0,n.stocks)
V <- 0
W <- w
pnl <- 0
tcvec <- sum(N)*tc
for(i in 1:days){
tickers <- memb(sp500.tickers,add,as.Date(time(data.bus)[1]))
tickers.index <- which(sp500.tickers.extended%in%tickers)
r <- dP[i,]/as.numeric(data.bus[i,])
pnl <- cbind(pnl,crossprod(N,dP[i,]))
capital <- init.capital+sum(pnl)-sum(tcvec)
L <- rbind(L,-r)
V <- V+crossprod(w,r^2)
T <- 2/3*sqrt(log(days)/V)
#w <- exp(-T*(apply(1+L,2,prod)-1))
w <- exp(-T*colSums(L))
w <- w/sum(w)
newN <- round((capital*w)/as.numeric(data.bus[(i+1),tickers.index]))
tcvec <- c(tcvec,sum(abs(newN-N)*tc))
N <- newN
if(i%%10==0){
cat(capital,sep="\n")
}
}
fixed.rebal <- function(P,delta){
init.capital <- 1
dP <- apply(P,2,diff)
capital <- init.capital
pnl <- c()
for(i in 1:(nrow(P)-1)){
r <- dP[i,]/P[i,]
pnl <- c(pnl,capital*crossprod(delta,r))
capital <- init.capital+sum(pnl)
}
return( pnl )
}
memb <- function(index,add,date){
#return the list of index constituents
toreverse <- add[add$Date>=date,]
current.index <- index
for(i in 1:nrow(toreverse)){
if(!is.na(toreverse$Add[i])){
current.index <- current.index[-which(current.index==toreverse$Add[i])]
}
if(!is.na(toreverse$Del[i])){
current.index <- sort(c(current.index,toreverse$Del[i]))
}
}
current.index
}
tickerslist <- c()
for(i in 101:1000){
tickerslist <- rbind(tickerslist,memb(sp500.tickers,add,as.Date(time(data)[i])))
}
|