FinanceDatabase Guide - A Comprehensive Database of Financial Symbols (Python Package) - AlgoTrading101 Blog
To obtain all US companies from different sectors using the FinanceDatabase, you can utilize the options and select features to loop over all the sectors and aggregate the companies per each of them.
equities_sectors_us = {}
for sector in equities.options(selection='sector', country='United States'):
try:
equities_sectors_us[sector] = len(equities.select(country='United States', sector=sector))
except ValueError as error:
print(error){'Communication Services': 444,
'Consumer Discretionary': 739,
'Consumer Staples': 402,
'Energy': 470,
'Financials': 3919,
'Health Care': 1559,
'Industrials': 1044,
'Information Technology': 1043,
'Materials': 424,
'Real Estate': 513,
'Utilities': 190}How to perform technical analysis with FinanceDatabase?
To perform technical analysis with FinanceDatabase, you will need to extend its features with other libraries such as yfinance, ta, talib, and similar. For example, let’s examine the french wine industry’s large caps in a post-covid market setting.
The first thing that we want to do is to install the libraries that are missing:
!pip install ta
!pip install yfinanceNow, we import everything that we’ll be using:
import pandas as pd
import yfinance as yf
from ta.volatility import BollingerBands
import matplotlib.pyplot as plt
%matplotlib inlineThe next step that we want to do is to obtain all the french beverage industry equities that have “wine” inside their summary and that are large caps. We will obtain the data from Match 2020 up to the beginning of 2023:
french_wine_equities = equities.search(
country="France",
industry="Beverages",
summary="wine",
market_cap="Large Cap"
)
french_wine_equitiesFrom the output, we can observe that Pernod Ricard SA is the main and only leader here. Now, let’s obtain the data on these tickers with yfinance to examine them further:
tickers = list(french_wine_equities.index)
stock_fr_wine = yf.download(tickers, start="2020-03-01", end="2023-01-01")['Adj Close']
stock_fr_wine.drop(["PER1.F", "PDRDY"], axis=1, inplace=True)
stock_fr_wine.head()As yfinance didn’t have the two tickers above, I removed them. Now, we will fill in the missing data and calculate the difference between the daily returns as I’d like to examine the difference in volatility, if any, between these tickers:
stock_fr_wine.fillna(method = 'ffill', inplace = True)
stock_fr_wine = stock_fr_wine.diff()Now, I’ll add a Bollinger Bands indicator to help us gauge the volatility between the returns and only plot the upper and lower band. I’ll also add a straight line on the 0 y-axes.
fig, axis = plt.subplots(3, 2)
row = 0
column = 0
for ticker in stock_fr_wine.columns:
data_plot = pd.DataFrame(stock_fr_wine[ticker])
indicator_bb = BollingerBands(close=stock_fr_wine[ticker], window=20, window_dev=2)
data_plot['bb_bbh'] = indicator_bb.bollinger_hband()
data_plot['bb_bbl'] = indicator_bb.bollinger_lband()
axis[row, column].plot(data_plot)
axis[row, column].set_title(ticker, fontsize=16)
axis[row, column].axhline(0, c="purple")
column += 1
if column == 2:
row += 1
column = 0
fig.suptitle('Technical Analysis of Pernod Ricard SA')
fig.set_size_inches(18.5, 10.5)
fig.tight_layout()As you can see, there are differences in volatility between these assets and their respective exchanges. Knowing this, a trading strategy pattern or investment plan might start to sprout. Feel free to extend this simple analysis and try out other assets.
How to store the FinanceDatabase in a different location?
To store the FinanceDatabase in a different location, for example, your own fork of the repository, you can do so with the variablebase_url which can be found in each of the asset classes.
fd.Equities(base_url=<YOUR URL>)You can also store the database locally and point to your local location with the variable base_url and by settinguse_local_location to True. For example:
fd.Equities(base_url=<YOUR PATH>, use_local_location=True)Where can I learn more about the FinanceDatabase?
To learn more about the FinanceDatabase, I advise checking out its GitHub repository. You can also find this Jupyter Notebook of examples useful.