Exemplo n.º 1
0
 def sendStatiscOrderEvent(self, signals):
     try:
         for signal in signals:
             signal['status_assets'] = json_escape(signal['status_assets'])
         # 构造事件对象
         TEMP_EVENT = json.loads(
             STATISTIC_ORDER_EVENT.substitute(id=self._engine.getEventID(),
                                              timeStamp=utcnow_timestamp(),
                                              signals=signals))
         event = Event(TEMP_EVENT)
         self._logger.debug(
             "src.core.engine.sender.Sender.sendStatiscOrderEvent: " +
             json.dumps(TEMP_EVENT))
         # 发送事件
         self._engine.sendEvent(event)
         # 返回参数
         return event.id
     except Exception as err:
         errStr = "src.core.engine.sender.Sender.sendStatiscOrderEvent: %s" % EngineException(
             err)
         raise EngineException(errStr)
Exemplo n.º 2
0
 def __process(self, event):
     self.__logger.debug(
         "src.core.engine.engine.EventEngine.__mainProcess.__run.__process: {id=%s, type=%s, priority=%s, timeStamp=%s, args=%s}"
         % (event.id, event.type, event.priority, event.timeStamp,
            event.args))
     try:
         if event.type in self.__handlers:
             for handler in self.__handlers[event.type]:
                 # 开一个进程去异步处理
                 p = Process(
                     target=handler, args=(event, self.__status.delEventStatus))
                 # 运行进程
                 p.start()
                 # 同步抄送至事件运行状态表格里
                 self.__status.addEventStatus(event.id)
                 # 保存到进程池
                 self.__processPool.append((event.id, p.pid))
     except Exception as err:
         errStr = "src.core.engine.engine.EventEngine.__mainProcess.__run.__process: {id=%s, type=%s, priority=%s, timeStamp=%s, args=%s}, exception err=%s" % (
             event.id, event.type, event.priority, event.timeStamp, event.args, EngineException(err))
         raise EngineException(errStr)
Exemplo n.º 3
0
 def register(self):
     self._logger.debug("src.core.engine.register.Register.register")
     try:
         # 注册事件
         self._eventEngine.register(
             self.LISTEN_ACCOUNT_BALANCE_EVENT_TYPE,
             self.LISTEN_ACCOUNT_BALANCE_EVENT_HANDLER)
         self._eventEngine.register(
             self.LISTEN_ACCOUNT_WITHDRAW_EVENT_TYPE,
             self.LISTEN_ACCOUNT_WITHDRAW_EVENT_HANDLER)
         self._eventEngine.register(self.LISTEN_MARKET_KLINE_EVENT_TYPE,
                                    self.LISTEN_MARKET_KLINE_EVENT_HANDLER)
         self._eventEngine.register(self.LISTEN_MARKET_TICKER_EVENT_TYPE,
                                    self.LISTEN_MARKET_TICKER_EVENT_HANDLER)
         self._eventEngine.register(self.LISTEN_MARKET_DEPTH_EVENT_TYPE,
                                    self.LISTEN_MARKET_DEPTH_EVENT_HANDLER)
         self._eventEngine.register(self.JUDGE_MARKET_DEPTH_EVENT_TYPE,
                                    self.JUDGE_MARKET_DEPTH_EVENT_HANDLER)
         self._eventEngine.register(self.JUDGE_MARKET_KLINE_EVENT_TYPE,
                                    self.JUDGE_MARKET_KLINE_EVENT_HANDLER)
         self._eventEngine.register(self.JUDGE_MARKET_TICKER_EVENT_TYPE,
                                    self.JUDGE_MARKET_TICKER_EVENT_HANDLER)
         self._eventEngine.register(
             self.BACKTEST_HISTORY_CREAT_EVENT_TYPE,
             self.BACKTEST_HISTORY_CREAT_EVENT_HANDLER)
         self._eventEngine.register(self.ORDER_HISTORY_SYNC_EVENT_TYPE,
                                    self.ORDER_HISTORY_SYNC_EVENT_HANDLER)
         self._eventEngine.register(self.ORDER_HISTORY_CREAT_EVENT_TYPE,
                                    self.ORDER_HISTORY_CREAT_EVENT_HANDLER)
         self._eventEngine.register(self.STATISTIC_JUDGE_EVENT_TYPE,
                                    self.STATISTIC_JUDGE_EVENT_HANDLER)
         self._eventEngine.register(self.STATISTIC_BACKTEST_EVENT_TYPE,
                                    self.STATISTIC_BACKTEST_EVENT_HANDLER)
         self._eventEngine.register(self.STATISTIC_ORDER_EVENT_TYPE,
                                    self.STATISTIC_ORDER_EVENT_HANDLER)
     except Exception as err:
         errStr = "src.core.engine.register.Register.register: %s" % EngineException(
             err)
         self._logger.error(errStr)
         raise EngineException(err)
