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

如何在Pine Script v5中过滤条件变更前首个多空信号外的后续信号?

Pine Script v5 信号过滤方案:仅保留方向切换后的首个信号

要实现"过滤掉条件变化前首个信号之后的所有后续信号",核心是跟踪上一次有效信号的方向,只允许与上一次方向不同的信号触发(或首次信号),同方向的重复信号直接过滤。

修改后的完整代码

//@version=5
indicator("过滤重复方向信号的箭头指标", overlay=true)

// 替换为你原有代码中的实际计算逻辑
up_sequence = ta.barssince(close > open) 
upDot = close > open 
down_sequence = ta.barssince(close < open) 
downDot = close < open 
filterLong = true 
filterShort = true 

// --- 新增信号方向跟踪逻辑 ---
var int lastSignalDir = 0 // 0=无信号, 1=多单信号, -1=空单信号

// 修改信号条件:仅当当前信号方向与上一次不同,或首次信号时才触发
upArrow = up_sequence[1] and upDot[1] and not downDot[1] and not down_sequence[2] and filterLong and lastSignalDir != 1
downArrow = down_sequence[1] and downDot[1] and not upDot[1] and not up_sequence[2] and filterShort and lastSignalDir != -1

// 更新上一次信号方向:有有效信号时记录当前方向
if upArrow
    lastSignalDir := 1
elif downArrow
    lastSignalDir := -1

anyArrow = upArrow or downArrow

// 绘图与警报
plotchar(upArrow, "", "↑", location.belowbar, size=size.small, color=color.green)
plotchar(downArrow, "", "↓", location.abovebar, size=size.small, color=color.red)
alertcondition(upArrow, "Long", "GoLong")
alertcondition(downArrow, "Short", "GoShort")
alertcondition(anyArrow, "Any direction", "GoAnywhere")

关键逻辑说明

  • var int lastSignalDir = 0:用var关键字初始化一个仅脚本加载时执行一次的变量,存储上一次有效信号的方向。
  • 信号条件新增lastSignalDir != 1(多单)和lastSignalDir != -1(空单):确保只有当前信号方向与上一次不同时,才会触发新信号。
  • 信号触发后更新lastSignalDir:记录当前有效信号的方向,后续同方向重复信号会被过滤,直到出现相反方向信号重置状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 02:01:26