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

如何修复TradingView中不生成交易信号的Pine Script唐奇安通道代码

Pine Script唐奇安通道策略无信号问题修复方案

核心问题原因

你当前代码无信号的核心原因是极值计算包含了当前K线,导致交叉判断逻辑永远无法触发:
ta.highest()、ta.lowest()函数默认包含当前K线的数据,你用来判断突破的hh(上轨)已经把当前K线的最高价纳入计算,当当前K线最高价突破过去周期极值时,hh的值会同步等于当前最高价,ta.crossover(high, hh)永远不会返回true。

修正点

  • 给所有极值计算添加偏移参数1,计算不包含当前K线的历史周期极值,符合唐奇安通道突破的标准逻辑
  • 补充平仓信号的可视化标记,方便调试
  • 新增默认资金、手续费等策略参数,符合回测常规配置

修正后完整代码

//@version=5
strategy("Donchian Channel", overlay=true, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1)

TOPLENGTH = input.int(defval=10,title='Top length',minval=5,maxval=100,step=1)
BOTTOMLENGTH = input.int(defval=10,title='Bottom length',minval=5,maxval=100,step=1)

midsellT = input.int(defval=10,title='midbuyT',minval=5,maxval=100,step=1)
midsellB = input.int(defval=10,title='midbuyB',minval=5,maxval=100,step=1)

midcoverT = input.int(defval=10,title='midshortT',minval=5,maxval=100,step=1)
midcoverB = input.int(defval=10,title='midshortB',minval=5,maxval=100,step=1)

// 修正:添加偏移1,排除当前K线计算历史极值
hh = ta.highest(high, TOPLENGTH, 1)
ll = ta.lowest(low, BOTTOMLENGTH, 1)

hhB = ta.highest(high, midsellT, 1)
llB = ta.lowest(low, midsellB, 1)

hhL = ta.highest(high, midcoverT, 1)
llL = ta.lowest(low, midcoverB, 1)

Top    = plot(hh, 'Upper Band', color = color.green)
Bottom = plot(ll, 'Lower Band', color = color.red)

midB = (hhB + llB) / 2
midL = (hhL + llL) / 2

midPlotB = plot(midB, 'BasisB', color = color.black)
midPlotS = plot(midL, 'BasisL', color = color.blue)

buy  = ta.crossover(high,hh)
sell = ta.crossunder(low,midB)
short= ta.crossunder(low,ll)
cover= ta.crossover(high,midL)

// 补充所有信号的可视化标记
plotshape(buy,style=shape.labelup,location=location.belowbar,color=color.green,title="buy",textcolor=color.black,text="buy", size=size.small)
plotshape(sell,style=shape.labeldown,location=location.abovebar,color=color.new(color.green, 0),title="sell",textcolor=color.white,text="sell", size=size.small)
plotshape(short,style=shape.labeldown,location=location.abovebar,color=color.red,title="short",textcolor=color.white,text="short", size=size.small)
plotshape(cover,style=shape.labelup,location=location.belowbar,color=color.new(color.red, 0),title="cover",textcolor=color.white,text="cover", size=size.small)

strategy.entry('long entry',strategy.long,when=buy)
strategy.close('long entry',when=sell)
strategy.entry('short entry',strategy.short,when=short)
strategy.close('short entry',when=cover)

测试说明

加载代码后如果信号较少,可以适当调小通道周期参数(比如把Top length、Bottom length改为5),在波动率较高的品种(比如加密货币、美股科技股)上测试,信号会更明显。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 20:06:07