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
相关产品推荐
相关产品推荐

