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

279 get quote resillience 2 #288

Closed
Closed
Changes from 2 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
60 changes: 23 additions & 37 deletions R/getQuote.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ function(Symbols,src='yahoo',what, ...) {
args <- list(Symbols=Symbols,...)
if(!missing(what))
args$what <- what
do.call(paste('getQuote',src,sep='.'), args)
df <- do.call(paste('getQuote',src,sep='.'), args)
if(nrow(df) != length(Symbols)){
#merge to generate empty rows for missing results from underlying source
df <- merge(list(Symbol = Symbols), df, by = "Symbol", all.x = TRUE)
joshuaulrich marked this conversation as resolved.
Show resolved Hide resolved
}
rownames(df) <- df$Symbol
df$Symbol <- NULL
df
}

`getQuote.yahoo` <-
Expand Down Expand Up @@ -48,9 +55,10 @@ function(Symbols,what=standardQuote(),...) {
SymbolsString,
"&fields=",QFc)
# The 'response' data.frame has fields in columns and symbols in rows
response <- jsonlite::fromJSON(URL)
response <- jsonlite::fromJSON(curl::curl(URL))
if (is.null(response$quoteResponse$error)) {
sq <- response$quoteResponse$result
Symbols <- sq$symbol
joshuaulrich marked this conversation as resolved.
Show resolved Hide resolved
} else {
stop(response$quoteResponse$error)
}
Expand All @@ -65,29 +73,21 @@ function(Symbols,what=standardQuote(),...) {
Qposix <- .POSIXct(sq$regularMarketTime, tz = NULL) # force local timezone
}

# merge join to produce empty rows for missing results, so that
# return value has the same number of rows and order as the input
if (length(Symbols) != NROW(sq)) {
allSymbols <- data.frame(symbol = Symbols, stringsAsFactors = FALSE)
sq <- merge(allSymbols, sq, by = "symbol", all = TRUE)
}

# Extract user-requested columns. Convert to list to avoid
# 'undefined column' error with data.frame.
qflist <- setNames(as.list(sq)[QF], QF)

# Fill any missing columns with NA
pad <- rep(NA, length(Symbols))
pad <- rep(NA, length(sq))
qflist <- lapply(qflist, function(e) if (is.null(e)) pad else e)

# Add the trade time and setNames() on other elements
qflist <- c(list(regularMarketTime = Qposix), setNames(qflist, QF))
qflist <- c(list(Symbol = Symbols, regularMarketTime = Qposix), setNames(qflist, QF))

df <- data.frame(qflist, stringsAsFactors = FALSE, check.names = FALSE)

rownames(df) <- Symbols
if(!is.null(QF.names)) {
colnames(df) <- c('Trade Time',QF.names)
colnames(df) <- c('Symbol','Trade Time',QF.names)
}
df
}
Expand Down Expand Up @@ -294,7 +294,7 @@ getQuote.av <- function(Symbols, api.key, ...) {
}
batchSymbols <- Symbols[i:min(nSymbols, i + 99)]
batchURL <- paste0(URL, paste(batchSymbols, collapse = ","))
response <- jsonlite::fromJSON(batchURL)
response <- jsonlite::fromJSON(curl::curl(batchURL))

if(NROW(response[["Stock Quotes"]]) < 1) {
syms <- paste(batchSymbols, collapse = ", ")
Expand All @@ -312,13 +312,9 @@ getQuote.av <- function(Symbols, api.key, ...) {
result$Last <- as.numeric(result$Last)
quoteTZ <- response[["Meta Data"]][["3. Time Zone"]]
result$`Trade Time` <- as.POSIXct(result$`Trade Time`, tz = quoteTZ)

# merge join to produce empty rows for missing results from AV
# so that return value has the same rows and order as the input
output <- merge(data.frame(Symbol = Symbols), result,
by = "Symbol", all.x = TRUE)
rownames(output) <- output$Symbol
return(output[, c("Trade Time", "Last", "Volume")])

#Normalize column names and output
return(result[, c("Symbol", "Trade Time", "Last", "Volume")])
}

`getQuote.tiingo` <- function(Symbols, api.key, ...) {
Expand Down Expand Up @@ -355,7 +351,7 @@ getQuote.av <- function(Symbols, api.key, ...) {
batch.url <- paste0(base.url, "&tickers=", paste(Symbols[i:batch.end], collapse = ","))
}

batch.result <- jsonlite::fromJSON(batch.url)
batch.result <- jsonlite::fromJSON(curl::curl(batch.url))

if(NROW(batch.result) < 1) {
syms <- paste(Symbols[i:batch.end], collapse = ", ")
Expand All @@ -373,19 +369,9 @@ getQuote.av <- function(Symbols, api.key, ...) {
}
r <- rbind(r, batch.result)
}

colnames(r) <- gsub("(^[[:alpha:]])", "\\U\\1", colnames(r), perl = TRUE)
r[, "Trade Time"] <- r[, "LastSaleTimestamp"]
r[, "LastSaleTimestamp"] <- NULL

# merge join to produce empty rows for missing results, so that
# return value has the same number of rows and order as the input
if(length(Symbols) != NROW(r)) {
allSymbols <- data.frame(Ticker = Symbols, stringsAsFactors = FALSE)
r <- merge(allSymbols, r, by = "Ticker", all.x = TRUE)
}

rownames(r) <- r$Ticker
std.cols <- c("Trade Time", "Open", "High", "Low", "Last", "Volume")
joshuaulrich marked this conversation as resolved.
Show resolved Hide resolved
return(r[, c(std.cols, setdiff(colnames(r), c(std.cols, "Ticker")))])

#Normalize column names and output
r <- r[, c("ticker", "lastsaleTimeStamp", "open", "high", "low", "last", "volume")]
joshuaulrich marked this conversation as resolved.
Show resolved Hide resolved
joshuaulrich marked this conversation as resolved.
Show resolved Hide resolved
colnames(r) <- c("Symbol", "Trade Time", "Open", "High", "Low", "Last", "Volume")
return(r)
}