Black Scholes Model in Python

KoshurAI
2 min readJan 7, 2023

--

The Black-Scholes model is a financial model used to calculate the fair price or theoretical value for a European call or put option, using assumptions of constant volatility, no dividends, and efficient markets. Here is a simple implementation of the Black-Scholes formula in Python:

import math

def black_scholes(S, K, T, r, sigma, option='call'):
d1 = (math.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * math.sqrt(T))
d2 = (math.log(S / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * math.sqrt(T))

if option == 'call':
return S * math.norm.cdf(d1) - K * math.exp(-r * T) * math.norm.cdf(d2)
if option == 'put':
return K * math.exp(-r * T) * math.norm.cdf(-d2) - S * math.norm.cdf(-d1)

This function takes in the following six parameters:

  • S: the spot price of the underlying asset
  • K: the strike price of the option
  • T: the time to expiration in years
  • r: the risk-free interest rate
  • sigma: the volatility of the underlying asset
  • option: the type of option, either 'call' or 'put' (default is 'call')

It returns the theoretical price of the option.

To use this function, you will need to import the math module. You can then call black_scholes with the appropriate parameters for your option. For example:

S = 100
K = 105
T = 0.5
r = 0.05
sigma = 0.2

call_price = black_scholes(S, K, T, r, sigma, option='call')
put_price = black_scholes(S, K, T, r, sigma, option='put')

print(f"Call price: {call_price:.2f}")
print(f"Put price: {put_price:.2f}")

This will print out the theoretical price of a European call option and a European put option with the given parameters.

--

--

KoshurAI
KoshurAI

Written by KoshurAI

Passionate about Data Science? I offer personalized data science training and mentorship. Join my course today to unlock your true potential in Data Science.

Responses (1)