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

Yahoo Finance returns 502 Bad Gateway #157

Closed
joshuaulrich opened this issue May 16, 2017 · 70 comments
Closed

Yahoo Finance returns 502 Bad Gateway #157

joshuaulrich opened this issue May 16, 2017 · 70 comments
Assignees
Labels
Milestone

Comments

@joshuaulrich
Copy link
Owner

After switching from HTTP to HTTPS (#149), it looks like Yahoo has now changed the URL.

R> getSymbols("AAPL", src="yahoo")
Error in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,  : 
  cannot open URL 'https://ichart.finance.yahoo.com/table.csv?s=AAPL&a=0&b=01&c=2007&d=4&e=16&f=2017&g=d&q=q&y=0&z=AAPL&x=.csv'
In addition: Warning message:
In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,  :
  cannot open URL 'https://ichart.finance.yahoo.com/table.csv?s=AAPL&a=0&b=01&c=2007&d=4&e=16&f=2017&g=d&q=q&y=0&z=AAPL&x=.csv': HTTP status was '502 Bad Gateway'

The "Download Data" link on the historical data page for AAPL is now:
https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1492379145&period2=1494971145&interval=1d&events=history&crumb=sgdBTt48gg.

@arbitamrazian
Copy link

I also noticed this bug today, but how do we deal with the crumb parameter? It's required since after https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1492379145&period2=1494971145&interval=1d&events=history (i.e. without the crumb param) I get
{ "finance": { "error": { "code": "Unauthorized", "description": "Invalid cookie" } } }

Any ideas here?

@joshuaulrich
Copy link
Owner Author

You need to establish a session with the Yahoo Finance server in order to get the crumb. That will probably require using curl (via either the RCurl or curl R package).

@machuck
Copy link

machuck commented May 17, 2017

Can someone post an example of how to use RCurl + getSymbols etc?

joshuaulrich added a commit that referenced this issue May 17, 2017
Yahoo changed their URL scheme to require an established session with
cookies. The 'curl' package handles all the details. First establish a
connection to the main site to get a cookie, then use that to get a
crumb, and use the crumb in the download URL. Note that times also
changed to POSIX/UNIX timestamps.

No additional changes were needed for getDividends(). getSplits() was
updated to read the new CSV format and parse the new split format.

getSymbols.yahoo() needed to re-order the columns, because Volume and
Adj.Close were swapped. It also appears the Open, High, and Low columns
are already adjusted for splits, but not dividends. Un-adjust them for
splits so the result is consistent with prior behavior.

See #157.
joshuaulrich added a commit that referenced this issue May 17, 2017
Keep a reference to the curl handle in quantmod's private environment.
This avoids having to re-establish a connection for every query.

The .getHandle() function will establish a session the first time it's
called. Then it will simply return the handle for subsequent calls.

Note that the curl package author recommends to not re-use curl
handles. I think it's okay in this case because we're always referencing
the same site. If we need to use curl for multiple sites, we should
establish handles for each, individually.

See #157.
joshuaulrich added a commit that referenced this issue May 17, 2017
Setting from="1900-01-01" resulted in a bad request. Pre-1950 should be
adequate for the majority of companies.

See #157.
@lompoc42
Copy link

I'm sorry, but I'm confused as to how to use the updates. How do I update the functions with the changes you just made?

@joshuaulrich
Copy link
Owner Author

@lompoc42 one of these:

remotes::install_github("joshuaulrich/quantmod", ref="157_yahoo_502")
# or
devtools::install_github("joshuaulrich/quantmod", ref="157_yahoo_502")

Or wait until it's merged to master and use:

remotes::install_github("joshuaulrich/quantmod")
# or
devtools::install_github("joshuaulrich/quantmod")

@lompoc42
Copy link

THANK YOU SO MUCH!!!!!!!!!!!

@lompoc42
Copy link

lompoc42 commented May 17, 2017

Getting an error with the update:

> getSymbols('^GSPC',env = stock)
Error in `/.default`(fr[, 4], fr[, 6]) : 
  non-numeric argument to binary operator

> getSymbols.yahoo('^GSPC',env=stock)
Error in `/.default`(fr[, 4], fr[, 6]) : 
  non-numeric argument to binary operator

@joshuaulrich
Copy link
Owner Author

@lompoc42 Please provide the command that causes that error. It's nearly impossible to help without it.

@lompoc42
Copy link

In RStudio

>stock = new.env()
>getSymbols.yahoo(Symbols = '^GSPC',env = stock)

@yankodev
Copy link

Hey Joshua, thanks for giving us quantmod, and thanks for the quick fix.

After installing the update I can download FB and APPL historical prices with, e.g., loadSymbols("FB").

I get the same error as lompoc42 when trying ^GSPC, ^GDAXI, SIE.DE, or LQD. It seems the query string varies with the ticker (just a shot in the dark, no time to check).

Cheers,
Yanko

@lompoc42
Copy link

lompoc42 commented May 17, 2017

Yeah, it works for some but not for others.

Got the same error also with tickers: PRWCX, VBTLX

joshuaulrich added a commit that referenced this issue May 17, 2017
The downloaded file may have "null" for some observations. Use the
na.strings argument to convert them to NA.

See #157.
@joshuaulrich
Copy link
Owner Author

The issue with the symbols listed should now be resolved. Thanks for the testing and feedback!

@lompoc42
Copy link

lompoc42 commented May 17, 2017

The '502 Bad Gateway' error is back. I didn't change anything which was working before and now no longer working.

I tried re-installing the package you referenced earlier

>devtools::install_github("joshuaulrich/quantmod", ref="157_yahoo_502")

and am getting the following error at the end

Error in dev_meta(pkg$package) : Namespace not found for quantmod. Is it loaded?

@lompoc42
Copy link

Not sure what's happened, but it's not working anymore

@bdfden
Copy link

bdfden commented May 17, 2017

It is now working for me. Thank you for your help. However, it looks like it is only returning data going back to 2014. Also, the finance.yahoo.com site seems to be having issues returning older data.

@joshuaulrich
Copy link
Owner Author

Not sure what's happened, but it's not working anymore

@lompoc42 it works fine for me from a fresh install on another machine. I assume you're running from RStudio; have you tried restarting RStudio? Unload/reload doesn't always work well. If that doesn't work, see if it works if you run from the GUI that comes with R and/or the terminal.

...it looks like it is only returning data going back to 2014. Also, the finance.yahoo.com site seems to be having issues returning older data.

@bdfden I just pulled data from 2007. If you're only getting data back to 2014 for a specific symbol, and you get the same behavior for the same instrument on the website, then there's no issue with quantmod.

@bdfden
Copy link

bdfden commented May 17, 2017

Agreed - this issue seems to be with Yahoo. Thanks again.

@wave-electron
Copy link

@joshuaulrich

Just want to thank you for the great work on the quantmod package. I spent a few weeks developing a shiny app that could mimic yahoo charts in April 2017...my goal was to showcase what is now possible using the shiny framework & R's immense library of packages. I'm using the highcharts R package which I also highly recommend for charting.

Here is a screen shot.

screen shot 2017-05-18 at 9 37 03 am

When I noticed the 502 Bad Gateway yesterday I feared the worse. But I've implemented your update and the app is working as per usual.

So thanks very much for all your great work!

@ellisvalentiner
Copy link

ellisvalentiner commented May 17, 2017 via email

@mcohen01
Copy link

mcohen01 commented May 18, 2017

if you happen to encounter the following error while installing under OS X....

> devtools::install_github("joshuaulrich/quantmod", ref="157_yahoo_502")
Downloading GitHub repo joshuaulrich/quantmod@157_yahoo_502
from URL https://api.github.com/repos/joshuaulrich/quantmod/zipball/157_yahoo_502
Installing quantmod
trying URL 'https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.3/zoo_1.8-0.tgz'
Content type 'application/x-gzip' length 887574 bytes (866 KB)
==================================================
downloaded 866 KB

Installing zoo
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file  \
  --no-environ --no-save --no-restore --quiet CMD INSTALL  \
  '/private/var/folders/64/7v2m4_bd57z9kt_dttsc79zhrhzc_h/T/RtmpRadWAr/devtools5bc1187d120b/zoo'  \
  --library='/Library/Frameworks/R.framework/Versions/3.3/Resources/library'  \
  --install-tests 

* installing *binary* package ‘zoo’ ...
* DONE (zoo)
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file  \
  --no-environ --no-save --no-restore --quiet CMD INSTALL  \
  '/private/var/folders/64/7v2m4_bd57z9kt_dttsc79zhrhzc_h/T/RtmpRadWAr/devtools5bc14faece28/joshuaulrich-quantmod-7e7744f'  \
  --library='/Library/Frameworks/R.framework/Versions/3.3/Resources/library'  \
  --install-tests 

* installing *source* package ‘quantmod’ ...
** libs
gfortran-4.8   -fPIC  -g -O2  -c ohlcq.f -o ohlcq.o
make: gfortran-4.8: No such file or directory
make: *** [ohlcq.o] Error 1
ERROR: compilation failed for package ‘quantmod’
* removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/quantmod’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/quantmod’
Error: Command failed (1)

the fix is to brew reinstall gcc48 --with-fortran and then add

FLIBS=""
F77="gfortran-4.8"
FC="gfortran-4.8"

to ~/.R/Makevars as described in http://stackoverflow.com/questions/23916219/os-x-package-installation-depends-on-gfortran-4-8

@joshuaulrich
Copy link
Owner Author

@mcohen01 See the second-to-last bullet in the Installation section of quantmod's README:
https://github.com/joshuaulrich/quantmod#installation

@jzerp
Copy link

jzerp commented May 18, 2017

Thanks for Quantmod Joshua, I'm very new to R and I have been using Quantmod for a couple of months and its been very useful. However, I ran into the same problem other users have talked about. Just wanted to ask you if you are planning to release a new version or should I stick to the fixes you listed before. I tried to run them but I'm getting errors. Thanks again!

@richjung
Copy link

Thank you for your help. Should I make any change in Ad(get(symbol[i]]) to get prices from multiple tickers?

@Gabylin
Copy link

Gabylin commented May 18, 2017

Thank you so much, it works now after installed devtools provided by Joshua. I can check stock data as usual now. But it looks like yahoo finance server miss a lot of daily information in some TW STOCKs.

joshuaulrich added a commit that referenced this issue May 23, 2017
The attempt to establish a session could result in a curl handle that
does not have a cookie, in which case the user will not be able to
import data.

Establish a connection and check if it resulted in a cookie. Try again,
up to 5 times, if it does not. Throw an error if the attempt fails 5
times.

Hopefully most users will not receive errors because of this, combined
with the second attempt to import data if the first attempt fails,

See #157.
@mlb92
Copy link

mlb92 commented May 23, 2017

@joshuaulrich I have ran your updates

remotes::install_github("joshuaulrich/quantmod")
# or
devtools::install_github("joshuaulrich/quantmod")

and I am still getting the 502 Bad Gateway issue...is there anything you recommend (with an example) for how I can get it to work. Thank you.

@joshuaulrich
Copy link
Owner Author

@mlb92 You have not run the commands to install the patch. The commands you used will only work once the patch is merged, which it has not been. You need to run these commands:

remotes::install_github("joshuaulrich/quantmod", ref="157_yahoo_502")
# or
devtools::install_github("joshuaulrich/quantmod", ref="157_yahoo_502")

@joshuaulrich joshuaulrich added this to the 0.4-9 milestone May 23, 2017
@mlb92
Copy link

mlb92 commented May 23, 2017

@joshuaulrich I am very new to programming, could you explain what you mean by "You have not run the commands to install the patch."? I have run these lines so far. Thank you for your help:

install.packages("remotes")
install.packages("devtools")
library(remotes)
library(devtools)

After running these, I get the following errors

remotes::install_github("joshuaulrich/quantmod")
Downloading GitHub repo joshuaulrich/quantmod@master

  • installing source package ‘quantmod’ ...
    ** libs
    gfortran-4.8 -fPIC -g -O2 -c ohlcq.f -o ohlcq.o
    make: gfortran-4.8: No such file or directory
    make: *** [ohlcq.o] Error 1
    ERROR: compilation failed for package ‘quantmod’
  • removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/quantmod’
  • restoring previous ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/quantmod’
    Warning message:
    In utils::install.packages(...) :
    installation of package ‘/var/folders/41/p78vz8291wx4f9k6gpb2c_1c0000gn/T//RtmpK1HTpO/remotesd8e77ec9991/joshuaulrich-quantmod-9305034’ had non-zero exit status

devtools::install_github("joshuaulrich/quantmod")
Downloading GitHub repo joshuaulrich/quantmod@master
from URL https://api.github.com/repos/joshuaulrich/quantmod/zipball/master
Installing quantmod
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file --no-environ --no-save --no-restore
--quiet CMD INSTALL
'/private/var/folders/41/p78vz8291wx4f9k6gpb2c_1c0000gn/T/RtmpK1HTpO/devtoolsd8e1f379e94/joshuaulrich-quantmod-9305034'
--library='/Library/Frameworks/R.framework/Versions/3.3/Resources/library' --install-tests

  • installing source package ‘quantmod’ ...
    ** libs
    gfortran-4.8 -fPIC -g -O2 -c ohlcq.f -o ohlcq.o
    make: gfortran-4.8: No such file or directory
    make: *** [ohlcq.o] Error 1
    ERROR: compilation failed for package ‘quantmod’
  • removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/quantmod’
  • restoring previous ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/quantmod’
    Installation failed: Command failed (1)

@joshuaulrich
Copy link
Owner Author

@mlb92 Then you should just wait until this is fixed. I told you to run remotes::install_github("joshuaulrich/quantmod", ref="157_yahoo_502") and you ran remotes::install_github("joshuaulrich/quantmod"). You also need to make sure you have the necessary tools needed to install packages with compiled code on Mac (which I have mentioned in a few other comments in this thread).

@umeshnathani
Copy link

umeshnathani commented May 24, 2017

I ran devtools::install_github("joshuaulrich/quantmod", ref="157_yahoo_502") and ran getSymbols("AAPL"). I get the follwoing:

Error in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m, :
cannot open URL 'https://ichart.finance.yahoo.com/table.csv?s=AAPL&a=0&b=01&c=2007&d=4&e=24&f=2017&g=d&q=q&y=0&z=AAPL&x=.csv'
In addition: Warning message:
In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m, :
cannot open URL 'https://ichart.finance.yahoo.com/table.csv?s=AAPL&a=0&b=01&c=2007&d=4&e=24&f=2017&g=d&q=q&y=0&z=AAPL&x=.csv': HTTP status was '502 Connection refused'

I am using windows and latest version - R 3.4.0 (also got the same error previously with R 3.3.2)

Is yahoo! down? I tried opening https://chart.finance.yahoo.com/ and I get an error message from Yahoo! saying "Will be right back.."

@selgamal
Copy link

selgamal commented May 24, 2017

@umeshnathani I think you might need to restart your R session after installing the branch (157_yahoo_502) or just remove quantmod package you already have and then run:
devtools::install_github("joshuaulrich/quantmod", ref="157_yahoo_502")

I just tested AAPL and it worked fine, here are the results and session info:

>  library(quantmod)

> getSymbols("AAPL")

[1] "AAPL"

> head(AAPL)

           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2007-01-03     86.29     86.58    81.90   83.80000   309579900      11.97143
2007-01-04     84.05     85.95    83.82   85.66000   211815100      12.23714
2007-01-05     85.77     86.20    84.40   85.04999   208685400      12.15000
2007-01-08     85.96     86.53    85.28   85.47000   199276700      12.21000
2007-01-09     86.45     92.98    85.15   92.57000   837324600      13.22429
2007-01-10     94.75     97.80    93.45   97.00000   738220000      13.85714
> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] quantmod_0.4-8 TTR_0.23-1     xts_0.9-7      zoo_1.8-0     

