From 414e3195293f0eb74d697a447bffa186330a06ed Mon Sep 17 00:00:00 2001 From: msincenselee Date: Thu, 4 Aug 2016 15:13:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=AD=96=E7=95=A5=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=A2=9E=E5=8A=A0=E2=80=9C?= =?UTF-8?q?=E5=BC=BA=E8=A1=8C=E5=88=9D=E5=A7=8B=E5=8C=96=E2=80=9D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/ctaAlgo/ctaEngine.py | 35 +++++++++++++++++++++++++++++--- vn.trader/ctaAlgo/uiCtaWidget.py | 11 +++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/vn.trader/ctaAlgo/ctaEngine.py b/vn.trader/ctaAlgo/ctaEngine.py index f58824e0..0fb968ef 100644 --- a/vn.trader/ctaAlgo/ctaEngine.py +++ b/vn.trader/ctaAlgo/ctaEngine.py @@ -177,6 +177,33 @@ class CtaEngine(object): else: self.writeCtaLog(u'委托单({0}不存在'.format(vtOrderID)) + # ---------------------------------------------------------------------- + def cancelOrders(self, symbol, offset=EMPTY_STRING): + """撤销所有单""" + # Symbol参数:指定合约的撤单; + # OFFSET参数:指定Offset的撤单,缺省不填写时,为所有 + + l = self.mainEngine.getAllWorkingOrders() + + self.writeCtaLog(u'撤销所有订单,数量:{0}'.format(len(l))) + + for order in l: + if offset == EMPTY_STRING: + offsetCond = True + else: + offsetCond = order.offset == offset + + if order.symbol == symbol and offsetCond: + req = VtCancelOrderReq() + req.symbol = order.symbol + req.exchange = order.exchange + req.frontID = order.frontID + req.sessionID = order.sessionID + req.orderID = order.orderID + self.writeCtaLog(u'撤单:{0}/{1},{2}{3}手' + .format(order.symbol, order.orderID, order.offset, order.totalVolume-order.tradedVolume)) + self.mainEngine.cancelOrder(req, order.gatewayName) + # ---------------------------------------------------------------------- def sendStopOrder(self, vtSymbol, orderType, price, volume, strategy): """发停止单(本地实现)""" @@ -302,6 +329,8 @@ class CtaEngine(object): # 1.获取事件的Order数据 order = event.dict_['data'] + # order.vtOrderID 在gateway中,已经格式化为 gatewayName.vtOrderID + # 2.判断order是否在映射字典中 if order.vtOrderID in self.orderStrategyDict: # 3.提取对应的策略 @@ -478,13 +507,13 @@ class CtaEngine(object): self.writeCtaLog(u'%s的交易合约%s无法找到' %(name, strategy.vtSymbol)) #---------------------------------------------------------------------- - def initStrategy(self, name): + def initStrategy(self, name, force = False): """初始化策略""" if name in self.strategyDict: strategy = self.strategyDict[name] - if not strategy.inited: - strategy.onInit() + if not strategy.inited or force == True: + strategy.onInit(force=force) strategy.inited = True else: self.writeCtaLog(u'请勿重复初始化策略实例:%s' %name) diff --git a/vn.trader/ctaAlgo/uiCtaWidget.py b/vn.trader/ctaAlgo/uiCtaWidget.py index 8986338a..65e5c5f8 100644 --- a/vn.trader/ctaAlgo/uiCtaWidget.py +++ b/vn.trader/ctaAlgo/uiCtaWidget.py @@ -88,14 +88,18 @@ class CtaStrategyManager(QtGui.QGroupBox): buttonInit = QtGui.QPushButton(u'初始化') buttonStart = QtGui.QPushButton(u'启动') buttonStop = QtGui.QPushButton(u'停止') + buttonInitForce = QtGui.QPushButton(u'强制初始化') buttonInit.clicked.connect(self.init) buttonStart.clicked.connect(self.start) buttonStop.clicked.connect(self.stop) + buttonInitForce.clicked.connect(self.initForce) hbox1 = QtGui.QHBoxLayout() hbox1.addWidget(buttonInit) hbox1.addWidget(buttonStart) hbox1.addWidget(buttonStop) + hbox1.addWidget(buttonInitForce) + hbox1.addStretch() hbox2 = QtGui.QHBoxLayout() @@ -132,7 +136,12 @@ class CtaStrategyManager(QtGui.QGroupBox): def init(self): """初始化策略""" self.ctaEngine.initStrategy(self.name) - + + def initForce(self): + + """强制初始化策略""" + self.ctaEngine.initStrategy(self.name,force = True) + #---------------------------------------------------------------------- def start(self): """启动策略"""