I have a very basic question… Suppose I can get a closing price for specific symbol for current date in an xts object using
closePrice<-as.double(Cl(get(symbol))[currentDate])
How can I get close price on the trading day one day, two days, three days, … or n days before/after?
closePriceNDaysBefore<-as.double(Cl(get(symbol))[currentDate - n])
is not ok…
Thanks in advance.
Kind regards,
Samo.
You can do this rather easily, depending on what you mean by ‘current date’.
If current date is the last entry (just pulled up to date data), then last and first will help:
If instead you need the current date to mean something like the date you care about in this particular loop/context, the which.i=TRUE argument to the subset will help – as it employes the very same fast ISO lookup, but returns the position(s) that match. That is, it doesn’t do the subset.