-
Notifications
You must be signed in to change notification settings - Fork 1
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
Hey! Need some help with Optimize script #1
Comments
I tried to create a for loop that looks like this: |
Sorry for the flood of info but I thought this might be helpful in case any one else encounters similar problems. I figured out some of my problems above, though i'll leave the text above in case some one also needs a work around for the data variable problem. I was able to fix the problem in "print_allocation" by passing the variables "print_allocation(train, solution, data[ticker, 'Close'].iloc[-1]) which does now print all the share info as necessary. This also requires changing the last division term in "def print_allocation" from "prices[data.columns[ticker_id]" to just "prices". But i'm not sure if this is contributing to the issue below. It doesn't seem to work unless i change this though. But here are is the next problem. Here are the results when I got it working. AAPL - 100.0000, $42928.00 USD, 1963.77 shares Why would it allocate everything to one asset? I also noticed when the solution was generated, instead of having multiple orange solution markers across the frontier as in your image, my graph only shows 1 identified solution. Is this a problem with my asset choices? or something about how the algo is assessing them? Any insights would be much appreciated! :) |
Here is the full code so you can see what is producing this result in comment above, I believe I have implemented everything exactly as possible per the script with a few modifications: i took out plotting for brevity PERIOD = '10y' apple = yf.Ticker("AAPL").history(period=PERIOD)['Close'] brkb = yf.Ticker("BRK-B").history(period=PERIOD)['Close'] gs = yf.Ticker("GS").history(period=PERIOD)['Close'] jpm = yf.Ticker("JPM").history(period=PERIOD)['Close'] rvlv = yf.Ticker("RVLV").history(period=PERIOD)['Close'] dataDF = pd.DataFrame(index=aaple.index, columns=["AAPL","BRK-B","GS","JPM","RVLV"]) dataDF["AAPL"] = appleDF['dailyreturn'] dataDFprices = pd.DataFrame(index=aaple.index, columns=["AAPL","BRK-B","GS","JPM","RVLV"]) YEAR_BARS = 252 train = dataDF[(dataDF.index < TRAIN_END_DATE)].fillna(0) def random_population(n_assets, population_size): def annualized_portfolio_return(returns, weights): def annualized_portfolio_volatility(returns, weights): def annualized_portfolio_performance(returns, weights): random_weights = random_population(train.shape[1], 100000) GAoptimizerTime = datetime.now() ov = annualized_portfolio_performance(train, solutions) def print_allocation(data, allocations, prices): print_allocation(train, solution, data[ticker, 'Close'].iloc[-1]) OUTPUT = |
I reran it with a population of 10,000, took considerably longer, and still produced same results: all in on AAPL. So it doesn't seem to be a problem with the size of the simulation and it getting stuck in a local optimum. It also still only produces a single solution on the plot. It also doesn't seem to be a problem with the calculation in print_allocation with my own price dataframe, because when you run: so that is definitely what the model is computing for some reason. I removed Apple to see if that was doing anything, and the model just seems to give 100% allocation to whatever asset it see's first. |
Hey there! First thanks so much for putting this together, I have been looking everywhere for a package to do this type of analysis.
I have been trying to implement your "Portfolio Optimize-Smaller.ipynb" but a few things aren't working.
For one, the yf.Download does work, but creates a multilayer column index with each Asset above its OHLC data, which some lines later on seems to get confused by.
I got around this by individually downloading closing data for 5 assets im interested in, then combined those in a DF and calculated daily returns. That works and got me towards the end of your script when i hit the section of printing the portfolio allocations, which is where it broke.
My DF is storing each column of closing data with a column titled for that asset such as "AAPL", "GS", etc. But the print_allocation function has no way of looping through each of these named columns to get latest closing price, and seems to get confused here and break as its calling "data" which i didnt create in the same way for reasons above. I tried to write my own for loop to go through each ticker and call the print_allocation function but can't seem to get that to work either. I believe the key issue is that you are passing "data['close'].iloc[-1] which I can't do because it won't find "close" in multiindex columns.
Do you have any guidance on how I can get this to work? It's really stumping me. Thanks so much!
The text was updated successfully, but these errors were encountered: