KMeans – SMART Trading Strategies https://smarttradingstrategies.com Statistical and Mathematical Approach to Retail Trading Sat, 18 Dec 2021 10:40:07 +0000 en-US hourly 1 https://smarttradingstrategies.com/wp-content/uploads/2021/08/logo-150x150.png KMeans – SMART Trading Strategies https://smarttradingstrategies.com 32 32 Unsupervised Learning – K Means Clustering https://smarttradingstrategies.com/unsupervised-learning-k-means-clustering/ https://smarttradingstrategies.com/unsupervised-learning-k-means-clustering/#respond Sat, 18 Dec 2021 10:37:33 +0000 https://smarttradingstrategies.com/?p=892 How to use the resample() method
How to do an elbow plot
How to cluster stocks based on their weekly ATR
Backtest of a strategy that clusters the daily data of TSLA into 3 clusters and comparing their performances]]>
k-means clustering groups data points into a predetermined number of clusters based on their distances to the cluster centres. The algorithm requires us to first specify the desired number of clusters (k) and the centres of these clusters. Data points are then assigned to the cluster with the nearest centre.

In this tutorial, we’ll learn to cluster stocks based on their weekly ATR (average true range).

The code demonstrates the following concepts:

  • How to get DOW 30 stock tickers from Wikipedia
  • How to use the resample() method
  • How to do an elbow plot
  • How to cluster stocks based on their weekly ATR

It also backtests a strategy that clusters Tesla stock (TSLA) based on scaled volume (volume/20D average volume) and range ((high – low)/ATR).

The stock’s daily data is grouped into 3 clusters and the performances of the clusters are compared.

Link to Code (as HTML file)

I did not have time to check through the code in this file. Hence, there may be errors in some of the code.

KMeans.html

]]>
https://smarttradingstrategies.com/unsupervised-learning-k-means-clustering/feed/ 0