Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy way to save the stock weights for all portfolios on the efficient frontier #532

Open
avrenli2 opened this issue May 1, 2023 · 1 comment
Labels
question Further information is requested

Comments

@avrenli2
Copy link

avrenli2 commented May 1, 2023

What are you trying to do?
I created the efficient frontier based on a group of stocks and plotted the efficient frontier from 200 points.
Is there an easy way to create a data frame that saves the stock weights for all portfolios on the plot?

What have you tried?
I have plotted the efficient frontier from 200 points as follows:

import yfinance as yf
import numpy as np
import pandas as pd
DATA = yf.download(["TSLA","SBUX","CAKE"], start="2018-1-1", end="2023-1-1", interval="1mo")
ADJUSTED = DATA["Adj Close"]
from pypfopt import EfficientFrontier, risk_models, expected_returns, plotting
EXP_RETS = expected_returns.mean_historical_return(ADJUSTED, frequency=1, compounding=False)
COV_MATRIX = risk_models.sample_cov(ADJUSTED , frequency=1)
EF1 = EfficientFrontier(EXP_RETS, COV_MATRIX, weight_bounds=(0,1))
FIG = plotting.plot_efficient_frontier(EF1, ef_param="return", points=200 , 
                                show_assets=False, showfig=True)
#Std. Deviation
LINE = FIG.lines[0]
LINE.get_xdata()
#Expected Return
LINE.get_ydata()
#Create a loop to save the stock weights as WEIGHTS
WEIGHTS =pd.DataFrame(columns=EF1.tickers)
for i in Y:
  EF1.efficient_return(target_return=i)
  ROW = pd.DataFrame.from_dict(data=[EF1.clean_weights()])
  WEIGHTS = pd.concat(objs=[WEIGHTS,ROW])

What data are you using?
I am downloading the data using yfinance.

@avrenli2 avrenli2 added the question Further information is requested label May 1, 2023
@avrenli2 avrenli2 changed the title Outputting the expected return, standard deviation and stock weights for all portfolios on the efficient frontier Easy way to saving the stock weights for all portfolios on the efficient frontier May 1, 2023
@avrenli2 avrenli2 changed the title Easy way to saving the stock weights for all portfolios on the efficient frontier Easy way to save the stock weights for all portfolios on the efficient frontier May 1, 2023
@88d52bdba0366127fffca9dfa93895
Copy link
Collaborator

This question is useful.

Yes, your approach seems correct, but maybe it’s not an “easy way”. We still don’t support this function, but we plan to have it in the next major version. If you have any more questions or suggestions for the next version, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants