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

TradingView Pine脚本'if'输入不匹配报错及版本5解决方案

TradingView Pine脚本语法报错解决方法

问题描述

编写了如下基于@version=2的TradingView Pine策略脚本:

//@version=2

// Define Order-Block parameters
blockSize = 100
minProfit = 0.01
stopLoss = 0.02

// Entry rule
if close > highest(close, blockSize) + minProfit
    strategy.entry("Long", strategy.long) 
else if close < lowest(close, blockSize) - minProfit
    strategy.entry("Short", strategy.short)

// Exit rule
if strategy.position_size > 0 and close < highest(close, blockSize) * (1 - stopLoss)
    strategy.close("Long")
else if strategy.position_size < 0 and close > lowest(close, blockSize) * (1 + stopLoss)
    strategy.close("Short")

运行时触发报错:
mismatched input 'if' expecting 'end of line without line continuation'

排查过程

先后尝试咨询AI工具、查阅相关资料,甚至尝试移除代码中的冒号,均未能解决报错问题。

解决方案

将脚本开头的版本声明从//@version=2修改为//@version=5,即可解决该语法报错。

核心原因是Pine脚本v2与v5存在语法规则差异,v5对条件判断等语法的兼容性更强,能够正确解析上述代码结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 23:45:47