3
votes

Quantmod's getSymbols() fetches historical prices up till yesterday's close. I do my analysis in the morning and would like to update the series with the current quote (I only use adjusted prices). I can't seem to get it to work. When I use this code :

getSymbols('AGNC')
q <- getQuote('AGNC')
d <- Sys.Date()
qq<- q$Last
x <- zoo(qq,d)
t <- rbind.zoo(Ad(AGNC), x)
print(tail(t))

It prints out OK, but when I try to do something further, like:

dum <- dailyReturn(t)

I get the following error:

Error in colnames<-(*tmp*, value = "daily.returns") : attempt to set colnames on object with less than two dimensions

Any ideas?

2
Are you open to using xts instead of zoo? - GSee
Yes, xts would be fine. - user1478354

2 Answers

3
votes
library(quantmod)
getSymbols('AGNC')
q <- getQuote('AGNC')
d <- Sys.Date()
qq<- q$Last
x <- xts(qq,d)
t <- rbind(Ad(AGNC), x)
print(tail(t))
dailyReturn(t)
0
votes

Here's another way to update a symbol:

require(quantmod)
getSymbols('AGNC')

q <- getQuote('AGNC')
row.names(q)<- trunc(q[,"Trade Time"], units="days")
q <- q[,c("Open","High","Low","Last","Volume","Last")]
names(q) <- c("Open","High","Low","Close","Volume","Adjusted")
AGNC <- merge(AGNC,q,by="Date")
print(last(AGNC))
dailyReturn(AGNC)