Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
order_optimal_portfolio() with custom csv data

I'm very confused on how order_optimal_portfolio() works with custom imported csv data. I went through the tutorial and am still a bit lost. I can understand the purpose of the constraints but do not understand how to actually order a position from my Longs or Shorts. How can there be alpha optimization of my personal tickers? I'm currently reading the data into a list of longs and shorts. Please help! Thanks!

2 responses

There are two objectives one can use with order_optimal_portfolio - MaximizeAlpha and TargetWeights.

Both take a pandas series or python dict as a parameter. The indexes or keys are the security objects one wishes to order. In this way both are identical. Place the securities one wants to order in a series or dict.

If one has already determined how much to order of each security then use the TargetWeights objective. This is very similar to the order_target_percent method. The values associated with each security are the target weights one would like for the securities (as a percent of portfolio value). The order_optimal_portfoliomethod will place all the orders needed to achieve this weighting. This is typically the objective one uses if the securities and weights have already been defined.

If however, one doesn't know how much of each security to order but only a general ranking (ie these should do better and these others not so much) then use the MaximizeAlpha objective. This is typically the objective one uses if the securities and some sort of ranking is defined.

So, since it seems one is starting from a defined list of longs and shorts, and presumably, some defined weights, then use the TargetWeights objective. I'll assume the goal is to have the portfolio equally weighted for the sake simplicity. The TargetWeights objective just needs a series of securities and associated weights. The first step then is to create this series. If the longs and shorts are already in lists then something like this works well.

import pandas as pd

# longs and shorts are existing lists of securities  
# equally weight all positions  
weight = 1.0 / (len(longs) + len(shorts))

# create two series for longs and shorts with associated weights  
# shorts should have a negative weight  
long_weights = pd.Series(weight, longs)  
short_weights = pd.Series(-weight, shorts)

# create a single series to pass to the TargetWeights objective  
weights = pd.concat([long_weights, short_weights])  

Once we have a series with the securities we want to order and the associated weights, just need to create our objective and then execute the order_optimal_portfoliomethod.


# Create our TargetWeights objective  
target_weights = opt.TargetWeights(weights) 

# Execute the order_optimal_portfolio method with above objective  
order_optimal_portfolio(  
    objective = target_weights,  
    constraints = []  
)

If one has defined the target weights already then no constraints are required. Just specify the TargetWeights objective. However, one could add constraints if desired which would then impact the final weights. Note that any currently held positions, which are not in longs or shorts, will be closed.

Check out the sample algo in this post (https://www.quantopian.com/posts/simple-example-investment-program-available-to-share) for this approach wrapped in a functioning algo.

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 Dan,

Thank you for the in depth response. I have gotten it to work by following your method. One question I have is that when giving the pandas Series to order_optimal_portfolio(), does it automatically exit out of positions from previous months which are no longer available in the new target_weights? Also I am passing the screen=QTradableStocksUS() when building the pipeline and still not meeting the structural constraint for "Tradable Universe". Any idea as why this is? Thank you for all the help so far, it has saved me a lot of time!

Roo