1

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?

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Fryc
  • 93
  • 1
  • 1
  • 7
  • 2
    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 Nov 21 '13 at 12:40
  • 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 Nov 21 '13 at 17:45
  • If you don't know how to use `dput` then type `?dput` at the RGui prompt for help. Also read [this question](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for full details of that and many other ways to make a reproducible example. – SlowLearner Nov 21 '13 at 18:53
  • 1
    What is that `make.xts` function? – GSee Nov 21 '13 at 18:53
  • 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 Nov 21 '13 at 19:59
  • Data in RData format is binary and cannot be posted here. Output from `dput()` is text, and can be posted. Hence our preference for the latter. – Dirk Eddelbuettel Nov 21 '13 at 20:00
  • ok i understand, but my binary are temporary on my dropbox not on stackoverflow. Never i would do that without permission. But if in this community is mandatory txt format next time i will use that – Fryc Nov 21 '13 at 21:11
  • I solved my problem. Simply used as.numeric function for every column. thank you for your patience. – Fryc Nov 24 '13 at 14:29

1 Answers1

1

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

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

Arun Raja
  • 1,554
  • 16
  • 26