From ce0931c977cffa4cde52e891d755e9f91a01a64a Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 11 Nov 2018 11:55:10 +0800 Subject: [PATCH] =?UTF-8?q?[Mod]=E5=A2=9E=E5=8A=A0=E6=B5=B7=E9=BE=9F?= =?UTF-8?q?=E5=85=A5=E5=9C=BA=E4=BB=B7=E6=A0=BC=E8=AE=A1=E7=AE=97=E6=97=B6?= =?UTF-8?q?=E5=AF=B9=E5=BD=93=E5=89=8DK=E7=BA=BF=E5=BC=80=E7=9B=98?= =?UTF-8?q?=E4=BB=B7=E7=9A=84=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/TurtleStrategy/turtleStrategy.py | 29 ++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/examples/TurtleStrategy/turtleStrategy.py b/examples/TurtleStrategy/turtleStrategy.py index a6ce811e..53f00134 100644 --- a/examples/TurtleStrategy/turtleStrategy.py +++ b/examples/TurtleStrategy/turtleStrategy.py @@ -78,10 +78,12 @@ class TurtleSignal(object): self.pos = 0 # 信号持仓 self.result = None # 当前的交易 self.resultList = [] # 交易列表 + self.bar = None # 最新K线 #---------------------------------------------------------------------- def onBar(self, bar): """""" + self.bar = bar self.am.updateBar(bar) if not self.am.inited: return @@ -175,6 +177,8 @@ class TurtleSignal(object): #---------------------------------------------------------------------- def buy(self, price, volume): """买入开仓""" + price = self.calculateTradePrice(DIRECTION_LONG, price) + self.open(price, volume) self.newSignal(DIRECTION_LONG, OFFSET_OPEN, price, volume) @@ -184,13 +188,17 @@ class TurtleSignal(object): #---------------------------------------------------------------------- def sell(self, price): """卖出平仓""" + price = self.calculateTradePrice(DIRECTION_SHORT, price) volume = abs(self.pos) + self.close(price) self.newSignal(DIRECTION_SHORT, OFFSET_CLOSE, price, volume) #---------------------------------------------------------------------- def short(self, price, volume): """卖出开仓""" + price = self.calculateTradePrice(DIRECTION_SHORT, price) + self.open(price, -volume) self.newSignal(DIRECTION_SHORT, OFFSET_OPEN, price, volume) @@ -200,7 +208,9 @@ class TurtleSignal(object): #---------------------------------------------------------------------- def cover(self, price): """买入平仓""" + price = self.calculateTradePrice(DIRECTION_LONG, price) volume = abs(self.pos) + self.close(price) self.newSignal(DIRECTION_LONG, OFFSET_CLOSE, price, volume) @@ -223,13 +233,26 @@ class TurtleSignal(object): self.result = None #---------------------------------------------------------------------- - def lastPnl(self): - """上一笔交易的盈亏""" + def getLastPnl(self): + """获取上一笔交易的盈亏""" if not self.resultList: return 0 result = self.resultList[-1] return result.pnl + + #---------------------------------------------------------------------- + def calculateTradePrice(self, direction, price): + """计算成交价格""" + # 买入时,停止单成交的最优价格不能低于当前K线开盘价 + if direction == DIRECTION_LONG: + tradePrice = max(self.bar.open, price) + # 卖出时,停止单成交的最优价格不能高于当前K线开盘价 + else: + tradePrice = min(self.bar.open, price) + + return tradePrice + ######################################################################## @@ -275,7 +298,7 @@ class TurtlePortfolio(object): if offset == OFFSET_OPEN: # 检查上一次是否为盈利 if signal.profitCheck: - pnl = signal.lastPnl() + pnl = signal.getLastPnl() if pnl > 0: return