Getsymbols and Using Lapply, Cl, and Merge to Extract Close Prices

getSymbols and using lapply, Cl, and merge to extract close prices

As I said in my reply on R-help, getSymbols is vectorized, so there's no need to loop over tickers. You don't need your myX function and the lapply call is completely unnecessary / redundant.

The code in my original answer works. Why are you trying other combinations?

tickers <- c("SPY","DIA","IWM","SMH","OIH","XLY",
"XLP","XLE","XLI","XLB","XLK","XLU")
getSymbols(tickers, from="2001-03-01", to="2011-03-11")
ClosePrices <- do.call(merge, lapply(tickers, function(x) Cl(get(x))))
head(ClosePrices)
# SPY.Close DIA.Close IWM.Close SMH.Close OIH.Close XLY.Close
# 2001-03-01 124.60 104.68 94.80 44.60 87.45 26.10
# 2001-03-02 123.61 104.80 95.05 45.34 91.20 26.30
# 2001-03-05 124.74 105.57 94.70 47.01 92.86 26.02
# 2001-03-06 126.08 106.15 96.10 49.59 94.34 26.68
# 2001-03-07 126.98 107.45 96.60 49.20 97.36 27.34
# 2001-03-08 127.12 108.61 95.80 49.20 97.59 27.78
# XLP.Close XLE.Close XLI.Close XLB.Close XLK.Close XLU.Close
# 2001-03-01 26.39 32.10 29.28 21.14 28.80 31.62
# 2001-03-02 26.64 32.83 29.45 21.64 27.80 31.70
# 2001-03-05 26.54 33.01 29.82 22.03 28.40 31.64
# 2001-03-06 26.00 33.18 30.25 21.98 29.60 31.60
# 2001-03-07 25.83 33.89 30.61 22.63 29.64 31.45
# 2001-03-08 26.05 34.23 30.80 22.71 29.05 32.04

Quantmod, getSymbols, Extracting Close Price in R

try the following:

  library(plyr)
library(quantmod)
stocks <- c("IBM","GM")
data.env <- new.env()

### here we use l_ply so that we don't double save the data
### getSymbols() does this already so we just want to be memory efficient
### go through every stock and try to use getSymbols()
l_ply(stocks, function(sym) try(getSymbols(sym,env=data.env),silent=T))

### now we only want the stocks that got stored from getSymbols()
### basically we drop all "bad" tickers
stocks <- stocks[stocks %in% ls(data.env)]

### now we just loop through and merge our good stocks
### if you prefer to use an lapply version here, that is also fine
### since now we are just collecting all the good stock xts() objects
data <- xts()
for(i in seq_along(stocks)) {
symbol <- stocks[i]
data <- merge(data, Ad(get(symbol,envir=data.env)))
}

How to extract only closing prices with Quantmod

You can do it with this pattern:

tickers <- c("1COV.DE", "ADS.DE", "ALV.DE", "BAS.DE")

# Store all data in a new environment
e <- new.env()
getSymbols(tickers, from = "2014-10-01", adjust = TRUE, env = e)

# Combine close prices
prices <- do.call(merge, lapply(e, Cl))
# remove leading "X" created by make.names()
colnames(prices) <- gsub("^X", "", colnames(prices))
# remove ".Close" suffix
colnames(prices) <- gsub(".Close", "", colnames(prices), fixed = TRUE)
# reorder columns to match 'tickers'
prices <- prices[, tickers]

How can I download a set of prices with getSymbols and store them in the order it was requested?

You could just subset the results of eapply into the order you want.

library(quantmod)
tickers <- c("^GSPC", "AAPL", "MSFT", "GOOG", "^RUT")
myenv <- new.env()
symnames <- getSymbols(tickers, env=myenv) # getSymbols returns adjusted names
ts <- do.call(merge, eapply(myenv, Ad)[symnames])

How to download multiple closing stock prices only with getSymbols into separate xts files?

for part 1, see my answer here. Your method won't work for index symbols like ^GSPC or in general any symbol starting with special characters (due to the automatic assignment).

as for part 3, once you fetched all your symbols and stored them into myList as described in the link above, try the following to loop through your list and export elements of the list to your working directory:

require(quantmod)

#Vector of symbols to fetch prices for
symbols <- c('MSFT','SBUX','GOOGL')

#Initialize a list to store the fetched prices
myList <- list()

#Loop through symbols, fetch prices, and store in myList
myList <-lapply(symbols, function(x) {getSymbols(x,auto.assign=FALSE)} )

#Housekeeping
names(myList) <- symbols

#Export to seperate files
quiet <- lapply(1:length(myList), function(x){ #looping through all elements of your list
write.csv(myList[[x]], #accessing the xth element of your list
paste0(names(myList)[x],'.csv'), #naming the exported element
row.names=index(myList[[x]]) #include dates in the export
) #close write.csv
} #close function
) #close lapply

EDIT: Combined the two posts as per the first comment.

Applying function to a subset of xts quantmod

Use apply.yearly() (a convenience wrapper around the more general period.apply()) to call a function on yearly subsets of the xts object returned by getSymbols().

You can use the Cl() function to extract the close column from objects returned by getSymbols().

stock = getSymbols("AAPL", from = "2010-01-01", auto.assign = FALSE)
apply.yearly(Cl(stock), sd)
## AAPL.Close
## 2010-12-31 5.365208
## 2011-12-30 3.703407
## 2012-12-31 9.568127
## 2013-12-31 6.412542
## 2014-12-31 13.371293
## 2015-12-31 7.683550
## 2016-12-30 7.640743
## 2017-12-29 14.621191
## 2018-12-31 20.593861
## 2019-12-31 34.538978
## 2020-06-19 29.577157

Quantmod save tickers to files in a loop or lapply

UPDATE: The solution below doesn't solve the OP's problem (see comments). See the edit after the jump.

The default of auto.assign=TRUE is supposed to make things easier when using getSymbols interactively. Set auto.assign=FALSE when using getSymbols in a function; it will make things much easier.

buildhist <- function(x,start,end) {
y <- getSymbols(x, from=start, to=end, adjust=TRUE, auto.assign=FALSE)
save(y, file= paste(x, "hist.rda", sep="_"), ascii = FALSE)
}

You can remove punctuation characters (including the caret) via gsub. See ?gsub and ?regex for details.

X <- gsub("[[:punct:]]","",x)  # remove all punctuation
X <- gsub("\\^","",x) # remove just the carat

I didn't test my initial answer. This solution should work.

buildhist <- function(x,start,end) {
getSymbols(x, from=start, to=end, adjust=TRUE)
X <- toupper(gsub("\\^","",x)) # what getSymbols.yahoo does
save(list=X, file= paste(X, "hist.rda", sep="_"), ascii = FALSE)
}

require(quantmod)
tckr <- c("^GSPC","YHOO","XLB")
lapply(tckr,buildhist,start="1995-01-01",end="2011-11-30")

If you're only using daily data on a small number of symbols, you may be able to load them all to one environment and just save the environment. That could save you a lot of trouble. Then you could load the environment to a new session, attach it, and have all the data at your fingers.

myEnv <- new.env()
getSymbols(paste(tckr,sep=";"), start="1995-01-01", end="2011-11-30",
env=myEnv, adjust=TRUE)
save(myEnv, file="myTickerData.rda")


Related Topics



Leave a reply



Submit