joshuaulrich / quantmod

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

getOptionChain can no longer access Yahoo Finance with the URL it uses #407

Closed cotyreh closed 8 months ago

cotyreh commented 8 months ago

Description

As of the morning of 26 Jan 2023 CST, there was some change on Yahoo Finance's side where getOptionChain() no longer has permission to access the option chain data with the link it uses. I know that it happened early in the morning because my script logs show that it was running as intended until the error popped up.

Expected behavior

Here is an example from my logs of before and after the error started occurring:

Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
Loading required package: xts
Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
Warning: no data for '^SPX' expiry , omitting
    (server response: cannot open the connection to 'https://query2.finance.yahoo.com/v7/finance/options/^SPX')
Warning message:
In open.connection(con, "rb") :
  cannot open URL 'https://query2.finance.yahoo.com/v7/finance/options/^SPX': HTTP status was '401 Unauthorized'

I tried different symbols and none of them worked. I also checked that the getQuote still works, so the issue is contained to accessing the options chains from Yahoo Finance.

lynncullotta commented 8 months ago

Ran into similar issue. My Mboum options data feed simultaneously broke so I wonder if the problem isn't further down the line?

Edit: I am also using the yfinance package in Python, not this package

ethanbsmith commented 8 months ago

looks like yahoo is using a crumb in the call. possibly related to what we saw w/ getQuote and GDPR. may be able to reuse some of the work we did on that

for reference:

https://query1.finance.yahoo.com/v7/finance/options/SPY?date=1706832000&formatted=true&straddle=false&lang=en-US&region=US&crumb=qnrVMPmwwBR

joshuaulrich commented 8 months ago

looks like yahoo is using a crumb in the call

That's it! It works when I add the crumb to the URL. I'll work on getting a patch pushed.

joshuaulrich commented 8 months ago

This should be fixed now. Please install and test with remotes::install_github("joshuaulrich/quantmod@407-getoptionchain")

cotyreh commented 8 months ago

This should be fixed now. Please install and test with remotes::install_github("joshuaulrich/quantmod@407-getoptionchain")

It works!

`

SPX <-getOptionChain("^SPX") print(SPX) $calls ContractID ConractSize Currency Expiration Strike Last Chg ChgPct Bid Ask Vol 1 SPXW240129C01200000 REGULAR USD 2024-01-29 1200 3594.25 0.0000000 0.00000000 3719.3 3737.7 2 2 SPXW240129C01800000 REGULAR USD 2024-01-29 1800 3088.40 0.0000000 0.00000000 3119.3 3137.7 1 3 SPXW240129C02000000 REGULAR USD 2024-01-29 2000 2888.70 0.0000000 0.00000000 2919.3 2937.7 9 4 SPXW240129C02600000 REGULAR USD 2024-01-29 2600 2288.52 0.0000000 0.00000000 2319.3 2337.7 5 5 SPXW240129C03600000 REGULAR USD 2024-01-29 3600 1181.60 0.0000000 0.00000000 1319.3 1337.7 2`

joshuaulrich commented 8 months ago

Thanks for verifying the fix!

avhz commented 8 months ago

I'm still running into this issue after installing from GitHub.

steversteves commented 8 months ago

I'm still running into this issue after installing from GitHub.

Still having the error as well, any other suggestions? Is quantmod going to be patched? I know Python had the same issue but yf has been since patched but it seems quantmod has not.

joshuaulrich commented 8 months ago

@avhz and @steversteves are you in GDPR countries?

@steversteves are you able to use yfinance to import options from yahoo? You say it had the same issue but has since been patched... but yfinance was last updated on Jan-21 and this issue began on Jan-26, 5 days after the last yfinance update.

avhz commented 8 months ago

@joshuaulrich No, Australia.

steversteves commented 8 months ago

@avhz and @steversteves are you in GDPR countries?

@steversteves are you able to use yfinance to import options from yahoo? You say it had the same issue but has since been patched... but yfinance was last updated on Jan-21 and this issue began on Jan-26, 5 days after the last yfinance update.

Thanks for getting back! No I am in Canada, I am not a Python user personally, it was just reported to me by a Python user that there was also an issue that they had to update the yf library.

I can't import options no, I get the crumb error, I am completely blocked out of anything option related. I did run your patch it just didn't fix the issue for me and I did try to update quantmod, but again it didn't fix. I'm also a very noob coder so its probably me! :-(

joshuaulrich commented 8 months ago

@avhz can you try https://github.com/ranaroussi/yfinance and let me know if it works for you?

justinjtownsend commented 7 months ago

Just looking at this (from the UK) and it's this check NROW(curl::handle_cookies(ses$h)) > 1) which for me fails the test for ses$can.crumb.

If I add a test crumb to the v7 API URL endpoint for options ( e.g. https://query2.finance.yahoo.com/v7/finance/options/%5ESPX?crumb=YdJC5tMaYMS ) then the dataset comes back successfully.