When you invest in stocks you want to get a high return, but a low risk. There's also going to be a few constraints. That sounds like a job for cvxpy.
The program that will optimise everything is listed below;
df_returns = df_stocks.set_index('Date').diff() mean_stock = df_returns.mean().values cov_stock = df_returns.cov().values x = cp.Variable(len(mean_stock)) stock_return = mean_stock * x stock_risk = cp.quad_form(x, cov_stock) p = 1 objective = cp.Maximize(stock_return - p * stock_risk) prob = cp.Problem(objective=objective) prob.solve(), x.value
Can you spot the issue at the end of the video though?
Feedback? See an issue? Something unclear? Feel free to mention it here.
If you want to be kept up to date, consider getting the newsletter.