YaWhore Dance with Yahoo Finance

On 17.04.2017 Yahoo.Finance changed its API, so ichart.finance.yahoo.com is (temporarily?!) unavailable. In particular it means that many R-scripts that rely on quantmod/getSymbols() will not function anymore. We discuss the ways to circumvent the API change of Yahoo.Finance and alternatives to it, esp. Alpha Vantage.
YaWhore_Yahoo_Finance

Our feelings about the abrupt change of the Yahoo Finance API are twofold. On one hand Yahoo's contribution to retail investors community was enormous and it was free of charge: so never look a gift horse in the mouse. On the other hand, Timeo Danaos et dona ferentes and for many of us this turned out to be a Trojan Horse, really. Since Yahoo missed the opportunity to buy Google it seems to be a perfect [anti]pattern how a company can blunder. Anyway, complaining brings nothing so let us discuss what we can do.

First of all you can still access Yahoo Finance manually via your browser and download history as .csv file. Thus if you just occasionally analyze the historical quotes in Excel, you likely will not even notice the change.
But if you need an automatic download, you are in trouble. Though the URL structure did not change much and the parameter structure in QUERY_STRING is easy recognizable, you cannot just change the URL.

Yahoo.Finance server response

{
"finance": {
"error": {
"code": "Unauthorized",
"description": "Invalid cookie"
}
}
}

[collapse]

You need to set cookies, which requires some programming skills, in particular the knowledge of http and/or sockets. Fortunately, there are already some solutions. If you can code in C#, have a look at this. I did not test it myself but a friend of mine reports it does work. And here is a sketch how to do it in PHP. We also hope that the author of quantmod library will soon fix the problem, too (actually they already work on it).

Anyway, we have to be prepared to the worst, i.e. that Yahoo might either completely shut down the service or require to enter a captcha. Thus it is worth looking at alternatives. One of them is Quandl. It provides a lot of databases, both free and for reasonable fee. There are also APIs for  Python, R and Matlab. For instance, in order to get historical quotes for Apple Stock register by Quandl, get your API key and use

library(Quandl)
Quandl("EOD/AAPL", api_key="<your API key here>")

But there is probably an even better alternative: Alpha Vantage. By their own account: "We have one shared aspiration: democratizing access to institution-grade financial analytics. Alpha Vantage is one of our deliverables towards this purpose. It provides free JSON APIs for stock market data, augmented by a comprehensive set of technical indicators. We are committed to making it free forever."
Indeed, everything what happens, happens for good: if Yahoo hadn't changed its API, I would likely not encounter Alpha Vantage. It seems to be a young project and e.g. in their documentation there is, so far, even no a list of tickers, for which the data are available. But they provide for free the intraday data upto 1 min granularity! (Indeed "free" nerves me a little bit, I would prefer to pay some reasonable monthly fee and be sure that they will not shut down due to lack of money).
Unfortunately, there is yet no API for R. The data come in JSON format, which I am not quite familiar with (if the JavaScript is the programming language of the future then my next job is barista). However, I found a (probably dirty but quick) way to parse the data in R

library(jsonlite)
#change demo to your API-Key, you can get it for free
dat <- fromJSON("http://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=AAPL&interval=1min&apikey=demo")
nms = names(dat$"Time Series (1min)")
N = length(nms)
dates = as.POSIXlt(nms)
OHLCV = array(0.0, dim=c(N,5))
for(n in 1:N)
{
  OHLCV[n, 1] = as.double((dat$"Time Series (1min)")[[n]]$`1. open`)
  OHLCV[n, 2] = as.double((dat$"Time Series (1min)")[[n]]$`2. high`) 
  OHLCV[n, 3] = as.double((dat$"Time Series (1min)")[[n]]$`3. low`)
  OHLCV[n, 4] = as.double((dat$"Time Series (1min)")[[n]]$`4. close`)
  OHLCV[n, 5] = as.double((dat$"Time Series (1min)")[[n]]$`5. volume`)
}
ts.plot(OHLCV[, 1:4])

Do you know any other alternatives to Yahoo Finance? Your comments and suggestions are very welcome!


UPDATE 15.10.2017
Paul Teetor has implemented support of AlphaVantage in R-package quantmod

Unfortunately, Alpha Vantage has recently done much the same what Yahoo Finance did before: without an announcement they depreciated their Global-API (i.e. API to get stocks from FRA, EPA and other European stock exchanges). American stocks are still functioning.

Like this post and wanna learn more? Have a look at Knowledge rather than Hope: A Book for Retail Investors and Mathematical Finance Students

Support letYourMoneyGrow.com for a good work

5 thoughts on “YaWhore Dance with Yahoo Finance”

  1. I agree that Alpha Vantage looks very interesting. But for those who need a quick fix to their VBA scrips for Excel, there is a way to emulate the behavior of the old ichart URL at http://www.signalsolver.com/EmulateURL . This will give daily, weekly and monthly prices along with adjusted closes, dividends and splits. Its free for as long as it works. Like you say, Yahoo could easily make it harder at any time.
    Andrew MacLean

Leave a Reply

Your email address will not be published. Required fields are marked *