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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

don't use limit

// Trade Logic
if (longCondition)
    strategy.entry("Long", strategy.long)

本文标签: Tradingview pine script strategy creating entry on wrong candleStack Overflow