diff --git a/vnpy/trader/app/algoTrading/__init__.py b/vnpy/trader/app/algoTrading/__init__.py index 5d8bab7c..0529d815 100644 --- a/vnpy/trader/app/algoTrading/__init__.py +++ b/vnpy/trader/app/algoTrading/__init__.py @@ -9,4 +9,4 @@ appName = 'AlgoTrading' appDisplayName = u'算法交易' appEngine = AlgoEngine appWidget = AlgoManager -appIco = 'ct.ico' +appIco = 'at.ico' diff --git a/vnpy/trader/app/algoTrading/at.ico b/vnpy/trader/app/algoTrading/at.ico new file mode 100644 index 00000000..83114df8 Binary files /dev/null and b/vnpy/trader/app/algoTrading/at.ico differ diff --git a/vnpy/trader/app/algoTrading/twapAlgo.py b/vnpy/trader/app/algoTrading/twapAlgo.py index f7723ddc..e5e93112 100644 --- a/vnpy/trader/app/algoTrading/twapAlgo.py +++ b/vnpy/trader/app/algoTrading/twapAlgo.py @@ -21,13 +21,14 @@ class TwapAlgo(AlgoTemplate): super(TwapAlgo, self).__init__(engine, setting, algoName) # 参数 - self.vtSymbol = setting['vtSymbol'] # 合约代码 - self.direction = setting['direction'] # 买卖 - self.price = setting['price'] # 目标价格 - self.totalVolume = setting['volume'] # 总数量 - self.time = setting['time'] # 执行时间 - self.interval = setting['interval'] # 执行间隔 - self.minVolume = setting['minVolume'] # 最小委托数量 + self.vtSymbol = setting['vtSymbol'] # 合约代码 + self.direction = setting['direction'] # 买卖 + self.targetPrice = setting['targetPrice'] # 目标价格 + self.totalVolume = setting['totalVolume'] # 总数量 + self.time = setting['time'] # 执行时间 + self.interval = setting['interval'] # 执行间隔 + self.minVolume = setting['minVolume'] # 最小委托数量 + self.priceLevel = setting['priceLevel'] # 使用第几档价格委托 # 变量 self.orderSize = self.totalVolume / (self.time / self.interval) @@ -87,15 +88,45 @@ class TwapAlgo(AlgoTemplate): # 买入 if self.direction == DIRECTION_LONG: # 市场买1价小于目标买价 - if tick.bidPrice1 < self.price: - self.buy(self.vtSymbol, self.price, size) - self.writeLog(u'委托买入%s,数量%,价格%s' %(self.vtSymbol, self.orderSize, self.price)) + if tick.bidPrice1 < self.targetPrice: + # 计算委托价格 + priceMap = { + 1: tick.askPrice1, + 2: tick.askPrice2, + 3: tick.askPrice3, + 4: tick.askPrice4, + 5: tick.askPrice5, + } + price = priceMap[self.priceLevel] + if price: + price = min(price, self.targetPrice) # 如果深度价格为0,则使用目标价 + else: + price = self.targetPrice + + # 发出委托 + self.buy(self.vtSymbol, price, size) + self.writeLog(u'委托买入%s,数量%,价格%s' %(self.vtSymbol, self.orderSize, price)) # 卖出 if self.direction == DIRECTION_SHORT: # 市场卖1价大于目标价 - if tick.askPrice1 > self.price: - self.sell(self.vtSymbol, self.price, size) - self.writeLog(u'委托卖出%s,数量%s,价格%s' %(self.vtSymbol, self.orderSize, self.price)) + if tick.askPrice1 > self.targetPrice: + # 计算委托价格 + priceMap = { + 1: tick.bidPrice1, + 2: tick.bidPrice2, + 3: tick.bidPrice3, + 4: tick.bidPrice4, + 5: tick.bidPrice5, + } + price = priceMap[self.priceLevel] + if price: + price = max(price, self.targetPrice) + else: + price = self.targetPrice + + # 发出委托 + self.sell(self.vtSymbol, price, size) + self.writeLog(u'委托卖出%s,数量%s,价格%s' %(self.vtSymbol, self.orderSize, price)) # 委托后等待到间隔一半的时间撤单 elif self.timerCount == round(self.interval/2, 0): @@ -129,10 +160,11 @@ class TwapAlgo(AlgoTemplate): d = OrderedDict() d[u'代码'] = self.vtSymbol d[u'方向'] = self.direction - d[u'目标价格'] = self.price + d[u'目标价格'] = self.targetPrice d[u'总数量'] = self.totalVolume d[u'总时间(秒)'] = self.time d[u'间隔(秒)'] = self.interval + d[u'委托档位'] = self.priceLevel self.putParamEvent(d) @@ -185,6 +217,11 @@ class TwapWidget(QtWidgets.QWidget): self.spinMinVolume.setDecimals(6) self.spinMinVolume.setValue(1) + self.spinPriceLevel = QtWidgets.QSpinBox() + self.spinPriceLevel.setMinimum(1) + self.spinPriceLevel.setMaximum(5) + self.spinPriceLevel.setValue(1) + buttonStart = QtWidgets.QPushButton(u'启动') buttonStart.clicked.connect(self.addAlgo) buttonStart.setMinimumHeight(100) @@ -204,9 +241,11 @@ class TwapWidget(QtWidgets.QWidget): grid.addWidget(self.spinTime, 4, 1) grid.addWidget(Label(u'间隔(秒)'), 5, 0) grid.addWidget(self.spinInterval, 5, 1) - grid.addWidget(Label(u'数量取整'), 6, 0) - grid.addWidget(self.spinMinVolume, 6, 1) - grid.addWidget(buttonStart, 7, 0, 1, 2) + grid.addWidget(Label(u'委托档位'), 6, 0) + 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) @@ -216,10 +255,11 @@ class TwapWidget(QtWidgets.QWidget): setting = { 'vtSymbol': str(self.lineSymbol.text()), 'direction': str(self.comboDirection.currentText()), - 'price': float(self.spinPrice.value()), - 'volume': float(self.spinVolume.value()), + 'targetPrice': float(self.spinPrice.value()), + 'totalVolume': float(self.spinVolume.value()), 'time': int(self.spinTime.value()), 'interval': int(self.spinInterval.value()), + 'priceLevel': int(self.spinPriceLevel.value()), 'minVolume': float(self.spinMinVolume.value()) }