The following code renders a certain response to a query:
library(quantmod)
# Load ticker data from 2020-01-01 till 2021-02-02
t <- c("NKLA", "MPNGF", "RMO", "JD", "MSFT")
getSymbols.yahoo(t, auto.assign = TRUE, env = globalenv(),
from = "2020-01-01", to = "2021-02-02")
# Close all Internet connections as a precaution
closeAllConnections()
# Find xts objects
x <- names(which(unlist(eapply(.GlobalEnv, is.xts))))
# Convert xts to data.frame
for (i in seq_along(x)) {
assign(x[i], fortify.zoo(get(x[i])))
}
# The query
sapply(mget(x), names)
# The rendering
NKLA MPNGF MSFT JD RMO
[1,] "Index" "Index" "Index" "Index" "Index"
[2,] "NKLA.Open" "MPNGF.Open" "MSFT.Open" "JD.Open" "RMO.Open"
[3,] "NKLA.High" "MPNGF.High" "MSFT.High" "JD.High" "RMO.High"
[4,] "NKLA.Low" "MPNGF.Low" "MSFT.Low" "JD.Low" "RMO.Low"
[5,] "NKLA.Close" "MPNGF.Close" "MSFT.Close" "JD.Close" "RMO.Close"
[6,] "NKLA.Volume" "MPNGF.Volume" "MSFT.Volume" "JD.Volume" "RMO.Volume"
[7,] "NKLA.Adjusted" "MPNGF.Adjusted" "MSFT.Adjusted" "JD.Adjusted" "RMO.Adjusted"
The same code adjusted to fit in a specific environment:
library(quantmod)
symbolUpdates.env <- new.env()
# Load ticker data from 2020-01-01 till 2021-02-02 to symbolUpdates.env
t2 <- c("NKLA", "MPNGF", "RMO", "JD", "MSFT")
getSymbols.yahoo(t2, auto.assign = TRUE, env = symbolUpdates.env,
from = "2020-01-01", to = "2021-02-02")
# Close all Internet connections as a precaution
closeAllConnections()
# Find xts objects in symbolUpdates.env
x2 <- names(which(unlist(eapply(symbolUpdates.env, is.xts))))
# Convert xts to data.frame that are in symbolUpdates.env
for (i2 in seq_along(x2)) {
assign(envir = symbolUpdates.env, x2[i], fortify.zoo(get(x2[i2])))
}
# The query in symbolUpdates.env
sapply(mget(x2, envir = symbolUpdates.env), names)
# The rendering from symbolUpdates.env
RMO NKLA JD MSFT MPNGF
[1,] "Index" "Index" "Index" "Index" "Index"
[2,] "Index" "Index" "Index" "Index" "Index"
[3,] "RMO.Open" "NKLA.Open" "JD.Open" "MSFT.Open" "MPNGF.Open"
[4,] "RMO.High" "NKLA.High" "JD.High" "MSFT.High" "MPNGF.High"
[5,] "RMO.Low" "NKLA.Low" "JD.Low" "MSFT.Low" "MPNGF.Low"
[6,] "RMO.Close" "NKLA.Close" "JD.Close" "MSFT.Close" "MPNGF.Close"
[7,] "RMO.Volume" "NKLA.Volume" "JD.Volume" "MSFT.Volume" "MPNGF.Volume"
[8,] "RMO.Adjusted" "NKLA.Adjusted" "JD.Adjusted" "MSFT.Adjusted" "MPNGF.Adjusted"
My questions:
- Are there any mistakes in the code that an additional
Index
column appears insymbolUpdates.env
? - If yes, what are they?
- What could correct the issue?
- Also, the order of the tickers is not respected in
symbolUpdates.env
, why?
Thanks in advance.
Systems used:
- R version: 4.1.1 (2021-08-10)
- RStudio version: 1.4.1717
- OS: macOS Catalina version 10.15.7 and macOS Big Sur version 11.6