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

getOptionChain does not pull down last trade date and implied volatility from Yahoo #304

Closed
hg2581 opened this issue May 15, 2020 · 6 comments

Comments

@hg2581
Copy link

hg2581 commented May 15, 2020

Description

getOptionChain from CRAN does not pull last trade or implied volatility from Yahoo.

Expected behavior

Both ot these should be pulled since they are useful.

Minimal, reproducible example

getOptionChain("SPY")

@joshuaulrich
Copy link
Owner

joshuaulrich commented May 16, 2020

I can add IV, but last trade is already returned in the "Last" column.

oc <- quantmod::getOptionChain("SPY")
head(oc$puts)
                   Strike Last Chg Bid  Ask Vol   OI
SPY200518P00140000    140 0.01   0   0 0.01   1  844
SPY200518P00145000    145 0.06   0   0 0.01   3   50
SPY200518P00150000    150 0.01   0   0 0.01   2 1125
SPY200518P00155000    155 0.04   0   0 0.01   1   16
SPY200518P00160000    160 0.01   0   0 0.01  40  166
SPY200518P00165000    165 0.01   0   0 0.01   1   96

Maybe you meant "lastTradeDate"?

@hg2581
Copy link
Author

hg2581 commented May 16, 2020

Yes, you are correct, I do mean lastTradeDate. Both that and IV have value.

Thank you!

@joshuaulrich joshuaulrich changed the title getOptionChain does not pull down last trade and implied volatility from Yahoo getOptionChain does not pull down last trade date and implied volatility from Yahoo May 23, 2020
@rjvelasquezm
Copy link

Just a heads up, I downloaded the github version of quantmod and am now running into issues with getOptionChain.

Trying to run the following command: spy_chain = getOptionChain('SPY','2020')

returns the error: Error in class(xx) <- cl : attempt to set an attribute on NULL

If I tried to pull options for AAPL rather than SPY there didn't seem to be any issue there.

@joshuaulrich
Copy link
Owner

Thanks for testing, and thanks for the report!

I can replicate, and it looks like it's because the lastTradeDate is NULL.

R> spy_chain = quantmod::getOptionChain('SPY','2020')
## Error in class(xx) <- cl : attempt to set an attribute on NULL
R> traceback()
## 6: .POSIXct(dftables$calls$lastTradeDate, tz = tz)
## 5: FUN(X[[i]], ...)
## 4: lapply(expiry.subset, getOptionChain.yahoo, Symbols = Symbols,
##        .expiry.known = TRUE)
## 3: getOptionChain.yahoo(Symbols = "SPY", Exp = "2020")
## 2: do.call(Call, list(Symbols = Symbols, Exp = Exp, ...))
## 1: quantmod::getOptionChain("SPY", "2020")
R> .POSIXct
## function (xx, tz = NULL, cl = c("POSIXct", "POSIXt"))
## {
##     class(xx) <- cl    ## <- this line throws the error because 'xx' is NULL
##     attr(xx, "tzone") <- tz
##     xx
## }
## <bytecode: 0x5644f9943b28>
## <environment: namespace:base>

I will fix. Thanks again!

joshuaulrich added a commit that referenced this issue Jun 20, 2020
Converting the trade time to the exchange timezone before calling
NewToOld() would throw an error when there was no lastTradeDate
column, because of the call to .POSIXct(NULL).

Add a 'tz' argument to NewToOld(), so we can pass in the exchange
timezone. Now the call to .POSIXct() is after adding a 'LastTradeTime'
column, and a call to .POSIXct(NA) does not throw an error.

The obsolete code was necessary when we were scraping HTML and is no
longer needed now that we're parsing JSON.

Replace the call to mapply() with lapply(), since we are only looping
over one list. We don't need gsub() to remove commas, because
jsonlite::fromJSON() will return integers and numbers when applicable.
We don't need to call type.convert() for the same reason. Plus it
would convert the datetimes to character.

See #304.
@joshuaulrich
Copy link
Owner

@rjvelasquezm this should be fixed in master now.

@rjvelasquezm
Copy link

Awesome, thanks a lot!!

@joshuaulrich joshuaulrich added this to the Release 0.4.18 milestone Nov 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants