I want to look into stock data.
According to stackoverflow ("alternative to google finance api"), financial information can be obtained through the Yahoo Finance API.
For instance, you can generate a CSV by a simple API call
# generate and save a CSV for AAPL, GOOG and MSFT
http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=sb2b3jk
or by using the webservice to return XML or JSON.
# All stock quotes in XML
http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote
# All stock quotes in JSON
http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json
In [1]:
url = "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json"
In [2]:
import requests
import json
response = requests.get(url)
# Load as JSON object
j = json.loads(response.content)
# Make tidy and print the first 3 entries
stock = [i['resource']['fields'] for i in j['list']['resources']]
stock[:3]
Out[2]:
In addition to making requests
by a url, there is a python package (yahoo-finance
) which provides functions to get stock data from Yahoo! Finance.
In [3]:
import yahoo_finance
yahoo = yahoo_finance.Share('YHOO')
print(yahoo.get_open())
print(yahoo.get_price())
print(yahoo.get_trade_datetime())
In [4]:
# Refresh
yahoo.refresh()
print(yahoo.get_price())
print(yahoo.get_trade_datetime())
Small issues
From the examples, there is a function to get_historical()
data. However, when I actually do run it, I get a YQLResponseMalformedError: Response malformed.
error. Googling the error message, The Financial Hacker (2017) comments:
The Yahoo Finance API is dead. Without prior announcement, Yahoo has abandoned their only remaining service that was clearly ahead of the competition.
The link to this issue is found here.
In [5]:
yahoo.get_historical('2014-04-25', '2014-04-29')
For now, not all functionality is lost.