def iv_at_the_money(dt_date, dt_last, name_code):
    dict_iv_call = {}
    dict_iv_put = {}
    df_iv_atm_1 = get_data.get_iv_by_moneyness(dt_last,
                                               dt_date,
                                               name_code,
                                               nbr_moneyness=0,
                                               cd_mdt_selection='hp_8_1st')
    dates = df_iv_atm_1['dt_date'].unique()
    for date in dates:
        iv_call = df_iv_atm_1[(df_iv_atm_1[c.Util.DT_DATE] == dt_date) &
                              (df_iv_atm_1[c.Util.CD_OPTION_TYPE] == 'call')][
                                  c.Util.PCT_IMPLIED_VOL].values[0] * 100
        iv_put = df_iv_atm_1[(df_iv_atm_1[c.Util.DT_DATE] == dt_date)
                             & (df_iv_atm_1[c.Util.CD_OPTION_TYPE] == 'put')][
                                 c.Util.PCT_IMPLIED_VOL].values[0] * 100
        dict_iv_call.update({date: iv_call})
        dict_iv_put.update({date: iv_put})
    return dict_iv_call, dict_iv_put
min_holding = 15
init_fund = c.Util.BILLION
slippage = 0
m = 1  # 期权notional倍数
""" commodity option """
# name_code = name_code_option = c.Util.STR_M
# 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)
""" 50ETF option """
name_code = c.Util.STR_IH
name_code_option = c.Util.STR_50ETF
df_metrics = get_data.get_50option_mktdata(start_date, end_date)
df_future_c1_daily = get_data.get_mktdata_cf_c1_daily(dt_histvol, end_date,
                                                      name_code)
""" 隐含波动率 """
df_iv = get_data.get_iv_by_moneyness(dt_histvol, end_date, name_code_option)
df_iv_call = df_iv[df_iv[c.Util.CD_OPTION_TYPE] == 'call']
df_iv_put = df_iv[df_iv[c.Util.CD_OPTION_TYPE] == 'put']
df_data = df_iv_call[[c.Util.DT_DATE, c.Util.PCT_IMPLIED_VOL
                      ]].rename(columns={c.Util.PCT_IMPLIED_VOL: 'iv_call'})
df_data = df_data.join(df_iv_put[[c.Util.DT_DATE,c.Util.PCT_IMPLIED_VOL]].set_index(c.Util.DT_DATE),on=c.Util.DT_DATE,how='outer')\
    .rename(columns={c.Util.PCT_IMPLIED_VOL:'iv_put'})
df_data = df_data.dropna().reset_index(drop=True)
df_data.loc[:, 'average_iv'] = (df_data.loc[:, 'iv_call'] +
                                df_data.loc[:, 'iv_put']) / 2
""" Volatility Statistics """
df_iv_stats = df_data[[c.Util.DT_DATE, 'average_iv']]
""" 2. Filtration :LLKSR """
# df_iv_stats['LLKSR_20'] = LLKSR(df_iv_stats['average_iv'], 20)
# df_iv_stats['LLKSR10'] = LLKSR(df_iv_stats['average_iv'], 10)
# df_iv_stats['LLKSR_5'] = LLKSR(df_iv_stats['average_iv'], 5)
nbr_maturity = 1
moneyness = -5

############
dt_close_position = end_date - datetime.timedelta(days=5)
min_holding = 15
slippage = 0
pct_underlying_invest = 1.0
pu = PlotUtil()

df_metrics = get_data.get_50option_mktdata(start_date, end_date)
df_underlying = get_data.get_index_mktdata(start_date, end_date,
                                           c.Util.STR_INDEX_50ETF)
df_underlying_llt = filtration_LLT(df_underlying, c.Util.AMT_CLOSE)

df_iv = get_data.get_iv_by_moneyness(start_date, end_date, c.Util.STR_50ETF)
df_iv_put = df_iv[df_iv[c.Util.CD_OPTION_TYPE] ==
                  c.Util.STR_PUT].dropna().reset_index(drop=True)
df_iv_llt = filtration_LLT(df_iv_put, c.Util.PCT_IMPLIED_VOL)

# plt.show()
""" Init Portfolio and Account """
init_fund = 10000000
optionset = BaseOptionSet(df_metrics)
optionset.init()
underlying = BaseInstrument(df_underlying)
underlying.init()
account = BaseAccount(init_fund, leverage=1.0, rf=0.03)
total_premium = 0.0
hedged = False
""" 初始开仓:基准指数 """