I simply download time series from Bloomberg and try to make an xts object:
library(Rbbg)
conn <- blpConnect()
tickers = c("SPX Index","GX1 Index","VG1 Index","NK1 Index",
"SM1 Index","RM1 Index","LT01TRUU Index")
df <- bdh(conn,tickers,c("PX_OPEN","PX_HIGH", "PX_LOW", "PX_LAST", "Volume"),
"19980101", "20131020",option_names = "CDR", option_values = "US")
blpDisconnect(conn)
make.xts <- function(x, order.by) {
tzone = Sys.getenv('TZ')
orderBy = class(order.by)
index = as.numeric(as.POSIXct(order.by, tz = tzone))
if( is.null(dim(x)) ) {
if( len(orderBy) == 1 )
x = t(as.matrix(x))
else
dim(x) = c(len(x), 1)
}
x = as.matrix(x)
x = structure(.Data = x,
index = structure(index, tzone = tzone, tclass = orderBy),
class = c('xts', 'zoo'), .indexCLASS = orderBy, tclass=orderBy,
.indexTZ = tzone, tzone=tzone)
return( x )
}
data <- new.env()
for(s in unique(df$ticker)) {
temp = df[df$ticker == s,]
date = as.Date(temp$date)
temp = temp[,spl('PX_OPEN,PX_HIGH,PX_LOW,PX_LAST,Volume')]
colnames(temp) = spl('Open,High,Low,Close,Volume')
temp = make.xts(temp[], date)
if (length(grep('Index',s)) == 1) {
data[[ trim(gsub('Index', '', s)) ]] = temp[!is.na(temp$Close),]
} else {
data[[ trim(gsub('US Equity', '', s)) ]] = temp[!is.na(temp$Close),]
}
}
This is df object (sorry, but i don't know dput) https://dl.dropboxusercontent.com/u/102669/df.rdata
and this is xts object: https://dl.dropboxusercontent.com/u/102669/temp.rdata
All data are CHR but i want numeric data.
I suppose that the problem are the NA. I can't remove all NA row because some time series have only close price.
So a rule could be: when only close price exists, put all other columns equal to close price, otherwise nothing. If some Na values exists inside any columns carry on the last not NA value
The final results should be an xts object with numeric data.
Any help please?