Quantmod Omitting Tickers in Getsymbols

quantmod omitting tickers in getSymbols

You can use try within sapply like this:

library(quantmod)
WoW <- new.env()
##
sapply(SiP, function(x){
try(
getSymbols(
x,
from=as.Date("2001-01-01"),
to=as.Date("2007-01-01"),
env=WoW),
silent=TRUE)
})

Errors will be printed to the console (you could probably mitigate this if desired), but the tickers that do not generate errors will still produce data:

R> ls(WoW)
[1] "AA" "AEE" "AEP" "AES" "AP" "ARG" "ATI" "AVY" "BLL" "CF" "CMS" "CNP" "CTL" "D" "DOW" "DTE" "DUK" "ECL" "ED" "EIX"
[21] "EMN" "ETR" "EXC" "FCX" "FE" "FMC" "FTR" "GAS" "IFF" "IP" "LVLT" "MON" "MOS" "MWV" "NEE" "NEM" "NI" "NRG" "NU" "NUE"
[41] "OI" "PCG" "PEG" "PNW" "POM" "PPG" "PPL" "SCG" "SO" "SRE" "T" "TE" "TEG" "VZ" "WEC" "WIN" "XEL"
##
R> length(ls(WoW))
[1] 57
R> length(SiP)
[1] 59

So it looks like there were issues with 2 of the stocks, as sapply(...) successfully returned data for the other 57.

From here, objects can be accessed within WoW through your preferred method, e.g.

R> with(WoW, chartSeries(ARG))

Sample Image


Data:

SiP=c('AES','GAS','AEE','AEP','CNP', 'CMS','ED','D',
'DTE','DUK','EIX', 'ETR','EXC','FE','TEG',
'NEE','NI', 'NU','NRG','PCG','POM','PNW','PPL',
'PEG','SCG','SRE','SO','TE','WEC', 'XEL','T',
'CTL','FTR','LVLT','VZ', 'WIN','AP','ARG',
'AA','ATI','AVY', 'BLL','CF','DOW','D',
'EMN','ECL', 'FMC','FCX','IP','IFF','LYB',
'MWV', 'MON','MOS','NEM','NUE','OI','PPG')

Quantmod Error Handling Incorrect Tickers

getSymbols should throw the error. Try

symbols <- c("KO","FANATASTICALLYCOOL","MSFT","LUCKYDEVIL","LMT")
out <- sapply(symbols, function(s) tryCatch({
getSymbols(s , env = NULL)
}, error = function(e) NA)
)

dd <- lapply(out, function(x) if (any(is.na(x))) NA else Ad(x))
dd <- do.call(cbind, dd)
# KO.Adjusted FANATASTICALLYCOOL MSFT.Adjusted LUCKYDEVIL LMT.Adjusted
# 2007-01-03 18.03268 NA 23.47842 NA 66.64122
# 2007-01-04 18.04010 NA 23.43910 NA 66.46724
# 2007-01-05 17.91389 NA 23.30543 NA 66.70646
# 2007-01-08 18.02896 NA 23.53346 NA 67.91707
# 2007-01-09 18.04381 NA 23.55704 NA 67.85182
# 2007-01-10 18.06979 NA 23.32116 NA 68.56224

How to handle dash in yahoo finance tickers with getSymbols in R?

Dashes are not legal in R object or column names. To avoid an error, you need to enclose the illegal name in backticks. Your code will work if you do the following:

BTC = `BTC-USD`[ , "BTC-USD.Adjusted", drop=F]

Quotation marks will work for indexing inside braces, so only the reference to the object needs to be enclosed in backticks. However, to use the dollar-sign notation, you'd need backticks for the column name too, giving this abominable bit of code:

`BTC-USD`$`BTC-USD.Adjusted`

It's normally better to work with legal column names and only change to "real-world" names when outputting to graphs, tables, etc.

