Python code to select a portfolio of stock (data directly grab from Yahoo Finance) and find the composition who optimizes both expected return and volatility Library to use :
-numpy -pandas -matplotlib.pyplot -pandas_datareader -scipy.stats -yfinance
For this code we will use the Modern Portfolio Theory based on H.Markowitz hypothesis (1952). For simplify the calculation we will use this formula of the Sharpe ratio : where we supposed that risk free rate equal to 0 :
At the end we will have a plot of the x combinaisons calculated by Python with the optimal portfolio :