Ejemplo n.º 1
0
    def order(self, asset, amount, limit_price, stop_price, style):
        is_buy = (amount > 0)
        zp_order = ZPOrder(
            dt=pd.to_datetime('now', utc=True),
            asset=asset,
            amount=amount,
            stop=style.get_stop_price(is_buy),
            limit=style.get_limit_price(is_buy))

        contract = Contract()
        contract.m_symbol = str(asset.symbol)
        contract.m_currency = self.currency
        contract.m_exchange = 'SMART'
        contract.m_secType = 'STK'

        order = Order()
        order.m_totalQuantity = int(fabs(amount))
        order.m_action = "BUY" if amount > 0 else "SELL"

        order.m_lmtPrice = 0
        order.m_auxPrice = 0

        if isinstance(style, MarketOrder):
            order.m_orderType = "MKT"
        elif isinstance(style, LimitOrder):
            order.m_orderType = "LMT"
            order.m_lmtPrice = limit_price
        elif isinstance(style, StopOrder):
            order.m_orderType = "STP"
            order.m_auxPrice = stop_price
        elif isinstance(style, StopLimitOrder):
            order.m_orderType = "STP LMT"
            order.m_auxPrice = stop_price
            order.m_lmtPrice = limit_price

        order.m_tif = "DAY"

        ib_order_id = self._tws.next_order_id
        zp_order.broker_order_id = ib_order_id
        self.orders[zp_order.id] = zp_order

        self._tws.placeOrder(ib_order_id, contract, order)

        return zp_order.id
    def tdx_order_to_zipline_order(self, order):
        """
        status
        0	已报
        1	部分成交
        2	全部成交
        3	部分撤单
        4	全部撤单
        5	交易所拒单
        6	柜台未接受

        :param order:
        :return:
        """
        if order.status == "3" or '4' == order.status:
            zp_status = ZP_ORDER_STATUS.CANCELLED
        elif order.status == "0":
            zp_status = ZP_ORDER_STATUS.OPEN
        elif order.status == "1":
            zp_status = ZP_ORDER_STATUS.FILLED
        elif order.status == "2":
            zp_status = ZP_ORDER_STATUS.HELD
        elif order.status == "5" or order.status == "6":
            zp_status = ZP_ORDER_STATUS.REJECTED


        zp_order_id = self._tdx_to_zp_order_id(order.order_id)

        od = ZPOrder(
            dt=order.dt,
            asset=symbol(order.symbol),
            amount=order.amount,
            filled=order.filled,
            stop=None,
            limit=order.price,  # TODO 市价单和限价单
            id=zp_order_id,
        )
        od.broker_order_id = order.order_id
        od.status = zp_status

        return od
Ejemplo n.º 3
0
    def tdx_order_to_zipline_order(self, order):
        if order.status is not None and 'CANCEL' == order.status:
            zp_status = ZP_ORDER_STATUS.CANCELLED
        elif order.filled == 0:
            zp_status = ZP_ORDER_STATUS.OPEN
        else:
            zp_status = ZP_ORDER_STATUS.FILLED

        zp_order_id = self._tdx_to_zp_order_id(order.order_id)

        od = ZPOrder(
            dt=pd.to_datetime(order.dt),
            asset=symbol(order.symbol),
            amount=order.amount,
            filled=order.filled,
            stop=None,
            limit=order.price,  # TODO 市价单和限价单
            id=zp_order_id,
        )
        od.broker_order_id = order.order_id
        od.status = zp_status

        return od
Ejemplo n.º 4
0
    def tdx_order_to_zipline_order(self, order):
        if order.status == '已撤':
            zp_status = ZP_ORDER_STATUS.CANCELLED
        elif order.filled == 0:
            zp_status = ZP_ORDER_STATUS.OPEN
        else:
            zp_status = ZP_ORDER_STATUS.FILLED

        zp_order_id = self._tdx_to_zp_order_id(order.order_id)

        od = ZPOrder(
            dt=pd.to_datetime(order.dt),
            asset=symbol(order.symbol),
            amount=order.amount,
            filled=order.filled,
            stop=None,
            limit=order.price,  # TODO 市价单和限价单
            id=zp_order_id,
        )
        od.broker_order_id = order.order_id
        od.status = zp_status

        return od