def get_intraday_vcs(**kwargs):

    if 'report_date' in kwargs.keys():
        report_date = kwargs['report_date']
    else:
        report_date = exp.doubledate_shift_bus_days()

    atm_vol_ratio = kwargs['atm_vol_ratio']

    vcs_output = vcs.generate_vcs_sheet_4date(date_to=report_date)
    vcs_pairs = vcs_output['vcs_pairs']

    if 'id' in kwargs.keys():
        id = kwargs['id']
        ticker1 = vcs_pairs['ticker1'].iloc[id]
        ticker2 = vcs_pairs['ticker2'].iloc[id]
    else:
        ticker1 = kwargs['ticker1']
        ticker2 = kwargs['ticker2']

    ticker_head = cmi.get_contract_specs(ticker1)['ticker_head']
    ticker_class = cmi.ticker_class[ticker_head]

    vcs_output = ops.get_vcs_signals(ticker_list=[ticker1, ticker2],
                                     settle_date=report_date,
                                     atm_vol_ratio=atm_vol_ratio)

    q = vcs_output['q']
    q1 = vcs_output['q1']

    filter_out = of.get_vcs_filters(data_frame_input=pd.DataFrame.from_dict({
        'tickerHead': [ticker_head],
        'tickerClass': [ticker_class],
        'Q': [q],
        'Q1': [q1]
    }),
                                    filter_list=['long2', 'short2'])

    if filter_out['selected_frame'].empty:
        validQ = False
    else:
        validQ = True

    return {
        'ticker1': ticker1,
        'ticker2': ticker2,
        'Q': q,
        'Q1': q1,
        'validQ': validQ
    }
def generate_vcs_formatted_output(**kwargs):

    if 'report_date' in kwargs.keys():
        report_date = kwargs['report_date']
    else:
        report_date = exp.doubledate_shift_bus_days()

    output_dir = ts.create_strategy_output_dir(strategy_class='vcs',
                                               report_date=report_date)

    vcs_output = vcs.generate_vcs_sheet_4date(date_to=report_date)
    vcs_pairs = vcs_output['vcs_pairs']

    filter_out = of.get_vcs_filters(data_frame_input=vcs_pairs,
                                    filter_list=['long2', 'short2'])
    good_vcs_pairs = filter_out['selected_frame']

    vcs_pairs_w_selected_columns = vcs_pairs[[
        'ticker1', 'ticker2', 'tickerHead', 'tickerClass', 'trDte1', 'trDte2',
        'Q', 'Q1', 'fwdVolQ', 'downside', 'upside', 'atmVolRatio', 'fwdVol',
        'realVolRatio', 'atmRealVolRatio', 'theta'
    ]]

    good_vcs_pairs_w_selected_columns = good_vcs_pairs[[
        'ticker1', 'ticker2', 'tickerHead', 'tickerClass', 'trDte1', 'trDte2',
        'Q', 'Q1', 'fwdVolQ', 'downside', 'upside', 'atmVolRatio', 'fwdVol',
        'realVolRatio', 'atmRealVolRatio', 'theta'
    ]]

    writer = pd.ExcelWriter(output_dir + '/' + futil.xls_file_names['vcs'] +
                            '.xlsx',
                            engine='xlsxwriter')

    vcs_pairs_w_selected_columns.to_excel(writer, sheet_name='all')
    good_vcs_pairs_w_selected_columns.to_excel(writer, sheet_name='good')

    worksheet_good = writer.sheets['good']
    worksheet_all = writer.sheets['all']

    worksheet_good.freeze_panes(1, 0)
    worksheet_all.freeze_panes(1, 0)

    worksheet_good.autofilter(0, 0,
                              len(good_vcs_pairs_w_selected_columns.index),
                              len(good_vcs_pairs_w_selected_columns.columns))

    worksheet_all.autofilter(0, 0, len(vcs_pairs_w_selected_columns.index),
                             len(vcs_pairs_w_selected_columns.columns))

    writer.save()
def generate_vcs_formatted_output(**kwargs):

    if 'report_date' in kwargs.keys():
        report_date = kwargs['report_date']
    else:
        report_date = exp.doubledate_shift_bus_days()

    output_dir = ts.create_strategy_output_dir(strategy_class='vcs', report_date=report_date)

    vcs_output = vcs.generate_vcs_sheet_4date(date_to=report_date)
    vcs_pairs = vcs_output['vcs_pairs']

    filter_out = of.get_vcs_filters(data_frame_input=vcs_pairs, filter_list=['long2', 'short2'])
    good_vcs_pairs = filter_out['selected_frame']

    vcs_pairs_w_selected_columns = vcs_pairs[['ticker1', 'ticker2', 'tickerHead', 'tickerClass', 'trDte1', 'trDte2', 'Q',
                                              'Q1', 'fwdVolQ', 'downside', 'upside', 'atmVolRatio', 'fwdVol','realVolRatio',
                                              'atmRealVolRatio','theta']]

    good_vcs_pairs_w_selected_columns = good_vcs_pairs[['ticker1', 'ticker2', 'tickerHead', 'tickerClass', 'trDte1', 'trDte2', 'Q',
                                              'Q1', 'fwdVolQ', 'downside', 'upside', 'atmVolRatio', 'fwdVol','realVolRatio',
                                              'atmRealVolRatio','theta']]

    writer = pd.ExcelWriter(output_dir + '/' + futil.xls_file_names['vcs'] + '.xlsx', engine='xlsxwriter')

    vcs_pairs_w_selected_columns.to_excel(writer, sheet_name='all')
    good_vcs_pairs_w_selected_columns.to_excel(writer, sheet_name='good')

    worksheet_good = writer.sheets['good']
    worksheet_all = writer.sheets['all']

    worksheet_good.freeze_panes(1, 0)
    worksheet_all.freeze_panes(1, 0)

    worksheet_good.autofilter(0, 0, len(good_vcs_pairs_w_selected_columns.index),
                              len(good_vcs_pairs_w_selected_columns.columns))

    worksheet_all.autofilter(0, 0, len(vcs_pairs_w_selected_columns.index),
                                   len(vcs_pairs_w_selected_columns.columns))
