Example #1
0
    def apply_contract_fill_to_parent_order_multiple_children(
            self, list_of_contract_order_ids: list,
            instrument_order: instrumentOrder):
        ## Three cases for multiple children (as normally one to one)
        # - Inter market spread: we can't handle these yet and we'll break
        # - Leg by leg flat spread eg forced roll order: do nothing since doesn't change instrument positions
        # Distributed roll order eg if we are short -2 front, want to buy 3, will do +2 front +1 next

        log = instrument_order.log_with_attributes(self.log)

        distributed_order = self.check_to_see_if_distributed_instrument_order(
            list_of_contract_order_ids, instrument_order)

        flat_order = instrument_order.is_zero_trade()

        if flat_order:
            # An inter-market flat spread
            # Meaningless to do this
            return None

        elif distributed_order:
            # a distributed order, all orders have the same sign as the
            # instrument order
            self.apply_contract_fill_to_parent_order_distributed_children(
                list_of_contract_order_ids, instrument_order)

        else:
            # A proper spread trade across markets can't do this
            log.critical(
                "Can't handle inter-market spread orders! Instrument order %s %s"
                % (str(instrument_order), str(instrument_order.order_id)))
def single_instrument_child_orders(
        data: dataBlob, instrument_order: instrumentOrder) -> listOfOrders:
    """
    Generate child orders for a single instrument (not rolls)

    :param data: dataBlob. Required as uses roll data to determine appropriate instrument
    :param instrument_order:
    :return: A list of contractOrders to submit to the stack
    """
    # We don't allow zero trades to be spawned
    # Zero trades can enter the instrument stack, where they can potentially
    # modify existing trades
    if instrument_order.is_zero_trade():
        return listOfOrders([])

    # Get required contract(s) depending on roll status
    list_of_child_contract_dates_and_trades = (
        get_required_contract_trade_for_instrument(data, instrument_order))

    list_of_contract_orders = list_of_contract_orders_from_list_of_child_date_and_trade(
        instrument_order, list_of_child_contract_dates_and_trades)

    # Get reference price for relevant contract(s)
    # used for TCA
    # Adjust price if reference contract is different from required contract
    list_of_contract_orders = calculate_reference_prices_for_direct_child_orders(
        data, instrument_order, list_of_contract_orders)

    # Now get the limit prices, where relevant
    # Adjust limit price if limit_contract is different from required contract
    list_of_contract_orders = calculate_limit_prices_for_direct_child_orders(
        data, instrument_order, list_of_contract_orders)

    return list_of_contract_orders
Example #3
0
    def apply_contract_fill_to_parent_order_single_child(
            self, contract_order_id: int, instrument_order: instrumentOrder):
        contract_order = self.contract_stack.get_order_with_id_from_stack(
            contract_order_id)
        log = contract_order.log_with_attributes(self.log)

        fill_for_contract = contract_order.fill
        filled_price = contract_order.filled_price
        fill_datetime = contract_order.fill_datetime
        if len(fill_for_contract) == 1:
            # Not a spread order, trivial
            self.fill_for_instrument_in_database(instrument_order,
                                                 fill_for_contract,
                                                 filled_price, fill_datetime)

        else:
            # Spread order: intra-market
            # Instrument order quantity is either zero (for a roll) or non zero
            # (for a spread)
            if instrument_order.is_zero_trade():
                # A forced leg roll; meaningless to do this
                pass
            else:
                # A spread order that isn't flat
                log.critical(
                    "Can't handle non-flat intra-market spread orders! Instrument order %s %s"
                    % (str(instrument_order), str(instrument_order.order_id)))
    def _put_new_order_on_stack_when_no_existing_order(
        self, new_order: instrumentOrder, allow_zero_orders: bool = False
    ) -> int:
        # no current order for this instrument/strategy

        log = new_order.log_with_attributes(self.log)

        if new_order.is_zero_trade() and not allow_zero_orders:
            log_msg = "Zero orders not allowed"
            log.msg(log_msg)
            raise zeroOrderException(log_msg)

        log.msg("New order %s putting on %s" % (str(new_order), str(self)))

        order_id = self._put_order_on_stack_and_get_order_id(new_order)

        return order_id