From 19f559f36ade04186d335eecb8ea6d9a50fc9652 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sat, 9 Jun 2018 18:55:31 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E6=8A=BD=E8=B1=A1=E5=89=A5=E7=A6=BB?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E6=8E=A7=E5=88=B6=E7=BB=84=E4=BB=B6=E7=88=B6?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/app/algoTrading/algoTemplate.py | 4 +- vnpy/trader/app/algoTrading/twapAlgo.py | 35 +++++------ vnpy/trader/app/algoTrading/uiAlgoManager.py | 2 +- vnpy/trader/app/algoTrading/uiAlgoWidget.py | 66 ++++++++++++++++++++ 4 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 vnpy/trader/app/algoTrading/uiAlgoWidget.py diff --git a/vnpy/trader/app/algoTrading/algoTemplate.py b/vnpy/trader/app/algoTrading/algoTemplate.py index 9866669f..29e89a2a 100644 --- a/vnpy/trader/app/algoTrading/algoTemplate.py +++ b/vnpy/trader/app/algoTrading/algoTemplate.py @@ -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 diff --git a/vnpy/trader/app/algoTrading/twapAlgo.py b/vnpy/trader/app/algoTrading/twapAlgo.py index e5e93112..6e06c9b9 100644 --- a/vnpy/trader/app/algoTrading/twapAlgo.py +++ b/vnpy/trader/app/algoTrading/twapAlgo.py @@ -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 \ No newline at end of file diff --git a/vnpy/trader/app/algoTrading/uiAlgoManager.py b/vnpy/trader/app/algoTrading/uiAlgoManager.py index 1c0baea3..7459339d 100644 --- a/vnpy/trader/app/algoTrading/uiAlgoManager.py +++ b/vnpy/trader/app/algoTrading/uiAlgoManager.py @@ -240,4 +240,4 @@ class AlgoManager(QtWidgets.QWidget): def addAlgoWidget(self, widgetClass): """""" w = widgetClass(self.algoEngine) - self.tab.addTab(w, w.name) \ No newline at end of file + self.tab.addTab(w, w.templateName) \ No newline at end of file diff --git a/vnpy/trader/app/algoTrading/uiAlgoWidget.py b/vnpy/trader/app/algoTrading/uiAlgoWidget.py new file mode 100644 index 00000000..7df9ddb1 --- /dev/null +++ b/vnpy/trader/app/algoTrading/uiAlgoWidget.py @@ -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