def get_intraday_vcs(**kwargs):

    if 'report_date' in kwargs.keys():
        report_date = kwargs['report_date']
    else:
        report_date = exp.doubledate_shift_bus_days()

    id = kwargs['id']
    atm_vol_ratio = kwargs['atm_vol_ratio']

    vcs_output = vcs.generate_vcs_sheet_4date(date_to=report_date)
    vcs_pairs = vcs_output['vcs_pairs']

    ticker1 = vcs_pairs['ticker1'].iloc[id]
    ticker2 = vcs_pairs['ticker2'].iloc[id]

    ticker_head = cmi.get_contract_specs(ticker1)['ticker_head']
    ticker_class = cmi.ticker_class[ticker_head]

    vcs_output = ops.get_vcs_signals(ticker_list=[ticker1, ticker2],settle_date=report_date,atm_vol_ratio=atm_vol_ratio)

    q = vcs_output['q']
    q1 = vcs_output['q1']

    filter_out = of.get_vcs_filters(data_frame_input=pd.DataFrame.from_items([('tickerHead', [ticker_head]),
                                                                              ('tickerClass', [ticker_class]),
                                                                              ('Q', [q]), ('Q1', [q1])]), filter_list=['long2', 'short2'])

    if filter_out['selected_frame'].empty:
        validQ = False
    else:
        validQ = True

    print(ticker1)
    print(ticker2)
    print('Q: ' + str(q))
    print('Q1: ' + str(q1))
    print('Valid?: ' + str(validQ))
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
    }
Exemple #6
0
try:
    log.info('update_options_greeks')
    ogl.update_options_greeks_4date(con=con, settle_date=folder_date)
except Exception:
    log.error('update_options_greeks failed', exc_info=True)

try:
    log.info('load_ticker_signals')
    osl.load_ticker_signals_4settle_date(con=con, settle_date=folder_date)
except Exception:
    log.error('load_ticker_signals failed', exc_info=True)

try:
    log.info('generate_vcs_sheet')
    vcs.generate_vcs_sheet_4date(con=con, date_to=folder_date)
except Exception:
    log.error('generate_vcs_sheet failed', exc_info=True)

try:
    osf.generate_vcs_formatted_output(report_date=folder_date)
    prep.prepare_strategy_daily(strategy_class='vcs', report_date=folder_date)
except Exception:
    pass

try:
    log.info('generate_scv_sheet')
    osf.generate_scv_formatted_output(report_date=folder_date)
    prep.prepare_strategy_daily(strategy_class='scv', report_date=folder_date)
except Exception:
    log.error('generate_scv_sheet failed', exc_info=True)