loaded via a namespace (and not attached):
[1] compiler_3.4.0  tools_3.4.0     curl_2.6        grid_3.4.0      lattice_0.20-35

@walts
Copy link

walts commented May 25, 2017

@joshuaulrich The current build of getSymbols.yahoo on github is removing downloaded rows which have a date but only NA prices and volumes. An example is VDIGX for 2016-06-29. This seems to be a change from last week's version which didn't remove these rows. I had been using na.approx(..., maxgap = 2) to deal with a small number of rows missing in the download and that worked for my purposes. Could removing missing rows be made an option with a warning if rows were missing rather than the fixed behavior?

@joshuaulrich
Copy link
Owner Author

@walts That's a fair point. I made the change to address @ghost's comment. While you could use xts::na.restore() to put the missing values back, I think it makes more sense to return them with a warning and let the user deal with them.

joshuaulrich added a commit that referenced this issue May 26, 2017
Removing missing values assumes that's what the user prefers. The
object contains the na.omit attribute, so they could call na.restore()
to put the missing values back. However, that's more work and makes it
easier to ignore the fact that the data are suspect.

Update the warning to explain why the missing values might be a problem
and suggest possible solutions.

See #157.
joshuaulrich added a commit that referenced this issue May 26, 2017
This allows the .yahooURL() function to be used in getDividends and
getSplits(), which reduces duplicated code. Also use the .dateToUNIX()
function to calculate the from and to POSIX dates in both functions.

