# Black-Scholes Option Pricing in Python

The Black-Scholes or Black-Scholes-Merton model is a financial mathematical equation for pricing options contracts and other derivatives.

Fischer Black and Myron Scholes published the formula in their 1973 paper “The Pricing of Options and Corporate Liabilities“. Robert C. Merton published a paper expanding on the mathematical understanding of the options pricing model and devised the term “Black-Scholes options pricing model”.

This tutorial will go through how to write the Black-Scholes options pricing formula from scratch in Python and how to use the formula to price an option.

You can use our free Black-Scholes option pricing calculator to estimate the fair value of a European call or put option.

## Formula

Using the Black-Scholes model, the price of a European call option (only one time you can exercise the option) is calculated using the formula:

$$C= S_{0}N(d_{1})~- Xe^{-r\tau}N(d_{2})$$

Where:

• C = price of the call option
• S0 = price of the underlying stock
• X = option exercise price
• r = risk-free interest rate
• 𝜏 = time until expiration (maturity)

N represents the cumulative distribution function for a normal (Gaussian) distribution, which we can understand as “the probability that a random variable is less or equal to its input for a normal distribution”.

$$d_{1} = \frac{\text{ln}\frac{S_{0}}{X} + (r + \sigma^{2}/2)*\tau}{\sigma\sqrt{\tau}}, \qquad d_{2} = \frac{\text{ln}\frac{S_{0}}{X} + (r – \sigma^{2}/2)*\tau}{\sigma\sqrt{\tau}} = d_{1} – \sigma\sqrt{\tau}$$

Where 𝞂 is the volatility of the underlying asset. For further reading on volatility, go to the article How to Calculate Implied Volatility in Python.

We can think of the two terms in the sum as the current price in the stock weighted by the probability of exercising the option to buy the stock minus the discounted price of exercising the option weighted by the probability of exercising the option. In other words, what you will pay minus what you will receive (Introduction to the Black-Scholes formula, Khan Academy 2013).

The price of a put option is:

$$P= ~Xe^{-r\tau}N(-d_{2}) ~- S_{0}N(-d_{1})$$

You can use our free Black-Scholes option pricing calculator to estimate the fair value of a European call or put option.

## Black-Scholes Assumptions

The Black-Scholes model holds the following assumptions:

• The option is European and can only be exercised at expiration
• The returns of the underlying asset are normally distributed
• Markets are random, in that market movements cannot be predicted
• No dividends are paid out during the life of the option
• No transaction costs for buying the option
• Risk-free interest rate and volatility of the underlying asset are known and constant

The original model does not account for dividends paid during the lifetime of the option, but there are versions of the model that account for dividends by determining the ex-dividend date value of the underlying asset. The model has also been adapted for the effect of options that can be exercised before expiration.

## Black-Scholes Option Pricing using Python from scratch

We can write a function to calculate the option price once we have the values for the parameters:

• S = price of the underlying stock
• K = option exercise price
• r = risk-free interest rate
• t = time until expiration (maturity)
• sigma = volatility of the underlying asset
from math import log, sqrt, pi, exp
from scipy.stats import norm
import numpy as np
import pandas as pd
from pandas import DataFrame

# Function for calculating d1

def d1(S,K,T,r,sig):
return(log(S/K)+(r+sigma**2/2.)*T)/(sigma*sqrt(T))

# Function for calculating d2

def d2(S,K,T,r,sigma):
return d1(S,K,T,r,sigma)-sigma*sqrt(T)

# Function for pricing a call option

def bs_call(S,K,T,r,sigma):
return S*norm.cdf(d1(S,K,T,r,sigma))-K*exp(-r*T)*norm.cdf(d2(S,K,T,r,sigma))

# Function for pricing a put option

def bs_put(S,K,T,r,sigma):
return K*exp(-r*T)*norm.cdf(-1 * d2(S,K,T,r,sigma)) - S*norm.cdf(-1*d1(S,K,T,r,sigma))

Let’s call the function and pass the five parameters to price a call option.

if __name__ == '__main__':

# Price call option using Black-Scholes function

call = bs_call(110, 100, 1, 0.04, 0.2)

print(f'Price of option is: ${round(call,2)}') Price of option is:$16.97

## Summary

Congratulations on reading to the end of this tutorial!

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

To learn how to use Black-Scholes in R and C++, go to the articles:

For further reading on the Options Greeks, go to the article:

You can use our free Black-Scholes option pricing calculator to estimate the fair value of a European call or put option.

You can use our free Implied Volatility calculator to estimate the Implied Volatility of a priced option.

Have fun and happy researching!