def main():
    app = algo.Algo()
    report_date = exp.doubledate_shift_bus_days()
    todays_date = cu.get_doubledate()
    con = msu.get_my_sql_connection()
    vcs_output = vcs.generate_vcs_sheet_4date(date_to=report_date)
    vcs_pairs = vcs_output['vcs_pairs']

    filter_out = of.get_vcs_filters(data_frame_input=vcs_pairs,
                                    filter_list=['long2', 'short2'])
    vcs_pairs = filter_out['selected_frame']

    vcs_pairs = vcs_pairs[vcs_pairs['downside'].notnull()
                          & vcs_pairs['upside'].notnull()]
    # &(vcs_pairs.tickerClass!='Energy')
    vcs_pairs = vcs_pairs[(vcs_pairs['trDte1'] >= 50)
                          & (vcs_pairs.tickerClass != 'Metal') &
                          (vcs_pairs.tickerClass != 'FX') &
                          (vcs_pairs.tickerClass != 'Energy')]
    vcs_pairs = vcs_pairs[((vcs_pairs['Q'] <= 30) &
                           (vcs_pairs['fwdVolQ'] >= 30)) |
                          ((vcs_pairs['Q'] >= 70) &
                           (vcs_pairs['fwdVolQ'] <= 70))]
    vcs_pairs.reset_index(drop=True, inplace=True)

    vcs_pairs['underlying_ticker1'] = [
        omu.get_option_underlying(ticker=x) for x in vcs_pairs['ticker1']
    ]
    vcs_pairs['underlying_ticker2'] = [
        omu.get_option_underlying(ticker=x) for x in vcs_pairs['ticker2']
    ]

    vcs_pairs['underlying_tickerhead'] = [
        cmi.get_contract_specs(x)['ticker_head']
        for x in vcs_pairs['underlying_ticker1']
    ]
    futures_data_dictionary = {
        x: gfp.get_futures_price_preloaded(ticker_head=x)
        for x in vcs_pairs['underlying_tickerhead'].unique()
    }

    proxy_output_list1 = [
        up.get_underlying_proxy_ticker(
            ticker=x,
            settle_date=report_date,
            futures_data_dictionary=futures_data_dictionary)
        for x in vcs_pairs['underlying_ticker1']
    ]
    vcs_pairs['proxy_ticker1'] = [x['ticker'] for x in proxy_output_list1]
    vcs_pairs['add_2_proxy1'] = [x['add_2_proxy'] for x in proxy_output_list1]

    proxy_output_list2 = [
        up.get_underlying_proxy_ticker(
            ticker=x,
            settle_date=report_date,
            futures_data_dictionary=futures_data_dictionary)
        for x in vcs_pairs['underlying_ticker2']
    ]
    vcs_pairs['proxy_ticker2'] = [x['ticker'] for x in proxy_output_list2]
    vcs_pairs['add_2_proxy2'] = [x['add_2_proxy'] for x in proxy_output_list2]

    vcs_pairs['expiration_date1'] = [
        int(
            exp.get_expiration_from_db(instrument='options', ticker=x,
                                       con=con).strftime('%Y%m%d'))
        for x in vcs_pairs['ticker1']
    ]
    vcs_pairs['expiration_date2'] = [
        int(
            exp.get_expiration_from_db(instrument='options', ticker=x,
                                       con=con).strftime('%Y%m%d'))
        for x in vcs_pairs['ticker2']
    ]

    vcs_pairs['interest_date1'] = [
        grfs.get_simple_rate(as_of_date=report_date, date_to=x)['rate_output']
        for x in vcs_pairs['expiration_date1']
    ]
    vcs_pairs['interest_date2'] = [
        grfs.get_simple_rate(as_of_date=report_date, date_to=x)['rate_output']
        for x in vcs_pairs['expiration_date2']
    ]
    vcs_pairs['exercise_type'] = [
        cmi.get_option_exercise_type(ticker_head=x)
        for x in vcs_pairs['tickerHead']
    ]

    admin_dir = dna.get_directory_name(ext='admin')
    risk_file_out = su.read_text_file(file_name=admin_dir +
                                      '/RiskParameter.txt')
    vcs_risk_parameter = 5 * 2 * float(risk_file_out[0])

    vcs_pairs['long_quantity'] = vcs_risk_parameter / abs(
        vcs_pairs['downside'])
    vcs_pairs['short_quantity'] = vcs_risk_parameter / vcs_pairs['upside']
    vcs_pairs['long_quantity'] = vcs_pairs['long_quantity'].round()
    vcs_pairs['short_quantity'] = vcs_pairs['short_quantity'].round()

    vcs_pairs['alias'] = [
        generate_vcs_alias(vcs_row=vcs_pairs.iloc[x])
        for x in range(len(vcs_pairs.index))
    ]

    vcs_pairs['call_mid_price1'] = np.nan
    vcs_pairs['put_mid_price1'] = np.nan
    vcs_pairs['call_mid_price2'] = np.nan
    vcs_pairs['put_mid_price2'] = np.nan
    vcs_pairs['call_iv1'] = np.nan
    vcs_pairs['put_iv1'] = np.nan
    vcs_pairs['call_iv2'] = np.nan
    vcs_pairs['put_iv2'] = np.nan
    vcs_pairs['underlying_mid_price1'] = np.nan
    vcs_pairs['underlying_mid_price2'] = np.nan
    vcs_pairs['proxy_mid_price1'] = np.nan
    vcs_pairs['proxy_mid_price2'] = np.nan
    vcs_pairs['current_strike1'] = np.nan
    vcs_pairs['current_strike2'] = np.nan

    ta_folder = dn.get_dated_directory_extension(folder_date=todays_date,
                                                 ext='ta')

    app.vcs_pairs = vcs_pairs
    app.con = con
    app.futures_data_dictionary = futures_data_dictionary
    app.report_date = report_date
    app.todays_date = todays_date
    app.log = lg.get_logger(file_identifier='vcs', log_level='INFO')
    app.trade_file = ta_folder + '/trade_dir.csv'
    app.vcs_risk_parameter = vcs_risk_parameter
    app.connect(client_id=3)
    app.run()
