Limiting Short-Selling Position Size

I never want to go 100% short so I was looking for a way that would cap my short positions off at 80% max-short.

This is how I am currently thinking of assigning the weights to the long side / short side.

Where if

then it would cap the short positions off and increase the long weights.

#context.longs = stocks to go long on
#context.shorts = stocks to go short on

wgt_short = 0.80/(len(context.longs) + len(context.shorts))
wgt_long = 0.20/(len(context.longs) + len(context.shorts))
elif (len(context.longs) + len(context.shorts)) >1.0:
wgt_short = 0.99/(len(context.longs) + len(context.shorts))
wgt_long = 0.99/(len(context.longs) + len(context.shorts))
else:
wgt_short = 0.01
wgt_long = 0.01


Does anyone have a smarter way of implementing this?

1 response

One very simple approach is to use the optimize NetExposure constraint (see https://www.quantopian.com/help#module-quantopian_optimize_constraints). Even if one isn't using order_optimal_portfolio to order there is still the calculate_optimal_portfolio method. Use this to input the original weights and it will calculate a set of new weights meeting any constraints. Something like this

    # Assume weights is an existing series of weights

objective = opt.TargetWeights(weights)
net_exposure_constraint = opt.NetExposure(max_short_exposure, 1.0)
opt_weights = opt.calculate_optimal_portfolio(objective, constraints=[net_exposure_constraint])



One word of caution. The optimize methods have a very literal approach to the world and the results may not always be as expected. Take for example the code above. It will return a series of weights meeting the max short weight criteria. However, part of the way it arrives at this is to limit gross exposure (or leverage) to something much less than 1. Probably not what one expected but it does meet the constraints.

I find using the calculate_optimal_portfolio helpful in this regard. One can look at the results of the optimization and make any adjustments before doing the actual ordering either with the order_optimal_portfolio or basic order methods.

Attached is an algo which iteratively executes calculate_optimal_portfolio until the gross leverage is at an acceptable level. Also note that the NetExposure constraint expects an input of exposure and not 'short weight'. There is a conversion for this in the notebook.

0