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

为何我的TradingView策略无交易信号?测试全程未生成订单

策略无交易订单问题排查(TradingView Pine Script v5)

问题描述

TradingView中名为"4:4 S1"的策略在测试范围内完全未生成任何订单,代码如下:

//@version=5
strategy("4:4 S1", overlay=true)
// Indicator Inputs
fairValueGap = input(0.5, "Lux Algo Fair Value Gap")
cciLevel = input(100, "CCI Level")
cci = ta.cci(close, 20)
regressionLength = input(20, "Regression Length")
supertrendLength = input(10, "Supertrend Length")
stopLoss = input(4, "Stop Loss (pips)")
takeProfit = input(4, "Take Profit (pips)")
// Calculate Indicators
fairValue = ta.sma(close, 20) + fairValueGap
supertrendUp = ta.sma(high, supertrendLength) + fairValueGap
supertrendDown = ta.sma(low, supertrendLength) - fairValueGap
regressionMA = ta.linreg(close, regressionLength, 0)
// Define Long Entry Condition
longEntry = close > (fairValue) and close > supertrendUp and (regressionMA) > 0 and ta.cci(close, 20) > -100 and ta.cci(close, 20) < 100
// Define Short Entry Condition
shortEntry = close < (fairValue) and close < (supertrendDown) and (regressionMA) < 0 and ta.cci(close, 20) > -100 and ta.cci(close, 20) < 100
// Execute Trades
if longEntry
    strategy.entry("Long", strategy.long)
if shortEntry
    strategy.entry("Short", strategy.short)
// Set Stop Loss and Take Profit
stopLossPips = ta.valuewhen(longEntry, close, 0) - stopLoss * syminfo.mintick
takeProfitPips = ta.valuewhen(longEntry, close, 0) + takeProfit * syminfo.mintick
strategy.exit("Stop Loss/Take Profit", "Long", stop=stopLossPips, limit=takeProfitPips)
strategy.exit("Stop Loss/Take Profit", "Short", stop=stopLossPips, limit=takeProfitPips)
plotshape(longEntry, location=location.belowbar, size=size.normal, color=color.green, style=shape.triangleup)
plotshape(shortEntry, location=location.abovebar, size=size.normal, color=color.red, style=shape.triangledown)

核心问题分析

1. 入场条件过于严苛,触发概率极低

  • 多条件逻辑冲突:以多单为例,supertrendUp = ta.sma(high,10)+0.5,而fairValue = ta.sma(close,20)+0.5,由于SMA(high)本身大于SMA(close),因此supertrendUp必然高于fairValue,要求close同时超过这两个值,常规行情中几乎不可能触发。
  • CCI区间限制冗余:ta.cci(close,20) > -100 and ta.cci(close,20) < 100是CCI的常规区间,进一步压缩了入场触发的可能性,且代码中已定义cci变量却重复计算,无实际意义。

2. 止损止盈赋值错误,导致交易执行异常

  • 仅在多单入场时赋值:stopLossPips和takeProfitPips仅通过ta.valuewhen(longEntry, close, 0)获取多单触发时的价格,空单触发时这些变量要么保留旧值,要么为NaN,导致空单的止损止盈参数无效,即便空单入场条件满足,也会因exit参数异常无法完成交易。
  • 空单止损止盈方向错误:空单的止损应高于入场价,止盈应低于入场价,但当前代码直接复用多单的计算逻辑,方向完全相反。

3. 回归趋势判断逻辑错误

  • regressionMA = ta.linreg(close,20,0)返回的是线性回归的价格水平而非趋势方向,判断regressionMA > 0或<0毫无意义(除非品种价格为负)。若要判断趋势,应使用线性回归斜率,即ta.linreg(close, regressionLength, 1),斜率为正代表上涨趋势,负代表下跌趋势。

修正建议示例

//@version=5
strategy("4:4 S1 Fixed", overlay=true)
// Indicator Inputs
fairValueGap = input(0.5, "Lux Algo Fair Value Gap")
cciLevel = input(100, "CCI Level")
cci = ta.cci(close, 20)
regressionLength = input(20, "Regression Length")
supertrendLength = input(10, "Supertrend Length")
stopLoss = input(4, "Stop Loss (pips)")
takeProfit = input(4, "Take Profit (pips)")
// Calculate Indicators
fairValue = ta.sma(close, 20) + fairValueGap
supertrendUp = ta.sma(high, supertrendLength) + fairValueGap
supertrendDown = ta.sma(low, supertrendLength) - fairValueGap
// 修正:用线性回归斜率判断趋势
regressionSlope = ta.linreg(close, regressionLength, 1)
// 简化入场条件,调整逻辑冲突
longEntry = close > fairValue and regressionSlope > 0 and cci > -cciLevel
shortEntry = close < fairValue and regressionSlope < 0 and cci < cciLevel
// Execute Trades
if (longEntry)
    strategy.entry("Long", strategy.long)
if (shortEntry)
    strategy.entry("Short", strategy.short)
// 修正:分多空计算止损止盈
if (strategy.position_size > 0)
    longStop = strategy.entry_price - stopLoss * syminfo.mintick
    longLimit = strategy.entry_price + takeProfit * syminfo.mintick
    strategy.exit("Long Exit", "Long", stop=longStop, limit=longLimit)
if (strategy.position_size < 0)
    shortStop = strategy.entry_price + stopLoss * syminfo.mintick
    shortLimit = strategy.entry_price - takeProfit * syminfo.mintick
    strategy.exit("Short Exit", "Short", stop=shortStop, limit=shortLimit)
plotshape(longEntry, location=location.belowbar, size=size.normal, color=color.green, style=shape.triangleup)
plotshape(shortEntry, location=location.abovebar, size=size.normal, color=color.red, style=shape.triangledown)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:34:53