[Mod]增加jaqsService的queryAccount和queryUniverse功能
This commit is contained in:
parent
d006349fc3
commit
4e7b64eb39
7
examples/JaqsService/CTP_connect.json
Normal file
7
examples/JaqsService/CTP_connect.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"brokerID": "9999",
|
||||||
|
"mdAddress": "tcp://180.168.146.187:10011",
|
||||||
|
"tdAddress": "tcp://180.168.146.187:10001",
|
||||||
|
"userID": "000300",
|
||||||
|
"password": "19890624"
|
||||||
|
}
|
4
examples/JaqsService/JS_setting.json
Normal file
4
examples/JaqsService/JS_setting.json
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 8901
|
||||||
|
}
|
18
examples/JaqsService/VT_setting.json
Normal file
18
examples/JaqsService/VT_setting.json
Normal file
@ -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"]
|
||||||
|
}
|
51
examples/JaqsService/run.py
Normal file
51
examples/JaqsService/run.py
Normal file
@ -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()
|
@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"host": "127.0.0.1",
|
"host": "127.0.0.1",
|
||||||
"port": 88888
|
"port": 8901
|
||||||
}
|
}
|
@ -9,7 +9,7 @@ from vnpy.event import Event
|
|||||||
from vnpy.trader.vtFunction import getJsonPath
|
from vnpy.trader.vtFunction import getJsonPath
|
||||||
from vnpy.trader.vtObject import VtLogData, VtOrderReq, VtCancelOrderReq
|
from vnpy.trader.vtObject import VtLogData, VtOrderReq, VtCancelOrderReq
|
||||||
from vnpy.trader.vtConstant import *
|
from vnpy.trader.vtConstant import *
|
||||||
|
from vnpy.trader.vtEvent import EVENT_ACCOUNT
|
||||||
|
|
||||||
|
|
||||||
EVENT_JS_LOG = 'eJsLog'
|
EVENT_JS_LOG = 'eJsLog'
|
||||||
@ -56,6 +56,7 @@ class JsEngine(object):
|
|||||||
|
|
||||||
self.server = None # RPC服务器
|
self.server = None # RPC服务器
|
||||||
self.cbDict = {} # 回调函数字典
|
self.cbDict = {} # 回调函数字典
|
||||||
|
self.account = None # 账户对象
|
||||||
|
|
||||||
# 注册日志事件类型
|
# 注册日志事件类型
|
||||||
self.mainEngine.registerLogEvent(EVENT_JS_LOG)
|
self.mainEngine.registerLogEvent(EVENT_JS_LOG)
|
||||||
@ -64,6 +65,11 @@ class JsEngine(object):
|
|||||||
self.initCallback()
|
self.initCallback()
|
||||||
self.initServer()
|
self.initServer()
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
def registerEvent(self):
|
||||||
|
"""注册事件监听"""
|
||||||
|
self.eventEngine.register(EVENT_ACCOUNT, self.processAccountEvent)
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def initCallback(self):
|
def initCallback(self):
|
||||||
"""初始化回调函数映射"""
|
"""初始化回调函数映射"""
|
||||||
@ -74,6 +80,8 @@ class JsEngine(object):
|
|||||||
self.cbDict['oms.query_order'] = self.onQueryOrder
|
self.cbDict['oms.query_order'] = self.onQueryOrder
|
||||||
self.cbDict['oms.place_order'] = self.onPlaceOrder
|
self.cbDict['oms.place_order'] = self.onPlaceOrder
|
||||||
self.cbDict['oms.cancel_order'] = self.onCancelOrder
|
self.cbDict['oms.cancel_order'] = self.onCancelOrder
|
||||||
|
self.cbDict['oms.query_account'] = self.onQueryAccount
|
||||||
|
self.cbDict['oms.query_universe'] = self.onQueryUniverse
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def initServer(self):
|
def initServer(self):
|
||||||
@ -265,7 +273,47 @@ class JsEngine(object):
|
|||||||
self.server.send_rsp(clientId, req, 'successful', error)
|
self.server.send_rsp(clientId, req, 'successful', error)
|
||||||
|
|
||||||
self.writeLog(u'发出响应:%s' %result)
|
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):
|
def writeLog(self, content):
|
||||||
"""发出日志事件"""
|
"""发出日志事件"""
|
||||||
@ -284,7 +332,10 @@ class JsEngine(object):
|
|||||||
return ''
|
return ''
|
||||||
|
|
||||||
e = EXCHANGE_MAP_REVERSE[contract.exchange]
|
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']
|
||||||
|
|
Loading…
Reference in New Issue
Block a user