Releases: JerBouma/FinanceToolkit
FinanceToolkit v1.7.2
Added a lot more economic metrics (over 50) into the Finance Toolkit, find the total list here. For example, get insights into how the United States Government spend their income:
from financetoolkit import Economics
economics = Economics()
central_government_spending = economics.get_central_government_spending()
central_government_spending['United States']
Which returns:
General Public Services | Defence | Public Order and Safety | Economic Affairs | Environmental Protection | Housing and Community Amenities | Health | Recreation, Culture and Religion | Education | Social Protection | |
---|---|---|---|---|---|---|---|---|---|---|
2007 | 0.1578 | 0.186 | 0.0148 | 0.0578 | 0 | 0.0192 | 0.2486 | 0.0015 | 0.0222 | 0.2921 |
2008 | 0.1392 | 0.1861 | 0.0148 | 0.069 | 0 | 0.0188 | 0.2369 | 0.0014 | 0.0221 | 0.3117 |
2009 | 0.1198 | 0.1772 | 0.0148 | 0.0639 | 0 | 0.0406 | 0.2417 | 0.0014 | 0.028 | 0.3127 |
2010 | 0.1211 | 0.1759 | 0.0148 | 0.0585 | 0 | 0.0309 | 0.2416 | 0.0015 | 0.0346 | 0.3211 |
2011 | 0.1324 | 0.1754 | 0.0149 | 0.0562 | 0 | 0.0271 | 0.2416 | 0.0014 | 0.0337 | 0.3173 |
2012 | 0.1348 | 0.173 | 0.0154 | 0.056 | 0 | 0.0216 | 0.2491 | 0.0014 | 0.0296 | 0.3191 |
2013 | 0.1333 | 0.162 | 0.0145 | 0.055 | 0 | 0.0172 | 0.2589 | 0.0013 | 0.0281 | 0.3296 |
2014 | 0.135 | 0.1525 | 0.0143 | 0.0524 | 0 | 0.0168 | 0.2765 | 0.0013 | 0.0269 | 0.3244 |
2015 | 0.1294 | 0.1461 | 0.0141 | 0.0473 | 0 | 0.0161 | 0.2919 | 0.0012 | 0.026 | 0.3279 |
2016 | 0.1321 | 0.141 | 0.0141 | 0.0526 | 0 | 0.0156 | 0.2952 | 0.0013 | 0.0244 | 0.3238 |
2017 | 0.1325 | 0.1408 | 0.0137 | 0.0526 | 0 | 0.0171 | 0.2961 | 0.0013 | 0.0233 | 0.3226 |
2018 | 0.1407 | 0.1427 | 0.0135 | 0.0519 | 0 | 0.014 | 0.2967 | 0.0012 | 0.0226 | 0.3165 |
2019 | 0.1417 | 0.1461 | 0.013 | 0.052 | 0 | 0.0133 | 0.2971 | 0.0012 | 0.021 | 0.3147 |
Or plotted:
Or see the Income Inequality in the Netherlands:
from financetoolkit import Toolkit
toolkit = Toolkit("MSFT")
income_inequality = toolkit.economics.get_income_inequality()
income_inequality['Netherlands']
Which returns:
Gini Coefficient | P90/P10 | P90/P50 | P50/P10 | Palma Ratio | S80/S20 | |
---|---|---|---|---|---|---|
2013 | 0.287 | 3.4 | 1.8 | 1.9 | 1.05 | 4.3 |
2014 | 0.305 | 3.4 | 1.8 | 1.9 | 1.16 | 4.6 |
2015 | 0.305 | 3.4 | 1.8 | 1.9 | 1.06 | 4.4 |
2016 | 0.292 | 3.4 | 1.8 | 1.9 | 1.07 | 4.4 |
2017 | 0.298 | 3.4 | 1.8 | 1.9 | 1.11 | 4.5 |
2018 | 0.295 | 3.3 | 1.8 | 1.9 | 1.09 | 4.4 |
2019 | 0.312 | 3.4 | 1.8 | 1.9 | 1.21 | 4.7 |
2020 | 0.295 | 3.3 | 1.8 | 1.9 | 1.09 | 4.4 |
2021 | 0.297 | 3.4 | 1.8 | 1.9 | 1.1 | 4.5 |
Or plotted:
And so much more!
FinanceToolkit v1.7.1
In addition to the many new Economics indicators in the previous release, I've added some new key economic rates.
I've added in the European Central Banks which include:
- The main refinancing operations (MRO) rate is the interest rate banks pay when they borrow money from the ECB for one week. When they do this, they have to provide collateral to guarantee that the money will be paid back.
- The marginal lending facility rate is the interest rate banks pay when they borrow from the ECB overnight. When they do this, they have to provide collateral, for example securities, to guarantee that the money will be paid back.
- The deposit facility rate which is one of the three interest rates the ECB sets every six weeks as part of its monetary policy. The rate defines the interest banks receive for depositing money with the central bank overnight.
These can be shown by using:
from financetoolkit import Economics
economics = Economics(start_date='2000-01-01')
economics.get_european_central_bank_rates()
Which returns (when plotted):
I've also added in the Federal Funds rates which include:
- The effective federal funds rate (EFFR) which is calculated as a volume-weighted median of overnight federal funds transactions reported in the FR 2420 Report of Selected Money Market Rates.
- The overnight bank funding rate (OBFR) which is calculated as a volume-weighted median of overnight federal funds transactions, Eurodollar transactions, and the domestic deposits reported as “Selected Deposits” in the FR 2420 Report.
- The TGCR which is calculated as a volume-weighted median of transaction-level tri-party repo data collected from the Bank of New York Mellon.
- The BGCR which is calculated as a volume-weighted median of transaction-level tri-party repo data collected from the Bank of New York Mellon as well as GCF Repo transaction data obtained from the U.S. Department of the Treasury’s Office of Financial Research (OFR).
- The SOFR which is calculated as a volume-weighted median of transaction-level tri-party repo data collected from the Bank of New York Mellon as well as GCF Repo transaction data and data on bilateral Treasury repo transactions cleared through FICC's DVP service, which are obtained from the U.S. Department of the Treasury’s Office of Financial Research (OFR).
These can be shown by using (and defining the rate):
from financetoolkit import Economics
economics = Economics(start_date='2016-01-01')
# Effective Federal Funds Rate
economics.get_federal_reserve_rates(rate='EFFR')
# Secured Overnight Financing Rate
economics.get_federal_reserve_rates(rate='SOFR')
Which returns (when plotted):
FinanceToolkit v1.7.0
This release features an entirely new module called Economics
. This is meant to collect key economic indicators such as the Gross Domestic Product (GDP), Consumer Price Index (CPI) and Long and Short Term Interest Rates for 60+ countries. Find a complete overview here. This module can be called via the Toolkit:
from financetoolkit import Toolkit
companies = Toolkit(tickers=["AAPL", "TSLA"], api_key="FMP_KEY")
companies.economics.get_house_prices()
But also as standalone module (given that it doesn't require a company ticker):
from financetoolkit import Economics
economics = Economics()
economics.get_house_prices()
Both return the same dataset:
Australia | Austria | Belgium | Brazil | Bulgaria | Canada | Chile | China | Colombia | Croatia | Czech Republic | Denmark | EA | Estonia | Euro Area 17 | Finland | France | Germany | Greece | Hungary | Iceland | India | Indonesia | Ireland | Israel | Italy | Japan | Latvia | Lithuania | Luxembourg | Mexico | Netherlands | New Zealand | Norway | OECD - Total | Poland | Portugal | Romania | Russia | Saudi Arabia | Slovakia | Slovenia | South Africa | South Korea | Spain | Sweden | Switzerland | Turkey | United Kingdom | United States | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2013 | 84.1065 | 92.1 | 98.962 | 98.269 | 95.915 | 89.9958 | 78.0367 | 96.4912 | 85.1748 | 104.627 | 93.875 | 90.0977 | 98.267 | 82.2933 | 98.4009 | 100.353 | 103.8 | 92.5982 | 113.816 | 84.87 | 85.2075 | 76.6863 | 88.5178 | 77.0045 | 88.8228 | 109.1 | 96.148 | 97.6225 | 90.645 | 90.8947 | 88.6885 | 95.7715 | 83.5338 | 91.75 | 92.1431 | 97.4165 | 93.085 | 99.26 | 95.574 | nan | 93.5868 | 106.21 | 87.4525 | 95.7696 | 96.2393 | 80.785 | 94.867 | 75.946 | 87.3505 | 90.7087 |
2014 | 91.7202 | 95.33 | 98.417 | 99.1345 | 97.29 | 94.828 | 88.4455 | 101.132 | 91.9212 | 102.975 | 96.175 | 93.4987 | 98.482 | 93.5795 | 98.5693 | 99.995 | 101.925 | 95.499 | 105.32 | 88.4275 | 92.3855 | 88.032 | 94.73 | 89.7232 | 94.4668 | 103.95 | 97.648 | 103.475 | 96.47 | 94.8777 | 92.4655 | 96.5565 | 88.9588 | 94.25 | 95.5783 | 98.4538 | 97.0375 | 97.22 | 97.9095 | 101.189 | 94.9068 | 99.2025 | 94.146 | 97.2716 | 96.5393 | 88.4025 | 97.4497 | 86.1545 | 94.3388 | 95.1076 |
2015 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 |
2016 | 105.5 | 108.532 | 102.641 | 100.868 | 107.02 | 110.589 | 106.573 | 112.23 | 109.995 | 100.888 | 107.15 | 104.727 | 104.01 | 104.745 | 104.012 | 101.285 | 100.925 | 107.527 | 97.623 | 113.377 | 109.767 | 106.686 | 103.159 | 107.484 | 107.459 | 100.25 | 102.213 | 108.487 | 105.4 | 106.006 | 107.282 | 105.052 | 114.178 | 107.025 | 105.352 | 101.859 | 107.115 | 105.95 | 102.856 | 95.091 | 106.703 | 103.252 | 105.616 | 101.622 | 104.623 | 108.243 | 101.627 | 113.615 | 106.975 | 105.586 |
2017 | 114.308 | 114.26 | 106.373 | 101.733 | 116.295 | 123.751 | 115.275 | 126.989 | 117.332 | 104.745 | 119.7 | 109.378 | 108.513 | 110.511 | 108.454 | 102.388 | 103.975 | 114.104 | 96.6342 | 127.24 | 131.196 | 115.658 | 106.415 | 119.162 | 111.649 | 99.175 | 104.805 | 118.013 | 114.797 | 111.96 | 115.687 | 112.978 | 121.596 | 112.35 | 111.13 | 105.768 | 117.015 | 112.355 | 108.782 | 88.9675 | 112.99 | 111.785 | 110.064 | 102.854 | 111.105 | 115.427 | 103.591 | 125.392 | 111.834 | 112.078 |
2018 | 112.664 | 119.64 | 109.415 | 102.599 | 123.955 | 128.378 | 126.812 | 134.268 | 123.825 | 111.14 | 130 | 114.529 | 113.668 | 117.066 | 113.604 | 103.335 | 107.15 | 121.681 | 98.379 | 145.495 | 142.006 | 122.248 | 109.895 | 131.363 | 110.724 | 98.625 | 106.849 | 129.3 | 123.18 | 119.862 | 125.951 | 123.656 | 125.966 | 113.975 | 116.592 | 112.765 | 129.032 | 118.618 | 116.147 | 86.4625 | 121.32 | 121.547 | 114.246 | 104.275 | 118.58 | 114.35 | 106.728 | 134.891 | 115.496 | 118.951 |
2019 | 108.084 | 126.6 | 113.779 | 103.464 | 131.417 | 129.959 | 137.5 | 139.503 | 130.601 | 121.103 | 141.9 | 117.53 | 118.641 | 125.267 | 118.505 | 103.77 | 110.675 | 128.707 | 105.481 | 170.167 | 148.291 | 126.309 | 111.91 | 134.431 | 112.812 | 98.525 | 108.553 | 140.875 | 131.61 | 131.987 | 136.739 | 132.658 | 129.9 | 116.8 | 121.327 | 122.55 | 141.882 | 122.69 | 119.822 | 83.5023 | 132.395 | 129.69 | 118.273 | 103.968 | 124.715 | 117.188 | 110.572 | 141.024 | 116.592 | 124.942 |
2020 | 113.916 | 136.311 | 118.595 | 104.332 | 137.412 | 138.454 | 146.659 | 144.577 | 133.834 | 130.38 | 153.85 | 123.531 | 124.813 | 132.792 | 124.761 | 105.595 | 116.825 | 138.685 | 110.223 | 178.577 | 157.788 | 129.465 | 113.646 | 134.847 | 116.261 | 100.4 | 108.63 | 145.768 | 141.178 | 151.115 | 144.687 | 142.761 | 143.109 | 121.775 | 128.945 | 135.335 | 154.327 | 128.435 | 144.26 | 84.833 | 145.061 | 135.657 | 121.228 | 107.451 | 127.505 | 122.11 | 115.355 | 173.709 | 119.904 | 134.761 |
2021 | 133.764 | 153.262 | 127.058 | 105.198 | 149.333 | 159.222 | 164.029 | 150.101 | 141.909 | 139.912 | 184.225 | 138.01 | 134.675 | 152.778 | 134.765 | 110.448 | 124.625 | 154.739 | 118.606 | 208.037 | 177.563 | 132.749 | 115.27 | 146 | 126.076 | 102.95 | 115.102 | 161.645 | 163.84 | 172.151 | 156.073 | 164.241 | 180.981 | 134.525 | 145.113 | 147.76 | 168.84 | 134.088 | 175.268 | 85.7263 | 154.334 | 151.317 | 126.269 | 116.89 | 132.195 | 134.463 | 123.334 | 237.534 | 130.515 | 157.24 |
2022 | 142.65 | 170.765 | 134.194 | 106.063 | 169.917 | 177.235 | 175.906 | 151.385 | 152.542 | 160.685 | 215.275 | 137.36 | 144.219 | 186.76 | 144.178 | 111.745 | 132.5 | 162.941 | 132.69 | 254.345 | 214.848 | 136.925 | nan | 163.977 | 147.852 | 106.875 | 124.755 | 184.005 | 195.048 | 188.6 | 169.973 | 186.321 | 182.022 | 141.575 | 165.45 | 165.18 | 190.17 | 143.697 | 225.506 | 87.5558 | 175.471 | 173.648 | 130.758 | 119.32 | 141.978 | 139.25 | 132.603 | 601.714 | 143.214 | 179.097 |
It is also possible to plot things such as the Employment Rate as also found in the README:
Or the Long Term (10 year) and Short Term (3 month) Interest Rates:
Or any of the other metrics:
- Gross Domestic Product (GDP) including Growth and Forecasts
- Consumer Confidence Index (CCI)
- Business Confidence Index (BCI)
- Composite Leading Indicator (CLI)
- Consumer Price Index (CPI)
- Producer Price Index (PPI)
- House and Rent Prices
- Unemployment Rates
- Long Term Interest Rates (10 year)
- Short Term Interest Rates (3 month)
- Purchasing Power Parity (PPP)
- Exchange Rates
Are you looking for any specific economic parameter? Let me know and I'll start working on it.
FinanceToolkit v1.6.6
This releases introduces the GARCH model including volatility forecasting. Next to that, it includes a bugfix for the currency conversion.
Designed by @northern-64bit (LinkedIn) in #82, this release introduces GARCH (Generalized autoregressive conditional heteroskedasticity) which is stochastic model for time series, which is for instance used to model volatility clusters, stock return and inflation. It is a generalisation of the ARCH models.
It can be found inside the risk
module and can be ran with the following code:
from financetoolkit import Toolkit
toolkit = Toolkit(["AMZN", "TSLA"], api_key=FMP_KEY)
toolkit.risk.get_garch()
Which produces the following result:
Date | AMZN | TSLA | Benchmark |
---|---|---|---|
2012 | 0 | 0 | 0 |
2013 | 0.2038 | 5.027 | 0.024 |
2014 | 0.4016 | 10.2307 | 0.0537 |
2015 | 0.53 | 13.2345 | 0.0688 |
2016 | 0.7664 | 15.6152 | 0.079 |
2017 | 0.8181 | 17.5204 | 0.0887 |
2018 | 0.8896 | 19.0642 | 0.0997 |
2019 | 0.9235 | 20.2789 | 0.104 |
2020 | 0.9479 | 21.2567 | 0.1169 |
2021 | 1.0203 | 27.5585 | 0.1209 |
2022 | 1.0201 | 27.0986 | 0.129 |
2023 | 1.0445 | 26.7482 | 0.1305 |
You can also forecast in the future for any period and for any interval. For example, the quarterly expected volatility estimations can be shown with:
from financetoolkit import Toolkit
toolkit = Toolkit(["AMZN", "TSLA"], api_key=FMP_KEY)
toolkit.risk.get_garch_forecast(period='quarterly')
Which returns:
AMZN | TSLA | Benchmark | |
---|---|---|---|
2024Q1 | 0 | 0 | 0 |
2024Q2 | 0 | 0 | 0 |
2024Q3 | 0.006 | 0.3185 | 0.0003 |
2024Q4 | 0.0114 | 0.6051 | 0.0006 |
2025Q1 | 0.0162 | 0.8631 | 0.0008 |
2025Q2 | 0.0206 | 1.0953 | 0.001 |
2025Q3 | 0.0245 | 1.3042 | 0.0012 |
2025Q4 | 0.0281 | 1.4923 | 0.0014 |
2026Q1 | 0.0312 | 1.6615 | 0.0015 |
2026Q2 | 0.0341 | 1.8138 | 0.0017 |
Next to that, a bug fix went in related to the currency conversions which makes it more robust when there is data missing. If you didn't know, when you have a Premium FMP plan the Finance Toolkit will now automatically convert currencies that do not match up with the historical data. For more see here: https://github.com/JerBouma/FinanceToolkit/releases/tag/v1.6.3
FinanceToolkit v1.6.3
This release features currency conversions. A user noticed that the financial statements are not always in the expected currency (different from the historical data):
This could lead to confusion when comparing numbers between companies and or when calculating ratios that require both data from the financial statement and the historical market data. This has now been resolved with an automatic converter that converts the financial statements accordingly. With this, the end of quarter or end of year exchange rate is used.
This is also saved in a seperate function called get_exchange_rates
as shown below:
This is quite a major change given that there are plenty of financial statements that report their financial statements in their local currency while the listed historical prices are in USD. This ensures that those numbers can be compared. This can be disabled by setting convert_currency
to False
in the Toolkit
initialisation. Note that by default it is automatically turned off when using the Free plan of FinancialModelingPrep as it could drain the API calls for that specific day.
I've also added in a parameter that recognises if you are using a Free or Premium plan from FinancialModelingPrep. This is a dependency for the sleep_timer
which will wait a maximum of 60 seconds before making a new request to FMP. This is only relevant for the Premium plans since there is a rate limit per minute. For the Free plan it doesn't matter given that you are limited to 250 requests per day. In any case, this should make the Free experience smoother since you are not waiting for no reason. This solves #80.
Note: this consumes 1 API call so if you don't want that, just set sleep_timer to True or False.
if sleep_timer is None:
# This tests the API key to determine the subscription plan. This is relevant for the sleep timer
# but also for other components of the Toolkit. This prevents wait timers from occurring while
# it wouldn't result to any other answer than a rate limit error.
determine_plan = helpers.get_financial_data(
url=f"https://financialmodelingprep.com/api/v3/income-statement/AAPL?period=quarter&apikey={api_key}",
sleep_timer=False,
)
self._fmp_plan = "Premium"
for option in ["NOT AVAILABLE", "LIMIT REACH", "INVALID API KEY"]:
if option in determine_plan:
self._fmp_plan = "Free"
break
else:
self._fmp_plan = "Premium"
FinanceToolkit v1.6.0
I did a new release last week but for everything I've added in it made sense to do another full release. This release includes new models and ESG scores and a lot of the code has been refactored, many new tests have been added and a lot of bug fixes went in. For example, @brianrasmusson noticed in #76 that Apple's data was a bit out of whack. This has now been resolved. I've also added in a lot pretty graphs into the README to show what you can really do with the data, e.g. see this one on the Value of Risk:
It is now possible to view ESG Scores for any range of companies with get_esg_scores
:
Next to that, within the Models
module, two new models are included. These are Altman Z-Score and Piotroski F-score. The former calculates the chance of bankruptcy based on a set of criteria:
The latter scores the financial strength of the company:
Furthermore, I've added a performance measurement which shows the period growth rate that has occurred (taking out fluctuations over time as shown in the plot below):
Other than that, I've extended the examples and gave more explanation how to add in your own datasets.
FinanceToolkit v1.5.0
Introducing the power of Threading into the FinanceToolkit. Update now via pip install financetoolkit -U
.
As an example, obtaining the balance sheet statements of 345 different companies for the period 1983 to 2023 takes less than 40 seconds. This makes data collection extremely quick and makes the FinanceToolkit an invaluable tool for any financial specialist.
Next to that, given that there are rate limits, these are correctly dealt with through sleep timers. If you have a subscription from FinancialModelingPrep that limits you to 300 requests per minute, the FinanceToolkit will deal with this and will wait until it can make requests again.
Furthermore, if some of the historical data is not available from FinancialModelingPrep it will try YahooFinance instead making sure that you don't lose vital data for your calculations.
Of course, all of this extends in all functionality. E.g. see Jensen's Alpha for all 345 tickers on a quarterly basis. Given that the calculations are done by the FinanceToolkit, this takes just a few seconds.
It's time to level up. 🚀
FinanceToolkit v1.4.1
I've added in the Dividend Calendar which can be shown by get_dividend_calendar()
:
from financetoolkit import Toolkit
API_KEY = "FINANCIAL_MODELING_PREP_KEY"
# Initialize the Toolkit with company tickers
companies = Toolkit(
["AAPL", "MSFT", "GOOGL", "AMZN"], api_key=API_KEY, start_date="2005-01-01"
)
companies.get_dividend_calendar()
Which returns:
Next to that, this release introduces historical data from FinancialModelingPrep. This is enabled by default when you provide an API key but can be changed back by setting historical_source
to "YahooFinance"
when initialising the Toolkit. When you don't provide an API key it defaults to Yahoo Finance.
This is done to prevent rate limits from becoming an issue when you have purchased a FinancialModelingPrep subscription given that Yahoo Finance will rate limit at some point. It of course makes little sense that you are paying and still get rate limited!
E.g. the following will net you the same result:
from financetoolkit import Toolkit
API_KEY = "FINANCIAL_MODELING_PREP_KEY"
# Use FinancialModelingPrep to obtain Historical Data
companies = Toolkit(
["AAPL", "MSFT", "GOOGL", "AMZN"], api_key=API_KEY, start_date="2005-01-01"
)
companies.get_historical_data()
# Use Yahoo Finance to obtain Historical Data even though you set a FinancialModelingPrep API Key
companies = Toolkit(
["AAPL", "MSFT", "GOOGL", "AMZN"], api_key=API_KEY, start_date="2005-01-01", historical_source='YahooFinance'
)
companies.get_historical_data()
# Use Yahoo Finance to obtain Historical Data without needing to set an API Key
companies = Toolkit(
["AAPL", "MSFT", "GOOGL", "AMZN"], start_date="2005-01-01"
)
companies.get_historical_data()
Which returns:
FinanceToolkit v1.4.0
This releases includes the Fama and French 5-factor model. Something I've been wanting to add into the Finance Toolkit for some time now. This allows you to understand the explanatory power of the following factors on each ticker included in the Toolkit on any period, let it be yearly, quarterly, monthly or weekly:
- Market Risk Premium (Mkt-RF): Represents the additional return that investors expect to earn for taking
on the risk of investing in the overall market as opposed to a risk-free asset. - Size Premium (SMB): Reflects the historical excess return of small-cap stocks over large-cap stocks.
- Value Premium (HML): Captures the historical excess return of value stocks over growth stocks.
- Profitability (RMW): Measures the historical excess return of high profitability stocks over
low profitability stocks. - Investment (CMA): Quantifies the historical excess return of low investment stocks over
high investment stocks.
I've added correlation functions that help you understand how factors correlate with each other over time. For example, the following GIF is created with data from the function performance.get_factor_correlations
.
Next to that, you can perform Linear Regressions on each factor and each factor combination to discover sensitivities of individual assets to each factor (this is not only limited to companies but can also be currencies, commodities, ETFs and more):
from financetoolkit import Toolkit
# Initialize the Finance Toolkit
companies = Toolkit(
tickers=["MSFT", 'AAPL', 'TSLA', 'GOOG', "AMZN", 'MU'], api_key=FMP_KEY, quarterly=False
)
companies.performance.get_fama_and_french_model()
Which returns regression coefficients for all factors for each individual stock:
You have the option to show both the results from a simple linear regression (which is defined as Excess Return = Intercept + Slope * Factor Value + Residuals
) or a multi linear regression (which is defined as Excess Return = Intercept + Beta1 * Mkt-RF + Beta2 * SMB + Beta3 * HML + Beta4 * RMW + Beta5 * CMA + Residuals
) the latter is the default. E.g. you can show the explanatory power of each individual factor for example for Microsoft as follows:
from financetoolkit import Toolkit
# Initialize the Finance Toolkit
companies = Toolkit(
tickers=["MSFT"], api_key=FREE_FMP_KEY, quarterly=False
)
result = companies.performance.get_fama_and_french_model(period='quarterly', method='simple')
result['MSFT'].xs('R Squared', level=1, axis=1).plot(figsize=(15, 5), title=f'Factor Sensitivities of Microsoft')
Which returns the following:
FinanceToolkit v1.3.10
This releases features a couple of Quality of Life improvements and some bug fixes:
- A user noticed that for some tickers, e.g. currencies, it returned an error since the index wasn't matched up correctly. This has now been corrected by only keeping the first duplicate if any are found. The issue here was that the same index was reported twice (e.g. 2020-05-06 being reported twice in the index) which will cause an issue if you try to combine such a DataFrame with other DataFrames.
- For all functionality, if the ticker doesn't correspond to the correct type, the Toolkit will mention this to you. E.g. if you would try to obtain an Income Statement from a ticker such as "^GSPC" (which is the S&P 500 index) it will return a well-written error. This also applies to if a subset of your tickers is not marked as an Equity.
If you didn't know, any type of asset class works with the Finance Toolkit (e.g. Equities, Currencies, Cryptocurrencies, ETFs, Mutual Funds, Indices, Money Markets, Commodities and more) which also allows you to obtain detailed calculations such as the Conditional Value at Risk (cVaR).