OnOrderEvent(self, event) not working

Dear all,

I use the OnOrderEvent(self, event) method in the algorithm whose code is attached to this post. However, I believe the code block is never executed (despite orders being filled) since I have debug/log messages in this code block that do not appear. 

def OnOrderEvent(self, event):
# Handle filling of buy & sell orders:
# Determine if order is the buy or the sell or the stop

order = self.Transactions.GetOrderById(event.OrderId)
self.Log("{0}: {1}: {2}".format(self.Time, order.Type, event))

if event.OrderId == self.buyTicket.OrderId:
self.Debug("Buy ticket event detected")
# If buy order is filled, create stop loss
if self.buyTicket.Status == OrderStatus.Filled:
self.Debug("Buy order filled")
quantity = self.buyTicket.Quantity
# limit price is set below the trigger to maximise the chances of catching a price decrease
trigger = (1 - self.initial_stop_percent_delta) * self.buyTicket.AverageFillPrice
limit = trigger * 0.99
self.stopTicket = self.StopLimitOrder(self.symbol, -quantity, stopPrice=trigger, limitPrice=limit)
self.previousTrigger = trigger
self.buyTicket = None

elif event.OrderId == self.sellTicket.OrderId:
self.Debug("Sell ticket event detected")
# If sell order is filled, cancel stop loss
if self.sellTicket.Status == OrderStatus.Filled:
self.Debug("Sell order filled")

elif event.OrderId == self.stopTicket.OrderId:
self.Debug("Stop ticket event detected")
# If stop order is filled, cancel the sell order, if any:
if self.stopTicket.Status == OrderStatus.Filled:
self.Debug("Stop order filled")
self.stopTicket = None
if self.sellTicket is not None:
self.sellTicket = None

What should I do for this code block to be executed?

Thank uou. Cordially, Benjamin.

Update Backtest

I believe you need to shift-tab the def OnOrderEvent(self, event): out of the previous def(). If I do I get next error of which I believe the orderevent get hits.

Runtime Error: Python.Runtime.PythonException: TypeError : unsupported operand type(s) for *: 'float' and 'decimal.Decimal' at Python.Runtime.PyObject.Invoke (Python.Runtime.PyTuple args, Python.Runtime.PyDict kw) [0x00033] in <08c5f7c671544a85833a052ad010684a>:0 at Python.Runtime.PyObject.InvokeMethod (System.String name, Python.Runtime.PyTuple args, Python.Runtime.PyDict kw) [0x00007] in <08c5f7c671544a85833a052ad010684a>:0 at Python.Runtime.PyObject.TryInvokeMember (System.Dynamic.InvokeMemberBinder binder, System.Object[] args, System.Object& result) [0x0003e] in <08c5f7c671544a85833a052ad010684a>:0 at (wrapper dynamic-method) System.Object.CallSite.Target(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,QuantConnect.Orders.OrderEvent) at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnOrderEvent (QuantConnect.Orders.OrderEvent newEvent) [0x0004f] in <fc711d2739fa45e1a39a550462704774>:0 at QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.HandleOrderEvent (QuantConnect.Orders.OrderEvent fill) [0x00186] in <b58b553d766a4d8491ec11a8d532c7ab>:0

Thank you QuantTrader, you were right, I indeed had an indentation problem. 

After adjusting this indentation and solving other issues, I end up with the following issue: "BrokerageModel declared unable to submit order" for my StopLimit Sell orders. I attached the backtest to this comment to reflect the corrections made.

In any cases, I will create a new discussion as this discussion's original issue was solved. Please see the new post at



Update Backtest


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.


This discussion is closed