def placeOrderWrapper(self, contract, order, ibpyRequest):
        self._log.debug(
            __name__ + '::placeOrderWrapper: contract=%s order=%s' %
            (print_IBCpp_contract(contract), print_IBCpp_order(order)))

        if isinstance(order.orderId, int):
            int_orderId = order.orderId
        else:
            int_orderId = self.use_next_id()
            order.orderId = int_orderId

        # Set for ending flat.
        # Otherwise, the following line in broker_client_factory::CallBacks::orderStatus will not be able to find a reqId
        # reqId = self.activeRequests.find_reqId_by_int_orderId(int_orderId)
        ibpyRequest.param['int_orderId'] = int_orderId

        ibpyOrderId = self._idConverter.fromIBtoBroker(int_orderId)

        # Register ibpyOrderId in SingleTrader so that it can search accountCode by incoming int_orderId
        self._singleTrader.set_from_send_req_to_server(self.name,
                                                       order.account,
                                                       ibpyOrderId)

        self.orderToBeProcessed[int_orderId] = (contract, order)
        self.simulateOpenOrder(
            int_orderId, contract, order, IBCpp.OrderState(),
            from_contract_to_security(contract).full_print())  # IBCpp function
        self.simulateOrderStatus(int_orderId, 'Submitted', 0,
                                 order.totalQuantity, 0.0, 0, 0, 0, 0,
                                 '')  # IBCpp function
        self.simulate_process_order(self.get_datetime())
Пример #2
0
    def placeOrderWrapper(self, contract, order, ibpyRequest):
        self._validate_contract(contract, 'placeOrderWrapper')
        self._log.info('Place Order to %s security=%s order=%s' % (self.name, print_IBCpp_contract(contract), print_IBCpp_order(order)))

        tdOrder = OrderConverter().fromIBtoTD(contract, order)
        try:
            ibpyOrderId = self._tdClient.place_orders(order.account, tdOrder)
        except RuntimeError as e:
            if 'buying power' in str(e):
                raise NotEnoughFund()
            else:
                raise RuntimeError(e)
        if ibpyOrderId is None:
            raise RuntimeError('Place order to TD failed but there is not RuntimeError.')
        self._log.info('Order was placed to %s successfully. ibpyOrderId=%s' % (self.name, ibpyOrderId))

        # Register int_orderId in _idConverter so that brokerClient::CallBack::orderStatus knows how to handle int_orderId
        int_orderId = self._idConverter.fromBrokerToIB(ibpyOrderId)
        self._idConverter.setRelationship(int_orderId, ibpyOrderId)

        # Set for ending flat.
        # Otherwise, the following line in broker_client_factory::CallBacks::orderStatus will not be able to find a reqId
        # reqId = self.activeRequests.find_reqId_by_int_orderId(int_orderId)
        ibpyRequest.param['int_orderId'] = int_orderId

        # Register ibpyOrderId in SingleTrader so that it can search accountCode by incoming int_orderId
        self._singleTrader.set_from_send_req_to_server(self.name, order.account, ibpyOrderId)

        # IBCpp function
        order.orderId = int_orderId
        self.simulateOpenOrder(int_orderId, contract, order, IBCpp.OrderState(),
                               from_contract_to_security(contract).full_print())  # IBCpp function
        # IBCpp function, this is the ending flag for PlaceOrder
        self.simulateOrderStatus(int_orderId, 'Submitted', 0, order.totalQuantity, 0.0, 0, 0, 0, 0, '')
Пример #3
0
def _fromTDChildOrdertoIBOpenOrder(tdOrder, idConverter):
    originalTdOrderId = str(tdOrder['orderId'])
    int_orderId = idConverter.fromBrokerToIB(originalTdOrderId)
    security = _fromTDInstrumentToIBridgePySecurity(tdOrder['orderLegCollection'][0]['instrument'])
    contract = from_security_to_contract(security)
    orderStatus = OrderStatusConverter().fromTDtoIB(str(tdOrder['status']))
    quantity = int(float(tdOrder['quantity']))

    orderState = IBCpp.OrderState()
    orderState.status = orderStatus

    ibOrder = IBCpp.Order()
    ibOrder.orderId = int_orderId
    ibOrder.account = str(tdOrder['accountId'])
    ibOrder.action = str(tdOrder['orderLegCollection'][0]['instruction'])
    ibOrder.totalQuantity = quantity
    ibOrder.orderType = OrderTypeConverter().fromTDtoIB(str(tdOrder['orderType']))
    ibOrder.tif = OrderTifConverter().fromTDtoIB(str(tdOrder['duration']))
    if ibOrder.orderType == OrderType.LMT:
        ibOrder.lmtPrice = float(str(tdOrder['price']))
    elif ibOrder.orderType == OrderType.STP:
        ibOrder.auxPrice = float(str(tdOrder['stopPrice']))
    elif ibOrder.orderType == OrderType.STP_LMT:
        ibOrder.lmtPrice = float(str(tdOrder['price']))
        ibOrder.auxPrice = float(str(tdOrder['stopPrice']))
    elif ibOrder.orderType == OrderType.MKT:
        pass
    else:
        raise RuntimeError(__name__ + '::reqOneOrderWrapper: EXIT, cannot handle orderType=%s' % (ibOrder.orderType,))
    ibOrder.orderRef = originalTdOrderId
    orderStatus, filledQuantity, remaining, price, whyHeld = OrderConverter().fromTDtoIBOrderStatus(tdOrder)
    return int_orderId, contract, ibOrder, orderState, security, orderStatus, filledQuantity, remaining, price, whyHeld
