Skip to content

FinanceToolkit v1.8.3

Compare
Choose a tag to compare
@JerBouma JerBouma released this 04 Feb 12:39
· 169 commits to main since this release

This release includes the Binomial Model, a mathematical model for pricing both American as well as European options. The Binomial Model is a discrete-time model that calculates the price of an option by creating a riskless hedge portfolio that replicates the payoff of the option. The model is based on the assumption that the price of the underlying asset follows a binomial distribution. The Binomial Model is a simple and intuitive model that is widely used in practice. It is also the basis for more complex models. See for an elaborate explanation the documentation as found here.

Binomial Tree

For example, when using the following code:

from financetoolkit import Toolkit

companies = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY")

companies.options.get_binomial_model(show_input_info=True)

It returns a large DataFrame with the binomial tree for each company and each strike price around the current price (as defined by the start_date parameter).

image

The resulting output is a DataFrame containing the tickers, strike prices and movements as the index and the time to expiration as the columns. The movements index contains the number of up movements and the number of down movements. The output is the binomial tree displayed in a table. E.g. when using 10 time steps, the table for each strike price from each company will contain the actual binomial tree as also depicted in the image as seen below. Find the documentation here.

When selecting for example Apple at a Strike Price of 140 you will get the actual Binomial Tree depicted as a table, this represents the tree you see in the image at the top.

Movement 2024-02-02 2024-03-09 2024-04-15 2024-05-21 2024-06-27 2024-08-02 2024-09-08 2024-10-14 2024-11-20 2024-12-26 2025-02-01
UUUUUUUUUU 54.7747 69.9327 87.4757 107.31 129.344 153.573 180.122 209.208 241.069 275.965 314.18
UUUUUUUUUD nan 39.9569 52.8423 68.2288 86.0206 106.037 128.14 152.365 178.911 207.994 239.852
UUUUUUUUDD nan nan 27.3011 37.7763 50.8718 66.5774 84.6651 104.825 126.925 151.146 177.689
UUUUUUUDDD nan nan nan 16.9659 24.8886 35.4656 48.9066 65.0645 83.4462 103.602 125.698
UUUUUUDDDD nan nan nan nan 9.1158 14.4288 22.208 33.0259 47.083 63.8384 82.2161
UUUUUDDDDD nan nan nan nan nan 3.8311 6.7007 11.4806 19.124 30.5822 45.85
UUUUDDDDDD nan nan nan nan nan nan 0.9669 1.9327 3.8631 7.722 15.4353
UUUDDDDDDD nan nan nan nan nan nan nan 0 0 0 0
UUDDDDDDDD nan nan nan nan nan nan nan nan 0 0 0
UDDDDDDDDD nan nan nan nan nan nan nan nan nan 0 0
DDDDDDDDDD nan nan nan nan nan nan nan nan nan nan 0

The model contains parameters to lengthen the time steps, change the risk-free rate and the dividend yield but more importantly, make it possible to calculate the price of both American and European options as well as Call and Put options. For example, let's calculate the price of a American Put option with a strike price of 140 for Apple again:

from financetoolkit import Toolkit

companies = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY")

companies.options.get_binomial_model(
    show_input_info=True,
    put_option=True,
    american_option=True,
    timesteps=12,
    risk_free_rate=0.01)

Which returns the Option valuations for an American Put Option.

Movement 2024-02-02 2024-03-03 2024-04-02 2024-05-03 2024-06-02 2024-07-03 2024-08-02 2024-09-01 2024-10-02 2024-11-01 2024-12-02 2025-01-01 2025-02-01
UUUUUUUUUUUU 2.3581 1.1115 0.4236 0.116 0.0171 0 0 0 0 0 0 0 0
UUUUUUUUUUUD nan 3.7011 1.8524 0.7546 0.2225 0.0355 0 0 0 0 0 0 0
UUUUUUUUUUDD nan nan 5.6933 3.0346 1.3274 0.4238 0.0736 0 0 0 0 0 0
UUUUUUUUUDDD nan nan nan 8.5588 4.8737 2.3002 0.8005 0.1529 0 0 0 0 0
UUUUUUUUDDDD nan nan nan nan 12.5322 7.6463 3.9148 1.4975 0.3173 0 0 0 0
UUUUUUUDDDDD nan nan nan nan nan 17.8023 11.6676 6.518 2.7676 0.6586 0 0 0
UUUUUUDDDDDD nan nan nan nan nan nan 24.4233 17.2193 10.5575 5.0375 1.3671 0 0
UUUUUDDDDDDD nan nan nan nan nan nan nan 32.2051 24.4052 16.5049 8.9884 2.8376 0
UUUUDDDDDDDD nan nan nan nan nan nan nan nan 40.6414 32.9347 24.6074 15.6102 5.89
UUUDDDDDDDDD nan nan nan nan nan nan nan nan nan 48.9936 41.9389 34.3151 26.0772
UUDDDDDDDDDD nan nan nan nan nan nan nan nan nan nan 56.6615 50.2044 43.2257
UDDDDDDDDDDD nan nan nan nan nan nan nan nan nan nan nan 63.702 57.7929
DDDDDDDDDDDD nan nan nan nan nan nan nan nan nan nan nan nan 70.1673

Under the hood of this model the stock prices are simulated based on up and down movements. These can be graphically depicted as a binomial tree and help in understanding the calculated option prices for each node in. More information about these stock price simulations can be found in the documentation here and as follows:

from financetoolkit import Toolkit

companies = Toolkit(["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY")

companies.options.get_stock_price_simulations(show_input_info=True, timesteps=10)

Which would return for Apple the following graph when plotted:

image

Given that the Finance Toolkit is modular, you do not have to use the Toolkit functionality directly and can also call each functionality of the model separately. For example, this shows the output of using the model directly, specifying each parameter yourself.

image