Exemple #8
0
def get_results_4strategy(**kwargs):

    signal_input = dict()

    if 'futures_data_dictionary' in kwargs.keys():
        signal_input['futures_data_dictionary'] = kwargs[
            'futures_data_dictionary']

    if 'date_to' in kwargs.keys():
        date_to = kwargs['date_to']
    else:
        date_to = exp.doubledate_shift_bus_days()

    if 'datetime5_years_ago' in kwargs.keys():
        signal_input['datetime5_years_ago'] = kwargs['datetime5_years_ago']

    if 'strategy_info_output' in kwargs.keys():
        strategy_info_output = kwargs['strategy_info_output']
    else:
        strategy_info_output = ts.get_strategy_info_from_alias(**kwargs)

    if 'broker' in kwargs.keys():
        broker = kwargs['broker']
    else:
        broker = 'abn'

    con = msu.get_my_sql_connection(**kwargs)

    strategy_info_dict = sc.convert_from_string_to_dictionary(
        string_input=strategy_info_output['description_string'])

    strategy_class = strategy_info_dict['strategy_class']

    pnl_frame = tpm.get_daily_pnl_snapshot(as_of_date=date_to, broker=broker)
    pnl_frame = pnl_frame[pnl_frame['alias'] == kwargs['alias']]
    strategy_position = ts.get_net_position_4strategy_alias(
        alias=kwargs['alias'], as_of_date=date_to)

    if strategy_class == 'futures_butterfly':

        ticker_head = cmi.get_contract_specs(
            strategy_info_dict['ticker1'])['ticker_head']
        if not strategy_position.empty:
            total_contracts2trade = strategy_position['qty'].abs().sum()
            t_cost = cmi.t_cost[ticker_head]
        QF_initial = float(strategy_info_dict['QF'])
        z1_initial = float(strategy_info_dict['z1'])

        bf_signals_output = fs.get_futures_butterfly_signals(
            ticker_list=[
                strategy_info_dict['ticker1'], strategy_info_dict['ticker2'],
                strategy_info_dict['ticker3']
            ],
            aggregation_method=int(strategy_info_dict['agg']),
            contracts_back=int(strategy_info_dict['cBack']),
            date_to=date_to,
            **signal_input)

        if bf_signals_output['success']:
            aligned_output = bf_signals_output['aligned_output']
            current_data = aligned_output['current_data']
            holding_tr_dte = int(
                strategy_info_dict['trDte1']) - current_data['c1']['tr_dte']
            success_status = True
            QF = bf_signals_output['qf']
            z1 = bf_signals_output['zscore1']
            short_tr_dte = current_data['c1']['tr_dte']
            second_spread_weight = bf_signals_output['second_spread_weight_1']

            if strategy_position.empty:
                recommendation = 'CLOSE'
            elif (z1_initial>0)&(holding_tr_dte > 5) &\
                    (bf_signals_output['qf']<QF_initial-20)&\
                    (pnl_frame['total_pnl'].iloc[0] > 3*t_cost*total_contracts2trade):
                recommendation = 'STOP'
            elif (z1_initial<0)&(holding_tr_dte > 5) &\
                    (bf_signals_output['qf']>QF_initial+20)&\
                    (pnl_frame['total_pnl'].iloc[0] > 3*t_cost*total_contracts2trade):
                recommendation = 'STOP'
            elif (current_data['c1']['tr_dte'] < 35)&\
                (pnl_frame['total_pnl'].iloc[0] > 3*t_cost*total_contracts2trade):
                recommendation = 'STOP'
            elif (current_data['c1']['tr_dte'] < 35)&\
                (pnl_frame['total_pnl'].iloc[0] < 3*t_cost*total_contracts2trade):
                recommendation = 'WINDDOWN'
            else:
                recommendation = 'HOLD'
        else:
            success_status = False
            QF = np.nan
            z1 = np.nan
            short_tr_dte = np.nan
            holding_tr_dte = np.nan
            second_spread_weight = np.nan
            recommendation = 'MISSING DATA'

        result_output = {
            'success': success_status,
            'ticker_head': ticker_head,
            'QF_initial': QF_initial,
            'z1_initial': z1_initial,
            'QF': QF,
            'z1': z1,
            'short_tr_dte': short_tr_dte,
            'holding_tr_dte': holding_tr_dte,
            'second_spread_weight': second_spread_weight,
            'recommendation': recommendation
        }

    elif strategy_class == 'spread_carry':
        trades4_strategy = ts.get_trades_4strategy_alias(**kwargs)
        grouped = trades4_strategy.groupby('ticker')
        net_position = pd.DataFrame()
        net_position['ticker'] = (grouped['ticker'].first()).values
        net_position['qty'] = (grouped['trade_quantity'].sum()).values
        net_position = net_position[net_position['qty'] != 0]

        net_position['ticker_head'] = [
            cmi.get_contract_specs(x)['ticker_head']
            for x in net_position['ticker']
        ]
        price_output = [
            gfp.get_futures_price_preloaded(ticker=x, settle_date=date_to)
            for x in net_position['ticker']
        ]
        net_position['tr_dte'] = [
            np.nan if x.empty else x['tr_dte'].values[0] for x in price_output
        ]

        results_frame = pd.DataFrame()
        unique_tickerhead_list = net_position['ticker_head'].unique()
        results_frame['tickerHead'] = unique_tickerhead_list
        results_frame['ticker1'] = [None] * len(unique_tickerhead_list)
        results_frame['ticker2'] = [None] * len(unique_tickerhead_list)
        results_frame['qty'] = [None] * len(unique_tickerhead_list)
        results_frame['pnl'] = [None] * len(unique_tickerhead_list)
        results_frame['downside'] = [None] * len(unique_tickerhead_list)
        results_frame['indicator'] = [None] * len(unique_tickerhead_list)
        results_frame['timeHeld'] = [None] * len(unique_tickerhead_list)
        results_frame['recommendation'] = [None] * len(unique_tickerhead_list)

        spread_carry_output = osc.generate_spread_carry_sheet_4date(
            report_date=date_to)
        spread_report = spread_carry_output['spread_report']

        pnl_output = tpnl.get_strategy_pnl(**kwargs)
        pnl_per_tickerhead = pnl_output['pnl_per_tickerhead']

        for i in range(len(unique_tickerhead_list)):
            net_position_per_tickerhead = net_position[
                net_position['ticker_head'] == unique_tickerhead_list[i]]
            net_position_per_tickerhead.sort_values('tr_dte',
                                                    ascending=True,
                                                    inplace=True)

            selected_spread = spread_report[
                (spread_report['ticker1'] ==
                 net_position_per_tickerhead['ticker'].values[0])
                & (spread_report['ticker2'] ==
                   net_position_per_tickerhead['ticker'].values[1])]

            results_frame['qty'][i] = net_position_per_tickerhead[
                'qty'].values[0]

            if selected_spread.empty:
                results_frame['ticker1'][i] = net_position_per_tickerhead[
                    'ticker'].values[0]
                results_frame['ticker2'][i] = net_position_per_tickerhead[
                    'ticker'].values[1]
            else:
                results_frame['ticker1'][i] = selected_spread[
                    'ticker1'].values[0]
                results_frame['ticker2'][i] = selected_spread[
                    'ticker2'].values[0]

                selected_trades = trades4_strategy[
                    trades4_strategy['ticker'] ==
                    results_frame['ticker1'].values[i]]

                price_output = gfp.get_futures_price_preloaded(
                    ticker=results_frame['ticker1'].values[i],
                    settle_date=pd.to_datetime(
                        selected_trades['trade_date'].values[0]))

                results_frame['timeHeld'][i] = price_output['tr_dte'].values[
                    0] - net_position_per_tickerhead['tr_dte'].values[0]
                results_frame['pnl'][i] = pnl_per_tickerhead[
                    unique_tickerhead_list[i]].sum()

                if unique_tickerhead_list[i] in ['CL', 'B', 'ED']:
                    results_frame['indicator'][i] = selected_spread[
                        'reward_risk'].values[0]

                    if results_frame['qty'][i] > 0:
                        results_frame['recommendation'][i] = 'STOP'
                    elif results_frame['qty'][i] < 0:
                        if results_frame['indicator'][i] > -0.06:
                            results_frame['recommendation'][i] = 'STOP'
                        else:
                            results_frame['recommendation'][i] = 'HOLD'
                else:

                    results_frame['indicator'][i] = selected_spread[
                        'q_carry'].values[0]

                    if results_frame['qty'][i] > 0:
                        if results_frame['indicator'][i] < 19:
                            results_frame['recommendation'][i] = 'STOP'
                        else:
                            results_frame['recommendation'][i] = 'HOLD'

                    elif results_frame['qty'][i] < 0:
                        if results_frame['indicator'][i] > -9:
                            results_frame['recommendation'][i] = 'STOP'
                        else:
                            results_frame['recommendation'][i] = 'HOLD'

                if results_frame['qty'][i] > 0:
                    results_frame['downside'][i] = selected_spread[
                        'downside'].values[0] * results_frame['qty'][i]
                else:
                    results_frame['downside'][i] = selected_spread[
                        'upside'].values[0] * results_frame['qty'][i]

        return {'success': True, 'results_frame': results_frame}

    elif strategy_class == 'vcs':

        greeks_out = sg.get_greeks_4strategy_4date(alias=kwargs['alias'],
                                                   as_of_date=date_to)
        ticker_portfolio = greeks_out['ticker_portfolio']
        options_position = greeks_out['options_position']

        if ticker_portfolio.empty and not options_position.empty:
            result_output = {
                'success': False,
                'net_oev': np.NaN,
                'net_theta': np.NaN,
                'long_short_ratio': np.NaN,
                'recommendation': 'MISSING DATA',
                'last_adjustment_days_ago': np.NaN,
                'min_tr_dte': np.NaN,
                'long_oev': np.NaN,
                'short_oev': np.NaN,
                'favQMove': np.NaN
            }
        elif ticker_portfolio.empty and options_position.empty:
            result_output = {
                'success': False,
                'net_oev': np.NaN,
                'net_theta': np.NaN,
                'long_short_ratio': np.NaN,
                'recommendation': 'EMPTY',
                'last_adjustment_days_ago': np.NaN,
                'min_tr_dte': np.NaN,
                'long_oev': np.NaN,
                'short_oev': np.NaN,
                'favQMove': np.NaN
            }

        else:
            min_tr_dte = min([
                exp.get_days2_expiration(ticker=x,
                                         date_to=date_to,
                                         instrument='options',
                                         con=con)['tr_dte']
                for x in ticker_portfolio['ticker']
            ])

            net_oev = ticker_portfolio['total_oev'].sum()
            net_theta = ticker_portfolio['theta'].sum()

            long_portfolio = ticker_portfolio[
                ticker_portfolio['total_oev'] > 0]
            short_portfolio = ticker_portfolio[
                ticker_portfolio['total_oev'] < 0]
            short_portfolio['total_oev'] = abs(short_portfolio['total_oev'])

            long_oev = long_portfolio['total_oev'].sum()
            short_oev = short_portfolio['total_oev'].sum()

            if (not short_portfolio.empty) & (not long_portfolio.empty):
                long_short_ratio = 100 * long_oev / short_oev

                long_portfolio.sort_values('total_oev',
                                           ascending=False,
                                           inplace=True)
                short_portfolio.sort_values('total_oev',
                                            ascending=False,
                                            inplace=True)

                long_ticker = long_portfolio['ticker'].iloc[0]
                short_ticker = short_portfolio['ticker'].iloc[0]

                long_contract_specs = cmi.get_contract_specs(long_ticker)
                short_contract_specs = cmi.get_contract_specs(short_ticker)

                if 12*long_contract_specs['ticker_year']+long_contract_specs['ticker_month_num'] < \
                                        12*short_contract_specs['ticker_year']+short_contract_specs['ticker_month_num']:
                    front_ticker = long_ticker
                    back_ticker = short_ticker
                    direction = 'long'
                else:
                    front_ticker = short_ticker
                    back_ticker = long_ticker
                    direction = 'short'

                if 'vcs_output' in kwargs.keys():
                    vcs_output = kwargs['vcs_output']
                else:
                    vcs_output = ovcs.generate_vcs_sheet_4date(date_to=date_to)

                vcs_pairs = vcs_output['vcs_pairs']
                selected_result = vcs_pairs[
                    (vcs_pairs['ticker1'] == front_ticker)
                    & (vcs_pairs['ticker2'] == back_ticker)]

                if selected_result.empty:
                    favQMove = np.NaN
                else:
                    current_Q = selected_result['Q'].iloc[0]
                    q_limit = of.get_vcs_filter_values(
                        product_group=long_contract_specs['ticker_head'],
                        filter_type='tickerHead',
                        direction=direction,
                        indicator='Q')
                    if direction == 'long':
                        favQMove = current_Q - q_limit
                    elif direction == 'short':
                        favQMove = q_limit - current_Q
            else:
                long_short_ratio = np.NaN
                favQMove = np.NaN

            trades_frame = ts.get_trades_4strategy_alias(**kwargs)
            trades_frame_options = trades_frame[trades_frame['instrument'] ==
                                                'O']
            last_adjustment_days_ago = len(
                exp.get_bus_day_list(
                    date_to=date_to,
                    datetime_from=max(
                        trades_frame_options['trade_date']).to_pydatetime()))

            if favQMove >= 10 and last_adjustment_days_ago > 10:
                recommendation = 'STOP-ratio normalized'
            elif min_tr_dte < 25:
                recommendation = 'STOP-close to expiration'
            elif np.isnan(long_short_ratio):
                recommendation = 'STOP-not a proper calendar'
            else:
                if long_short_ratio < 80:
                    if favQMove < 0:
                        recommendation = 'buy_options_to_grow'
                    else:
                        recommendation = 'buy_options_to_shrink'
                elif long_short_ratio > 120:
                    if favQMove < 0:
                        recommendation = 'sell_options_to_grow'
                    else:
                        recommendation = 'sell_options_to_shrink'
                else:
                    recommendation = 'HOLD'

            result_output = {
                'success': True,
                'net_oev': net_oev,
                'net_theta': net_theta,
                'long_short_ratio': long_short_ratio,
                'recommendation': recommendation,
                'last_adjustment_days_ago': last_adjustment_days_ago,
                'min_tr_dte': min_tr_dte,
                'long_oev': long_oev,
                'short_oev': short_oev,
                'favQMove': favQMove
            }

    elif strategy_class == 'ocs':

        datetime_to = cu.convert_doubledate_2datetime(date_to)
        time_held = (datetime_to.date() -
                     strategy_info_output['created_date'].date()).days
        notes = ''

        strategy_position = ts.get_net_position_4strategy_alias(
            alias=kwargs['alias'], as_of_date=date_to, con=con)

        if len(strategy_position.index) == 0:
            tpnl.close_strategy(alias=kwargs['alias'],
                                close_date=date_to,
                                broker=broker,
                                con=con)
            result_output = {
                'success': True,
                'time_held': time_held,
                'dollar_noise': np.nan,
                'notes': 'closed'
            }
        elif strategy_position['qty'].sum() != 0:
            result_output = {
                'success': True,
                'time_held': time_held,
                'dollar_noise': np.nan,
                'notes': 'check position'
            }
        else:
            strategy_position['cont_indx'] = [
                cmi.get_contract_specs(x)['cont_indx']
                for x in strategy_position['ticker']
            ]
            strategy_position.sort_values('cont_indx',
                                          ascending=True,
                                          inplace=True)

            ocs_output = ocs.generate_overnight_spreads_sheet_4date(
                date_to=date_to)
            overnight_calendars = ocs_output['overnight_calendars']

            selection_indx = (overnight_calendars['ticker1'] == strategy_position['ticker'].iloc[0])&\
                             (overnight_calendars['ticker2'] == strategy_position['ticker'].iloc[1])

            if sum(selection_indx) > 0:
                dollar_noise = (overnight_calendars.loc[
                    selection_indx, 'dollarNoise100'].values[0]) * abs(
                        strategy_position['qty'].iloc[0])
            else:
                dollar_noise = np.nan

            result_output = {
                'success': True,
                'time_held': time_held,
                'dollar_noise': dollar_noise,
                'notes': 'hold'
            }

    elif strategy_class == 'skpt':

        long_ticker = strategy_position.loc[strategy_position['qty'] > 0,
                                            'ticker'].iloc[0]
        short_ticker = strategy_position.loc[strategy_position['qty'] < 0,
                                             'ticker'].iloc[0]

        long_data = gsp.get_stock_price_preloaded(ticker=long_ticker,
                                                  data_source='iex',
                                                  settle_date_to=date_to)
        short_data = gsp.get_stock_price_preloaded(ticker=short_ticker,
                                                   data_source='iex',
                                                   settle_date_to=date_to)
        merged_data = pd.merge(long_data[['close', 'settle_datetime']],
                               short_data[['close', 'settle_datetime']],
                               how='inner',
                               on='settle_datetime')
        merged_data.set_index('settle_datetime', drop=True, inplace=True)

        intaday_output_long = pweb.DataReader(long_ticker, 'iex-tops')
        intaday_output_short = pweb.DataReader(short_ticker, 'iex-tops')
        merged_data = merged_data.append(
            pd.DataFrame(
                {
                    'close_x': intaday_output_long.iloc[4].values[0],
                    'close_y': intaday_output_short.iloc[4].values[0]
                },
                index=[dt.datetime.now()]))

        signal_output = spt.backtest(merged_data, 'close_x', 'close_y')

        return {
            'long_ticker': long_ticker,
            'short_ticker': short_ticker,
            'zScoreC': signal_output['data_frame']['zScore'].iloc[-1],
            'zScore': signal_output['data_frame']['zScore'].iloc[-2]
        }

    else:
        result_output = {'success': False}

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

    return result_output
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}
    opl.update_options_price_database_from_cme_files(con=con, settle_date=folder_date)
