From 915a4bfcb08dbdff924ed1287adea30bc622446a Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 26 Jan 2018 11:16:37 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E4=BB=B7=E5=B7=AE=E4=BA=A4=E6=98=93?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E6=94=B9=E4=B8=BA=E8=BF=90=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E7=A6=81=E6=AD=A2=E4=BF=AE=E6=94=B9=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/app/spreadTrading/stAlgo.py | 51 +++++++++------- vnpy/trader/app/spreadTrading/uiStWidget.py | 64 ++++++++++++++++++++- 2 files changed, 92 insertions(+), 23 deletions(-) diff --git a/vnpy/trader/app/spreadTrading/stAlgo.py b/vnpy/trader/app/spreadTrading/stAlgo.py index 2a5444d3..76a895c8 100644 --- a/vnpy/trader/app/spreadTrading/stAlgo.py +++ b/vnpy/trader/app/spreadTrading/stAlgo.py @@ -288,6 +288,33 @@ class SniperAlgo(StAlgoTemplate): if self.hedgeCount > self.hedgeInterval: self.cancelAllPassiveLegOrders() self.hedgeCount = 0 + + #---------------------------------------------------------------------- + def checkPrice(self): + """检查价格""" + # 做多检查 + if self.mode != self.MODE_SHORTONLY: + if self.buyPrice >= self.sellPrice: + self.writeLog(u'启动失败,允许多头交易时BuyPrice必须小于SellPrice') + return False + + # 做空检查 + if self.mode != self.MODE_LONGONLY: + if self.shortPrice <= self.coverPrice: + self.writeLog(u'启动失败,允许空头交易时ShortPrice必须大于CoverPrice') + return False + + # 多空检查 + if self.mode == self.MODE_LONGSHORT: + if self.buyPrice >= self.coverPrice: + self.writeLog(u'启动失败,允许双向交易时BuyPrice必须小于CoverPrice') + return False + + if self.shortPrice <= self.sellPrice: + self.writeLog(u'启动失败,允许双向交易时ShortPrice必须大于SellPrice') + return False + + return True #---------------------------------------------------------------------- def start(self): @@ -296,27 +323,9 @@ class SniperAlgo(StAlgoTemplate): if self.active: return self.active - # 做多检查 - if self.mode != self.MODE_SHORTONLY: - if self.buyPrice >= self.sellPrice: - self.writeLog(u'启动失败,允许多头交易时BuyPrice必须小于SellPrice') - return self.active - - # 做空检查 - if self.mode != self.MODE_LONGONLY: - if self.shortPrice <= self.coverPrice: - self.writeLog(u'启动失败,允许空头交易时ShortPrice必须大于CoverPrice') - return self.active - - # 多空检查 - if self.mode == self.MODE_LONGSHORT: - if self.buyPrice >= self.coverPrice: - self.writeLog(u'启动失败,允许双向交易时BuyPrice必须小于CoverPrice') - return self.active - - if self.shortPrice <= self.sellPrice: - self.writeLog(u'启动失败,允许双向交易时ShortPrice必须大于SellPrice') - return self.active + # 检查价格安全性 + if not self.checkPrice(): + return False # 启动算法 self.quoteCount = 0 diff --git a/vnpy/trader/app/spreadTrading/uiStWidget.py b/vnpy/trader/app/spreadTrading/uiStWidget.py index 0445a24b..a6881378 100644 --- a/vnpy/trader/app/spreadTrading/uiStWidget.py +++ b/vnpy/trader/app/spreadTrading/uiStWidget.py @@ -141,7 +141,16 @@ class StBuyPriceSpinBox(QtWidgets.QDoubleSpinBox): def setPrice(self, value): """设置价格""" self.algoEngine.setAlgoBuyPrice(self.spreadName, value) - + + #---------------------------------------------------------------------- + def algoActiveChanged(self, active): + """算法运行状态改变""" + # 只允许算法停止时修改运行模式 + if active: + self.setEnabled(False) + else: + self.setEnabled(True) + ######################################################################## class StSellPriceSpinBox(QtWidgets.QDoubleSpinBox): @@ -165,6 +174,15 @@ class StSellPriceSpinBox(QtWidgets.QDoubleSpinBox): def setPrice(self, value): """设置价格""" self.algoEngine.setAlgoSellPrice(self.spreadName, value) + + #---------------------------------------------------------------------- + def algoActiveChanged(self, active): + """算法运行状态改变""" + # 只允许算法停止时修改运行模式 + if active: + self.setEnabled(False) + else: + self.setEnabled(True) ######################################################################## @@ -190,6 +208,15 @@ class StShortPriceSpinBox(QtWidgets.QDoubleSpinBox): """设置价格""" self.algoEngine.setAlgoShortPrice(self.spreadName, value) + #---------------------------------------------------------------------- + def algoActiveChanged(self, active): + """算法运行状态改变""" + # 只允许算法停止时修改运行模式 + if active: + self.setEnabled(False) + else: + self.setEnabled(True) + ######################################################################## class StCoverPriceSpinBox(QtWidgets.QDoubleSpinBox): @@ -214,6 +241,15 @@ class StCoverPriceSpinBox(QtWidgets.QDoubleSpinBox): """设置价格""" self.algoEngine.setAlgoCoverPrice(self.spreadName, value) + #---------------------------------------------------------------------- + def algoActiveChanged(self, active): + """算法运行状态改变""" + # 只允许算法停止时修改运行模式 + if active: + self.setEnabled(False) + else: + self.setEnabled(True) + ######################################################################## class StMaxPosSizeSpinBox(QtWidgets.QSpinBox): @@ -237,7 +273,16 @@ class StMaxPosSizeSpinBox(QtWidgets.QSpinBox): """设置价格""" self.algoEngine.setAlgoMaxPosSize(self.spreadName, size) - + #---------------------------------------------------------------------- + def algoActiveChanged(self, active): + """算法运行状态改变""" + # 只允许算法停止时修改运行模式 + if active: + self.setEnabled(False) + else: + self.setEnabled(True) + + ######################################################################## class StMaxOrderSizeSpinBox(QtWidgets.QSpinBox): """""" @@ -260,6 +305,15 @@ class StMaxOrderSizeSpinBox(QtWidgets.QSpinBox): """设置价格""" self.algoEngine.setAlgoMaxOrderSize(self.spreadName, size) + #---------------------------------------------------------------------- + def algoActiveChanged(self, active): + """算法运行状态改变""" + # 只允许算法停止时修改运行模式 + if active: + self.setEnabled(False) + else: + self.setEnabled(True) + ######################################################################## class StModeComboBox(QtWidgets.QComboBox): @@ -422,6 +476,12 @@ class StAlgoManager(QtWidgets.QTableWidget): self.setCellWidget(row, 8, comboMode) self.setCellWidget(row, 9, buttonActive) + buttonActive.signalActive.connect(spinBuyPrice.algoActiveChanged) + buttonActive.signalActive.connect(spinSellPrice.algoActiveChanged) + buttonActive.signalActive.connect(spinShortPrice.algoActiveChanged) + buttonActive.signalActive.connect(spinCoverPrice.algoActiveChanged) + buttonActive.signalActive.connect(spinMaxOrderSize.algoActiveChanged) + buttonActive.signalActive.connect(spinMaxPosSize.algoActiveChanged) buttonActive.signalActive.connect(comboMode.algoActiveChanged) self.buttonActiveDict[d['spreadName']] = buttonActive