Random

# Pricing options with python.

## Vanilla European puts/calls

The black-scholes formula is a must-know differential equation in financial mathematics. It is used for various purposes to price financial derivatives. In this post we will price vanilla European puts/calls.

The differential equation is given by:

where:

• V –> price of the derivative
• S –> price of the underlying asset
• r –> risk-free interest rate
• σ –> asset volatility of the underlying S

The pricing formula:

Depending on the derivative being priced, different conditions will apply, and consequently the equation will have different solutions.

The price of a European Vanilla Call, C(S,t) is given below:

With d1 and d2 defined as follows:

And thanks to put-call parity, we are able to price a European Vanilla Put P(S,T), as well, with the following formula:

The only thing we need to describe is N, which is the cumulative distribution function of the standard normal distribution, N is given by:

Since we will like to have a closed form solutions for the “Greeks”, i.e. the option price sensitivities to the underlying variables and parameters. For this reason we also need the formula for the probability density function of the standard normal distribution which is given below:

Python implementation:

 1 import scipy
2 import math
3 from scipy import stats
4
5 """ Price an option using the Black-Scholes model.
6 s: initial stock price
7 k: strike price
8 t: expiration time
9 v: volatility
10 r: risk-free rate
11 div: dividend
12 cp: +1/-1 for call/put
13 """
14
15 d1 = lambda s,k,v,r,t,div : (scipy.log(s/k)+(r-div+0.5*math.pow(v,2))*t)/(v*scipy.sqrt(t))
16 d2 = lambda s,k,v,r,t,div : d1(s,k,v,r,t,div) - v*scipy.sqrt(t)
17 call = lambda s,k,v,r,t,div : (s*math.exp(-div*t)*stats.norm.cdf(d1(s,k,v,r,t,div))) - (k*scipy.exp(-r*t)*stats.norm.cdf(d2(s,k,v,r,t,div)))
18 put =  lambda s,k,v,r,t,div : (k*math.exp(-r*t)*stats.norm.cdf(-d2(s,k,v,r,t,div))) - (s*math.exp(-div*t)*stats.norm.cdf(-d1(s,k,v,r,t,div)))
19
20 #for checking purpose this two functions will assert true if the pricing is correct in case of vanilla options
21 check_call = lambda s,k,r,t,call_price : k * math.exp( -r * t ) + call_price - s
22 check_put = lambda s,k,r,t,put_price : s + put_price - k * math.exp( -r * t )

In a next post we will see how to calculate the sensitivity of the price to the underlying variables using the “Greecs”.