joshuaulrich / quantmod

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

getOptionChain: object 'bid' not found #338

Closed mathieu-lemire closed 3 years ago

mathieu-lemire commented 3 years ago

If there are no bid, ask or volume data for all strikes at a certain expiry date, getOptionChain() will return the error message

getOptionChain("AAPL",Exp="2021-09-03" )
Error in data.frame(Strike = strike, Last = lastprice, Chg = change, Bid = bid,  : 
  object 'bid' not found

getOptionChain("AAPL", NULL ) will fail as well.

The above call is probably hard to reproduce since data change, so I am attaching below the json from

https://query2.finance.yahoo.com/v7/finance/options/AAPL?&date=1630627200

AAPL.txt (downloaded on 21/07/25)

tbl <- jsonlite::fromJSON("AAPL.txt") 
dftables <- lapply(tbl$optionChain$result$options[[1]][,c("calls", "puts")], `[[`, 1L)
names(dftables$calls)

 [1] "contractSymbol"    "strike"            "currency"         
 [4] "lastPrice"         "change"            "percentChange"    
 [7] "openInterest"      "contractSize"      "expiration"       
[10] "lastTradeDate"     "impliedVolatility" "inTheMoney"       

bid, ask and volume are missing, and as a result the NewToOld() function in getOptionChain.yahoo() will fail.

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1    jsonlite_1.6  
joshuaulrich commented 3 years ago

I assume you deleted your comment because it had already been fixed. I restored it so it wouldn't confuse me or others in the future. Don't worry if you accidentally report something that's been fixed. It happens.

I ask people to make sure to use the most recent development version so they don't spend time hunting down bugs that have been fixed; out of respect for your time. :wink:

Duplicate of #300.