[Add]新增算法交易RPC服务功能

This commit is contained in:
vn.py 2018-07-25 10:03:34 +08:00
parent 76fd43dcd4
commit f3dcc0bf20
3 changed files with 121 additions and 9 deletions

View File

@ -0,0 +1,46 @@
# encoding: UTF-8
from time import sleep
from vnpy.rpc import RpcClient
from vnpy.trader.vtConstant import OFFSET_OPEN, DIRECTION_LONG
########################################################################
class TestClient(RpcClient):
""""""
#----------------------------------------------------------------------
def __init__(self, reqAddress, subAddress):
"""Constructor"""
super(TestClient, self).__init__(reqAddress, subAddress)
#----------------------------------------------------------------------
def callback(self, topic, data):
"""回调函数实现"""
print('client received topic:', topic, ', data:', data)
if __name__ == '__main__':
reqAddress = 'tcp://localhost:8899'
subAddress = 'tcp://localhost:9988'
tc = TestClient(reqAddress, subAddress)
tc.subscribeTopic('')
tc.start()
setting = {
'templateName': u'BestLimit 最优限价',
'vtSymbol': 'rb1810.SHFE',
'volume': 10,
'direction': DIRECTION_LONG,
'offset': OFFSET_OPEN
}
algoName = tc.addAlgo(setting)
print u'启动算法,实例名', algoName
sleep(5)
tc.stopAlgo(algoName)
print u'停止算法'
raw_input()

View File

@ -1,6 +1,7 @@
# encoding: UTF-8
'''
算法交易引擎
'''
from __future__ import division
@ -8,6 +9,7 @@ import os
import importlib
from vnpy.event import Event
from vnpy.rpc import RpcServer
from vnpy.trader.vtEvent import EVENT_TIMER, EVENT_TICK, EVENT_ORDER, EVENT_TRADE
from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT,
PRICETYPE_LIMITPRICE, PRICETYPE_MARKETPRICE,
@ -38,6 +40,7 @@ class AlgoEngine(object):
""""""
self.mainEngine = mainEngine
self.eventEngine = eventEngine
self.rpcServer = None
self.algoDict = {} # algoName:algo
self.orderAlgoDict = {} # vtOrderID:algo
@ -58,7 +61,8 @@ class AlgoEngine(object):
#----------------------------------------------------------------------
def stop(self):
"""停止"""
pass
if self.rpcServer:
self.rpcServer.stop()
#----------------------------------------------------------------------
def processTickEvent(self, event):
@ -322,3 +326,32 @@ class AlgoEngine(object):
event = Event(EVENT_ALGO_SETTING)
event.dict_['data'] = algoSetting
self.eventEngine.put(event)
#----------------------------------------------------------------------
def startRpc(self, repPort, pubPort):
"""启动RPC服务"""
if self.rpcServer:
return
self.rpcServer = AlgoRpcServer(self, repPort, pubPort)
self.rpcServer.start()
self.writeLog(u'算法交易RPC服务启动成功REP端口:%sPUB端口:%s' %(repPort, pubPort))
########################################################################
class AlgoRpcServer(RpcServer):
"""算法交易RPC服务器"""
#----------------------------------------------------------------------
def __init__(self, engine, repPort, pubPort):
"""Constructor"""
self.engine = engine
repAddress = 'tcp://*:%s' %repPort
pubAddress = 'tcp://*:%s' %pubPort
super(AlgoRpcServer, self).__init__(repAddress, pubAddress)
self.register(self.engine.addAlgo)
self.register(self.engine.stopAlgo)
self.register(self.engine.stopAll)

View File

