Skip to content

PhilipNJ/Stocks-nifty50-knn-clustering

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Clustering for Stock Data

We perform a K-means clustering for nifty50 stocks with an aim to diversify a portfolio with these bluechip stocks

Please note: This is not meant to be investment advice. Only for educational purposes

K-Means Clustering performed on Nifty50 stocks on the basis of price action from past 10 years

The following steps were followed:

1.     Using YFinance python package the information for the nifty50 tickers were extracted.

2.     New DataFrame created with daily close price, Annual returns and annual variance

3.     Inertia is calculated and based on elbow method number of clusters are calculated as 4 

image

4.     We then fit the KMeans algorithm and get the cluster labels

image

5.     A function is built to extract the stock with most returns in each cluster

image