1
votes

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?

1
How did you create this xts object? It has rownames and xts objects should never have rownames. The best solution to your problem will be, "don't construct the xts object like this in the first place."Joshua Ulrich
Please show us what code you have already written. This is Stack Overflow - the idea is not that we do the work for you, but that we help you when you get stuck. Also rather than using an .rdata file please use dput to paste a SMALL subset of data into your question.SlowLearner
If you don't know how to use dput then type ?dput at the RGui prompt for help. Also read this question for full details of that and many other ways to make a reproducible example.SlowLearner
What is that make.xts function?GSee
ok, understood dput. Is mandatory to use dput? I'm more confident with .rdata. I can zip it next time, ok? sorry, as you can see, i'm newbie in R.Fryc

1 Answers

1
votes

Just keep it very simple dont write a make.xts function.

df<- xts(df[,2:7],order.by=as.Date(df[,1]))