2025-05-13 09:11:14
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:
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."
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
From Put-Call Parity We can calculate the price of a Put Option by:
P=Ke−rTN(−d2)−SN(−d1)
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
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))
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/
2025-01-10 10:12:01
2024-05-31 03:06:49
2024-05-28 03:09:25
There are many other interesting articles, try selecting them from below.
2024-12-17 04:04:33
2023-10-06 01:14:11
2024-04-08 11:54:09
2023-09-25 09:57:32
2024-04-22 03:41:52
2024-01-19 04:28:14
2024-01-25 02:08:43
2024-11-06 10:54:23
2024-05-10 01:34:44