Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Augmented Dickey-Fuller (ADF) test, SPY & SH dollar-volume

I'm not sure how to interpret this yet (or even if it makes sense), but here's some analysis that may be useful. I lifted the ADF code from https://www.leinenbock.com/tag/adfuller/.

I'd like to run the code on minute-level data, but it effectively hangs. Any ideas?

Grant

8 responses

The same as above, but for a 180-day trailing window of daily dollar-volumes. --Grant

Hello Grant,

I tried the first one in minutely mode and got:

ValueError: array must not contain infs or NaNs  

at 4.8% complete around 05-Nov-2006.

P.

Grant,

I had added your code to a previous algo of mine (https://www.quantopian.com/posts/trading-strategy-statistical-arbitrage-and-mean-reversion) that only executes the strategy, which is a simple bollinger bands cross when the SPY and SHY are cointegrated (adf < 1 and p < 0.1). The original strategy is taken from here (http://gekkoquant.com/2013/01/21/statistical-arbitrage-trading-a-cointegrated-pair/). I don't think the results look too meaningful, partly due to the amount of orders.

I could think of performing cointegration tests on a universe of stocks and then executing a strategy based off which stocks are cointegrated

Seong

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.

I am not sure if this is the sort of algorithm that it makes sense to run within the backtester. I am also investigating cointegrated pairs and trips trading, but the actual stats code to select the pairs I have been working on in MATLAB offline/cloud.

Simon,

Are you referring to the idea of performing cointegration tests on a universe of stocks? Maybe it would be better to perform the tests offline and then import the pairs into a trading algo?

Here's the adfuller result, daily mode, with a trailing window of 252 days (1 trading year). Not sure what to make of it...there is an indication that on a dollar-volume basis, SPY & SH have been cointegrated for the past year or so. And the adfuller output seems to capture when things are kinda haywire in the market (see the p-value chart). There's been relative calm since early 2012. --Grant

Hello Simon,

I'd be interested in an outline of how you are carrying out the offline cointegration analysis using MATLAB. And why MATLAB rather than Python? Where are you getting the data? Do you use any form of high-performance computing (e.g. cluster/parallel/cloud/GPU)? What tests do you apply for determining cointegration? Etc.

I realize that some details you may need to keep confidential...I'm interested in the mechanics.

Grant

Here's the adfuller on SPY & SH, minute-level dollar-volume data, 5 day trailing window (5*390 minutes). It runs rather slowly, but eventually completes.

I don't know if this is at all useful, given that the analysis is on dollar-volume not price. Any ideas?

Grant