-
Notifications
You must be signed in to change notification settings - Fork 24
/
BlackScholes.py
73 lines (62 loc) · 1.93 KB
/
BlackScholes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from numpy import exp, sqrt, log
from scipy.stats import norm
class BlackScholes:
def __init__(
self,
time_to_maturity: float,
strike: float,
current_price: float,
volatility: float,
interest_rate: float,
):
self.time_to_maturity = time_to_maturity
self.strike = strike
self.current_price = current_price
self.volatility = volatility
self.interest_rate = interest_rate
def run(
self,
):
time_to_maturity = self.time_to_maturity
strike = self.strike
current_price = self.current_price
volatility = self.volatility
interest_rate = self.interest_rate
d1 = (
log(current_price / strike) +
(interest_rate + 0.5 * volatility ** 2) * time_to_maturity
) / (
volatility * sqrt(time_to_maturity)
)
d2 = d1 - volatility * sqrt(time_to_maturity)
call_price = current_price * norm.cdf(d1) - (
strike * exp(-(interest_rate * time_to_maturity)) * norm.cdf(d2)
)
put_price = (
strike * exp(-(interest_rate * time_to_maturity)) * norm.cdf(-d2)
) - current_price * norm.cdf(-d1)
self.call_price = call_price
self.put_price = put_price
# GREEKS
# Delta
self.call_delta = norm.cdf(d1)
self.put_delta = 1 - norm.cdf(d1)
# Gamma
self.call_gamma = norm.pdf(d1) / (
strike * volatility * sqrt(time_to_maturity)
)
self.put_gamma = self.call_gamma
if __name__ == "__main__":
time_to_maturity = 2
strike = 90
current_price = 100
volatility = 0.2
interest_rate = 0.05
# Black Scholes
BS = BlackScholes(
time_to_maturity=time_to_maturity,
strike=strike,
current_price=current_price,
volatility=volatility,
interest_rate=interest_rate)
BS.run()