class Marketing(object): def __init__(self, reqAddress, pubAddress): self.reqAddr = reqAddress self.pubAddr = pubAddress self.mainEngine = None self.isRunning = False def start(self, gatewayList): """ :param gatewayList: such as ['CHBTC', 'HUOBI'] :type gatewayList: :return: :rtype: """ if self.isRunning: return self.isRunning = True # 创建主引擎对象 self.mainEngine = MainEngine(enableCtaEngine=False, enableDrEngine=True, enableRmEngine=False) # 启动server server = VtServer(self.reqAddr, self.pubAddr, self.mainEngine) server.start() self._connectGateway(gatewayList) printLog('-' * 50) printLog(u'marketing服务器已启动') # 进入主循环 self.isRunning = True self.onRunging() server.stopServer() self.mainEngine.exit() def onRunging(self): cnt = 0 while self.isRunning: if cnt >= 10: cnt = 0 printLog(u'请输入Ctrl-C来关闭服务器') cnt += 1 sleep(1) def _connectGateway(self, gatewayList): for gateway in gatewayList: # Marketing不需要对账户信息和交易信息进行查询 self.mainEngine.gatewayDict[gateway].setQryEnabled(False) self.mainEngine.connect(gateway) printLog('Gateway(%s) connect finished.' % (gateway, ))
class VtServer(RpcServer): """vn.trader服务器""" #---------------------------------------------------------------------- def __init__(self, repAddress, pubAddress): """Constructor""" super(VtServer, self).__init__(repAddress, pubAddress) self.usePickle() # 创建主引擎对象 self.engine = MainEngine() # 注册主引擎的方法到服务器的RPC函数 self.register(self.engine.connect) self.register(self.engine.subscribe) self.register(self.engine.sendOrder) self.register(self.engine.cancelOrder) self.register(self.engine.qryAccount) self.register(self.engine.qryPosition) self.register(self.engine.exit) self.register(self.engine.writeLog) self.register(self.engine.dbConnect) self.register(self.engine.dbInsert) self.register(self.engine.dbQuery) self.register(self.engine.dbUpdate) self.register(self.engine.getContract) self.register(self.engine.getAllContracts) self.register(self.engine.getOrder) self.register(self.engine.getAllWorkingOrders) self.register(self.engine.getAllGatewayNames) self.register(self.engine.getGateway4sysMenu) # 注册事件引擎发送的事件处理监听 self.engine.eventEngine.registerGeneralHandler(self.eventHandler) #---------------------------------------------------------------------- def eventHandler(self, event): """事件处理""" self.publish(event.type_, event) #---------------------------------------------------------------------- def stopServer(self): """停止服务器""" # 关闭引擎 self.engine.exit() # 停止服务器线程 self.stop()
class App(object): def __init__(self): self.mainEngine = MainEngine() self.ctaEngine = self.mainEngine.ctaEngine self.drEngine = self.mainEngine.drEngine self.gatewayConnectedDict = {} self.strategyInited = False self.strategyStarted = False self.mainEngine.writeLog("MainEngine Started.") #self.connectGateway('SHZD') # self.connectGateway('CTP') #self.connectGateway('OANDA') def quit(self): try: self.mainEngine.exit() except Exception, e: pass else: