robertmartin8 / PyPortfolioOpt

Financial portfolio optimisation in python, including classical efficient frontier, Black-Litterman, Hierarchical Risk Parity
https://pyportfolioopt.readthedocs.io/
MIT License
4.47k stars 951 forks source link

Max sharpe ratio optimization #140

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi Robert, this is part of my code I write to optimize 700 Securities. I add sector constraint and loop through my benchmark weight to set individual weight in the securities cannot more than 10% of benchmark weight. it seems working well. However, I have a few questions.

Does the constraint now work as well in max sharpe ratio? If there is a new version of the module, can you share the code to update latest module? Thank you!

        #porfolioOpt Library        
        mu = expected_returns.capm_return(data2, frequency = last)
        S = risk_models.CovarianceShrinkage(data2, frequency = last).ledoit_wolf()

        ef = EfficientFrontier(mu, S)  # weight_bounds automatically set to (0, 1)
        ef.add_sector_constraints(sector_mapper, sector_lower, sector_upper)

        #loop benchmark securities weight to set constraint of securities !> 10 %
        for i in range (585 ):
            tsla_index = ef.tickers.index(df2.index[i])
            benchmarkW = df2['index_weight_market_cap'].iloc[i]
            benchmarC = benchmarkW * 0.1
            benchmarC = benchmarkW + benchmarC
            ef.add_constraint(lambda w: w[tsla_index] <= benchmarC)

        ef.min_volatility()
        min_volatility_weights = ef.clean_weights()

        checkWeight = np.array(list(min_volatility_weights.values()))
        testData.append(checkWeight)
robertmartin8 commented 4 years ago

Hi @krisutopian,

As far as I'm aware everything should be working for max sharpe, as of v1.2.3. However, I'm not exactly clear what your code sample is trying to achieve.

You may find the cookbook recipe helpful.

Let me know if you have any further questions!

Robert

ghost commented 4 years ago

Thanks. The cookbook is really helpful! I think I manage to do some optimization from the cookbook. Currently, I am trying to run a monthly basis of optimization. 1 year of pricing data, I will try to run MVO Optimization every month. Is there any example for this?

robertmartin8 commented 4 years ago

I don't have an example of that built-in, but you should be able to write a quick for-loop to get it done. If you provide more details with what exactly you're trying to achieve, I'll try to give more concrete suggestions

Robert

ghost commented 4 years ago

Hi Robert,

I try to optimize 700 securities with sector constraint and individual constraint. it works for min_variance and efficient risk. however, it does not work for max_sharpe. Any ideas?

Thank you!

robertmartin8 commented 4 years ago

That sounds like an issue with the solvers, since your optimisation problem is quite big. As of v1.2.3, you can change the solver. It's possible that one of the other solvers might be able to:

ef = EfficientFrontier(mu, S)
ef.solver = "ECOS"
ef.max_sharpe()

Supported solvers are listed here.

Cheers, Robert