Skip to content

Commit

Permalink
Handle missing regularMarketTime
Browse files Browse the repository at this point in the history
getQuote() would error if the user did not request any metrics that
have a value for regularMarketTime. Set the value to NA in these cases
so the output remains the same regardless of whether the endpoint
returns a regularMarketTime or not.

Thanks to @mehdiMBH for the report!

Fixes #255.
  • Loading branch information
joshuaulrich committed Jul 27, 2023
1 parent a585043 commit 0f97314
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions R/getQuote.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function(Symbols,what=standardQuote(),session=NULL,...) {
QF.names <- NULL
}
# JSON API currently returns the following fields with every request:
# language, quoteType, regularMarketTime, marketState, exchangeDataDelayedBy,
# language, quoteType, marketState, exchangeDataDelayedBy,
# exchange, fullExchangeName, market, sourceInterval, exchangeTimezoneName,
# exchangeTimezoneShortName, gmtOffSetMilliseconds, tradeable, symbol
QFc <- paste0(QF,collapse=',')
Expand All @@ -117,15 +117,21 @@ function(Symbols,what=standardQuote(),session=NULL,...) {
} else {
stop(response$quoteResponse$error)
}
# Always return symbol and time

# Always return symbol and time (if regularMarketTime is provided)
# Use exchange TZ, if possible. POSIXct must have only one TZ, so times
# from different timezones will be converted to a common TZ
tz <- sq[, "exchangeTimezoneName"]
if (length(unique(tz)) == 1L) {
Qposix <- .POSIXct(sq[,"regularMarketTime"], tz=tz[1L])
regularMktTime <- sq$regularMarketTime
if (is.null(regularMktTime)) {
Qposix <- .POSIXct(NA)
} else {
warning("symbols have different timezones; converting to local time")
Qposix <- .POSIXct(sq$regularMarketTime, tz = NULL) # force local timezone
tz <- sq[, "exchangeTimezoneName"]
if (length(unique(tz)) == 1L) {
Qposix <- .POSIXct(regularMktTime, tz = tz[1L])
} else {
warning("symbols have different timezones; converting to local time")
Qposix <- .POSIXct(regularMktTime, tz = NULL) # force local timezone
}
}

# Extract user-requested columns. Convert to list to avoid
Expand Down

0 comments on commit 0f97314

Please sign in to comment.