BS Daniel
BS Daniel
BS Daniel
The Black-Scholes formula is used to price European call and put options. It is a solution to
the Black-Scholes Partial Differential Equation given below:
rC ( S t , t ) rC s ( S t , t ) S t C t ( S t , t ) 1
2 C ss ( S t , t ) 2 S t2 0
C ( S T , T ) ( S T k ) max(S T k ,0)
Ln
St
r v 2 (T )
k
d1
2
v T
d 2 d1 v T
P( S t , t ) K exp(r (T )) N (d1 ) S t N (d 2 )
Where:
T - Time to maturity
St The current Stock Price
R The risk free interest rate
N(x) The cumulative normal distribution evaluated at x
v- The volatility
K The Strike Price
It is assumed that the stock will not pay dividends during the period.
We successfully implemented the Black-Scholes model in Python using the attached code and
used it to value call and put options. The values generated have been plotted as function of the
strike Price. Further, different graphs with different times to maturity were generated showing
clearly how the solutions converge to a hockey stick as maturity nears. As an aside, we have
also created a code for calculating the Greeks in Black Scholes.
1
The Black Scholes Python code
"""
# The Black Scholes Formula
# CallPutFlag - This is set to 'c' for call option, anything else for put
# S - Stock price
# K - Strike price
# T - Time to maturity
# r - Riskfree interest rate
# d - Dividend yield
# v - Volatility
"""
def BlackScholes(CallPutFlag,S,K,T,r,d,v):
d1 = (log(float(S)/K)+((r-d)+v*v/2.)*T)/(v*sqrt(T))
d2 = d1-v*sqrt(T)
if CallPutFlag=='c':
return S*exp(-d*T)*norm.cdf(d1)-K*exp(-r*T)*norm.cdf(d2)
else:
return K*exp(-r*T)*norm.cdf(-d2)-S*exp(-d*T)*norm.cdf(-d1)
plt.clf()
fig,ax = plt.subplots()
maturity = 0
S = np.linspace(80,120,200)
p = []
for i in S:
p.append(BlackScholes('c', i, 100, 0.005, 0.06, 0, 0.4))
line, = ax.plot(p)
#ax.set_ylim()
2
def update(step):
p = []
for i in S:
p.append(BlackScholes('c', i, 100, step, 0.06, 0, 0.4))
line.set_ydata(p)
def data_gen():
expStop = 0.0005
expStart = 1.5
T = np.linspace(expStop,expStart,200)
m = -log(expStop/expStart)/expStart
for t in T:
yield expStart*exp(-m*t)
plt.show()
"""
# The Black Scholes Formula
# CallPutFlag - This is set to 'c' for call option, anything else for put
# S - Stock price
# K - Strike price
# T - Time to maturity
# r - Riskfree interest rate
# d - Dividend yield
# v - Volatility
"""
def BlackScholes(CallPutFlag,S,K,T,r,d,v):
d1 = (log(float(S)/K)+((r-d)+v*v/2.)*T)/(v*sqrt(T))
d2 = d1-v*sqrt(T)
if CallPutFlag=='c':
return S*exp(-d*T)*norm.cdf(d1)-K*exp(-r*T)*norm.cdf(d2)
else:
return K*exp(-r*T)*norm.cdf(-d2)-S*exp(-d*T)*norm.cdf(-d1)
3
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
plt.clf()
fig,ax = plt.subplots()
maturity = 0
S = np.linspace(80,120,200)
p = []
for i in S:
p.append(BlackScholes('p', i, 100, 0.005, 0.06, 0, 0.4))
line, = ax.plot(p)
#ax.set_ylim()
def update(step):
p = []
for i in S:
p.append(BlackScholes('p', i, 100, step, 0.06, 0, 0.4))
line.set_ydata(p)
def data_gen():
expStop = 0.0005
expStart = 1.5
T = np.linspace(expStop,expStart,200)
m = -log(expStop/expStart)/expStart
for t in T:
yield expStart*exp(-m*t)
plt.show()
4
The Greeks
5
from scipy.stats import norm
from math import *