From 64c63c9c5dedc1ff4a772d6af35227b3fe16ea25 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 21 Jan 2018 16:07:48 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E5=A2=9E=E5=8A=A0=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E7=9A=84=E8=B5=84=E6=BA=90=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/WebTrader/run.py | 60 +++++++++++++++++++++-------- vnpy/trader/vtEngine.py | 81 ++++++++++++++++++++++++++++++++++----- 2 files changed, 116 insertions(+), 25 deletions(-) diff --git a/examples/WebTrader/run.py b/examples/WebTrader/run.py index 6ff8b476..36ecc4a7 100644 --- a/examples/WebTrader/run.py +++ b/examples/WebTrader/run.py @@ -35,7 +35,7 @@ api = Api(app) # 创建资源 ######################################################################## -class Gateways(Resource): +class Gateway(Resource): """接口""" #---------------------------------------------------------------------- @@ -60,7 +60,7 @@ class Gateways(Resource): ######################################################################## -class Orders(Resource): +class Order(Resource): """委托""" #---------------------------------------------------------------------- @@ -89,7 +89,7 @@ class Orders(Resource): ######################################################################## -class Trades(Resource): +class Trade(Resource): """成交""" #---------------------------------------------------------------------- @@ -101,44 +101,74 @@ class Trades(Resource): ######################################################################## -class Accounts(Resource): +class Account(Resource): """账户""" #---------------------------------------------------------------------- def get(self): """查询""" - pass + data = me.getAllAccounts() + l = {o.__dict__ for o in data} + return l ######################################################################## -class Positions(Resource): +class Position(Resource): """持仓""" #---------------------------------------------------------------------- def get(self): """查询""" - pass + data = me.getAllPositions() + l = {o.__dict__ for o in data} + return l ######################################################################## -class Contracts(Resource): +class Contract(Resource): """合约""" #---------------------------------------------------------------------- def get(self): """查询""" - pass + data = me.getAllContracts() + l = {o.__dict__ for o in data} + return l +######################################################################## +class Log(Resource): + """日志""" + + #---------------------------------------------------------------------- + def get(self): + """查询""" + data = me.getLog() + l = {o.__dict__ for o in data} + return l + + +######################################################################## +class Error(Resource): + """错误""" + + #---------------------------------------------------------------------- + def get(self): + """查询""" + data = me.getError() + l = {o.__dict__ for o in data} + return l # 注册资源 -api.add_resource(Gateways, '/gateways') -api.add_resource(Orders, '/orders') -api.add_resource(Trades, '/trades') -api.add_resource(Accounts, '/accounts') -api.add_resource(Positions, '/positions') -api.add_resource(Contracts, '/contracts') +api.add_resource(Gateway, '/gateway') +api.add_resource(Order, '/order') +api.add_resource(Trade, '/trades') +api.add_resource(Account, '/account') +api.add_resource(Position, '/position') +api.add_resource(Contract, '/contract') +api.add_resource(Log, '/log') +api.add_resource(Error, '/error') diff --git a/vnpy/trader/vtEngine.py b/vnpy/trader/vtEngine.py index 385d9b3d..2681e0c7 100644 --- a/vnpy/trader/vtEngine.py +++ b/vnpy/trader/vtEngine.py @@ -299,6 +299,16 @@ class MainEngine(object): """查询所有成交""" return self.dataEngine.getAllTrades() + #---------------------------------------------------------------------- + def getAllAccounts(self): + """查询所有账户""" + return self.dataEngine.getAllAccounts() + + #---------------------------------------------------------------------- + def getAllPositions(self): + """查询所有持仓""" + return self.dataEngine.getAllPositions() + #---------------------------------------------------------------------- def getAllPositionDetails(self): """查询本地持仓缓存细节""" @@ -360,6 +370,16 @@ class MainEngine(object): """转换委托请求""" return self.dataEngine.convertOrderReq(req) + #---------------------------------------------------------------------- + def getLog(self): + """查询日志""" + return self.dataEngine.getLog() + + #---------------------------------------------------------------------- + def getError(self): + """查询错误""" + return self.dataEngine.getError() + ######################################################################## class DataEngine(object): @@ -374,17 +394,15 @@ class DataEngine(object): """Constructor""" self.eventEngine = eventEngine - # 保存合约详细信息的字典 + # 保存数据的字典和列表 self.contractDict = {} - - # 保存委托数据的字典 self.orderDict = {} - - # 保存成交数据的字典 + self.workingOrderDict = {} # 可撤销委托 self.tradeDict = {} - - # 保存活动委托数据的字典(即可撤销) - self.workingOrderDict = {} + self.accountDict = {} + self.positionDict= {} + self.logList = [] + self.errorList = [] # 持仓细节相关 self.detailDict = {} # vtSymbol:PositionDetail @@ -403,6 +421,9 @@ class DataEngine(object): self.eventEngine.register(EVENT_ORDER, self.processOrderEvent) self.eventEngine.register(EVENT_TRADE, self.processTradeEvent) self.eventEngine.register(EVENT_POSITION, self.processPositionEvent) + self.eventEngine.register(EVENT_ACCOUNT, self.processAccountEvent) + self.eventEngine.register(EVENT_LOG, self.processLogEvent) + self.eventEngine.register(EVENT_ERROR, self.processErrorEvent) #---------------------------------------------------------------------- def processContractEvent(self, event): @@ -442,11 +463,31 @@ class DataEngine(object): def processPositionEvent(self, event): """处理持仓事件""" pos = event.dict_['data'] + + self.positionDict[pos.vtPositionName] = pos # 更新到持仓细节中 detail = self.getPositionDetail(pos.vtSymbol) detail.updatePosition(pos) + #---------------------------------------------------------------------- + def processAccountEvent(self, event): + """处理账户事件""" + account = event.dict_['data'] + self.accountDict[account.vtAccountID] = account + + #---------------------------------------------------------------------- + def processLogEvent(self, event): + """处理日志事件""" + log = event.dict_['data'] + self.logList.append(log) + + #---------------------------------------------------------------------- + def processErrorEvent(self, event): + """处理错误事件""" + error = event.dict_['data'] + self.errorList.append(error) + #---------------------------------------------------------------------- def getContract(self, vtSymbol): """查询合约对象""" @@ -498,7 +539,17 @@ class DataEngine(object): #---------------------------------------------------------------------- def getAllTrades(self): """获取所有成交""" - return self.tradeDict.values() + return self.tradeDict.values() + + #---------------------------------------------------------------------- + def getAllPositions(self): + """获取所有持仓""" + return self.positionDict.values() + + #---------------------------------------------------------------------- + def getAllAccounts(self): + """获取所有资金""" + return self.accountDict.values() #---------------------------------------------------------------------- def getPositionDetail(self, vtSymbol): @@ -549,6 +600,16 @@ class DataEngine(object): else: return detail.convertOrderReq(req) + #---------------------------------------------------------------------- + def getLog(self): + """获取日志""" + return self.logList + + #---------------------------------------------------------------------- + def getError(self): + """获取错误""" + return self.errorList + ######################################################################## class LogEngine(object): @@ -651,7 +712,7 @@ class LogEngine(object): function = self.levelFunctionDict[log.logLevel] # 获取日志级别对应的处理函数 msg = '\t'.join([log.gatewayName, log.logContent]) function(msg) - + ######################################################################## class PositionDetail(object):