Reputation: 31
I am attempting to create a series of empty xts objects via a for loop, but I am failing.
I have created a character vector named SYMBOL_vector which contains the names of the empty XTS objects I wish to create. I also have also downloaded some stock market data using getSymbols, including data for the symbol SPY. As a result, an XTS object named SPY exists.
My for loop code is:
for (i in 1 : length(SYMBOL_vector)) {
SYMBOL_vector[i] <- as.xts(order.by = index(SPY))
}
When I run the code I receive the following error:
Error in xts(x = NULL, order.by = x, ...) : formal argument "order.by" matched by multiple actual arguments
If it matters to the feedback, once the empty xts objects are created, I am going to write another for loop to fill each with daily return data.
I'm not opposed to taking a totally different approach to the loop...this one just seemed to be quick and easy. Thanks in advance for any help!
Upvotes: 3
Views: 2971
Reputation: 49810
Your code should work if you just use xts
instead of as.xts
, however, it's not really the "R way" (or "quantmod way") to attack the problem.
Consider this:
library(quantmod)
s <- c("SPY", "DIA", "QQQ")
e <- new.env() # an empty environment to hold yahoo price data
getSymbols(s, env=e)
L <- eapply(e, dailyReturn) # a list of returns
L$SPY
You can probably stop here, but if you want, you can convert the list to an environment.
ret <- as.environment(L) # an environment with xts objects of daily returns
ls(ret)
get("DIA", pos=ret)
Or if you really want you could attach that ret
environment (NOT recommended)
attach(ret) # not recommended
head(QQQ)
# daily.returns
#2007-01-03 -0.0050621261
#2007-01-04 0.0189639223
#2007-01-05 -0.0047662279
#2007-01-08 0.0006841505
#2007-01-09 0.0050136737
#2007-01-10 0.0117913832
Upvotes: 3
Reputation: 2986
@PHXtrader what you are trying to do is to replace an element in a vector of characters with a xts object which is not what you want to do and does not make sense. To assign a value to a name you have to use the 'assign' function (?assign to get further information). The other error in your code is that you use 'as.xts' instead of only 'xts' . Have a look at this toy example:
tickers <- c("AMZN","AAPL") # this is a character object
getSymbols("SPY",from="2013-09-01") # this is a xts object
for (i in 1:length(tickers)) {
assign(tickers[i],xts(order.by = index(SPY)))
}
the result are 2 xts objects, named AMZN and AAPL with zero-with.
>str(AAPL)
An 'xts' object of zero-width
> AAPL
Data:
numeric(0)
Index:
Date[1:9], format: "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" "2013-09-09" ...
I think a better and safer way to accomplish what you want to do is by using a list ( or sometimes creating a new environment). Creating objects as above tends to lead to bugs that are very difficult to find and debug, these types of constructs lead to accidentally overwriting variables and many other potential errors. The following example avoids a loop and creates the empty objects in a list:
tickers <- c("AMZN","AAPL")
mylist <- rep(as.list(xts(order.by = index(SPY))),2) # creates 2 empty xts-objects in list
names(mylist) <- tickers
> str(mylist)
List of 2
$ AMZN:An 'xts' object of zero-width
$ AAPL:An 'xts' object of zero-width
Upvotes: 0