def main(): """主程序入口""" le = LogEngine() le.setLogLevel(le.LEVEL_INFO) le.addConsoleHandler() le.addFileHandler() le.info(u'启动JAQS服务进程') ee = EventEngine() le.info(u'事件引擎创建成功') me = MainEngine(ee) me.addGateway(ctpGateway) me.addApp(jaqsService) le.info(u'主引擎创建成功') ee.register(EVENT_LOG, le.processLogEvent) ee.register(EVENT_JS_LOG, le.processLogEvent) le.info(u'注册日志事件监听') me.connect('CTP') le.info(u'连接CTP接口') while True: sleep(1)
class EventDrivenStrategy(Strategy, Subscriber): def __init__(self): Strategy.__init__(self) self.pm = PortfolioManager() self.pm.strategy = self # TODO remove self.eventEngine = EventEngine() self.eventEngine.register(eventType.EVENT_TIMER, self.on_cycle) self.eventEngine.register(eventType.EVENT_MD_QUOTE, self.on_quote) self.eventEngine.register(eventType.EVENT_TRADE_IND, self.pm.on_trade_ind) self.eventEngine.register(eventType.EVENT_ORDERSTATUS_IND, self.pm.on_order_status) @abstractmethod def on_new_day(self, trade_date): pass @abstractmethod def on_quote(self, quote): pass @abstractmethod def on_cycle(self): pass def initialize(self, runmode): if runmode == common.RUN_MODE.REALTIME: self.subscribe_events() def subscribe_events(self): universe = self.context.universe data_server = self.context.dataserver for i in xrange(len(universe)): self.subscribe(data_server, universe[i]) def subscribe(self, publisher, topic): publisher.add_subscriber(self, topic) def start(self): self.eventEngine.start(False) def stop(self): self.eventEngine.stop() def register_event(self, event): self.eventEngine.put(event)