def get_candlestick_patterns(**kwargs):

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

    output_dictionary = {}
    output_dictionary['bearish_big_shadowQ'] = 0
    output_dictionary['bullish_big_shadowQ'] = 0
    output['success'] = 0


    data_input = gfp.get_futures_price_4ticker(ticker=ticker, date_to=date_to)


    data_input.rename(columns={'open_price': 'open', 'high_price': 'high', 'low_price': 'low', 'close_price': 'close'}, inplace=True)

    high1 = data_input['high'].iloc[-1]
    low1 = data_input['low'].iloc[-1]
    open1 = data_input['open'].iloc[-1]
    close1 = data_input['close'].iloc[-1]

    high2 = data_input['high'].iloc[-2]
    low2 = data_input['low'].iloc[-2]
    open2 = data_input['open'].iloc[-2]
    close2 = data_input['close'].iloc[-2]

    data_input['range'] = data_input['high']-data_input['low']

    return data_input

    # checking for big shadow

    pattern_list = []

    if (high1>high2) and (low1<low2) and len(data_input.index)>=10 and (data_input['range'].iloc[-10:].max() <= data_input['range'].iloc[-1]):
        if(close2>open2) and (close1<0.75*low1+0.25*high1) and (data_input['high'].iloc[-2]-data_input['high'].iloc[-9:-2].max()>0):

            pattern_list.append('bearishBigShadow')

        if(close2<open2) and (close1>0.25*low1+0.75*high1) and (data_input['low'].iloc[-2]-data_input['low'].iloc[-9:-2].min()<0):
            pattern_list.append('bullishBigShadow')
Example #2
0
def move_position_from_strategy_2_strategy(**kwargs):

    strategy_from = kwargs['strategy_from']
    strategy_to = kwargs['strategy_to']

    now_time = dt.datetime.now()

    con = msu.get_my_sql_connection(**kwargs)

    if 'con' not in kwargs.keys():
        kwargs['con'] = con

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

    if 'pricing_date' in kwargs.keys():
        pricing_date = kwargs['pricing_date']
    else:
        pricing_date = as_of_date

    net_position_frame = get_net_position_4strategy_alias(alias=strategy_from,
                                                          **kwargs)

    target_strategy_id = get_strategy_id_from_alias(alias=strategy_to,
                                                    **kwargs)
    source_strategy_id = get_strategy_id_from_alias(alias=strategy_from,
                                                    **kwargs)

    futures_position_frame = net_position_frame[
        net_position_frame['instrument'] == 'F']
    options_position_frame = net_position_frame[
        net_position_frame['instrument'] == 'O']

    futures_position_frame['trade_price'] = \
        [float(gfp.get_futures_price_4ticker(ticker=x,date_from=pricing_date,date_to=pricing_date,con=con)['close_price'][0]) for x in futures_position_frame['ticker']]

    if not options_position_frame.empty:
        options_position_frame['trade_price'] = options_position_frame.apply(
            lambda row: gop.get_options_price_from_db(
                ticker=row['ticker'],
                strike=row['strike_price'],
                option_type=row['option_type'],
                con=con,
                return_nan_if_emptyQ=True,
                settle_date=pricing_date)['close_price'][0],
            axis=1)

        net_position_frame = pd.concat(
            [futures_position_frame, options_position_frame])
    else:
        net_position_frame = futures_position_frame

    column_names = net_position_frame.columns.tolist()

    ticker_indx = column_names.index('ticker')
    option_type_indx = column_names.index('option_type')
    strike_price_indx = column_names.index('strike_price')
    trade_price_indx = column_names.index('trade_price')
    trade_quantity_indx = column_names.index('qty')
    instrument_indx = column_names.index('instrument')

    column_str = "ticker, option_type, strike_price, strategy_id, trade_price, trade_quantity, trade_date, instrument, real_tradeQ, created_date, last_updated_date"
    insert_str = ("%s, " * 11)[:-2]

    final_str = "INSERT INTO trades (%s) VALUES (%s)" % (column_str,
                                                         insert_str)

    tuples_target = [
        tuple([
            x[ticker_indx], x[option_type_indx],
            None if np.isnan(x[strike_price_indx]) else x[strike_price_indx],
            target_strategy_id, x[trade_price_indx], x[trade_quantity_indx],
            as_of_date, x[instrument_indx], True, now_time, now_time
        ]) for x in net_position_frame.values
    ]

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

    tuples_source = [
        tuple([
            x[ticker_indx], x[option_type_indx],
            None if np.isnan(x[strike_price_indx]) else x[strike_price_indx],
            source_strategy_id, x[trade_price_indx], -x[trade_quantity_indx],
            as_of_date, x[instrument_indx], True, now_time, now_time
        ]) for x in net_position_frame.values
    ]

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

    if 'con' not in kwargs.keys():
        con.close()
