Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to buy back a percent of shares shorted?

I have a strategy where a particular trigger should buy back 50% of the total number of shares shorted. How can I do this? I tried order_percent and order_target_percent, but they over buy back at times. I'm not shorting a designated number of shares to start, but rather a percentage of the portfolio. Thanks!

1 response

This may be the simplest

    order( stock, -1 * context.portfolio.positions[stock].amount / 2 )  

Example:
If short -11 shares (amount) currently, dividing by an integer would be -5 (rounds to an integer).
Then -1 * -5 would be +5.
So that would buy 5 shares.
Starting with -11 shares and resulting in -6 shares.
Or starting with -10 shares and resulting in -5 shares.

Also works for long as is.