示例#1
0
def get_scv_signals(**kwargs):

    ticker = kwargs['ticker']
    date_to = kwargs['date_to']

    con = msu.get_my_sql_connection(**kwargs)

    if 'futures_data_dictionary' in kwargs.keys():
        futures_data_dictionary = kwargs['futures_data_dictionary']
    else:
        futures_data_dictionary = {
            x: gfp.get_futures_price_preloaded(ticker_head=x)
            for x in [cmi.get_contract_specs(ticker)['ticker_head']]
        }

    aligned_indicators_output = ops.get_aligned_option_indicators(
        ticker_list=[ticker], settle_date=date_to, con=con)

    if not aligned_indicators_output['success']:
        return {
            'downside': np.NaN,
            'upside': np.NaN,
            'theta': np.NaN,
            'realized_vol_forecast': np.NaN,
            'real_vol20_current': np.NaN,
            'imp_vol': np.NaN,
            'imp_vol_premium': np.NaN,
            'q': np.NaN
        }

    hist = aligned_indicators_output['hist']
    current = aligned_indicators_output['current']

    vcs_output = vcs.generate_vcs_sheet_4date(date_to=date_to, con=con)

    if 'con' not in kwargs.keys():
        con.close()

    clean_indx = hist['c1']['profit5'].notnull()
    clean_data = hist[clean_indx]

    if clean_data.empty:
        downside = np.NaN
        upside = np.NaN
    else:
        last_available_align_date = clean_data.index[-1]
        clean_data = clean_data[clean_data.index >= last_available_align_date -
                                dt.timedelta(5 * 365)]
        profit5 = clean_data['c1']['profit5']

        percentile_vector = stats.get_number_from_quantile(
            y=profit5.values,
            quantile_list=[1, 15, 85, 99],
            clean_num_obs=max(100, round(3 * len(profit5.values) / 4)))

        downside = (percentile_vector[0] + percentile_vector[1]) / 2
        upside = (percentile_vector[2] + percentile_vector[3]) / 2

    realized_vol_output = rvue.forecast_realized_vol_until_expiration(
        ticker=ticker,
        futures_data_dictionary=futures_data_dictionary,
        date_to=date_to)

    realized_vol_forecast = realized_vol_output['realized_vol_forecast']
    real_vol20_current = realized_vol_output['real_vol20_current']
    imp_vol = current['imp_vol'][0]

    imp_vol_premium = 100 * (imp_vol - realized_vol_forecast) / imp_vol

    q = np.NaN

    if vcs_output['success']:
        vcs_pairs = vcs_output['vcs_pairs']
        selected_pairs = vcs_pairs[vcs_pairs['ticker2'] == ticker]
        if not selected_pairs.empty:
            q = 100 - selected_pairs['Q'].mean()

    return {
        'downside': downside,
        'upside': upside,
        'theta': current['theta'][0],
        'realized_vol_forecast': realized_vol_forecast,
        'real_vol20_current': real_vol20_current,
        'imp_vol': imp_vol,
        'imp_vol_premium': imp_vol_premium,
        'q': q
    }
