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:
- Calls self.Protfolio.Invested and it returns false
- The previously placed limit order is executed, placing that into the portfolio
- Calls self.Transactions.GetOpenOrders() and it returns false (placed in #2)
- 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
David Thielen
I checked, Python does evaluate within the if in order. So flipping the conditions should eliminate the race condition.
David Thielen
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!