示例#1
0
    def on_order(self, order_dict):
        if not order_dict.is_valid:
            return
        self.on_debug('订单回报: %s' % str(order_dict))
        if self._data_update_date != date.today():
            return

        order = self._cache.get_cached_order(order_dict)

        account = Environment.get_instance().get_account(order.order_book_id)

        if order.status == ORDER_STATUS.PENDING_NEW:
            self._env.event_bus.publish_event(
                RqEvent(EVENT.ORDER_PENDING_NEW, account=account, order=order))
            order.active()
            self._env.event_bus.publish_event(
                RqEvent(EVENT.ORDER_CREATION_PASS,
                        account=account,
                        order=order))
            if order_dict.status == ORDER_STATUS.ACTIVE:
                self._cache.cache_open_order(order)
            elif order_dict.status in [
                    ORDER_STATUS.CANCELLED, ORDER_STATUS.REJECTED
            ]:
                order.mark_rejected('Order was rejected or cancelled.')
                self._env.event_bus.publish_event(
                    RqEvent(EVENT.ORDER_UNSOLICITED_UPDATE,
                            account=account,
                            order=order))
                self._cache.remove_open_order(order)

            elif order_dict.status == ORDER_STATUS.FILLED:
                order._status = order_dict.status
                self._cache.remove_open_order(order)

        elif order.status == ORDER_STATUS.ACTIVE:
            if order_dict.status == ORDER_STATUS.FILLED:
                order._status = order_dict.status
            if order_dict.status == ORDER_STATUS.CANCELLED:
                order.mark_cancelled("%d order has been cancelled." %
                                     order.order_id)
                self._env.event_bus.publish_event(
                    RqEvent(EVENT.ORDER_CANCELLATION_PASS,
                            account=account,
                            order=order))
                self._cache.remove_open_order(order)

        elif order.status == ORDER_STATUS.PENDING_CANCEL:
            if order_dict.status == ORDER_STATUS.CANCELLED:
                order.mark_cancelled("%d order has been cancelled." %
                                     order.order_id)
                self._env.event_bus.publish_event(
                    RqEvent(EVENT.ORDER_CANCELLATION_PASS,
                            account=account,
                            order=order))
                self._cache.remove_open_order(order)
            if order_dict.status == ORDER_STATUS.FILLED:
                order._status = order_dict.status
                self._cache.remove_open_order(order)
示例#2
0
    def on_trade(self, trade_dict):
        self.on_debug('交易回报: %s' % str(trade_dict))
        if self._data_update_date != date.today():
            self._cache.cache_trade(trade_dict)
        else:
            account = Environment.get_instance().get_account(
                trade_dict.order_book_id)

            if trade_dict.trade_id in account._backward_trade_set:
                return

            try:
                order = self.order_objects[trade_dict.order_id]
            except KeyError:
                order = Order.__from_create__(trade_dict.order_book_id,
                                              trade_dict.amount,
                                              trade_dict.side,
                                              trade_dict.style,
                                              trade_dict.position_effect)
            commission = cal_commission(trade_dict, order.position_effect)
            trade = Trade.__from_create__(trade_dict.order_id,
                                          trade_dict.price,
                                          trade_dict.amount,
                                          trade_dict.side,
                                          trade_dict.position_effect,
                                          trade_dict.order_book_id,
                                          trade_id=trade_dict.trade_id,
                                          commission=commission,
                                          frozen_price=trade_dict.price)

            order.fill(trade)
            self._env.event_bus.publish_event(
                RqEvent(EVENT.TRADE, account=account, trade=trade))
示例#3
0
    def on_order(self, event):
        vnpy_order = event.dict_['data']
        system_log.debug("on_order {}", vnpy_order.__dict__)

        if vnpy_order.status == STATUS_UNKNOWN:
            return

        order = self._data_factory.get_order(vnpy_order)

        if self._account_inited:
            account = Environment.get_instance().get_account(
                order.order_book_id)

            order.active()

            self._env.event_bus.publish_event(
                RqEvent(EVENT.ORDER_CREATION_PASS,
                        account=account,
                        order=order))

            self._data_factory.cache_vnpy_order(order.order_id, vnpy_order)

            if vnpy_order.status == STATUS_NOTTRADED or vnpy_order.status == STATUS_PARTTRADED:
                self._data_factory.cache_open_order(order)
            elif vnpy_order.status == STATUS_ALLTRADED:
                self._data_factory.del_open_order(int(vnpy_order.orderID))
            elif vnpy_order.status == STATUS_CANCELLED:
                self._data_factory.del_open_order(int(vnpy_order.orderID))
                if order.status == ORDER_STATUS.PENDING_CANCEL:
                    order.mark_cancelled(
                        "%d order has been cancelled by user." %
                        order.order_id)
                    self._env.event_bus.publish_event(
                        RqEvent(EVENT.ORDER_CANCELLATION_PASS,
                                account=account,
                                order=order))
                else:
                    order.mark_rejected(
                        'Order was rejected or cancelled by vnpy.')
                    self._env.event_bus.publish_event(
                        RqEvent(EVENT.ORDER_UNSOLICITED_UPDATE,
                                account=account,
                                order=order))
示例#4
0
    def cancel_order(self, order):
        account = Environment.get_instance().get_account(order.order_book_id)
        self._env.event_bus.publish_event(
            RqEvent(EVENT.ORDER_PENDING_CANCEL, account=account, order=order))

        cancel_order_req = self._data_factory.make_cancel_order_req(order)
        if cancel_order_req is None:
            system_log.warn('Cannot find VN.PY order in order cache.')

        self.vnpy_gateway.cancelOrder(cancelOrderReq=cancel_order_req)
示例#5
0
    def send_order(self, order):
        account = Environment.get_instance().get_account(order.order_book_id)
        self._env.event_bus.publish_event(
            RqEvent(EVENT.ORDER_PENDING_NEW, account=account, order=order))

        order_req = self._data_factory.make_order_req(order)

        if order_req is None:
            self._env.event_bus.publish_event(
                RqEvent(EVENT.ORDER_PENDING_CANCEL))
            order.mark_cancelled(
                'No contract exists whose order_book_id is %s' %
                order.order_book_id)
            self._env.event_bus.publish_event(
                RqEvent(EVENT.ORDER_CANCELLATION_PASS))

        if order.is_final():
            return

        self.vnpy_gateway.sendOrder(order_req)
        self._data_factory.cache_order(order)
示例#6
0
    def on_trade(self, event):
        vnpy_trade = event.dict_['data']
        system_log.debug("on_trade {}", vnpy_trade.__dict__)

        if not self._account_inited:
            self._data_factory.cache_vnpy_trade_before_init(vnpy_trade)
        else:
            order = self._data_factory.get_order(vnpy_trade)
            trade = make_trade(vnpy_trade, order.order_id)
            account = Environment.get_instance().get_account(
                order.order_book_id)
            self._env.event_bus.publish_event(
                RqEvent(EVENT.TRADE, account=account, trade=trade))
示例#7
0
 def cancel_order(self, order):
     account = Environment.get_instance().get_account(order.order_book_id)
     self._env.event_bus.publish_event(
         RqEvent(EVENT.ORDER_PENDING_CANCEL, account=account, order=order))
     self.td_api.cancelOrder(order)