Hello,
I am having difficulty figuring out how to get the price at which a security was liquidated. For example, how can I get the price at which SPY was liquiidated to do with that price whatever I want (Like sending market orders at that price)?
Any help would be highly appreciated!
Derek Melchin
Hi Marvin,
After using Liquidate() or SetHoldings(), we can get the order ID of the market order submitted with
last_o_id = self.Transactions.LastOrderId
Next, we can get the average fill price of the order with
fill = self.Transactions.GetOrderTicket(last_o_id).AverageFillPrice
Although, we actually recommend using a different, faster technique to submit orders and retrieve the fill price. Instead of using Liquidate() and SetHoldings(), we can use CalculateOrderQuantity to determine our position size and then issue market orders directly. Market orders return an OrderTicket after their construction, which allow us to immediately get the fill price of the order.
Here's a quick example:
if self.Securities['SPY'].Invested: target = 0 else: target = 1 quantity = self.CalculateOrderQuantity("SPY", target) ticket = self.MarketOrder("SPY", quantity) fill = ticket.AverageFillPrice
See the attached backtest which shows the full solution file. For more information on ordering in this way is new, review our Trading and Orders documentation. For future reference, I recommend utilizing the LEAN documentation to search the classes and their properties/methods when trying to access specific data.
Best,
Derek Melchin
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.
Marvin Montoya
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!