Black-Scholes – Python Code

This is a simple Python code for calculating the price of a European call or put option using the Black-Scholes-Merton closed-form solution. The inputs can be easily modified in the corresponding block (initial stock price, strike price, annual volatility, time, risk-free rate and dividend yield).

## European call / put option

import numpy as np
from scipy.stats import norm

# inputs
S = 10 # stock price
K = 10 # strike price
sigma = 0.2 # volatility
t = 1.5 # number of years
r = 0.02 # risk-free rate
d = 0 # expected dividend yield

# calculate d1 & d2
d1 = (np.log(S/K) + (r - d + 0.5*sigma ** 2)*t) / (sigma * np.sqrt(t))
d2 = d1 - sigma * np.sqrt(t)

# calculate call price
c = S * norm.cdf(d1) * np.exp(-d * t) - K * norm.cdf(d2) * np.exp(-r * t)

# calculate put price
p = K * norm.cdf(-d2) * np.exp(-r * t) - S * norm.cdf(-d1) * np.exp(-d * t)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s