joshuaulrich / quantmod

Quantitative Financial Modelling Framework
http://www.quantmod.com/
GNU General Public License v3.0
809 stars 223 forks source link

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

Closed hg2581 closed 4 years ago

hg2581 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

Thank you!

rjvelasquezm commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

@rjvelasquezm this should be fixed in master now.

rjvelasquezm commented 4 years ago

Awesome, thanks a lot!!