Technology

Pricing European Vanilla Options with Python

2025-05-13 09:11:14


Why use Python?

Although Python is known as a scripting language used to connect various parts of software systems, with tools like NumPy and SciPy, it has sufficient capabilities to fully perform option pricing.

Reasons to choose Python include:

  • You write much less code than in languages like Java or C++.
  • There are good libraries available following the "batteries included" principle.
  • Easy to understand, like pseudo-code.
  • Can be continuously optimized with NumPy, Numba, and Cython.


This article series will focus on "Simplicity Before Optimization" according to Daniel Duffy's concept:

"Make it work first, then make it right, and finally make it fast."



Black-Scholes formula

The Black-Scholes formula for calculating the price of a European Vanilla Call Option is as follows:

C = SN(d1) − Ke−rT N(d2)


Where:

d1 = vT ln(S/K) + (r + 21v2)T, d2 = d1 − vT


  • S: Reference asset price
  • K: Strike Price
  • r: risk-free interest rate
  • T: When the expiration date arrives
  • v: Asset price volatility
  • N(x): The CDF function of the standard normal distribution


From Put-Call Parity We can calculate the price of a Put Option by:

P=Ke−rTN(−d2​)−SN(−d1​)



Writing statistical functions with Python

Create a file named statistics.py and insert the following code:

python

CopyEdit

from math import exp, log, pi
def norm_pdf(x):
    """
    Standard normal probability density function
    """
    return (1.0/((2*pi)**0.5)) * exp(-0.5 * x * x)
def norm_cdf(x):
    """
    Approximation to the cumulative distribution function for standard normal
    """
    k = 1.0 / (1.0 + 0.2316419 * abs(x))
    k_sum = k * (0.319381530 + k * (-0.356563782 + 
             k * (1.781477937 + k * (-1.821255978 + 1.330274429 * k))))
    result = 1.0 - (1.0 / ((2 * pi)**0.5)) * exp(-0.5 * x * x) * k_sum
    return result if x >= 0 else 1.0 - result



Calculating prices using the closed-form formula

Create a new file named closed_form.py and add this code:

python

CopyEdit

from math import exp, log, sqrt
from statistics import norm_pdf, norm_cdf
def d_j(j, S, K, r, v, T):
    """
    Computes d1 (j=1) or d2 (j=2) used in Black-Scholes
    """
    return (log(S/K) + (r + ((-1)**(j-1))*0.5*v*v)*T) / (v * sqrt(T))
def vanilla_call_price(S, K, r, v, T):
    """
    Price of a European call option
    """
    return S * norm_cdf(d_j(1, S, K, r, v, T)) - \
           K * exp(-r*T) * norm_cdf(d_j(2, S, K, r, v, T))
def vanilla_put_price(S, K, r, v, T):
    """
    Price of a European put option
    """
    return -S * norm_cdf(-d_j(1, S, K, r, v, T)) + \
            K * exp(-r*T) * norm_cdf(-d_j(2, S, K, r, v, T))



Next step

The next step is to verify the results to ensure that these formulas provide accurate prices and meet the conditions, such as Put-Call Parity



Reference: European Vanilla Call-Put Option Pricing with Python

From https://www.quantstart.com/articles/European-Vanilla-Call-Put-Option-Pricing-with-Python/

Leave a comment :