I'm looking to use a cointegration test as part of a pairs trading strategy. The Johansen Test seems to have some good characteristics. Anyone have a working formula?
I'd implement one myself, but so far, I haven't found an explanation or existing implementation that doesn't cause my brain to hurt. :)
UPDATE: To explain more, QuantShare has a function for Correlation (how much two stocks generally move together) and for Covariance (how much two stocks generally move in the same direction), but not Cointegration (how likely it is for two stocks to revert back to a relative mean). From there you can generate some other variables, like the half-life of two stocks (how long it generally takes them to revert back to the mean) and then create some formulas to identify good pairs trade candidates.
I came across some a complete Johansen cointegration test GUI with source code. There are links below. If you use them, you'll have to cut and paste the whole thing. It looks like the Wayback Machine's URLs get mangled here in the forum.
Once downloaded, I believe the only two files you'll need are "MaxEigenData.cs" and "JohansenHelper.cs".
I was able to extract the source code just for the Johansen test itself, and I tried creating a new QuantShare function with it, but I couldn't clear all the errors and get it to compile. Until I gain more experience, this is beyond the level of what I can do with C#. If someone would like to give it a try, you not only have my blessing, you have my thanks as well.
Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.