Exemplo n.º 4
0
 def delEventStatus(self, id):
     self._logger.debug(
         "src.core.engine.status.Status.delEventStatus: {id=%s}" % id)
     try:
         if id in self._activeStatus:
             self._activeStatus.remove(id)
         if id not in self._doneStatus:
             if len(self._doneStatus) < self._cachesize:
                 self._doneStatus.append(id)
             else:
                 self._doneStatus.pop(0)
                 self._doneStatus.append(id)
     except Exception as err:
         raise (EngineException(err))
Exemplo n.º 5
0
 def register(self, type, handler):
     self.__logger.debug(
         "src.core.engine.engine.EventEngine.register: {type:%s, handler:%s}"
         % (type, handler))
     # 尝试获取该事件类型对应的处理函数列表,若无则创建
     try:
         try:
             handlerList = self.__handlers[type]
         except KeyError:
             handlerList = []
         # 若要注册的处理器不在该事件的处理器列表中,则注册该事件
         if handler not in handlerList:
             handlerList.append(handler)
         self.__handlers[type] = handlerList
     except (KeyError, Exception) as err:
         errStr = "src.core.engine.engine.EventEngine.register: {type:%s, handler:%s}, exception err=%s" % (type, handler, EngineException(
             err))
         raise EngineException(errStr)
Exemplo n.º 6
0
 def sendListenAccountBalanceEvent(self, exchange):
     try:
         # 构造事件对象
         TEMP_EVENT = json.loads(
             LISTEN_ACCOUNT_BALANCE_EVENT.substitute(
                 id=self._engine.getEventID(),
                 timeStamp=utcnow_timestamp(),
                 exchange=exchange))
         event = Event(TEMP_EVENT)
         self._logger.debug(
             "src.core.engine.sender.Sender.sendListenAccountBalanceEvent: "
             + json.dumps(TEMP_EVENT))
         # 发送事件
         self._engine.sendEvent(event)
         # 返回参数
         return event.id
     except Exception as err:
         errStr = "src.core.engine.sender.Sender.sendListenAccountBalanceEvent: %s" % EngineException(
             err)
         raise EngineException(errStr)
Exemplo n.º 7
0
 def sendJudgeMarketTickerEvent(self, exchange, types):
     try:
         # 构造事件对象
         TEMP_EVENT = json.loads(
             JUDGE_MARKET_TICKER_EVENT.substitute(
                 id=self._engine.getEventID(),
                 timeStamp=utcnow_timestamp(),
                 exchange=exchange,
                 types=types))
         event = Event(TEMP_EVENT)
         self._logger.debug(
             "src.core.engine.sender.Sender.sendJudgeMarketTickerEvent: " +
             json.dumps(TEMP_EVENT))
         # 发送事件
         self._engine.sendEvent(event)
         # 返回参数
         return event.id
     except Exception as err:
         errStr = "src.core.engine.sender.Sender.sendJudgeMarketTickerEvent: %s" % EngineException(
             err)
         raise EngineException(errStr)
Exemplo n.º 8
0
 def sendOrderHistoryCreatEvent(self, exchange, signals, timeout):
     try:
         # 构造事件对象
         TEMP_EVENT = json.loads(
             BACKTEST_HISTORY_CREAT_EVENT.substitute(
                 id=self._engine.getEventID(),
                 timeStamp=utcnow_timestamp(),
                 exchange=exchange,
                 signals=signals,
                 timeout=timeout))
         event = Event(TEMP_EVENT)
         self._logger.debug(
             "src.core.engine.sender.Sender.sendOrderHistoryCreatEvent: " +
             json.dumps(TEMP_EVENT))
         # 发送事件
         self._engine.sendEvent(event)
         # 返回参数
         return event.id
     except Exception as err:
         errStr = "src.core.engine.sender.Sender.sendOrderHistoryCreatEvent: %s" % EngineException(
             err)
         raise EngineException(errStr)
Exemplo n.º 9
0
 def sendListenMarketTickerEvent(self, exchange, fSymbol, tSymbol,
                                 aggDepth):
     try:
         # 构造事件对象
         TEMP_EVENT = json.loads(
             LISTEN_MARKET_TICKER_EVENT.substitute(
                 id=self._engine.getEventID(),
                 timeStamp=utcnow_timestamp(),
                 exchange=exchange,
                 fSymbol=fSymbol,
                 tSymbol=tSymbol,
                 aggDepth=aggDepth))
         event = Event(TEMP_EVENT)
         self._logger.debug(
             "src.core.engine.sender.Sender.sendListenMarketTickerEvent: " +
             json.dumps(TEMP_EVENT))
         # 发送事件
         self._engine.sendEvent(event)
         # 返回参数
         return event.id
     except Exception as err:
         errStr = "src.core.engine.sender.Sender.sendListenMarketTickerEvent: %s" % EngineException(
             err)
         raise EngineException(errStr)
