Pine Script V5中‘Undeclared identifier Short_Condition’错误求助
问题描述
从Pine Script V5官方手册复制警报脚本并修改适配自身策略,希望在策略入场、离场点触发警报,但运行时报错:Error at 58:36 Undeclared identifier 'Short_Condition'。移除警报脚本后,原策略可正常运行。
相关代码
//@version=5 strategy("Inside Bar Strategy", overlay=true) Stop_Buy_Perc = input.float(0.1, "止损买入比例") Stop_Loss_Perc = input.float(0.2, "止损比例") Take_Prof_Perc = input.float(0.4, "止盈比例") Risk = input.float(0.01, "每笔交易风险") Window = input.bool(true, "交易窗口开启") Inside_Bar = high[1] > high[0] and low[1] < low[0] Prev_Range = high[1] - low[1] Bullish = open[1] < close[1] Bearish = open[1] > close[1] // 关键价格水平计算 Long_Stop_Buy_Level = high[1] + Prev_Range * Stop_Buy_Perc Short_Stop_Buy_Level = low[1] - Prev_Range * Stop_Buy_Perc Long_Stop_Loss_Level = high[1] - Prev_Range * Stop_Loss_Perc Short_Stop_Loss_Level = low[1] + Prev_Range * Stop_Loss_Perc Long_Take_Prof_Level = high[1] + Prev_Range * Take_Prof_Perc Short_Take_Prof_Level = low[1] - Prev_Range * Take_Prof_Perc // 仓位计算 long_qty = math.floor(strategy.equity * Risk) short_qty = math.floor(strategy.equity * Risk) // -------------------------- 多单条件 --------------------------------// Long_Condition = Window and Inside_Bar and Bullish if Long_Condition strategy.cancel_all() strategy.close_all() strategy.entry('Bullish IB', strategy.long, stop=Long_Stop_Buy_Level, qty=long_qty) strategy.exit('Bullish Exit', 'Bullish IB', stop=Long_Stop_Loss_Level, limit=Long_Take_Prof_Level) // -------------------------- 空单条件 -------------------------------// Short_Condition = Window and Inside_Bar and Bearish if Short_Condition strategy.cancel_all() strategy.close_all() strategy.entry('Bearish IB', strategy.short, stop=Short_Stop_Buy_Level, qty=short_qty) strategy.exit('Bearish Exit', 'Bearish IB', stop=Short_Stop_Loss_Level, limit=Short_Take_Prof_Level) // -------------------------- 警报逻辑 --------------------------------// detectLongsInput = input.bool(true, "检测多单") detectShortsInput = input.bool(true, "检测空单") repaintInput = input.bool(false, "允许重绘") // 仅在输入允许的方向生成入场信号 enterLong = detectLongsInput and Long_Condition and (repaintInput or barstate.isconfirmed) exitLong = strategy.exit_id('Bullish Exit') enterShort = detectShortsInput and Short_Condition and (repaintInput or barstate.isconfirmed) exitShort = strategy.exit_id('Bearish Exit') // 触发警报 if enterLong alert("开多单") else if enterShort alert("开空单") if exitLong alert("平多单") else if exitShort alert("平空单!") // ----------------------------- 绘图 ------------------------------------// plotshape(Inside_Bar, style=shape.arrowdown, location=location.abovebar, color=color.new(color.purple, 0))
错误信息
Error at 58:36 Undeclared identifier 'Short_Condition'
解决方法
调整变量定义顺序:Pine Script是自上而下逐行执行的,原代码中
Short_Condition在警报逻辑之后才定义,导致编译器处理警报代码时找不到该变量。将Short_Condition的定义移到警报逻辑之前即可解决这个报错。移除冲突的
indicator()声明:原脚本是策略(strategy)类型,不能同时存在indicator()声明,否则会导致脚本类型冲突,直接删除这行代码。修正离场条件:原代码中
exitLong = (Long_Stop_Loss_Level or Long_Take_Prof_Level)是错误写法——这两个是价格数值,不是布尔条件。改用strategy.exit_id()来检测离场订单是否触发,或者通过strategy.position_size的变化判断离场。
补充说明
- 确保脚本开头有正确的
strategy()声明(原代码可能遗漏,否则原策略无法正常运行)。 - 所有用到的输入变量(如
Stop_Buy_Perc、Risk等)需提前用input定义,否则会触发未定义变量报错。
内容的提问来源于stack exchange,提问作者Mikey T
相关产品推荐
相关产品推荐

