Pine Script报错求助:'strategy'输入不匹配,期望无续行的行尾
Pine Script策略报错解决:'Mismatched input "strategy" expecting "end of line without line continuation"'
错误根源
- 缩进缺失:Pine Script v5要求
if语句后的执行代码必须缩进,否则会被识别为独立语句,引发语法冲突。你的strategy.entry、strategy.close等语句直接跟在if后,没有缩进,这是触发报错的核心原因。 - 只读变量赋值:
strategy.profits和strategy.losses是平台内置的只读变量,不能直接赋值修改。 - 订单语法错误:
strategy.order的参数格式不符合v5规范,且strategy.order_type、strategy.close_all的用法存在错误。
修复后的完整代码
// This source code is subject to the terms of the Mozilla Public License 2.0 // © vovulka //@version=5 strategy("My Strategy") // 初始化随机指标 stoch_osc = stoch(length = 14) // 买入规则:随机指标下穿50 if (stoch_osc.crossunder(50.0)) // 止损单入场,止损价用当前收盘价替代(无持仓时持仓均价为空,避免异常) strategy.entry("buy", strategy.long, stop = close * 1.01) // 自定义计算累计盈亏(替代对内置只读变量的非法赋值) var float total_profit = 0.0 var float total_loss = 0.0 if strategy.position_size != 0 current_pnl = strategy.position_size * (close - strategy.position_avg_price) if current_pnl > 0 total_profit := total_profit + current_pnl else total_loss := total_loss - current_pnl // 卖出规则:随机指标上穿50,平多单 if (stoch_osc.crossover(50.0)) strategy.close("buy") // 平仓离场规则:持有多单时,挂限价单离场 if (strategy.position_size > 0) strategy.exit("Exit Buy", "buy", limit = strategy.position_avg_price * 0.998)
关键修复说明
- 为所有
if语句后的执行代码添加缩进(4个空格),确保语法结构正确。 - 删除对
strategy.profits和strategy.losses的非法赋值,改用自定义变量计算累计盈亏。 - 用
strategy.exit替代错误的strategy.order写法,符合Pine Script v5订单管理逻辑;同时修正了入场止损单的价格来源,避免无持仓时的异常值。
内容的提问来源于stack exchange,提问作者Mr. MoonLion
相关产品推荐
相关产品推荐

