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

Pine Script代码报错:Mismatched input 'stopLossLongSaved'排查

问题定位

你遇到的语法报错核心原因是 Pine Script缩进规则不匹配,同时代码中还存在多处其他语法错误,具体问题点如下:

  • 缩进层级混乱:Pine Script和Python一样属于缩进敏感语言,同一代码块必须保持统一缩进。你编写的if strategy.position_size==0块内,stoplossShortSaved :=stopLossShort这一行缩进错误,既没有和上一行的多单逻辑保持同层级缩进,也没有正确归属到对应的条件块内,解析器读到这行时无法识别代码块边界,直接抛出你看到的行尾匹配错误。
  • plot语句存在多余右括号:所有plot()调用的末尾都多写了一个半角右括号,比如plot(dailyHigh, color=blue, line=2,title='DailyHigh'))末尾多了一个),会触发括号不匹配错误。
  • 函数参数使用错误:
    • strategy.entry()的开多/开空参数需要传入布尔值,你写的long=strategy.long、short=strategy.short属于错误写法,开多传long=true、开空传short=true即可。
    • plot()设置线宽的参数名是linewidth,不是line,你写的line=2参数名无法被识别。
    • 内置颜色需要加color.前缀,比如蓝色要写color.blue,直接写blue会被识别为未定义变量。
    • 计算K线周期变化的change()函数在Pine Script v5版本中归属ta命名空间,需要写为ta.change(),直接调用change()会报未定义函数错误。
  • 笔误:空单止盈的plot标题写为'Takeprofit Shor',少了末尾的字母t,正确写法应为'Takeprofit Short'。
修复后完整代码

以下为修正缩进、修复所有语法错误后的可运行代码,由于你贴出的片段中没有给出dailyHigh/dailylow/buyPoint/sellPoint/各止损止盈价位的计算逻辑,我在代码中保留了变量位置,你把自己之前计算这些价位的逻辑替换掉示例占位部分即可运行:

//@version=5
strategy("自定义交易策略", overlay=true, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=1000, commission_type=strategy.commission.percent, commission_value=0.1)

// --- 替换为你自己的价位计算逻辑:dailyHigh、dailylow、buyPoint、sellPoint、stopLossLong、takeprofitLong、stopLossShort、takeprofitShort 的计算代码 ---
// 以下为示例占位逻辑
dailyHigh = ta.highest(high, 20)
dailylow = ta.lowest(low, 20)
buyPoint = dailyHigh * 1.001
sellPoint = dailylow * 0.999
stopLossLong = buyPoint * 0.99
takeprofitLong = buyPoint * 1.02
stopLossShort = sellPoint * 1.01
takeprofitShort = sellPoint * 0.98
// --- 占位逻辑结束 ---

//New day calculation 
var float stopLossLongSaved = 0.0
var float takeprofitLongSaved= 0.0
var float stoplossShortSaved= 0.0
var float takeprofitShortSaved= 0.0
bool newDay = ta.change(time("D"))

if newDay
    if strategy.position_size == 0
        // 保存多单止损止盈,挂多单
        stopLossLongSaved := stopLossLong
        takeprofitLongSaved := takeprofitLong
        strategy.entry(id='Long', long=true, stop=buyPoint, oca_name='x', oca_type=strategy.oca.cancel)     
        // 保存空单止损止盈,挂空单(和多单逻辑同属空仓判断内层块,缩进保持一致)
        stoplossShortSaved := stopLossShort
        takeprofitShortSaved := takeprofitShort
        strategy.entry(id='Short', short=true, stop=sellPoint, oca_name='x', oca_type=strategy.oca.cancel)


//Exit after sl or tp is hit
strategy.exit(id='LongExit', from_entry='Long', limit=takeprofitLongSaved, stop=stopLossLongSaved)
strategy.exit(id='ShortExit', from_entry='Short', limit=takeprofitShortSaved, stop=stoplossShortSaved)

//Draw data to chart
plot(dailyHigh, color=color.blue, linewidth=2, title='DailyHigh')
plot(dailylow, color=color.red, linewidth=2, title='DailyLow')
plot(buyPoint, color=color.purple, title='Buypoint')
plot(sellPoint, color=color.yellow, title='Sellpoint')
plot(stopLossLong, color=color.red, title='Long Stop Loss')
plot(takeprofitLong, color=color.green, title='Takeprofit Long')
plot(takeprofitShort, color=color.red, title='Takeprofit Short')
plot(stopLossShort, color=color.orange, title='Stop Loss Short')
修复说明
  • 统一了条件块内的缩进:所有属于if strategy.position_size == 0块内的代码都保持相同缩进层级,空单相关的赋值和挂单逻辑与多单逻辑对齐,从根源解决了最初的语法解析报错。
  • 给所有全局变量加了显式类型声明,符合Pine Script v5的最佳实践,避免隐式类型推断导致的运行异常。
  • 修正了所有函数参数错误、括号冗余、颜色引用、命名空间引用问题,代码可以直接通过语法校验。
  • 修正了标题拼写错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 05:03:19