def iv_at_the_money(dt_date, dt_last, name_code): dict_iv_call = {} dict_iv_put = {} df_metrics = get_data.get_comoption_mktdata(dt_last, dt_date, name_code) optionset = BaseOptionSet(df_metrics, rf=0.0) optionset.init() dt_maturity = optionset.select_maturity_date(0, 0) call_list, put_list = optionset.get_options_list_by_moneyness_mthd1( 0, dt_maturity) atm_call = optionset.select_higher_volume(call_list) atm_put = optionset.select_higher_volume(put_list) iv_call = atm_call.get_implied_vol() * 100 iv_put = atm_put.get_implied_vol() * 100 dict_iv_call.update({dt_last: iv_call}) dict_iv_put.update({dt_last: iv_put}) optionset.go_to(dt_date) dt_maturity = optionset.select_maturity_date(0, 0) call_list, put_list = optionset.get_options_list_by_moneyness_mthd1( 0, dt_maturity) atm_call = optionset.select_higher_volume(call_list) atm_put = optionset.select_higher_volume(put_list) iv_call = atm_call.get_implied_vol() * 100 iv_put = atm_put.get_implied_vol() * 100 dict_iv_call.update({dt_date: iv_call}) dict_iv_put.update({dt_date: iv_put}) return dict_iv_call, dict_iv_put
def iv_volume_weighted(dt_date, dt_last, name_code): dict_iv = {} df_metrics = get_data.get_comoption_mktdata(dt_last, dt_date, name_code) optionset = BaseOptionSet(df_metrics) optionset.init() dt_maturity = optionset.select_maturity_date(0, 0) iv_volume_weighted = optionset.get_volume_weighted_iv(dt_maturity) * 100 dict_iv.update({dt_last: iv_volume_weighted}) optionset.go_to(dt_date) dt_maturity = optionset.select_maturity_date(0, 0) iv_volume_weighted = optionset.get_volume_weighted_iv(dt_maturity) * 100 dict_iv.update({dt_date: iv_volume_weighted}) return dict_iv
def iv_htbr(dt_date, dt_last, name_code): dict_iv = {} df_metrics = get_data.get_comoption_mktdata(dt_last, dt_date, name_code) optionset = BaseOptionSet(df_metrics, rf=0) optionset.init() dt_maturity = optionset.select_maturity_date(0, 0) iv_curve_htbr = optionset.get_implied_vol_curves_htbr(dt_maturity) call_list, put_list = optionset.get_options_list_by_moneyness_mthd1( 0, dt_maturity) atm_k = call_list[0].applicable_strike() iv = iv_curve_htbr[iv_curve_htbr[c.Util.AMT_APPLICABLE_STRIKE] == atm_k][ c.Util.PCT_IV_PUT_BY_HTBR].values[0] * 100 dict_iv.update({dt_last: iv}) optionset.go_to(dt_date) dt_maturity = optionset.select_maturity_date(0, 0) iv_curve_htbr = optionset.get_implied_vol_curves_htbr(dt_maturity) call_list, put_list = optionset.get_options_list_by_moneyness_mthd1( 0, dt_maturity) atm_k = call_list[0].applicable_strike() iv = iv_curve_htbr[iv_curve_htbr[c.Util.AMT_APPLICABLE_STRIKE] == atm_k][ c.Util.PCT_IV_PUT_BY_HTBR].values[0] * 100 dict_iv.update({dt_date: iv}) return dict_iv
def get_atm_iv_average(optionset, maturity): atm_call, atm_put = get_atm_options(optionset, maturity) iv_call = atm_call.get_implied_vol() iv_put = atm_put.get_implied_vol() iv_avg = (iv_call + iv_put) / 2 return iv_avg pu = PlotUtil() start_date = datetime.date(2018, 9, 12) end_date = datetime.date(2018, 9, 13) nbr_maturity = 0 min_holding = 8 df_daily = get_data.get_comoption_mktdata(start_date, end_date, Util.STR_SR) optionset = BaseOptionSet(df_data=df_daily, rf=0.015) optionset.init() # optionset.go_to(end_date) maturity = optionset.select_maturity_date(nbr_maturity, min_holding=min_holding) iv_htr = optionset.get_atm_iv_by_htbr(maturity) iv_avg = get_atm_iv_average(optionset, maturity) htbr = optionset.get_htb_rate(maturity) print('iv_htr : ', iv_htr) print('iv_avg : ', iv_avg) print('htb rate : ', htbr) # curve = optionset.get_implied_vol_curves(maturity)
end_date = datetime.date(2018, 9, 28) last_week = datetime.date(2018, 9, 21) start_date = last_week # start_date = datetime.date(2017, 4, 1) dt_histvol = start_date - datetime.timedelta(days=200) min_holding = 5 """""" df_res = pd.DataFrame() pu = PlotUtil() """ 白糖 """ name_code = c.Util.STR_SR core_id = 'sr_1901' df_metrics = get_data.get_comoption_mktdata(start_date, end_date, name_code) df_future_c1_daily = get_data.get_future_c1_by_option_daily(dt_histvol, end_date, name_code, min_holding) d1 = max(df_metrics[c.Util.DT_DATE].values[0], df_future_c1_daily[c.Util.DT_DATE].values[0]) df_metrics = df_metrics[(df_metrics[c.Util.DT_DATE] >= d1) & (df_metrics[c.Util.DT_DATE] <= end_date)].reset_index( drop=True) """ 隐含波动率期限结构 """ dt_1 = last_week - datetime.timedelta(days=7) dt_2 = last_week - datetime.timedelta(days=14) """ PCR """ df_res = pcr(d1, end_date, name_code, df_res) print('2.PCR Finished') """ 隐含波动率 """ df_res = implied_vol(df_metrics, df_res, [dt_2, dt_1, last_week, end_date],name_code) df_res = df_res.reset_index(drop=True) print('4.隐含波动率 Finished')
from data_access.get_data import get_50option_mktdata, get_comoption_mktdata start_date = datetime.date(2018,8,27) end_date = datetime.date(2018,8,31) calendar = ql.China() daycounter = ql.ActualActual() util = BktUtil() optionMetrics = admin.table_option_metrics() conn = admin.conn_metrics() name_option = 'sr' print(name_option) df_option_metrics = get_comoption_mktdata(start_date,end_date,name_option) bkt_optionset = BktOptionSet(df_option_metrics) while bkt_optionset.index <= len(bkt_optionset.dt_list): evalDate = bkt_optionset.eval_date option_metrics = bkt_optionset.collect_option_metrics() try: for r in option_metrics: res = optionMetrics.select((optionMetrics.c.id_instrument == r['id_instrument']) &(optionMetrics.c.dt_date == r['dt_date'])).execute() if res.rowcount > 0: optionMetrics.delete((optionMetrics.c.id_instrument == r['id_instrument']) &(optionMetrics.c.dt_date == r['dt_date'])).execute() conn.execute(optionMetrics.insert(), r) print(evalDate, ' : option metrics -- inserted into data base succefully') except Exception as e: print(e)
print(evalDate, 'No complete collar portfolio constructed, try next day !') """按当日价格调整保证金,计算投资组合盯市价值""" bkt_account.mkm_update_portfolio(evalDate,self.portfolio) print(evalDate, ' , ', bkt_account.npv, bkt_account.mtm_long_positions, bkt_account.mtm_short_positions) """Back Test Settings""" start_date = datetime.date(2017, 6, 1) # start_date = datetime.date(2018, 1, 20) end_date = datetime.date(2018, 5, 21) """Collect Mkt Date""" vol_data = pd.read_excel('../data/AE1_HISTVOL.xlsx').rename(columns={'IV':'amt_close','Date':'dt_date'}).sort_values(by='dt_date',ascending=True) index_data = pd.read_excel('../data/AE1_HISTVOL.xlsx').rename(columns={'PRICE':'amt_close','Date':'dt_date'}).sort_values(by='dt_date',ascending=True).dropna() df_option_metrics = get_comoption_mktdata(start_date, end_date, 'm') """Run Backtest""" bkt_strategy = BktStrategyStraddle(df_option_metrics) bkt_strategy.set_min_holding_days(8) df_vol_boll = bkt_strategy.get_bollinger_signal_calculate(vol_data,start_date,cd_long=60) df_index_boll = bkt_strategy.get_bollinger_signal_calculate(index_data,start_date,cd_long=60) # df_vol_pctl = bkt_strategy.get_percentile_signal(vol_data,start_date,cd_long=60) bkt_strategy.set_volatility_boll(df_vol_boll) bkt_strategy.set_index_boll(df_index_boll) # bkt_strategy.set_volatility_boll(df_vol_pctl) df_vol_boll.to_csv('../df_m_vol_boll.csv') # df_vol_pctl.to_csv('../df_m_vol_pctl.csv')