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

TradingView Pine Script策略报错求助:多错误排查修复指导

修复后的「Bull Trading 1 Minute Scalping Sys v3.0」策略代码
//@version=5
strategy("Bull Trading 1 Minute Scalping Sys v3.0", overlay=true)

// Indicator 1: Bull Trading 1 Minute Easy Scalping Sys v3.0
bullTradingColor = input(color.white, "Bull Trading Color")
bullTrading = false
// 修复重复调用ta.change的问题
closeChange = ta.change(close)
bullTradingCondition = closeChange > 0 ? 1 : (closeChange < 0 ? -1 : 0)
bullTrading := bullTradingCondition != 0 ? bullTradingCondition : bullTrading
longCondition = bullTrading and bullTradingCondition == 1
shortCondition = bullTrading and bullTradingCondition == -1

// Indicator 2: ADX and DI
adxPeriod = input(14, "ADX Period")
plusDM = ta.max(high - high[1], 0)
minusDM = ta.max(low[1] - low, 0)
// 先计算真实波幅tr,再做平滑处理
tr = ta.tr
smoothedTr = ta.rma(tr, adxPeriod)
// 修正ta.rma参数过多问题
plusDI = 100 * ta.rma(plusDM, adxPeriod) / smoothedTr
minusDI = 100 * ta.rma(minusDM, adxPeriod) / smoothedTr
// 用math.abs替代全局abs函数
dx = 100 * ta.rma(math.abs(plusDI - minusDI) / (plusDI + minusDI), adxPeriod)
adx = ta.rma(dx, adxPeriod)

whiteLineCrossesBelowOrange = ta.crossover(plusDI, minusDI)
whiteLineCrossesAboveOrange = ta.crossunder(plusDI, minusDI)

// Indicator 3: Candle Color
candleColor = close >= open ? color.white : color.red

// Indicator 4: Color Switch
lastColor = 0.0
colorSwitch = bullTradingCondition != 0
if colorSwitch
    lastColor := bullTradingCondition

// Indicator 5: ATR Stop Loss Finder
atrPeriod = input(14, "ATR Period")
atrMultiplier = input(2.0, "ATR Multiplier")
// 修正ta.atr参数过多问题
atr = ta.atr(atrPeriod)
stopLossPrice = lastColor > 0 ? close - atrMultiplier * atr : close + atrMultiplier * atr
takeProfitPrice = lastColor > 0 ? close + 1.5 * atr : close - 1.5 * atr

// Trading Logic
// 修正仓位判断逻辑,移除弃用的when参数
if longCondition and whiteLineCrossesAboveOrange and candleColor == color.white and strategy.position_size <= 0 and barstate.isconfirmed
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit Long", "Long", stop=stopLossPrice, limit=takeProfitPrice)
if shortCondition and whiteLineCrossesBelowOrange and candleColor == color.red and strategy.position_size >= 0 and barstate.isconfirmed
    strategy.entry("Short", strategy.short)
    strategy.exit("Exit Short", "Short", stop=stopLossPrice, limit=takeProfitPrice)
错误逐条修复说明
  • 错误1(10:52):ta.change在三元运算符中重复调用,Pine Script v5要求这类函数必须每次计算都被显式调用,不能嵌套在三元里重复执行。提前将ta.change(close)的结果存入变量closeChange,再用该变量做判断。
  • 错误2、3(17:17、18:18):ta.rma仅接受2个参数(数据源、周期),原代码错误传递了多余参数,直接使用ta.rma(plusDM, adxPeriod)和ta.rma(minusDM, adxPeriod)即可。
  • 错误4(19:13):未声明tr变量,真实波幅需先通过ta.tr计算,再用ta.rma平滑得到smoothedTr。
  • 错误5(22:19):Pine Script v5中没有全局abs函数,需使用math.abs替代。
  • 错误6(40:14):ta.atr在v5中仅需传入周期参数(默认使用high/low/close计算),原代码多传了参数,改为ta.atr(atrPeriod)。
  • 错误7(45:85):原代码not strategy.position_size > 0逻辑错误,且将布尔值与数值比较,修复为直接判断strategy.position_size <= 0(无多单仓位时开多)。
  • 错误8(46:5):when参数已被弃用,将barstate.isconfirmed加入if条件判断即可。
  • 错误9(49:83):同错误7,原代码not strategy.position_size < 0逻辑错误,修复为strategy.position_size >= 0(无空单仓位时开空)。
  • 错误10(50:5):同错误8,移除when参数,将barstate.isconfirmed加入if条件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:28:15