Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import numpy as np
  2. import scipy.stats as stats
  3.  
  4. def call(S, K, T, r, div):
  5. d1 = (np.log(S / K) + (r - div + (vol ** 2) / 2) * T) \
  6. / (vol * np.sqrt(T))
  7. d2 = (np.log(S / K) + (r - div - (vol ** 2) / 2) * T) \
  8. / (vol * np.sqrt(T))
  9. price = (S * np.exp(-div * T) * stats.norm.cdf(d1, 0.0, 1.0)
  10. - K * np.exp(-r * T) * stats.norm.cdf(d2, 0.0, 1.0))
  11. return price
  12.  
  13. def put(S, K, T, r, div):
  14. d1 = (np.log(S / K) + (r - div + (vol ** 2) / 2) * T) \
  15. / (vol * np.sqrt(T))
  16. d2 = (np.log(S / K) + (r - div - (vol ** 2) / 2) * T) \
  17. / (vol * np.sqrt(T))
  18. price = (K * np.exp(-r * T) * stats.norm.cdf(-d2, 0.00, 1.0)
  19. - S * np.exp(-div * T) * stats.norm.cdf(-d1, 0.0, 1.0))
  20. return price
  21.  
  22. option_type = input("Price a C or a P?")
  23. market_price = float(input("Input market price: "))
  24. S = float(input("Input spot: "))
  25. K = float(input("Input strike: "))
  26. T = float(input("Input time to maturity: "))
  27. r = float(input("Interest rate: "))
  28. div = float(input("Input dividend yield: "))
  29. vol = 0
  30. implied_price = 0
  31.  
  32. # Add more decimals to increase accuracy
  33. while (market_price - implied_price) > 0.0001:
  34. vol += 0.0001 # Add more decimals to increase accuracy
  35. if option_type == 'C':
  36. implied_price = call(S, K, T, r, div)
  37. elif option_type == 'P':
  38. implied_price = put(S, K, T, r, div)
  39. else:
  40. print("Implied vol is :" + str(vol))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement