Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help needed to improve this sample algo to use the new "order_optimal_portfolio" function

I am trying to improve this sample algorithm to use the "order_optimal_portfolio". I've been running into some errors when trying to use:

order_optimal_portfolio(
objective=TargetWeights({context.security_list: context.weights}),
constraints=[],
)

FYI...I am new to programming.

  • Thanks in advance.
2 responses

Hi Okana,

In summary, The Optimize API allows a strategy to describe desired orders in terms of high level Objectives and Constraints.
For example, if you have a long/short portfolio with specific stocks and their respective share counts, you could create a dictionary of weights by determining the weight of each position relative to the total amount your strategy is trading. Passing this weight dictionary into the optimizer, namely the order_optimal_portfolio function, will calculate and order a portfolio of assets that satisfy the objective and adhere to the constraints passed in.

I cloned your algorithm and adjusted it to use the order_optimal_portfolio function with the TargetWeights objective and no constraints. The way asset weights are calculated is arbitrary, so feel free to expand on how the strategy weights each asset before passing the dictionary of weights into the optimizer.

I hope this is helpful and gives you enough guidance on how to move forward using the optimize API in your algorithm.

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.

Hello Robert,

and thanks for your guidance. This definitely solves my initial problem, will expand further.

On a separate note, could an algorithm submitted to the contest be adjusted down the road?...In reference, for example, to this algorithm above which uses a selected set of securities. Would the author be able to adjust the basket?