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 1 commit
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
54 changes: 20 additions & 34 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)
}
rownames(df) <- df$Symbol
df$Symbol <- NULL
df
}

`getQuote.yahoo` <-
Expand Down Expand Up @@ -51,6 +58,7 @@ function(Symbols,what=standardQuote(),...) {
response <- jsonlite::fromJSON(curl::curl(URL))
if (is.null(response$quoteResponse$error)) {
sq <- response$quoteResponse$result
Symbols <- sq$symbol
} 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 @@ -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 @@ -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")
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")]
colnames(r) <- c("Symbol", "Trade Time", "Open", "High", "Low", "Last", "Volume")
return(r)
}