Back to Community
MA trading system with ATR zones

This is a simple MA trading system using ATR zones to limit risk at the beginning of a trade and only triggering exits when the price is above the usual volatility. The use of the ATR filter fixes some of the whipsaws common in trend following systems. I tried this on a few other stocks and it seemed to work best only with AAPL, hopefully someone can improve upon my idea and make this work with a larger range of securities. The 2X leverage really helps boost performance in this one.

Clone Algorithm
239
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 53d73492ef386707411f7d39
There was a runtime error.
8 responses

Cool algo Abel, I took the liberty of porting it over to minute data. I used the talib functions instead of the built in ta transforms. The ta transforms have become buggy and are being depreciated in favor of talib, just an fyi.

Clone Algorithm
83
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 53d7cdc892d3ca072e5b5923
There was a runtime error.

So the talib library is the best way to do ta analysis on quantopian ? I had been playing around with ta.RSI(timeperiod=2) based on Larry Connor's work but RSI(2) always seems to return odd values and can't match the performance of other backtesting websites. Eg: Connors RSI 2 system on stockfetcher returned 50% last year, meanwhile the same algorithm ported to python and run on quantopian returns less than 15% for the year.

Should I be using talib instead ? Could this be causing that issue as well ?

Hi Abel,
If you are looking to use RSI in your algo, then I'd say that talib is your best option in Quantopian. You can also do your own Python implementation of the formula, but talib is optimized in C, so it will be more efficient than a custom function.

The ta functions are a wrapped implementation of talib, but as Quantopian has progressed, the wrapper has gotten buggy and less compatible with the new architecture. It is a much safer bet to let the talib maintainers make sure talib works correctly and allow it to be imported as a regular python module rather than maintaining a custom wrapper for the library.

There could be a few reasons for the discrepancies between the results from other platforms. Maybe the ta wrapper was giving incorrect results, or more commonly, the commission/slippage models are different, so the final fill prices and costs are not the same. Quantopian's default commission and slippage models are pretty conservative so results can have smaller returns than other platforms. It is probably better to have conservative estimates so there are no big surprises when you switch to a live trading situation.

David

Hello Abel, there are some talib.RSI() examples here. Or in a search engine for more discussion: rsi quantopian

On Quantopian currently in 2014 there some things to watch out for:
1. It allows borrowing by default (good for some, requiring extra code for others)
2. Percent Returns right now are calculated based on the GUI setting (a million in this case) and do not take borrowing into account.

In this version of the algo I added some cash tracking (and bumped one of your items for record() of cash since it is limited to five).
You could do the math for output/input for the real return (in my opinion) including the max borrowed $149 million as input.

And then, if by chance you might want to try trading just once per day while using history() which requires minute mode, could try something like:

def handle_data(context, data):  
    time = str(get_datetime().time())   # is your local time, look up astimezone timezone 'US/Eastern' to convert  
    if time != '15:24:00': # only trading at this time once a day  
        return

Clone Algorithm
42
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 53d923d1de2bec07392da370
There was a runtime error.

@Abel.. same code.. as yours.... how come its not working... on my end... thanks....

Runtime Errors

2 Warning Undefined name 'ta'
2 Error Runtime exception: NameError: name 'ta' is not defined
3 Warning Undefined name 'ta'
4 Warning Undefined name 'ta'
5 Warning Undefined name 'ta'
19 Warning Local variable 'aapl_reg' is assigned to but never used

Hi guys,

Just few quick questions on the code.

Firstly, you wrote:

order_target_percent(context.stock, 2.0)  

The "order_target_percent" method will actually order the stock of 200% cash value compared to your total portfolio, how is that possible?

Secondly, I cloned the code ran it on other assets. The results are way worse than the one with AAPL. Does that mean the algo itself has the overfitting bias? (like the choices of value for the fast MA, slow MA, ATR window, etc)

Thanks,
Shane

Hi Shane.

To test on a few stocks is not too valid - try using, say 10 or even 50, to get a more scientific sample. It may be that AAPL is an anomaly.

Secondly, the order_target_percent is set as 2. This uses something called leverage - you can borrow capital to trade with from a broker. Read more online for full details.

Hi Max,

Thanks for the reply, it makes sense. As of the leverage, I thought the argument passed through should be only between -1 to 1 and I was wrong.

Thanks!