diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2011-10-14 20:01:02 -0400 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2011-10-14 20:01:02 -0400 |
| commit | e8a5cfea058c145e751fcee27daace0c991d1606 (patch) | |
| tree | 8ecd611073ee1e355f24833b014b95e11482120e | |
| parent | 2c2b03d3ade43107a0776468bcf97468a7b81d58 (diff) | |
| download | bandit-e8a5cfea058c145e751fcee27daace0c991d1606.tar.gz | |
More bug fixes
should be almost there!
| -rw-r--r-- | bloomberg-data.R | 4 | ||||
| -rw-r--r-- | getBloombergData.R | 40 |
2 files changed, 24 insertions, 20 deletions
diff --git a/bloomberg-data.R b/bloomberg-data.R index 8118d6c..bf7afad 100644 --- a/bloomberg-data.R +++ b/bloomberg-data.R @@ -1,5 +1,5 @@ require(RBloomberg) -source(getBloombergData.R) +source("getBloombergData.R") conn <- blpConnect(jvm.params = "-Xmx1024m") sp500.tickers <- as.character(bds(conn,"SPX Index","INDX_MEMBERS")[,1]) #remove exchange information @@ -8,13 +8,13 @@ for(i in 1:length(sp500.tickers)){ sp500.tickers[i] <- strsplit(ticker," ")[[1]][1] } +start.date <- as.Date("2000-01-01") list.sp500 <- list() for(i in 1:10){ ticker <- sp500.tickers[i] list.sp500[[ticker]] <- getBloombergData(conn,ticker,start.date) } - 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 index 302aac9..ccb0b41 100644 --- a/getBloombergData.R +++ b/getBloombergData.R @@ -1,23 +1,22 @@ -getBloombergData <- function(conn,ticker,start,date){ +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 - 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 + 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 + # 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") @@ -33,16 +32,21 @@ getBloombergData <- function(conn,ticker,start,date){ } } #div information - div <- bds(conn,paste(ticker,"Equity"),c("DVD_HIST"),override_fields,override <- values) + #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)){ - divspl <- merge(NA,spl,all=T) + #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(div,NA,all=T) + divspl <- merge.xts(div,NA,all=T) }else{ divspl <- merge(div,spl,all=T) } |
