Skip to content

Commit

Permalink
Use crumb in getOptionChain.yahoo()
Browse files Browse the repository at this point in the history
Fixes #407.
  • Loading branch information
joshuaulrich committed Jan 29, 2024
1 parent 12dd38f commit 1964a32
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions R/getOptionChain.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function(Symbols, Exp=NULL, src="yahoo", ...) {
optionChain[!vapply(optionChain, is.null, logical(1))]
}

getOptionChain.yahoo <- function(Symbols, Exp, ...)
getOptionChain.yahoo <- function(Symbols, Exp, ..., session=NULL)
{
NewToOld <- function(x, tz = NULL) {
if(is.null(x) || length(x) < 1)
Expand Down Expand Up @@ -67,16 +67,24 @@ getOptionChain.yahoo <- function(Symbols, Exp, ...)
return(d)
}

if (is.null(session)) {
session <- .yahooSession()
}
if (!session$can.crumb) {
stop("Unable to obtain yahoo crumb. If this is being called from a GDPR country, Yahoo requires GDPR consent, which cannot be scripted")
}

# Don't check the expiry date if we're looping over dates we just scraped
checkExp <- !hasArg(".expiry.known") || !match.call(expand.dots=TRUE)$.expiry.known
# Construct URL
urlExp <- paste0("https://query2.finance.yahoo.com/v7/finance/options/", Symbols[1])
urlExp <- paste0("https://query2.finance.yahoo.com/v7/finance/options/", Symbols[1],
"?crumb=", session$crumb)
# Add expiry date to URL
if(!checkExp)
urlExp <- paste0(urlExp, "?&date=", Exp)
urlExp <- paste0(urlExp, "&date=", Exp)

# Fetch data (jsonlite::fromJSON will handle connection)
tbl <- try(jsonlite::fromJSON(urlExp), silent = TRUE)
tbl <- try(jsonlite::fromJSON(curl::curl(urlExp, handle = session$h)), silent = TRUE)

if(inherits(tbl, "try-error")) {
msg <- attr(tbl, "condition")[["message"]]
Expand All @@ -94,7 +102,7 @@ getOptionChain.yahoo <- function(Symbols, Exp, ...)

if(is.null(Exp)) {
# Return all expiries if Exp = NULL
out <- lapply(all.expiries, getOptionChain.yahoo, Symbols=Symbols, .expiry.known=TRUE)
out <- lapply(all.expiries, getOptionChain.yahoo, Symbols=Symbols, .expiry.known=TRUE, session=session)
# Expiry format was "%b %Y", but that's not unique with weeklies. Change
# format to "%b.%d.%Y" ("%Y-%m-%d wouldn't be good, since names should
# start with a letter or dot--naming things is hard).
Expand All @@ -116,9 +124,9 @@ getOptionChain.yahoo <- function(Symbols, Exp, ...)

expiry.subset <- all.expiries[valid.expiries]
if(length(expiry.subset) == 1)
return(getOptionChain.yahoo(Symbols, expiry.subset, .expiry.known=TRUE))
return(getOptionChain.yahoo(Symbols, expiry.subset, .expiry.known=TRUE, session=session))
else {
out <- lapply(expiry.subset, getOptionChain.yahoo, Symbols=Symbols, .expiry.known=TRUE)
out <- lapply(expiry.subset, getOptionChain.yahoo, Symbols=Symbols, .expiry.known=TRUE, session=session)
# See comment above regarding the output names
return(setNames(out, format(all.expiries.posix[valid.expiries], "%b.%d.%Y")))
}
Expand Down

0 comments on commit 1964a32

Please sign in to comment.