You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TradingView PineScript: strategy.close与exit注释同步及编译错误求助

解决方案

1. 编译错误修复

错误根源是**strategy.close()函数在Pine Script中没有comment参数**,你调用时额外传入了该参数,不符合函数的重载定义,导致编译失败。

直接移除strategy.close()中的comment参数即可修复,修正后的基础代码如下:

// === STRATEGY - LONG POSITION EXECUTION ===
strategy.entry(id = "Buy", long = true, when = buy[shunt]==1, comment = "ENTER-LONG_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c621" )

// 移除comment参数,修复编译错误
strategy.close(id = "Buy", when = direction[shunt]!=direction[shunt+1])

// === STRATEGY - SHORT POSITION EXECUTION ===
strategy.entry(id = "Sell", long = false, when = sell[shunt]==1, comment = "ENTER-SHORT_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c621")

// 移除comment参数,修复编译错误
strategy.close(id = "Sell", when = direction[shunt]!=direction[shunt+1])

// === STRATEGY RISK MANAGEMENT EXECUTION ===
strategy.exit("Exit Buy", from_entry = "Buy", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset, comment = "EXIT-LONG_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c6c021")

strategy.exit("Exit Sell", from_entry = "Sell", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset, comment = "EXIT-SHORT_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c6c021")

2. 统一strategy.close与strategy.exit的注释(适配Alert功能)

由于strategy.close本身不支持comment参数,推荐采用以下两种方案实现注释统一:

方案1:用变量管理注释+自定义Alert消息

  1. 先定义统一的退出注释变量:
// 定义多空退出的统一注释变量
longExitComment = "EXIT-LONG_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c6c021"
shortExitComment = "EXIT-SHORT_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c6c021"
  1. 将变量代入strategy.exit的comment参数:
strategy.exit("Exit Buy", from_entry = "Buy", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset, comment = longExitComment)
strategy.exit("Exit Sell", from_entry = "Sell", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset, comment = shortExitComment)
  1. 为strategy.close的触发条件创建自定义Alert:
    在TradingView的Alert设置中,选择你的策略,触发条件选**"Strategy order fill"**,在"Message"框中直接填入对应注释变量值;或者在策略内用alertcondition提前绑定:
// 多仓平仓触发Alert
alertcondition(condition = strategy.position_size > 0 and direction[shunt]!=direction[shunt+1], title = "Long Close", message = longExitComment)
// 空仓平仓触发Alert
alertcondition(condition = strategy.position_size < 0 and direction[shunt]!=direction[shunt+1], title = "Short Close", message = shortExitComment)

方案2:用strategy.exit替代strategy.close(推荐)

直接用strategy.exit实现单纯平仓逻辑(不设置止盈止损),这样可以直接复用注释变量,无需额外配置Alert:

// 定义统一的退出注释变量
longExitComment = "EXIT-LONG_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c6c021"
shortExitComment = "EXIT-SHORT_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c6c021"

// === STRATEGY - LONG POSITION EXECUTION ===
strategy.entry(id = "Buy", long = true, when = buy[shunt]==1, comment = "ENTER-LONG_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c621" )

// 用strategy.exit替代strategy.close,复用注释变量
strategy.exit("Close Buy", from_entry = "Buy", when = direction[shunt]!=direction[shunt+1], comment = longExitComment)

// === STRATEGY - SHORT POSITION EXECUTION ===
strategy.entry(id = "Sell", long = false, when = sell[shunt]==1, comment = "ENTER-SHORT_BINANCE-FUTURES_ETHBUSD_ETH_30M_9c621")

// 用strategy.exit替代strategy.close,复用注释变量
strategy.exit("Close Sell", from_entry = "Sell", when = direction[shunt]!=direction[shunt+1], comment = shortExitComment)

// === STRATEGY RISK MANAGEMENT EXECUTION ===
strategy.exit("Exit Buy", from_entry = "Buy", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset, comment = longExitComment)

strategy.exit("Exit Sell", from_entry = "Sell", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset, comment = shortExitComment)

该方案可让所有退出动作(手动平仓、止盈止损平仓)使用统一注释,完美适配Alert的消息输出需求。

内容的提问来源于stack exchange,提问作者Racina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 21:45:29