示例#2
0
def get_vcs_panel_plot(**kwargs):

    ticker_list = kwargs['ticker_list']

    if 'diagnostics_q' in kwargs.keys():
        diagnostics_q = kwargs['diagnostics_q']
    else:
        diagnostics_q = False

    #con = msu.get_my_sql_connection(**kwargs)

    option_indicator_output = ops.get_aligned_option_indicators(
        ticker_list=ticker_list, settle_date=kwargs['report_date'])
    hist = option_indicator_output['hist']

    new_index = list(range(len(hist.index)))
    contract_change_indx = (hist['c1']['ticker_year'] -
                            hist['c1']['ticker_year'].shift(1) != 0).values
    front_contract_year = hist['c1']['ticker_year'].astype('int') % 10

    contract_change_indx[0] = False

    x_tick_locations = [x for x in new_index if contract_change_indx[x]]

    x_tick_values = [
        cmi.letter_month_string[int(hist['c1']['ticker_month'].values[x]) - 1]
        + str(front_contract_year.values[x]) for x in new_index
        if contract_change_indx[x]
    ]

    plt.figure(figsize=(16, 7))
    plt.plot(range(len(hist.index)),
             hist['c1']['imp_vol'] / hist['c2']['imp_vol'])
    plt.xticks(x_tick_locations, x_tick_values)
    plt.ylabel('atmVolRatio')
    plt.grid()
    plt.show()

    fwd_var = hist['c2']['cal_dte'] * (hist['c2']['imp_vol']**
                                       2) - hist['c1']['cal_dte'] * (
                                           hist['c1']['imp_vol']**2)
    fwd_vol_sq = fwd_var / (hist['c2']['cal_dte'] - hist['c1']['cal_dte'])
    fwd_vol_adj = np.sign(fwd_vol_sq) * ((abs(fwd_vol_sq)).apply(np.sqrt))
    hist['fwd_vol_adj'] = fwd_vol_adj

    plt.figure(figsize=(16, 7))
    plt.plot(range(len(hist.index)), hist['fwd_vol_adj'])
    plt.xticks(x_tick_locations, x_tick_values)
    plt.ylabel('Fwd Vol')
    plt.grid()
    plt.show()

    plt.figure(figsize=(16, 7))
    plt.plot(range(len(hist.index)),
             hist['c1']['close2close_vol20'] / hist['c2']['close2close_vol20'])
    plt.xticks(x_tick_locations, x_tick_values)
    plt.ylabel('realVolRatio')
    plt.grid()
    plt.show()

    plt.figure(figsize=(16, 7))
    plt.plot(range(len(hist.index)),
             hist['c1']['imp_vol'] / hist['c1']['close2close_vol20'])
    plt.xticks(x_tick_locations, x_tick_values)
    plt.ylabel('atmRealVolRatio')
    plt.grid()
    plt.show()

    if diagnostics_q:
        plt.figure(figsize=(16, 7))
        plt.plot(range(len(hist.index)), hist['c1']['ticker_year'])
        plt.xticks(x_tick_locations, x_tick_values)
        plt.ylabel('ticker_year')
        plt.grid()
        plt.show()

        plt.figure(figsize=(16, 7))
        plt.plot(range(len(hist.index)),
                 hist['c1']['tr_dte'] - hist['c2']['tr_dte'])
        plt.xticks(x_tick_locations, x_tick_values)
        plt.ylabel('tr dte diff')
        plt.grid()
        plt.show()

        plt.figure(figsize=(16, 7))
        plt.plot(range(len(hist.index)), hist['c1']['tr_dte'])
        plt.xticks(x_tick_locations, x_tick_values)
        plt.ylabel('tr dte')
        plt.grid()
        plt.show()

        plt.figure(figsize=(16, 7))
        plt.plot(range(len(hist.index)), hist['c1']['ticker_month'])
        plt.xticks(x_tick_locations, x_tick_values)
        plt.ylabel('ticker_month')
        plt.grid()
        plt.show()
def get_scv_signals(**kwargs):

    ticker = kwargs['ticker']
    date_to = kwargs['date_to']

    con = msu.get_my_sql_connection(**kwargs)

    if 'futures_data_dictionary' in kwargs.keys():
        futures_data_dictionary = kwargs['futures_data_dictionary']
    else:
        futures_data_dictionary = {x: gfp.get_futures_price_preloaded(ticker_head=x) for x in [cmi.get_contract_specs(ticker)['ticker_head']]}

    aligned_indicators_output = ops.get_aligned_option_indicators(ticker_list=[ticker],
                                                                  settle_date=date_to, con=con)

    if not aligned_indicators_output['success']:
        return {'downside': np.NaN, 'upside': np.NaN, 'theta': np.NaN,
                'realized_vol_forecast': np.NaN,
                'real_vol20_current': np.NaN,
                'imp_vol': np.NaN,
                'imp_vol_premium': np.NaN,
                'q': np.NaN}

    hist = aligned_indicators_output['hist']
    current = aligned_indicators_output['current']

    vcs_output = vcs.generate_vcs_sheet_4date(date_to=date_to,con=con)

    if 'con' not in kwargs.keys():
            con.close()

    clean_indx = hist['c1']['profit5'].notnull()
    clean_data = hist[clean_indx]

    if clean_data.empty:
        downside = np.NaN
        upside = np.NaN
    else:
        last_available_align_date = clean_data.index[-1]
        clean_data = clean_data[clean_data.index >= last_available_align_date-dt.timedelta(5*365)]
        profit5 = clean_data['c1']['profit5']

        percentile_vector = stats.get_number_from_quantile(y=profit5.values,
                                                       quantile_list=[1, 15, 85, 99],
                                                       clean_num_obs=max(100, round(3*len(profit5.values)/4)))

        downside = (percentile_vector[0]+percentile_vector[1])/2
        upside = (percentile_vector[2]+percentile_vector[3])/2

    realized_vol_output = rvue.forecast_realized_vol_until_expiration(ticker=ticker,
                                                futures_data_dictionary=futures_data_dictionary,
                                                date_to=date_to)

    realized_vol_forecast = realized_vol_output['realized_vol_forecast']
    real_vol20_current = realized_vol_output['real_vol20_current']
    imp_vol = current['imp_vol'][0]

    imp_vol_premium = 100*(imp_vol-realized_vol_forecast)/imp_vol

    q = np.NaN

    if vcs_output['success']:
        vcs_pairs = vcs_output['vcs_pairs']
        selected_pairs = vcs_pairs[vcs_pairs['ticker2'] == ticker]
        if not selected_pairs.empty:
            q = 100-selected_pairs['Q'].mean()

    return {'downside': downside, 'upside': upside, 'theta': current['theta'][0],
            'realized_vol_forecast': realized_vol_forecast,
            'real_vol20_current': real_vol20_current,
            'imp_vol': imp_vol,
            'imp_vol_premium': imp_vol_premium,
            'q': q}
