Hey! I'm trying to get the fill price of my order for the day to compare it to the current price. I'm having a little trouble accessing the FillPrice field from the OrderEvent Class that it belongs to. I've imported the namespace Orders with using QuantConnect.Orders;. It always shows 0 in the console. Do you know how to do this?
Michael Handschuh
Hey Travis, In order to get order events you'll need to override the OnOrderEvent(OrderEvent) method. I've modified your algorithm to do this. The issue was you were using your own OrderEvents class and that value is never updated by the LEAN engine. You can inspect the log and see the fill prices.
Travis Teichelmann
Thank you! Michael that worked like a charm. Now how do I access the fill.FillPrice publicly then store it in a variable? I'm trying this
OrderEvent percentFill = new OrderEvent();
but it isn't workingNicholas Stein
@Travis, Michael has it right but you also need to look at the order status. The first time an OnOrderEvent fires, the orderEvent.Status is Submitted and the fill price will be 0 because the order has not filled yet. Subsequent events will tell you what happened in more detail.
switch (orderEvent.Status) { case OrderStatus.New: case OrderStatus.None: break; case OrderStatus.Submitted: Log("Order Submitted"); case OrderStatus.Invalid: break; case OrderStatus.PartiallyFilled: decimal nEntryPrice = Portfolio[orderEvent.Symbol].HoldStock ? Portfolio[orderEvent.Symbol].AveragePrice : 0; Log(string.Format("Partial Fill: {0}",nEntryPrice )); } break; case OrderStatus.Canceled: Log(string.Format("Order {0} cancelled.", orderEvent.OrderId)); break; case OrderStatus.Filled: decimal nEntryPrice = Portfolio[orderEvent.Symbol].HoldStock ? Portfolio[orderEvent.Symbol].AveragePrice : 0; Log(string.Format("Order Filled: {0}",nEntryPrice )); break;
Travis Teichelmann
Michael - It finally clicked in my mind why you said to use the override modifier. Its implementation needs to be inherited from the QCAlgorithm class. Nicholas - I've never seen a switch statement before but I was able to implement it in my code. This is definitely a good step towards a more resilient algorithm that can handle different situations.
Nicholas Stern
Does anyone know how to do this in Python?Â
Daniel Chen
Hi Nicholas,
It's also easy to get the FillPrice of an order using Python. Generally, we can get the help from OnOrderEvent event handler which receives the events created by the relevant order. The following code snippet shows how to get the FillPrice and other information of one order:
def OnOrderEvent(self, orderEvent): Â Â order = self.Transactions.GetOrderById(orderEvent.OrderId) Â Â self.Log("{0}: {1}: {2}".format(self.Time, order.Type, orderEvent))
Besides, this backtest will help you understand how the OnOrderEvent method works. Please check the Log and you will find the FillPrice you need. For more details, please visit this page. Thank you!
Travis Teichelmann
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!