Exemplo n.º 10
0
    def __run(self):
        self.__logger.debug(
            "src.core.engine.engine.EventEngine.__mainProcess.__run")
        try:
            while self.__active.value:
                # 执行 Epoch
                time.sleep(self.__epoch)
                # 控制最大进程数量
                if self.getActiveEventNum() > int(self.__maxProcess):
                    self.__logger.warn(
                        "src.core.engine.engine.EventEngine.__mainProcess.__run.__eventQueue: Too Many"
                    )
                else:
                    # 按优先级 获取队列中的事件 超时1秒
                    event = None
                    if not self.__highEventQueue.empty():
                        self.__logger.debug(
                            "src.core.engine.engine.EventEngine.__mainProcess.__run.__highEventQueue"
                        )
                        event = self.__highEventQueue.get(block=False)
                        while utcnow_timestamp(
                        ) - event.timeStamp > HIGH_PRIORITY_EVENT_TIMEOUT:
                            self.__status.delEventStatus(event)
                            if not self.__highEventQueue.empty():
                                self.__logger.error(
                                    "src.core.engine.engine.EventEngine.__mainProcess.__run.__highEventQueue TIMEOUT: {id=%s, type=%s, priority=%s, timeStamp=%s, args=%s}"
                                    % (event.id, event.type, event.priority,
                                       event.timeStamp, event.args))
                                event = self.__highEventQueue.get(block=False)
                            else:
                                event = None
                                break

                    if not self.__mediumEventQueue.empty() and event == None:
                        self.__logger.debug(
                            "src.core.engine.engine.EventEngine.__mainProcess.__run.__mediumEventQueue"
                        )
                        event = self.__mediumEventQueue.get(block=False)
                        while utcnow_timestamp(
                        ) - event.timeStamp > MEDIUM_PRIORITY_EVENT_TIMEOUT:
                            self.__status.delEventStatus(event)
                            if not self.__mediumEventQueue.empty():
                                self.__logger.error(
                                    "src.core.engine.engine.EventEngine.__mainProcess.__run.__mediumEventQueue TIMEOUT: {id=%s, type=%s, priority=%s, timeStamp=%s, args=%s}"
                                    % (event.id, event.type, event.priority,
                                       event.timeStamp, event.args))
                                event = self.__mediumEventQueue.get(
                                    block=False)
                            else:
                                event = None
                                break

                    if not self.__lowEnventQueue.empty() and event == None:
                        self.__logger.debug(
                            "src.core.engine.engine.EventEngine.__mainProcess.__run.__lowEnventQueue"
                        )
                        event = self.__lowEnventQueue.get(block=False)
                        while utcnow_timestamp(
                        ) - event.timeStamp > LOW_PRIORITY_EVENT_TIMEOUT:
                            self.__status.delEventStatus(event)
                            if not self.__lowEnventQueue.empty():
                                self.__logger.error(
                                    "src.core.engine.engine.EventEngine.__mainProcess.__run.__lowEnventQueue TIMEOUT: {id=%s, type=%s, priority=%s, timeStamp=%s, args=%s}"
                                    % (event.id, event.type, event.priority,
                                       event.timeStamp, event.args))
                                event = self.__lowEnventQueue.get(block=False)
                            else:
                                event = None
                                break

                    # 事件队列非空
                    if not event == None:
                        # 执行事件
                        self.__logger.debug(
                            "src.core.engine.engine.EventEngine.__mainProcess.__run.__eventQueue: {id=%s, type=%s, priority=%s, timeStamp=%s, args=%s}"
                            % (event.id, event.type, event.priority,
                               event.timeStamp, event.args))
                        self.__process(event)
                    else:
                        self.__logger.debug(
                            "src.core.engine.engine.EventEngine.__mainProcess.__run.__eventQueue: empty"
                        )
                    # 定期清理进程池
                    if len(self.__processPool) > self.__maxProcess:
                        for (_id, _pid) in self.__processPool:
                            if not psutil.pid_exists(_pid):
                                self.__processPool.remove((_id, _pid))
            # break out while
            # 终止所有事件处理进程
            for (_id, _pid) in self.__processPool:
                if psutil.pid_exists(_pid):
                    _p = psutil.Process(_pid)
                    _p.terminate()
                    self.__processPool.remove((_id, _pid))
        except Exception as err:
            errStr = "src.core.engine.engine.EventEngine.__mainProcess.__run: exception err=%s" % EngineException(
                err)
            raise EngineException(errStr)
Exemplo n.º 11
0
 def getDoneStatusTable(self):
     self._logger.debug("src.core.engine.status.Status.getDoneStatusTable")
     try:
         return self._doneStatus
     except Exception as err:
         raise (EngineException(err))