Advertisement
Guest User

IV calc

a guest
Apr 7th, 2021
2,439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. import math as m
  2. from scipy.stats import norm as nd
  3. from bs4 import BeautifulSoup
  4. import requests
  5.  
  6.  
  7. class InvalidDataError(Exception):
  8. pass
  9.  
  10.  
  11. class OptionPrice(object):
  12. def __init__(self, stock_price, strike_price, time_to_exp_days, \
  13. annual_vol_pc, risk_free_rate_pc):
  14.  
  15. self.S = stock_price
  16. self.K = strike_price
  17. self.t = time_to_exp_days
  18. self.v = annual_vol_pc
  19. self.r = risk_free_rate_pc
  20.  
  21. self.t_yrs = time_to_exp_days / 365
  22. self.v_dec = annual_vol_pc / 100
  23. self.r_dec = risk_free_rate_pc / 100
  24.  
  25. self.check_parameters()
  26.  
  27. def check_parameters(self):
  28. # check input
  29. CONDITIONS = [
  30. self.S <= 0.0,
  31. self.K <= 0.0,
  32. self.t <= 0.0,
  33. self.v <= 0.0,
  34. self.r < 0.0
  35. ]
  36. if any(CONDITIONS):
  37. raise InvalidDataError("[Error] Input parameter(s) out of range")
  38.  
  39. def calc_d1(self):
  40. return (m.log(self.S / self.K) + (self.r_dec + self.v_dec**2 / 2) * self.t_yrs) \
  41. /(self.v_dec * m.sqrt(self.t_yrs))
  42.  
  43. def calc_d2(self):
  44. d1 = self.calc_d1()
  45. return d1 - self.v_dec * m.sqrt(self.t_yrs)
  46.  
  47. def call_price(self):
  48. d1 = self.calc_d1()
  49. d2 = self.calc_d2()
  50. callprice = self.S * nd.cdf(d1) \
  51. -self.K * m.exp(-self.r_dec * self.t_yrs) * nd.cdf(d2)
  52. return OptionPrice.round_price(callprice)
  53.  
  54. def put_price(self):
  55. d1 = self.calc_d1()
  56. d2 = self.calc_d2()
  57. putprice = self.K * m.exp(-self.r_dec * self.t_yrs) * nd.cdf(-d2) \
  58. -self.S * nd.cdf(-d1)
  59. return OptionPrice.round_price(putprice)
  60.  
  61. @staticmethod
  62. def round_price(value):
  63. return round(value,2)
  64.  
  65.  
  66. class OptionGreeks(OptionPrice):
  67. """The sensitivities of the Black-Scholes Model"""
  68.  
  69. def __init__(self, stock_price, strike_price, time_to_exp_days, \
  70. annual_vol_pc, risk_free_rate_pc):
  71. OptionPrice.__init__(self, stock_price, strike_price, time_to_exp_days, \
  72. annual_vol_pc, risk_free_rate_pc)
  73.  
  74. @staticmethod
  75. def round_greeks(value):
  76. return round(value,4)
  77.  
  78. def call_delta(self):
  79. d1 = self.calc_d1()
  80. calldelta = nd.cdf(d1)
  81. return OptionGreeks.round_greeks(calldelta)
  82.  
  83. def put_delta(self):
  84. putdelta = self.call_delta()-1
  85. return OptionGreeks.round_greeks(putdelta)
  86.  
  87. def call_gamma(self):
  88. d1 = self.calc_d1()
  89. callgamma = nd.pdf(d1) / (self.S * self.v_dec * m.sqrt(self.t_yrs))
  90. return OptionGreeks.round_greeks(callgamma)
  91.  
  92. def put_gamma(self):
  93. return self.call_gamma()
  94.  
  95. def call_theta(self):
  96. d1 = self.calc_d1()
  97. d2 = self.calc_d2()
  98. calltheta = (-self.S * self.v_dec * nd.pdf(d1) / (2 * m.sqrt(self.t_yrs))\
  99. -self.r_dec * self.K * m.exp(-self.r_dec * self.t_yrs) * nd.cdf(d2))
  100. calltheta = calltheta / 365
  101. return OptionGreeks.round_greeks(calltheta)
  102.  
  103. def put_theta(self):
  104. d1 = self.calc_d1()
  105. d2 = self.calc_d2()
  106. puttheta = (-self.S * self.v_dec * nd.pdf(d1) / (2 * m.sqrt(self.t_yrs))\
  107. +self.r_dec * self.K * m.exp(-self.r_dec * self.t_yrs) * nd.cdf(-d2))
  108. puttheta = puttheta / 365
  109. return OptionGreeks.round_greeks(puttheta)
  110.  
  111. def call_vega(self):
  112. d1 = self.calc_d1()
  113. callvega = (self.S * m.sqrt(self.t_yrs) * nd.pdf(d1)) / 100
  114. return OptionGreeks.round_greeks(callvega)
  115.  
  116. def put_vega(self):
  117. return self.call_vega()
  118.  
  119. def call_rho(self):
  120. d2 = self.calc_d2()
  121. callrho = self.K * self.t_yrs * m.exp(-self.r_dec * self.t_yrs)\
  122. * nd.cdf(d2) / 100
  123. return OptionGreeks.round_greeks(callrho)
  124.  
  125. def put_rho(self):
  126. d2 = self.calc_d2()
  127. putrho = -self.K * self.t_yrs * m.exp(-self.r_dec * self.t_yrs)\
  128. * nd.cdf(-d2) / 100
  129. return OptionGreeks.round_greeks(putrho)
  130.  
  131. S = 65.0
  132. K = 60.0
  133. DTE = 30.0
  134. IV = 35.0
  135. r = 1
  136.  
  137. price = OptionPrice(S, K, DTE, IV, r)
  138.  
  139. print(price.call_price())
  140. print(price.put_price())
  141.  
  142. greeks = OptionGreeks(S, K, DTE, IV, r)
  143.  
  144. print(greeks.call_vega())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement