Esempio n. 1
0
def main():
    app = algo.Algo()
    con = msu.get_my_sql_connection()
    date_now = cu.get_doubledate()
    datetime_now = dt.datetime.now()
    report_date = exp.doubledate_shift_bus_days()

    ticker_head_list = ['HO', 'LC', 'FC', 'NQ']  # avoid HO on wednesdays for now!!

    data_list = [gfp.get_futures_price_preloaded(ticker_head=x, settle_date=report_date) for x in ticker_head_list]
    ticker_frame = pd.concat(data_list)
    ticker_frame.sort(['ticker_head','volume'], ascending=[True, False], inplace=True)
    ticker_frame.drop_duplicates(subset=['ticker_head'], take_last=False, inplace=True)

    ticker_list = list(ticker_frame['ticker'])

    theme_name_list = set([x + '_long' for x in ticker_list]).union(set([x + '_short' for x in ticker_list]))

    alias_portfolio = aup.portfolio(ticker_list=theme_name_list)

    latest_trade_entry_datetime = dt.datetime(datetime_now.year, datetime_now.month, datetime_now.day, 12, 0, 0)
    latest_livestock_exit_datetime = dt.datetime(datetime_now.year, datetime_now.month, datetime_now.day, 13, 55, 0)
    latest_macro_exit_datetime = dt.datetime(datetime_now.year, datetime_now.month, datetime_now.day, 15, 55, 0)

    daily_sd_dictionary = {}

    for i in range(len(ticker_list)):
        daily_data_frame = gfp.get_futures_price_preloaded(ticker=ticker_list[i], settle_date_to=report_date)
        daily_data_frame['close_diff'] = daily_data_frame['close_price'].diff()
        daily_sd_dictionary[ticker_list[i]] = np.std(daily_data_frame['close_diff'].iloc[-40:])

    db_alias = 'itf_' + datetime_now.strftime('%b_%y')
    strategy_frame = ts.get_open_strategies(as_of_date=int(datetime_now.strftime('%Y%m%d')), con=con)

    if db_alias not in strategy_frame.values:
        db_strategy_output = ts.generate_db_strategy_from_alias(alias=db_alias, description_string='strategy_class=itf',con=con)
        db_alias = db_strategy_output['alias']



    app.ticker_list = ticker_list
    app.db_alias = db_alias
    app.tick_size_dictionary = {x:cmi.tick_size[cmi.get_contract_specs(x)['ticker_head']] for x in ticker_list}
    app.daily_sd_dictionary = daily_sd_dictionary
    app.contract_multiplier_dictionary = {x:cmi.contract_multiplier[cmi.get_contract_specs(x)['ticker_head']] for x in ticker_list}
    app.ticker_head_dictionary = {x:cmi.get_contract_specs(x)['ticker_head'] for x in ticker_list}
    app.latest_trade_entry_datetime = latest_trade_entry_datetime
    app.latest_trade_exit_datetime_dictionary = {'HO': latest_macro_exit_datetime, 'LC': latest_livestock_exit_datetime, 'FC': latest_livestock_exit_datetime, 'NQ': latest_macro_exit_datetime}
    app.alias_portfolio = alias_portfolio
    app.output_dir = ts.create_strategy_output_dir(strategy_class='itf', report_date=report_date)
    app.log = lg.get_logger(file_identifier='ib_itf',log_level='INFO')
    app.con = con

    app.connect(client_id=4)
    app.run()
Esempio n. 2
0
def get_tagged_tt_fills(**kwargs):

    fill_frame = load_latest_tt_fills(**kwargs)

    str_indx = fill_frame['Contract'].values[0].find('-')

    if str_indx == 2:
        date_format = '%y-%b'
    elif str_indx == -1:
        date_format = '%b%y'

    datetime_conversion = [dt.datetime.strptime(x,date_format) for x in fill_frame['Contract']]
    fill_frame['ticker_year'] = [x.year for x in datetime_conversion]
    fill_frame['ticker_month'] = [x.month for x in datetime_conversion]
    fill_frame['ticker_head'] = [conversion_from_tt_ticker_head[x] for x in fill_frame['Product']]

    fill_frame['ticker'] = [fill_frame.loc[x,'ticker_head'] +
                            cmi.full_letter_month_list[fill_frame.loc[x,'ticker_month']-1] +
                            str(fill_frame.loc[x,'ticker_year']) for x in fill_frame.index]

    fill_frame['trade_price'] = [convert_trade_price_from_tt(price=fill_frame.loc[x,'Price'],ticker_head=fill_frame.loc[x,'ticker_head'])
                                 for x in fill_frame.index]

    fill_frame['PQ'] = fill_frame['trade_price']*fill_frame['Qty']

    grouped = fill_frame.groupby(['ticker','B/S', 'Order Tag'])

    aggregate_trades = pd.DataFrame()
    aggregate_trades['trade_price'] = grouped['PQ'].sum()/grouped['Qty'].sum()
    aggregate_trades['trade_quantity'] = grouped['Qty'].sum()

    aggregate_trades.loc[(slice(None),'S'),'trade_quantity']=-aggregate_trades.loc[(slice(None),'S'),'trade_quantity']
    aggregate_trades['ticker'] = grouped['ticker'].first()
    aggregate_trades['ticker_head'] = grouped['ticker_head'].first()
    aggregate_trades['order_tag'] = grouped['Order Tag'].first()
    aggregate_trades['instrument'] = [product_type_instrument_conversion[x] for x in grouped['Product Type'].first()]

    aggregate_trades['option_type'] = None
    aggregate_trades['strike_price'] = None
    aggregate_trades['real_tradeQ'] = True

    ta_directory = dn.get_dated_directory_extension(ext='ta', folder_date=cu.get_doubledate())
    trade_alias_frame = pd.read_csv(ta_directory + '/tradeAlias.csv')

    combined_list = [None]*len(trade_alias_frame.index)

    for i in range(len(trade_alias_frame.index)):

        selected_trades = aggregate_trades[aggregate_trades['order_tag'] == trade_alias_frame['tag'].iloc[i]]
        combined_list[i] = selected_trades[['ticker','option_type','strike_price','trade_price','trade_quantity','instrument','real_tradeQ']]
        combined_list[i]['alias'] = trade_alias_frame['alias'].iloc[i]

    aggregate_trades = pd.concat(combined_list).reset_index(drop=True)

    return {'raw_trades': fill_frame, 'aggregate_trades': aggregate_trades}
def reconcile_position(**kwargs):

    abn_position = load_and_convert_abn_position_file(**kwargs)

    db_position = tpm.get_position_4portfolio(trade_date_to=cu.get_doubledate())

    db_position['generalized_ticker'] = [x.rstrip('0').rstrip('.') for x in db_position['generalized_ticker']]

    merged_data = pd.merge(abn_position,db_position,how='outer',on='generalized_ticker')
    merged_data['qty_diff'] = merged_data['qty_x'].astype('float64')-merged_data['qty_y'].astype('float64')
    return merged_data[merged_data['qty_diff']!=0]
def test_ib_order_follow_up():
    app = ib_order_follow_up()
    date_now = cu.get_doubledate()
    con = msu.get_my_sql_connection()
    delta_strategy_frame = ts.get_filtered_open_strategies(as_of_date=date_now, con=con, strategy_class_list=['delta'])
    app.delta_alias = delta_strategy_frame['alias'].iloc[-1]
    ta_folder = dn.get_dated_directory_extension(folder_date=date_now, ext='ta')
    app.trade_file = ta_folder + '/trade_dir.csv'
    app.con = con

    app.connect(client_id=7)
    app.run()
Esempio n. 5
0
def test_ib_reconciler():
    app = ib_reconciler()
    con = msu.get_my_sql_connection()
    app.con = con
    position_frame = tpm.get_position_4portfolio(
        trade_date_to=cu.get_doubledate())
    position_frame['con_id'] = np.nan
    position_frame['ib_position'] = np.nan
    position_frame['ib_symbol'] = ''
    position_frame.reset_index(drop=True, inplace=True)
    app.position_frame = position_frame
    app.connect(client_id=1)
    app.run()
Esempio n. 6
0
def get_db_ticker_from_ib_contract(**kwargs):

    ib_contract = kwargs['ib_contract']

    if 'contract_id_dictionary' in kwargs.keys():
        contract_id_dictionary = kwargs['contract_id_dictionary']
        return su.get_key_in_dictionary(dictionary_input=contract_id_dictionary, value=ib_contract.conId)

    contract_output = {}
    contract_output['option_type'] = None
    contract_output['strike'] = None

    sec_type = ib_contract.secType


    if sec_type=='STK':
        contract_output['ticker'] = ib_contract.symbol
        contract_output['instrument'] = 'S'
        return contract_output

    ticker_head = conversion_from_ib_ticker_head[ib_contract.symbol]
    local_symbol_out = ib_contract.localSymbol.split(' ')

    date_now = cu.get_doubledate()

    if len(local_symbol_out) in [1,2]:
        contract_month_str = local_symbol_out[0][-2]

        if local_symbol_out[0][-1]=='0' and 0:
            contract_year_str = str(m.ceil(date_now / 10000))
        else:
            contract_year_str = str(m.floor(date_now / 100000)) + local_symbol_out[0][-1]
    else:
        contract_month_str = cmi.full_letter_month_list[cu.three_letter_month_dictionary[local_symbol_out[3]]-1]
        contract_year_str = str(m.floor(date_now / 1000000)) + local_symbol_out[4]

    #contract_year_str = '2020'

    contract_output['ticker'] = ticker_head + contract_month_str + contract_year_str


    if sec_type=='FOP':
        contract_output['instrument'] = 'O'
        contract_output['option_type'] = ib_contract.right
        contract_output['strike'] = round(ib_contract.strike/ib_strike_multiplier_dictionary.get(ticker_head, 1),4)
    else:
        contract_output['instrument'] = 'F'


    return contract_output
Esempio n. 7
0
def save_ticker_frame(**kwargs):

    if 'client' in kwargs.keys():
        client = kwargs['client']
    else:
        client = btu.get_binance_client()

    folder_date = cu.get_doubledate()

    folder_name = dn.get_dated_directory_extension(folder_date=folder_date,
                                                   ext='binance')
    tickers = client.get_ticker()
    ticker_frame = pd.DataFrame(tickers)

    ticker_frame.to_pickle(folder_name + '/ticker_frame.pkl')
Esempio n. 8
0
def reconcile_position(**kwargs):

    abn_position = load_and_convert_man_position_file(**kwargs)

    db_position = tpm.get_position_4portfolio(
        trade_date_to=cu.get_doubledate())

    db_position['generalized_ticker'] = [
        x.rstrip('0').rstrip('.') for x in db_position['generalized_ticker']
    ]

    merged_data = pd.merge(abn_position,
                           db_position,
                           how='outer',
                           on='generalized_ticker')
    merged_data['qty_diff'] = merged_data['qty_x'].astype(
        'float64') - merged_data['qty_y'].astype('float64')
    return merged_data[merged_data['qty_diff'] != 0]
def strategy_hedge_report(**kwargs):

    current_date = cu.get_doubledate()

    con = msu.get_my_sql_connection(**kwargs)

    strategy_frame = tas.get_open_strategies(as_of_date=current_date,con=con)

    strategy_class_list = [sc.convert_from_string_to_dictionary(string_input=strategy_frame['description_string'][x])['strategy_class']
                           for x in range(len(strategy_frame.index))]

    hedge_indx = [x in ['vcs', 'scv','optionInventory'] for x in strategy_class_list]
    hedge_frame = strategy_frame[hedge_indx]

    #hedge_frame = hedge_frame[hedge_frame['alias'] != 'LNZ16V16VCS']
    [hedge_strategy_against_delta(alias=x, con=con) for x in hedge_frame['alias']]

    if 'con' not in kwargs.keys():
        con.close()
Esempio n. 10
0
def get_symbol_frame(**kwargs):

    frame_type = kwargs['frame_type']
    settle_date = kwargs['settle_date']

    if frame_type == 'nasdaq':
        symbol_address = nasdaq_symbol_address
    elif frame_type == 'other':
        symbol_address = other_symbol_address

    output_dir = dn.get_directory_name(ext='stock_data')
    file_name = output_dir + '/' + frame_type + '_' + str(settle_date) + '.pkl'

    if os.path.isfile(file_name):
        return pd.read_pickle(file_name)
    else:

        datetime_now = dt.datetime.now()

        if datetime_now.weekday() in [5, 6]:
            last_settle_date = exp.doubledate_shift_bus_days()
        elif 100 * datetime_now.hour + datetime_now.minute > 930:
            last_settle_date = cu.get_doubledate()
        else:
            last_settle_date = exp.doubledate_shift_bus_days()

        if settle_date == last_settle_date:

            data_list = sd.download_txt_from_web(web_address=symbol_address)
            column_names = data_list[0].decode('iso-8859-1').split("|")
            parset_data_list = [
                data_list[x].decode('iso-8859-1').split("|")
                for x in range(1,
                               len(data_list) - 1)
            ]
            symbol_frame = pd.DataFrame(parset_data_list, columns=column_names)
            symbol_frame.to_pickle(file_name)
        else:
            symbol_frame = pd.DataFrame()

    return symbol_frame
Esempio n. 11
0
def save_ib_data(**kwargs):

    if 'duration_str' in kwargs.keys():
        duration_str = kwargs['duration_str']
    else:
        duration_str = '2 M'

    app = algo.Algo()
    con = msu.get_my_sql_connection()
    date_now = cu.get_doubledate()
    datetime_now = dt.datetime.now()
    report_date = exp.doubledate_shift_bus_days()

    ticker_head_list = cmi.cme_futures_tickerhead_list

    data_list = [
        gfp.get_futures_price_preloaded(ticker_head=x, settle_date=report_date)
        for x in ticker_head_list
    ]
    ticker_frame = pd.concat(data_list)
    ticker_frame = ticker_frame[~((ticker_frame['ticker_head'] == 'ED') &
                                  (ticker_frame['tr_dte'] < 250))]
    ticker_frame = ticker_frame[~((ticker_frame['ticker_head'] == 'GC') |
                                  (ticker_frame['ticker_head'] == 'SI'))]

    ticker_frame.sort_values(['ticker_head', 'volume'],
                             ascending=[True, False],
                             inplace=True)
    ticker_frame.drop_duplicates(subset=['ticker_head'],
                                 keep='first',
                                 inplace=True)

    app.ticker_list = list(ticker_frame['ticker'])
    app.output_dir = sd.get_directory_name(ext='ib_data')
    app.durationStr = duration_str
    app.con = con

    app.connect(client_id=5)

    app.run()
Esempio n. 12
0
def main():

    app = dh_algo.Algo()
    con = msu.get_my_sql_connection()
    date_now = cu.get_doubledate()

    contract_frame = tsh.get_intraday_data_contract_frame(con=con)

    contract_frame = dh_ut.calculate_contract_risk(
        contract_frame=contract_frame, current_date=date_now)

    contract_frame['bid_p'] = np.nan
    contract_frame['ask_p'] = np.nan
    contract_frame['mid_price'] = np.nan
    contract_frame['spread_cost'] = np.nan

    contract_frame['bid_q'] = np.nan
    contract_frame['ask_q'] = np.nan

    contract_frame_outright = contract_frame[contract_frame['is_spread_q'] ==
                                             False]
    outright_ticker_list = list(contract_frame_outright['ticker'].values)
    contract_frame_spread = contract_frame[contract_frame['is_spread_q']]

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

    app.ticker_list = outright_ticker_list
    app.price_request_dictionary['spread'] = list(
        contract_frame_spread['ticker'].values)
    app.price_request_dictionary['outright'] = outright_ticker_list
    app.contract_frame = contract_frame

    app.log = lg.get_logger(file_identifier='ib_delta_hedge', log_level='INFO')
    app.trade_file = ta_folder + '/trade_dir.csv'
    app.delta_alias = tsh.get_delta_strategy_alias(con=con)
    app.con = con
    app.current_date = date_now
    app.connect(client_id=6)
    app.run()
Esempio n. 13
0
def strategy_hedge_report(**kwargs):

    current_date = cu.get_doubledate()

    con = msu.get_my_sql_connection(**kwargs)

    strategy_frame = tas.get_open_strategies(as_of_date=current_date, con=con)

    strategy_class_list = [
        sc.convert_from_string_to_dictionary(
            string_input=strategy_frame['description_string'][x])
        ['strategy_class'] for x in range(len(strategy_frame.index))
    ]

    hedge_indx = [
        x in ['vcs', 'scv', 'optionInventory'] for x in strategy_class_list
    ]
    hedge_frame = strategy_frame[hedge_indx]

    #hedge_frame = hedge_frame[(hedge_frame['alias'] == 'BPX2018_short_scv')]
    #hedge_frame = hedge_frame[(hedge_frame['alias'] != 'CLZ17H18VCS')]

    if 'intraday_price_frame' in kwargs.keys():
        [
            hedge_strategy_against_delta(
                alias=x,
                intraday_price_frame=kwargs['intraday_price_frame'],
                delta_alias=kwargs['delta_alias'],
                con=con) for x in hedge_frame['alias']
        ]
    else:
        [
            hedge_strategy_against_delta(alias=x,
                                         delta_alias=kwargs['delta_alias'],
                                         con=con) for x in hedge_frame['alias']
        ]

    if 'con' not in kwargs.keys():
        con.close()
Esempio n. 14
0
def get_hedge_4strategy(**kwargs):

    con = msu.get_my_sql_connection(**kwargs)
    current_date = cu.get_doubledate()
    settle_price_date = exp.doubledate_shift_bus_days(double_date=current_date,
                                                      shift_in_days=1)

    position_frame = tas.get_net_position_4strategy_alias(
        alias=kwargs['alias'], as_of_date=current_date, con=con)

    if 'intraday_price_frame' in kwargs.keys():
        intraday_price_frame = kwargs['intraday_price_frame']
    else:
        intraday_price_frame = gip.get_cme_direct_prices()
        intraday_price_frame.rename(columns={'ticker': 'underlying_ticker'},
                                    inplace=True)

        intraday_price_frame['ticker_head'] = [
            cmi.get_contract_specs(x)['ticker_head']
            for x in intraday_price_frame['underlying_ticker']
        ]
        intraday_price_frame['mid_price'] = (
            intraday_price_frame['bid_price'] +
            intraday_price_frame['ask_price']) / 2

        intraday_price_frame['mid_price'] = [
            tfl.convert_trade_price_from_cme_direct(
                ticker_head=intraday_price_frame['ticker_head'].iloc[x],
                price=intraday_price_frame['mid_price'].iloc[x])
            for x in range(len(intraday_price_frame.index))
        ]

    options_frame = position_frame[position_frame['instrument'] == 'O']
    futures_frame = position_frame[position_frame['instrument'] == 'F']

    if options_frame.empty:
        futures_frame.rename(columns={
            'ticker': 'underlying_ticker',
            'qty': 'underlying_delta'
        },
                             inplace=True)
        futures_frame = futures_frame[[
            'underlying_ticker', 'underlying_delta'
        ]]
        net_position = pd.merge(futures_frame,
                                intraday_price_frame,
                                how='left',
                                on='underlying_ticker')
        net_position['hedge_price'] = net_position['mid_price']
        net_position['hedge'] = -net_position['underlying_delta']
        return net_position

    imp_vol_list = [
        gop.get_options_price_from_db(
            ticker=options_frame['ticker'].iloc[x],
            settle_date=settle_price_date,
            strike=options_frame['strike_price'].iloc[x],
            column_names=['imp_vol'],
            con=con)['imp_vol'] for x in range(len(options_frame.index))
    ]

    options_frame['imp_vol'] = [
        imp_vol_list[x][1] if
        (np.isnan(imp_vol_list[x][0])
         and len(imp_vol_list[x]) > 1) else imp_vol_list[x][0]
        for x in range(len(options_frame.index))
    ]

    options_frame['underlying_ticker'] = [
        omu.get_option_underlying(ticker=x) for x in options_frame['ticker']
    ]
    #print(intraday_price_frame)

    options_frame = pd.merge(options_frame,
                             intraday_price_frame,
                             how='left',
                             on='underlying_ticker')

    options_frame['ticker_head'] = [
        cmi.get_contract_specs(x)['ticker_head']
        for x in options_frame['ticker']
    ]
    options_frame['exercise_type'] = [
        cmi.get_option_exercise_type(ticker_head=x)
        for x in options_frame['ticker_head']
    ]
    options_frame['strike_price'] = options_frame['strike_price'].astype(
        'float64')

    options_frame['delta'] = [
        omu.option_model_wrapper(
            ticker=options_frame['ticker'].iloc[x],
            calculation_date=current_date,
            interest_rate_date=settle_price_date,
            underlying=options_frame['mid_price'].iloc[x],
            strike=options_frame['strike_price'].iloc[x],
            implied_vol=options_frame['imp_vol'].iloc[x],
            option_type=options_frame['option_type'].iloc[x],
            exercise_type=options_frame['exercise_type'].iloc[x],
            con=con)['delta'] for x in range(len(options_frame.index))
    ]

    options_frame[
        'total_delta'] = options_frame['qty'] * options_frame['delta']

    grouped = options_frame.groupby('underlying_ticker')

    net_position = pd.DataFrame()

    net_position['underlying_ticker'] = (
        grouped['underlying_ticker'].first()).values
    net_position['hedge_price'] = (grouped['mid_price'].first()).values
    net_position['option_delta'] = (grouped['total_delta'].sum()).values
    net_position['option_delta'] = net_position['option_delta'].round(2)

    if futures_frame.empty:
        net_position['total_delta'] = net_position['option_delta']
    else:
        futures_frame.rename(columns={
            'ticker': 'underlying_ticker',
            'qty': 'underlying_delta'
        },
                             inplace=True)
        futures_frame = futures_frame[[
            'underlying_ticker', 'underlying_delta'
        ]]

        isinOptions = futures_frame['underlying_ticker'].isin(
            net_position['underlying_ticker'])
        futures_frame_w_options = futures_frame[isinOptions]
        futures_frame_wo_options = futures_frame[~isinOptions]

        if futures_frame_w_options.empty:
            net_position['underlying_delta'] = 0
            net_position['total_delta'] = net_position['option_delta']
        else:
            net_position = pd.merge(net_position,
                                    futures_frame_w_options,
                                    how='outer',
                                    on='underlying_ticker')
            net_position[['underlying_delta'
                          ]] = net_position[['underlying_delta'
                                             ]].fillna(value=0, inplace=False)
            net_position['total_delta'] = net_position[
                'option_delta'] + net_position['underlying_delta']

        if not futures_frame_wo_options.empty:
            net_position_futures = pd.merge(futures_frame_wo_options,
                                            intraday_price_frame,
                                            how='left',
                                            on='underlying_ticker')
            net_position_futures['hedge_price'] = net_position_futures[
                'mid_price']
            net_position_futures['option_delta'] = 0
            net_position_futures['total_delta'] = net_position_futures[
                'underlying_delta']
            net_position = pd.concat([
                net_position, net_position_futures[[
                    'underlying_ticker', 'hedge_price', 'option_delta',
                    'underlying_delta', 'total_delta'
                ]]
            ])

    net_position['hedge'] = -net_position['total_delta']

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

    return net_position
Esempio n. 15
0
        },
                        inplace=True)
        data_out['settle_datetime'] = [
            dt.datetime.strptime(x, '%Y-%m-%d')
            for x in data_out['settle_date']
        ]
    elif data_source == 'iex':
        data_out = web.DataReader(symbol,
                                  data_source='iex',
                                  start='01/01/2013')
        data_out['settle_datetime'] = [
            dt.datetime.strptime(x, '%Y-%m-%d') for x in data_out.index
        ]
        data_out.set_index('settle_datetime', drop=False, inplace=True)

    return data_out


if __name__ == "__main__":

    datetime_now = dt.datetime.now()

    if datetime_now.weekday() in [5, 6]:
        last_settle_date = exp.doubledate_shift_bus_days()
    elif 100 * datetime_now.hour + datetime_now.minute > 930:
        last_settle_date = cu.get_doubledate()
    else:
        last_settle_date = exp.doubledate_shift_bus_days()

    save_stock_data(settle_date=last_settle_date)
Esempio n. 16
0
def get_intraday_data_contract_frame(**kwargs):

    current_date = cu.get_doubledate()
    con = msu.get_my_sql_connection(**kwargs)

    strategy_frame = tas.get_open_strategies(as_of_date=current_date, con=con)

    strategy_class_list = [
        sc.convert_from_string_to_dictionary(
            string_input=strategy_frame['description_string'][x])
        ['strategy_class'] for x in range(len(strategy_frame.index))
    ]

    hedge_indx = [
        x in ['vcs', 'scv', 'optionInventory'] for x in strategy_class_list
    ]
    hedge_frame = strategy_frame[hedge_indx]

    options_frame_list = []

    for i in range(len(hedge_frame.index)):

        position_frame = tas.get_net_position_4strategy_alias(
            alias=hedge_frame['alias'].iloc[i],
            as_of_date=current_date,
            con=con)
        options_frame = position_frame[position_frame['instrument'] == 'O']
        options_frame['underlying_ticker'] = [
            omu.get_option_underlying(ticker=x)
            for x in options_frame['ticker']
        ]
        options_frame_list.append(options_frame)

    merged_frame = pd.concat(options_frame_list)
    underlying_ticker_list = list(merged_frame['underlying_ticker'].unique())

    delta_alias = get_delta_strategy_alias(con=con)
    delta_position_frame = tas.get_net_position_4strategy_alias(
        alias=delta_alias, as_of_date=current_date, con=con)

    contract_frame = pd.DataFrame()
    contract_frame['ticker'] = list(
        set(delta_position_frame['ticker'].unique())
        | set(underlying_ticker_list))

    contract_specs_output_list = [
        cmi.get_contract_specs(x) for x in contract_frame['ticker']
    ]

    contract_frame['ticker_head'] = [
        x['ticker_head'] for x in contract_specs_output_list
    ]
    contract_frame['ticker_class'] = [
        x['ticker_class'] for x in contract_specs_output_list
    ]

    contract_frame['cont_indx'] = [
        x['cont_indx'] for x in contract_specs_output_list
    ]

    contract_frame['is_spread_q'] = False

    contract_frame.sort_values(['ticker_head', 'cont_indx'],
                               ascending=[True, True],
                               inplace=True)
    contract_frame.reset_index(drop=True, inplace=True)

    non_flat_frame = contract_frame[~contract_frame['ticker_class'].
                                    isin(flat_curve_ticker_class_list)]

    unique_ticker_head_list = non_flat_frame['ticker_head'].unique()

    for i in range(len(unique_ticker_head_list)):
        ticker_head_frame = non_flat_frame[non_flat_frame['ticker_head'] ==
                                           unique_ticker_head_list[i]]
        if len(ticker_head_frame.index) > 1:
            for j in range(len(ticker_head_frame.index) - 1):
                for k in range(j + 1, len(ticker_head_frame.index)):
                    spread_ticker = ticker_head_frame['ticker'].iloc[
                        j] + '-' + ticker_head_frame['ticker'].iloc[k]
                    contract_frame.loc[len(contract_frame.index)] = [
                        spread_ticker,
                        ticker_head_frame['ticker_head'].iloc[j],
                        ticker_head_frame['ticker_class'].iloc[j],
                        ticker_head_frame['cont_indx'].iloc[j], True
                    ]

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

    return contract_frame
