TradingView
millerrh
1 มกรา 2020 เวลา 4 นาฬิกา 22 นาที

Donchian Breakout Strategy 

Bitcoin / United States DollarCoinbase

คำอธิบาย

This strategy buys when the Donchian Channel is broken to the upside and uses the lower Donchian Channel line as a trailing stop. You can also choose to use a moving average as a filter to keep you out of trades that are counter trend.

You can also configure which dates you want to backtest, so you can see how this behaves over specific time frames and market cycles.

เอกสารเผยแพร่

Added the ability to select different lookback periods for upper and lower bands. Results seem to be better with higher numbers for the upper band and lower numbers (tighter stops) for the lower band.

เอกสารเผยแพร่

Made the following changes:
  • Update to Pine v5
  • Cleaned up code
  • Added a high time frame moving average filter (so you can choose to focus only on the strongest stocks with longer term uptrends).
  • Added a table showing the Average Daily Range (ADR - which is equivalent to the Monthly Volatility indicator in the stock screener) to help you see how much a stock moves around on average.
  • Added the ability to filter out trades that are outside of a user-defined percentage outside of the ADR to focus on better risk/reward trades.

เอกสารเผยแพร่

The old adage is: "Cut your losers quick and let your winners run." So I figured a good tweak to this strategy would be to use a tighter Donchian channel as your initial stop until you are in profit, then use a larger channel to give the trade more room to breathe after that. So now this is an option that you can chose (but not the default).

เอกสารเผยแพร่

I discovered that when you have calculated colors in a script, it disables the ability for the user to change any colors on anything (even non-calculated colors). The ADR table uses calculated colors, so to bring back the ability for the user to control the display better, I've brought user controls for the colors for everything else (Donchian Channels, Moving Averages). I also upped the initial capital to 100k so that trades will appear on expensive instruments and futures.

เอกสารเผยแพร่

Added a new filter: If a moving average is sloping downward, it can ignore breaks of the DC Channel.

Note, it has a bug currently - it remembers the value of the DC channel the previous time the filter was true and will make a trade based on this previous (higher) value instead of resetting. Publishing this buggy version to seek help with fixing this and to be able to reset this value when the filter turns false.

เอกสารเผยแพร่

  • Removed barmerge.lookahead_on from ADR calculation
  • Remove resting stop order once buy conditions are no longer met. Previously this was retaining old DC levels as stop orders even when the filters were used and price went below them.

เอกสารเผยแพร่

Accidentally left an unrelated indicator visible on the previous upload screen shot.

เอกสารเผยแพร่

Bug fix:
Previously, I had the Donchian channel set to the previous bar's value so that the current bar would react based on the historical bars' data. But it was causing some odd entries and exits that I didn't expect. Using the Replay feature, I realized that the historical bars don't actually update the Donchian Channel values for the current bar until it is finished (unlike in live markets where you'll see the Donchian Channel update in real time). So I've updated this code to refer to current value for the Donchian Channel instead of previous bar value. It should make the historical testing accurate to your settings now.

เอกสารเผยแพร่

Fixed a bug where choosing to take the trades on close was not showing up. This is because if you wait until the bar is closed before taking trades, it will never show up because the Donchian Channel is reset with the new high/low of that bar. This fix looks at the previous bars' values to determine if it breaks it or not.
ความคิดเห็น
franzb0
Hi. Wonderful script.
I'm trying to use the "Close" bar strategy instead of "Wick", but it seems to me that the script can never open a trade because the close price will never be above the current Donchian high. I think the "previous bar's value" fix effectively removed the ability to use close. Possible? I did a test by modifying these lines:
//Logic
dcUpper = ta.highest(high[1], dcPeriodHigh)
dcLower = ta.lowest(low[1], dcPeriodLow)
dcStop = ta.lowest(low[1], dcPeriod2Low)

Thus he manages to open trades also for "Close".
but I have no idea if this is the change causing the issue you mention in the Oct 23, 2022 release
millerrh
@Francesco_Marzolo, Thanks for pointing this out. This was a definitely a bug, likely caused by the change I made that you referenced. I fixed it by ensuring that when selecting Close (instead of Wick) it looks at the previous bars' data for DC channel and stop to determine if it broke it or not. When using "Wick" it leaves the DC channel as is because the way TV calculates it, it doesn't change until the candle closes, so it works correctly using the current candle's settings for DC and Stop. Please check out the update and let me know if it fixes things for you.
Dtrade16
Hello, thank you for this script and making it open source. I have a couple of questions, does the buy signal occur based on the high of the last 20 days and sell signal on the low of the last 10 days? And if so, how come on the MATIC/USDT pair on daily time frame, it buys on the second candle at the beginning of the price data? It also has a bunch of buys and sells like 2 weeks after that. Can you please advise why this is? I have not modified the parameters in any way. Thanks again.
millerrh
@Dtrade16, It looks like an issue when the price data is not complete. When the upper DC value is 0 (because it doesn't exist), it is buying on the next candle (because the price is greater than 0). You can see that after the beginning of the data when the Donchian boundaries start to appear it starts behaving correctly.
Dtrade16
@Dtrade16, Ok thanks for clarifying that. Do you know why this indicator doesn't seem to work that well on stocks? The cryptos I backtested it on it performed well. I know that this strategy is mainly designed for commodities but was wondering if people use it with stocks too?
millerrh
@Dtrade16, One thing to keep in mind that a good backtest on single stocks isn't necessarily correlated to a good backtest on a portfolio of stocks. This strategy by its nature holds on to winners much longer than the losers. What that means is that over time, your portfolio will have a bunch of winners in it and cycle in and out of losers. The winners being there for so long vs. the losers means that sometimes you will be fully invested and "miss" many of the losers because you are holding onto the winners. I really wish TradingView would allow backtesting on a portfolio basis to show some of this dynamic. I've had to use Amibroker for this and settled on some parameters that don't seem so great on individual stocks, but in the portfolio view, seem to do great.
CryptoZeeKay
I have a question please , is checking of every tick really necessary ?
millerrh
@CryptoZeeKay, I suppose you are referring to "calc_on_every_tick = true" being in the top part of the code? That is there to allow the ADR table to show up. I can't recall the reason why, but if you remove it, the table disappears.
CryptoZeeKay
@millerrh, Thanks it is a good information.
hbhuong39
wick crosses the lower band but exit long doesn't trigger. there seems to be some condition
เพิ่มเติม