在您的“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")