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

Pine Script V6买卖条件惰性求值问题:参数修改后脚本停算

Pine Script V6策略修改SMA长度后停止交易的问题排查与解决

问题现象

将Pine Script V5策略转换为V6版本后,当把sma_1_length参数从11修改为36时,脚本仅完成前2笔交易就停止后续计算。拆分条件变量单独声明后,问题仍未解决。

相关代码

V5原策略代码

//@version=5
strategy("V5_long", precision = 2, overlay=true, default_qty_type=strategy.cash, default_qty_value=50, currency=currency.USD, initial_capital=50, commission_type =strategy.commission.percent, commission_value =0.2)

// Input RSI
rsi_1_length = input.int(defval=27, title="RSI_1 Length", minval=1, maxval=100, step=1, group ='RSI')
rsi_2_length = input.int(defval=77, title="RSI_2 Length", minval=1, maxval=100, step=1, group ='RSI')
cross_bull= input.int(51, minval=1, maxval=99, step=1, group ='RSI')
cross_bear= input.int(50, minval=1, maxval=99, step=1, group ='RSI')

//input MA
sma_1_length = input.int(defval=10, title="SMA_1 Length", minval=1, maxval=200, step=1, group ='Moving Averages')
ema_1_length = input.int(defval=16, title="EMA_1 Length", minval=1, maxval=200, step=1, group ='Moving Averages')

// calculate MA
sma_1 = ta.sma(close, sma_1_length)
ema_1 = ta.ema(close, ema_1_length)

// Calculate RSI
rsi_1 = ta.rsi(close, rsi_1_length)
rsi_2 = ta.rsi(close, rsi_2_length)

// Detect Cross Overs 
bullish_cross = ta.crossover(rsi_1, cross_bull) and ema_1 > sma_1 
bearish_cross = ta.crossunder(rsi_2, cross_bear)

// Entry and Exit Conditions
if bullish_cross and strategy.position_size == 0 and barstate.isconfirmed 
    strategy.entry(id = "buy", direction=strategy.long, comment="B")
if bearish_cross and strategy.position_size > 0 and barstate.isconfirmed
    strategy.close(id = "buy", comment="C")

V6修改版本1

// Detect Cross Overs 
bullish_cross_1 = ta.crossover(rsi_1, cross_bull) 
bullish_cross_2 = ema_1 > sma_1
bearish_cross = ta.crossunder(rsi_2, cross_bear)

// Entry and Exit Conditions
if bullish_cross_1 and bullish_cross_2 and barstate.isconfirmed and strategy.position_size == 0 
    strategy.entry(id = 'buy', direction = strategy.long, comment = 'B')
if bearish_cross and strategy.position_size > 0 and barstate.isconfirmed
    strategy.close(id = 'buy', comment = 'C')

V6修改版本2

// Detect Cross Overs
bullish_cross_1 = ta.crossover(rsi_1, cross_bull) 
bullish_cross_2 = ema_1 > sma_1
bearish_cross = ta.crossunder(rsi_2, cross_bear)
entry_long = bullish_cross_1 and bullish_cross_2

// Entry and Exit Conditions
if entry_long and barstate.isconfirmed and strategy.position_size == 0 
    strategy.entry(id = 'buy', direction = strategy.long, comment = 'B')
if bearish_cross and strategy.position_size > 0 and barstate.isconfirmed
    strategy.close(id = 'buy', comment = 'C')

问题根源分析

  1. 指标初始化问题:当SMA长度改为36时,ta.sma()需要36根历史K线才能生成有效数值,策略运行前期会返回na值。此时ema_1 > sma_1的条件会判定为false,导致后续没有入场信号触发。
  2. 资金耗尽:原策略初始资金仅50美元,每次用50美元现金下单,加上0.2%的佣金,两笔交易后剩余资金可能不足50美元,无法满足固定金额下单要求,即使满足入场条件也无法开仓。
  3. 条件触发频率变化:修改SMA长度后,ema_1 > sma_1的趋势判断逻辑改变,后续K线可能不再同时满足RSI上穿和均线多头的条件,自然没有新交易。

解决方案与修改代码

针对上述问题,给出以下修改后的完整V6策略代码:

//@version=6
strategy("V6_long", precision = 2, overlay=true, default_qty_type=strategy.cash, default_qty_value=50, currency=currency.USD, initial_capital=100, commission_type =strategy.commission.percent, commission_value =0.2)

// Input RSI
rsi_1_length = input.int(defval=27, title="RSI_1 Length", minval=1, maxval=100, step=1, group ='RSI')
rsi_2_length = input.int(defval=77, title="RSI_2 Length", minval=1, maxval=100, step=1, group ='RSI')
cross_bull= input.int(51, minval=1, maxval=99, step=1, group ='RSI')
cross_bear= input.int(50, minval=1, maxval=99, step=1, group ='RSI')

//input MA
sma_1_length = input.int(defval=10, title="SMA_1 Length", minval=1, maxval=200, step=1, group ='Moving Averages')
ema_1_length = input.int(defval=16, title="EMA_1 Length", minval=1, maxval=200, step=1, group ='Moving Averages')

// calculate MA
sma_1 = ta.sma(close, sma_1_length)
ema_1 = ta.ema(close, ema_1_length)

// Calculate RSI
rsi_1 = ta.rsi(close, rsi_1_length)
rsi_2 = ta.rsi(close, rsi_2_length)

// Detect Cross Overs 
bullish_cross_1 = ta.crossover(rsi_1, cross_bull) 
bullish_cross_2 = ema_1 > sma_1
bearish_cross = ta.crossunder(rsi_2, cross_bear)
// 加入指标有效性判断,排除na值影响
entry_long = bullish_cross_1 and bullish_cross_2 and not na(sma_1) and not na(ema_1)

// 调试标记:在图表上直观查看条件触发情况
plotshape(entry_long, title="Entry Signal", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, text="ENTRY", textcolor=color.white)
plotshape(bearish_cross, title="Exit Signal", location=location.abovebar, color=color.new(color.red, 0), style=shape.labeldown, text="EXIT", textcolor=color.white)

// Entry and Exit Conditions
if entry_long and barstate.isconfirmed and strategy.position_size == 0 
    strategy.entry(id = 'buy', direction = strategy.long, comment = 'B')
if bearish_cross and strategy.position_size > 0 and barstate.isconfirmed
    strategy.close(id = 'buy', comment = 'C')

修改说明

  • 提高初始资金至100美元,避免两次交易后资金不足以开新仓
  • 入场条件添加not na(sma_1) and not na(ema_1),确保指标已完成初始化
  • 加入plotshape调试标记,可在图表上直接观察入场/离场条件的触发情况,便于后续排查

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 10:09:59