diff options
Diffstat (limited to 'getBloombergData.R')
| -rw-r--r-- | getBloombergData.R | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/getBloombergData.R b/getBloombergData.R index ccb0b41..2421759 100644 --- a/getBloombergData.R +++ b/getBloombergData.R @@ -18,15 +18,20 @@ getBloombergData <- function(conn,ticker,start.date){ # can't handle 3 or 4 yet if(NROW(spl)!=0){ - if (NROW(spl[spl[,"Adjustment Factor Operator Type"]%in% c(3,4),])>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[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) + spl <- xts(data.frame(spl$"Adjustment Factor", + ifelse(spl$"Adjustment Factor Flag" %in% c(2,3), + spl$"Adjustment Factor",NA)), + order.by = as.Date(spl$"Adjustment Date")) + #aggregate non-unique dates + spl <- as.xts(aggregate(spl, identity, prod, na.rm=T)) + if(any(time(spl)>= start.date & time(spl)<=Sys.Date())){ + spl <- window(spl, start=start.date, end = Sys.Date()) }else{ spl <- NULL } @@ -36,30 +41,31 @@ getBloombergData <- function(conn,ticker,start.date){ #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) + 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")) + div <- xts(div$"Dividend Amount", as.Date(div$"Ex-Date")) } - if(is.null(div)&&is.null(spl)){ + 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) + divspl <- merge.xts(NA, spl ,all=T) }else if(is.null(spl)){ - divspl <- merge.xts(div,NA,all=T) + divspl <- merge.xts(div ,NA, NA, all=T) }else{ - divspl <- merge(div,spl,all=T) + 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)) + colnames(divspl) <- c("Adj.Div","Split","Split2") + ohlc <- merge(ohlc, divspl, all = TRUE) + if (all(is.na(ohlc[, "Split2"]))) { + s.ratio2 <- rep(1, NROW(ohlc)) }else { - s.ratio <- adjRatios(split = divspl[, "Split"])[, 1] + s.ratio2 <- adjRatios(split = ohlc[, "Split2"])[, 1] } - divspl <- cbind(divspl, divspl[, "Adj.Div"] * (1/s.ratio)) - colnames(divspl)[3] <- "Div" - ohlc <- merge(ohlc, divspl, all = TRUE) + ohlc <- cbind(ohlc, ohlc[, "Adj.Div"] * (1/s.ratio2)) + colnames(ohlc)[NCOL(ohlc)] <- "Div" adj <- adjRatios(ohlc[, "Split"], ohlc[, "Div"], ohlc[, "Close"]) s.ratio <- adj[, 1] d.ratio <- adj[, 2] @@ -70,7 +76,7 @@ getBloombergData <- function(conn,ticker,start.date){ 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[, "Volume"] <- ohlc[, "Volume"] * (1/s.ratio2) ohlc <- ohlc[, c("Open", "High", "Low", "Close", "Volume", "Unadj.Close", "Div", "Split", "Adj.Div")] }else{ |
