Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
long vs short position

Hello,
How can I identify if a open position is a long or short position? Is there an ID associated with a long vs short position?
So for instance, if I have a long position and it meets X criteria, I would like to close the order....etc?

Thanks!

3 responses

You can check the amount of shares purchased. If it is negative, then it's short. You probably want to add something like this to your handle_data() routine anyway:

# Record our leverage and exposure  
leverage=context.account.leverage  
exposure=context.account.net_leverage  
record(leverage=leverage, exposure=exposure)  

longs = shorts = 0  
for position in context.portfolio.positions.itervalues():  
    if position.amount > 0:  
        longs += 1  
    if position.amount < 0:  
        shorts += 1  
record(long_count=longs, short_count=shorts)  

hello Eliot,
assuming if I have only 1 stock in my universe, could this work as well:

current_position = context.portfolio.positions[context.security].amount

so that if current_position > 0 ...then its long, etc?

This code doesn't work now.
Please help with any advances