Skip to content

Commit

Permalink
Black-Scholes and greeks
Browse files Browse the repository at this point in the history
  • Loading branch information
hashABCD committed Jun 29, 2021
1 parent 37ae432 commit 8af0e14
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ __pycache__/
# C extensions
*.so

#VS Code
.vscode/

# Distribution / packaging
.Python
env/
Expand Down
3 changes: 2 additions & 1 deletion opstrat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__version__ = "0.1.2"
__version__ = "0.1.3"
__author__ = "Abhijith Chandradas"

from .basic_multi import *
from .basic_single import *
from .yf import *
from .blackscholes import black_scholes
103 changes: 103 additions & 0 deletions opstrat/blackscholes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import numpy as np
from scipy.stats import norm


def black_scholes(t=40, r=4.00, v=32.00, K=60, St=62, type='c'):
"""
Parameters:
K : Excercise Price
St: Current Stock Price
v : Volatility in percentage
r : Risk free rate in percentage
t : Time to expiration in days
type: Type of option 'c' for call 'p' for put
default: 'c'
"""
# if type = 'c' or 'C' call option else put option
try:
type=type.lower()
if(type=='c'):
option_type='call'
else:
option_type='put'
except:
option_type='put'

#Check time
try:
#convert time in days to years
t=t/365
except:
raise TypeError("Enter numerical value for time")

#Check risk free rate
try:
#convert percentage to decimal
r=r/100
except:
raise TypeError("Enter numerical value for risk free rate")

#Check volatility
try:
#convert percentage to decimal
v=v/100
except:
raise TypeError("Enter numerical value for volatility")

#Check Stock Price
try:
St=St+0
except:
raise TypeError("Enter numerical value for stock price")

#Check Exercise Price
try:
K=K+0
except:
raise TypeError("Enter numerical value for Exercise price")

n1=np.log(St/K)
n2=(r+(np.power(v,2)/2))*t
d=v*(np.sqrt(t))

d1=(n1+n2)/d
d2=d1-(v*np.sqrt(t))

if type=='c':
N_d1=norm.cdf(d1)
N_d2=norm.cdf(d2)
else:
N_d1=norm.cdf(-d1)
N_d2=norm.cdf(-d2)

A=(St*N_d1)
B=(K*N_d2*(np.exp(-r*t)))

if type=='c':
val=A-B
val_int=max(0,St-K)
else:
val=B-A
val_int=max(0,K-St)
val_time=val-val_int

# Option values in dictionary
value={'option value':val, 'intrinsic value':val_int, 'time value':val_time}

#CALCULATE OPTION GREEKS
if type=='c':
delta=N_d1
theta=(-((St*v*np.exp(-np.power(d1,2)/2))/(np.sqrt(8*np.pi*t)))-(N_d2*r*K*np.exp(-r*t)))/365
rho=t*K*N_d2*np.exp(-r*t)/100
else:
delta=-N_d1
theta=(-((St*v*np.exp(-np.power(d1,2)/2))/(np.sqrt(8*np.pi*t)))+(N_d2*r*K*np.exp(-r*t)))/365
rho=-t*K*N_d2*np.exp(-r*t)/100

gamma=(np.exp(-np.power(d1,2)/2))/(St*v*np.sqrt(2*np.pi*t))
vega=(St*np.sqrt(t)*np.exp(-np.power(d1,2)/2))/(np.sqrt(2*np.pi)*100)

#Option greeks in Dictionary
greeks={'delta':delta, 'gamma':gamma, 'theta':theta, 'vega':vega, 'rho':rho}

return {'value':value, 'greeks':greeks}
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
matplotlib==3.4.2
numpy==1.21.0
pandas==1.2.5
seaborn==0.11.1
yfinance==0.1.59
29 changes: 1 addition & 28 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,7 @@
README = readme_file.read()


'''
here = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
'''

'''
try:
long_description = pypandoc.convert('README.md', 'rst')
long_description = long_description.replace("\r","") # Do not forget this line
except OSError:
print("Pandoc not found. Long_description conversion failure.")
import io
# pandoc is not installed, fallback to using raw contents
with io.open('README.md', encoding="utf-8") as f:
long_description = f.read()
'''

'''
try:
import pypandoc
long_description = pypandoc.convert('README.md', 'rst')
except(IOError, ImportError):
long_description = open('README.md').read()
'''

VERSION = '0.1.2'
VERSION = '0.1.3'
DESCRIPTION = 'Option stategy visualizer'
#LONG_DESCRIPTION = DESCRIPTION
URL = 'https://github.com/abhijith-git/opstrat'
Expand Down

0 comments on commit 8af0e14

Please sign in to comment.