# Black-Scholes Model: A Python Guide for Option Pricing

Options are among the most actively traded derivatives in financial markets. Experienced traders employ various models to determine the value of an option chain. Given the volatility of stocks and associated trading costs, accurately pricing an option chain is crucial. The Black Scholes model stands out as a reliable method for this purpose.

Options are essentially derivatives, which derive their value from another underlying asset. There are two types of Options: call and put options. We will use the Black Scholes model to value these options.

The Black-Scholes model is a pivotal tool for pricing European options, integrating variables like strike price, underlying asset’s current price, volatility, time until expiration, and risk-free interest rate to calculate precise option values. This model’s reliability makes it a staple in trading strategies, providing a clear framework for evaluating potential investments.

Recommended: Stochastic Indicator: Python Implementation

Recommended: (3/5) Relative Strength Index (RSI): A Powerful Trading Indicator Implemented in Python

## Exploring the Black-Scholes Model

The Black Scholes Model considers many factors to value a stock’s option chain. Please note that it only applies to a European option where the expiry date is considered and not an American option.

The Black Scholes model takes into account various factors like:

1. Strike Price: It is the price at which we can call or put a certain stock. It is the price at a certain point in the future and is different from Spot Price.
2. Underlying price of asset: It is the current price or price at the moment of a stock. It is also known as the Spot price.
3. Volatility: Volatility is the uncertainty or the standard deviation of a stock price. It is calculated using the historical data of the stock.
4. Time of expiration: Time of expiration is the time after which the option chain is not valid.
5. Risk-free interest rate: In general, a risk-free interest rate is the interest rate that the government of a country offers on its bond.

Let us now look at the mathematical formulation of the Black Scholes Model. We will then discuss the Black Scholes pricing formula for a call option.

From the above formula, we will calculate d2 and d1. These components are calculated by putting in the current stock price, exercise price, risk-free rate, maturity date, etc. Thereafter, we put the values of d2 and d1 in the call option price formula, as mentioned in the formula sheet above.

## Coding the Black-Scholes Model in Python

Let us now look at how to implement this model in the Python programming language. We will try to implement the same formula as mentioned in the previous section.

```import numpy as np
from scipy.stats import norm
```

We will import Numpy and Scipy libraries of Python programming language to help us calculate the option chain pricing.

```def black_scholes(spot_price, strike_price, risk_free_rate, time_to_expiry, volatility, option_type="call"):
"""
This function calculates the Black-Scholes option price.

Args:
spot_price (float): The current price of the underlying asset.
strike_price (float): The strike price of the option.
risk_free_rate (float): The risk-free interest rate.
time_to_expiry (float): The time to expiry of the option in years.
volatility (float): The implied volatility of the underlying asset.
option_type (str, optional): The type of option ("call" or "put"). Defaults to "call".
```

We consider different arguments for obtaining the value of our Options: spot price, strike price, risk-free rate, time to expiry, etc. We have put all the above parameters as arguments in the block of code above.

```  Returns:
float: The Black-Scholes option price.
"""

d1 = (np.log(spot_price / strike_price) + (risk_free_rate + volatility**2 / 2) * time_to_expiry) / (volatility * np.sqrt(time_to_expiry))
d2 = d1 - volatility * np.sqrt(time_to_expiry)

if option_type == "call":
price = spot_price * norm.cdf(d1) - strike_price * np.exp(-risk_free_rate * time_to_expiry) * norm.cdf(d2)
elif option_type == "put":
price = strike_price * np.exp(-risk_free_rate * time_to_expiry) * norm.cdf(-d2) - spot_price * norm.cdf(-d1)
else:
raise ValueError("Invalid option type. Please use 'call' or 'put'.")

return price

# Example usage
spot_price = 50
strike_price = 55
risk_free_rate = 0.05
time_to_expiry = 1
volatility = 0.2

call_price = black_scholes(spot_price, strike_price, risk_free_rate, time_to_expiry, volatility)
put_price = black_scholes(spot_price, strike_price, risk_free_rate, time_to_expiry, volatility, option_type="put")

print("Call price:", call_price)
print("Put price:", put_price)

```

We have built a user-defined function that considers the above factors as parameters. As mentioned in the formula, we then calculate d1 and d2. We can get all this data from the CSV or Excel file from Yahoo Finance.

We then applied the mathematical formulation in Python, passing the values of parameters like Spot_price, Strike_price, etc. Finally, we printed the output. Let us now look at the output of the above code.

## Conclusion

You now have a deeper understanding of the Black Scholes model. This guide explored its formula and practical implementation using Python. Are you prepared to leverage this model for more accurate trading predictions in your financial strategies?

Recommended: (2/5) Moving Average Crossover Strategy: Python Implementation

Recommended: (1/5) Understanding Technical Analysis and Indicators using Python