admin管理员组文章数量:1123202
I'm experimenting with the Donchian channel breakout strategy and aiming to make an entry on the same candle when the current candle breaks above the previous Donchian upper band. I want to set the entry price to the previous Donchian upper band instead of the current candle's close. However, it's not working as expected. I added a plotshape to verify the logic, and it does show the shape on the correct candle, but the entry is happening on the wrong one.
What might be causing this issue?
My Code :
//@version=6
strategy("DC Strategy", overlay=true, commission_type=strategymission.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
Sample entry :
I'm experimenting with the Donchian channel breakout strategy and aiming to make an entry on the same candle when the current candle breaks above the previous Donchian upper band. I want to set the entry price to the previous Donchian upper band instead of the current candle's close. However, it's not working as expected. I added a plotshape to verify the logic, and it does show the shape on the correct candle, but the entry is happening on the wrong one.
What might be causing this issue?
My Code :
//@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
Sample entry :
Share Improve this question asked 7 hours ago acracr 1,74211 gold badges49 silver badges83 bronze badges1 Answer
Reset to default 0don't use limit
// Trade Logic
if (longCondition)
strategy.entry("Long", strategy.long)
本文标签: Tradingview pine script strategy creating entry on wrong candleStack Overflow
版权声明:本文标题:Tradingview pine script strategy creating entry on wrong candle - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736554407a1944557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论