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
|
getBloombergData <- function(conn,ticker,start.date){
require(TTR)
ohlc <- bdh(conn,paste(ticker,"Equity"),c("PX_OPEN","PX_HIGH","PX_LOW","PX_LAST","VOLUME"),start.date,dates.as.row.names=F)
colnames(ohlc) <- c("Date","Open","High","Low","Close","Volume")
ohlc <- xts(ohlc[,-1],as.Date(ohlc$Date))
#split information
spl <- bds(conn,paste(ticker,"Equity"),c("EQY_DVD_ADJUST_FACT"))
# bds returns NULL if there is no data.
#Adjustment Factor Operator Type
# 1 = div
# 2 = mul
# 3 = add
# 4 = sub
#Adjustment Factor Flag
# 1 = prices only
# 2 = volumes only
# 3 = prices and volume
# can't handle 3 or 4 yet
if(NROW(spl)!=0){
if (NROW(spl[spl[,"Adjustment Factor Operator Type"]%in% c(3,4),])>0){
stop("case not handled")
}else{
spl[spl[,"Adjustment Factor Operator Type"]==1,"Adjustment Factor"] <-
1/spl[spl[,"Adjustment Factor Operator Type"]==1,"Adjustment Factor"]
}
spl <- xts(spl$"Adjustment Factor",as.Date(spl$"Adjustment Date"))
if(time(last(spl))>=start.date){
spl <- window(spl,start=start.date)
}else{
spl <- NULL
}
}
#div information
#we need to override the end date as well cause the Ex-Date might be in the
#future
override_fields <- c("DVD_START_DT", "DVD_END_DT")
override_values <- c(format(start.date,"%Y%m%d"),format(Sys.Date(),"%Y%m%d"))
div <- bds(conn,paste(ticker,"Equity"),c("DVD_HIST"),override_fields,override_values)
if(NROW(div)!=0){
div <- xts(div$"Dividend Amount",as.Date(div$"Ex-Date"))
}
if(is.null(div)&&is.null(spl)){
divspl <- NULL
}else if(is.null(div)){
#need to use merge.xts, otherwise spl is cast to a numeric
divspl <- merge.xts(NA,spl,all=T)
}else if(is.null(spl)){
divspl <- merge.xts(div,NA,all=T)
}else{
divspl <- merge(div,spl,all=T)
}
if(!is.null(divspl)){
colnames(divspl) <- c("Adj.Div","Split")
if (all(is.na(divspl[, "Split"]))) {
s.ratio <- rep(1, NROW(divspl))
}else {
s.ratio <- adjRatios(split = divspl[, "Split"])[, 1]
}
divspl <- cbind(divspl, divspl[, "Adj.Div"] * (1/s.ratio))
colnames(divspl)[3] <- "Div"
ohlc <- merge(ohlc, divspl, all = TRUE)
adj <- adjRatios(ohlc[, "Split"], ohlc[, "Div"], ohlc[, "Close"])
s.ratio <- adj[, 1]
d.ratio <- adj[, 2]
cn <- colnames(ohlc)
ohlc <- cbind(ohlc, ohlc[, "Close"])
colnames(ohlc) <- c(cn, "Unadj.Close")
ohlc[, "Open"] <- ohlc[, "Open"] * d.ratio * s.ratio
ohlc[, "High"] <- ohlc[, "High"] * d.ratio * s.ratio
ohlc[, "Low"] <- ohlc[, "Low"] * d.ratio *s.ratio
ohlc[, "Close"] <- ohlc[, "Close"] * d.ratio * s.ratio
ohlc[, "Volume"] <- ohlc[, "Volume"] * (1/d.ratio)
ohlc <- ohlc[, c("Open", "High", "Low", "Close", "Volume",
"Unadj.Close", "Div", "Split", "Adj.Div")]
}else{
cn <- colnames(ohlc)
ohlc <- cbind(ohlc, ohlc[, "Close"])
colnames(ohlc) <- c(cn, "Unadj.Close")
ohlc <- merge(ohlc,NA,NA,NA,all = TRUE)
colnames(ohlc) <- c(colnames(ohlc)[1:6],"Adj.Div","Split","Div")
}
return( ohlc )
}
|