except Exception:
    pass

try:
    ogl.update_options_greeks_4date(con=con, settle_date=folder_date)
except Exception:
    pass

try:
    osl.load_ticker_signals_4settle_date(con=con, settle_date=folder_date)
except Exception:
    pass

try:
    vcs.generate_vcs_sheet_4date(con=con,date_to=folder_date)
except Exception:
    pass

try:
    osf.generate_vcs_formatted_output(report_date=folder_date)
    prep.prepare_strategy_daily(strategy_class='vcs', report_date=folder_date)
except Exception:
    pass

try:
    osf.generate_scv_formatted_output(report_date=folder_date)
    prep.prepare_strategy_daily(strategy_class='scv', report_date=folder_date)
except Exception:
    pass
def get_results_4strategy(**kwargs):

    signal_input = dict()

    if 'futures_data_dictionary' in kwargs.keys():
        signal_input['futures_data_dictionary'] = kwargs['futures_data_dictionary']

    if 'date_to' in kwargs.keys():
        date_to = kwargs['date_to']
    else:
        date_to = exp.doubledate_shift_bus_days()

    if 'datetime5_years_ago' in kwargs.keys():
        signal_input['datetime5_years_ago'] = kwargs['datetime5_years_ago']

    if 'strategy_info_output' in kwargs.keys():
        strategy_info_output = kwargs['strategy_info_output']
    else:
        strategy_info_output = ts.get_strategy_info_from_alias(**kwargs)

    con = msu.get_my_sql_connection(**kwargs)

    strategy_info_dict = sc.convert_from_string_to_dictionary(string_input=strategy_info_output['description_string'])
    #print(kwargs['alias'])

    strategy_class = strategy_info_dict['strategy_class']

    pnl_frame = tpm.get_daily_pnl_snapshot(as_of_date=date_to)
    pnl_frame = pnl_frame[pnl_frame['alias']==kwargs['alias']]
    strategy_position = ts.get_net_position_4strategy_alias(alias=kwargs['alias'],as_of_date=date_to)

    if strategy_class == 'futures_butterfly':

        ticker_head = cmi.get_contract_specs(strategy_info_dict['ticker1'])['ticker_head']
        if not strategy_position.empty:
            total_contracts2trade = strategy_position['qty'].abs().sum()
            t_cost = cmi.t_cost[ticker_head]
        QF_initial = float(strategy_info_dict['QF'])
        z1_initial = float(strategy_info_dict['z1'])

        bf_signals_output = fs.get_futures_butterfly_signals(ticker_list=[strategy_info_dict['ticker1'],
                                                                          strategy_info_dict['ticker2'],
                                                                          strategy_info_dict['ticker3']],
                                          aggregation_method=int(strategy_info_dict['agg']),
                                          contracts_back=int(strategy_info_dict['cBack']),
                                          date_to=date_to,**signal_input)

        aligned_output = bf_signals_output['aligned_output']
        current_data = aligned_output['current_data']
        holding_tr_dte = int(strategy_info_dict['trDte1'])-current_data['c1']['tr_dte']

        if strategy_position.empty:
            recommendation = 'CLOSE'
        elif (z1_initial>0)&(holding_tr_dte > 5) &\
                (bf_signals_output['qf']<QF_initial-20)&\
                (pnl_frame['total_pnl'].iloc[0] > 3*t_cost*total_contracts2trade):
            recommendation = 'STOP'
        elif (z1_initial<0)&(holding_tr_dte > 5) &\
                (bf_signals_output['qf']>QF_initial+20)&\
                (pnl_frame['total_pnl'].iloc[0] > 3*t_cost*total_contracts2trade):
            recommendation = 'STOP'
        elif (current_data['c1']['tr_dte'] < 35)&\
            (pnl_frame['total_pnl'].iloc[0] > 3*t_cost*total_contracts2trade):
            recommendation = 'STOP'
        elif (current_data['c1']['tr_dte'] < 35)&\
            (pnl_frame['total_pnl'].iloc[0] < 3*t_cost*total_contracts2trade):
            recommendation = 'WINDDOWN'
        else:
            recommendation = 'HOLD'

        result_output = {'success': True,'ticker_head': ticker_head,
                        'QF_initial':QF_initial,'z1_initial': z1_initial,
                        'QF': bf_signals_output['qf'],'z1': bf_signals_output['zscore1'],
                        'short_tr_dte': current_data['c1']['tr_dte'],
                        'holding_tr_dte': holding_tr_dte,
                        'second_spread_weight': bf_signals_output['second_spread_weight_1'],'recommendation': recommendation}

    elif strategy_class == 'spread_carry':
        trades4_strategy = ts.get_trades_4strategy_alias(**kwargs)
        grouped = trades4_strategy.groupby('ticker')
        net_position = pd.DataFrame()
        net_position['ticker'] = (grouped['ticker'].first()).values
        net_position['qty'] = (grouped['trade_quantity'].sum()).values
        net_position = net_position[net_position['qty'] != 0]

        net_position['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in net_position['ticker']]
        price_output = [gfp.get_futures_price_preloaded(ticker=x, settle_date=date_to) for x in net_position['ticker']]
        net_position['tr_dte'] = [x['tr_dte'].values[0] for x in price_output]

        results_frame = pd.DataFrame()
        unique_tickerhead_list = net_position['ticker_head'].unique()
        results_frame['tickerHead'] = unique_tickerhead_list
        results_frame['ticker1'] = [None]*len(unique_tickerhead_list)
        results_frame['ticker2'] = [None]*len(unique_tickerhead_list)
        results_frame['qty'] = [None]*len(unique_tickerhead_list)
        results_frame['pnl'] = [None]*len(unique_tickerhead_list)
        results_frame['downside'] = [None]*len(unique_tickerhead_list)
        results_frame['indicator'] = [None]*len(unique_tickerhead_list)
        results_frame['timeHeld'] = [None]*len(unique_tickerhead_list)
        results_frame['recommendation'] = [None]*len(unique_tickerhead_list)

        spread_carry_output = osc.generate_spread_carry_sheet_4date(report_date=date_to)
        spread_report = spread_carry_output['spread_report']

        pnl_output = tpnl.get_strategy_pnl(**kwargs)
        pnl_per_tickerhead = pnl_output['pnl_per_tickerhead']

        for i in range(len(unique_tickerhead_list)):

            net_position_per_tickerhead = net_position[net_position['ticker_head'] == unique_tickerhead_list[i]]
            net_position_per_tickerhead.sort('tr_dte',ascending=True,inplace=True)

            selected_spread = spread_report[(spread_report['ticker1'] == net_position_per_tickerhead['ticker'].values[0]) &
                             (spread_report['ticker2'] == net_position_per_tickerhead['ticker'].values[1])]

            results_frame['ticker1'][i] = selected_spread['ticker1'].values[0]
            results_frame['ticker2'][i] = selected_spread['ticker2'].values[0]
            results_frame['qty'][i] = net_position_per_tickerhead['qty'].values[0]

            selected_trades = trades4_strategy[trades4_strategy['ticker'] == results_frame['ticker1'].values[i]]

            price_output = gfp.get_futures_price_preloaded(ticker=results_frame['ticker1'].values[i],
                                                           settle_date=pd.to_datetime(selected_trades['trade_date'].values[0]))

            results_frame['timeHeld'][i] = price_output['tr_dte'].values[0]-net_position_per_tickerhead['tr_dte'].values[0]
            results_frame['pnl'][i] = pnl_per_tickerhead[unique_tickerhead_list[i]].sum()

            if unique_tickerhead_list[i] in ['CL', 'B', 'ED']:
                results_frame['indicator'][i] = selected_spread['reward_risk'].values[0]

                if results_frame['qty'][i] > 0:
                    results_frame['recommendation'][i] = 'STOP'
                elif results_frame['qty'][i] < 0:
                    if results_frame['indicator'][i] > -0.06:
                        results_frame['recommendation'][i] = 'STOP'
                    else:
                        results_frame['recommendation'][i] = 'HOLD'
            else:

                results_frame['indicator'][i] = selected_spread['q_carry'].values[0]

                if results_frame['qty'][i] > 0:
                    if results_frame['indicator'][i] < 19:
                        results_frame['recommendation'][i] = 'STOP'
                    else:
                        results_frame['recommendation'][i] = 'HOLD'

                elif results_frame['qty'][i] < 0:
                    if results_frame['indicator'][i] > -9:
                        results_frame['recommendation'][i] = 'STOP'
                    else:
                        results_frame['recommendation'][i] = 'HOLD'

            if results_frame['qty'][i] > 0:
                results_frame['downside'][i] = selected_spread['downside'].values[0]*results_frame['qty'][i]
            else:
                results_frame['downside'][i] = selected_spread['upside'].values[0]*results_frame['qty'][i]

        return {'success': True, 'results_frame': results_frame}

    elif strategy_class == 'vcs':

        greeks_out = sg.get_greeks_4strategy_4date(alias=kwargs['alias'], as_of_date=date_to)
        ticker_portfolio = greeks_out['ticker_portfolio']

        if ticker_portfolio.empty:
            min_tr_dte = np.NaN
            result_output = {'success': False, 'net_oev': np.NaN, 'net_theta': np.NaN, 'long_short_ratio': np.NaN,
                         'recommendation': 'EMPTY', 'last_adjustment_days_ago': np.NaN,
                         'min_tr_dte': np.NaN, 'long_oev': np.NaN, 'short_oev': np.NaN, 'favQMove': np.NaN}
        else:
            min_tr_dte = min([exp.get_days2_expiration(ticker=x,date_to=date_to,instrument='options',con=con)['tr_dte'] for x in ticker_portfolio['ticker']])

            net_oev = ticker_portfolio['total_oev'].sum()
            net_theta = ticker_portfolio['theta'].sum()

            long_portfolio = ticker_portfolio[ticker_portfolio['total_oev'] > 0]
            short_portfolio = ticker_portfolio[ticker_portfolio['total_oev'] < 0]
            short_portfolio['total_oev']=abs(short_portfolio['total_oev'])

            long_oev = long_portfolio['total_oev'].sum()
            short_oev = short_portfolio['total_oev'].sum()

            if (not short_portfolio.empty) & (not long_portfolio.empty):
                long_short_ratio = 100*long_oev/short_oev

                long_portfolio.sort('total_oev', ascending=False, inplace=True)
                short_portfolio.sort('total_oev', ascending=False, inplace=True)

                long_ticker = long_portfolio['ticker'].iloc[0]
                short_ticker = short_portfolio['ticker'].iloc[0]

                long_contract_specs = cmi.get_contract_specs(long_ticker)
                short_contract_specs = cmi.get_contract_specs(short_ticker)

                if 12*long_contract_specs['ticker_year']+long_contract_specs['ticker_month_num'] < \
                                        12*short_contract_specs['ticker_year']+short_contract_specs['ticker_month_num']:
                    front_ticker = long_ticker
                    back_ticker = short_ticker
                    direction = 'long'
                else:
                    front_ticker = short_ticker
                    back_ticker = long_ticker
                    direction = 'short'

                if 'vcs_output' in kwargs.keys():
                    vcs_output = kwargs['vcs_output']
                else:
                    vcs_output = ovcs.generate_vcs_sheet_4date(date_to=date_to)

                vcs_pairs = vcs_output['vcs_pairs']
                selected_result = vcs_pairs[(vcs_pairs['ticker1'] == front_ticker) & (vcs_pairs['ticker2'] == back_ticker)]

                if selected_result.empty:
                    favQMove = np.NaN
                else:
                    current_Q = selected_result['Q'].iloc[0]
                    q_limit = of.get_vcs_filter_values(product_group=long_contract_specs['ticker_head'],
                                                   filter_type='tickerHead',direction=direction,indicator='Q')
                    if direction == 'long':
                        favQMove = current_Q-q_limit
                    elif direction == 'short':
                        favQMove = q_limit-current_Q
            else:
                long_short_ratio = np.NaN
                favQMove = np.NaN

            trades_frame = ts.get_trades_4strategy_alias(**kwargs)
            trades_frame_options = trades_frame[trades_frame['instrument'] == 'O']
            last_adjustment_days_ago = len(exp.get_bus_day_list(date_to=date_to,datetime_from=max(trades_frame_options['trade_date']).to_datetime()))

            if favQMove >= 10 and last_adjustment_days_ago > 10:
                recommendation = 'STOP-ratio normalized'
            elif min_tr_dte<25:
                recommendation = 'STOP-close to expiration'
            elif np.isnan(long_short_ratio):
                recommendation = 'STOP-not a proper calendar'
            else:
                if long_short_ratio < 80:
                    if favQMove < 0:
                        recommendation = 'buy_options_to_grow'
                    else:
                        recommendation = 'buy_options_to_shrink'
                elif long_short_ratio > 120:
                    if favQMove < 0:
                        recommendation = 'sell_options_to_grow'
                    else:
                        recommendation = 'sell_options_to_shrink'
                else:
                    recommendation = 'HOLD'

            result_output = {'success': True, 'net_oev': net_oev, 'net_theta': net_theta, 'long_short_ratio': long_short_ratio,
                         'recommendation': recommendation, 'last_adjustment_days_ago': last_adjustment_days_ago,
                         'min_tr_dte': min_tr_dte, 'long_oev': long_oev, 'short_oev': short_oev, 'favQMove': favQMove}

    else:
        result_output = {'success': False}

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

    return result_output