无法使用参数 'when'='long' 调用 'strategy.close'

Cannot call 'strategy.close' with argument 'when'='long'

首先让我说我是 Pine Script 的新手,我认为我的问题是微不足道的,但我没有发现太多。

举例来说,我想要制定一个策略,在 EMA25 超过 EMA200 时买入,在下跌时卖出,而不需要 limit stoploss

//@version=5
strategy("Pine Script EMA Strategy", overlay = true, initial_capital = 100, default_qty_value = 100, default_qty_type = strategy.percent_of_equity)

EMA25 = ta.ema(close, 25)
EMA200 = ta.ema(close, 200)

plot(EMA25, color=color.red, title="EMA25")
plot(EMA200, color=color.yellow, title="EMA200")

bool longPositionCondition = ta.crossover(EMA25, EMA200)
bool exitLong = ta.crossover(EMA200, EMA25)

if (exitLong)
    bool exitWorkAround = true
else
    bool exitWorkAround = false

if (longPositionCondition)
    strategy.entry("long", strategy.long)
    strategy.close("exit", "long", when = exitLong)

每当我尝试执行此代码时,我都会收到此错误:
Add to Chart operation failed, reason: line 20: Cannot call 'strategy.close' with argument 'when'='long'. An argument of 'literal string' type was used but a 'series bool' is expected

我已经研究这个问题一段时间了,但我仍然不知道我做错了什么,
它似乎在 when = 中要求一个 boolean 变量但是说它是 'when'='long' 据我所知这是 strategy.entry 的 id 所以它没有任何意义我。

您可以通过按住 Ctrl 并单击它来查看 strategy.close() 函数的签名。这是签名:

strategy.close(id, when, comment, qty, qty_percent, alert_message) → void

第一个参数是 id,即您要退出的订单的 ID(因此它必须与您在创建订单的 strategy.entry() 中指定的相同).第二个参数是 when,即退出应该发生的时间。您首先将 "long" 隐式传递给它,方法是将 "long" 字符串放在第二个位置,这就是脚本无法编译的原因。

这将编译:

if (longPositionCondition)
    strategy.entry("long", strategy.long)
    strategy.close("long", when = exitLong)