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

TradingView策略无交易信号排查:连续绿烛多单代码问题

TradingView策略脚本无信号问题排查

在TradingView测试策略时,脚本无法识别任何符合条件的交易机会,但手动回测能发现大量符合规则的场景,现需要排查代码错误。

交易规则

  • 找到任意红烛后的第一根绿烛;
  • 找到该绿烛之后,高点高于它的第二根绿烛;
  • 找到高点比第二根绿烛高点高10点的绿烛,在该烛立即开多单;
  • 第一根绿烛、第二根绿烛与入场烛之间可存在其他绿烛,但不能出现红烛,否则条件失效;
  • 若出现红烛,条件立即失效。

现有策略代码

//@version=5
strategy("Green and Red Candles Strategy", overlay=true, calc_on_every_tick = true)

var int firstGreen = na
var int secondGreen = na
var int entryGreen = na

var float entryPrice = na

isGreen = close > open
isRed = close < open

firstGreenFound = false
secondGreenFound = false

if isGreen and isRed[1]
    firstGreen := bar_index
    firstGreenFound := true

// Find second green candle after firstGreen with higher high, and no red candles in between                                                                                            
if not na(firstGreen) and bar_index > firstGreen
    if isGreen and na(secondGreen)
        if high > high[firstGreen]
            secondGreen := bar_index
            secondGreenFound := true
            entryPrice := high[secondGreen] + 10

if not na(secondGreen) and bar_index > secondGreen
    if isGreen and na(entryGreen)
        if strategy.position_size < 1 and high > high[secondGreen] + 10
            entryGreen := bar_index
          
plotFirstGreen = plot(firstGreen, title='First Green', color=color.blue)
plotSecondGreen = plot(secondGreen, title='Second Green', color=color.green)
plotEntryGreen = plot(entryGreen, title='Entry Green', color=color.red)

if not na(entryGreen)
    // Check for red candles between firstGreen and entryGreen
    for i = firstGreen + 1 to entryGreen - 1
        if isRed[i]
            entryGreen := na
            break

if not na(entryGreen)
    strategy.entry("Long", strategy.long)
    
firstRed = close < open and close[1] > open[1]
secondRed = firstRed and close < close[1] and low < low[1]

    
if strategy.position_size > 0 and (secondRed and low < low[1] - 10)
    strategy.close("Long")

plotshape(firstGreenFound, color=color.green, style=shape.triangleup, location=location.belowbar)
plotshape(secondGreenFound, color=color.green, style=shape.triangleup, location=location.belowbar)

代码错误点分析

1. 红烛触发后未及时重置状态

代码中没有在红烛出现时立即重置firstGreen、secondGreen等核心状态变量,导致之前的无效状态被保留,即使后续出现符合条件的K线组合,也无法触发新的信号。

修复方案:在每根K线开头添加红烛检测,触发则重置所有状态:

// 新增:红烛出现时立即重置所有状态变量
if isRed
    firstGreen := na
    secondGreen := na
    entryGreen := na
    firstGreenFound := false
    secondGreenFound := false

2. 第二根绿烛的筛选未排除中间红烛

寻找第二根绿烛时,仅判断了bar_index > firstGreen和isGreen,但未校验从firstGreen到当前K线之间是否存在红烛,会导致错误标记无效的第二根绿烛。

修复方案:结合上述红烛重置逻辑,确保firstGreen有效时,中间没有出现过红烛,无需额外判断。

3. 入场前红烛校验逻辑存在漏洞

当前通过循环检查firstGreen到entryGreen之间的红烛,但如果entryGreen是firstGreen+1,循环范围会失效,跳过检查。且该检查仅在entryGreen赋值时执行一次,后续状态变化无法同步更新。

优化方案:利用红烛即时重置逻辑,确保从firstGreen到当前K线全程无红烛,无需事后循环检查,提升代码准确性和效率。

4. 状态标记变量未持久化

firstGreenFound和secondGreenFound未用var声明,每根K线都会被重置为false,导致plotshape无法持续显示标记,也不利于状态跟踪。

修复方案:将变量改为持久化声明:

var bool firstGreenFound = false
var bool secondGreenFound = false

5. 入场价格变量未被使用

代码中entryPrice被赋值但未在入场条件中使用,属于冗余逻辑,可删除或替换入场判断中的high[secondGreen] +10。

内容的提问来源于stack exchange,提问作者Knight of the Vale

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 20:13:27