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

PineScript 'strategy'输入语法错误,排查常见方案仍未解决

问题:Pine Script 出现 Syntax error at input 'strategy' 语法错误

问题详情

已尝试网络上的常见解决方案,条件块前也添加了制表符,但仍触发Syntax error at input 'strategy'语法错误。代码参考YouTube教程编写,教程中的代码可正常运行。

错误代码

import TradingView/ta/5

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arek1313

//@version=5
strategy("Simple Pullback Strategy",
     overlay=true,
     initial_capital = 50000,
     default_qty_type = strategy.percent_of_equity,
     default_qty_value = 100, //100% of balance invested on each trade
     commission_type = strategy.commission.cash_per_contract,
     commission_value = 0.005) //Interactive Brokers Rate 

//Get User Inpu
i_ma1          = input.int(title = "MA 1 Length", defval = 200, step = 10, group = "Strategy Parameters", tooltip = "Long-term MA")
i_ma2          = input.int(title = "MA 2 Length", defval = 10, step = 10, group = "Strategy Parameters", tooltip = "Short-term MA")
i_StopPercent  = input.float(title = "Stop Loss Percent", defval = 0.10, step = 0.1, group = "Strategy Parameters", tooltip = "Failsafe Stop Loss Percent Decline")
i_StartTime    = input.time(title = "Start Filter", defval = timestamp("01 Jan 1995 13:30 +0000"), group = "Time Filter", tooltip = "Start date & time to begin strategy")
i_EndTime      = input.time(title = "End Filter", defval = timestamp("1 Jan 2099 19:30 +0000"), group = "Time Filter", tooltip = "End date & time to stop searching")

//Get Indicator values
ma1 = ta.sma(close, i_ma1)
ma2 = ta.sma(close, i_ma2)

// Check Filter(s)
f_datefilter = time >= i_StartTime and time <= i_EndTime

//Check buy / sell conditions
var float buyPrice = 0

buyCondition   = close > ma1 and close < ma2 and strategy.position_size == 0 and f_datefilter
sellCondition  = close > ma2 and strategy.position_size > 0
stopDistance   = strategy.position_size > 0 ? ((buyPrice - close)/ close) : na
stopPrice      = strategy.position_size > 0 ? buyPrice - (buyPrice * i_StopPercent) : na
stopCondition  = strategy.position_size > 0 and stopDistance > i_StopPercent

// Enter positions 
if buyCondition
     strategy.entry(id="Long", direction=strategy.long)

if buyCondition[1]
     buyPrice := open

// Exit positions
if sellCondition or stopCondition
     strategy.close(id="Long", comment="Exit" + (stopCondition ? "SL=True" : ""))
     buyPrice :=na


// Draw pretty colours
plot(buyPrice, color=color.lime, style=plot.style_linebr)
plot(stopPrice, color=color.red, style=plot.style_linebr, offset=-1)

plot(ma1, color = color.blue)
plot(ma2, color = color.orange)

问题原因与修复方案

错误的核心是版本声明位置错误:在Pine Script v5中,//@version=5必须是代码的第一行有效语句(注释除外),你把import TradingView/ta/5放在了版本声明之前,导致解析器无法正确识别脚本版本,进而报错无法解析strategy关键词。

修复步骤:

  1. 将import TradingView/ta/5移动到//@version=5之后;
  2. 可选修正:注释//Get User Inpu拼写错误为//Get User Input(不影响语法,仅优化可读性)。

修正后的代码开头部分:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arek1313

//@version=5
import TradingView/ta/5

strategy("Simple Pullback Strategy",
     overlay=true,
     initial_capital = 50000,
     default_qty_type = strategy.percent_of_equity,
     default_qty_value = 100, //100% of balance invested on each trade
     commission_type = strategy.commission.cash_per_contract,
     commission_value = 0.005) //Interactive Brokers Rate 

调整后脚本即可正常识别版本并解析strategy声明,解决语法错误。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 16:02:10