From d223c9fea1de3d8270fbefcb315024b0f79fb8c2 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 11 Nov 2018 13:59:02 +0800 Subject: [PATCH] =?UTF-8?q?[Mod]=E9=99=90=E5=88=B6=E6=B5=B7=E9=BE=9F?= =?UTF-8?q?=E7=AD=96=E7=95=A5=E7=9A=84=E9=95=BF=E7=9F=AD=E4=BF=A1=E5=8F=B7?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E6=97=B6=E9=97=B4=E5=8F=AA=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E4=BA=A4=E6=98=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/TurtleStrategy/turtleStrategy.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/TurtleStrategy/turtleStrategy.py b/examples/TurtleStrategy/turtleStrategy.py index 53f00134..d33d6b08 100644 --- a/examples/TurtleStrategy/turtleStrategy.py +++ b/examples/TurtleStrategy/turtleStrategy.py @@ -55,7 +55,7 @@ class TurtleSignal(object): self.atrWindow = atrWindow # 计算ATR周期数 self.profitCheck = profitCheck # 是否检查上一笔盈利 - self.am = ArrayManager() # K线容器 + self.am = ArrayManager(60) # K线容器 self.atrVolatility = 0 # ATR波动率 self.entryUp = 0 # 入场通道 @@ -79,7 +79,7 @@ class TurtleSignal(object): self.result = None # 当前的交易 self.resultList = [] # 交易列表 self.bar = None # 最新K线 - + #---------------------------------------------------------------------- def onBar(self, bar): """""" @@ -137,7 +137,7 @@ class TurtleSignal(object): return # 没有仓位或者持有空头仓位的时候,可以做空(加仓) - elif self.pos <= 0: + if self.pos <= 0: if bar.low <= self.shortEntry1 and self.pos > -1: self.short(self.shortEntry1, 1) @@ -269,6 +269,8 @@ class TurtlePortfolio(object): self.posDict = {} # 每个品种的持仓情况 self.totalLong = 0 # 总的多头持仓 self.totalShort = 0 # 总的空头持仓 + + self.tradingDict = {} # 交易中的信号字典 #---------------------------------------------------------------------- def init(self, vtSymbolList): @@ -333,6 +335,18 @@ class TurtlePortfolio(object): volume = min(volume, abs(pos)) + # 获取当前交易中的信号,如果不是本信号,则忽略 + currentSignal = self.tradingDict.get(signal.vtSymbol, None) + if currentSignal and currentSignal is not signal: + return + + # 开仓则缓存该信号的交易状态 + if offset == OFFSET_OPEN: + self.tradingDict[signal.vtSymbol] = signal + # 平仓则清除该信号 + else: + self.tradingDict.pop(signal.vtSymbol) + self.sendOrder(signal.vtSymbol, direction, offset, price, volume) #----------------------------------------------------------------------