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

Pine Script v4三指标策略优化求助:非同时满足开平仓逻辑

基于Stoch、SMA、MACD的三指标策略优化方案(Pine Script v4)

核心逻辑设计

  • 入场规则:允许三个指标任意顺序依次满足,当最后一个指标达标时,若此时三个指标仍全部处于满足状态,则触发多单
  • 平仓规则:同理,三个平仓条件依次满足后,当全部条件同时生效时执行平仓
  • 用var变量跟踪每个指标的历史满足状态,确保触发时每个指标都曾达标过

完整可运行代码

//@version=4
strategy("三指标依次触发策略", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// 1. 计算核心指标
// 随机指标(Stochastic)
stochLength = 14
stochSmoothK = 3
stochSmoothD = 3
stochK = sma(stoch(close, high, low, stochLength), stochSmoothK)
stochD = sma(stochK, stochSmoothD)
// 移动平均线(SMA)
smaLength = 50
smaVal = sma(close, smaLength)
// MACD
macdFast = 12
macdSlow = 26
macdSignal = 9
[macdLine, signalLine, _] = macd(close, macdFast, macdSlow, macdSignal)

// 2. 定义单指标入场/平仓条件
// 多单入场条件
longStoch = stochK > stochD and stochK < 20  // Stoch金叉+超卖
longSma = close > smaVal                     // 价格站在SMA上方
longMacd = macdLine > signalLine             // MACD快线大于慢线

// 多单平仓条件
exitStoch = stochK < stochD and stochK > 80  // Stoch死叉+超买
exitSma = close < smaVal                     // 价格跌破SMA
exitMacd = macdLine < signalLine             // MACD快线小于慢线

// 3. 跟踪指标历史满足状态
var bool stochLongMet = false
var bool smaLongMet = false
var bool macdLongMet = false

var bool stochExitMet = false
var bool smaExitMet = false
var bool macdExitMet = false

// 更新标记:只要满足过一次,就保持为true
stochLongMet := stochLongMet or longStoch
smaLongMet := smaLongMet or longSma
macdLongMet := macdLongMet or longMacd

stochExitMet := stochExitMet or exitStoch
smaExitMet := smaExitMet or exitSma
macdExitMet := macdExitMet or exitMacd

// 4. 构建最终信号
enterLong = longStoch and longSma and longMacd and stochLongMet and smaLongMet and macdLongMet and strategy.position_size <= 0
exitLong = exitStoch and exitSma and exitMacd and stochExitMet and smaExitMet and macdExitMet and strategy.position_size > 0

// 5. 执行策略
if (enterLong)
    strategy.entry("Long", strategy.long)
    // 入场后重置标记,避免同一趋势重复触发
    stochLongMet := false
    smaLongMet := false
    macdLongMet := false

if (exitLong)
    strategy.close("Long")
    // 平仓后重置平仓标记
    stochExitMet := false
    smaExitMet := false
    macdExitMet := false

// 6. 可视化信号(修复plotshape不显示问题)
plotshape(series=enterLong, title="多单入场", location=location.belowbar, color=color.green, style=shape.labelup, text="多", textcolor=color.white, size=size.small)
plotshape(series=exitLong, title="平仓信号", location=location.abovebar, color=color.red, style=shape.labeldown, text="平", textcolor=color.white, size=size.small)

// 辅助指标展示
plot(smaVal, title="SMA", color=color.blue, linewidth=2)
plot(stochK, title="Stoch K", color=color.orange, linewidth=1)
plot(stochD, title="Stoch D", color=color.purple, linewidth=1)
plot(macdLine, title="MACD", color=color.red, linewidth=1)
plot(signalLine, title="Signal", color=color.green, linewidth=1)

关键细节说明

  1. 历史状态跟踪:var变量仅初始化一次,能记录指标是否曾满足过条件,确保触发时是“依次达标”而非“同时瞬间满足”
  2. 信号重置:入场/平仓后重置对应标记,避免同一轮趋势中重复触发无效信号
  3. plotshape修复方案:
    • 明确绑定series参数到信号变量,确保仅在信号触发时显示
    • 指定location参数让标签显示在K线上下,避免被K线本身遮挡
    • 设置高对比度的颜色和样式,确保可视化清晰可见
  4. 灵活性调整:若需修改单指标逻辑(比如Stoch阈值、SMA周期),直接修改对应变量即可

常见问题排查

  • plotshape不显示:
    • 打开数据窗口查看enterLong/exitLong的数值,确认信号是否真的触发过
    • 检查location参数是否与信号类型匹配(比如多单信号用location.belowbar)
    • 确保series参数正确绑定到信号变量,而非错误的条件表达式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 05:22:43