Ejemplo n.º 1
0
def hist_vol(dt_start, df_future_c1_daily, df_res, name_code):
    if name_code == c.Util.STR_CU:
        m = 100
    else:
        m = 1
    df_future_c1_daily.loc[:, 'histvol_10'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=10) * m
    df_future_c1_daily.loc[:, 'histvol_20'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=20) * m
    df_future_c1_daily.loc[:, 'histvol_30'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=30) * m
    df_future_c1_daily.loc[:, 'histvol_60'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=20 * 3) * m
    df_future_c1_daily.loc[:, 'histvol_120'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=20 * 6) * m
    df_tmp = df_future_c1_daily[
        df_future_c1_daily[c.Util.DT_DATE] >= dt_start].dropna()
    df_tmp = df_tmp.sort_values(by=c.Util.DT_DATE,
                                ascending=False).reset_index(drop=True)
    df_res.loc[:, 'U:date'] = df_tmp[c.Util.DT_DATE]
    df_res.loc[:, 'V:histvol_10'] = df_tmp.loc[:, 'histvol_10']
    df_res.loc[:, 'W:histvol_20'] = df_tmp.loc[:, 'histvol_20']
    df_res.loc[:, 'X:histvol_30'] = df_tmp.loc[:, 'histvol_30']
    df_res.loc[:, 'Y:histvol_60'] = df_tmp.loc[:, 'histvol_60']
    df_res.loc[:, 'Z:histvol_120'] = df_tmp.loc[:, 'histvol_120']
    return df_res
Ejemplo n.º 2
0
def hist_vol1(df_future_c1_daily):
    m = 100
    df_future_c1_daily.loc[:, 'histvol_10'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=10) * m
    df_future_c1_daily.loc[:, 'histvol_20'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=20) * m
    df_future_c1_daily.loc[:, 'histvol_30'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=30) * m
    df_future_c1_daily.loc[:, 'histvol_60'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=60) * m
    df_future_c1_daily.loc[:, 'histvol_90'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=90) * m
    return df_future_c1_daily
Ejemplo n.º 3
0
 def init(self):
     df_future_histvol = self.df_f_c1.copy()
     df_future_histvol['amt_hv'] = histvol.hist_vol(
         df_future_histvol[c.Util.AMT_CLOSE], n=self.n_hv)
     self.dt_start = max(self.df_f_c1[c.Util.DT_DATE].values[0],
                         self.df_metrics[c.Util.DT_DATE].values[0])
     self.end_date = min(self.df_f_c1[c.Util.DT_DATE].values[-1],
                         self.df_metrics[c.Util.DT_DATE].values[-1])
     self.df_metrics = self.df_metrics[
         self.df_metrics[c.Util.DT_DATE] >= self.dt_start].reset_index(
             drop=True)
     self.df_f_c1 = self.df_f_c1[
         self.df_f_c1[c.Util.DT_DATE] >= self.dt_start].reset_index(
             drop=True)
     self.df_f_all = self.df_f_all[
         self.df_f_all[c.Util.DT_DATE] >= self.dt_start].reset_index(
             drop=True)
     self.df_vol = pd.merge(self.df_vol,
                            df_future_histvol[[c.Util.DT_DATE, 'amt_hv']],
                            on=c.Util.DT_DATE).set_index(c.Util.DT_DATE)
     self.optionset = BaseOptionSet(self.df_metrics)
     self.optionset.init()
     self.hedging = SytheticOption(self.df_f_c1,
                                   rf=self.rf,
                                   df_futures_all_daily=self.df_f_all)
     self.hedging.init()
     self.hedging.amt_option = 1 / 1000  # 50ETF与IH点数之比
     self.account = BaseAccount(init_fund=c.Util.BILLION, rf=self.rf)
     self.prepare_timing()
    def __init__(self, start_date, end_date):
        self.ttm = 30
        self.buywrite = BuyWrite.BUY
        self.fund = Util.BILLION
        self.invest_underlying_ratio = 0.7
        # self.invest_underlying_ratio = 1
        self.slippage = 2
        self.start_date = start_date
        self.end_date = end_date
        hist_date = start_date - datetime.timedelta(days=40)
        df_future_c1 = get_dzqh_cf_c1_minute(start_date, end_date, 'if')
        df_future_c1_daily = get_dzqh_cf_c1_daily(hist_date, end_date, 'if')
        df_futures_all_daily = get_dzqh_cf_daily(
            start_date, end_date, 'if')  # daily data of all future contracts
        df_index = get_index_mktdata(
            start_date, end_date,
            'index_300sh')  # daily data of underlying index
        df_index = df_index[df_index[Util.DT_DATE].isin(
            Util.DZQH_CF_DATA_MISSING_DATES) == False].reset_index(drop=True)
        # df_index.to_csv('df_index.csv')
        self.trade_dates = sorted(df_future_c1_daily[Util.DT_DATE].unique())
        self.df_vol_1m = Histvol.hist_vol(df_future_c1_daily)
        # df_parkinson_1m = Histvol.parkinson_number(df_future_c1_daily)
        self.df_garman_klass = Histvol.garman_klass(df_future_c1_daily)
        # df_hist_vol = self.df_vol_1m.join(self.df_garman_klass, how='left')
        # df_hist_vol.to_csv('../../data/df_hist_vol.csv')
        self.underlying = BaseInstrument(df_data=df_index)
        self.underlying.init()
        self.synthetic_option = SytheticOption(
            df_c1_data=df_future_c1,
            # df_c1_daily=df_future_c1_daily,
            df_futures_all_daily=df_futures_all_daily,
            df_index_daily=df_index)
        self.synthetic_option.init()

        self.account = BaseAccount(self.fund, leverage=20.0, rf=0.0)
        self.trading_desk = Trade()
        self.init_spot = self.synthetic_option.underlying_state_daily[
            Util.AMT_CLOSE]
        self.df_analysis = pd.DataFrame()
Ejemplo n.º 5
0
def hist_vol(dt_start, df_future_c1_daily, df_res, name_code):
    m = 100
    df_future_c1_daily.loc[:, 'histvol_10'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=10) * m
    df_future_c1_daily.loc[:, 'histvol_20'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=20) * m
    df_future_c1_daily.loc[:, 'histvol_30'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=30) * m
    df_future_c1_daily.loc[:, 'histvol_60'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=60) * m
    df_future_c1_daily.loc[:, 'histvol_90'] = Histvol.hist_vol(
        df_future_c1_daily[c.Util.AMT_CLOSE], n=90) * m
    # df_tmp = df_future_c1_daily[df_future_c1_daily[c.Util.DT_DATE] >= dt_start].dropna()
    df_tmp = df_future_c1_daily.sort_values(
        by=c.Util.DT_DATE, ascending=False).reset_index(drop=True)
    df_res.loc[:, name_code + ':U:date'] = df_tmp[c.Util.DT_DATE]
    df_res.loc[:, name_code + ':V:histvol_10'] = df_tmp.loc[:, 'histvol_10']
    df_res.loc[:, name_code + ':W:histvol_20'] = df_tmp.loc[:, 'histvol_20']
    df_res.loc[:, name_code + ':X:histvol_30'] = df_tmp.loc[:, 'histvol_30']
    df_res.loc[:, name_code + ':Y:histvol_60'] = df_tmp.loc[:, 'histvol_60']
    df_res.loc[:, name_code + ':Z:histvol_90'] = df_tmp.loc[:, 'histvol_90']
    return df_res
