Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
NYSE coeintegrated stocks R script

For those who want to test pair trading strategies, you may want to know what stocks to use ? Correlated or cointegrated stocks ? If you want to find correlated stocks, it can be done easily in Excel, but for cointegrated stocks it's more complicated ...
I compiled a list of all NYSE symbols, and then I ran a cointegration test, based on this R script: http://quanttrader.info/public/testForCoint.html

The script is still running, so I can't post the results here, but here is the R script I'm using:

http://pastebin.com/vSEePUMf

symbols.txt -> contains all the symbols you want to test for cointegration
output.txt -> will output the result under this format "From 2004-05-18 to 2012-08-31 FCCY and FCTY are 99 % cointegrated"

7 responses

@Greg, I love seeing the code for the screen. Thanks for sharing. I gave FCCY and FCTY a run through the GLD/GDX code I shared previously, and discovered that FCTY does not trade until 12/18/2008. I couldn't discern from the code how you specify the history of the stocks you are running through the script, but I think at a minimum your output is assuming a longer history than may exist.

It also occurred to me that you are running this screen on today's symbols, which introduces a survivorship bias to your results. While any given year in the quantopian history has about 6-8,000 stocks that trade, the ten year history has about 17,000.

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.

@fawce data come from Yahoo Finance and apparently FCTY does trade before 2008 (http://finance.yahoo.com/echarts?s=FCTY+Interactive#symbol=fcty;range=my;compare=;indicator=volume;charttype=area;crosshair=on;ohlcvalues=0;logscale=off;source=undefined;)
The script takes every data Yahoo Finance provides, a date range could be specified when fetching data.

@Greg. Hmm, this is an interesting case. Yahoo Finance goes back to 2004, but Google Finance and Quantopian agree that the stock started trading at the end of 2008. Here is the google chart.

@Greg: has your script finished? Would be great to share the results so that we could start experimenting with them.

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.

http://pastebin.com/Njsv7aJv
This is only the partial results, Yahoo Finance seems to limit the number of connexions as the script downloads each time all charts.
The script can be easily improved by caching the .csv files, then you could have the full results.

it sounds like you need access to an API that provides historical pricing data :)

@Vishal you're right ! It was just some "hacking" with the original script I found