└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Cluster-Analysis-Machine-Learning-for-Pairs-Trading 2 | Algotrading101 blog 3 | 4 | What trading assets work the best together to form a trading pair for the Pairs Trading strategy? 5 | 6 | Pairs trading is a strategy in which a trader buys one asset while shorting another. The main premise of the trade is that when the two pairs diverge, they will likely converge again resulting in profit for the trader. 7 | 8 | I categorized this problem as a clustering unsupervised machine learning task. 9 | 10 | The structure of the code and the problem-solving is made up of the following steps: 11 | 12 | 1. Obtain all the S&P 500 stock from Yahoo Finance 13 | 2. Clean and prepare the data (treat missing values, calculate volatility, scale the date, etc.) 14 | 3. Fit three Machine Learning Models to the data and explore their outputs 15 | a) k-Means Clustering 16 | b) Hierarchical Clustering 17 | c) Affinity Propagation Clustering 18 | 4. Compare the models by their silhouette score and pick the best one 19 | 6. Extract the Trading Pairs 20 | a) Do a cointegration test on clusters to find the best pairs 21 | b) Plot the Pairs 22 | 23 | For a more detailed Guide behind the process please visit the Algotrading101 blog post: https://algotrading101.com/learn/cluster-analysis-guide/ 24 | 25 | ![download (6)](https://user-images.githubusercontent.com/74266147/112733578-0dacb280-8f41-11eb-91ac-456f8245d065.png) 26 | 27 | ----------- 28 | Info 29 | ----------- 30 | 31 | | Author | Igor Radovanovic 32 | --- | --- 33 | | Published | May 14, 2021 34 | --------------------------------------------------------------------------------