def proc_iv(df, T): r = 0.03 # 固定无风险短期利率 iv_c_list = [] iv_p_list = [] for i, row in df.iterrows(): S0 = float(row.obj) # 标的价格 K = int(row.atm) # 行权价格 C0 = float(row.close_C) # 期权价格 iv_c = bsm_call_imp_vol(S0, K, T, r, C0) iv_c_list.append(iv_c) C0 = float(row.close_P) # 期权价格 iv_p = bsm_put_imp_vol(S0, K, T, r, C0) iv_p_list.append(iv_p) # print(T, S0, K, iv_c, iv_p) df['iv_c'] = iv_c_list df['iv_p'] = iv_p_list df['iv_a'] = (df['iv_c'] + df['iv_p']) / 2 df = df[[ 'date', 'time', 'pz', 'symbol', 'atm', 'obj', 'close_C', 'close_P', 'iv_c', 'iv_p', 'iv_a' ]] return df
def test_iv(): r = 0.03 S0 = 4489 K = 4600 T = 10 / 365 C0 = 64.8 sa = bsm_call_imp_vol(S0, K, T, r, C0) # C0 = 71.4 # sa = bsm_put_imp_vol(S0, K, T, r, C0) print(sa)
def df_iv(basic, df): """ 输入:df,包含字段 dt, symbol, close, close_obj """ try: r = 0.03 fn = get_dss() + 'fut/cfg/opt_mature.csv' df2 = pd.read_csv(fn) df2 = df2[pd.notnull(df2.flag)] df2 = df2.set_index('symbol') mature_dict = dict(df2.mature) iv_list = [] for i, row in df.iterrows(): strike = get_contract(row.symbol).strike opt_flag = get_contract(row.symbol).opt_flag date_mature = mature_dict[basic] date_mature = datetime.strptime(date_mature, '%Y-%m-%d') td = datetime.strptime(row['dt'][:10], '%Y-%m-%d') T = float((date_mature - td).days) / 365 # 剩余期限 T = 0.0015 if T == 0 else T # 最后一天特殊处理 try: if opt_flag == 'C': iv = bsm_call_imp_vol(row.close_obj, strike, T, r, row.close) else: iv = bsm_put_imp_vol(row.close_obj, strike, T, r, row.close) except: iv = 0 iv_list.append(round(100 * iv, 2)) df['iv'] = iv_list df['value'] = df['iv'] df2 = df.set_index('dt') df2 = df2.sort_index() df2 = df2[['value']] df2.index.name = df.index.name return df2 except Exception as e: s = traceback.format_exc() to_log(s) return None
def calc_iv(self, basic, flag, S0, K, C0): r = 0.03 fn = get_dss() + 'fut/cfg/opt_mature.csv' df2 = pd.read_csv(fn) df2 = df2[df2.pz == df2.pz] # 筛选出不为空的记录 df2 = df2.set_index('symbol') mature_dict = dict(df2.mature) date_mature = mature_dict[basic] date_mature = datetime.strptime(date_mature, '%Y-%m-%d') td = datetime.now() T = float((date_mature - td).days) / 365 # 剩余期限 skew = 0 if flag == 'C': skew = bsm_call_imp_vol(S0, K, T, r, C0) if flag == 'P': skew = bsm_put_imp_vol(S0, K, T, r, C0) return skew
def calc_greeks_common(symbol, row, S0, is_call, r, today, mature_dict, term, K): """ 计算新下载数据的希腊字母值,保存到yyyy-dd_greeks.csv文件 Parameters: S0: 标的价格 K: 行权价格 T: 剩余期限,已做年化处理 r: 固定无风险短期利率 C0:期权价格 """ C0 = float(row['LastPrice']) # 期权价格 date_mature = mature_dict[term] date_mature = datetime.strptime(date_mature, '%Y-%m-%d') td = datetime.strptime(today, '%Y-%m-%d') T = float((date_mature - td).days) / 365 # 剩余期限 # print(S0, K, C0, T) if is_call == True: n = 1 iv = bsm_call_imp_vol(S0, K, T, r, C0) else: n = -1 iv = bsm_put_imp_vol(S0, K, T, r, C0) row['obj'] = S0 row['delta'] = delta(S0, K, r, T, iv, n) row['gamma'] = gamma(S0, K, r, T, iv) row['theta'] = theta(S0, K, r, T, iv, n) row['vega'] = vega(S0, K, r, T, iv) row['iv'] = iv df2 = pd.DataFrame([row]) df2.index.name = 'Instrument' fn2 = get_dss() + 'opt/' + today[:7] + '_greeks.csv' if os.path.exists(fn2): df2.to_csv(fn2, mode='a', header=None) else: df2.to_csv(fn2)
def skew_now_show(basic, date): # 获得上一个交易日,用于确定差值的基准 symbol_obj = 'IF' + basic[2:] fn = get_dss() + 'fut/bar/day_' + symbol_obj + '.csv' df = pd.read_csv(fn) df = df[df.date <= date] if date == df.iat[-1, 0]: date_pre = df.iat[-2, 0] else: date_pre = df.iat[-1, 0] # 取开盘数据的atm fn = get_dss() + 'fut/put/rec/min5_' + symbol_obj + '.csv' df = pd.read_csv(fn) df = df[df.date == date] df1 = df[df.time == '09:34:00'] if len(df1) == 0: return '当前日期无数据' rec = df1.iloc[0, :] obj = rec.close gap = 50 atm = int(round(round(obj * (100 / gap) / 1E4, 2) * 1E4 / (100 / gap), 0)) # 获得平值 fn = get_dss() + 'fut/put/rec/min5_' + basic + '-C-' + str(atm) + '.csv' df_atm_c = pd.read_csv(fn) df_atm_c = df_atm_c[df_atm_c.date == date] fn = get_dss() + 'fut/put/rec/min5_' + basic + '-P-' + str(atm) + '.csv' df_atm_p = pd.read_csv(fn) df_atm_p = df_atm_p[df_atm_p.date == date] fn = get_dss() + 'fut/put/rec/min5_' + basic + '-C-' + str(atm + 2 * gap) + '.csv' df_right_c = pd.read_csv(fn) df_right_c = df_right_c[df_right_c.date == date] fn = get_dss() + 'fut/put/rec/min5_' + basic + '-P-' + str(atm + 2 * gap) + '.csv' df_right_p = pd.read_csv(fn) df_right_p = df_right_p[df_right_p.date == date] fn = get_dss() + 'fut/put/rec/min5_' + basic + '-C-' + str(atm - 2 * gap) + '.csv' df_left_c = pd.read_csv(fn) df_left_c = df_left_c[df_left_c.date == date] fn = get_dss() + 'fut/put/rec/min5_' + basic + '-P-' + str(atm - 2 * gap) + '.csv' df_left_p = pd.read_csv(fn) df_left_p = df_left_p[df_left_p.date == date] df = df.reset_index() df_atm_c = df_atm_c.reset_index() df_atm_p = df_atm_p.reset_index() df_right_c = df_right_c.reset_index() df_right_p = df_right_p.reset_index() df_left_c = df_left_c.reset_index() df_left_p = df_left_p.reset_index() r = 0.03 fn = get_dss() + 'fut/cfg/opt_mature.csv' df2 = pd.read_csv(fn) df2 = df2[df2.pz == 'IO'] # 筛选出不为空的记录 df2 = df2.set_index('symbol') mature_dict = dict(df2.mature) date_mature = mature_dict[basic] date_mature = datetime.strptime(date_mature, '%Y-%m-%d') td = datetime.strptime(date, '%Y-%m-%d') T = float((date_mature - td).days) / 365 # 剩余期限 skew_c_list = [] skew_p_list = [] # skew_mean_c_list = [] # skew_mean_p_list = [] skew_mean_list = [] for i, row in df.iterrows(): try: S0 = row.close iv_atm_c = bsm_call_imp_vol(S0, atm, T, r, df_atm_c.at[i, 'close']) iv_atm_p = bsm_put_imp_vol(S0, atm, T, r, df_atm_p.at[i, 'close']) iv_right_c = bsm_call_imp_vol(S0, atm + 2 * gap, T, r, df_right_c.at[i, 'close']) iv_right_p = bsm_put_imp_vol(S0, atm + 2 * gap, T, r, df_right_p.at[i, 'close']) iv_left_c = bsm_call_imp_vol(S0, atm - 2 * gap, T, r, df_left_c.at[i, 'close']) iv_left_p = bsm_put_imp_vol(S0, atm - 2 * gap, T, r, df_left_p.at[i, 'close']) skew_c_list.append( round(100 * (iv_right_c - iv_atm_c) / iv_atm_c, 2)) skew_p_list.append( round(100 * (iv_left_p - iv_atm_p) / iv_atm_p, 2)) # skew_mean_c_list.append( round( 100*( iv_right_c + iv_right_p - iv_atm_c - iv_atm_p ) / (iv_atm_c + iv_atm_p), 2) ) # skew_mean_p_list.append( round( 100*( iv_left_c + iv_left_p - iv_atm_c - iv_atm_p ) / (iv_atm_c + iv_atm_p), 2) ) skew_mean_list.append( round( 100 * (iv_right_c + iv_right_p - iv_left_c - iv_left_p) / (iv_left_c + iv_left_p), 2)) except: break plt.figure(figsize=(13, 7)) plt.title(basic + ' ' + str(atm)) plt.xticks(rotation=90) plt.plot(skew_c_list, label='c') plt.plot(skew_p_list, label='p') # plt.plot(skew_mean_c_list, label='mean_c') # plt.plot(skew_mean_p_list, label='mean_p') plt.plot(skew_mean_list, label='mean') plt.legend() fn = 'static/skew_show.jpg' plt.savefig(fn) plt.cla() rr = '' fn = 'skew_show.jpg' now = str(int(time.time())) rr = '<img src=\"static/' + fn + '?rand=' + now + '\" />' return rr
def iv_min5_show(symbol_list, date): plt.figure(figsize=(13, 7)) plt.title(date) for symbol in symbol_list: basic = get_contract(symbol).basic strike = get_contract(symbol).strike opt_flag = get_contract(symbol).opt_flag if basic[:2] == 'IO': symbol_obj = 'IF' + basic[2:] else: symbol_obj = basic fn = get_dss() + 'fut/put/rec/min5_' + symbol_obj + '.csv' df_obj = pd.read_csv(fn) df_obj = df_obj.set_index(['date', 'time']) df_obj['close_obj'] = df_obj['close'] df_obj = df_obj[['close_obj']] # print(df_obj.tail()) fn = get_dss() + 'fut/put/rec/min5_' + symbol + '.csv' df = pd.read_csv(fn) df = df[df.date == date] df = df.set_index(['date', 'time']) # print(df.tail()) df = df.join(df_obj) df = df.dropna() df = df.reset_index() df = df.set_index('time') # print(df.tail()) r = 0.03 fn = get_dss() + 'fut/cfg/opt_mature.csv' df2 = pd.read_csv(fn) df2 = df2[df2.flag == df2.flag] # 筛选出不为空的记录 df2 = df2.set_index('symbol') mature_dict = dict(df2.mature) date_mature = mature_dict[basic] date_mature = datetime.strptime(date_mature, '%Y-%m-%d') td = datetime.strptime(date, '%Y-%m-%d') T = float((date_mature - td).days) / 365 # 剩余期限 iv_list = [] for i, row in df.iterrows(): try: if opt_flag == 'C': iv = bsm_call_imp_vol(row.close_obj, strike, T, r, row.close) else: iv = bsm_put_imp_vol(row.close_obj, strike, T, r, row.close) iv_list.append(round(100 * iv, 2)) except: break df['iv'] = iv_list plt.plot(df.iv, label=symbol) plt.xticks(rotation=90) plt.legend() fn = 'static/iv_min5_show.jpg' plt.savefig(fn) plt.cla() rr = '' fn = 'iv_min5_show.jpg' now = str(int(time.time())) rr = '<img src=\"static/' + fn + '?rand=' + now + '\" />' return rr