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

Quick question:

How do we check if our strategy is net long or short at any given minute?

Thanks

4 responses

maybe look at

context.portfolio.positions_value

This is a positive dollar value when long and a negative dollar value when short. There's a little bit of documentation here https://www.quantopian.com/help#api-portfolio .

Thanks Dan. Do you think that checking context.portfolio.positions_value = 0 will have issues with floating values? I have seen people doing

If context.portfolio.positions_value == 0:
...............

normally this will not work for floating values because they are never equal to 0 exactly.

You are correct it's generally not good practice to check for equality of real numbers. However, it seemed you just wanted to know if the portfolio was net long or net short. In that case use 'greater than" or ' less than'. Depending upon what you are trying to check for you could use

if context.portfolio.positions_value > 0.0:  
    net_long = True  
else:  
    net_long = False  

However, if you want to know if you have ANY positions you could check the quantity of unique SIDs using:

len(context.portfolio.positions) == 0

Dan

Dan, thanks for your help.