Skip to content

C# Console Application: Asks for two files containing historical financial data in the same format as files from Yahoo Finance. Performs the two-step Engel-Granger Test for Cointegration and simulates profits of applying the Pairs Trading Strategy to these stocks. To Project further Includes code to conduct statistical inference and a Function t…

License

Notifications You must be signed in to change notification settings

Tobias-Mann/Pairs-Trading-Analyzer

Repository files navigation

Pairs-Trading-Analyzer

C# Console Application: Asks for two files containing historical financial data in the same format as files from Yahoo Finance. Performs the two-step Engel-Granger Test for Cointegration and simulates profits of applying the Pairs Trading Strategy to these stocks. To Project further Includes code to conduct statistical inference and a Function to perform the Augmented Dickey Fuller Test for stationarity of a timeseries, which is part of the Engel-Granger Test for cointegration.

Motivation for Trading Pairs Strategy

Generally stock pairs that might be strongly affected by the same economic conditions tend to perform well under this strategy. However, in theory any valuation relevant similarity between stocks could be used to apply the strategy, like similar structure of the balance sheet. The idea is to identify short term deviations from a long term relative valuation and try to arbitrage these deviations, by shorting the overvalued stock and at the same time going long on the undervalued stock. A nice side effect of this strategy is that at any given time, the portfolio is Hedged against market shocks, as there is always an equally balanced exposure to a short position. However, the simulation ignores any costs that would theoretically apply, such as interest on the borrowed stock for the short position, transaction cost and comissions and in the case of large trades, liquidity constrained gains. Insted the simulation assumes,that always the colsing price of the next day can be realized. However, using limit orders the so calculated returns could likely be outperformed!

Ideas for pairs to analyze:

  • American Prison stocks: GEO & CXW
  • Swiss Pharmaceutical Giants: Hoffmann-La Roche & Novartis
  • German Car Manufacturers: Daimler & Porsche
  • Investment Banks: Citi & J.P. Morgan

How to use the Program

Clone the repository to a local directory on your machine. Open the Program.cs file with Visual Studio. Ensure in the Nuget Manager that the package Math.Numerics is available. Otherwise, install/restore, the Nuget and run/build or debug the application. When the console asks for the files, copy/paste the paths referencing to the historical price data. According data can be downloaded from Yahoo finance. Any Data used must be a csv (comma seperated not by semicolon), that contains a date parseble in the first column and the closing price in the fith colum.

The csv files in Excel:

How to interpret the output

First, the program analyzes the two stocks for possible cointegration using the two step Engel-Granger approach (described further below). This indicates how profitable a pairs trading Strategy might be. The screenshot below shows what an example output of the analysis for the stock pair Novartis and Roche.

Secondly, returns of a pairs trading strategy are simulated, by default the simulation uses a 50-day moving average. Parameters in the code can be changed to simulate different moving averages or to replace the average with a static average, based on the entire sample. The latter is not recommendable as trading results might be significantly distorted. Depending on what stock is assigned first, the strategies returns are drastically different. The smoothing average yields more consistent results. However, retruns may still vary depending on the order in which the stocks are feeded into the program. This is due to the Heteroskedastic deviations in the relative valuations of two stocks. In other words, the strategy behaves as if, the stocks have a constant (or moving) relative valuation, where any deviations in the deviation are normally distributed around the estimated mean. However, in reality these deviations might be heavily skewed or auto-correllated, both causes the strategy to yield inconsistent returns depending on the order of inputs, as this deterines enumerator and denominator of the relative valuation.

The Screenshot below Illustrates the simulation Output for the stock pair Novartis and Roche. The output now might vary a little as the digit numbers have been restricted to 4 digits after the comma and dates are now shown without clock time.

Outlook

Later versions of this program might facilitate the analysis of multiple stocks, repeated trade simulations with different parameters and an inmproved treatment of the problem of heteroskedasticly distributed deviations by improving the estimator of the relative valuation. For instance, replacing the moving average model, with an SARIMA model of the relative valuation. For instance, the auto regressive character of such a model, might control for autocorrelation in the deviations and thus improve results. A better fitting model, that that yields more "up todate" estimates of the average value should not suffer as strongly from trends in the relative valuation. Also introducing a distinct upward standard error and a distinct downward standard error for calculating the z variable might reduce the effect of skewed deviations in the relative valuation of the stocks.

Validity

While no guarantee for any prediction made by the program is given, it should be noted that some unit tests were implemented and can be activated by setting the variable "perforUnitTests" to true. In particular the tests of the Augmented Dickey-Fuller and of the Standard Errors for the regression estimators might be of interest, since they are enherently relevant for testing cointegration. A possible extension to this would be to implement a test of the Engel-Granger functions, however, due to time limitations this has not yet been implemented.

About

C# Console Application: Asks for two files containing historical financial data in the same format as files from Yahoo Finance. Performs the two-step Engel-Granger Test for Cointegration and simulates profits of applying the Pairs Trading Strategy to these stocks. To Project further Includes code to conduct statistical inference and a Function t…

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages