I'd like to retrieve the fill price when I place an order using SetHoldings(). However, the code below doesn't seem to accurately return the fill price. Any examples demonstrating this would be greatly appreciated.
# Get the current price of the asset
current_price = self.Securities[self.symbol].Price
# Place an order to set holdings to 100%
self.SetHoldings(self.symbol, 1)
# Calculate the fill price
fill_price = current_price
# Print the fill price
self.Debug("Fill Price: {}".format(fill_price))
Mia Alissi
The
SetHoldings()
method in QuantConnect does not return the fill price directly. It sends a market order to adjust the portfolio to the desired target, but it does not wait for this order to fill before proceeding to the next line of code. Therefore, thecurrent_price
you are using to calculate thefill_price
is not the actual fill price of the order.To get the fill price of an order, you can use the
Transactions
property to access theOrderTicket
object associated with each order. TheOrderTicket
object has anAverageFillPrice
property that gives the average fill price of the order.Here is an example of how you can do this in Python:
This code will print the average fill price of the last order placed by the
SetHoldings()
method.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.
Sources:
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.
Hasan
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!