Different Financial Data Sources: Fetching data
Hi All! Till now, we have mainly worked on fetching financial data from yahoo finance using pandas_datareader library. In this post, we will learn about other well-known different financial data sources from where we can get financial data and how to use them to fetch data in Python. In case you are new to this series, please go to part 1 of Financial Analytics series to learn the basics.
Different Financial Data Sources – The sources
The sources we will identify in this post are:
- Yahoo finance
- Quandl
- Intrinio
Different Financial Data Sources – Yahoo finance
Besides pandas_datareader library of Python, we can use yfinance and yahoofinancials library of Python to fetch the historical stocks data. Let’s learn how to use yfinance to fetch the historical stocks data of Microsoft Corporation.
1. Download and install the libraries
For doing this, go to Anaconda prompt and type pip install package_name
For installing yfinance, type pip install yfinance in Anaconda prompt.
2. Import the libraries
import pandas as pd
import yfinance as yf
3. Download the data
Here, we are going to use download function of yfinance library to fetch the data.
msftStockData = yf.download( 'MSFT',
start = '2010-01-01',
end = '2020-04-14',
progress = False)
4. Viewing and summarizing the data
msftStockData.head()
msftStockData.tail()
msftStockData.describe()
More points to know
- In the download function, we have to define the ticker (‘MSFT’ for Microsoft,’PG’ for The Procter and Gamble Company, ‘AAPL’ for Apple etc.) as the first argument.
- If we don’t define the start parameter (starting date), it tries to fetch all the data since 1950.
- List of multiple tickers can be passed as the first argument.
- If you want to download only the Adjusted price (Adj Close), use auto_adjust = True.
- progress parameter is for providing us with the progress bar.
- end denotes the end date.
Quandl
Quandl is another provider of historical financial data. In order to use Quandl via Python, first of all, we need to go to https://www.quandl.com/ and create our profile there. After that, we need to get the api key from our personal profile at https://www.quandl.com/account/profile in order to fetch data from Python.
Drawback of using Quandl – It contains all historical data till April 2018. You will not be able to find any data after April 2018.
1. Importing the libraries
import quandl
2. Key Authentication
quandl.ApiConfig.api_key = key
3. Download the data
msftQuandlData = quandl.get(dataset='WIKI/MSFT',
start_date='2010-01-01',
end_date='2020-04-14')
4. Viewing and summarizing the data
msftQuandlData.head()
msftQuandlData.tail()
msftQuandlData.describe()
More points to note
- You can define tickers in the form ‘WIKI/ticker_symbol’.
- You can pass the list of tickets in the form [‘WIKI/ticker1’, ‘WIKI/ticker2’]
- You can use collapse parameter to define if you want data on daily, weekly, monthly or yearly basis.
Intrinio
Intrinio is a paid database with some features coming under free access. From Intrinio, one can also get some pre-calculated KPIs such as Moving Averages etc. Like Quandl, one will have to make a profile on Intrinio to access the data using Python via API. Go to https://intrinio.com/ and sign up and copy the API key.
1. Import libraries
import intrinio_sdk
2. Key authentication
intrinio_sdk.ApiClient().configuration.api_key['api_key'] = 'key'
security_api = intrinio_sdk.SecurityApi()
3. Fetching the data
fetch = security_api.get_security_stock_prices(identifier='MSFT',
start_date='2010-01-01',
end_date='2020-04-14',
frequency='weekly',
page_size=10)
fetch
As can be seen above, this is a JSON object (dictionary) having 10 key: value pairs. Let’s convert this into a dataframe.
4. Converting fetched dictionary to dataframe
fetch_list = [f.to_dict() for f in fetch.stock_prices]
#Provides list of dictionaries for each week
fetch_list
#Converting each dictionary to a row of dataframe with each key as column and then sorting by date
msftIntrinio = pd.DataFrame(fetch_list).sort_values('date')
msftIntrinio.head()
#Renaming row indices to date in place of row numbers
msftIntrinio.set_index('date', inplace=True)
msftIntrinio.head()
So guys, In this tutorial, we learnt about different sources from where we can get historical financial data and the ways to access them via API. With this, I conclude this tutorial. In our next tutorial we will learn about Adjusted Returns including Inflation. Stay tuned for more such informative tutorials! Please don’t forget to subscribe our channel here to learn Power BI, DAX, Python and develop foundation for Business intelligence and data science.
One thought on “Different Financial Data Sources: FA6”