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

解决TradingView策略无信号误报及单向重复警报问题

警报误触发排查与解决方案

需求

实现仅在做多时触发1次「Go Long」警报,需等待首次「Go Short」警报触发后,才允许再次触发「Go Long」警报。

尝试方案1及问题

//@version=5
strategy("Single Alert Strategy", overlay=true)

// Create flags to avoid multiple Long signals when already in Long and multiple Short signals when already in Short
isLong = false              // We initialize a value and give it false (we are not in a Long position)
isLong := nz(isLong[1])     // We check the previous value and assign the previous value to this variable (when no previous value exist it will become false)

isShort = false             // We initialize a value and give it false (we are not in a Short position)
isShort := nz(isShort[1])   // We check the previous value and assign the previous value to this variable (when no previous value exist it will become false)

// Long Short conditions
tot_rsi = ta.rsi(close, 14)
bullLevel = 30
bearLevel = 70

longCondition = ta.crossover(tot_rsi, bullLevel) and not isLong     // Go Long only if we are not already Long
shortCondition = ta.crossunder(tot_rsi, bearLevel) and not isShort  // Go Short only if we are not already Short

// Change the flags to avoid duplicate signals
if (longCondition)
    isLong := true
    isShort := false

if (shortCondition)
    isLong := false
    isShort := true

if longCondition
    strategy.entry('Long', strategy.long, alert_message = "GoLong")

if shortCondition
    strategy.entry('Short', strategy.short, alert_message = "GoShort")

// Plot the signals and compare them with the Alerts log
plotshape(series=longCondition, title="Long", text="Long", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=shortCondition, title="Short", text="Short", style=shape.triangledown, location=location.belowbar, color=color.red, size=size.small)

问题:信号指标绘制正常,但创建新警报后,在RSI为48.20(未达30的做多触发条件)时误触发了「GoLong」警报。

尝试方案2及问题

//@version=5
strategy("Single Alert Strategy", overlay=true, calc_on_every_tick = true)

noOrder = strategy.closedtrades == 0   // There is no previous order
openLong = strategy.position_size > 0  // We are going Long
openShort = strategy.position_size < 0 // We are going Short

// Long Short conditions
tot_rsi = ta.rsi(close, 14)
bullLevel = 30
bearLevel = 70

longCondition = ta.crossover(tot_rsi, bullLevel) and (openShort[1] or noOrder)  // Go Long only if the previous order was Short
shortCondition = ta.crossunder(tot_rsi, bearLevel) and (openLong[1] or noOrder) // Go Short only if the previous order was Long

if longCondition
    strategy.entry('Long', strategy.long, alert_message = "GoLong")

if shortCondition
    strategy.entry('Short', strategy.short, alert_message = "GoShort")

// Plot the signals and compare them with the Alerts log
plotshape(series=longCondition, title="Long", text="Long", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=shortCondition, title="Short", text="Short", style=shape.triangledown, location=location.belowbar, color=color.red, size=size.small)

问题:仍出现无信号指标绘制时触发警报的情况。

误触发原因分析

  1. 状态重置问题:方案1中isLong/isShort未用var声明,策略重启(如创建新警报)时变量会被重置为初始值,导致逻辑判断失效;方案2依赖的strategy.position_size在创建新警报时会重置为0,同样破坏交替逻辑。
  2. 历史bar扫描:TradingView创建警报时会默认扫描历史bar,若历史bar满足条件会直接触发警报,哪怕当前实时bar不满足。
  3. tick级计算干扰:方案2开启calc_on_every_tick=true后,tick级的RSI波动会导致信号判断反复切换,增加误触发概率。

有效解决方法

使用持久化状态变量+实时bar判断,严格控制多空信号交替逻辑,代码如下:

//@version=5
strategy("Alternating Alert Strategy", overlay=true, default_qty_type=strategy.cash, default_qty_value=1000, initial_capital=10000)

// 持久化状态变量,策略重启不会重置
var bool can_long = true  // 初始允许做多
var bool can_short = true // 初始允许做空

// RSI条件
tot_rsi = ta.rsi(close, 14)
bullLevel = 30
bearLevel = 70

// 仅在最新bar判断信号,避免历史bar触发
long_signal = ta.crossover(tot_rsi, bullLevel) and can_long and ta.barstate.islast
short_signal = ta.crossunder(tot_rsi, bearLevel) and can_short and ta.barstate.islast

// 触发信号后更新状态,实现交替逻辑
if long_signal
    strategy.entry("Long", strategy.long, alert_message="GoLong")
    can_long := false  // 触发做多后禁止再次做多
    can_short := true  // 允许做空

if short_signal
    strategy.entry("Short", strategy.short, alert_message="GoShort")
    can_short := false // 触发做空后禁止再次做空
    can_long := true   // 允许做多

// 绘制信号
plotshape(series=long_signal, title="Long", text="LONG", style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 0), size=size.small)
plotshape(series=short_signal, title="Short", text="SHORT", style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.small)

代码关键点说明

  • var关键字声明状态变量,确保策略重启(创建新警报)时状态不会丢失,维持交替逻辑。
  • ta.barstate.islast限制仅在最新实时bar判断信号,彻底避免历史bar扫描导致的误触发。
  • 信号触发后立即更新can_long/can_short标志,严格实现「做多→等待做空→再次做多」的交替逻辑。
  • 关闭calc_on_every_tick(默认关闭),避免tick级波动干扰信号判断,仅在bar收盘时确认信号,提升稳定性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 19:00:53