Esempio n. 1
0
    def on_match(self):
        """交易撮合"""
        self.write_log("{}:交易市场已开启".format(self.market_name))
        try:
            # 行情连接
            self.hq_client.connect_api()

            # 加载数据
            self.load_data()

            while self._active:
                # 交易时间检验
                if not self.time_verification():
                    continue

                if not self.orders_book:
                    continue

                # 复制交易簿
                orders = copy.copy(self.orders_book)
                for order_id, order in orders.items():
                    sleep(1)
                    # 订单撮合
                    if self.on_orders_match(order):
                        del self.orders_book[order_id]

        except Exception as e:
            event = Event(EVENT_ERROR, traceback.format_exc())
            self.event_engine.put(event)
Esempio n. 2
0
 def write_log(self, msg: str, level: int = logging.INFO):
     """"""
     log = LogData(
         log_content=msg,
         log_level=level
     )
     event = Event(EVENT_LOG, log)
     self.event_engine.put(event)
Esempio n. 3
0
    def on_match(self, db):
        """交易撮合"""
        self.db = db
        self.write_log("{}:交易市场已开启".format(self.market_name))

        try:
            if self.match_mode == EngineMode.REALTIME.value:
                self.on_realtime_match()
            else:
                self.on_simulation_match()

        except Exception as e:
            event = Event(EVENT_ERROR, traceback.format_exc())
            self.event_engine.put(event)
Esempio n. 4
0
    def on_close(self):
        """模拟交易市场关闭"""
        # 关闭市场撮合
        self._active = False

        # 模拟交易结束,拒绝所有未成交的订单
        self.on_refused_all()

        # 清算
        self.liquidation()

        # 关闭行情接口
        self.hq_client.close()

        # 推送关闭事件
        event = Event(EVENT_MARKET_CLOSE, self.market_name)
        self.event_engine.put(event)
Esempio n. 5
0
    def on_match(self):
        """交易撮合"""
        self.write_log("{}:交易市场已开启".format(self.market_name))

        try:
            while self._active:
                if self.orders_queue.empty():
                    continue

                order = self.orders_queue.get(block=True)

                # 订单成交
                # 回测使用委托价格作为成交价格
                order.trade_price = order.order_price
                self.on_order_deal(order)

        except Exception as e:
            event = Event(EVENT_ERROR, traceback.format_exc())
            self.event_engine.put(event)
Esempio n. 6
0
    def on_close(self):
        """模拟交易市场关闭"""
        # 阻止接收新订单
        SETTINGS["MARKET_NAME"] = ""

        # 关闭市场撮合
        self._active = False

        # 模拟交易结束,拒绝所有未成交的订单
        self.on_orders_book_rejected_all()

        # 清算
        self.liquidation()

        # 关闭行情接口
        self.hq_client.close()

        # 推送关闭事件
        event = Event(EVENT_MARKET_CLOSE, self.market_name)
        self.event_engine.put(event)
Esempio n. 7
0
 def __make_event(self, event_name, data):
     """制造事件"""
     if self.__pst_active:
         new_data = copy.deepcopy(data)
         event = Event(event_name, new_data)
         self.event_engine.put(event)