我创建了一个简单的 EMA 交叉策略,止盈为 ATR 的 1.5 倍。出于某种原因,如果在另一个进入信号(交叉)出现之前我的第一个止盈未被满足,该策略会将我的止盈从第二个进入信号移动到 ATR 的 1.5 倍,而不是将其保持在 ATR 从第二个进入信号的 1.5 倍第一个进入信号。有人对此有解决办法吗?下面是我的代码和解释问题的图片:
//@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")
我去掉了止损条件,只是为了调试。我还尝试在不同的时间范围内测试该策略。