代码之家  ›  专栏  ›  技术社区  ›  francis4396

如果还没有达到获利回吐,为什么我的策略使用第二个进入信号的数据而不是第一个进入信号?

  •  0
  • francis4396  · 技术社区  · 3 年前

    我创建了一个简单的ema交叉策略,获利是ATR的1.5倍。

    出于某种原因,如果在另一个进入信号(交叉)发生之前,我的第一次获利没有得到满足,该策略将使我的获利从第二个进入信号移动到ATR的1.5倍,而不是保持在第一个进入信号ATR的150倍。

    有人能解决这个问题吗?
    以下是我的代码和解释该问题的图片:

    Image of issue (https://i.stack.imgur.com/AYgZl.png)

    //@version=5
    strategy("EMA Bullish Cross with 2 TP Levels", overlay=true)
     
    // Input variables
    emaFastLength = input.int(10, "EMA Fast Length")
    emaSlowLength = input.int(20, "EMA Slow Length")
    atrLength = input.int(5, "ATR Length")
    tpMultiplier = input.float(1.5, "Take Profit 1 Multiplier")
     
    // Calculate EMA and ATR
    emaFast = ta.ema(close, emaFastLength)
    emaSlow = ta.ema(close, emaSlowLength)
    atr = ta.atr(atrLength)
    var float persistantATR = na
     
    // Determine if there's a bullish EMA cross
    Cross = ta.cross(emaFast, emaSlow)
     
    // Enter long trade if there's a bullish EMA cross
    if (Cross) 
        persistantATR := atr
        strategy.entry("Buy", strategy.long, qty = 100 )
     
        entry_price = strategy.opentrades.entry_price(0)
        strategy.exit("Take Profit", "Buy", qty_percent = 100, limit= entry_price + (persistantATR *           tpMultiplier))
     
    // debugging
    plot(atr, title = "atr")
    plot(persistantATR, title = "persistantATR")
    

    我删除了止损条件,只是为了调试。我还尝试在不同的时间框架上测试该策略。

    0 回复  |  直到 3 年前
        1
  •  0
  •   G.Lebret    3 年前

    在您的“if(Cross)”条件下,每次发生交叉时,它都会执行strategy.entry和strategy.exit。
    在您的示例中: 在第一个十字架上,完成strategy.entry并放置一个strategy.exit(您的限制=entry_price+(persistentATR*tpMultiplier))。
    在第二个十字上,strategy.entry被读取,但没有执行,因为你已经有一个同名的条目。但strategy.exit被执行,将你的限制更改为实际值。

    为了防止这种情况发生,您可以使用strategy.pentrades并在进入您的条件块之前测试是否没有交易:
    注意:strategy.pentrades会给你打开的交易数量(在你的情况下是0或1)

    //@version=5
    strategy("EMA Bullish Cross with 2 TP Levels", overlay=true)
     
    // Input variables
    emaFastLength = input.int(10, "EMA Fast Length")
    emaSlowLength = input.int(20, "EMA Slow Length")
    atrLength = input.int(5, "ATR Length")
    tpMultiplier = input.float(1.5, "Take Profit 1 Multiplier")
     
    // Calculate EMA and ATR
    emaFast = ta.ema(close, emaFastLength)
    emaSlow = ta.ema(close, emaSlowLength)
    atr = ta.atr(atrLength)
    var float persistantATR = na
     
    // Determine if there's a bullish EMA cross
    Cross = ta.cross(emaFast, emaSlow)
     
    // Enter long trade if there's a bullish EMA cross
    if (Cross) and strategy.opentrades == 0
        persistantATR := atr
        strategy.entry("Buy", strategy.long, qty = 100 )
     
        entry_price = strategy.opentrades.entry_price(0)
        strategy.exit("Take Profit", "Buy", qty_percent = 100, limit= entry_price + (persistantATR *           tpMultiplier))
     
    // debugging
    plot(atr, title = "atr")
    plot(persistantATR, title = "persistantATR")