Pinescript v5代码报错:多余输入')',期望无续行的行尾,求协助
解决Pine Script v5策略报错:"Extraneous input ')' expecting 'end of line without line continuation'"
错误原因
你代码里的问题在于错误地将if关键字直接作为strategy.entry()和strategy.close()的参数,这不符合Pine Script的语法规则。if不能直接放在函数参数列表中,正确的做法是把入场/平仓条件通过when参数传入,或者用if语句包裹整个策略函数调用。
修正方案1:使用when参数(简洁写法)
这是Pine Script策略中指定条件的标准方式,直接将判断逻辑传给函数的when参数:
// 多单入场:hist由负转正时开多 strategy.entry("Long", strategy.long, when = hist > 0 and nz(hist[1]) <= 0) // 多单平仓:hist由正转负时平多 strategy.close("Long", when = hist < 0 and nz(hist[1]) >= 0) // 空单入场:hist由正转负时开空 strategy.entry("Short", strategy.short, when = hist < 0 and nz(hist[1]) >= 0) // 空单平仓:hist由负转正时平空 strategy.close("Short", when = hist > 0 and nz(hist[1]) <= 0)
修正方案2:使用if语句包裹(可读性更强)
如果你的逻辑后续需要扩展,用if块包裹策略调用会更清晰:
// 多单入场逻辑 if hist > 0 and nz(hist[1]) <= 0 strategy.entry("Long", strategy.long) // 多单平仓逻辑 if hist < 0 and nz(hist[1]) >= 0 strategy.close("Long") // 空单入场逻辑 if hist < 0 and nz(hist[1]) >= 0 strategy.entry("Short", strategy.short) // 空单平仓逻辑 if hist > 0 and nz(hist[1]) <= 0 strategy.close("Short")
说明
两种写法功能完全一致,可根据自己的代码习惯选择。when参数写法更紧凑,适合简单条件;if包裹的方式在处理复杂嵌套条件时更易维护。
内容的提问来源于stack exchange,提问作者Alejandro
相关产品推荐
相关产品推荐

