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_portfolio
method 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_portfolio
method.
# 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.