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