Exemple #1
0
    def apply_contract_fill_to_parent_order_distributed_children(
            self, list_of_contract_order_ids, instrument_order):
        # A distributed instrument order is like this: buy 2 EDOLLAR instrument order
        # split into buy 1 202306, buy 1 203209

        ##
        log = instrument_order.log_with_attributes(self.log)

        contract_orders = [
            self.contract_stack.get_order_with_id_from_stack(contract_id)
            for contract_id in list_of_contract_order_ids
        ]

        # We apply: total quantity, average price, highest datetime

        list_of_filled_qty = [order.fill for order in contract_orders]
        list_of_filled_price = listOfFillPrice(
            [order.filled_price for order in contract_orders])
        list_of_filled_datetime = [
            order.fill_datetime for order in contract_orders
        ]

        final_fill_datetime = max(list_of_filled_datetime)
        total_filled_qty = sum(list_of_filled_qty)
        average_fill_price = list_of_filled_price.average_fill_price()

        result = self.fill_for_instrument_in_database(instrument_order,
                                                      total_filled_qty,
                                                      average_fill_price,
                                                      final_fill_datetime)

        return result
Exemple #2
0
    def apply_broker_fill_to_contract_order(self, contract_order_id):
        contract_order = self.contract_stack.get_order_with_id_from_stack(
            contract_order_id
        )

        children = contract_order.children
        if children is no_children:
            # no children created yet, definitely no fills
            return None

        broker_order_list = self.broker_stack.get_list_of_orders_from_order_id_list(
            children)

        # We apply: total quantity, average price, highest datetime

        list_of_filled_qty = [order.fill for order in broker_order_list]

        zero_fills = [fill.equals_zero() for fill in list_of_filled_qty]
        if all(zero_fills):
            # nothing to do here
            return None

        list_of_filled_price = listOfFillPrice(
            [order.filled_price for order in broker_order_list]
        )
        list_of_filled_datetime = listOfFillDatetime(
            [order.fill_datetime for order in broker_order_list]
        )

        final_fill_datetime = list_of_filled_datetime.final_fill_datetime()
        total_filled_qty = sum(list_of_filled_qty)
        average_fill_price = list_of_filled_price.average_fill_price()

        result = self.contract_stack.change_fill_quantity_for_order(
            contract_order.order_id,
            total_filled_qty,
            filled_price=average_fill_price,
            fill_datetime=final_fill_datetime,
        )

        # if fill has changed then update positions
        # we do this here, because we can get here eithier from fills process
        # or after an execution
        self.apply_position_change_to_contracts(
            contract_order, total_filled_qty)

        return result