In my handle_data call, I loop through all the stocks in context.stocks, executing the trade logic on each. As a result, multiple positions can be opened during a single handle_data call. This leads to a static (and inaccurate) count of my positions. I know context.portfolio.positions isn't updated until the next bar, so I've also tried the following (with no success):
len(get_open_orders())+len(context.portfolio.positions)
Is there a way to track the number of positions as it changes within a single handle_data call? Any help would be much appreciated!