can any anyone explain what does calculate_optimal_portfolio does ?

can any anyone explain what does calculate_optimal_portfolio does ? in non-mathematical I was able to understand all other order function but wasn't able to understand optimal_portolio

3 responses

Good question! Basically you give this calculate_optimal_portfolio method a set of assets, an objective, and a set of constraints. It then calculates the 'best' weights of those assets while meeting all the constraints. What does 'best' mean? There are currently two objectives - TargetWeights and MaximizeAlpha. The best weights when using the TargetWeights objective is the set of weights closest to the weights one inputs with the objective. The best weights when using the MaximizeAlpha objective is the set of weights which maximizes the total alpha using the individual alphas inputed with the objective. Using these methods can greatly reduce the code and complexity in ones logic. This is especially true if one wants to balance a lot of stocks based upon sector, long/short exposure, etc. Look at the documentation for a pretty good overview (https://www.quantopian.com/docs/user-guide/tools/optimize).

I personally feel the calculate_optimal_portfolio is under-appreciated. One can think of this method as the optimization and calculation portion of the order_optimal_portfolio method. It basically does everything the order method does EXCEPT the actual ordering. Instead of returning a series of orders it returns a series of weights.

The order_optimal_portfolio method doesn't work in the research environment. To do notebook analysis using optimize one must use the calculate_optimal_portfoliomethod. In the IDE, the returned series of weights is in the same format which the order_optimal_portfoliomethod expects. So, one can do something like this

    weights = opt.calculate_optimal_portfolio(objective=my_objective,   constraints=my_constraints)
algo.order_optimal_portfolio(objective=opt.TargetWeights(weights),  constraints=[] )



Why would one want to calculate the weights simply to feed them into the order_optimal_portfolio method? In a word, visibility. I find it helpful to sometimes record the net leverage which the ordering method is targeting or possibly the total target positions. Like this

    record(leverage=weights.abs().sum())
record(stocks=weights.where(weights!=0).size)



Additionally, one could do some final tweaking to the weights before passing it to the order_optimal_portfolio method. Maybe ensure leverage is 1.

    weights = opt.calculate_optimal_portfolio(objective=my_objective,   constraints=my_constraints)
weights = weights / weights.abs().sum()
algo.order_optimal_portfolio(objective=opt.TargetWeights(weights),  constraints=[] )



Hope that helps?

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

sir sounds quite confusing for me if u could explain with trading example it would be great of you sir .suppose for example, I am building a long-short equity strategy simply buying the stocks wich are top 10 gainers in the past month and shorting the stocks which are in top 10 losers and say order amount will be 5000$this amount will be divided equally for long and short stocks and to place my order I have used order_optimal _portfolio so now can u tell what will order optimal portfolio will do how will it decide in which quantity to buy wich share as it as to place total order of 2500$ for long stock and 2500\$ for short stocks ?

sir on what criterion the optimization of weight is done is it giving more weight to stocks wich have shown higher return I mean what is the decision making done on like for efficient frontier we select the portfolio weights which has given the maximum returns for a specific risk according to that we weight the stocks but in order optimal portfolio I am still not getting on what basis is it is selecting weights?