Esempio n. 17
0
def get_hedge_frame(**kwargs):

    con = msu.get_my_sql_connection(**kwargs)
    delta_alias = get_delta_strategy_alias(con=con)
    current_date = cu.get_doubledate()

    delta_position_frame = tas.get_net_position_4strategy_alias(
        alias=delta_alias, as_of_date=current_date, con=con)

    contract_specs_output_list = [
        cmi.get_contract_specs(x) for x in delta_position_frame['ticker']
    ]

    delta_position_frame['ticker_head'] = [
        x['ticker_head'] for x in contract_specs_output_list
    ]
    delta_position_frame['ticker_class'] = [
        x['ticker_class'] for x in contract_specs_output_list
    ]
    delta_position_frame['cont_indx'] = [
        x['cont_indx'] for x in contract_specs_output_list
    ]

    #delta_position_frame['qty'].iloc[0]  = 2
    #delta_position_frame['qty'].iloc[1] =  -1
    #delta_position_frame['qty'].iloc[2] = 1

    non_flat_frame = delta_position_frame[~delta_position_frame['ticker_class']
                                          .isin(flat_curve_ticker_class_list)]

    unique_ticker_head_list = non_flat_frame['ticker_head'].unique()

    non_flat_curve_hedge_frame = pd.DataFrame(columns=[
        'ticker', 'ticker_head', 'is_spread_q', 'is_expiration_roll_q',
        'tr_days_2_roll', 'hedge'
    ])

    for i in range(len(unique_ticker_head_list)):
        ticker_head_frame = non_flat_frame[non_flat_frame['ticker_head'] ==
                                           unique_ticker_head_list[i]]
        ticker_head_frame.reset_index(drop=True, inplace=True)

        if len(ticker_head_frame.index) == 1:
            non_flat_curve_hedge_frame.loc[len(non_flat_curve_hedge_frame.index)] = \
                [ticker_head_frame['ticker'].iloc[0], ticker_head_frame['ticker_head'].iloc[0],
                 False, False, 100, calc_hedge_quantity(qty=ticker_head_frame['qty'].iloc[0])]
            continue

        raw_sum = ticker_head_frame['qty'].sum()
        outright_hedge = calc_hedge_quantity(qty=raw_sum)

        max_indx = ticker_head_frame['qty'].idxmax()
        min_indx = ticker_head_frame['qty'].idxmin()

        if (outright_hedge > 0):
            non_flat_curve_hedge_frame.loc[len(non_flat_curve_hedge_frame.index)] = \
                [ticker_head_frame['ticker'].loc[min_indx], ticker_head_frame['ticker_head'].loc[min_indx],
                 False, False, 100, outright_hedge]
            ticker_head_frame['qty'].loc[min_indx] = ticker_head_frame[
                'qty'].loc[min_indx] + outright_hedge

        if (outright_hedge < 0):
            non_flat_curve_hedge_frame.loc[len(non_flat_curve_hedge_frame.index)] = \
                [ticker_head_frame['ticker'].loc[max_indx], ticker_head_frame['ticker_head'].loc[max_indx],
                 False, False, 100, outright_hedge]
            ticker_head_frame['qty'].loc[max_indx] = ticker_head_frame[
                'qty'].loc[max_indx] + outright_hedge

        position_cleaned_q = False

        while (not position_cleaned_q):
            max_indx2 = ticker_head_frame['qty'].idxmax()
            min_indx2 = ticker_head_frame['qty'].idxmin()

            position_cleaned_q = not (
                (ticker_head_frame['qty'].loc[max_indx2] >= 1) and
                (ticker_head_frame['qty'].loc[min_indx2] <= -1))

            if position_cleaned_q:
                break

            spread_hedge = calc_hedge_quantity(
                qty=min(ticker_head_frame['qty'].loc[max_indx2],
                        -ticker_head_frame['qty'].loc[min_indx2]))

            if ticker_head_frame['cont_indx'].loc[
                    max_indx2] < ticker_head_frame['cont_indx'].loc[min_indx2]:
                spread_ticker = ticker_head_frame['ticker'].loc[
                    max_indx2] + '-' + ticker_head_frame['ticker'].loc[
                        min_indx2]
                hedge_qty = spread_hedge
            else:
                spread_ticker = ticker_head_frame['ticker'].loc[
                    min_indx2] + '-' + ticker_head_frame['ticker'].loc[
                        max_indx2]
                hedge_qty = -spread_hedge

            non_flat_curve_hedge_frame.loc[len(non_flat_curve_hedge_frame.index)] = \
                [spread_ticker, unique_ticker_head_list[i], True, False, 100, hedge_qty]
            ticker_head_frame['qty'].loc[max_indx2] = ticker_head_frame[
                'qty'].loc[max_indx2] + spread_hedge
            ticker_head_frame['qty'].loc[min_indx2] = ticker_head_frame[
                'qty'].loc[min_indx2] - spread_hedge

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

    return non_flat_curve_hedge_frame
import get_price.presave_price as pp
import opportunity_constructs.vcs as vcs
import formats.options_strategy_formats as osf
import formats.intraday_futures_strategy_formats as ifsf
import ta.prepare_daily as prep

commodity_address = 'ftp://ftp.cmegroup.com/pub/settle/stlags'
equity_address = 'ftp://ftp.cmegroup.com/pub/settle/stleqt'
fx_address = 'ftp://ftp.cmegroup.com/pub/settle/stlcur'
interest_rate_address = 'ftp://ftp.cmegroup.com/pub/settle/stlint'
comex_futures_csv_address = 'ftp://ftp.cmegroup.com/pub/settle/comex_future.csv'
comex_options_csv_address = 'ftp://ftp.cmegroup.com/pub/settle/comex_option.csv'
nymex_futures_csv_address = 'ftp://ftp.cmegroup.com/pub/settle/nymex_future.csv'
nymex_options_csv_address = 'ftp://ftp.cmegroup.com/pub/settle/nymex_option.csv'

folder_date = cu.get_doubledate()

options_data_dir = dn.get_dated_directory_extension(folder_date=folder_date, ext='raw_options_data')

commodity_output = sd.download_txt_from_web(web_address=commodity_address)
equity_output = sd.download_txt_from_web(web_address=equity_address)
fx_output = sd.download_txt_from_web(web_address=fx_address)
interest_rate_output = sd.download_txt_from_web(web_address=interest_rate_address)

comex_futures_output = sd.download_csv_from_web(web_address=comex_futures_csv_address)
comex_options_output = sd.download_csv_from_web(web_address=comex_options_csv_address)

