Financial Data with Finnhub in Python

Augustin Goudet
5 min readMar 31, 2020

--

Finnhub is a source of financial data. Out of all the places that offer a free version access, this is the most complete. Whether you are a data-driven fundamental investor, a crypto guru or a curious mind interested in gathering financial and economic data, this is the right place for you to start.

How can you access the Data?

Create an account on https://finnhub.io/ and retrieve a free API key. Begin by importing pandas, websocket and requests (for websocket, you may need to pip install websocket-client if you haven’t used it before).

I. Access to finnhub is made easy with a secure API.

A. Define Company and retrieve Api_Key (provided by Finnhub)

B. Define url to retrieve data — these will be provided in the API docummentation

C. Use request to pull the data into a jupyter notebook

— — — — — —

A. company = “SK.PA” / api_key = keys [‘finnhub’]
B. url = “https://finnhub.io/api/v1/stock/profile?symbol={}&token={}".format(company, api_key)
C. req = requests.get(url)

II. Data is returned in the following json format:

{'companyName: SEB SA, 'employeeTotal': '32690',
'exchange': 'NYSE EURONEXT - EURONEXT PARIS',
'ggroup': 'Consumer Durables & Apparel',
'gind': 'Household Durables',
'gsector': 'Consumer Discretionary',
'gsubind': 'Household Appliances',
'ipo': '1985-01-07',
'isin': 'FR0000121709',
'marketCapitalization': 5468.378}

III. Data can then be transformed into a DataFrame and manipulated

You can use from pandas.io.json import json_normalize for nested dictionaries

Data Frame Display

What type of data can you access?

I. Fundamental Company Data

Company specific data with profiling, board members, executive remuneration, performance metrics etc.

Starbucks CEO compensation and Board of Directors:

The data available can give insights into corporate governance of each company as well as insights into the members of the board and management team.

Starbucks Financial Performance Metrics and Stock Ownership:

Several company performance metrics are available. Finnhub divides the data into four sets: management, margin, growth and price.

Management will cover general efficiency metrics on revenue and assets. Margin will provide insights into profitability and cash-flow generation. Growth metrics show trends in revenue, eps and book value growth. Price covers price action and volume metrics.

Stock ownership metrics will provide you with the main institutional holders of the company’s stock based on the latest filings where applicable. Specific fund data is also available if you’re looking for a more granular set of data.

Sell-side research analyst opinions are also available. This includes consensus data on EPS, revenue, stock price and recommendation trends.

II. News & Economic Data

General and company news as well as country/regional economic data points

30/03/2020 news

Articles are classified into categories such as company news, business news, top news.

Latest news on Starbucks

Company-level news available with sources and a summary.

Economic Data

Many data points exist — GDP, unemployment, bankruptcies, balance of trade, balance of payments and many others. Each data field has a corresponding code. More than 8000 economic indicators are available across countries.

Code for the number of monthly bankruptcies in France: A-FRA-6682 

III. Market Data

Stock / FX market prices are available.

Closing / High / Low / Open / Previous CloseStarbucks{'c': 66.34, 'h': 68.25, 'l': 64.82, 'o': 66.68, 'pc': 69.9}Bitcoin vs USD{'c': 6266.27, 'h': 6330.01, 'l': 5857.76, 'o': 5880.5, 'pc': 5881.42}

Many other market data fields are also accessible, most notably relating to technical analysis which is not covered in this post.

IV. Alternative data

News sentiment is another feature provided. It measures the company’s ‘buzz’ by dividing the number of articles in the news in the past weeks vs the weekly average. It then measures investor sentiment on the company vs the sector it belongs to.

{'buzz': {'articlesInLastWeek': 28, 
'buzz': 0.4028,
'weeklyAverage': 69.5},
'companyNewsScore': 0.5,
'sectorAverageBullishPercent': 0.5022,
'sectorAverageNewsScore': 0.4995,
'sentiment': {'bearishPercent': 0.5, 'bullishPercent': 0.5},
'symbol': 'SBUX'}

Covid-19 statistics for the USA are also accessible via the platform sourced from the CDC.

Conclusion

Finnhub is a great place to start if you’re looking to gather data for financial modelling in python or apply machine learning techniques to large datasets. One of the drawbacks of Finnhub is in its data request structure as the information is pulled at a stock level as opposed to a group level (e.g. All S&P 500 companies). This functionality would simplify data gathering to sector analysis or other higher level analysis.

To remediate this you can loop through a list of tickers and collate them in a DataFrame:

tick = list(tickers.Ticker)
tick1 = tick[:15]
results = []for items in tick1:
url = “https://finnhub.io/api/v1/stock/ceo-compensation?symbol={}&token={}".format(items, api_key)
req = requests.get(url)
content = req.json()
results.append(content)
CEO_PAY = pd.DataFrame(results)
CEO_PAY.dropna(inplace = True)
CEO_PAY

--

--