Skip to content

techwithmatty/Optimal-Portfolio-Allocation-Using-Machine-Learning

Repository files navigation

Optimal Portfolio Allocation Using Machine Learning

Summary

This project combines unsupervised and supervised Machine Learning techniques with Financial Theory and Statistics, to build a diversified stock portfolio using stocks from the S&P500 Index.

Files

Data:

  • S&P_500_End_of_Year_2020_Fundamental_FINAL.xlsx

Software/Deliverables:

  • Constructing a Diversified Stock Portfolio using Machine Learning Techniques - FULL VERSION.ipynb
  • Classification Models - EVALUATION.ipynb
  • Clustering Algorithms - EVALUATION.ipynb
  • main.py

Specs

Runs on macOS Big Sur (Version 11.1) and Windows 10 (or later)

  • Possible Backwards Compatibility

Running Requirements

This project requires you to have the following tools installed:

After installing anaconda, install streamlit:

The following python libraries should be installed:

You should be able to install most, if not all the above libraries using 'pip install (LIBRARY_NAME)'

Instructions for files

Constructing a Diversified Stock Portfolio using Machine Learning Techniques - FULL VERSION.ipynb

  • Contains the main investigation carried out in this priject
  1. Save the 'S&P_500_End_of_Year_2020_Fundamental_FINAL.xlsx' file in a location of your choice
  2. Go to the second block of code where the pandas reads the excel file into a DataFrame and change the line of code so the filepath is directed to the location where you saved the excel file.
  3. Run the code

Clustering Algorithms - EVALUATION.ipynb

  • Contains the evaluation of the clusering algorithms
  1. Save the 'S&P_500_End_of_Year_2020_Fundamental_FINAL.xlsx' file in a location of your choice
  2. Go to the second block of code where the pandas reads the excel file into a DataFrame and change the line of code so the filepath is directed to the location where you saved the excel file.
  3. Run the code

Classification Models - EVALUATION.ipynb

  • Contains the evaluation of the classification models

main.py

  • Contains the user interface which demonstrates the practical use of our investigation
  • It simulates the 2021 trading year (from 04/01/2021 to 31/12/2021)
  • The data is from yfinance
  • The stocks determined from the 'Constructing a Diversified Stock Portfolio using Machine Learning Techniques - FULL VERSION.ipynb' have been used in this application
  1. Set up streamlit
  2. Open terminal and change the directory to the location where you downloaded 'main.py'
  3. Run 'streamlit run main.py'
  4. Input an initial amount on the sidebar (this has to be a minimum of 100 dollars)
  5. Select allocation strategy
  6. Click on the 'Generate Portfolio' button
  7. You should see the current date, current porfolio value, profit/loss (which is initially N/A) and a pie chart of the initial allocation.
  8. Click the 'Rebalance Portfolio' button and see the changes on the pie chart of the current holdings and scroll down to see a graph of the portfolio history.
  9. Click the 'Rebalance Portfolio' another 12 times to simulate through the whole year.
  10. Clear the cache before re-running the program.

About

This project investigated the use of machine learning techniques in building a diversified stock portfolio.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published