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 :
Your priceAboveUpper
condition is unnecessary. You are adding a condition to place your order. And in that condition, you are checking if there is a break. If you do it like that, you will miss your desired entry because your order will be placed when the bar is closed (already after the fact that the level is broken).
Remove your condition and use a stop order.
timeCondition = time >= startDate and time <= endDate
if (timeCondition)
strategy.entry("Long", strategy.long, stop = upperBand[1])