def post_trade_processing(data: dataBlob,
                          broker_order_with_controls: orderWithControls) -> orderWithControls:
    data_broker = dataBroker(data)
    data_broker.cancel_market_data_for_order(broker_order_with_controls.order)

    # update the order one more time
    broker_order_with_controls.update_order()

    # This order will now contain all fills so we set trades==fills
    # so the order is treated as completed
    # FIXME don't think I need to do this
    #broker_order_with_controls.order.change_trade_qty_to_filled_qty()

    return broker_order_with_controls
Beispiel #2
0
def reason_to_switch_to_aggressive(
        broker_order_with_controls: orderWithControls) -> str:
    ticker_object = broker_order_with_controls.ticker

    too_much_time = (broker_order_with_controls.seconds_since_submission() >
                     PASSIVE_TIME_OUT)
    adverse_price = ticker_object.adverse_price_movement_vs_reference()
    adverse_size = adverse_size_issue(ticker_object)

    if too_much_time:
        return ("Time out after %f seconds" %
                broker_order_with_controls.seconds_since_submission())
    elif adverse_price:
        return "Adverse price movement"
    elif adverse_size:
        return ("Imbalance ratio of %f exceeds threshold" %
                ticker_object.latest_imbalance_ratio())

    return no_need_to_switch
Beispiel #3
0
    def manage_live_trade(
            self, broker_order_with_controls: orderWithControls
    ) -> orderWithControls:
        log = broker_order_with_controls.order.log_with_attributes(
            self.data.log)
        data_broker = self.data_broker

        trade_open = True
        log.msg("Managing trade %s with market order" %
                str(broker_order_with_controls.order))
        while trade_open:
            log_message_required = broker_order_with_controls.message_required(
                messaging_frequency_seconds=MESSAGING_FREQUENCY)
            if log_message_required:
                file_log_report_market_order(log, broker_order_with_controls)

            is_order_completed = broker_order_with_controls.completed()
            is_order_timeout = (
                broker_order_with_controls.seconds_since_submission() >
                ORDER_TIME_OUT)
            is_order_cancelled = (
                data_broker.check_order_is_cancelled_given_control_object(
                    broker_order_with_controls))
            if is_order_completed:
                log.msg("Trade completed")
                break

            if is_order_timeout:
                log.msg("Run out of time to execute: cancelling")
                broker_order_with_controls = cancel_order(
                    self.data, broker_order_with_controls)
                break

            if is_order_cancelled:
                log.warn(
                    "Order has been cancelled apparently by broker: not by algo!"
                )
                break

        return broker_order_with_controls
Beispiel #4
0
def file_log_report_limit_order(log, is_aggressive: bool,
                                broker_order_with_controls: orderWithControls):

    if is_aggressive:
        agg_txt = "Aggressive"
    else:
        agg_txt = "Passive"

    limit_price = broker_order_with_controls.order.limit_price
    broker_limit_price = broker_order_with_controls.broker_limit_price()

    ticker_object = broker_order_with_controls.ticker
    current_tick = str(ticker_object.current_tick())

    log_report = "%s execution with limit price desired:%f actual:%f last tick %s" % (
        agg_txt, limit_price, broker_limit_price, current_tick)

    log.msg(log_report)
def check_current_limit_price_at_inside_spread(broker_order_with_controls: orderWithControls) -> float:
    # When we are aggressive we want to remain on the correct side of the
    # spread

    ## We store two types of limit price. The limit price saved in the broker order (current_limit_price),
    ##   and the limit price in the IB control object (broker_limit_price)
    ## The former is changed when we change a limit price, the latter is not, we wait for IB to
    ##    update it and reflect the object
    current_limit_price = broker_order_with_controls.broker_limit_price()

    ticker_object = broker_order_with_controls.ticker
    current_side_price = ticker_object.current_side_price

    if current_limit_price == current_side_price:
        return limit_price_is_at_inside_spread

    # change limit
    new_limit_price = current_side_price

    return new_limit_price
Beispiel #6
0
    def manage_live_trade(
        self, broker_order_with_controls_and_order_id: orderWithControls
    ) -> orderWithControls:

        data = self.data
        log = broker_order_with_controls_and_order_id.order.log_with_attributes(
            data.log)
        data_broker = dataBroker(data)

        trade_open = True
        is_aggressive = False
        log.msg("Managing trade %s with algo 'original-best'" %
                str(broker_order_with_controls_and_order_id.order))

        is_limit_trade = broker_order_with_controls_and_order_id.order.order_type == limit_order_type

        while trade_open:
            if broker_order_with_controls_and_order_id.message_required(
                    messaging_frequency_seconds=MESSAGING_FREQUENCY):
                file_log_report(log, is_aggressive,
                                broker_order_with_controls_and_order_id)

            if is_limit_trade:
                if is_aggressive:
                    ## aggressive keep limit price in line
                    set_aggressive_limit_price(
                        data, broker_order_with_controls_and_order_id)
                else:
                    # passive limit trade
                    reason_to_switch = reason_to_switch_to_aggressive(
                        broker_order_with_controls_and_order_id)
                    need_to_switch = required_to_switch_to_aggressive(
                        reason_to_switch)

                    if need_to_switch:
                        log.msg("Switch to aggressive because %s" %
                                reason_to_switch)
                        is_aggressive = True
            else:
                # market trade nothing to do
                pass

            order_completed = broker_order_with_controls_and_order_id.completed(
            )

            order_timeout = (broker_order_with_controls_and_order_id.
                             seconds_since_submission() > TOTAL_TIME_OUT)

            order_cancelled = data_broker.check_order_is_cancelled_given_control_object(
                broker_order_with_controls_and_order_id)

            if order_completed:
                log.msg("Trade completed")
                break

            if order_timeout:
                log.msg("Run out of time: cancelling")
                broker_order_with_controls_and_order_id = cancel_order(
                    data, broker_order_with_controls_and_order_id)
                break

            if order_cancelled:
                log.warn("Order has been cancelled: not by algo")
                break

        return broker_order_with_controls_and_order_id