What I like to do is to get and attach current stock price to an historical xts object. example,
require(quantmod)
x=getSymbols("AAPL", from = "2014-10-27" ,auto.assign=FALSE)
q = getQuote('AAPL')
# this produces, > tail(x)
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2015-05-06 126.56 126.75 123.36 125.01 72141000 124.49
2015-05-07 124.77 126.08 124.02 125.26 43940900 125.26
2015-05-08 126.68 127.62 126.11 127.62 55550400 127.62
2015-05-11 127.39 127.56 125.63 126.32 42035800 126.32
2015-05-12 125.60 126.88 124.82 125.87 47109200 125.87
2015-05-13 126.15 127.19 125.87 126.01 34322000 126.01
> q
Trade Time Last Change % Change Open High Low Volume
AAPL 2015-05-14 11:38:00 128.3993 2.3893 +1.8961% 127.45 128.45 127.16 22635316
What I like to do is place the "Last" column from q as the AAPL.Close column, High & Low into AAPL.High, AAPL.Close, respectively. I have tried so far to create a new dataframe by slowly adding in new columns, renaming them and merging back onto the original xts but it does not appear to work. Thanks in advance.
You just need to create a new xts object from the quote data and rbind
it to the historical data.
require(quantmod)
x <- getSymbols("AAPL", from = "2014-10-27" ,auto.assign=FALSE)
q <- getQuote('AAPL')
qCols <- c("Open","High","Low","Last","Volume","Last")
qx <- xts(q[,qCols], as.Date(q[,"Trade Time"]))
y <- rbind(x, qx)
tail(y)
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
# 2015-05-07 124.77 126.08 124.02 125.26 43940900 125.26
# 2015-05-08 126.68 127.62 126.11 127.62 55550400 127.62
# 2015-05-11 127.39 127.56 125.63 126.32 42035800 126.32
# 2015-05-12 125.60 126.88 124.82 125.87 47109200 125.87
# 2015-05-13 126.15 127.19 125.87 126.01 34322000 126.01
# 2015-05-14 127.45 128.45 127.16 128.40 22635316 128.40