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