Example #1
0
def get_fills_for_instrument(data, instrument_code, strategy_name):
    data_orders = dataOrders(data)
    instrument_strategy = instrumentStrategy(instrument_code=instrument_code, strategy_name=strategy_name)

    list_of_fills = data_orders.get_fills_history_for_instrument_strategy(instrument_strategy)

    return list_of_fills
Example #2
0
    def handle_completed_instrument_order(self, instrument_order_id):
        ## Check children all done
        instrument_order = self.instrument_stack.get_order_with_id_from_stack(instrument_order_id)

        list_of_broker_order_id, list_of_contract_order_id = self.\
            get_all_children_and_grandchildren_for_instrument_order_id(instrument_order_id)

        completely_filled = self.\
            confirm_all_children_and_grandchildren_are_filled(list_of_broker_order_id, list_of_contract_order_id)

        if not completely_filled:
            return success

        # If we have got this far then all our children are filled, and the parent is filled
        contract_order_list = self.contract_stack.get_list_of_orders_from_order_id_list(list_of_contract_order_id)
        broker_order_list = self.broker_stack.get_list_of_orders_from_order_id_list(list_of_broker_order_id)

        # update positions
        position_updater = updatePositions(self.data)
        position_updater.update_positions_with_instrument_and_contract_orders(instrument_order, contract_order_list)

        # Update historic order database
        order_data = dataOrders(self.data)
        order_data.add_historic_orders_to_data(instrument_order, contract_order_list, broker_order_list)

        # Make orders inactive
        # A subsequent process will delete them
        self.deactivate_family_of_orders(instrument_order_id, list_of_contract_order_id, list_of_broker_order_id)

        return success
Example #3
0
def get_trade_df_for_contract(data, instrument_code, contract_id):
    data_orders = dataOrders(data)
    contract = futuresContract(instrument_code, contract_id)
    list_of_trades = data_orders.get_fills_history_for_contract(contract)
    list_of_trades_as_pd_df = list_of_trades.as_pd_df()

    return list_of_trades_as_pd_df
Example #4
0
def get_tuple_object_from_order_id(data, order_id):
    data_orders = dataOrders(data)
    order = data_orders.get_historic_broker_order_from_order_id_with_execution_data(
        order_id)
    tuple_object = transfer_object_attributes(tradesData, order)

    return tuple_object
Example #5
0
    def handle_completed_instrument_order(
        self,
        instrument_order_id,
        allow_partial_completions=False,
        allow_zero_completions=False,
    ):
        # Check children all done

        (
            list_of_broker_order_id,
            list_of_contract_order_id,
        ) = self.get_all_children_and_grandchildren_for_instrument_order_id(
            instrument_order_id)

        completely_filled = self.confirm_all_children_and_grandchildren_are_filled(
            list_of_broker_order_id,
            list_of_contract_order_id,
            allow_partial_completions=allow_partial_completions,
            allow_zero_completions=allow_zero_completions,
        )

        if not completely_filled:
            return success

        # If we have got this far then all our children are filled, and the
        # parent is filled

        # We need to seperate out broker and contract spread orders into their individual components
        # When we do this the original orders are deactivated
        # the instrument order has it's children removed and replaced by the components of each spread order
        # the contract order
        self.split_up_spread_orders(instrument_order_id)

        # we need to do this again in case of any splits
        (
            list_of_broker_order_id,
            list_of_contract_order_id,
        ) = self.get_all_children_and_grandchildren_for_instrument_order_id(
            instrument_order_id)

        contract_order_list = self.contract_stack.get_list_of_orders_from_order_id_list(
            list_of_contract_order_id)
        broker_order_list = self.broker_stack.get_list_of_orders_from_order_id_list(
            list_of_broker_order_id)
        instrument_order = self.instrument_stack.get_order_with_id_from_stack(
            instrument_order_id)

        # Make orders inactive
        # A subsequent process will delete them
        self.deactivate_family_of_orders(instrument_order_id,
                                         list_of_contract_order_id,
                                         list_of_broker_order_id)

        # Update historic order database
        order_data = dataOrders(self.data)
        order_data.add_historic_orders_to_data(instrument_order,
                                               contract_order_list,
                                               broker_order_list)

        return success
