Back to Community
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

short_breadth> 0.80

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

    long_breadth = len(context.longs)/(len(context.longs) + len(context.shorts))  
    short_breadth = len(context.shorts)/(len(context.longs) + len(context.shorts))  
    if short_breadth > 0.80:  
        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.

Loading notebook preview...
Notebook previews are currently unavailable.
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.