As a new R user, the most intuitive and transparent approach for changing object and column names is probably to just rename the data explicitly for each symbol. We'll switch from hyphens to underscores, since underscores are legal:

# Change name of object to legal name
BTC_USD = `BTC-USD`

# Change column names to legal names
names(BTC_USD) = gsub("-", "_", names(BTC_USD))

A more efficient, but less intuitive and transparent approach is to put the data in a list and operate on the list:

# Put data into a list; one symbol per list element
dat = mget(symbol.vec)

names(dat)
lapply(dat, head)
> names(dat)
[1] "BTC-USD" "ETH-USD"

> lapply(dat, head)
$`BTC-USD`
BTC-USD.Open BTC-USD.High BTC-USD.Low BTC-USD.Close BTC-USD.Volume BTC-USD.Adjusted
2015-08-06 278.00 279.60 274.28 277.89 11919665 277.89
2015-08-07 277.89 278.92 257.42 258.60 22308123 258.60
2015-08-08 258.60 266.75 258.56 263.87 15154749 263.87
2015-08-09 263.87 266.63 260.52 263.30 12873441 263.30
2015-08-10 263.30 269.90 261.44 269.03 13681939 269.03
2015-08-11 269.03 271.50 263.66 267.66 15232934 267.66

$`ETH-USD`
ETH-USD.Open ETH-USD.High ETH-USD.Low ETH-USD.Close ETH-USD.Volume ETH-USD.Adjusted
2015-08-06 0.6747 3.00 0.6747 3.00 371 3.00
2015-08-07 3.0000 3.00 0.1500 1.20 1438 1.20
2015-08-08 1.2000 1.20 1.2000 1.20 0 1.20
2015-08-09 1.2000 1.20 1.2000 1.20 0 1.20
2015-08-10 1.2000 1.20 0.6504 0.99 7419 0.99
2015-08-11 0.9900 1.29 0.9050 1.29 2376 1.29
# Rename list elements and columns to legal names
names(dat) = gsub("-", "_", names(dat))
dat = lapply(dat, function(x) setNames(x, gsub("-","_", names(x))))

names(dat)
lapply(dat, head)
> names(dat)
[1] "BTC_USD" "ETH_USD"

> lapply(dat, head)
$BTC_USD
BTC_USD.Open BTC_USD.High BTC_USD.Low BTC_USD.Close BTC_USD.Volume BTC_USD.Adjusted
2015-08-06 278.00 279.60 274.28 277.89 11919665 277.89
2015-08-07 277.89 278.92 257.42 258.60 22308123 258.60
2015-08-08 258.60 266.75 258.56 263.87 15154749 263.87
2015-08-09 263.87 266.63 260.52 263.30 12873441 263.30
2015-08-10 263.30 269.90 261.44 269.03 13681939 269.03
2015-08-11 269.03 271.50 263.66 267.66 15232934 267.66

$ETH_USD
ETH_USD.Open ETH_USD.High ETH_USD.Low ETH_USD.Close ETH_USD.Volume ETH_USD.Adjusted
2015-08-06 0.6747 3.00 0.6747 3.00 371 3.00
2015-08-07 3.0000 3.00 0.1500 1.20 1438 1.20
2015-08-08 1.2000 1.20 1.2000 1.20 0 1.20
2015-08-09 1.2000 1.20 1.2000 1.20 0 1.20
2015-08-10 1.2000 1.20 0.6504 0.99 7419 0.99
2015-08-11 0.9900 1.29 0.9050 1.29 2376 1.29

Calling a list of tickers in quantmod using R

This should get you started.

