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

合并VWAP与RSI编写Pine Script策略时strategy.exit参数报错求助

问题解决方案

核心错误原因

  1. 脚本类型不匹配:使用indicator()声明指标脚本,但调用了strategy.entry()、strategy.exit()等策略专属函数,这类函数仅能在strategy()声明的策略脚本中运行。
  2. 函数参数错误:Pine Script v6中strategy.exit()不存在when或condition参数,需通过条件判断语句触发平仓操作。

修改后的完整可运行代码

//@version=6
strategy(title="VWAP and RSI Strategy", shorttitle="VWAP RSI Strategy", overlay=true, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// --- VWAP Code ---
// 注意:原v3代码为滚动30周期VWAP,若需复刻该逻辑请使用ta.sum(),当前代码为从图表起始至今的累积VWAP
cumulativePeriod = input.int(30, title="VWAP Period") 

typicalPrice = (high + low + close) / 3
typicalPriceVolume = typicalPrice * volume
// 若需滚动30周期VWAP,替换下方两行的ta.cum为ta.sum(..., cumulativePeriod)
cumulativeTypicalPriceVolume = ta.cum(typicalPriceVolume)
cumulativeVolume = ta.cum(volume)
vwapValue = cumulativeTypicalPriceVolume / cumulativeVolume

plot(vwapValue, title="VWAP", color=color.blue, linewidth=2)

// --- RSI Code ---
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
rsi = ta.rsi(rsiSourceInput, rsiLengthInput)

// Plot RSI on the main chart as well (if desired)
rsiPlot = plot(rsi, "RSI", color=color.purple)
rsiUpperBand = hline(70, "RSI Upper Band", color=color.red)
rsiLowerBand = hline(30, "RSI Lower Band", color=color.green)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")

// --- Strategy Conditions ---

// Buy Condition: Price crosses over VWAP and RSI ≤25
longCondition = ta.crossover(close, vwapValue) and rsi <= 25
if longCondition
    strategy.entry("Long", strategy.long)

// Short Condition: Price crosses below VWAP and RSI ≥65
shortCondition = ta.crossunder(close, vwapValue) and rsi >= 65
if shortCondition
    strategy.entry("Short", strategy.short)

// --- Strategy Exits ---
// 用条件判断触发平仓,替代原错误的strategy.exit参数
if rsi >= 30
    strategy.close("Long")  // 平多单

if rsi >= 70  // 注意:原做空平仓条件描述为RSI≥70,若需修正为RSI≤70请自行调整
    strategy.close("Short") // 平空单

// --- Additional Plots ---
plotshape(longCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(shortCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

关键修改说明

  1. 脚本类型修正:将开头的indicator()改为strategy(),并添加了可选的策略配置参数(初始资金、佣金),确保策略函数能正常运行。
  2. 平仓逻辑修正:移除了带错误参数的strategy.exit(),改用if语句判断RSI条件,调用strategy.close()直接平掉对应仓位,这是Pine Script中基于条件平仓的标准写法。
  3. VWAP逻辑提示:原v3代码的VWAP是滚动30周期计算,当前代码使用ta.cum()是从图表起始至今的累积VWAP,若需复刻原v3逻辑,需将ta.cum()替换为ta.sum(..., cumulativePeriod)。
  4. 平仓条件校验:原做空平仓条件描述为RSI≥70,若实际需求是平空单时RSI回落至70以下(如RSI≤70),请自行修改对应if语句的条件。

内容的提问来源于stack exchange,提问作者Mr.Code

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 16:42:03