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

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)

额外说明:

  1. 原代码中两个plot的title都设为"Slope",修正后分别命名为"20周期斜率"和"200周期斜率",方便在图表上区分
  2. 添加了仓位检查和平仓逻辑,避免同一方向重复开仓导致仓位叠加,更符合常规策略逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 14:45:59