Finance Toolkit
While browsing a variety of websites, I kept finding that the same financial metric can greatly vary per source and so do the financial statements reported while little information is given how the metric was calculated.
For example, Microsoft’s Price-to-Earnings (PE) ratio on the 6th of May, 2023 is reported to be 28.93 (Stockopedia), 32.05 (Morningstar), 32.66 (Macrotrends), 33.09 (Finance Charts), 33.66 (Y Charts), 33.67 (Wall Street Journal), 33.80 (Yahoo Finance) and 34.4 (Companies Market Cap). All of these calculations are correct, however the method applied varies leading to different results. Therefore, collecting data from multiple sources can lead to wrong interpretation of the results given that one source could be applying a different calculation method than another. And that is, if it is even freely available. Often the calculation is hidden behind a paid subscription.
This is why I designed the FinanceToolkit, this is an open-source toolkit in which all relevant financial methods (500+) are written down in the most simplistic way allowing for complete transparency of the calculation method (proof). This enables you to avoid dependence on metrics from other providers that do not provide their methods. With a large selection of financial statements in hand, it facilitates streamlined calculations, promoting the adoption of a consistent and universally understood methods and formulas.
Beyond Equities, it supports Options, Currencies, Cryptocurrencies, ETFs, Mutual Funds, Indices, Money Markets, Commodities, Key Economic Indicators and more, allowing you to obtain historical data as well as important performance and risk measurements such as the Sharpe Ratio and Value at Risk.
The Finance Toolkit is complemented very well by the Finance Database 🌎, a database that features 300.000+ symbols containing Equities, ETFs, Funds, Indices, Currencies, Cryptocurrencies and Money Markets. By utilising both, it is possible to do a fully-fledged competitive analysis with the tickers found from the FinanceDatabase inputted into the FinanceToolkit.

