Merge Getsymbols Result into One Xts Object

Merging Multiple XTS Objects into a single XTS object

This can be easily done by using the quantmod add-on qmao. Take for instance several ticker symbols, download data (or import data from file ) as xts-objects and then create one object with prices or returns in one go. Let’s assume you want to create a correlation matrix from the daily returns:

library(qmao)
tickers <- c('MSFT','AAPL','AMZN')
getsSymbols(tickers, from = '2010-01-01')
# xts-object of daily returns (RF stands for `Return Frame`,PF returns a
# Price Frame)
returns <- RF(tickers, silent = TRUE, type = 'discrete')

> tail(returns)
MSFT AAPL AMZN
2017-01-10 -0.0003192848 0.001008513 -0.001279876
2017-01-11 0.0091025233 0.005373176 0.003920085
2017-01-12 -0.0091786360 -0.004175365 0.018297408
2017-01-13 0.0014374700 -0.001760998 0.004301657
2017-01-17 -0.0027113556 0.008064508 -0.009080505
2017-01-18 -0.0004797537 -0.000083350 -0.002766377

To get the correlation matrix:

> cor(returns,use = 'pairwise.complete.obs')
MSFT AAPL AMZN
MSFT 1.0000000 0.4655617 0.4701170
AAPL 0.4655617 1.0000000 0.4390303
AMZN 0.4701170 0.4390303 1.0000000

Have a look at the functions PF and RF they are compact and incredibly useful.

Combine multiple xts objects created by getSymbols

The simplest way to do this is to load all the data into a new environment. Then you can use eapply to extract all the adjusted close columns into a list. Finally, you can use do.call to merge all the adjusted close prices in the list into one xts object.

library(quantmod)
tickers <- c("SNC.TO", "PHII", "HBC.TO", "GTE", "MOO",
"MND.TO", "STKL", "SXC","XIU.TO")
dataEnv <- new.env()
getSymbols(tickers, from="2010-06-30", to="2015-06-30", env=dataEnv)
plist <- eapply(dataEnv, Ad)
pframe <- do.call(merge, plist)

Merge output from quantmod::getSymbols

The premise of your question is wrong. getSymbols returns an xts object, not a data.frame:

R> library(quantmod)
R> f <- getSymbols("AAPL", auto.assign=FALSE)
R> str(f)
An ‘xts’ object on 2007-01-03/2015-08-14 containing:
Data: num [1:2170, 1:6] 86.3 84 85.8 86 86.5 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
$ src : chr "yahoo"
$ updated: POSIXct[1:1], format: "2015-08-15 00:46:49"

xts objects do not have a "Date" column. They have an index attribute that holds the datetime. xts extends zoo, so please see the zoo vignettes as well as the xts vignette and FAQ for information about how to use the classes.

Merging xts objects is as simple as:

R> f <- merge(f, rsi14=RSI(Ad(f), 14))

Or you could just use $<- to add/merge a column to an existing xts object:

R> f$rsi14 <- RSI(Ad(f), 14)

merge.xts on specific columns over a for loop

You're on the right track by using an environment. Good job!

You want to extract the adjusted close column from each symbol and merge them into one xts object. You're on the right track with your for loop, but there's an easier way. You can loop over the elements in an environment using lapply(), and use the Ad() function to extract the adjusted close column. Then use do.call(merge, ...) to call the merge function on the output of lapply().

merged_prices <- do.call(merge, lapply(e, Ad))

You can merge the adjusted close for GSPC at this point (merge(Ad(GSPC), merged_prices)), or you could include it in your list of tickers.

Merge new row into an existing xts ( purpose: to add current stock quote to historical object from quantmod)

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

Merge two xts objects (matrices) into a single array in R

I don't think xts objects support 3d arrays. You'll probably need an ugly solution like the one below. Putting everything into an array coerces things to numeric values. But at least this preserves the date index, albeit in a different format because arrays can only have one data type.

require(quantmod)
getSymbols("GLD;SLV")
GLD <- cbind(index(GLD), as.matrix(GLD))
SLV <- cbind(index(SLV), as.matrix(SLV))
C <- array(,c(dim(GLD),2))
C[,,1] <- GLD
C[,,2] <- SLV

How to combine xts datasets with slightly different dates

What you are looking for is merge function. Something like

merge(bondIndex[,1], bondIndex[,2], all = TRUE)


Related Topics



Leave a reply



Submit