Example #6
0
def check_and_if_required_allocate_algo_to_single_contract_order(
        data, contract_order):
    """

    :param data: dataBlog
    :param instrument_order: parent instrument order
    :param list_of_contract_orders:
    :return: list of contract orders with algo added
    """

    if contract_order.algo_to_use != '':
        ## Already done
        return contract_order

    instrument_order_id = contract_order.parent
    order_data = dataOrders(data)

    instrument_stack = order_data.instrument_stack()
    instrument_order = instrument_stack.get_order_with_id_from_stack(
        instrument_order_id)
    if instrument_order is missing_order:
        ## Have to use some kind of default order, but for now the logic isn't required since we
        ##   only have market orders.
        pass
    else:
        # not used yet, but maybe in the future
        instrument_order_type = instrument_order.order_type
        is_roll_order = instrument_order.roll_order

    contract_order.algo_to_use = "sysexecution.algos.algo_market.algo_market"

    return contract_order
Example #7
0
def get_trade_df_for_instrument(data, instrument_code, strategy_name):
    data_orders = dataOrders(data)
    list_of_trades = data_orders.get_fills_history_for_strategy_and_instrument(
        strategy_name, instrument_code)
    list_of_trades_as_pd_df = list_of_trades.as_pd_df()

    return list_of_trades_as_pd_df
def view_individual_order(data):
    list_of_order_types = [
        "Instrument / Strategy",
        "Instrument / Contract",
        "Broker level",
    ]
    print("Which order queue?")
    order_type = print_menu_of_values_and_get_response(list_of_order_types)
    order_id = get_and_convert(
        "Order number?", type_expected=int, default_value=None, default_str="CANCEL"
    )
    if order_id is None:
        return None

    data_orders = dataOrders(data)
    if order_type == list_of_order_types[0]:
        order = data_orders.get_historic_instrument_order_from_order_id(order_id)
    elif order_type == list_of_order_types[1]:
        order = data_orders.get_historic_contract_order_from_order_id(order_id)
    elif order_type == list_of_order_types[2]:
        order = data_orders.get_historic_broker_order_from_order_id(order_id)
    else:
        print("Don't know what to do")
        return None

    print(order.full_repr())

    return None
    def __init__(self, data: dataBlob, strategy_name: str):

        self._strategy_name = strategy_name
        self._data = data
        data_orders = dataOrders(data)
        self._log = data.log
        self._data_orders = data_orders
Example #10
0
    def __init__(self, data):
        data_orders = dataOrders(data)

        self.data = data
        self.data_orders = data_orders
        self.log = data.log

        self._create_strategy_generators()
def check_and_if_required_allocate_algo_to_single_contract_order(
        data, contract_order, instrument_order=arg_not_supplied):
    """

    :param data: dataBlog
    :param instrument_order: parent instrument order
    :param list_of_contract_orders:
    :return: list of contract orders with algo added
    """
    log = contract_order.log_with_attributes(data.log)

    if contract_order.algo_to_use != '':
        ## Already done
        return contract_order

    if instrument_order is arg_not_supplied:
        instrument_order_id = contract_order.parent
        order_data = dataOrders(data)

        instrument_stack = order_data.instrument_stack()
        instrument_order = instrument_stack.get_order_with_id_from_stack(
            instrument_order_id)
        if instrument_order is missing_order:
            log.warn(
                "Couldn't find instrument order and none passed so allocating default algo_market"
            )
            contract_order.algo_to_use = DEFAULT_ALGO
            return contract_order

    instrument_order_type = instrument_order.order_type

    # not used yet, but maybe in the future
    is_roll_order = instrument_order.roll_order

    data_broker = dataBroker(data)
    short_of_time = data_broker.less_than_one_hour_of_trading_leg_for_instrument_code_and_contract_date(
        contract_order.instrument_code, contract_order.contract_id[0])

    ## HORRIBLE HACK!!!!!
    if instrument_order.instrument_code == "EUROSTX":
        contract_order.algo_to_use = MARKET_ALGO
    elif instrument_order_type == 'market':
        log.msg("Market order type, so allocating to algo_market")
        contract_order.algo_to_use = MARKET_ALGO
    elif instrument_order_type in ["best", "Zero-roll-order"]:
        if short_of_time:
            log.warn("Short of time, so allocating to algo_market")
            contract_order.algo_to_use = MARKET_ALGO
        else:
            log.msg("'Best' order so allocating to original_best")
            contract_order.algo_to_use = ORIGINAL_BEST
    else:
        log.warn(
            "Don't recognise order type %s so allocating to default algo_market"
            % instrument_order_type)
        contract_order.algo_to_use = DEFAULT_ALGO

    return contract_order
Example #12
0
def get_recent_trades_from_db(data):
    data_orders = dataOrders(data)
    start_date = datetime.datetime.now() - datetime.timedelta(days = 1)
    order_id_list = data_orders.get_historic_broker_orders_in_date_range(start_date)
    orders_as_list = [get_tuple_object_from_order_id(data, order_id)
                      for order_id in order_id_list]
    pdf = make_df_from_list_of_named_tuple(tradesData, orders_as_list)

    return pdf
Example #13
0
def get_recent_broker_orders(data, start_date, end_date):
    data_orders = dataOrders(data)
    order_id_list = data_orders.get_historic_broker_order_ids_in_date_range(
        start_date, end_date)
    orders_as_list = [
        get_tuple_object_from_order_id(data, order_id)
        for order_id in order_id_list
    ]
    pdf = make_df_from_list_of_named_tuple(tradesData, orders_as_list)

    return pdf
    def __init__(self, data):
        order_data = dataOrders(data)
        instrument_stack = order_data.instrument_stack()
        contract_stack = order_data.contract_stack()

        self.instrument_stack = instrument_stack
        self.contract_stack = contract_stack

        self.order_data = order_data
        self.data = data
        self.log = data.log
def check_and_if_required_allocate_algo_to_single_contract_order(data, contract_order):
    """

    :param data: dataBlog
    :param instrument_order: parent instrument order
    :param list_of_contract_orders:
    :return: list of contract orders with algo added
    """
    log = contract_order.log_with_attributes(data.log)

    if contract_order.algo_to_use!='':
        ## Already done
        return contract_order

    instrument_order_id = contract_order.parent
    order_data = dataOrders(data)

    instrument_stack = order_data.instrument_stack()
    instrument_order = instrument_stack.get_order_with_id_from_stack(instrument_order_id)
    if instrument_order is missing_order:
        log.warn("Couldn't find instrument order so allocating default algo_market")
        contract_order.algo_to_use = "sysexecution.algos.algo_market.algo_market"
        return contract_order
    else:
        instrument_order_type = instrument_order.order_type

        # not used yet, but maybe in the future
        is_roll_order = instrument_order.roll_order

    data_broker = dataBroker(data)
    short_of_time = data_broker.less_than_one_hour_of_trading_leg_for_instrument_code_and_contract_date(contract_order.instrument_code,
                                                                                        contract_order.contract_id[0])

    contract_order.algo_to_use = "sysexecution.algos.algo_market.algo_market"

    """
    UNCOMMENT WHEN BEST ALGO TESTED
    if instrument_order_type == 'market':
        log.msg("Market order type, so allocating to algo_market")
        contract_order.algo_to_use = "sysexecution.algos.algo_market.algo_market"
    elif instrument_order_type == "best":
        if short_of_time:
            log.warn("Short of time, so allocating to algo_market")
            contract_order.algo_to_use = "sysexecution.algos.algo_market.algo_market"
        else:
            log.msg("'Best' order so allocating to original_best")
            contract_order.algo_to_use = "sysexecution.algos.algo_original_best.original_best"
    else:
        log.warn("Don't recognise order type %s so allocating to default algo_market" % instrument_order_type)
        contract_order.algo_to_use = "sysexecution.algos.algo_market.algo_market"
    """

    return contract_order
Example #16
0
    def add_order_family_to_historic_orders_database(
            self, order_family: orderFamily):

        instrument_order = self.instrument_stack.get_order_with_id_from_stack(
            order_family.instrument_order_id)
        contract_order_list = self.contract_stack.get_list_of_orders_from_order_id_list(
            order_family.list_of_contract_order_id)
        broker_order_list = self.broker_stack.get_list_of_orders_from_order_id_list(
            order_family.list_of_broker_order_id)

        # Update historic order database
        order_data = dataOrders(self.data)
        order_data.add_historic_orders_to_data(instrument_order,
                                               contract_order_list,
                                               broker_order_list)
Example #17
0
    def __init__(self, data: dataBlob=arg_not_supplied):
        if data is arg_not_supplied:
            data = dataBlob()

        self._data = data
        self._log = data.log

        order_data = dataOrders(data)

        instrument_stack = order_data.db_instrument_stack_data
        contract_stack = order_data.db_contract_stack_data
        broker_stack = order_data.db_broker_stack_data

        self._instrument_stack = instrument_stack
        self._contract_stack = contract_stack
        self._broker_stack = broker_stack
Example #18
0
def get_order_pd(data, list_method = 'get_historic_instrument_orders_in_date_range',
                 getter_method = 'get_historic_instrument_order_from_order_id'):
    start_date = get_datetime_input("Start Date", allow_default = True)
    end_date = get_datetime_input("End Date", allow_default = True)

    data_orders = dataOrders(data)
    list_func = getattr(data_orders, list_method)
    getter_func  = getattr(data_orders, getter_method)

    order_id_list = list_func(start_date, end_date)
    order_list = [getter_func(id)
                  for id in order_id_list]
    order_list_object = listOfOrders(order_list)
    order_pd = order_list_object.as_pd()

    return order_pd
Example #19
0
    def __init__(self, data=arg_not_supplied):
        if data is arg_not_supplied:
            data = dataBlob()

        order_data = dataOrders(data)

        instrument_stack = order_data.instrument_stack()
        contract_stack = order_data.contract_stack()
        broker_stack = order_data.broker_stack()

        self.instrument_stack = instrument_stack
        self.contract_stack = contract_stack
        self.broker_stack = broker_stack

        self.order_data = order_data
        self.data = data
        self.log = data.log
def get_order_pd(
    data,
    list_method="get_historic_instrument_order_ids_in_date_range",
    getter_method="get_historic_instrument_order_from_order_id",
):
    start_date, end_date = get_report_dates()

    data_orders = dataOrders(data)
    list_func = getattr(data_orders, list_method)
    getter_func = getattr(data_orders, getter_method)

    order_id_list = list_func(start_date, end_date)
    order_list = [getter_func(id) for id in order_id_list]
    order_list_object = listOfOrders(order_list)
    order_pd = order_list_object.as_pd()

    return order_pd
    def handle_completed_instrument_order(self, instrument_order_id):
        ## Check children all done
        instrument_order = self.instrument_stack.get_order_with_id_from_stack(
            instrument_order_id)
        list_of_contract_order_id = instrument_order.children
        if list_of_contract_order_id is no_children:
            list_of_contract_order_id = []

        for contract_order_id in list_of_contract_order_id:
            completely_filled = self.contract_stack.is_completed(
                contract_order_id)
            if not completely_filled:
                ## OK We can't do this unless all our children are filled
                return success

        # If we have got this far then all our children are filled, and the parent is filled

        list_of_contract_orders = []
        for contract_order_id in list_of_contract_order_id:
            list_of_contract_orders.append(
                self.contract_stack.get_order_with_id_from_stack(
                    contract_order_id))

        position_updater = updatePositions(self.data)
        # Update strategy position table
        position_updater.update_strategy_position_table_with_instrument_order(
            instrument_order)

        # Update contract position table
        for contract_order in list_of_contract_orders:
            position_updater.update_contract_position_table_with_contract_order(
                contract_order)

        order_data = dataOrders(self.data)
        # Update historic order database
        order_data.add_historic_instrument_order_to_data(instrument_order)
        for contract_order in list_of_contract_orders:
            order_data.add_historic_contract_order_to_data(contract_order)

        # Make orders inactive
        # A subsequent process will delete them
        self.instrument_stack.deactivate_order(instrument_order_id)
        for contract_order_id in list_of_contract_order_id:
            self.contract_stack.deactivate_order(contract_order_id)

        return success
Example #22
0
def check_and_if_required_allocate_algo_to_single_contract_order(data, contract_order):
    """

    :param data: dataBlog
    :param instrument_order: parent instrument order
    :param list_of_contract_orders:
    :return: list of contract orders with algo added
    """
    log = contract_order.log_with_attributes(data.log)

    if contract_order.algo_to_use!='':
        ## Already done
        return contract_order

    instrument_order_id = contract_order.parent
    order_data = dataOrders(data)

    instrument_stack = order_data.instrument_stack()
    instrument_order = instrument_stack.get_order_with_id_from_stack(instrument_order_id)
    if instrument_order is missing_order:
        ## Have to use some kind of default order, but for now the logic isn't required since we
        ##   only have market orders.
        pass
    else:
        # not used yet, but maybe in the future
        instrument_order_type = instrument_order.order_type
        is_roll_order = instrument_order.roll_order

    if instrument_order_type == 'market':
        contract_order.algo_to_use = "sysexecution.algos.algo_market.algo_market"
    elif instrument_order_type == "best":
        contract_order.algo_to_use = "sysexecution.algos.algo_market.algo_market"
        #contract_order.algo_to_use = "sysexecution.algos.algo_original_best.original_best"
    else:
        log.warn("Don't recognise order type %s so allocating to default algo_market" % instrument_order_type)
        contract_order.algo_to_use = "sysexecution.algos.algo_market.algo_market"


    return contract_order
 def setup_before_placing(self, data):
     data_orders = dataOrders(data)
     self.data = data
     self.log = data.log
     self.data_orders = data_orders
Example #24
0
def get_fills_for_contract(data, instrument_code, contract_id):
    data_orders = dataOrders(data)
    contract = futuresContract(instrument_code, contract_id)
    list_of_fills = data_orders.get_fills_history_for_contract(contract)

    return list_of_fills