diff options
| -rw-r--r-- | bloomberg-data.R | 10 | ||||
| -rw-r--r-- | getBloombergData.R | 46 | ||||
| -rw-r--r-- | sp500 add.csv | 1 |
3 files changed, 35 insertions, 22 deletions
diff --git a/bloomberg-data.R b/bloomberg-data.R index bf7afad..229254d 100644 --- a/bloomberg-data.R +++ b/bloomberg-data.R @@ -10,9 +10,15 @@ for(i in 1:length(sp500.tickers)){ start.date <- as.Date("2000-01-01") list.sp500 <- list() -for(i in 1:10){ +for(i in 1:500){ ticker <- sp500.tickers[i] - list.sp500[[ticker]] <- getBloombergData(conn,ticker,start.date) + #list.sp500.new[[ticker]] <- getBloombergData(conn,ticker,start.date) + list.sp500[[ticker]] <- bdh(conn,paste(ticker,"Equity"),c("PX_OPEN","PX_HIGH","PX_LOW","PX_LAST","VOLUME"),start.date,dates.as.row.names=F) + cat(paste("ticker", i, ": ",ticker),"\n") +} +test <- c() +for(i in 1:158){ + test <- c(test,sum(abs(as.numeric(list.sp500.new[[i]][1,1:5])-as.numeric(list.sp500[[sp500.tickers[i]]][1,2:6])))) } add <- read.table("sp500 add.csv",sep=",",fill=T,header=T,colClasses="character",quote="") 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{ diff --git a/sp500 add.csv b/sp500 add.csv index 018d1ba..49507a1 100644 --- a/sp500 add.csv +++ b/sp500 add.csv @@ -1,4 +1,5 @@ date,ticker.add,name.add,ticker.del,name.del +10/17/2011,TEL,TE CONNECTIVITY LTD,CEPH,CEPHALON INC 9/26/2011,MOS,MOSAIC CO,NSM,NATIONAL SEMICONDUCTOR 7/6/2011,ACN,ACCENTURE PLC,MI,MARSHALL & ILSLEY CORP (WI) 7/1/2011,MPC,MARATHON PETROLEUM CORP,RSH,RADIOSHACK CORP |
