-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBlack-Scholes formula + Greeks.py
45 lines (43 loc) · 1.54 KB
/
Black-Scholes formula + Greeks.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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 27 11:44:30 2017
@author: marco
"""
#Black-Scholes model in Python
import numpy as np
import scipy.stats as ss
import time
#Data for input in Black-Scholes formula:
T=2.0 #supposed in years. It is not the maturity, but the time to maturity
S=100.0
K=105.0
r=0.075
vol=0.20 #supposing it is annual
option_type='P' #for the put insert 'P'
#dividend yield assumed to be 0
#Compute d1 and d2
d1=(np.log(S/K)+(r+0.5*vol)*T)/(vol*np.sqrt(T))
d2=d1-vol*np.sqrt(T)
if option_type in ['C','P']:
if option_type in ['C']:
Opt_Price=S*ss.norm.cdf(d1)-K*np.exp(-r*T)*ss.norm.cdf(d2)
Delta=ss.norm.cdf(d1)
Gamma=ss.norm.cdf(d1)/(S*vol*np.sqrt(T))
Vega=S*ss.norm.cdf(d1)*np.sqrt(T)
Theta=-(S*ss.norm.cdf(d1)*vol)/(2*np.sqrt(T))-r*K*np.exp(-r*T)*ss.norm.cdf(d2)
Rho=K*T*np.exp(-r*T)*ss.norm.cdf(d2)
else:
Opt_Price=K*np.exp(-r*T)*ss.norm.cdf(-d2)-S*ss.norm.cdf(-d1)
Delta=-ss.norm.cdf(-d1)
Gamma=ss.norm.cdf(d1)/(S*vol*np.sqrt(T))
Vega=S*ss.norm.cdf(d1)*np.sqrt(T)
Theta=-(S*ss.norm.cdf(d1)*vol)/(2*np.sqrt(T))+r*K*np.exp(-r*T)*ss.norm.cdf(-d2)
Rho=-K*T*np.exp(-r*T)*ss.norm.cdf(-d2)
else:
Opt_Price= 'Error: option type incorrect. Choose P for a put option or C for a call option.'
print Opt_Price
print 'Delta = {}'.format(Delta)
print 'Gamma = {}'.format(Gamma)
print 'Vega = {}'.format(Vega)
print 'Theta = {}'.format(Theta)
print 'Rho = {}'.format(Rho)