See #157.
joshuaulrich added a commit that referenced this issue May 26, 2017
Similar to the first call to getSymbols, the first call to
getSymbols.yahoo should throw a warning alerting users to the changes
in the Yahoo Finance data. Include more details in ?getSymbols.yahoo.

Also update the message printed after the first call to getSymbols().

See #157.
@joshuaulrich joshuaulrich self-assigned this May 26, 2017
@joshuaulrich
Copy link
Owner Author

The 157_yahoo_502 branch is now merged to master. There may still be some edge-case weirdness, but the main issues appear to be resolved, so I'm closing this as fixed.

Thanks for everyone's feedback, testing, and support!

@Lois47ed
Copy link

Hi Mr Joshualrich, I´m Lois.

Since past may 12 of 2017, WannaCry event, I can´t see this chart (Nike Inc. stok) in my browser:

http://ichart.finance.yahoo.com/z?s=NKE&t=6m&q=c&l=on&z=m&p=e50,e20&a=v,r14

Dear Joshualrich, ¿Is there any way to fix this problem?

You said that was a change of URL, this is, ¨https¨ instead of ¨http¨

Thanks for your time.

Bye for now

ceschi added a commit to ceschi/us_macro_data that referenced this issue May 30, 2017
Use temporary fix from 'quantmod' pkg, hopefully to be included by
default soon.

