In “Algorithmic Trading Using Python #5 - Trading & Orders” it has the code:

        if not self.Portfolio.Invested and not self.Transactions.GetOpenOrders(self.qqq):
            quantity = self.CalculateOrderQuantity(self.qqq, 0.9)
            self.entryTicket = self.LimitOrder(self.qqq, quantity, price, "Entry Order")
            self.entryTime = self.Time

As placing limit orders is asynchronous, isn't it possible that the following occurs:

  1. Calls self.Protfolio.Invested and it returns false
  2. The previously placed limit order is executed, placing that into the portfolio
  3. Calls self.Transactions.GetOpenOrders() and it returns false (placed in #2)
  4. Executes the if contents, placing a 2nd order

Or am I not understanding something about how asynchronous orders are executed?

Also, if this is a race condition, I think you eliminate the problem if you flip the two booleans to the following - depending on how the Python interpreter may optimize the and. It might be safest to do nested if statements, but again, depends on the Python optimizer.

if not self.Transactions.GetOpenOrders(self.qqq) and not self.Portfolio.Invested:

thanks - dave