nymex_futures_output = sd.download_csv_from_web(web_address=nymex_futures_csv_address)
nymex_options_output = sd.download_csv_from_web(web_address=nymex_options_csv_address)

with open(options_data_dir + '/commodity.pkl', 'wb') as handle:
Esempio n. 19
0
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()
Esempio n. 20
0
def main():
    app = algo.Algo()

    admin_dir = dn.get_directory_name(ext='admin')
    risk_file_out = su.read_text_file(file_name=admin_dir +
                                      '/RiskParameter.txt')
    app.bet_size = float(risk_file_out[0])

    con = msu.get_my_sql_connection()
    date_now = cu.get_doubledate()
    report_date = exp.doubledate_shift_bus_days()
    report_date_list = [
        exp.doubledate_shift_bus_days(shift_in_days=x) for x in range(1, 10)
    ]
    overnight_calendars_list = []

    for i in range(len(report_date_list)):
        ocs_output = ocs.generate_overnight_spreads_sheet_4date(
            date_to=report_date_list[i])
        overnight_calendars = ocs_output['overnight_calendars']

        overnight_calendars = \
            overnight_calendars[overnight_calendars['tickerHead'].isin(['CL', 'HO', 'NG', 'C', 'W', 'KW', 'S', 'SM', 'BO', 'LC', 'LN', 'FC'])]

        #isin(['CL', 'HO','NG', 'C', 'W', 'KW', 'S', 'SM', 'BO', 'LC', 'LN', 'FC'])]

        overnight_calendars = overnight_calendars[
            (overnight_calendars['ticker1L'] != '')
            & (overnight_calendars['ticker2L'] != '')]
        overnight_calendars['back_spread_price'] = np.nan
        overnight_calendars['front_spread_price'] = np.nan
        overnight_calendars['mid_ticker_price'] = np.nan

        overnight_calendars['back_spread_ticker'] = [
            overnight_calendars['ticker1'].iloc[x] + '-' +
            overnight_calendars['ticker2'].iloc[x]
            for x in range(len(overnight_calendars.index))
        ]
        overnight_calendars['front_spread_ticker'] = [
            overnight_calendars['ticker1L'].iloc[x] + '-' +
            overnight_calendars['ticker2L'].iloc[x]
            for x in range(len(overnight_calendars.index))
        ]
        overnight_calendars['target_quantity'] = [
            min(mth.ceil(app.bet_size / x),
                app.total_traded_volume_max_before_user_confirmation)
            for x in overnight_calendars['dollarNoise100']
        ]

        overnight_calendars['alias'] = [
            overnight_calendars['ticker1'].iloc[x] + '_' +
            overnight_calendars['ticker2'].iloc[x] + '_ocs'
            for x in range(len(overnight_calendars.index))
        ]
        overnight_calendars['total_quantity'] = 0
        overnight_calendars['total_risk'] = 0
        overnight_calendars['holding_period'] = 0
        #overnight_calendars['expiring_position_q'] = 0

        overnight_calendars.reset_index(drop=True, inplace=True)

        overnight_calendars_list.append(overnight_calendars)
    overnight_calendars = overnight_calendars_list.pop(0)

    open_strategy_frame = ts.get_filtered_open_strategies(
        strategy_class_list=['ocs'], as_of_date=date_now)

    for i in range(len(open_strategy_frame.index)):
        position_manager_output = pm.get_ocs_position(
            alias=open_strategy_frame['alias'].iloc[i],
            as_of_date=date_now,
            con=con)

        trades_frame = ts.get_trades_4strategy_alias(
            alias=open_strategy_frame['alias'].iloc[i], con=con)

        datetime_now = cu.convert_doubledate_2datetime(date_now)
        holding_period = (datetime_now - trades_frame['trade_date'].min()).days

        if (not position_manager_output['empty_position_q']) & (
                not position_manager_output['correct_position_q']):
            print('Check ' + open_strategy_frame['alias'].iloc[i] +
                  ' ! Position may be incorrect')
        elif position_manager_output['correct_position_q']:

            ticker_head = cmi.get_contract_specs(
                position_manager_output['sorted_position']
                ['ticker'].iloc[0])['ticker_head']
            position_name = ''

            if position_manager_output['scale'] > 0:
                position_name = ticker_head + '_long'
            else:
                position_name = ticker_head + '_short'

            app.ocs_portfolio.order_send(ticker=position_name,
                                         qty=abs(
                                             position_manager_output['scale']))
            app.ocs_portfolio.order_fill(ticker=position_name,
                                         qty=abs(
                                             position_manager_output['scale']))

            ticker1 = position_manager_output['sorted_position'][
                'ticker'].iloc[0]
            ticker2 = position_manager_output['sorted_position'][
                'ticker'].iloc[1]

            selection_indx = overnight_calendars[
                'back_spread_ticker'] == ticker1 + '-' + ticker2

            if sum(selection_indx) == 1:
                overnight_calendars.loc[
                    selection_indx,
                    'total_quantity'] = position_manager_output['scale']
                overnight_calendars.loc[
                    selection_indx, 'total_risk'] = position_manager_output[
                        'scale'] * overnight_calendars.loc[selection_indx,
                                                           'dollarNoise100']
                overnight_calendars.loc[
                    selection_indx,
                    'alias'] = open_strategy_frame['alias'].iloc[i]
                overnight_calendars.loc[selection_indx,
                                        'holding_period'] = holding_period

                app.ocs_risk_portfolio.order_send(
                    ticker=position_name,
                    qty=abs(position_manager_output['scale'] *
                            overnight_calendars.loc[selection_indx,
                                                    'dollarNoise100']))
                app.ocs_risk_portfolio.order_fill(
                    ticker=position_name,
                    qty=abs(position_manager_output['scale'] *
                            overnight_calendars.loc[selection_indx,
                                                    'dollarNoise100']))

            else:
                for j in range(len(overnight_calendars_list)):
                    overnight_calendars_past = overnight_calendars_list[j]
                    selection_indx = overnight_calendars_past[
                        'back_spread_ticker'] == ticker1 + '-' + ticker2
                    if sum(selection_indx) == 1:
                        overnight_calendars_past.loc[
                            selection_indx,
                            'total_quantity'] = position_manager_output[
                                'scale']
                        overnight_calendars_past.loc[
                            selection_indx,
                            'total_risk'] = position_manager_output[
                                'scale'] * overnight_calendars_past.loc[
                                    selection_indx, 'dollarNoise100']
                        overnight_calendars_past.loc[
                            selection_indx,
                            'alias'] = open_strategy_frame['alias'].iloc[i]
                        overnight_calendars_past.loc[
                            selection_indx, 'holding_period'] = holding_period

                        app.ocs_risk_portfolio.order_send(
                            ticker=position_name,
                            qty=abs(position_manager_output['scale'] *
                                    overnight_calendars_past.loc[
                                        selection_indx, 'dollarNoise100']))
                        app.ocs_risk_portfolio.order_fill(
                            ticker=position_name,
                            qty=abs(position_manager_output['scale'] *
                                    overnight_calendars_past.loc[
                                        selection_indx, 'dollarNoise100']))

                        if j > 1:
                            overnight_calendars_past.loc[
                                selection_indx, 'butterflyMean'] = np.nan
                            overnight_calendars_past.loc[
                                selection_indx, 'butterflyNoise'] = np.nan

                        overnight_calendars = overnight_calendars.append(
                            overnight_calendars_past[selection_indx])
                        break

    overnight_calendars.reset_index(drop=True, inplace=True)
    overnight_calendars['working_order_id'] = np.nan

    spread_ticker_list = list(
        set(overnight_calendars['back_spread_ticker']).union(
            overnight_calendars['front_spread_ticker']))
    back_spread_ticker_list = list(overnight_calendars['back_spread_ticker'])

    theme_name_list = set([
        x + '_long' for x in back_spread_ticker_list
    ]).union(set([x + '_short' for x in back_spread_ticker_list]))
    ocs_alias_portfolio = aup.portfolio(ticker_list=theme_name_list)

    for i in range(len(overnight_calendars.index)):

        if overnight_calendars.loc[i, 'total_quantity'] > 0:
            position_name = overnight_calendars.loc[
                i, 'back_spread_ticker'] + '_long'
            ocs_alias_portfolio.order_send(
                ticker=position_name,
                qty=overnight_calendars.loc[i, 'total_quantity'])
            ocs_alias_portfolio.order_fill(
                ticker=position_name,
                qty=overnight_calendars.loc[i, 'total_quantity'])
        elif overnight_calendars.loc[i, 'total_quantity'] < 0:
            position_name = overnight_calendars.loc[
                i, 'back_spread_ticker'] + '_short'
            ocs_alias_portfolio.order_send(
                ticker=position_name,
                qty=-overnight_calendars.loc[i, 'total_quantity'])
            ocs_alias_portfolio.order_fill(
                ticker=position_name,
                qty=-overnight_calendars.loc[i, 'total_quantity'])

    app.price_request_dictionary['spread'] = spread_ticker_list
    app.price_request_dictionary['outright'] = overnight_calendars[
        'ticker1'].values
    app.overnight_calendars = overnight_calendars
    app.open_strategy_list = list(open_strategy_frame['alias'])
    app.ocs_alias_portfolio = ocs_alias_portfolio
    app.ticker_list = list(
        set(overnight_calendars['ticker1']).union(
            overnight_calendars['ticker2']).union(
                set(overnight_calendars['ticker1L'])).union(
                    set(overnight_calendars['ticker2L'])))
    app.output_dir = ts.create_strategy_output_dir(strategy_class='ocs',
                                                   report_date=report_date)
    app.log = lg.get_logger(file_identifier='ib_ocs', log_level='INFO')

    app.con = con
    app.pnl_frame = tpm.get_daily_pnl_snapshot(as_of_date=report_date)
    print('Emre')

    app.connect(client_id=2)
    app.run()
