From 4e7b64eb39c8cd37189abf75db753564af5ad2d4 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Wed, 20 Dec 2017 08:16:57 +0800 Subject: [PATCH] =?UTF-8?q?[Mod]=E5=A2=9E=E5=8A=A0jaqsService=E7=9A=84quer?= =?UTF-8?q?yAccount=E5=92=8CqueryUniverse=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/JaqsService/CTP_connect.json | 7 +++ examples/JaqsService/JS_setting.json | 4 ++ examples/JaqsService/VT_setting.json | 18 +++++++ examples/JaqsService/run.py | 51 ++++++++++++++++++ vnpy/trader/app/jaqsService/JS_setting.json | 2 +- vnpy/trader/app/jaqsService/jsEngine.py | 57 +++++++++++++++++++-- 6 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 examples/JaqsService/CTP_connect.json create mode 100644 examples/JaqsService/JS_setting.json create mode 100644 examples/JaqsService/VT_setting.json create mode 100644 examples/JaqsService/run.py diff --git a/examples/JaqsService/CTP_connect.json b/examples/JaqsService/CTP_connect.json new file mode 100644 index 00000000..1b4d5a42 --- /dev/null +++ b/examples/JaqsService/CTP_connect.json @@ -0,0 +1,7 @@ +{ + "brokerID": "9999", + "mdAddress": "tcp://180.168.146.187:10011", + "tdAddress": "tcp://180.168.146.187:10001", + "userID": "000300", + "password": "19890624" +} \ No newline at end of file diff --git a/examples/JaqsService/JS_setting.json b/examples/JaqsService/JS_setting.json new file mode 100644 index 00000000..6eb31c5a --- /dev/null +++ b/examples/JaqsService/JS_setting.json @@ -0,0 +1,4 @@ +{ + "host": "127.0.0.1", + "port": 8901 +} \ No newline at end of file diff --git a/examples/JaqsService/VT_setting.json b/examples/JaqsService/VT_setting.json new file mode 100644 index 00000000..a00ff35e --- /dev/null +++ b/examples/JaqsService/VT_setting.json @@ -0,0 +1,18 @@ +{ + "fontFamily": "微软雅黑", + "fontSize": 12, + + "mongoHost": "localhost", + "mongoPort": 27017, + "mongoLogging": true, + + "darkStyle": true, + "language": "chinese", + + "logActive": true, + "logLevel": "debug", + "logConsole": true, + "logFile": true, + + "tdPenalty": ["IF", "IH", "IC"] +} \ No newline at end of file diff --git a/examples/JaqsService/run.py b/examples/JaqsService/run.py new file mode 100644 index 00000000..caedbcbe --- /dev/null +++ b/examples/JaqsService/run.py @@ -0,0 +1,51 @@ +# encoding: UTF-8 + +# 重载sys模块,设置默认字符串编码方式为utf8 +import sys +reload(sys) +sys.setdefaultencoding('utf8') + +# 判断操作系统 +import platform +system = platform.system() + +# vn.trader模块 +from vnpy.event import EventEngine +from vnpy.trader.vtEngine import MainEngine +from vnpy.trader.uiQt import createQApp +from vnpy.trader.uiMainWindow import MainWindow + +# 加载底层接口 +from vnpy.trader.gateway import ctpGateway + +# 加载上层应用 +from vnpy.trader.app import jaqsService + +#---------------------------------------------------------------------- +def main(): + """主程序入口""" + # 创建Qt应用对象 + qApp = createQApp() + + # 创建事件引擎 + ee = EventEngine() + + # 创建主引擎 + me = MainEngine(ee) + + # 添加交易接口 + me.addGateway(ctpGateway) + + # 添加上层应用 + me.addApp(jaqsService) + + # 创建主窗口 + mw = MainWindow(me, ee) + mw.showMaximized() + + # 在主线程中启动Qt事件循环 + sys.exit(qApp.exec_()) + + +if __name__ == '__main__': + main() diff --git a/vnpy/trader/app/jaqsService/JS_setting.json b/vnpy/trader/app/jaqsService/JS_setting.json index f08c2ebb..6eb31c5a 100644 --- a/vnpy/trader/app/jaqsService/JS_setting.json +++ b/vnpy/trader/app/jaqsService/JS_setting.json @@ -1,4 +1,4 @@ { "host": "127.0.0.1", - "port": 88888 + "port": 8901 } \ No newline at end of file diff --git a/vnpy/trader/app/jaqsService/jsEngine.py b/vnpy/trader/app/jaqsService/jsEngine.py index bacab694..e73c9c9d 100644 --- a/vnpy/trader/app/jaqsService/jsEngine.py +++ b/vnpy/trader/app/jaqsService/jsEngine.py @@ -9,7 +9,7 @@ from vnpy.event import Event from vnpy.trader.vtFunction import getJsonPath from vnpy.trader.vtObject import VtLogData, VtOrderReq, VtCancelOrderReq from vnpy.trader.vtConstant import * - +from vnpy.trader.vtEvent import EVENT_ACCOUNT EVENT_JS_LOG = 'eJsLog' @@ -56,6 +56,7 @@ class JsEngine(object): self.server = None # RPC服务器 self.cbDict = {} # 回调函数字典 + self.account = None # 账户对象 # 注册日志事件类型 self.mainEngine.registerLogEvent(EVENT_JS_LOG) @@ -64,6 +65,11 @@ class JsEngine(object): self.initCallback() self.initServer() + #---------------------------------------------------------------------- + def registerEvent(self): + """注册事件监听""" + self.eventEngine.register(EVENT_ACCOUNT, self.processAccountEvent) + #---------------------------------------------------------------------- def initCallback(self): """初始化回调函数映射""" @@ -74,6 +80,8 @@ class JsEngine(object): self.cbDict['oms.query_order'] = self.onQueryOrder self.cbDict['oms.place_order'] = self.onPlaceOrder self.cbDict['oms.cancel_order'] = self.onCancelOrder + self.cbDict['oms.query_account'] = self.onQueryAccount + self.cbDict['oms.query_universe'] = self.onQueryUniverse #---------------------------------------------------------------------- def initServer(self): @@ -265,7 +273,47 @@ class JsEngine(object): self.server.send_rsp(clientId, req, 'successful', error) self.writeLog(u'发出响应:%s' %result) + + #---------------------------------------------------------------------- + def onQueryAccount(self, clientId, req): + """查询账户""" + params = req['params'] + account = self.account + + result = defaultdict(list) + result['id'].append(account.accountID) + result['init_balance'].append(account.preBalance) + result['enable_balance'].append(account.available) + result['margin'].append(account.margin) + result['float_pnl'].append(0.0) + result['close_pnl'].append(account.closeProfit) + result['holding_pnl'].append(0.0) + result['trading_pnl'].append(0.0) + result['type'].append('BA') + + error = [0, ''] + self.server.send_rsp(clientId, req, result, error) + + self.writeLog(u'发出响应:%s' %result) + + #---------------------------------------------------------------------- + def onQueryUniverse(self, clientId, req): + """查询账户""" + params = req['params'] + + l = self.mainEngine.getAllContracts() + + result = defaultdict(list) + for contract in l: + result['ba_id'].append(1000) + result['security'].append(self.converSymbol(contract.vtSymbol)) + + error = [0, ''] + self.server.send_rsp(clientId, req, result, error) + + self.writeLog(u'发出响应:%s' %result) + #---------------------------------------------------------------------- def writeLog(self, content): """发出日志事件""" @@ -284,7 +332,10 @@ class JsEngine(object): return '' e = EXCHANGE_MAP_REVERSE[contract.exchange] - return '.'.join(contract.symbol, e) - + return '.'.join([contract.symbol, e]) + #---------------------------------------------------------------------- + def processAccountEvent(self, event): + """账户资金更新""" + self.account = event.dict_['data'] \ No newline at end of file