Ejemplo n.º 6
0
def LLKSR_analysis(dt_start, series_iv, df_future_c1_daily, name_code):
    iv_atm = series_iv
    df_estimated_iv = pd.DataFrame()
    df_estimated_iv['LLKSR_iv_5'] = LLKSR(iv_atm, 5)
    df_estimated_iv['LLKSR_iv_10'] = LLKSR(iv_atm, 10)
    df_estimated_iv['LLKSR_iv_20'] = LLKSR(iv_atm, 20)
    df_future_c1_daily.loc[:, 'histvol_20'] = Histvol.hist_vol(df_future_c1_daily[c.Util.AMT_CLOSE], n=20) * 100
    df_histvol = df_future_c1_daily[df_future_c1_daily[c.Util.DT_DATE] >= dt_start].dropna().reset_index(drop=True)
    dates = list(df_histvol[c.Util.DT_DATE])
    f1 = pu.plot_line_chart(dates, [list(df_estimated_iv['LLKSR_iv_5']), list(df_estimated_iv['LLKSR_iv_10']),
                                    list(df_estimated_iv['LLKSR_iv_20'])],
                            ['隐含波动率LLKSR趋势线 (h=5)', '隐含波动率LLKSR趋势线 (h=10)', '隐含波动率LLKSR趋势线 (h=20)'])
    f2 = pu.plot_line_chart(dates, [list(df_estimated_iv['LLKSR_iv_10']), list(df_histvol['histvol_20'])],
                            ['隐含波动率LLKSR趋势线 (h=10)', '历史波动率LLKSR趋势线 (h=10)'])
    f3 = pu.plot_line_chart(dates, [list(df_estimated_iv['LLKSR_iv_10']), list(iv_atm)],
                            ['隐含波动率LLKSR趋势线 (h=10)', 'iv_atm'])
    f1.savefig('../data/' + name_code + '_iv_LLKSRs.png', dpi=300, format='png', bbox_inches='tight')
    f2.savefig('../data/' + name_code + '_iv_hv_LLKSR.png', dpi=300, format='png', bbox_inches='tight')
    f3.savefig('../data/' + name_code + '_iv_LLKSR.png', dpi=300, format='png', bbox_inches='tight')
from Utilities.PlotUtil import PlotUtil


pu = PlotUtil()
start_date = datetime.date(2010, 1, 1)
end_date = datetime.date(2018, 8, 8)
dt_histvol = start_date

