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

Intermittent timeout issues with new getSymbols.yahoo as well as getSplits and getDividends #166

Closed
the-tourist- opened this issue Jun 3, 2017 · 17 comments
Assignees
Labels

Comments

@the-tourist-
Copy link

the-tourist- commented Jun 3, 2017

Description

I am intermittently getting what appear to be timeout errors with getSymbols, getSplits and getDividends using yahoo as the source. Most of the time these work but moderately often I get

Error in new.session(curl::new_handle()) :
Could not establish session after 5 attempts.

Expected behavior

The results of the calls

Minimal, reproducible example

getSymbols("MSFT", src = "yahoo")
getSplits("MSFT")
getDividends("MSFT")

Session Info

R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server >= 2012 x64 (build 9200)

Matrix products: default

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

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

other attached packages:
[1] quantmod_0.4-9 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

@bennysiu
Copy link

bennysiu commented Jun 6, 2017

I also have this problem, i need to insert Sys.sleep(60) between each getXXXXX.

@joshuaulrich
Copy link
Owner

I've noticed similar intermittent behavior at the beginning of a clean R session, but calling getSymbols a second time usually works. And subsequent calls to getSymbols in the same session also work.

I've run @the-tourist-'s example a few times in a new R session, but it works for me...

Can someone please provide a reproducible example that produces the behavior somewhat frequently? I know this is an intermittent issue, but I need code I can run to reliably reproduce the issue in order to investigate.

Please provide as much detail as possible. Does it happen at the start of a new/clean R session? Or after the R session has been running for awhile? Does it stop working after prior calls to getSymbols worked? Et cetera.

@bennysiu
Copy link

bennysiu commented Jun 7, 2017

Hi Joshua,

I run below script in command prompt, it will no problem for first few run, but after it, it will show
"Could not establish session after 5 attempts."

####test.r
.libPaths("C:/Users/goI/Documents/R/win-library/3.4")
library(quantmod)
library(TTR)
args <- commandArgs(TRUE)
stockNumber <- args[1]
fromDate <- args[2]
toDate <- args[3]
stock = getSymbols(stockNumber, auto.assign=FALSE ,from=fromDate , to=toDate,src="yahoo")
print(Cl(stock))
###Command Prompt
c:\>.\Rscript.exe" test.r MSFT 2010-01-01 2017-06-01

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
Loading required package: methods
Version 0.4-0 included new data defaults. See ?getSymbols.
'getSymbols' currently uses auto.assign=TRUE by default, but will
use auto.assign=FALSE in 0.5-0. You will still be able to use
'loadSymbols' to automatically load data. getOption("getSymbols.env")
and getOption("getSymbols.auto.assign") will still be checked for
alternate defaults.

This message is shown once per session and may be disabled by setting
options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.

WARNING: There have been significant changes to Yahoo Finance data.
Please see the Warning section of '?getSymbols.yahoo' for details.

This message is shown once per session and may be disabled by setting
options("getSymbols.yahoo.warning"=FALSE).
Error in new.session(curl::new_handle()) :
  Could not establish session after 5 attempts.
Calls: getSymbols ... do.call -> getSymbols.yahoo -> .getHandle -> new.session
Execution halted

@bennysiu
Copy link

bennysiu commented Jun 7, 2017

i run below code using rscript.exe in command prompt with 5 times, it will have "Could not establish session after 5 attempts." for 3 times.

####Scripts
test.r
h <- (curl::new_handle())
tmp <- tempfile()

for (i in 1:5) {
  curl::curl_download("https://finance.yahoo.com", tmp, handle = h)
	   
  if (NROW(curl::handle_cookies(h)) > 0)
	curl::handle_cookies(h)
    break;
  }

  if (NROW(curl::handle_cookies(h)) == 0)
      stop("Could not establish session after 5 attempts.")
####Scripts	  
####Result
Rscript.exe test.r
C:\Users\goI>"c:\Program Files\R\R-3.4.0\bin\x64\Rscript.exe" test.r

C:\Users\goI>"c:\Program Files\R\R-3.4.0\bin\x64\Rscript.exe" test.r

C:\Users\goI>"c:\Program Files\R\R-3.4.0\bin\x64\Rscript.exe" test.r
Error: Could not establish session after 5 attempts.
Execution halted

C:\Users\goI>"c:\Program Files\R\R-3.4.0\bin\x64\Rscript.exe" test.r
Error: Could not establish session after 5 attempts.
Execution halted

C:\Users\goI>"c:\Program Files\R\R-3.4.0\bin\x64\Rscript.exe" test.r
Error: Could not establish session after 5 attempts.
Execution halted
####Result

