Hi
In live trading, what happens when SetHoldings() is invoked repeatedly, but the trade isn't completed. For instance:
self.SetHoldings(A, 0) # sell A
self.SetHoldings(B, 1) # buy B
Does it wait for A to be sold before buying B using the entire portfolio value? What is the canonical approach to wait for the first (sell) instruction to fill prior to calling the second (buy)?
Thanks
Mia Alissi
The
SetHoldings()
method in QuantConnect does not inherently wait for a sell order to complete before initiating a buy order. When you callSetHoldings()
to sell a security (A in your case), it sends a sell order, but it doesn't wait for this order to fill before proceeding to the next line of code where you callSetHoldings()
to buy another security (B in your case).If you want to ensure that the sell order is filled before the buy order is placed, you can use the
Order()
method to place the sell order and then use theOrderTicket
object returned by this method to monitor the status of the order. Once the sell order is filled, you can then place the buy order.Here is an example of how you can do this:
Please note that this is a simplified example and actual implementation may require more complex handling, especially when dealing with partial fills or rejected orders.
For more information, you can refer to the QuantConnect API Reference and Trading and Orders documentation.
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.
John Munro
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!