Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Donchian Channel Breakout Strategy

Hi. I'm brand new to this community and the python programming, and excited to learn this. Is anyone using breakout strategy? I'm wondering if Donchian Channel Breakout trading strategy code available somewhere or someone could help with this. The trading algo is:

1) ADX should be rising.
2) Buy if the price breaks 55-day channel with stop loss at 20-day price channel.
3) Stop should be updated using 20-day channel as price moves up.
4) Liquidate everything if ADX is over 40 and ADX starts to go down.

Any help would be appreciated. Thanks!

9 responses

Well, it is easy enough to implement, but the performance is not great. It trades in little bursts and stays in cash too long - sometimes for years.

Wow. Thanks, Ray. This will help me learn the code a lot faster. BTW, when I said ADX should be rising, I meant ADX today > ADX previous day when channel breakout occurs. Just wondering if the code reflects what I intended because I think it should give more signals. Thanks for all your help!

I think I have accounted for the ADX rising. A condition that I buy is that this be positive:

    adx_change = adxs[context.trade_me] - context.last_adx[context.trade_me]  

Yes, that will do it. Looking at the log, it seems ADX and price info are different than the ones on other chart service. Do you know how I can see the actual price (high, low, open, close) that quantopian provides and adx values for each day? Thanks!

If you look at my code for guidance, you can use the "record" function to plot anything you would like and the "log" function to spit out any numbers you would like. The ADX can be set to use any time period. I used 14 days because you did not specify. You can access a bunch of things by inspecting the "data" dictionary inside of the handle_data function. It contains the following data on every security in your universe: datetime, price, open_price, close_price, high, low, volume

So back to my example, to spit out a chart with high and low for each day:

record(high=data[context.trade_me].high)  
record(low=data[context.trade_me].low)  

The data variable passed to the handle_data function will contain the info for the *previous bar in a backtest. The attached backtest demos the data that is available. You might also want to check out the help docs

*UPDATE: The data variable is the data from the previous bar (not the current bar). For example, if you're running a call of handle_data at 3:01PM, and have data[stock].close_price, it can't know the close_price until the end of the minute bar (ie 3:01:59). Instead it returns the close price of the previous minute bar at 3:00:59.

This is a pretty old posting, so hopefully I will still get a response. I am new to Python, so maybe that is what is causing my lack of results.
I cloned the first code above , by Ray Cathert. It produced an error, which had to do with an empty array.

Here's the fix I added

if adxs.empty:  
    ## if pd.isnull(adxs):

At that point the code compiled. However, when I run it , it never triggers any buys. What am I doing wrong?
New to this forum, so don't know if I should be posting code algo here, even though the only change is shown above.

Thanks

They have updated Pandas and that broke this algo. The fix is trivial:

Line 30 goes from:

if pd.isnull(adxs): to
if pd.isnull(adxs).all():

Attached is the latest backtest.

And here's one of my typical modular (over done) versions... I don't much care for the entry mechanism though. Maybe it'll work flipped over?