def get_vcs_panel_plot(**kwargs):

    ticker_list = kwargs['ticker_list']

    if 'diagnostics_q' in kwargs.keys():
        diagnostics_q = kwargs['diagnostics_q']
    else:
        diagnostics_q = False

    #con = msu.get_my_sql_connection(**kwargs)

    option_indicator_output = ops.get_aligned_option_indicators(ticker_list=ticker_list,settle_date=kwargs['report_date'])
    hist = option_indicator_output['hist']

    new_index = list(range(len(hist.index)))
    contract_change_indx = (hist['c1']['ticker_year']-hist['c1']['ticker_year'].shift(1) != 0).values
    front_contract_year = hist['c1']['ticker_year'].astype('int') % 10

    contract_change_indx[0] = False

    x_tick_locations = [x for x in new_index if contract_change_indx[x]]

    x_tick_values = [cmi.letter_month_string[int(hist['c1']['ticker_month'].values[x])-1]+
                     str(front_contract_year.values[x]) for x in new_index if contract_change_indx[x]]

    plt.figure(figsize=(16, 7))
    plt.plot(hist['c1']['imp_vol']/hist['c2']['imp_vol'])
    plt.xticks(x_tick_locations,x_tick_values)
    plt.ylabel('atmVolRatio')
    plt.grid()
    plt.show()

    fwd_var = hist['c2']['cal_dte']*(hist['c2']['imp_vol']**2)-hist['c1']['cal_dte']*(hist['c1']['imp_vol']**2)
    fwd_vol_sq = fwd_var/(hist['c2']['cal_dte']-hist['c1']['cal_dte'])
    fwd_vol_adj = np.sign(fwd_vol_sq)*((abs(fwd_vol_sq)).apply(np.sqrt))
    hist['fwd_vol_adj'] = fwd_vol_adj

    plt.figure(figsize=(16, 7))
    plt.plot(hist['fwd_vol_adj'])
    plt.xticks(x_tick_locations,x_tick_values)
    plt.ylabel('Fwd Vol')
    plt.grid()
    plt.show()

    plt.figure(figsize=(16, 7))
    plt.plot(hist['c1']['close2close_vol20']/hist['c2']['close2close_vol20'])
    plt.xticks(x_tick_locations,x_tick_values)
    plt.ylabel('realVolRatio')
    plt.grid()
    plt.show()

    plt.figure(figsize=(16, 7))
    plt.plot(hist['c1']['imp_vol']/hist['c1']['close2close_vol20'])
    plt.xticks(x_tick_locations,x_tick_values)
    plt.ylabel('atmRealVolRatio')
    plt.grid()
    plt.show()

    if diagnostics_q:
        plt.figure(figsize=(16, 7))
        plt.plot(hist['c1']['ticker_year'])
        plt.xticks(x_tick_locations,x_tick_values)
        plt.ylabel('ticker_year')
        plt.grid()
        plt.show()

        plt.figure(figsize=(16, 7))
        plt.plot(hist['c1']['tr_dte']-hist['c2']['tr_dte'])
        plt.xticks(x_tick_locations,x_tick_values)
        plt.ylabel('tr dte diff')
        plt.grid()
        plt.show()

        plt.figure(figsize=(16, 7))
        plt.plot(hist['c1']['tr_dte'])
        plt.xticks(x_tick_locations,x_tick_values)
        plt.ylabel('tr dte')
        plt.grid()
        plt.show()

        plt.figure(figsize=(16, 7))
        plt.plot(hist['c1']['ticker_month'])
        plt.xticks(x_tick_locations,x_tick_values)
        plt.ylabel('ticker_month')
        plt.grid()
        plt.show()