The FinanceToolkit is an open-source toolkit in which all relevant financial ratios (100+), indicators and performance measurements are written down in the most simplistic way allowing for complete transparency of the calculation method. This allows you to not have to rely on metrics from other providers and, given a financial statement, allow for efficient manual calculations. This leads to one uniform method of calculation being applied that is available and understood by everyone.
To install the FinanceToolkit it simply requires the following:
pip install financetoolkit -U
From within Python use:
from financetoolkit import Toolkit
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.
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. When you notice that 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.
The current Notebook is revolved around the Techniclas class. If you are interested in the other modules, you can find the related Notebooks below. Please view the documentation here to find all the available indicators.
import pandas as pd
from financetoolkit import Toolkit
API_KEY = "FINANCIAL_MODELING_PREP_API_KEY"
Initializing only is required once. This is the case for any function so once you have obtained a balance sheet statement, it will be stored accordingly which means that requests to FinancialModelingPrep, the source used in these examples, are kept to a minimum. Note that in this example annual data is used but by adding quarterly=True
to the Toolkit initialization, quarterly data can also be collected. Note that this requires a Premium subscription from FMP.
# Initialize the Toolkit with company tickers
companies = Toolkit(
["MSFT", "ASML", "MU", "AMZN"],
api_key=API_KEY,
start_date="2023-01-01",
end_date="2024-01-01",
)
After initialization of Toolkit
, you can get access to the Technicals module which includes a variety of well-known technical indicators. This can be done by calling the technicals
property. Some of the technical indicators you can find here are for example a collection of all technical indicators.
companies.technicals.collect_all_indicators()
Obtaining historical data: 100%|██████████| 5/5 [00:00<00:00, 9.49it/s]
McClellan Oscillator | Advancers - Decliners | ... | Keltner Channel Middle | Keltner Channel Lower | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
MSFT | ASML | MU | AMZN | Benchmark | MSFT | ASML | MU | AMZN | Benchmark | ... | AMZN | ASML | Benchmark | MSFT | MU | AMZN | ASML | Benchmark | MSFT | MU | |
Date | |||||||||||||||||||||
2023-01-03 | -0.4868 | 6.9243 | -1.4919 | -4.7495 | -1.0564 | 237.47 | 544.14 | 50.02 | 85.82 | 375.12 | ... | 85.9672 | 555.6601 | 378.582 | 239.4149 | 50.8278 | 79.8772 | 516.8473 | 359.8078 | 225.7049 | 47.2021 |
2023-01-04 | -1.1123 | 6.8877 | -1.2982 | -4.6069 | -1.094 | 227.09 | 565.19 | 53.83 | 85.14 | 378.01 | ... | 85.8569 | 556.9308 | 378.5058 | 237.7716 | 51.2281 | 80.0683 | 519.2651 | 360.9529 | 224.3644 | 47.4224 |
2023-01-05 | -1.9781 | 6.5495 | -1.1011 | -4.5629 | -1.3362 | 220.36 | 559.74 | 54.33 | 83.12 | 373.7 | ... | 85.492 | 557.3053 | 377.865 | 235.45 | 51.6417 | 79.8805 | 520.4725 | 361.3879 | 222.3986 | 47.9045 |
2023-01-06 | -2.579 | 7.7386 | -0.8246 | -4.3545 | -1.1082 | 222.95 | 589.96 | 56.38 | 86.08 | 382.27 | ... | 85.5704 | 561.6593 | 378.4523 | 233.7834 | 52.2735 | 79.8361 | 522.9093 | 360.8052 | 220.9048 | 48.3906 |
2023-01-09 | -2.9713 | 9.9781 | -0.6046 | -4.0905 | -0.9186 | 225.12 | 615.19 | 55.97 | 87.36 | 382.05 | ... | 85.809 | 568.7967 | 378.932 | 232.6283 | 52.7663 | 79.9633 | 526.8439 | 360.4434 | 219.4697 | 48.7563 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2023-12-22 | 6.4246 | 27.985 | 2.7915 | 4.4264 | 9.1062 | 374.58 | 752.53 | 86.37 | 153.42 | 473.65 | ... | 150.2291 | 729.5707 | 466.2563 | 372.3543 | 80.3988 | 143.5812 | 702.3497 | 456.8944 | 360.8239 | 75.6059 |
2023-12-26 | 6.2577 | 28.6891 | 3.0298 | 4.4236 | 9.2682 | 374.66 | 762.68 | 86.94 | 153.41 | 475.65 | ... | 150.6532 | 733.9853 | 467.5088 | 372.6618 | 81.271 | 144.6475 | 705.865 | 458.1197 | 361.7048 | 76.4281 |
2023-12-27 | 6.0542 | 29.2152 | 3.1983 | 4.3956 | 9.4034 | 374.07 | 764.03 | 86.54 | 153.34 | 476.51 | ... | 151.0115 | 737.9913 | 468.709 | 372.8495 | 81.9735 | 145.2786 | 710.7167 | 459.7142 | 362.4499 | 77.3021 |
2023-12-28 | 5.9104 | 29.2099 | 3.2934 | 4.3517 | 9.481 | 375.28 | 757.85 | 85.88 | 153.38 | 476.69 | ... | 151.3273 | 740.6391 | 469.7731 | 373.1736 | 82.4944 | 145.9187 | 713.6767 | 461.4483 | 363.1661 | 77.8672 |
2023-12-29 | 5.796 | 29.0128 | 3.3313 | 4.2204 | 9.4309 | 376.04 | 756.92 | 85.34 | 151.94 | 475.31 | ... | 151.409 | 742.8099 | 470.5114 | 373.5558 | 82.8738 | 145.9404 | 715.9589 | 462.278 | 363.9126 | 78.3618 |
250 rows × 200 columns
Individual indicators are also available such as the Bollinger Bands and plot accordingly.
bollinger_bands = companies.technicals.get_bollinger_bands()
display(bollinger_bands)
bollinger_bands.xs("MSFT", level=1, axis=1).plot(
figsize=(15, 3),
title="Microsoft's Bollinger Bands",
grid=True,
legend=True,
colormap="seismic",
)
Close | Lower Band | Middle Band | Upper Band | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
AMZN | ASML | Benchmark | MSFT | MU | AMZN | ASML | Benchmark | MSFT | MU | AMZN | ASML | Benchmark | MSFT | MU | AMZN | ASML | Benchmark | MSFT | MU | |
Date | ||||||||||||||||||||
2023-01-03 | 85.82 | 544.14 | 375.12 | 237.47 | 50.02 | 79.9044 | 505.9969 | 365.7281 | 227.267 | 47.2349 | 86.0829 | 561.6179 | 378.8129 | 240.9586 | 50.7836 | 92.2613 | 617.2388 | 391.8976 | 254.6501 | 54.3322 |
2023-01-04 | 85.14 | 565.19 | 378.01 | 227.09 | 53.83 | 80.5952 | 513.9386 | 367.9959 | 225.8679 | 47.4606 | 85.5579 | 557.5557 | 377.6607 | 238.9893 | 50.7186 | 90.5205 | 601.1729 | 387.3255 | 252.1107 | 53.9765 |
2023-01-05 | 83.12 | 559.74 | 373.7 | 220.36 | 54.33 | 81.2488 | 522.2057 | 370.7933 | 223.3208 | 47.3998 | 84.9536 | 553.9814 | 376.3807 | 236.5179 | 50.7357 | 88.6583 | 585.7572 | 381.9682 | 249.7149 | 54.0717 |
2023-01-06 | 86.08 | 589.96 | 382.27 | 222.95 | 56.38 | 81.5858 | 520.1739 | 370.7371 | 221.1884 | 46.5843 | 84.7843 | 554.6536 | 376.3971 | 234.8129 | 51.08 | 87.9828 | 589.1333 | 382.0572 | 248.4373 | 55.5757 |
2023-01-09 | 87.36 | 615.19 | 382.05 | 225.12 | 55.97 | 81.6899 | 510.9826 | 370.315 | 219.8029 | 46.1902 | 84.7486 | 557.9779 | 376.72 | 233.5686 | 51.3929 | 87.8073 | 604.9731 | 383.125 | 247.3342 | 56.5955 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2023-12-22 | 153.42 | 752.53 | 473.65 | 374.58 | 86.37 | 142.781 | 678.3379 | 450.1339 | 367.0362 | 70.529 | 149.4671 | 727.1171 | 465.3 | 371.99 | 79.1193 | 156.1533 | 775.8964 | 480.4661 | 376.9438 | 87.7096 |
2023-12-26 | 153.41 | 762.68 | 475.65 | 374.66 | 86.94 | 143.1151 | 683.6142 | 451.9868 | 366.9905 | 71.1685 | 149.9336 | 731.985 | 466.7921 | 372.1429 | 80.0729 | 156.7521 | 780.3558 | 481.5975 | 377.2952 | 88.9773 |
2023-12-27 | 153.34 | 764.03 | 476.51 | 374.07 | 86.54 | 144.2918 | 691.8258 | 455.18 | 367.6571 | 72.5007 | 150.5636 | 737.1243 | 468.4771 | 372.5193 | 81.0379 | 156.8353 | 782.4228 | 481.7743 | 377.3815 | 89.5751 |
2023-12-28 | 153.38 | 757.85 | 476.69 | 375.28 | 85.88 | 144.9721 | 700.3232 | 457.9497 | 367.847 | 74.2046 | 151.0279 | 741.2814 | 469.9279 | 372.8286 | 81.9186 | 157.0836 | 782.2397 | 481.906 | 377.8101 | 89.6325 |
2023-12-29 | 151.94 | 756.92 | 475.31 | 376.04 | 85.34 | 145.6521 | 713.1454 | 460.9056 | 367.7317 | 75.932 | 151.3507 | 745.6021 | 471.14 | 372.9579 | 82.6671 | 157.0493 | 778.0589 | 481.3744 | 378.184 | 89.4023 |
250 rows × 20 columns
<Axes: title={'center': "Microsoft's Bollinger Bands"}, xlabel='Date'>
Each functionality also has the ability to change parameters. As an example, the window for the Average Directional Index can be changed and the growth and lag can also be defined.
companies.technicals.get_average_directional_index(
window=20, growth=True, lag=[1, 3, 5, 10]
)
MSFT | ASML | MU | AMZN | Benchmark | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Lag 1 | Lag 3 | Lag 5 | Lag 10 | Lag 1 | Lag 3 | Lag 5 | Lag 10 | Lag 1 | Lag 3 | Lag 5 | Lag 10 | Lag 1 | Lag 3 | Lag 5 | Lag 10 | Lag 1 | Lag 3 | Lag 5 | Lag 10 | |
Date | ||||||||||||||||||||
2023-01-03 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2023-01-04 | -0.0982 | NaN | NaN | NaN | -0.1167 | NaN | NaN | NaN | -0.0152 | NaN | NaN | NaN | -0.0006 | NaN | NaN | NaN | -0.0334 | NaN | NaN | NaN |
2023-01-05 | -0.0563 | NaN | NaN | NaN | -0.1236 | NaN | NaN | NaN | -0.0071 | NaN | NaN | NaN | -0.0032 | NaN | NaN | NaN | -0.0175 | NaN | NaN | NaN |
2023-01-06 | -0.031 | -0.1754 | NaN | NaN | -0.107 | -0.3087 | NaN | NaN | 0.002 | -0.0203 | NaN | NaN | -0.0048 | -0.0087 | NaN | NaN | -0.0421 | -0.0902 | NaN | NaN |
2023-01-09 | -0.0725 | -0.1519 | NaN | NaN | -0.0851 | -0.2839 | NaN | NaN | 0.0082 | 0.003 | NaN | NaN | -0.0394 | -0.0472 | NaN | NaN | -0.0811 | -0.1351 | NaN | NaN |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2023-12-22 | -0.1182 | -0.237 | -0.3291 | -0.4433 | -0.0377 | -0.0996 | -0.1484 | -0.1191 | -0.0525 | -0.1232 | -0.1703 | -0.208 | -0.0701 | -0.1032 | -0.1387 | -0.2751 | -0.0219 | -0.0337 | 0.0002 | 0.2748 |
2023-12-26 | -0.132 | -0.2857 | -0.3828 | -0.5028 | -0.0283 | -0.0991 | -0.154 | -0.1578 | -0.0601 | -0.1435 | -0.2039 | -0.2562 | -0.0724 | -0.1813 | -0.1916 | -0.3057 | -0.0244 | -0.0566 | -0.0456 | 0.1663 |
2023-12-27 | -0.1397 | -0.3415 | -0.4302 | -0.5622 | -0.0061 | -0.0707 | -0.1304 | -0.1789 | -0.0064 | -0.1151 | -0.1812 | -0.2579 | -0.0238 | -0.1579 | -0.1879 | -0.3041 | -0.022 | -0.0667 | -0.078 | 0.0732 |
2023-12-28 | -0.1614 | -0.3738 | -0.4847 | -0.6205 | -0.0117 | -0.0456 | -0.1152 | -0.1911 | -0.0271 | -0.0914 | -0.1721 | -0.2654 | -0.0391 | -0.1298 | -0.2321 | -0.2883 | -0.0235 | -0.0683 | -0.0991 | -0.0002 |
2023-12-29 | -0.1784 | -0.4073 | -0.5463 | -0.673 | 0.0038 | -0.014 | -0.0781 | -0.1846 | 0.0017 | -0.0317 | -0.1376 | -0.2563 | -0.0063 | -0.0678 | -0.1959 | -0.2573 | -0.0261 | -0.0699 | -0.1125 | -0.0645 |
250 rows × 20 columns
Furthermore, the technical indicators can be plotted in a single figure with relative ease.
# Obtain the Relative Strength Index (RSI) for each company
rsi = companies.technicals.get_relative_strength_index()
# Show the RSI for each company
display(rsi)
# Plot the RSI for each company
rsi.plot(
subplots=True,
figsize=(15, 5),
title="Relative Strength Index for Microsoft, ASML, Amazon and Micron Technologies",
legend=False,
grid=True,
ylim=(0, 100),
sharex=True,
)
MSFT | ASML | MU | AMZN | Benchmark | |
---|---|---|---|---|---|
Date | |||||
2023-01-03 | 35.4119 | 33.0826 | 28.4821 | 39.9746 | 34.5279 |
2023-01-04 | 22.4024 | 34.1808 | 46.9484 | 33.5423 | 34.4395 |
2023-01-05 | 19.3154 | 35.5309 | 50.813 | 31.9539 | 33.2898 |
2023-01-06 | 26.5198 | 52.7054 | 66.8768 | 44.9076 | 50.2186 |
2023-01-09 | 32.1223 | 62.4279 | 64.9386 | 48.9566 | 54.6772 |
... | ... | ... | ... | ... | ... |
2023-12-22 | 56.6471 | 73.4666 | 76.4553 | 67.9198 | 75.1606 |
2023-12-26 | 52.8442 | 74.6047 | 79.0344 | 64.9019 | 76.5979 |
2023-12-27 | 57.6399 | 76.1846 | 79.5883 | 72.4771 | 80.8044 |
2023-12-28 | 56.4531 | 71.3971 | 76.8394 | 68.7861 | 79.006 |
2023-12-29 | 52.9165 | 72.6198 | 73.6036 | 62.4176 | 74.6442 |
250 rows × 5 columns
array([<Axes: xlabel='Date'>, <Axes: xlabel='Date'>, <Axes: xlabel='Date'>, <Axes: xlabel='Date'>, <Axes: xlabel='Date'>], dtype=object)
Given that the models of the Technicals module are very flexible and merely contain the calculation, it is possible to directly use these models instead of the Toolkit as well given that you have your own data or are looking for specific alterations.
from financetoolkit.technicals import momentum_model, volatility_model
display(
momentum_model.get_ichimoku_cloud(
prices_low=pd.Series(range(70, 140, 1)),
prices_high=pd.Series(range(90, 160, 1)),
conversion_window=9,
base_window=20,
lead_span_b_window=40,
)
)
display(
volatility_model.get_bollinger_bands(
prices=pd.Series([100, 105, 113, 126, 148]),
window=2,
num_std_dev=2,
)
)
Conversion Line | Base Line | Leading Span A | Leading Span B | |
---|---|---|---|---|
0 | NaN | NaN | NaN | NaN |
1 | NaN | NaN | NaN | NaN |
2 | NaN | NaN | NaN | NaN |
3 | NaN | NaN | NaN | NaN |
4 | NaN | NaN | NaN | NaN |
... | ... | ... | ... | ... |
65 | 141.0 | 135.5 | 129.25 | 116.5 |
66 | 142.0 | 136.5 | 130.25 | 117.5 |
67 | 143.0 | 137.5 | 131.25 | 118.5 |
68 | 144.0 | 138.5 | 132.25 | 119.5 |
69 | 145.0 | 139.5 | 133.25 | 120.5 |
70 rows × 4 columns
Upper Band | Middle Band | Lower Band | Close | |
---|---|---|---|---|
0 | NaN | NaN | NaN | 100 |
1 | 109.57106781186548 | 102.5 | 95.42893218813452 | 105 |
2 | 120.31370849898477 | 109.0 | 97.68629150101523 | 113 |
3 | 137.88477631085024 | 119.5 | 101.11522368914976 | 126 |
4 | 168.1126983722081 | 137.0 | 105.88730162779191 | 148 |