首页 > 解决方案 > 使用定义了限制和停止的 strategy.exit 进行回测不一致

问题描述

似乎无法弄清楚为什么回测没有严格遵守我的限制和停止值。大多数订单都在 3%,但我得到的订单在交易列表中以 3.38% 或 3.91% 的价格退出,这不是我需要的行为。我看到大约 15% 的时间差异不适合 3%

我认为它可能是 strategy.position_avg_price 但我不知道有任何其他方法可以获取从 strategy.entry 创建的订单的价格。

//@version=4
strategy("GS MACD Strategy v2", 
      shorttitle="GSMACDv2", 
      overlay=true, 
      pyramiding=0, 
      currency=currency.USD, 
      initial_capital=1000, 
      default_qty_type=strategy.percent_of_equity, 
      default_qty_value=90, 
      commission_type=strategy.commission.percent, 
      commission_value=0.3, 
      calc_on_every_tick=true, 
      backtest_fill_limits_assumption=1)

// MACD Params      
fastLength = input(12)
slowlength = input(26)
MACDLength = input(7)

PercentVal = .03
SellPrice     = strategy.position_avg_price * (1 + PercentVal)
StopLossPrice = strategy.position_avg_price * (1 - PercentVal)

// Calc MACD Lines
MACD  = ema(close, fastLength) - ema(close, slowlength)
aMACD = ema(MACD, MACDLength)
delta = MACD - aMACD

enterLong  = crossover(delta, 0)

if (enterLong)
    strategy.entry("LE", strategy.long)

if (strategy.position_size > 0)
    strategy.exit("LC", "LE", limit = SellPrice , stop = StopLossPrice)

标签: pine-script

解决方案


推荐阅读