Pine Script报错:‘Syntax error at input 'strategy'’求助
解决Pine Script编译报错:Syntax error at input 'strategy'
你的代码触发语法错误的核心原因是使用了HTML转义字符"代替标准双引号",Pine Script解析器无法识别这种转义引号,导致无法正确解析strategy函数的参数。
修正步骤:
- 把代码中所有的
"替换为普通双引号" - (可选)修正重复的
plot标题,避免图例混淆 - (可选)添加策略平仓逻辑,防止同一方向重复开仓
修正后的完整代码:
//@version=5 strategy("Slope", overlay=false, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1) sma20 = ta.wma(close,20) sma200= ta.wma(close,200) slope20 = sma20-sma20[1] slope200 = sma200-sma200[1] angle20 = math.atan(slope20)*(180/math.pi) angle200 = math.atan(slope200)*(180/math.pi) plot(angle20 , title = "20周期斜率", color=color.blue) plot(angle200 , title = "200周期斜率", color=color.red) hline(0) // 平仓逻辑:持有多单时,若angle200转空则平仓 if (strategy.position_size > 0 and angle200 < 0) strategy.close("buy") // 平仓逻辑:持有空单时,若angle200转多则平仓 if (strategy.position_size < 0 and angle200 > 0) strategy.close("sell") // 开仓逻辑 if (angle200 > 0 and strategy.position_size <= 0) strategy.entry("buy", strategy.long) if (angle200 < 0 and strategy.position_size >= 0) strategy.entry("sell", strategy.short)
额外说明:
- 原代码中两个
plot的title都设为"Slope",修正后分别命名为"20周期斜率"和"200周期斜率",方便在图表上区分 - 添加了仓位检查和平仓逻辑,避免同一方向重复开仓导致仓位叠加,更符合常规策略逻辑
内容的提问来源于stack exchange,提问作者tututim
相关产品推荐
相关产品推荐

