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!
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!
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)