This project is a Python implementation of Modern Portfolio Theory (MPT) to optimize a portfolio of stocks using historical data. The goal is to maximize the Sharpe ratio, which represents the risk-adjusted return of the portfolio.
Two separate methodologies are presented:
- using Scipy optimizer
- using a MonteCarlo simulation across 100,000 samples
- Fetch stock price data using
yfinancelibrary - Calculate daily returns, mean returns, and covariance matrix
- Optimize the portfolio by maximizing the Sharpe ratio
- Display individual stock performance, optimized portfolio weights, and overall portfolio performance
- numpy
- pandas
- yfinance
- datetime
- scipy
To install the dependencies, run:
pip install numpy pandas yfinance datetime scipy
- Define the stocks in your portfolio by modifying the
stockslist:
stocks = ['BHP.AX', 'BXB.AX', 'CSL.AX', 'MQG.AX', 'NAB.AX', 'RIO.AX', 'RMD.AX', 'SHL.AX', 'TCL.AX', 'WDS.AX', 'WTC.AX', 'TNE.AX', 'TLX.AX', 'APA.AX', 'TLS.AX']
- Set the time period for the analysis by modifying the
start_dateandend_datevariables:
end_date = datetime.datetime.now() start_date = end_date - datetime.timedelta(days=180)
- Run the script to fetch stock price data, calculate returns and optimize the portfolio:
python mpt_optimizer.py
- The script will display the individual stock performance, optimized portfolio weights, and overall portfolio performance, including expected return, volatility, and Sharpe ratio.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Please make sure to update tests as appropriate.