Ref: joshuaulrich/quantmod#157
joshuaulrich added a commit that referenced this issue Jun 13, 2017
Setting from="1949-01-01" in 05e87ea
worked because it avoided date-to-POSIX conversion error that was fixed
in 97e4df6. The issue is that the
POSIXct value for 1900-01-01 is just slightly less than the 32-bit
integer minimum.

Return the defaults to their original value, since this isn't a problem
for the Yahoo Finance servers.

See #157.
@Harrymon12
Copy link

Yahoo finance API is not available anymore. I have moved to MarketXLS after this change, much more reliable data.

@joshuaulrich
Copy link
Owner Author

@Harrymon12 It simply isn't true that the Yahoo Finance API is not available. I just ran this code:

R> quantmod::getSymbols("SPY", src = "yahoo")
[1] "SPY"
R> head(SPY)
           SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
2007-01-03  176.779  177.537 174.691    141.37   94807600     113.7571
2007-01-04  175.512  176.531 174.741    141.67   69620600     113.9985
2007-01-05  175.636  175.723 174.455    140.54   76645300     113.0893
2007-01-08  175.002  175.735 174.294    141.19   71655000     113.6122
2007-01-09  175.611  175.971 174.480    141.07   75680100     113.5157
2007-01-10  174.704  175.934 174.356    141.54   72428000     113.8939
R> packageVersion("quantmod")
[1] '0.4.10'

@Harrymon12
Copy link

@joshuaulrich Oh I see. It came back.

@chadworth
Copy link

It looks like same underlying data source is used for both the query1 and query2 APIs. E.g. the WBA issue that @lowcsharp mentioned (#157 (comment)) is an issue in both endpoints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests