1 # -*- coding: utf-8 -*-
2 """
3 Created on Wed Nov 24 20:12:47 2021
4 求隐波
5 @author: Administrator
6 """
7
8 from scipy import stats
9 import numpy as np
10 from scipy.optimize import fsolve
11
12 def blsprice(price, strike, rate, time, volatility):
13 #price标的资产市场价格,strike执行价格,rate无风险利率,
14 #time距离到期时间,volatility标的资产价格波动率
15
16 price, strike, rate, time, volatility = float(price), float(strike), \
17 float(rate), float(time), float(volatility)
18
19 d1 = (np.log(price / strike) + (rate + 0.5 * volatility ** 2) * time) / \
20 (volatility * np.sqrt(time))
21
22 d2 = d1 - volatility * np.sqrt(time)
23
24 call = price * stats.norm.cdf(d1, 0.0, 1.0) - \
25 strike * np.exp(-rate * time) * stats.norm.cdf(d2, 0.0, 1.0)
26 put = strike * np.exp(-rate * time) * stats.norm.cdf(-d2, 0.0, 1.0) - \
27 price * stats.norm.cdf(-d1, 0.0, 1.0)
28
29 return call, put
30
31 def ImpliedVolatitityCallObj(call, Price, Strike, Rate, Time, volatility_est=0.174):
32 #volatility_est预期的标的资产价格波动率
33 def difference(volatility_est, Price, Strike, Rate, Time):
34 # 根据参数,使用blsprice计算期权价格
35 est_call = blsprice(Price, Strike, Rate, Time, volatility_est)[0]
36 return est_call - call
37 # 存在一个波动率使得下列等式成立
38 iv = fsolve(difference, volatility_est, args=(Price, Strike, Rate, Time))[0]
39
40 return iv
41
42 def ImpliedVolatitityPutObj(put, Price, Strike, Rate, Time, volatility_est=0.174):
43 def difference(volatility_est, Price, Strike, Rate, Time):
44 # 根据参数,使用blsprice计算期权价格
45 est_put = blsprice(Price, Strike, Rate, Time, volatility_est)[1]
46 return est_put - put
47 # 存在一个波动率使得下列等式成立
48 iv = fsolve(difference, volatility_est, args=(Price, Strike, Rate, Time))[0]
49
50 return iv
51
52 # 函数求解
53
54
55 Price = 3.273
56 Strike = 3.2
57 Rate = 0.04
58 Time = 28/365
59
60 print ('Implied Volatitity of Call =', ImpliedVolatitityCallObj(0.1101, Price, Strike, Rate, Time))
61 print( 'Implied Volatitity of Put =', ImpliedVolatitityPutObj(0.0269, Price, Strike, Rate, Time))