@ -397,17 +397,17 @@ class AlgoManager(QtWidgets.QWidget):
""""""
self.setWindowTitle(u'算法交易')
buttonWidth = 400
buttonHeight = 60
#buttonWidth = 400
#buttonHeight = 60
self.comboTemplate = QtWidgets.QComboBox()
self.comboTemplate.setMaximumWidth(buttonWidth)
#self.comboTemplate.setMaximumWidth(buttonWidth)
self.comboTemplate.currentIndexChanged.connect(self.changeWidget)
vbox = QtWidgets.QVBoxLayout()
for templateName, widgetClass in WIDGET_DICT.items():
widget = widgetClass(self.algoEngine)
widget.setMaximumWidth(buttonWidth)
#widget.setMaximumWidth(buttonWidth)
widget.hide()
vbox.addWidget(widget)
@ -415,18 +415,21 @@ class AlgoManager(QtWidgets.QWidget):
self.comboTemplate.addItem(templateName)
self.buttonStop = StopButton(self.algoEngine)
self.buttonStop.setMaximumWidth(buttonWidth)
self.buttonStop.setFixedHeight(buttonHeight)
self.buttonAddAlgo = QtWidgets.QPushButton(u'启动篮子算法')
self.buttonAddAlgo.setStyleSheet("color:white;background-color:green")
self.buttonAddAlgo.clicked.connect(self.addAlgoFromCsv)
self.buttonAddAlgo.setFixedHeight(buttonHeight)
self.buttonSaveSetting = QtWidgets.QPushButton(u'加载算法配置')
self.buttonSaveSetting.setStyleSheet("color:white;background-color:blue")
self.buttonSaveSetting.clicked.connect(self.saveSettingFromCsv)
self.buttonSaveSetting.setFixedHeight(buttonHeight)
self.lineRepPort = QtWidgets.QLineEdit('8899')
self.linePubPort = QtWidgets.QLineEdit('9988')
self.buttonStartRpc = QtWidgets.QPushButton(u'启动RPC服务')
self.buttonStartRpc.setStyleSheet("color:black;background-color:orange")
self.buttonStartRpc.clicked.connect(self.startRpc)
label = QtWidgets.QLabel(u'算法类型')
label.setFixedWidth(100)
@ -435,6 +438,12 @@ class AlgoManager(QtWidgets.QWidget):
hbox.addWidget(label)
hbox.addWidget(self.comboTemplate)
grid = QtWidgets.QGridLayout()
grid.addWidget(QtWidgets.QLabel(u'REP端口'), 0, 0)
grid.addWidget(self.lineRepPort, 0, 1)
grid.addWidget(QtWidgets.QLabel(u'PUB端口'), 1, 0)
grid.addWidget(self.linePubPort, 1, 1)
vbox1 = QtWidgets.QVBoxLayout()
vbox1.addLayout(hbox)
vbox1.addLayout(vbox)
@ -442,8 +451,13 @@ class AlgoManager(QtWidgets.QWidget):
vbox1.addWidget(self.buttonStop)
vbox1.addWidget(self.buttonAddAlgo)
vbox1.addWidget(self.buttonSaveSetting)
vbox1.addStretch()
vbox1.addLayout(grid)
vbox1.addWidget(self.buttonStartRpc)
workingMonitor = AlgoStatusMonitor(self.algoEngine, AlgoStatusMonitor.MODE_WORKING)
workingMonitor.setFixedWidth(1500)
historyMonitor = AlgoStatusMonitor(self.algoEngine, AlgoStatusMonitor.MODE_HISTORY)
logMonitor = AlgoLogMonitor(self.algoEngine)
settingMonitor = AlgoSettingMonitor(self.algoEngine)
@ -526,3 +540,22 @@ class AlgoManager(QtWidgets.QWidget):
l = self.loadCsv(path)
for setting in l:
self.algoEngine.addAlgo(setting)
#----------------------------------------------------------------------
def startRpc(self):
"""启动算法服务"""
try:
repPort = int(self.lineRepPort.text())
pubPort = int(self.linePubPort.text())
except:
self.algoEngine.writeLog(u'请检查RPC端口只能使用整数')
return
self.algoEngine.startRpc(repPort, pubPort)
#----------------------------------------------------------------------
def show(self):
""""""
self.showMaximized()