r - Merge new row into an existing xts ( purpose: to add current stock quote to historical object from quantmod) -
what , attach current stock price historical xts object. example,
require(quantmod) x=getsymbols("aapl", = "2014-10-27" ,auto.assign=false) q = getquote('aapl') # 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 place "last" column q aapl.close column, high & low aapl.high, aapl.close, respectively. have tried far create new dataframe adding in new columns, renaming them , merging onto original xts not appear work. in advance.
you need create new xts object quote data , rbind historical data.
require(quantmod) x <- getsymbols("aapl", = "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
Comments
Post a Comment