Yahoo Finance recently discontinued their (largely undocumented) historical stock quote API.
Previously, you get simply send a HTTP:-Get request like this…
…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
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
Grab the crumb from the response
crumbValue := res[i+22..i+32]
crumbValue := "btW01FWTBn3"
Store the cookie from the response
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.
p1 := 1497709183:
p2 := 1500301183:
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.