def get_hedge_4strategy(**kwargs):

    con = msu.get_my_sql_connection(**kwargs)
    current_date = cu.get_doubledate()
    settle_price_date = exp.doubledate_shift_bus_days(double_date=current_date, shift_in_days=1)

    position_frame = tas.get_net_position_4strategy_alias(alias=kwargs['alias'],as_of_date=current_date,con=con)

    intraday_price_frame = gip.get_cme_direct_prices()
    intraday_price_frame.rename(columns={'ticker': 'underlying_ticker'},inplace=True)

    intraday_price_frame['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in intraday_price_frame['underlying_ticker']]
    intraday_price_frame['mid_price'] = (intraday_price_frame['bid_price'] + intraday_price_frame['ask_price'])/2

    intraday_price_frame['mid_price'] = [tfl.convert_trade_price_from_cme_direct(ticker_head=intraday_price_frame['ticker_head'].iloc[x],
                                        price=intraday_price_frame['mid_price'].iloc[x]) for x in range(len(intraday_price_frame.index))]

    options_frame = position_frame[position_frame['instrument'] == 'O']
    futures_frame = position_frame[position_frame['instrument'] == 'F']

    if options_frame.empty:
        futures_frame.rename(columns={'ticker': 'underlying_ticker', 'qty': 'underlying_delta'},inplace=True)
        futures_frame = futures_frame[['underlying_ticker', 'underlying_delta']]
        net_position = pd.merge(futures_frame, intraday_price_frame, how='left', on='underlying_ticker')
        net_position['hedge_price'] = net_position['mid_price']
        net_position['hedge'] = -net_position['underlying_delta']
        return net_position

    imp_vol_list = [gop.get_options_price_from_db(ticker=options_frame['ticker'].iloc[x],
                                  settle_date=settle_price_date,
                                  strike=options_frame['strike_price'].iloc[x],
                                  column_names=['imp_vol'],
                                  con=con)['imp_vol'] for x in range(len(options_frame.index))]

    options_frame['imp_vol'] = [imp_vol_list[x][1] if (np.isnan(imp_vol_list[x][0]) and len(imp_vol_list[x]) > 1) else imp_vol_list[x][0]
                                for x in range(len(options_frame.index))]

    options_frame['underlying_ticker'] = [omu.get_option_underlying(ticker=x) for x in options_frame['ticker']]

    options_frame = pd.merge(options_frame, intraday_price_frame, how='left', on='underlying_ticker')

    options_frame['ticker_head'] = [cmi.get_contract_specs(x)['ticker_head'] for x in options_frame['ticker']]
    options_frame['exercise_type'] = [cmi.get_option_exercise_type(ticker_head=x) for x in options_frame['ticker_head']]
    options_frame['strike_price'] = options_frame['strike_price'].astype('float64')

    options_frame['delta'] = [omu.option_model_wrapper(ticker=options_frame['ticker'].iloc[x],
                             calculation_date=current_date,
                             interest_rate_date=settle_price_date,
                             underlying=options_frame['mid_price'].iloc[x],
                             strike=options_frame['strike_price'].iloc[x],
                             implied_vol=options_frame['imp_vol'].iloc[x],
                             option_type=options_frame['option_type'].iloc[x],
                             exercise_type=options_frame['exercise_type'].iloc[x],
                             con=con)['delta'] for x in range(len(options_frame.index))]

    options_frame['total_delta'] = options_frame['qty']*options_frame['delta']

    grouped = options_frame.groupby('underlying_ticker')

    net_position = pd.DataFrame()

    net_position['underlying_ticker'] = (grouped['underlying_ticker'].first()).values
    net_position['hedge_price'] = (grouped['mid_price'].first()).values
    net_position['option_delta'] = (grouped['total_delta'].sum()).values
    net_position['option_delta'] = net_position['option_delta'].round(2)

    if futures_frame.empty:
        net_position['total_delta'] = net_position['option_delta']
    else:
        futures_frame.rename(columns={'ticker': 'underlying_ticker', 'qty': 'underlying_delta'},inplace=True)
        futures_frame = futures_frame[['underlying_ticker', 'underlying_delta']]

        isinOptions = futures_frame['underlying_ticker'].isin(net_position['underlying_ticker'])
        futures_frame_w_options = futures_frame[isinOptions]
        futures_frame_wo_options = futures_frame[~isinOptions]

        if futures_frame_w_options.empty:
            net_position['underlying_delta'] = 0
            net_position['total_delta'] = net_position['option_delta']
        else:
            net_position = pd.merge(net_position, futures_frame_w_options, how='outer', on='underlying_ticker')
            net_position['total_delta'] = net_position['option_delta']+net_position['underlying_delta']

        if not futures_frame_wo_options.empty:
            net_position_futures = pd.merge(futures_frame_wo_options, intraday_price_frame, how='left', on='underlying_ticker')
            net_position_futures['hedge_price'] = net_position_futures['mid_price']
            net_position_futures['option_delta'] = 0
            net_position_futures['total_delta'] = net_position_futures['underlying_delta']
            net_position = pd.concat([net_position,net_position_futures[['underlying_ticker','hedge_price','option_delta','underlying_delta','total_delta']]])

    net_position['hedge'] = -net_position['total_delta']

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

    return net_position
Esempio n. 22
0
def load_and_convert_wh_position_file(**kwargs):

    positions_directory = dna.get_directory_name(ext='wh_positions')
    file_name = 'open pos .tul01 5-1.xlsx'

    double_date = cu.get_doubledate()
    century_mark = m.floor(double_date / 1e6) * 100

    wh_frame = pd.read_excel(positions_directory + '/' + file_name,
                             header=None,
                             names=['raw_symbol', 'qty'])

    raw_symbol_list = []
    qty_list = []
    instrument_list = []
    current_instrument = ''
    direction_list = []
    current_direction = ''

    strike_price_list = []
    option_type_list = []
    ticker_list = []
    ticker_head_list = []

    for i in range(len(wh_frame.index) - 1):
        if len(str(wh_frame['raw_symbol'].iloc[i])) > 4 and (str(
                wh_frame['qty'].iloc[i]).isnumeric()):
            raw_symbol_list.append(wh_frame['raw_symbol'].iloc[i])
            instrument_list.append(current_instrument)

            split_out = wh_frame['raw_symbol'].iloc[i].split(' ')
            if split_out[0] in ['CALL', 'PUT']:
                strike_price_list.append(float(split_out[-1]))
                option_type_list.append(split_out[0][0])
                if split_out[0] == 'CALL':
                    month_indx = 1
                    year_indx = 2
                else:
                    month_indx = 2
                    year_indx = 3
            else:
                strike_price_list.append(np.nan)
                option_type_list.append(None)
                month_indx = 0
                year_indx = 1

            ticker_head = conversion_from_man_ticker_head[current_instrument]
            ticker_head_list.append(ticker_head)
            ticker_list.append(ticker_head + cmi.full_letter_month_list[
                cu.three_letter_month_dictionary[split_out[month_indx]] - 1] +
                               str(century_mark + int(split_out[year_indx])))

            if current_direction == 'B':
                qty_list.append(wh_frame['qty'].iloc[i])
            elif current_direction == 'S':
                qty_list.append(-wh_frame['qty'].iloc[i])
            else:
                print(current_direction)
        elif wh_frame['raw_symbol'].iloc[i] in ['B', 'S']:
            current_direction = wh_frame['raw_symbol'].iloc[i]
        else:
            current_instrument = str(wh_frame['raw_symbol'].iloc[i])

    wh_frame = pd.DataFrame()
    wh_frame['Instrument'] = instrument_list
    wh_frame['raw_symbol'] = raw_symbol_list
    wh_frame['strike_price'] = strike_price_list
    wh_frame['option_type'] = option_type_list
    wh_frame['qty'] = qty_list
    wh_frame['ticker_head'] = ticker_head_list
    wh_frame['ticker'] = ticker_list

    wh_frame['strike_multiplier'] = [
        wh_strike_multiplier.get(x, 1) for x in wh_frame['ticker_head']
    ]
    wh_frame['strike_price'] = round(
        wh_frame['strike_multiplier'] * wh_frame['strike_price'], 4)

    wh_frame['instrumet'] = 'F'
    option_indx = (wh_frame['option_type'] == 'C') | (wh_frame['option_type']
                                                      == 'P')
    wh_frame['instrumet'][option_indx] = 'O'

    wh_frame['generalized_ticker'] = wh_frame['ticker']
    wh_frame['generalized_ticker'][option_indx] = wh_frame['ticker'][option_indx] + '-' + \
                                                   wh_frame['option_type'][option_indx] + '-' + \
                                                   wh_frame['strike_price'][option_indx].astype(str)

    wh_frame['generalized_ticker'] = [
        x.rstrip('0').rstrip('.') for x in wh_frame['generalized_ticker']
    ]

    return wh_frame[['generalized_ticker', 'qty']]
import get_price.presave_price as pp
import opportunity_constructs.vcs as vcs
import formats.options_strategy_formats as osf
import formats.futures_strategy_formats as fsf
import ta.prepare_daily as prep

commodity_address = 'ftp://ftp.cmegroup.com/pub/settle/stlags'
equity_address = 'ftp://ftp.cmegroup.com/pub/settle/stleqt'
fx_address = 'ftp://ftp.cmegroup.com/pub/settle/stlcur'
interest_rate_address = 'ftp://ftp.cmegroup.com/pub/settle/stlint'
comex_futures_csv_address = 'ftp://ftp.cmegroup.com/pub/settle/comex_future.csv'
comex_options_csv_address = 'ftp://ftp.cmegroup.com/pub/settle/comex_option.csv'
nymex_futures_csv_address = 'ftp://ftp.cmegroup.com/pub/settle/nymex_future.csv'
nymex_options_csv_address = 'ftp://ftp.cmegroup.com/pub/settle/nymex_option.csv'

folder_date = cu.get_doubledate()

options_data_dir = dn.get_dated_directory_extension(folder_date=folder_date, ext='raw_options_data')

commodity_output = sd.download_txt_from_web(web_address=commodity_address)
equity_output = sd.download_txt_from_web(web_address=equity_address)
fx_output = sd.download_txt_from_web(web_address=fx_address)
interest_rate_output = sd.download_txt_from_web(web_address=interest_rate_address)

comex_futures_output = sd.download_csv_from_web(web_address=comex_futures_csv_address)
comex_options_output = sd.download_csv_from_web(web_address=comex_options_csv_address)

nymex_futures_output = sd.download_csv_from_web(web_address=nymex_futures_csv_address)
nymex_options_output = sd.download_csv_from_web(web_address=nymex_options_csv_address)

with open(options_data_dir + '/commodity.pkl', 'wb') as handle: