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

PineScript看涨三角旗策略回测显示“No Data”问题求助

看涨三角旗策略测试显示“No Data”问题排查

作为PineScript新手,我编写了一个简单的看涨三角旗策略,但TradingView策略测试控制台一直显示“No Data”。已经精简代码调试,怀疑是条件过于严苛,但添加简单价格判断语句也无效。以下是我的策略代码:

//@version=5
strategy("Bullish Pennant strategy", overlay=true)

// Define parameters
ema_period = 50

// Calculate the 50-day EMA of the closing price
ema = ta.ema(close, ema_period)

// Determine if there was a sharp increase in price over earliest 6 bars of our period
sharp_increase = close[14] > close[19]
peak_price = math.max(high[19], high[18], high[17], high[16], high[15], high[14])

// Determine if there was a retracement going down in price over the next 12 bars
downtrend = true
highest_price = peak_price
for i = 13 to 1
    highest_price := math.max(highest_price, high[i])
    if low[i] < ema or (high[i] > high[i+1] and (high[i] > peak_price or high[i] > highest_price))
        downtrend := false
        break

// Determine if there was a solid bullish bar
bullish_bar = close[0] > open[0]

// Generate the buy signal
if sharp_increase and downtrend and bullish_bar
    strategy.entry("Buy", strategy.long)

问题原因分析

  1. 循环执行逻辑错误
    Pine Script中for i = a to b是递增循环,当a > b时循环不会执行。代码里for i = 13 to 1,13大于1,这段循环完全没运行,downtrend始终保持初始值true,但结合其他条件后仍无法触发信号。正确写法应该用downto实现递减循环:for i = 13 downto 1。

  2. downtrend判断逻辑矛盾
    循环中highest_price := math.max(highest_price, high[i])会不断更新为当前最大值,导致后续high[i] > highest_price的判断永远为假——因为highest_price已经包含了当前high[i]的最大值。同时low[i] < ema的条件过于严格,只要回调期间有一根K线跌破EMA,就会直接将downtrend设为false,很难满足条件。

  3. sharp_increase条件缺乏幅度要求
    close[14] > close[19]仅判断收盘价上涨,没有涨幅限制,无法定义“急涨”形态,很多微小波动都会被纳入,但这不是导致“No Data”的核心原因。

修正后的代码示例

//@version=5
strategy("Bullish Pennant strategy", overlay=true)

// Define parameters
ema_period = 50
sharp_rise_pct = 0.05  // 急涨幅度要求:5%

// Calculate the 50-day EMA of the closing price
ema = ta.ema(close, ema_period)

// 定义急涨阶段:19到14 bar(共6根)涨幅超过设定比例
sharp_increase = close[14] / close[19] > 1 + sharp_rise_pct
peak_price = ta.highest(high, 6)[14]  // 用ta.highest简化峰值计算

// 定义回调阶段:13到1 bar(共13根)高点逐步降低,低点在EMA上方
downtrend = true
prev_high = peak_price
for i = 13 downto 1
    // 回调期间高点需低于前一个高点,且低点不跌破EMA
    if high[i] >= prev_high or low[i] < ema
        downtrend := false
        break
    prev_high := high[i]

// 触发信号的阳线条件
bullish_bar = close > open

// 生成买入信号
if sharp_increase and downtrend and bullish_bar
    strategy.entry("Buy", strategy.long)

修正说明

  • 把循环改为downto确保代码执行;
  • 用ta.highest简化峰值计算,避免手动写多个math.max;
  • 给急涨添加幅度要求,符合三角旗形态的前置条件;
  • 调整回调阶段的判断逻辑:要求高点逐步降低(收缩形态),且回调不跌破EMA,更贴合三角旗的技术特征。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 15:57:02