Hello 😊 As bracket orders are not integrated in QuantConnect (which is a pity) I figured I would attempt to create the functionality myself. I was hoping I could get some help from the community to finish the functionality, so we can all benefit from using it (I have seen multiple posts requesting functionality for bracket orders).  Maybe it can even be used to help QC in creating support for bracket orders.

My idea is to create a class that initiates and handles all new bracket orders (the BracketOrder class in the BracketOrder.py file) - one object per new bracket order. It works decently well, but I have encountered a problem: New bracket orders are stored in a dictionary using the parent ticket's OrderId (self.bracketOrders - the ticket IDs of profit target and stop loss orders are also stored here to reference the same BracketOrder object). Orders are placed asynchronously and whenever an update to an order happens it immediately calls the OnOrderEvent function. On occasion, an order can be filled before I have had the time to create a BracketOrder object. When that happens, the given OrderId is not found in self.bracketOrders which results in an error.

I am not sure if it would help to create the BracketOrder object once it is filled and obtained in OnOrderEvent as multiple partial fills can happen before an object is created causing the same error.

I have attached a backtest displaying the code. Can't attach a backtest with an error, unfortunately. However, I discovered that the error occurs because order is filled before the ticket for the order, and the BracketOrder object of that order has been created. You get the error if you change the end date to self.SetEndDate(2022, 6, 15)  # Set End Date.