Exploring the yahoofinancials Python library
The yahoofinancials Python library provides free access to the market data available from Yahoo Finance, whose provider is ICE Data Services.
It provides access to historical and, for most assets, also real-time pricing data for the following:
- Currencies
- Indexes
- Stocks
- Commodities
- ETFs
- Mutual funds
- US Treasuries
- Cryptocurrencies
pip install yahoofinancials
from yahoofinancials import YahooFinancials
Single-ticker retrieval
aapl = yf.Ticker("AAPL")
hist = aapl.get_historical_price_data('2020-01-01', '2020-12-31', 'daily')
hist = aapl.get_historical_price_data('2020-01-01', '2020-12-31', 'weekly')
hist = aapl.get_historical_price_data('2020-01-01', '2020-12-31', 'monthly'
Convert the nested JSON to a pandas’ DataFrame
import pandas as pd
hist_df = pd.DataFrame(hist['AAPL']['prices'].drop('date', axis=1).set_index('fromatted_date')
print(hist_df)
Real-time data retrieval
Get real-time stock price data:
print(aapl.get_stock_price_data())
Real-time data for free data sources is usually delayed by 10 to 30 minutes.
Retrieving a financial statement
statements = aapl.get_financial_stmts('quarterly', ['income', 'cash', 'balance'])
print(statements)
Summary data retrieval
print(aapl.get_summary_data())
Multiple-tickers retrieval
Multiple-tickers retrieval, also known as a bulk retrieval, is far more efficient and faster than single-ticker retrieval since most of the time associated with each download request is spent on establishing and closing the network connection.
Historical data retrieval
currencies = YahooFinancials(['EURCHF=X', 'USDEUR=X', 'GBPUSD=X'])
print(currencies.get_historical_price_data('2020-01-01', '2020-12-31', 'weekly'))
List of methods:
- get_200day_moving_avg()
- get_50day_moving_avg()
- get_annual_avg_div_rate()
- get_annual_avg_div_yield()
- get_beta()
- get_book_value()
- get_cost_of_revenue()
- get_currency()
- get_current_change()
- get_current_percent_change()
- get_current_price()
- get_current_volume()
- get_daily_dividend_data(start_date, end_date)
- get_daily_high()
- get_daily_low()
- get_dividend_rate()
- get_dividend_yield()
- get_earnings_per_share()
- get_ebit()
- get_exdividend_date()
- get_financial_stmts(frequency, statement_type,
- reformat=True)
- get_five_yr_avg_div_yield()
- get_gross_profit()
- get_historical_price_data(start_date, end_date, time_
- interval)
- get_income_before_tax()
- get_income_tax_expense()
- get_interest_expense()
- get_key_statistics_data()
- get_market_cap()
- get_net_income()
- get_net_income_from_continuing_ops()
- get_num_shares_outstanding(price_type=’current’)
- get_open_price()
- get_operating_income()
- get_payout_ratio()
- get_pe_ratio()
- get_prev_close_price()
- get_price_to_sales()
- get_research_and_development()
- get_stock_earnings_data(reformat=True)
- get_stock_exchange()
- get_stock_price_data(reformat=True)
- get_stock_quote_type_data()
- get_summary_data(reformat=True)
- get_ten_day_avg_daily_volume()
- get_three_month_avg_daily_volume()
- get_total_operating_expense()
- get_total_revenue()
- get_yearly_high()
- get_yearly_low()
Exploring the pandas_datareader Python library
Some of the data sources supported are as follows:
- Yahoo Finance
- The Federal Reserve Bank of St Louis’ FRED
- IEX
- Quandl
- Kenneth French’s data library
- World Bank
- OECD
- Eurostat
- Econdb
- Nasdaq Trader symbol definitions
pip install pandas-datareader
Set up the basic data retrieval parameters:
from pandas_datareader import data
start_date = '2010-01-01'
end_date = '2020-12-31'
Access to Yahoo Finance
aapl = data.DataReader('AAPL', 'yahoo', start_date, end_date)
Access to EconDB
oilprodus = data.DataReader('ticker=OILPRODUS', 'econdb', start_date, end_date)
Access to the Federal Reserve Bank of St Louis’ FRED
import pandas as pd
pd.set_option('display.max_rows', 2)
gdp = data.DataReader('GDP', 'fred', start_date, end_date)
Cashing queries
One of the key advantages of the library is its implementation of caching the results of queries, thereby saving bandwidth, speeding up code execution, and preventing the banning of IPs due to the overuse of APIs.
import datetime
import requests_cache
session = requests_cache.CacheSession(cache_name='cache', backend='sqlite', expire_after=datetime.timedelta(days=7))
aapl_full_history = data.DataReader("AAPL", 'yahoo', datetime.datetime(1980, 1, 1), datetime.datetime(2020, 12, 31), session=session)
Access jest one data point:
aapl_full_history.loc['2013-01-07']
Exploring the Quandl data source
Quandl is one of the largest repositories of economic/financial data on the internet. Its data sources can be accessed free of charge. It also offers premium data sources, for which there is a charge.
pip install quandl
import quandl
quandl.ApiConfig.api_key = 'XXXXXXXXXX'
Download the Monthly average consumer prices in metropolitan France – Apple (1 KG); EUR data
papple = quandl.get('ODA/PAPPLE_USD')
Download Apple’s fundamental data:
aapl_fundamental_data = quandl.get_table('ZACKS/FC', ticker='AAPL')
Exploring the IEX Cloud data source
IEX Cloud is one of the commercial offerings. It offers a plan for individuals at USD 9 per month. It also offers a free plan, with a limit of 50,000 API calls per month.
pip install iexfinance
from iexfinance.refdata import get_symbols
get_symbols(output_format='pandas', token="XXXXXXXXXX")
Obtain Apple’s balance sheet
from iexfinance.stocks import Stock
aapl = Stock("aapl", token="XXXXXXXXX")
aapl.get_balance_sheet()
Get the current price
aapl.get_price()
Get the sector performance report
from iexfinance.stocks import get_sector_performance
get_sector_performance(output_format='pandas', token=token)
Get historical market data for Apple
from iexfinance.stocks import get_historical_data
get_historical_data("AAPL", start="20190101", end="20200101", output_format='pandas', token=token)
Exploring the MarketStack data source
MarketStack offers an extensive database of real-time, intra-day, and historical market data across major global stock exchanges. It offers free access for up to 1,000 monthly API requests.
While there is no official MarketStack Python library, the REST JSON API provides comfortable access to all its data in Python.
Download the adjusted close data for Apple
import requests
params = {
'access_key': 'XXXXX'
}
api_result = requests.get('http://api.marketstack.com/v1/tickers/aapl/eod', params)
api_response = api_result.json()
print(f"Symbol = {api_response['data']['symbol']}")
for eod in api_response['data']['eod']:
print(f"{eod['date']}:{eod['adj_close']}")
Download all tickers on the Nasdaq stock exchange:
api_result = requests.get('http://api.marketstack.com/v1/exchanges/XNAX/tickers', params)
api_response = api_result.json()
print(f"Exchange Name = {api_response['data']['name']}")
for ticker in api_response['data']['tickers']:
print(f"{ticker['name']}:{ticker['symbol']}")









