Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplified signature and implementation #344

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ quantmod.Rproj
src/*.o
src/*.so
src/*.dll
*.Rproj
61 changes: 14 additions & 47 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,6 @@ getSymbols.tiingo <- function(Symbols, env, api.key,
adjust=FALSE,
from='2007-01-01',
to=Sys.Date(),
data.type="json",
...) {

importDefaults("getSymbols.tiingo")
Expand Down Expand Up @@ -1510,62 +1509,30 @@ getSymbols.tiingo <- function(Symbols, env, api.key,
if (verbose) cat("loading", sym.name, ".....")
from.strftime <- strftime(from, format = "%Y-%m-%d")
to.strftime <- strftime(to, format = "%Y-%m-%d")

tiingo.names <- c("open", "high", "low", "close", "volume",
"adjOpen", "adjHigh", "adjLow", "adjClose",
"adjVolume", "divCash", "splitFactor")
qm.names <- paste(sym, c("Open", "High", "Low", "Close", "Volume",
"Open", "High", "Low", "Close", "Volume",
"DivCash", "SplitFactor"), sep=".")
if (isTRUE(adjust)) {
return.columns <- tiingo.names[6:10]
} else {
return.columns <- tiingo.names[1:5]
}

URL <- paste0("https://api.tiingo.com/tiingo/",
periodicity, "/",
sym.name, "/prices",
"?startDate=", from.strftime,
"&endDate=", to.strftime,
"&format=", data.type,
"&columns=", paste0(return.columns, collapse=","))
# If rate limit is hit, the csv API returns HTTP 200 (OK), while json API
# returns HTTP 429. The latter caused download.file() to error, but the
# contents of 'tmp' still contain the error message.
h <- curl::new_handle()
curl::handle_setheaders(h, Authorization = paste("Token", api.key))
response <- curl::curl_fetch_memory(URL, h)
response.data <- rawToChar(response$content)

if (data.type == "json") {
stock.data <- jsonlite::fromJSON(response.data)
if (verbose) cat("done.\n")
} else {
stock.data <- read.csv(text=response.data, as.is=TRUE)
}
"&format=csv",
"&token=", api.key)
#tiingo will return a text error for ticker not found, which read.csv converts
#to a zero row, 1 column data.frame, with a warning
stock.data <- suppressWarnings(read.csv(URL, as.is=TRUE))
# check for error
if (!all(return.columns %in% names(stock.data))) {
if (data.type == "json") {
msg <- stock.data$detail
} else {
msg <- readLines(response.data, warn=FALSE)
}
msg <- sub("Error: ", "", msg)
if (NCOL(stock.data) == 1) {
msg <- sub("Error: ", "", colnames(stock.data))
stop(msg, call. = FALSE)
}

tm.stamps <- as.POSIXct(stock.data[, "date"], ...)
stock.data[, "date"] <- NULL

# adjusted column names
adjcols <- grepl("^adj", colnames(stock.data))
# order Tiingo column names before converting to quantmod names
stock.data <- OHLCV(stock.data)
if (any(adjcols)) {
# put adjusted columns last
stock.data <- stock.data[, c(which(!adjcols), which(adjcols))]
if (adjust) {
stock.data <- stock.data[, c("adjOpen", "adjHigh", "adjLow", "adjClose", "adjVolume")]
} else {
stock.data <- stock.data[, c("open", "high", "low", "close", "volume")]
}
# now convert to quantmod column names
colnames(stock.data) <- qm.names[match(colnames(stock.data), tiingo.names)]
colnames(stock.data) <- paste(sym, c("Open", "High", "Low", "Close", "Volume"), sep=".")

# convert data to xts
xts.data <- xts(stock.data, tm.stamps, src="tiingo", updated=Sys.time())
Expand Down