Installation
Before installation, consider starring the project on GitHub which helps others find the project as well.
To install the FinanceToolkit it simply requires the following:
pip install financetoolkit -U
Then within Python use:
from financetoolkit import Toolkit
companies = Toolkit(
tickers=['GOOGL', 'MSFT', 'AMZN'],
api_key="FINANCIAL_MODELING_PREP_KEY",
)
To be able to get started, you need to obtain an API Key from FinancialModelingPrep. This is used to gain access to 30+ years of financial statement both annually and quarterly. Note that the Free plan is limited to 250 requests each day, 5 years of data and only features companies listed on US exchanges.
Obtain an API Key from FinancialModelingPrep
Through the link you are able to subscribe for the free plan and also premium plans at a 15% discount. This is an affiliate link and thus supports the project at the same time. I have chosen FinancialModelingPrep as a source as I find it to be the most transparent, reliable and at an affordable price. I have yet to find a platform offering such low prices for the amount of data offered. When you notice that the data is inaccurate or have any other issue related to the data, note that I simply provide the means to access this data and I am not responsible for the accuracy of the data itself. For this, use their contact form or provide the data yourself.
By default, the Finance Toolkit prioritizes Financial Modeling Prep for data retrieval. If data acquisition from Financial Modeling Prep is unsuccessful (e.g., due to plan restrictions or API key issues), the toolkit automatically switches to Yahoo Finance as a secondary source. To disable this fallback behavior and exclusively use Financial Modeling Prep, set enforce_source="FinancialModelingPrep" during Toolkit initialization.
Functionality
This section is an introduction to the Finance Toolkit. Find below a fully-fledged code documentation as well as the How-To Guides section further down the page, in which you can see many examples ranging from basic examples to creating custom ratios to working with your own datasets.
A basic example of how to use the Finance Toolkit is shown below. Every code snippet in the sections that follow builds on this same companies instance.
from financetoolkit import Toolkit
# Initialize the Toolkit for Apple and Microsoft
companies = Toolkit(["AAPL", "MSFT"], api_key=API_KEY, start_date="2017-12-31")
Each ratio, indicator and metric has a corresponding function that can be called directly, for example ratios.get_return_on_equity or technicals.get_relative_strength_index. Every module also has one or more collect_ functions that return a whole category at once, e.g. ratios.collect_profitability_ratios, useful when you want everything in one call instead of assembling it metric by metric.
Three capabilities cut across nearly the whole toolkit:
rollingandtrailingwindows. Many metrics return one value per reporting period by default. Passrolling=<n>to compute the metric over a sliding window instead, ortrailing=<n>for a trailing sum/average (e.g. a trailing 4-quarter sum to annualize a quarterly flow) — turning a snapshot into a proper time series.growthandlag. Passgrowth=Trueon almost anyget_orcollect_function to return the period-over-period growth instead of the raw value.lag(anintor list ofints, default1) controls how many periods back that growth is measured against, e.g.lag=4for year-over-year growth on quarterly data. Combine withtrailing(e.g.trailing=4, growth=True) to get TTM growth.standardize(Z-Score). Mostget_*methods across Economics, Ratios, Technicals, Risk, Performance, Models, Options and Fixed Income acceptstandardize=True, converting raw values into standard deviations from their own historical mean/std. Useful for ranking, scoring, or spotting an unusual reading across metrics that otherwise live on incompatible scales.
Every module below also has a How-To Guide notebook (see the How-To Guides section below) and full code documentation (formulas, parameters, worked examples) linked in its own section below.
Discovering Instruments & News
Before analyzing a ticker you often need to find it. The Discovery module is standalone and covers among other things lists of companies, cryptocurrencies, forex, commodities, ETFs and indices.
from financetoolkit import Discovery
# Initialize the standalone Discovery module
discovery = Discovery(api_key="FINANCIAL_MODELING_PREP_KEY")
# Screen for stocks matching a set of criteria
discovery.get_stock_screener(
market_cap_higher=1000000,
price_higher=100,
price_lower=200,
beta_higher=1,
beta_lower=1.5,
dividend_higher=1,
)
Which returns:
| Symbol | Name | Market Cap | Sector | Industry | Beta | Price | Dividend | Exchange | Country |
|---|---|---|---|---|---|---|---|---|---|
| NKE | NIKE, Inc. | 163403295604 | Consumer Cyclical | Footwear & Accessories | 1.079 | 107.36 | 1.48 | New York Stock Exchange | US |
| SAF.PA | Safran SA | 66234006559 | Industrials | Aerospace & Defense | 1.339 | 160.16 | 1.35 | Paris | FR |
| ROST | Ross Stores, Inc. | 46724188589 | Consumer Cyclical | Apparel Retail | 1.026 | 138.785 | 1.34 | NASDAQ Global Select | US |
Furthermore, you can find in this module stock screeners, sector/industry performance and news feeds and more. Find the full instrument discovery documentation here.
Obtaining Historical Data
Obtain historical data on a daily, weekly, monthly or yearly basis. This includes OHLC, volumes, dividends, returns and cumulative returns for each corresponding period.
# Obtain historical market data for all tickers
historical_data = companies.get_historical_data()
# Select the results for Apple
historical_data.xs('AAPL', axis=1, level=1)
For example, a portion of the historical data for Apple is shown below.
| date | Open | High | Low | Close | Adj Close | Volume | Dividends | Return | Volatility | Excess Return | Excess Volatility | Cumulative Return |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2018-01-02 | 42.54 | 43.075 | 42.315 | 43.065 | 40.78 | 1.02224e+08 | 0 | 0 | 0.0202 | -0.0067 | 0.0233 | 1 |
| 2018-01-03 | 43.1325 | 43.6375 | 42.99 | 43.0575 | 40.77 | 1.17982e+08 | 0 | -0.0002 | 0.0202 | -0.0247 | 0.0233 | 0.9998 |
| 2018-01-04 | 43.135 | 43.3675 | 43.02 | 43.2575 | 40.96 | 8.97384e+07 | 0 | 0.0047 | 0.0202 | -0.0198 | 0.0233 | 1.0044 |
| 2018-01-05 | 43.36 | 43.8425 | 43.2625 | 43.75 | 41.43 | 9.46401e+07 | 0 | 0.0115 | 0.0202 | -0.0133 | 0.0233 | 1.0159 |
| 2018-01-08 | 43.5875 | 43.9025 | 43.4825 | 43.5875 | 41.27 | 8.22711e+07 | 0 | -0.0039 | 0.0202 | -0.0287 | 0.0233 | 1.012 |
And below the cumulative returns are plotted which include the S&P 500 as benchmark:
Metrics such as Volatility, Excess Return and Excess Volatility are calculated as dedicated Risk and Performance methods rather than columns on this table to create more efficient and flexible functionalities.
Obtaining Financial Statements
Obtain an Income Statement on an annual or quarterly basis. This can also be a balance statement or cash flow statement.
# Obtain the Income Statement for all tickers
income_statement = companies.get_income_statement()
# Select the results for Apple
income_statement.loc['AAPL']
For example, the first 5 rows of the Income Statement for Apple are shown below.
| 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | |
|---|---|---|---|---|---|---|---|
| Revenue | 2.29234e+11 | 2.65595e+11 | 2.60174e+11 | 2.74515e+11 | 3.65817e+11 | 3.94328e+11 | 3.83285e+11 |
| Cost of Goods Sold | 1.41048e+11 | 1.63756e+11 | 1.61782e+11 | 1.69559e+11 | 2.12981e+11 | 2.23546e+11 | 2.14137e+11 |
| Gross Profit | 8.8186e+10 | 1.01839e+11 | 9.8392e+10 | 1.04956e+11 | 1.52836e+11 | 1.70782e+11 | 1.69148e+11 |
| Gross Profit Ratio | 0.3847 | 0.3834 | 0.3782 | 0.3823 | 0.4178 | 0.4331 | 0.4413 |
| Research and Development Expenses | 1.1581e+10 | 1.4236e+10 | 1.6217e+10 | 1.8752e+10 | 2.1914e+10 | 2.6251e+10 | 2.9915e+10 |
And below the Earnings Before Interest, Taxes, Depreciation and Amortization (EBITDA) are plotted for both Apple and Microsoft.
Obtaining Financial Ratios
Get Profitability Ratios based on the inputted balance sheet, income and cash flow statements. This can be any of the 80+ ratios within the ratios module.
# Collect all Profitability Ratios for all tickers
profitability_ratios = companies.ratios.collect_profitability_ratios()
# Select the results for Microsoft
profitability_ratios.loc['MSFT']
For example, see some of the profitability ratios of Microsoft below.
| 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | |
|---|---|---|---|---|---|---|---|
| Gross Margin | 0.6191 | 0.6525 | 0.659 | 0.6778 | 0.6893 | 0.684 | 0.6892 |
| Operating Margin | 0.2482 | 0.3177 | 0.3414 | 0.3703 | 0.4159 | 0.4206 | 0.4177 |
| Net Profit Margin | 0.2357 | 0.1502 | 0.3118 | 0.3096 | 0.3645 | 0.3669 | 0.3415 |
| Interest Coverage Ratio | 13.9982 | 16.5821 | 20.3429 | 25.3782 | 34.7835 | 47.4275 | 52.0244 |
| Income Before Tax Profit Margin | 0.2574 | 0.3305 | 0.3472 | 0.3708 | 0.423 | 0.4222 | 0.4214 |
And below a few of the profitability ratios are plotted for Microsoft.
The 80+ ratios are divided into five categories: Efficiency (asset/inventory/receivables turnover, cash conversion cycle, R&D/SG&A/SBC-to-revenue), Liquidity (current, quick and cash ratios, working capital), Profitability (margins, ROE/ROA/ROIC, cash vs. effective tax rate), Solvency (debt-to-equity, debt-to-capital, interest and dividend coverage) and Valuation (P/E, PEG, Forward P/E, EV multiples, buyback and shareholder yield). It’s also possible to define fully custom ratios calculated automatically from the balance sheet, income and cash flow statements.
Obtaining Financial Models
Get an Extended DuPont Analysis based on the inputted balance sheet, income and cash flow statements.
# Get the Extended DuPont Analysis for all tickers
extended_dupont_analysis = companies.models.get_extended_dupont_analysis()
# Select the results for Apple
extended_dupont_analysis.loc['AAPL']
For example, this shows the Extended DuPont Analysis for Apple:
| 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | |
|---|---|---|---|---|---|---|---|
| Interest Burden Ratio | 0.9572 | 0.9725 | 0.9725 | 0.988 | 0.9976 | 1.0028 | 1.005 |
| Tax Burden Ratio | 0.7882 | 0.8397 | 0.8643 | 0.8661 | 0.869 | 0.8356 | 0.8486 |
| Operating Profit Margin | 0.2796 | 0.2745 | 0.2527 | 0.2444 | 0.2985 | 0.302 | 0.2967 |
| Asset Turnover | nan | 0.7168 | 0.7389 | 0.8288 | 1.0841 | 1.1206 | 1.0868 |
| Equity Multiplier | nan | 3.0724 | 3.5633 | 4.2509 | 5.255 | 6.1862 | 6.252 |
| Return on Equity | nan | 0.4936 | 0.5592 | 0.7369 | 1.4744 | 1.7546 | 1.7195 |
And below each component of the Extended Dupont Analysis is plotted including the resulting Return on Equity (ROE).
The models module covers 10+ models in total, for example DuPont Analysis, WACC, Economic Value Added (EVA), Altman Z-Score, Beneish M-Score and the Graham Number.
Obtaining Options and Greeks
Get the Black Scholes Model for both call and put options including the relevant Greeks, in this case Delta, Gamma, Theta and Vega. This can be any of the First, Second or Third Order Greeks.
# Get Delta for all tickers across strikes and expirations
delta = companies.options.get_delta(expiration_time_range=180)
# Select the results for Apple
delta.loc['AAPL']
For example, see the delta of the Call options for Apple for multiple expiration times and strike prices below (Stock Price: 185.92, Volatility: 31.59%, Dividend Yield: 0.49% and Risk Free Rate: 3.95%):
| 1 Month | 2 Months | 3 Months | 4 Months | 5 Months | 6 Months | |
|---|---|---|---|---|---|---|
| 175 | 0.7686 | 0.7178 | 0.6967 | 0.6857 | 0.6794 | 0.6759 |
| 180 | 0.6659 | 0.64 | 0.6318 | 0.629 | 0.6285 | 0.6291 |
| 185 | 0.5522 | 0.5583 | 0.5648 | 0.571 | 0.5767 | 0.5816 |
| 190 | 0.4371 | 0.4762 | 0.4977 | 0.513 | 0.5249 | 0.5342 |
| 195 | 0.3298 | 0.3971 | 0.4324 | 0.4562 | 0.474 | 0.4875 |
Which can also be plotted together with Gamma, Theta and Vega as follows:
The options module is divided into four categories: Option Pricing (Black-Scholes, Binomial Model, Implied Volatility), First-Order Greeks (Delta, Vega, Theta, Rho), Second-Order Greeks (Gamma, Vanna, Charm, Vomma) and Third-Order Greeks (Speed, Zomma, Color, Ultima).
Obtaining Performance Metrics
Get the correlations with the factors as defined by Fama-and-French. These include market, size, value, operating profitability and investment. The beauty of all functionality here is that it can be based on any period as the function accepts the period intraday, weekly, monthly, quarterly and yearly.
# Get the Fama-French factor correlations for all tickers, quarterly
factor_asset_correlations = companies.performance.get_factor_asset_correlations(period="quarterly")
# Select the results for Apple
factor_asset_correlations['AAPL']
For example, this shows the quarterly correlations for Apple:
| Mkt-RF | SMB | HML | RMW | CMA | |
|---|---|---|---|---|---|
| 2022Q2 | 0.9177 | -0.1248 | -0.5077 | -0.3202 | -0.2624 |
| 2022Q3 | 0.8092 | 0.1528 | -0.5046 | -0.1997 | -0.5231 |
| 2022Q4 | 0.8998 | 0.2309 | -0.5968 | -0.1868 | -0.5946 |
| 2023Q1 | 0.7737 | 0.1606 | -0.3775 | -0.228 | -0.5707 |
| 2023Q2 | 0.7416 | -0.1166 | -0.2722 | 0.0093 | -0.4745 |
And below the correlations with each factor are plotted over time for both Apple and Microsoft.
Beyond Beta, CAPM and the Fama-French factors, the performance module covers around 20+ metrics in total, for example Sharpe Ratio, Sortino Ratio, Calmar Ratio, Omega Ratio and the Correlation Matrix. Most of these also support rolling=<n> for a value that evolves through time instead of one number per period.
Obtaining Risk Metrics
Get the Value at Risk for each week. Here, the days within each week are considered for the Value at Risk. This makes it so that you can understand within each period what is the expected Value at Risk (VaR) which can again be any period but also based on distributions such as Historical, Gaussian, Student-t, Cornish-Fisher, or a Peak-over-Threshold Extreme Value Theory (distribution="evt") fit for the tail.
# Get the weekly Value at Risk for all tickers
companies.risk.get_value_at_risk(period="weekly")
| AAPL | MSFT | Benchmark | |
|---|---|---|---|
| 2023-09-25/2023-10-01 | -0.0205 | -0.0133 | -0.0122 |
| 2023-10-02/2023-10-08 | -0.0048 | -0.0206 | -0.0108 |
| 2023-10-09/2023-10-15 | -0.0089 | -0.0092 | -0.0059 |
| 2023-10-16/2023-10-22 | -0.0135 | -0.0124 | -0.0131 |
| 2023-10-23/2023-10-29 | -0.0224 | -0.0293 | -0.0139 |
And below the Value at Risk (VaR) for Apple, Microsoft and the benchmark (S&P 500) are plotted also demonstrating the impact of COVID-19.
Beyond VaR/CVaR/Entropic VaR, the risk module covers around 20+ metrics in total, for example Conditional Drawdown at Risk, Maximum Drawdown Duration, EWMA Volatility and the Hurst Exponent. Most of these support rolling=<n> for a value that evolves through time instead of one number per period.
Obtaining Technical Indicators
Get the Ichimoku Cloud parameters based on the historical market data. This can be any of the 40+ technical indicators within the technicals module.
# Get the Ichimoku Cloud for all tickers
ichimoku_cloud = companies.technicals.get_ichimoku_cloud()
# Select the results for Apple
ichimoku_cloud.xs('AAPL', axis=1, level=1)
For example, see some of the parameters for Apple below:
| Date | Base Line | Conversion Line | Leading Span A | Leading Span B |
|---|---|---|---|---|
| 2023-10-30 | 174.005 | 171.755 | 176.245 | 178.8 |
| 2023-10-31 | 174.005 | 171.755 | 176.37 | 178.8 |
| 2023-11-01 | 174.005 | 170.545 | 176.775 | 178.8 |
| 2023-11-02 | 174.005 | 171.725 | 176.235 | 178.8 |
| 2023-11-03 | 174.005 | 171.725 | 175.558 | 178.8 |
And below the Ichimoku Cloud parameters are plotted for Apple and Microsoft side-by-side.
The 40+ indicators are divided into four categories: Breadth (McClellan Oscillator, Advancers/Decliners, OBV, ADL, Chaikin Oscillator, TRIN, New Highs - New Lows), Momentum (RSI, MACD, Stochastic, Williams %R, Aroon, CCI, ADX and more), Overlap (SMA, EMA, DEMA, TRIX, WMA, Hull MA, VWAP, Parabolic SAR, Pivot Points, Support/Resistance) and Volatility (ATR, Keltner Channels, Bollinger Bands, Donchian Channels, Volatility Cone).
Obtaining Fixed Income Metrics
Get access to the ICE BofA Corporate Bond benchmark indices and a variety of other bond and derivative related valuations within the fixedincome module.
# Get the ICE BofA Effective Yield for each Credit Rating
companies.fixedincome.get_ice_bofa_effective_yield(maturity=False)
For example, see the Effective Yield for the ICE BofA Corporate Bond Index below for each Credit Rating:
| Date | AAA | AA | A | BBB | BB | B | CCC |
|---|---|---|---|---|---|---|---|
| 2024-04-19 | 0.0518 | 0.0532 | 0.0561 | 0.0594 | 0.0678 | 0.0804 | 0.1385 |
| 2024-04-22 | 0.0517 | 0.0532 | 0.056 | 0.0593 | 0.0671 | 0.0793 | 0.1377 |
| 2024-04-23 | 0.0514 | 0.0528 | 0.0556 | 0.0589 | 0.066 | 0.0777 | 0.1364 |
| 2024-04-24 | 0.0518 | 0.0531 | 0.0559 | 0.0592 | 0.0664 | 0.0778 | 0.1361 |
| 2024-04-25 | 0.0524 | 0.0537 | 0.0564 | 0.0598 | 0.0673 | 0.079 | 0.1368 |
And below a variety of Fixed Income metrics are shown all acquired from the Fixed Income module.
Beyond ICE BofA benchmarks, the fixedincome module covers Bond Valuations (Present Value, Macaulay/Modified Duration, Convexity, Yield to Maturity), Derivative Valuations (Black and Bachelier models for Swaptions), Government Bonds (3-month and 10-year yields) and Central Bank rates (Euribor, ECB and Federal Reserve rates incl. SOFR). It can be called via companies.fixedincome or standalone through from financetoolkit import FixedIncome.
Understanding Key Economic Indicators
Get insights for 60+ countries into key economic indicators such as the Consumer Price Index (CPI), Gross Domestic Product (GDP), Unemployment Rates and 3-month and 10-year Government Interest Rates. This is done through the economics module and can be used as a standalone module as well by using from financetoolkit import Economics.
# Get the Unemployment Rate for a selection of countries
companies.economics.get_unemployment_rate()
For example see a selection of the countries below:
| Colombia | United States | Sweden | Japan | Germany | |
|---|---|---|---|---|---|
| 2017 | 0.093 | 0.0435 | 0.0686 | 0.0281 | 0.0357 |
| 2018 | 0.0953 | 0.039 | 0.0648 | 0.0244 | 0.0321 |
| 2019 | 0.1037 | 0.0367 | 0.0691 | 0.0235 | 0.0298 |
| 2020 | 0.1586 | 0.0809 | 0.0848 | 0.0278 | 0.0362 |
| 2021 | 0.1381 | 0.0537 | 0.0889 | 0.0282 | 0.0358 |
| 2022 | 0.1122 | 0.0365 | 0.0748 | 0.026 | 0.0307 |
And below these Unemployment Rates are plotted over time:
The 40+ indicators are divided into five categories: Government (GDP, government debt/revenue/expenditure/deficit, trust in government), Economy (CPI, inflation, consumer/business confidence, house/rent/share prices), Finance (money supply, central bank policy rate, short/long-term interest rates), Environment (renewable energy, carbon footprint) and Jobs & Society (unemployment, labour productivity, income inequality, population, poverty rate).
Explore your own Portfolio
Through a custom XLSX, XLS or CSV file you are able to load in your own portfolio directly into the Finance Toolkit. This allows you to view your positions and performance (over time) versus a benchmark and other positions as well as your PnL development over time. Furthermore, the portfolio can be directly loaded in the core functionality of the Finance Toolkit as well making it possible to calculate all metrics and ratios for your portfolio (which is a time-weighted sum of all positions). The portfolio module is a standalone module and can be used as such by using from financetoolkit import Portfolio.
It is important to note that it requires a specific Excel template to work, see for further instructions the following notebook here.
from financetoolkit import Portfolio
# Initialize the Portfolio module with your own dataset
portfolio = Portfolio(example=True, api_key="FINANCIAL_MODELING_PREP_KEY")
# Get an overview of all positions
portfolio.get_positions_overview()
The table below shows one of the functionalities of the Portfolio module but is purposely shrunken down given the >30 assets.
| Identifier | Volume | Costs | Price | Invested | Latest Price | Latest Value | Return | Return Value | Benchmark Return | Volatility | Benchmark Volatility | Alpha | Beta | Weight |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| AAPL | 137 | -28 | 38.9692 | 5310.78 | 241.84 | 33132.1 | 5.2386 | 27821.3 | 2.2258 | 0.3858 | 0.1937 | 3.0128 | 1.2027 | 0.0405 |
| ALGN | 81 | -34 | 117.365 | 9472.53 | 187.03 | 15149.4 | 0.5993 | 5676.9 | 2.1413 | 0.5985 | 0.1937 | -1.542 | 1.5501 | 0.0185 |
| AMD | 78 | -30 | 11.9075 | 898.784 | 99.86 | 7789.08 | 7.6662 | 6890.3 | 3.7945 | 0.6159 | 0.1937 | 3.8718 | 1.6551 | 0.0095 |
| AMZN | 116 | -28 | 41.5471 | 4791.46 | 212.28 | 24624.5 | 4.1392 | 19833 | 1.8274 | 0.4921 | 0.1937 | 2.3118 | 1.1594 | 0.0301 |
| ASML | 129 | -25 | 33.3184 | 4273.07 | 709.08 | 91471.3 | 20.4065 | 87198.3 | 3.8005 | 0.4524 | 0.1937 | 16.606 | 1.4407 | 0.1119 |
| VOO | 77 | -12 | 238.499 | 18352.5 | 546.33 | 42067.4 | 1.2922 | 23715 | 1.1179 | 0.1699 | 0.1937 | 0.1743 | 0.9973 | 0.0515 |
| WMT | 92 | -18 | 17.8645 | 1625.53 | 98.61 | 9072.12 | 4.581 | 7446.59 | 2.4787 | 0.2334 | 0.1937 | 2.1024 | 0.4948 | 0.0111 |
| Portfolio | 2142 | -532 | 59.8406 | 128710 | 381.689 | 817577 | 5.3521 | 688867 | 2.0773 | 0.4193 | 0.1937 | 3.2747 | 1.2909 | 1 |
In which the weights and returns can be depicted as follows:
Applying Econometric Techniques
The econometrics module provides regression, hypothesis testing, unit root and cointegration, Granger causality and panel data methods built on statsmodels and linearmodels. Unlike the other modules it depends on those two packages, so it ships as an optional extra:
pip install "financetoolkit[econometrics]" -U
# AAPL is the Toolkit's first ticker, so it's the default dependent ticker;
# every other ticker becomes the default independent set
companies.econometrics.get_ols(period="weekly")
Regressing Apple’s returns on a mix of its chip suppliers, megacap peers and two unrelated names (Benchmark excluded) gives:
| Coefficient | Std. Error | t-Statistic | P-Value | |
|---|---|---|---|---|
| Intercept | 0.0028 | 0.0017 | 1.6815 | 0.0943 |
| TSM | -0.0054 | 0.0523 | -0.1028 | 0.9182 |
| QCOM | 0.1432 | 0.0361 | 3.9717 | 0.0001 |
| SWKS | 0.2141 | 0.0484 | 4.4221 | 0.0000 |
| MSFT | 0.3036 | 0.0864 | 3.5144 | 0.0005 |
| GOOGL | 0.1448 | 0.0689 | 2.1015 | 0.0369 |
| AMZN | 0.0617 | 0.0529 | 1.1664 | 0.2448 |
| META | -0.0132 | 0.0389 | -0.3398 | 0.7343 |
| NVDA | -0.0024 | 0.0415 | -0.0575 | 0.9542 |
| XOM | -0.0291 | 0.0373 | -0.7799 | 0.4364 |
| PG | 0.2858 | 0.0707 | 4.0393 | 0.0001 |
Only QCOM, SWKS, MSFT and GOOGL come out statistically significant once every regressor is controlled for at once. The module covers 48 methods in total, including unit root tests (ADF, KPSS, Phillips-Perron, Zivot-Andrews), cointegration and Granger causality, panel data estimators (Fixed/Random Effects, Hausman), causal inference (IV-2SLS, Difference-in-Differences, Regression Discontinuity, Propensity Score Matching, Synthetic Control), time-series forecasting (ARIMA, VAR, VECM, impulse response) and event studies. Find the full econometrics documentation here.
How-To Guides for the FinanceToolkit
Explore the Finance Toolkit through a series of Jupyter Notebooks, each covering a distinct area of financial analysis. Click any card to open the notebook.
Getting Started
New to the Finance Toolkit? This notebook walks through every major feature with practical examples, from fetching historical prices and financial statements to running a complete financial analysis. Start here.
Discovery
Browse companies, cryptocurrencies, forex, ETFs, commodities and indices. Includes screeners, live quotes and performance metrics to help you identify and select the right tickers.
Ratios
80+ financial ratios across five categories: efficiency, liquidity, profitability, solvency and valuation. Every formula is transparently calculated from the underlying financial statements.
Models
Execute well-known financial models: Extended DuPont Analysis, Discounted Cash Flow (DCF), Enterprise Value Breakdown, Altman Z-Score and Weighted Average Cost of Capital (WACC).
Options
Calculate First, Second and Third Order Greeks, run the Black-Scholes model, explore Option Chains with Implied Volatilities and apply Breeden–Litzenberger to derive risk-neutral densities.
Technicals
40+ Technical Indicators across breadth, momentum, overlap and volatility categories. Use alongside fundamental data for a comprehensive view of market behaviour.
Risk
Quantify downside exposure with Value at Risk (VaR), Conditional VaR (cVaR), Maximum Drawdown, Correlation matrices, GARCH and EWMA, essential for any risk-aware investment strategy.
Performance
Evaluate strategies with Sharpe, Sortino and Treynor Ratios, Information Ratio, Jensen's Alpha, Beta and the Capital Asset Pricing Model (CAPM). Understand what truly drives returns.
Econometrics
Test the statistical properties of return series and estimate causal effects: OLS and panel regression, unit root and cointegration tests, Granger causality, ARIMA/VAR forecasting, event studies and synthetic control.
Fixed Income
Analyse bonds with Effective Yield, Macaulay Duration, Modified Duration, Convexity and Yield to Maturity. Includes derivative pricing via Black and Bachelier models for instruments such as Swaptions.
Economics
Key economic indicators for 60+ countries: CPI, GDP, Unemployment Rates and Government Interest Rates at 3-month and 10-year maturities. Works as a standalone notebook as well.
Portfolio
Import your own transaction data to track portfolio performance over time, identify key return drivers and benchmark against an index, directly integrated with live Finance Toolkit data.
External Datasets
Connect the Finance Toolkit to any data provider. Fully compatible with Yahoo Finance, OpenBB, Quandl, EODH and Bloomberg, so you can bring your preferred data without being locked to a single source.
Questions & Answers
This section includes frequently asked questions and is meant to clear up confusion about certain results and/or deviations from other sources. If you have any questions that are not answered here, feel free to reach out to me via the contact details below.
How do you deal with companies that have different fiscal years?
For any financial statement, I make sure to line it up with the corresponding calendar period. For example, Apple’s Q4 2023 relates to July to September of 2023. This corresponds to the calendar period Q3 which is why I normalize Apple’s numbers to Q3 2023 instead. This is done to allow for comparison between companies that have different fiscal years.
Why do the numbers in the financial statements sometimes deviate from the data from FinancialModelingPrep?
When looking at a company such as Hyundai Motor Company (ticker: 005380.KS), you will notice that the financial statements are reported in KRW (South Korean won). As this specific ticker is listed on the Korean Exchange, the historical market data will also be reported in KRW. However, if you use the ticker HYMTF, which is listed on the American OTC market, the historical market data will be reported in USD. To deal with this discrepancy, the end of year or end of quarter exchange rate is retrieved which is used to convert the financial statements to USD. This is done to prevent ratio calculations such as the Free Cash Flow Yield (which is based on the market capitalization) or Price Earnings Ratio (which is based on the stock price) from being incorrect. This can be disabled by setting convert_currency=False in the Toolkit initialization. It is recommended to always use the ticker that is listed on the exchange where the company is based.
How can I get TTM (Trailing Twelve Months) and Growth metrics?
Most functions will have the option to define the trailing parameter. This lets you define the number of periods that you want to use to calculate the trailing metrics. For example, if you want to calculate the trailing 12-month (TTM) Price-to-Earnings Ratio, you can set trailing=4 when you have set quarterly=True in the Toolkit initialization. The same goes for growth metrics which can be calculated by setting growth=True. This will calculate the growth for each period based on the previous period. This also includes a lag parameter in which you can define lagged growth. Furthermore, you can also combine the trailing and growth parameters to get trailing growth. For example, set trailing=4 and growth=True for the Price-to-Earnings Ratio which will then calculate the TTM growth.
How can I save the data periodically so that I don’t have to retrieve it every single time again?
The Toolkit has the option to work with cached data through use_cached_data=True when initializing the Toolkit class. Any data that comes from an external source (financial statements, historical prices, economic indicators, and so on) is then stored in a local SQLite database and reused on the next run. Anything the Toolkit calculates itself is never cached, it is always derived from that data on demand.
The cache keeps track of what it already holds per ticker and per date range, which means changing a parameter does not throw the rest away:
- Repeating the same request retrieves nothing at all.
- Widening the period only retrieves the years that were missing.
- Adding a new ticker to an existing Toolkit only retrieves that ticker.
By default the database lives in your user configuration directory, which is the same one the MCP server uses, so both share a single cache. You can also select a specific location by providing a string to the use_cached_data parameter, which will store the database in the provided folder.
To see what is currently stored, use toolkit.get_cache_contents(). It reports the entries grouped by source and dataset:
from financetoolkit import Toolkit
toolkit = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY", use_cached_data=True)
toolkit.get_balance_sheet_statement()
toolkit.get_cache_contents()
The Finance Toolkit never clears the cache on its own, not even when its own internal structure changes. Removing data is always something you ask for explicitly with toolkit.clear_cache(), and it can be narrowed instead of wholesale:
# Remove only the price history of a single ticker
toolkit.clear_cache(source="FinancialModelingPrep", ticker="AAPL")
# Remove everything retrieved from the OECD
toolkit.clear_cache(source="OECD")
# Remove the entire cache, which has to be confirmed
toolkit.clear_cache(confirm=True)
The source names match the ones used by the enforce_source parameter, so “FinancialModelingPrep” and “YahooFinance” mean the same thing in both places.
What is the “Benchmark” that is automatically obtained when acquiring historical data?
This is related to the benchmark_ticker parameter which is set to “SPY” (S&P 500) by default. This is important when calculating performance metrics such as the Sharpe Ratio or Treynor Ratio that require a market return. This can be disabled by setting benchmark_ticker=None in the Toolkit initialization.
Data collection seems to be slow, what could be the issue?
Generally, it should take less than 15 seconds to retrieve the historical data of 100 tickers. If it takes much longer, this could be due to reaching the API limit (the Starter plan has 250 requests per minute), due to a slow internet connection or due to unoptimized code. As the Finance Toolkit makes use of threading, initializing the Toolkit with a single ticker will result in a slow process. This is because the Toolkit will have to wait for the previous request to finish before it can start the next one. Therefore, it is recommended to initialize the Toolkit with all tickers you want to analyze. If it is taking 10+ minutes consider having a look at this issue that managed to resolve the problem.
Are you part of FinancialModelingPrep?
No, I am not. I’ve merely picked them as the primary data provider given that they have a generous free tier and fair pricing compared to other providers. Therefore, any questions related to the data should go through their contact form. When it comes to any type of ratios, performance metrics, risk metrics, technical indicators or economic indicators, feel free to reach out to me as this is my own work.