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

TradingView策略警报异常:无持仓时仍触发平仓警报求助

问题排查与修复方案

核心问题

你的代码触发无持仓平仓警报的原因是平仓条件shortside没有校验当前是否持有多头仓位:

  • 当crossunder(close, sma) and crossunder(close, vwap)满足时,哪怕没有持仓,if shortside分支也会执行alert(),导致误触发。
  • stopLoss在无持仓时,strategy.position_avg_price为0,close < stopLoss等价于close < 0(正常行情不会触发,但逻辑上存在漏洞)。

修复步骤

1. 给平仓条件添加持仓校验

修改shortside的定义,加入strategy.position_size > 0(判断当前持有多头仓位),确保只有持仓时才触发平仓逻辑:

// Close the long position if conditions are true
shortside = strategy.position_size > 0 and (crossunder(close, sma) and crossunder(close, vwap) or close < stopLoss)

2. 优化止损逻辑(可选但推荐)

无持仓时strategy.position_avg_price为0,计算的stopLoss没有意义,可以加入条件判断只在持仓时计算止损:

// Define the stop loss level (仅在持仓时计算)
stopLoss = strategy.position_size > 0 ? strategy.position_avg_price * (1 - stopLossPct/100) : na

3. 确保警报仅在有效动作时触发

原代码中alert()直接放在if分支里,哪怕strategy.close()没有实际执行(无持仓时),警报依然会触发。修复后,只有当持仓存在且平仓条件满足时,才会触发警报。

完整修复后的代码

//@version=4
strategy("ABC", initial_capital=100, currency="USD", default_qty_type=strategy.percent_of_equity, default_qty_value=100, pyramiding=0, commission_type=strategy.commission.percent, commission_value=0.1, process_orders_on_close=true)

// Define the input variables
smaPeriod = input(title="SMA Short", type=input.integer, defval=30)
smaPeriodLong = input(title="SMA Long", type=input.integer, defval = 220)
vwapPeriod = input(title="VWAP Period", type=input.integer, defval=14)
stopLossPct = input(title="Stop Loss %", type=input.float, defval=2.0)
ma_length = input(title="Vol. SMA Short", type=input.integer, defval=30)
ma_length_long = input(title="Vol. SMA Long", type=input.integer, defval=220)
volume_multiplier = input(title="Volume Multiplier", type=input.float, defval=1.0)
rsi_length = input(14, "RSI Length")
rsi_sma_length = input(30, "RSI SMA Length")

// Calculate the indicators
sma = sma(close, smaPeriod)
sma_long=sma(close, smaPeriodLong)
vwap = vwma(close, vwapPeriod)

ma_volume = sma(volume, ma_length)
volume_1 = volume[1]
ma_volume_1= sma(volume[1], ma_length)
ma_volume_long= sma(volume, ma_length_long)

rsi_value = rsi(close, rsi_length)
sma_value = sma(rsi_value, rsi_sma_length)

// Define the long condition
long_condition = rsi_value > sma_value * 1.1

// Define the price conditions
aboveVWAP = close > vwap
aboveSMA = close > sma
aboveSMA_Long = close > sma_long

// Define the volume conditions
volume_condition_1 = volume * volume_multiplier > ma_volume * 1.3
volume_condition_2 =  volume_1 > ma_volume_1 * 1.3
volume_condition_3 = volume > ma_volume_long * 1.3

// Define the stop loss level (仅在持仓时计算)
stopLoss = strategy.position_size > 0 ? strategy.position_avg_price * (1 - stopLossPct/100) : na

// Open a long position if conditions are true
longside = long_condition and volume_condition_1 and volume_condition_2 and volume_condition_3 and aboveSMA_Long and aboveSMA and aboveVWAP

// Close the long position if conditions are true (添加持仓校验)
shortside = strategy.position_size > 0 and (crossunder(close, sma) and crossunder(close, vwap) or close < stopLoss)

if longside
    strategy.entry("Long", strategy.long)
    alert("开仓警报") // 替换为你的警报内容
     
if shortside
    strategy.close("Long")
    alert("平仓警报") // 替换为你的警报内容

额外说明

  • strategy.position_size > 0表示当前持有多头仓位,若持有空头则为负数,这里只针对多头平仓,所以用这个判断。
  • 修复后,只有当确实持有多头仓位且平仓条件满足时,才会触发平仓动作和对应的警报,彻底解决无持仓时误触发的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 08:43:23