def forecast_realized_vol_until_expiration(**kwargs):

    past_realized_vol_frame = get_past_realized_vol_until_expiration(**kwargs)

    kwargs['settle_date'] = kwargs['date_to']
    real_vol20_current = ops.calc_realized_vol_4options_ticker(**kwargs)

    realized_vol_forecast = np.NaN
    clean_indx = (past_realized_vol_frame['real_vol_till_expiration'].notnull())&(past_realized_vol_frame['real_vol20'].notnull())

    if sum(clean_indx) > 10:

        clean_vol_frame = past_realized_vol_frame[clean_indx]
        clean_vol_frame['real_vol_diff'] = abs(clean_vol_frame['real_vol20']-real_vol20_current)
        clean_vol_frame['forecast_multiplier'] = clean_vol_frame['real_vol_till_expiration']/clean_vol_frame['real_vol20']

        clean_vol_frame.sort('real_vol_diff',ascending=True,inplace=True)

        num_relevant_obs = max(round(len(clean_vol_frame.index)/5), 10)

        if sum(clean_indx) <= 20:
            forecast_multiplier_mean = sum([clean_vol_frame['forecast_multiplier'].iloc[i]*(num_relevant_obs-i) for i in range(num_relevant_obs)])/sum([(num_relevant_obs-i) for i in range(num_relevant_obs)])
        else:
            forecast_multiplier_mean = clean_vol_frame['forecast_multiplier'][:num_relevant_obs].mean()

        realized_vol_forecast = forecast_multiplier_mean*real_vol20_current

    return {'realized_vol_forecast': realized_vol_forecast, 'real_vol20_current': real_vol20_current}
示例#2
0
def load_ticker_signals_4settle_date(**kwargs):

    if 'con' not in kwargs.keys():
        close_connection_before_exit = True
        con = msu.get_my_sql_connection(**kwargs)
        kwargs['con'] = con
    else:
        close_connection_before_exit = False
        con = kwargs['con']

    if not exp.is_business_day(double_date=kwargs['settle_date']):
        if close_connection_before_exit:
            con.close()
        return

    options_frame = cl.generate_liquid_options_list_dataframe(**kwargs)

    futures_data_dictionary = {
        x: gfp.get_futures_price_preloaded(ticker_head=x)
        for x in cmi.get_tickerhead_list('cme_futures')
    }

    real_vol_output = [
        ops.calc_realized_vol_4options_ticker(
            ticker=x,
            futures_data_dictionary=futures_data_dictionary,
            settle_date=kwargs['settle_date']) for x in options_frame['ticker']
    ]

    delta_vol_output = [
        ops.calc_delta_vol_4ticker(ticker=x,
                                   settle_date=kwargs['settle_date'],
                                   delta_target=0.5)
        for x in options_frame['ticker']
    ]

    volume_interest_output = [
        ops.calc_volume_interest_4ticker(ticker=x,
                                         settle_date=kwargs['settle_date'])
        for x in options_frame['ticker']
    ]

    atm_vol_frame = pd.concat([
        options_frame,
        pd.DataFrame(delta_vol_output),
        pd.DataFrame(volume_interest_output)
    ],
                              axis=1)
    atm_vol_frame['real_vol'] = real_vol_output

    contract_specs_output = [
        cmi.get_contract_specs(x) for x in atm_vol_frame['ticker']
    ]

    atm_vol_frame['ticker_head'] = [
        x['ticker_head'] for x in contract_specs_output
    ]
    atm_vol_frame['ticker_month'] = [
        x['ticker_month_num'] for x in contract_specs_output
    ]
    atm_vol_frame['ticker_year'] = [
        x['ticker_year'] for x in contract_specs_output
    ]

    column_names = atm_vol_frame.columns.tolist()

    ticker_indx = column_names.index('ticker')
    cal_dte_indx = column_names.index('cal_dte')
    tr_dte_indx = column_names.index('tr_dte')
    ticker_head_indx = column_names.index('ticker_head')
    ticker_month_indx = column_names.index('ticker_month')
    ticker_year_indx = column_names.index('ticker_year')
    imp_vol_indx = column_names.index('delta_vol')
    strike_indx = column_names.index('strike')
    theta_indx = column_names.index('theta')
    open_interest_indx = column_names.index('open_interest')
    volume_indx = column_names.index('volume')
    real_vol_indx = column_names.index('real_vol')

    now = dt.datetime.now()
    settle_datetime = cu.convert_doubledate_2datetime(kwargs['settle_date'])

    tuples = [
        tuple([
            x[ticker_indx], x[ticker_head_indx], x[ticker_month_indx],
            x[ticker_year_indx],
            None if np.isnan(x[cal_dte_indx]) else x[cal_dte_indx],
            None if np.isnan(x[tr_dte_indx]) else x[tr_dte_indx],
            None if np.isnan(x[imp_vol_indx]) else 100 * x[imp_vol_indx], 0.5,
            None if np.isnan(x[theta_indx]) else x[theta_indx],
            None if np.isnan(x[strike_indx]) else x[strike_indx],
            None if np.isnan(x[real_vol_indx]) else x[real_vol_indx],
            None if np.isnan(x[open_interest_indx]) else x[open_interest_indx],
            None if np.isnan(x[volume_indx]) else x[volume_indx],
            settle_datetime.date(), now, now
        ]) for x in atm_vol_frame.values
    ]

    column_str = "ticker, ticker_head, ticker_month, ticker_year, " \
                 " cal_dte, tr_dte, imp_vol, delta, theta, strike, " \
                 "close2close_vol20, open_interest, volume, price_date, created_date, last_updated_date"

    insert_str = ("%s, " * len(column_str.split(',')))[:-2]
    final_str = "REPLACE INTO option_ticker_indicators (%s) VALUES (%s)" % (
        column_str, insert_str)

    msu.sql_execute_many_wrapper(final_str=final_str, tuples=tuples, con=con)

    load_pnls_4settle_date(**kwargs)

    if close_connection_before_exit:
        con.close()