This is a code exploring a statistical arbitrage strategy to trade crypto pairs.
First, the code (cointegration_analsis.py) analyzes the performance of 8 cryptocurrencies from 1-1-2020 to 5-31-2020 with respect to their intial values:

Then, the code tests for cointegration of the cryptocurrencies against each other.
Cointegration implies that the ratio of two stationary time series will vary around a mean. This is the basis for trading pairs. For example, if BTC and ETH are highly cointegrated, and BTC diverts up from the mean while ETH diverts down from the mean, we can go long on ETH and short on BTC because they will revert back.
The code finds the p values of each pair, and plots a heat map that allows us to pick highly cointegrated pairs for trading:
We can see that the best trades are BTC-ETH, BTC-LTC, and BTC-XRP
We can calculate the z-scores for these pairs (how many standard deviations the asset has moved from the mean) and place trades accordingly.
Sharpe Ratio Table (> 1 is ideal)
| BTC-ETH | BTC-LTC | BTC-XRP | |
|---|---|---|---|
| Training Set | 4.744124 | 3.182864 | 3.630110 |
| Testing Set | 1.158460 | -1.808723 | 2.060752 |
Cumulative PNL:

