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

TradingView Pine Script唐奇安通道策略入场蜡烛异常问题排查

唐奇安通道突破策略入场异常问题排查

我正在测试唐奇安通道(Donchian Channel)突破策略,目标是当前K线突破前一根唐奇安上轨时,在同一根K线以该上轨价格入场,而非当前K线收盘价。但实际效果不符合预期:添加plotshape验证逻辑显示信号在正确K线上,然而交易入场却发生在错误K线上。请排查问题原因。

我的代码

//@version=6
strategy("DC Strategy", overlay=true, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3, calc_on_every_tick=true, process_orders_on_close=false)

// Inputs for the Donchian Channel
upperPeriod = input.int(20, title="Upper Band Lookback Period", minval=1)
lowerPeriod = input.int(20, title="Lower Band Lookback Period", minval=1)

// Start and End Date inputs
startDate = input.time(timestamp("2018-01-01 00:00 +0000"), title="Start Date")
endDate = input.time(timestamp("2069-12-31 23:59 +0000"), title="End Date")

// Entry and Exit Type Input
entryExitType = input.string("Close", title="Entry/Exit Type", options=["Close", "Wick"])
exitBandType = input.string("Middle Band", title="Exit Band Type", options=["Middle Band", "Lower Band", "Initial Stop Loss"])


// Donchian Channel Calculations
upperBand = ta.highest(high, upperPeriod)
lowerBand = ta.lowest(low, lowerPeriod)
middleBand = (upperBand + lowerBand) / 2

// Plotting the Donchian Channel
plot(upperBand, color=color.green, title="Upper Band")
plot(lowerBand, color=color.red, title="Lower Band")
plot(middleBand, color=color.blue, title="Middle Band")


// Selected Exit Band
selectedExitBand = exitBandType == "Middle Band" ? middleBand : lowerBand

// Strategy Logic
useWick = entryExitType == "Wick"
priceAboveUpper = useWick ? high > upperBand[1] : close > upperBand[1]
priceBelowSelectedBand = useWick ? low < selectedExitBand : close < selectedExitBand

// Debugging - Plot signals for breakout and entry
plotshape(priceAboveUpper, style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 0), title="Breakout Signal")
plotshape(priceBelowSelectedBand, style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), title="Exit Signal")

// Entry and Exit Conditions
longCondition = priceAboveUpper and time >= startDate and time <= endDate
exitCondition = priceBelowSelectedBand

// Trade Logic
if (longCondition)
    strategy.entry("Long", strategy.long,limit = upperBand[1])

if (strategy.position_size > 0)
    strategy.exit("Exit Long", from_entry="Long", stop=selectedExitBand)  // Exit at the selected exit band

示例入场情况

入场示例1:
入场示例1

入场示例2:
入场示例2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 02:32:01