-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblack_scholes.py
More file actions
49 lines (35 loc) · 1.3 KB
/
black_scholes.py
File metadata and controls
49 lines (35 loc) · 1.3 KB
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
'''
implement the black scholes module
'''
import numpy as np
from scipy.stats import norm
# define variables
r = 0.01 # 1% = 0.01
s = 30 # price of underlying, ccy
k = 40 # price of strike, ccy
t = 240/365 # number of years
v = 0.30 # volatility in ccy
y = 'call' # type, call or put
def black_scholes(r,s,k,t,v,y)->float|None:
'''calculate bs option price for put or call
args:
r (float): interest rate. where 0.01 = 1%, 0.05 = 5%
s (float): current price of the underlying (in currency). so 30 means 30 USD
k (float): strike price of the option (in currency). so 50 means 50 USD
t (float): number of years
v (float): volatility in currency. so 1.5 mean 1.5 usd
y (string): the option type. So either call or put
return:
price = the price of the option
'''
d1 = (np.log(s/k) + (r + v**2/2)*t) / (v*np.sqrt(t) )
d2 = d1 - v*np.sqrt(t)
if y == 'call':
price = s*norm.cdf(d1, 0, 1) - k*np.exp(-r*t)*norm.cdf(d2, 0, 1)
elif y == 'put':
price = k*np.exp(-r*t)*norm.cdf(-d2, 0, 1) - s*norm.cdf(-d1, 0, 1)
else:
raise ValueError('y must be call or put')
return price
prem = black_scholes(r,s,k,t,v,y)
print(round(prem, 2))