""" 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_dzqh_cf_c1_daily(dt_histvol, end_date, name_code)
# df_future_c1_daily = get_data.get_index_mktdata(start_date,end_date,'index_50sh')
""" 历史波动率 """
df_cc_1m = Histvol.hist_vol(df_future_c1_daily)
# df_p_1m = Histvol.parkinson_number(df_future_c1_daily)
# df_gk_1m = Histvol.garman_klass(df_future_c1_daily)
df_data = df_future_c1_daily.join(df_cc_1m,on=c.Util.DT_DATE,how='left')
# df_data = df_data.join(df_p_1m,on=c.Util.DT_DATE,how='left')
# df_data = df_data.join(df_gk_1m,on=c.Util.DT_DATE,how='left')

df_cc_1w = Histvol.hist_vol(df_future_c1_daily,n=5)
# df_p_1w = Histvol.parkinson_number(df_future_c1_daily,n=5)
# df_gk_1w = Histvol.garman_klass(df_future_c1_daily,n=5)
df_data = df_data.join(df_cc_1w,on=c.Util.DT_DATE,how='left')
# df_data = df_data.join(df_p_1w,on=c.Util.DT_DATE,how='left')
# df_data = df_data.join(df_gk_1w,on=c.Util.DT_DATE,how='left')

df_cc_3m = Histvol.hist_vol(df_future_c1_daily,n=60)
# df_p_3m = Histvol.parkinson_number(df_future_c1_daily,n=60)
Ejemplo n.º 8
0
from Utilities import timebase
import numpy as np

pu = PlotUtil()
start_date = datetime.date(2010, 1, 1)
end_date = datetime.date.today()
dt_histvol = start_date - datetime.timedelta(days=40)
# min_holding = 18

name_code = name_code_option = c.Util.STR_CU
# df_metrics = get_data.get_comoption_mktdata(start_date, end_date,name_code)
df_future_c1_daily = get_data.get_mktdata_future_c1_daily(
    dt_histvol, end_date, name_code)
# df_future_c1_daily = get_data.get_index_mktdata(dt_histvol, end_date, name_code)
""" 历史波动率 """
vol_10 = Histvol.hist_vol(df_future_c1_daily[c.Util.AMT_CLOSE], n=10)
vol_20 = Histvol.hist_vol(df_future_c1_daily[c.Util.AMT_CLOSE], n=20)
vol_30 = Histvol.hist_vol(df_future_c1_daily[c.Util.AMT_CLOSE], n=30)
vol_60 = Histvol.hist_vol(df_future_c1_daily[c.Util.AMT_CLOSE], n=60)
vol_90 = Histvol.hist_vol(df_future_c1_daily[c.Util.AMT_CLOSE], n=90)

df = df_future_c1_daily[[c.Util.DT_DATE]]
df['hist_vol_10'] = vol_10
df['hist_vol_20'] = vol_20
df['hist_vol_30'] = vol_30
df['hist_vol_60'] = vol_60
df['hist_vol_90'] = vol_90
df = df.dropna()
df = df.sort_values(c.Util.DT_DATE, ascending=False)
print(df)
df.to_csv('../../../data/' + name_code + '_histvol.csv')
Ejemplo n.º 9
0
    m_id_c1 = dict_core_underlying[namecode]
    # m_df_future_c1 = get_mktdata_future_c1(dt_start, dt_date, namecode)
    m_df_future_c1 = get_data.get_future_c1_by_option_daily(dt_start,
                                                            dt_date,
                                                            namecode,
                                                            min_holding=5)
    # m_df_future = get_mktdata_future(admin.table_futures_mktdata(), m_id_c1, dt_yesterday, dt_date)
    m_dict_iv_call, m_dict_iv_put = iv_at_the_money(dt_date, dt_yesterday,
                                                    namecode)
    m_iv_call_today = m_dict_iv_call[dt_date]
    m_iv_put_today = m_dict_iv_put[dt_date]
    m_iv_call_yesterday = m_dict_iv_call[dt_yesterday]
    m_iv_put_yesterday = m_dict_iv_put[dt_yesterday]
    m_df_future_c1['hist_60-1'] = calculate_histvol(
        m_df_future_c1['amt_close'], 60)
    m_df_future_c1['hist_60-2'] = Histvol.hist_vol(m_df_future_c1['amt_close'],
                                                   60)
    m_hisvol_1M = list(calculate_histvol(m_df_future_c1['amt_close'],
                                         20))[-1] * 100
    m_hisvol_3M = list(calculate_histvol(m_df_future_c1['amt_close'],
                                         60))[-1] * 100
    dict_iv = iv_htbr(dt_date, dt_yesterday, namecode)
    iv_today = dict_iv[dt_date]
    iv_last = dict_iv[dt_yesterday]

    text_2 = '隐含波动率方面,{}期权{}月合约隐含波动率整体{},其中,看跌期权平值合约隐含波动率为{:.2f}%,' \
             '较前一周({:.2f}%){}约{:.1f}%,' \
             '看涨期权平值合约隐含波动率为{:.2f}%,较前一周({:.2f}%){}约{:.1f}%。' \
             '从认沽认购平价隐含波动率来看,{}期权隐含波动率为{:.2f}%,前一周该值为{:.2f}%,{}{:.2f}%。' \
             '期货主力合约近1M历史波动率为{:.2f}%,近3M历史波动率为{:.2f}%,' \
             '平值期权隐含波动率较近1M历史波动率{},较近3M历史波动率{}{}。'\
             '\n \n'.format(