-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreeks.py
112 lines (90 loc) · 4.23 KB
/
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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import math
from scipy.stats import norm
# S0 = underlying price
# X = strike price
# t = time to expiration
# σ = volatility
# r = continuously compounded risk-free interest rate
# q = continuously compounded dividend yield
# For,
# σ = Volatility = India VIX has been taken.
# r = 10% (As per NSE Website, it is fixed.)
# q = 0.00% (Assumed No Dividend)
def black_scholes_dexter(S0, X, t, σ="", r=10, q=0.0, td=365):
# if(σ==""):σ =indiavix()
S0, X, σ, r, q, t = float(S0), float(X), float(σ / 100), float(r / 100), float(q / 100), float(t / td)
# https://unofficed.com/black-scholes-model-options-calculator-google-sheet/
d1 = (math.log(S0 / X) + (r - q + 0.5 * σ ** 2) * t) / (σ * math.sqrt(t))
# stackoverflow.com/questions/34258537/python-typeerror-unsupported-operand-types-for-float-and-int
# stackoverflow.com/questions/809362/how-to-calculate-cumulative-normal-distribution
Nd1 = (math.exp((-d1 ** 2) / 2)) / math.sqrt(2 * math.pi)
d2 = d1 - σ * math.sqrt(t)
Nd2 = norm.cdf(d2)
call_theta = (-((S0 * σ * math.exp(-q * t)) / (2 * math.sqrt(t)) * (1 / (math.sqrt(2 * math.pi))) * math.exp(
-(d1 * d1) / 2)) - (r * X * math.exp(-r * t) * norm.cdf(d2)) + (q * math.exp(-q * t) * S0 * norm.cdf(d1))) / td
put_theta = (-((S0 * σ * math.exp(-q * t)) / (2 * math.sqrt(t)) * (1 / (math.sqrt(2 * math.pi))) * math.exp(
-(d1 * d1) / 2)) + (r * X * math.exp(-r * t) * norm.cdf(-d2)) - (
q * math.exp(-q * t) * S0 * norm.cdf(-d1))) / td
call_premium = math.exp(-q * t) * S0 * norm.cdf(d1) - X * math.exp(-r * t) * norm.cdf(d1 - σ * math.sqrt(t))
put_premium = X * math.exp(-r * t) * norm.cdf(-d2) - math.exp(-q * t) * S0 * norm.cdf(-d1)
call_delta = math.exp(-q * t) * norm.cdf(d1)
put_delta = math.exp(-q * t) * (norm.cdf(d1) - 1)
gamma = (math.exp(-r * t) / (S0 * σ * math.sqrt(t))) * (1 / (math.sqrt(2 * math.pi))) * math.exp(-(d1 * d1) / 2)
vega = ((1 / 100) * S0 * math.exp(-r * t) * math.sqrt(t)) * (
1 / (math.sqrt(2 * math.pi)) * math.exp(-(d1 * d1) / 2))
call_rho = (1 / 100) * X * t * math.exp(-r * t) * norm.cdf(d2)
put_rho = (-1 / 100) * X * t * math.exp(-r * t) * norm.cdf(-d2)
return call_theta, put_theta, call_premium, put_premium, call_delta, put_delta, gamma, vega, call_rho, put_rho
def print_option_greeks(
underlying_price,
strike_price,
time_to_expiration,
volatility,
risk_free_interest_rate,
dividend_yield,
td=365,
):
call_theta, put_theta, call_premium, put_premium, call_delta, put_delta, gamma, vega, call_rho, put_rho = \
black_scholes_dexter(
underlying_price,
strike_price,
time_to_expiration,
volatility,
risk_free_interest_rate,
dividend_yield,
td,
)
print('CALL')
print(f'\t premium: {call_premium}')
print(f'\t delta: {call_delta}')
print(f'\t theta: {call_theta}')
print(f'\t rho: {call_rho}')
print('PUT')
print(f'\t premium: {put_premium}')
print(f'\t delta: {put_delta}')
print(f'\t theta: {put_theta}')
print(f'\t rho: {put_rho}')
print()
print(f'Gamma: {gamma}')
print(f'Vega: {vega}')
def main():
try:
underlying_price = float(input("Enter the underlying price: "))
strike_price = float(input("Enter the strike price: "))
time_to_expiration = float(input("Enter the time to expiration (in days): "))
volatility = float(input("Enter the volatility (in percentage): "))
risk_free_interest_rate = float(input("Enter the risk-free interest rate (in percentage): "))
dividend_yield = float(input("Enter the dividend yield (in percentage): "))
print_option_greeks(
underlying_price=underlying_price,
strike_price=strike_price,
time_to_expiration=time_to_expiration,
volatility=volatility,
risk_free_interest_rate=risk_free_interest_rate,
dividend_yield=dividend_yield,
)
except ValueError:
print("Please enter valid numerical values.")
# Assuming that 'print_option_greeks' is already defined somewhere in your code
if __name__ == '__main__':
main()