I am attempting to create my own class to create and manage BracketOrder (One-cancels-all) - would love to share it with you when I get it working. 

So far, I have just come to the point of creating a stop market order and a profit order when placing the initial trade. I am having a problem with it, however, because the ticket for the initial (parent) order is not created and stored in my self.bracketOrders dictionary before the OnOrderEvent is called for the first time - I am assuming that is because order placements are asynchronous. It makes for a big problem because I can't fetch the correct BracketOrder object in OnOrderEvent. I am also placing a stop market order and a profit stop limit order in the BracketOrder object itself, and I will surely get the same problem there. 

Please see the backtest - the log displays the following length of the bracketOrder dictionary (notice it has a length of 0 the first time around:

Length: 0 Length: 1 Length: 2 Length: 3 Length: 4

I tried creating the BracketOrder object in OnOrderEvent, but I don't think it's possible to fetch the order ticket from an OrderEvent. Also don't think it would work because OnOrderEvent may fire on the stop order created inside the BracketOrder object BEFORE the ticket for the stop order is stored in the self.bracketOrders dictionary.

The only solution I can think of is making the order placement and insert into dictionary synchronous, so OnOrderEvent can't fire until the BracketOrder object is created and stored in the dictionary. Is that possible? Other solutions would be appreciated as well.

Author