library(quantmod)
library(stringr) # for str_pad
stocks <- paste(str_pad(2705:2730,width=6,side="left",pad="0"),"SZ",sep=".")
get.stock <- function(s) {
s <- try(Cl(getSymbols(s,auto.assign=FALSE)),silent=T)
if (class(s)=="xts") return(s)
return (NULL)
}
result <- do.call(cbind,lapply(stocks,get.stock))
head(result)
# X002705.SZ.Close X002706.SZ.Close X002707.SZ.Close X002708.SZ.Close X002709.SZ.Close X002711.SZ.Close X002712.SZ.Close X002713.SZ.Close
# 2014-01-21 15.25 27.79 NA 17.26 NA NA NA NA
# 2014-01-22 14.28 28.41 NA 16.56 NA NA NA NA
# 2014-01-23 13.65 27.78 33.62 15.95 19.83 NA 36.58 NA
# 2014-01-24 15.02 30.56 36.98 17.55 21.81 NA 40.24 NA
# 2014-01-27 14.43 31.26 40.68 18.70 23.99 26.34 44.26 NA
# 2014-01-28 14.18 30.01 44.75 17.66 25.57 28.97 48.69 NA

This takes advantage of the fact that getSymbols(...) returns either an xts object, or a character string with an error message if the fetch fails.

Note that cbind(...) for xts objects aligns according to the index, so it acts like merge(...).

This produces an xts object, not a data frame. To convert this to a data.frame, use:

result.df <- data.frame(date=index(result),result)

getSplits (Quantmod) error when pulling multiple tickers

You can use try() to prevent it from breaking:

library(tidyverse)
library(BatchGetSymbols)
library(quantmod)

tickers <- GetSP500Stocks()[1:20,]

split_env = lapply(tickers$Tickers,function(x)try(getSplits(x)))
names(split_env) = tickers$Tickers

And if I am not wrong, you can get the ones without error

head(split_env[sapply(split_env,is.xts)])
$MMM
MMM.spl
1972-06-16 0.5
1987-06-16 0.5
1994-04-11 0.5
2003-09-30 0.5

$ABT
ABT.spl
1981-06-01 0.5000
1986-06-02 0.5000
1990-06-01 0.5000
1992-06-01 0.5000
1998-06-01 0.5000
2004-05-03 0.9356
2013-01-02 0.4798

$ABMD
ABMD.spl
2000-10-02 0.5

$ACN
ACN.spl
2011-12-30 0.1

$ATVI
ATVI.spl
2001-11-21 0.6666667
2003-06-09 0.6666667
2004-03-16 0.6666667
2005-03-23 0.7500000
2005-10-25 0.7500000
2008-09-08 0.5000000

$ADBE
ADBE.spl
1987-03-12 0.5
1988-11-23 0.5
1993-08-11 0.5
1997-07-29 1.0
1997-10-29 1.0
1999-10-27 0.5
2000-10-25 0.5
2005-05-24 0.5

getSymbols downloading data for multiple symbols and calculate returns

lapply is your friend:

Stocks = lapply(Symbols, function(sym) {
dailyReturn(na.omit(getSymbols(sym, from=StartDate, auto.assign=FALSE)))
})

Then to merge:

do.call(merge, Stocks)

Similar application for the other assignments

getSymbols (quantmod) giving wrong dates

As you mentioned in the comments, this looks like a timezone issue, possibly due to POSIX date conversion in the xts function (see this answer).

I am able to reproduce the issue in a fresh R session when Sys.getenv("TZ") is an empty character string. Setting the timezone to any valid timezone (not all tested), for example, "America/Chicago" yields expected dates, i.e., no Sundays:

In a fresh session (December 16th 2012 was a Sunday):

Sys.getenv("TZ")
# [1] ""

library(quantmod)
Data <- getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
tail(index(Data))
# [1] "2012-12-13" "2012-12-16" "2012-12-17" "2012-12-18" "2012-12-19" "2012-12-20"

Then change the timezone

Sys.setenv(TZ="America/Chicago")

Data <- getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
tail(index(Data))
# [1] "2012-12-14" "2012-12-17" "2012-12-18" "2012-12-19" "2012-12-20" "2012-12-21"

No Sundays.



Related Topics



Leave a reply



Submit