Example #3
0
def move_position_from_strategy_2_strategy(**kwargs):

    strategy_from = kwargs['strategy_from']
    strategy_to = kwargs['strategy_to']

    now_time = dt.datetime.now()

    con = msu.get_my_sql_connection(**kwargs)

    if 'con' not in kwargs.keys():
        kwargs['con'] = con

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

    net_position_frame = get_net_position_4strategy_alias(alias=strategy_from, **kwargs)

    target_strategy_id = get_strategy_id_from_alias(alias=strategy_to, **kwargs)
    source_strategy_id = get_strategy_id_from_alias(alias=strategy_from, **kwargs)

    futures_position_frame = net_position_frame[net_position_frame['instrument'] == 'F']
    options_position_frame = net_position_frame[net_position_frame['instrument'] == 'O']

    futures_position_frame['trade_price'] = \
        [float(gfp.get_futures_price_4ticker(ticker=x,date_from=as_of_date,date_to=as_of_date,con=con)['close_price'][0]) for x in futures_position_frame['ticker']]

    if not options_position_frame.empty:
        options_position_frame['trade_price'] = options_position_frame.apply(lambda row: gop.get_options_price_from_db(ticker=row['ticker'],
                                                                    strike=row['strike_price'],
                                                                    option_type=row['option_type'],con=con,
                                                                    return_nan_if_emptyQ=True,
                                                                    settle_date=as_of_date)['close_price'][0], axis=1)

        net_position_frame = pd.concat([futures_position_frame,options_position_frame])
    else:
        net_position_frame = futures_position_frame

    column_names = net_position_frame.columns.tolist()

    ticker_indx = column_names.index('ticker')
    option_type_indx = column_names.index('option_type')
    strike_price_indx = column_names.index('strike_price')
    trade_price_indx = column_names.index('trade_price')
    trade_quantity_indx = column_names.index('qty')
    instrument_indx = column_names.index('instrument')

    column_str = "ticker, option_type, strike_price, strategy_id, trade_price, trade_quantity, trade_date, instrument, real_tradeQ, created_date, last_updated_date"
    insert_str = ("%s, " * 11)[:-2]

    final_str = "INSERT INTO trades (%s) VALUES (%s)" % (column_str, insert_str)

    tuples_target = [tuple([x[ticker_indx],x[option_type_indx],
                            None if np.isnan(x[strike_price_indx]) else x[strike_price_indx], target_strategy_id,
              x[trade_price_indx], x[trade_quantity_indx],
              as_of_date,x[instrument_indx], True,now_time,now_time]) for x in net_position_frame.values]

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

    tuples_source = [tuple([x[ticker_indx],x[option_type_indx],
                            None if np.isnan(x[strike_price_indx]) else x[strike_price_indx], source_strategy_id,
              x[trade_price_indx], -x[trade_quantity_indx],
              as_of_date,x[instrument_indx], True,now_time,now_time]) for x in net_position_frame.values]

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

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

    ticker_head = kwargs['ticker_head']

    con = msu.get_my_sql_connection(**kwargs)

    if os.path.isfile(presaved_futures_data_folder + '/' + ticker_head +
                      '.pkl'):
        old_data = pd.read_pickle(presaved_futures_data_folder + '/' +
                                  ticker_head + '.pkl')
        last_available_date = int(
            old_data['settle_date'].max().to_pydatetime().strftime('%Y%m%d'))
        date_from = cu.doubledate_shift(last_available_date, 60)
        data4_tickerhead = gfp.get_futures_price_4ticker(
            ticker_head=ticker_head, date_from=date_from, con=con)
    else:
        data4_tickerhead = gfp.get_futures_price_4ticker(
            ticker_head=ticker_head, con=con)

    data4_tickerhead = pd.merge(data4_tickerhead,
                                dirty_data_points,
                                on=['settle_date', 'ticker'],
                                how='left')
    data4_tickerhead = data4_tickerhead[data4_tickerhead['discard'] != True]
    data4_tickerhead = data4_tickerhead.drop('discard', 1)

    data4_tickerhead['close_price'] = [
        float(x) if x is not None else float('NaN')
        for x in data4_tickerhead['close_price'].values
    ]
    data4_tickerhead['open_price'] = [
        float(x) if x is not None else float('NaN')
        for x in data4_tickerhead['open_price'].values
    ]
    data4_tickerhead['high_price'] = [
        float(x) if x is not None else float('NaN')
        for x in data4_tickerhead['high_price'].values
    ]
    data4_tickerhead['low_price'] = [
        float(x) if x is not None else float('NaN')
        for x in data4_tickerhead['low_price'].values
    ]

    data4_tickerhead['cont_indx'] = 100 * data4_tickerhead[
        'ticker_year'] + data4_tickerhead['ticker_month']
    unique_cont_indx_list = data4_tickerhead['cont_indx'].unique()
    num_contracts = len(unique_cont_indx_list)
    unique_cont_indx_list = np.sort(unique_cont_indx_list)
    merged_dataframe_list = [None] * num_contracts

    bday_us = CustomBusinessDay(calendar=exp.get_calendar_4ticker_head('CL'))
    full_dates = pd.date_range(start=data4_tickerhead['settle_date'].min(),
                               end=data4_tickerhead['settle_date'].max(),
                               freq=bday_us)

    for i in range(num_contracts):

        contract_data = data4_tickerhead[data4_tickerhead['cont_indx'] ==
                                         unique_cont_indx_list[i]]

        contract_full_dates = full_dates[
            (full_dates >= contract_data['settle_date'].min())
            & (full_dates <= contract_data['settle_date'].max())]
        full_date_frame = pd.DataFrame(contract_full_dates,
                                       columns=['settle_date'])
        merged_dataframe_list[i] = pd.merge(full_date_frame,
                                            contract_data,
                                            on='settle_date',
                                            how='left')

        merged_dataframe_list[i]['ticker'] = contract_data['ticker'][
            contract_data.index[0]]
        merged_dataframe_list[i]['ticker_head'] = contract_data['ticker_head'][
            contract_data.index[0]]
        merged_dataframe_list[i]['ticker_month'] = contract_data[
            'ticker_month'][contract_data.index[0]]
        merged_dataframe_list[i]['ticker_year'] = contract_data['ticker_year'][
            contract_data.index[0]]
        merged_dataframe_list[i]['cont_indx'] = contract_data['cont_indx'][
            contract_data.index[0]]

        merged_dataframe_list[i][
            'change1'] = merged_dataframe_list[i]['close_price'].shift(
                -2) - merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i]['change1_instant'] = merged_dataframe_list[i][
            'close_price'].shift(-1) - merged_dataframe_list[i]['close_price']
        merged_dataframe_list[i]['high1_instant'] = merged_dataframe_list[i][
            'high_price'].shift(-1)
        merged_dataframe_list[i]['low1_instant'] = merged_dataframe_list[i][
            'low_price'].shift(-1)
        merged_dataframe_list[i][
            'change2'] = merged_dataframe_list[i]['close_price'].shift(
                -3) - merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i][
            'change5'] = merged_dataframe_list[i]['close_price'].shift(
                -6) - merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i][
            'change10'] = merged_dataframe_list[i]['close_price'].shift(
                -11) - merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i][
            'change20'] = merged_dataframe_list[i]['close_price'].shift(
                -21) - merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i]['change_5'] = merged_dataframe_list[i][
            'close_price'] - merged_dataframe_list[i]['close_price'].shift(5)
        merged_dataframe_list[i]['change_1'] = merged_dataframe_list[i][
            'close_price'] - merged_dataframe_list[i]['close_price'].shift(1)

    data4_tickerhead = pd.concat(merged_dataframe_list)

    if os.path.isfile(presaved_futures_data_folder + '/' + ticker_head +
                      '.pkl'):
        data4_tickerhead['past_indx'] = [
            1 if np.isfinite(x) else 0
            for x in data4_tickerhead['change_5'].values
        ]
        clean_data = data4_tickerhead
        clean_data['frame_indx'] = 1

        data_columns = old_data.columns
        old_data['frame_indx'] = 0
        old_data['past_indx'] = [
            1 if np.isfinite(x) else 0 for x in old_data['change_5'].values
        ]
        merged_data = pd.concat([old_data, clean_data],
                                ignore_index=True,
                                sort=True)
        merged_data.sort_values(
            ['cont_indx', 'settle_date', 'past_indx', 'frame_indx'],
            ascending=[True, True, False, False],
            inplace=True)
        merged_data.drop_duplicates(subset=['settle_date', 'cont_indx'],
                                    keep='first',
                                    inplace=True)
        data4_tickerhead = merged_data.drop(['frame_indx', 'past_indx'],
                                            1,
                                            inplace=False)
        data4_tickerhead = data4_tickerhead[data_columns]

    data4_tickerhead.to_pickle(presaved_futures_data_folder + '/' +
                               ticker_head + '.pkl')

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

    ticker_head = kwargs['ticker_head']

    con = msu.get_my_sql_connection(**kwargs)

    if os.path.isfile(presaved_futures_data_folder + '/' + ticker_head + '.pkl'):
        old_data = pd.read_pickle(presaved_futures_data_folder + '/' + ticker_head + '.pkl')
        last_available_date = int(old_data['settle_date'].max().to_datetime().strftime('%Y%m%d'))
        date_from = cu.doubledate_shift(last_available_date, 60)
        data4_tickerhead = gfp.get_futures_price_4ticker(ticker_head=ticker_head, date_from=date_from, con=con)
    else:
        data4_tickerhead = gfp.get_futures_price_4ticker(ticker_head=ticker_head, con=con)

    data4_tickerhead = pd.merge(data4_tickerhead, dirty_data_points, on=['settle_date', 'ticker'],how='left')
    data4_tickerhead = data4_tickerhead[data4_tickerhead['discard'] != True]
    data4_tickerhead = data4_tickerhead.drop('discard', 1)

    data4_tickerhead['close_price'] = [float(x) if x is not None else float('NaN') for x in data4_tickerhead['close_price'].values]
    data4_tickerhead['open_price'] = [float(x) if x is not None else float('NaN') for x in data4_tickerhead['open_price'].values]
    data4_tickerhead['high_price'] = [float(x) if x is not None else float('NaN') for x in data4_tickerhead['high_price'].values]
    data4_tickerhead['low_price'] = [float(x) if x is not None else float('NaN') for x in data4_tickerhead['low_price'].values]

    data4_tickerhead['cont_indx'] = 100*data4_tickerhead['ticker_year']+data4_tickerhead['ticker_month']
    unique_cont_indx_list = data4_tickerhead['cont_indx'].unique()
    num_contracts = len(unique_cont_indx_list)
    unique_cont_indx_list = np.sort(unique_cont_indx_list)
    merged_dataframe_list = [None]*num_contracts

    bday_us = CustomBusinessDay(calendar=exp.get_calendar_4ticker_head('CL'))
    full_dates = pd.date_range(start=data4_tickerhead['settle_date'].min(),end=data4_tickerhead['settle_date'].max(), freq=bday_us)

    for i in range(num_contracts):

        contract_data = data4_tickerhead[data4_tickerhead['cont_indx']==unique_cont_indx_list[i]]

        contract_full_dates = full_dates[(full_dates >= contract_data['settle_date'].min()) & (full_dates<=contract_data['settle_date'].max())]
        full_date_frame = pd.DataFrame(contract_full_dates, columns=['settle_date'])
        merged_dataframe_list[i] = pd.merge(full_date_frame,contract_data,on='settle_date',how='left')

        merged_dataframe_list[i]['ticker'] = contract_data['ticker'][contract_data.index[0]]
        merged_dataframe_list[i]['ticker_head'] = contract_data['ticker_head'][contract_data.index[0]]
        merged_dataframe_list[i]['ticker_month'] = contract_data['ticker_month'][contract_data.index[0]]
        merged_dataframe_list[i]['ticker_year'] = contract_data['ticker_year'][contract_data.index[0]]
        merged_dataframe_list[i]['cont_indx'] = contract_data['cont_indx'][contract_data.index[0]]

        merged_dataframe_list[i]['change1'] = merged_dataframe_list[i]['close_price'].shift(-2)-merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i]['change1_instant'] = merged_dataframe_list[i]['close_price'].shift(-1)-merged_dataframe_list[i]['close_price']
        merged_dataframe_list[i]['change2'] = merged_dataframe_list[i]['close_price'].shift(-3)-merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i]['change5'] = merged_dataframe_list[i]['close_price'].shift(-6)-merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i]['change10'] = merged_dataframe_list[i]['close_price'].shift(-11)-merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i]['change20'] = merged_dataframe_list[i]['close_price'].shift(-21)-merged_dataframe_list[i]['close_price'].shift(-1)
        merged_dataframe_list[i]['change_5'] = merged_dataframe_list[i]['close_price']-merged_dataframe_list[i]['close_price'].shift(5)
        merged_dataframe_list[i]['change_1'] = merged_dataframe_list[i]['close_price']-merged_dataframe_list[i]['close_price'].shift(1)

    data4_tickerhead = pd.concat(merged_dataframe_list)

    if os.path.isfile(presaved_futures_data_folder + '/' + ticker_head + '.pkl'):
        clean_data = data4_tickerhead[np.isfinite(data4_tickerhead['change_5'])]
        clean_data['frame_indx'] = 1
        old_data['frame_indx'] = 0
        merged_data = pd.concat([old_data,clean_data],ignore_index=True)
        merged_data.sort(['cont_indx', 'settle_date', 'frame_indx'], ascending=[True, True, False], inplace=True)
        merged_data.drop_duplicates(subset=['settle_date', 'cont_indx'], take_last=False, inplace=True)
        data4_tickerhead = merged_data.drop('frame_indx', 1, inplace=False)

    data4_tickerhead.to_pickle(presaved_futures_data_folder + '/' + ticker_head + '.pkl')

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