Пример #4
0
    def placeOrderWrapper(self, contract, order, ibpyRequest):
        instrument = self._robinhoodClient.instruments(contract.symbol)[0]
        action = OrderActionConverter().fromIBtoRB(order.action)
        tif = OrderTifConverter().fromIBtoRB(order.tif)
        quantity = int(order.totalQuantity)
        ans = None
        if order.orderType == OrderType.MKT:
            ans = self._robinhoodClient.place_market_order(
                action, instrument, quantity, tif)
        elif order.orderType == OrderType.LMT:
            ans = self._robinhoodClient.place_limit_order(
                action,
                instrument=instrument,
                limit_price=order.lmtPrice,
                quantity=quantity,
                time_in_force=tif)
        elif order.orderType == OrderType.STP:
            ans = self._robinhoodClient.place_stop_order(
                action,
                instrument=instrument,
                stop_price=order.auxPrice,
                quantity=quantity,
                time_in_force=tif)
        else:
            self._log.error(
                __name__ +
                '::placeOrderWrapper: EXIT, cannot handle orderType=%s' %
                (order.orderType, ))
            exit()
        ibpyOrderId = str(ans['id'])
        self._log.info('Order was placed to %s successfully. ibpyOrderId=%s' %
                       (self.name, ibpyOrderId))

        # Register int_orderId in _idConverter so that brokerClient::CallBack::orderStatus knows how to handle int_orderId
        int_orderId = self._idConverter.fromBrokerToIB(ibpyOrderId)
        self._idConverter.setRelationship(int_orderId, ibpyOrderId)

        # Set for ending flat.
        # Otherwise, the following line in broker_client_factory::CallBacks::orderStatus will not be able to find a reqId
        # reqId = self.activeRequests.find_reqId_by_int_orderId(int_orderId)
        ibpyRequest.param['int_orderId'] = int_orderId

        # Register ibpyOrderId in SingleTrader so that it can search accountCode by incoming int_orderId
        self._singleTrader.set_from_send_req_to_server(self.name,
                                                       order.account,
                                                       ibpyOrderId)

        # IBCpp function
        order.orderId = int_orderId
        self.simulateOpenOrder(
            int_orderId, contract, order, IBCpp.OrderState(),
            from_contract_to_security(contract).full_print())  # IBCpp function
        # IBCpp function, this is the ending flag for PlaceOrder
        self.simulateOrderStatus(int_orderId, 'Submitted', 0,
                                 order.totalQuantity, 0.0, 0, 0, 0, 0, '')
Пример #5
0
    def fromRBtoIBOpenOrder(rbOrder, idConverter, accountCode):
        # IBCpp.Order().orderId must be an integer so that an integer orderId has to be created
        originalTdOrderId = str(rbOrder['id'])
        int_orderId = idConverter.fromBrokerToIB(originalTdOrderId)

        security = symbol(str(rbOrder['symbol']))
        contract = from_security_to_contract(security)
        orderStatus = OrderStatusConverter().fromRBtoIB(str(rbOrder['state']))
        quantity = int(float(rbOrder['quantity']))

        orderState = IBCpp.OrderState()
        orderState.status = orderStatus

        ibOrder = IBCpp.Order()
        ibOrder.orderId = int_orderId
        ibOrder.account = accountCode
        ibOrder.action = OrderActionConverter().fromRBtoIB(
            str(rbOrder['side']))  # _robinhoodClient side : buy, sell
        ibOrder.totalQuantity = quantity
        ibOrder.orderType = OrderTypeConverter().fromRBtoIB(
            str(rbOrder['type']).lower(),
            str(rbOrder['trigger']).lower())
        ibOrder.tif = OrderTifConverter().fromRBtoIB(
            str(rbOrder['time_in_force']))
        if ibOrder.orderType == OrderType.LMT:
            ibOrder.lmtPrice = float(str(rbOrder['price']))
        elif ibOrder.orderType == OrderType.STP:
            ibOrder.auxPrice = float(str(rbOrder['stop_price']))
        elif ibOrder.orderType == OrderType.STP_LMT:
            ibOrder.lmtPrice = float(str(rbOrder['price']))
            ibOrder.auxPrice = float(str(rbOrder['stop_price']))
        elif ibOrder.orderType == OrderType.MKT:
            pass
        else:
            print(__name__ +
                  '::reqOneOrderWrapper: EXIT, cannot handle orderType=%s' %
                  (ibOrder.orderType, ))
            exit()
        return int_orderId, contract, ibOrder, orderState, security