Yahoo Finance recently discontinued their (largely undocumented) historical stock quote API.

Previously, you simply send a HTTP:-Get request like this…

HTTP:-Get(“http://ichart.yahoo.com/table.csv?s=AAPL&a=00&b=1&c=2016&d=00&e=1&f=2017&g=d&ignore=.csv")

…and get historical OHLCV (open, high, low, close, trading volume) data in your worksheet (in this case for AAPL between 1 January 2016 and 1 January 2017).

This no longer works! Yahoo shut the door on this easy-to-use and widely disseminated API.

You can still download historical stock quotes from Yahoo Finance into Maple, but the process is now somewhat more involved. My complete code in this worksheet but I'll step through the process below.

If you visit the updated Yahoo Finance website and download historical data for a ticker, you see a URL like this in the status bar of your browser

https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1497727945&period2=1500319945&interval=1d&events=history&crumb=C9luNcNjVkK

Let's examine how ths URL is constructed.

  • period1 and period2 are Unix time stamps for your start and end date
  • interval is the data retrieval interval (this can be either 1d, 1w or 1m)
  • crumb is an alphanumeric code that’s periodically regenerated every time you download new historical data from from the Yahoo Finance website using your browser. Moreover, crumb is paired with a cookie that’s stored by your browser.

Here’s how to extract and supply the cookie-crumb pair to Yahoo Finance so you can still use Maple to retrieve historical stock quotes

Send a dummy request to get a cookie-crumb pair

res:=HTTP:-Get("https://finance.yahoo.com/lookup?s=bananas"):

Grab the crumb from the response

i:=StringTools:-Search("CrumbStore\":{\"crumb\":\"",res[2]):
crumbValue := res[2][i+22..i+32]
                  crumbValue := "btW01FWTBn3"

Store the cookie from the response

cookieHeader:=res[3]["Set-Cookie"]
    cookieHeader := "B=702eqhdcmq7cl&b=3&s=0t; expires=Mon,17-Jul-2018 20:27:01 GMT; path=/; domain=.yahoo.com

Construct the URL

  • Your desired start and end dates have to be defined as Unix time stamps. Converting a human readable date (like 1st January 2017) to a Unix timestamp is simple, so I won't cover it here.
  • The previously retrieved crumb has to be added to the URL.
ticker:="AAPL":
p1 := 1497709183:
p2 := 1500301183:
url:=cat("https://query1.finance.yahoo.com/v7/finance/download/",ticker,"?period1=",p1,"&period2=",p2,"&interval=1d&events=history&crumb=", crumbValue):

Send the request to Yahoo Finance, including the cookie in the header

data:=HTTP:-Get(url,headers = ["Cookie" = cookieHeader])

Your historical data is now returned

The historical data is now easily parsed into a matrix.

Please note that any use of Yahoo Finance has to be consistent with their terms of service.


Please Wait...