[Add]抽象剥离算法控制组件父类

This commit is contained in:
vn.py 2018-06-09 18:55:31 +08:00
parent 08253b087b
commit 19f559f36a
4 changed files with 83 additions and 24 deletions

View File

@ -12,7 +12,7 @@ STATUS_ACTIVE = [STATUS_NOTTRADED, STATUS_PARTTRADED, STATUS_UNKNOWN]
########################################################################
class AlgoTemplate(object):
"""算法模板"""
name = 'AlgoTemplate'
templateName = 'AlgoTemplate'
count = 0
@classmethod
@ -20,7 +20,7 @@ class AlgoTemplate(object):
def new(cls, engine, setting):
"""创建新对象"""
cls.count += 1
algoName = '%s_%s' %(cls.name, cls.count)
algoName = '%s_%s' %(cls.templateName, cls.count)
algo = cls(engine, setting, algoName)
return algo

View File

@ -6,14 +6,15 @@ from collections import OrderedDict
from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT)
from vnpy.trader.uiQt import QtWidgets
from algoTemplate import AlgoTemplate
from .algoTemplate import AlgoTemplate
from .uiAlgoWidget import AlgoWidget, QtWidgets
########################################################################
class TwapAlgo(AlgoTemplate):
"""TWAP算法"""
name = 'TWAP'
templateName = 'TWAP'
#----------------------------------------------------------------------
def __init__(self, engine, setting, algoName):
@ -168,23 +169,19 @@ class TwapAlgo(AlgoTemplate):
self.putParamEvent(d)
########################################################################
class TwapWidget(QtWidgets.QWidget):
class TwapWidget(AlgoWidget):
""""""
name = TwapAlgo.name
#----------------------------------------------------------------------
def __init__(self, algoEngine):
def __init__(self, algoEngine, parent=None):
"""Constructor"""
super(TwapWidget, self).__init__()
super(TwapWidget, self).__init__(algoEngine, parent)
self.algoEngine = algoEngine
self.initUi()
self.templateName = TwapAlgo.templateName
#----------------------------------------------------------------------
def initUi(self):
def initAlgoLayout(self):
""""""
self.lineSymbol = QtWidgets.QLineEdit()
@ -245,14 +242,14 @@ class TwapWidget(QtWidgets.QWidget):
grid.addWidget(self.spinPriceLevel, 6, 1)
grid.addWidget(Label(u'数量取整'), 7, 0)
grid.addWidget(self.spinMinVolume, 7, 1)
grid.addWidget(buttonStart, 8, 0, 1, 2)
self.setLayout(grid)
return grid
#----------------------------------------------------------------------
def addAlgo(self):
def getAlgoSetting(self):
""""""
setting = {
'templateName': TwapAlgo.templateName,
'vtSymbol': str(self.lineSymbol.text()),
'direction': str(self.comboDirection.currentText()),
'targetPrice': float(self.spinPrice.value()),
@ -263,10 +260,6 @@ class TwapWidget(QtWidgets.QWidget):
'minVolume': float(self.spinMinVolume.value())
}
self.algoEngine.addAlgo(TwapAlgo.name, setting)
return setting

View File

@ -240,4 +240,4 @@ class AlgoManager(QtWidgets.QWidget):
def addAlgoWidget(self, widgetClass):
""""""
w = widgetClass(self.algoEngine)
self.tab.addTab(w, w.name)
self.tab.addTab(w, w.templateName)

View File

@ -0,0 +1,66 @@
# encoding: UTF-8
from vnpy.trader.uiQt import QtWidgets
########################################################################
class AlgoWidget(QtWidgets.QWidget):
"""算法启动组件"""
#----------------------------------------------------------------------
def __init__(self, algoEngine, parent=None):
"""Constructor"""
super(AlgoWidget, self).__init__(parent)
self.templateName = ''
self.algoEngine = algoEngine
self.initUi()
#----------------------------------------------------------------------
def initUi(self):
""""""
algoLayout = self.initAlgoLayout()
buttonStart = QtWidgets.QPushButton(u'启动算法')
buttonStart.clicked.connect(self.addAlgo)
buttonStart.setMinimumHeight(100)
buttonSave = QtWidgets.QPushButton(u'保存配置')
buttonSave.clicked.connect(self.saveAlgoSetting)
buttonSave.setMinimumHeight(100)
self.lineSettingName = QtWidgets.QLineEdit()
self.lineSettingName.setPlaceholderText(u'算法配置名称')
vbox = QtWidgets.QVBoxLayout()
vbox.addLayout(algoLayout)
vbox.addWidget(buttonStart)
vbox.addWidget(QtWidgets.QLabel(''))
vbox.addWidget(QtWidgets.QLabel(''))
vbox.addWidget(self.lineSettingName)
vbox.addWidget(buttonSave)
self.setLayout(vbox)
#----------------------------------------------------------------------
def addAlgo(self):
"""启动算法"""
setting = self.getAlgoSetting()
self.algoEngine.addAlgo(setting)
#----------------------------------------------------------------------
def saveAlgoSetting(self):
"""保存算法配置"""
setting = self.getAlgoSetting()
self.algoEngine.saveAlgoSetting(setting)
#----------------------------------------------------------------------
def initAlgoLayout(self):
"""初始化算法相关的组件部分"""
pass
#----------------------------------------------------------------------
def getAlgoSetting(self):
"""获取算法配置"""
pass