Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Naive pair trading AAPL/SBUX

I did some simple tests under Excel to check securities that are correlated (just using the CORREL formula), and I found out that Apple and Starbucks had a 0.98 correlation ! (I have no explanation for this, if someone have an idea ?)

So I tried to implement a simple pair trading strategy and apparently it works well until some point where it stops because of "MAXIMUM DAILY LOSS EXCEEDED".
How can I stop this ?

14 responses

@Greg, this is a really cool pair trade. Not sure why Apple and Starbucks correlate. The best hypothesis I can come up with is overlap in their customer demographics. It is really cool that you did some research and then tried verifying your findings with a simulation.

Max daily loss is a limit you set in the backtest request. If you want to give the algorithm a lot of leeway, you can set the max daily loss to 99%. At the moment, we don't support turning the loss limit off completely, purely because we thought having the limit in place would be useful. Would a 99% limit work, or do you want to let the algorithm run without any guards?

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

A nice feature request would be to have access to all the historical data in your database, so one could build a huge correlation matrix and then extract all the max correlations and then run similar simulations on all those pairs :)

Ernie Chan points out in a blog post (http://epchan.blogspot.com/2006/11/cointegration-is-not-same-as.html) that cointegration (and not correlation) is required for successful pairs trading. It is not clear that this is a pure pairs trading example. On Google Finance, when I plot AAPL and SBUX over the time period covered by the backtest, I see that both equities are ramping up significantly in price. Intuitively, it seems that a significant portion of the return might be from the the increase in prices over the backtest period.

In response to Vishal's post immediately above, is it at all feasible to screen the entire universe of equities for pairs trading opportunities? Or would this be a monumental computing task?

Grant - absolutely correct, we should be looking for cointegration (via the Dickey-Fuller test) and not correlation

I gave this pair a try with the code I wrote for the GLD/GDX pair - check out the results.

So, the obvious implication is that for Quantopian to be a useful backtesting/screening tool for pairs trading, users need to be able to apply statistical tests, including visualization of the results (time series, histograms, curve fits, etc.). Alternatively, there needs to be a way to download the data, so that the detailed analysis can be carried out offline. Admittedly, I haven't spent much time poking around the site, so maybe there are such tools?

Grant -> +1 my sentiments exactly. although I think an easier solution would be to give us an interface to do online analysis (say via ipython notebooks) and perhaps to even share the analysis in the same flavor as backtesting, so people can comment on the various preprocessing steps.

@Grant, you're right on. The toolkit we have today is focused on simulating a strategy that has already identified securities, rather than researching ideas. I think there are some clear steps for us to add the functionality you're describing - the first concrete move it to create daily bar historical data. We will definitely keep you posted. Please keep the feedback coming, it is great.

The reason of that kind of correlation value between AAPL & SBUX is inexistent.
Try to calc the correlation in the last month and you will see that is even negative.
:-) Maybe if you apply the algo to a pair with real factor of correlation you will get better results (maybe) :-)

@Alberto, you mean like say CVX/XOM? ;)

I am in no way an expert on this topic but it seems to me that this strategy is buggy. You are only reducing the size of Apple and leaving Starbucks on so basically the gain that you're seeing is just the effect of accumulating a position in Starbucks over a couple of years and holding it. I wasn't able to run the strategy as Quantopian gives me some internal error when I try to run it.

Hi @Gunnar, sorry for the runtime errors -- those were due to some api changes we made since this test was first shared. I've fixed the errors, but I didn't patch the logic for reducing the position. Want to try that?

thanks,
fawce

great algorithm, i haven't heard of the score method yet but it makes perfect sense to me!
however i don't understand why you chose such an unusual hedge ratio...does it involve some mean reversal logic or am i overseeing something?

Hahaha... I cracked up when I saw "AAPL/SBUX pair trade" ... I immediately thought it should be called the "douche index"