@joshuaulrich
Copy link
Owner

joshuaulrich commented Jun 13, 2017

@bennysiu Thanks for the examples! I see some failed attempts (i.e. the value of i printed to the console) if I run the following command:

while ./curl_cookies.R; do :; done

Where curl_cookies.R contains:

#!/usr/bin/Rscript
.libPaths("~/R/library")
tmp <- tempfile()
on.exit(unlink(tmp))

h <- curl::new_handle()

for (i in 1:5) {
  curl::curl_download("https://finance.yahoo.com", tmp, handle = h)
  n <- NROW(curl::handle_cookies(h))
  if (n > 0)
    break
  cat(i, "\n")
}

if (n == 0)
  quit("no", status = 1)

That said, I've let that command run for ~5 minutes and didn't encounter an instance where it failed 5 times and therefore exited with status = 1. Maybe it is related to your physical location? Also, what version of the curl package are you using?


Here's a version that actually calls getSymbols, in an attempt to replicate the error. As with the curl example, I wasn't able to trigger the "Could not establish session after 5 attempts." error.

#!/usr/bin/Rscript
.libPaths("~/R/library")
suppressMessages(require(quantmod))
options("getSymbols.yahoo.warning"=FALSE)
options("getSymbols.warning4.0"=FALSE)

x <- try(getSymbols("SPY;EEM;AGG;IWM"))
if (inherits(x, "try-error")) {
  quit("no", status = 1)
} else {
  quit("no", status = 0)
}

@the-tourist-
Copy link
Author

Joshua,

The scripts I am using are running on a server in France with 10mbs up and down stream bandwidths. Doing a ping of finance.yahoo.com takes about 30ms when I tried it just now.

I am working around the issue by using tryCatch and repeated calls with a small delay until it works, but maybe this could be moved into the quantmod code instead?

joshuaulrich added a commit that referenced this issue Jun 13, 2017
Some users are still experiencing intermittent issues importing data
from Yahoo Finance. Add 100ms between each connection attempt, and
also use a new handle.

See #166.
@joshuaulrich
Copy link
Owner

@the-tourist- getSymbols.yahoo already re-attempts the connection... but I just added some code that waits 100ms between attempts. It would be great if you could test it. The changes are on the 166_getSymbols_timeout branch.

@the-tourist-
Copy link
Author

Hi, I just retried the alternate version, but still received the same message.

@joshuaulrich
Copy link
Owner

Thanks for the feedback!

joshuaulrich added a commit that referenced this issue Jun 17, 2017
Trying to fetch the Yahoo Finance! homepage multiple times can result
in getting a cached response from a proxy. The cached response does
not have a "Set-cookie" header, so the curl handle will still not have
a cookie and the connection will be retried.

This StackOverflow Q/A has two approaches to avoid a cache hit:
https://stackoverflow.com/q/31653271/271616
1) Add a "Cache-control: no-cache" header
2) Add a random query to the URL.

The first approach didn't work because it appears the Yahoo proxies
simply ignore the request. The second approach did work though.

Fixes #166.
@joshuaulrich
Copy link
Owner

@the-tourist-, @bennysiu I just pushed a commit that seems to fix this issue. Please test and let me know if it works for you too.

@the-tourist-
Copy link
Author

Is it committed to the master or to the 166_getSymbols_timeout branch?

@joshuaulrich
Copy link
Owner

It's only on the 166_getSymbols_timeout branch right now.

@the-tourist-
Copy link
Author

@joshuaulrich it would appear to be working. I tried calling a simple getSymbols script from the command line and after calling it 10 times I have not encountered the curl session error.

Thanks.

@joshuaulrich
Copy link
Owner

Awesome, thanks for the feedback!

@joshuaulrich joshuaulrich added this to the Release 0.4-10 milestone Jun 19, 2017
@joshuaulrich joshuaulrich self-assigned this Jun 19, 2017
joshuaulrich added a commit that referenced this issue Jun 19, 2017
Some users are still experiencing intermittent issues importing data
from Yahoo Finance. Add 100ms between each connection attempt, and
also use a new handle.

See #166.
@bennysiu
Copy link

it works great for me too. Thanks joshuaulrich.

@fededemo
Copy link

Hi!
today I'm having a similar issue with quantmod::getSymbols('EEM', from = (Sys.Date()-10)).
I'm getting: Error in new.session() : Could not establish session after 5 attempts

more related problems like this in the last 2 days:

Thanks for your help!

@joshuaulrich
Copy link
Owner

@fededemo see #358.

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

4 participants