diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2011-10-14 01:12:46 -0400 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2011-10-14 01:12:46 -0400 |
| commit | 13b979c5152960458736a33a428b06d6e342ea7a (patch) | |
| tree | e0c67aa7d550ce7007fb554ae6c4252bce910b0c | |
| parent | 2275d3d22d9cc40f2b90ef33d403f2a8535dc7cd (diff) | |
| download | bandit-13b979c5152960458736a33a428b06d6e342ea7a.tar.gz | |
Preliminary getBloombergData function
not very robust yet, need to test more corner cases.
| -rw-r--r-- | bloomberg-data.R | 9 | ||||
| -rw-r--r-- | getBloombergData.R | 80 |
2 files changed, 83 insertions, 6 deletions
diff --git a/bloomberg-data.R b/bloomberg-data.R index 92d864a..8118d6c 100644 --- a/bloomberg-data.R +++ b/bloomberg-data.R @@ -1,4 +1,5 @@ require(RBloomberg) +source(getBloombergData.R) conn <- blpConnect(jvm.params = "-Xmx1024m") sp500.tickers <- as.character(bds(conn,"SPX Index","INDX_MEMBERS")[,1]) #remove exchange information @@ -8,15 +9,11 @@ for(i in 1:length(sp500.tickers)){ } list.sp500 <- list() -for(i in 1:length(sp500.tickers)){ +for(i in 1:10){ ticker <- sp500.tickers[i] - list.sp500[[ticker]] <- bdh(conn,paste(ticker,"Equity"),c("PX_OPEN","PX_HIGH","PX_LOW","PX_LAST"),as.Date("2000-01-01"),dates.as.row.names=F) + list.sp500[[ticker]] <- getBloombergData(conn,ticker,start.date) } -#split information -EQY_DVD_HIST_SPLITS -#dividend information -DVD_HIST add <- read.table("sp500 add.csv",sep=",",fill=T,header=T,colClasses="character",quote="") add$date <- as.Date(add$date,format="%m/%d/%Y") diff --git a/getBloombergData.R b/getBloombergData.R new file mode 100644 index 0000000..302aac9 --- /dev/null +++ b/getBloombergData.R @@ -0,0 +1,80 @@ +getBloombergData <- function(conn,ticker,start,date){ + 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 + override_field="DVD_START_DT" #DVD_END_DT + override_values=format(start,"%Y%M%d") + spl <- bds(conn,paste(ticker,"Equity"),c("EQY_DVD_ADJUST_FACT"),override_field,override_values) + # 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 + 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)){ + divspl <- merge(NA,spl,all=T) + }else if(is.null(spl)){ + divspl <- merge(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 ) +} |
