@@ -801,11 +801,8 @@ def new_order(self,
801801 f"TP ({ tp } ) < LIMIT ({ limit or stop or adjusted_price } ) < SL ({ sl } )" )
802802
803803 order = Order (self , size , limit , stop , sl , tp , trade , tag )
804- # Put the new order in the order queue,
805- # inserting SL/TP/trade-closing orders in-front
806- if trade :
807- self .orders .insert (0 , order )
808- else :
804+
805+ if not trade :
809806 # If exclusive orders (each new order auto-closes previous orders/position),
810807 # cancel all non-contingent orders and close all open trades beforehand
811808 if self ._exclusive_orders :
@@ -815,7 +812,8 @@ def new_order(self,
815812 for t in self .trades :
816813 t .close ()
817814
818- self .orders .append (order )
815+ # Put the new order in the order queue, Ensure SL orders are processed first
816+ self .orders .insert (0 if trade and stop else len (self .orders ), order )
819817
820818 return order
821819
@@ -1067,9 +1065,6 @@ def _open_trade(self, price: float, size: int,
10671065 # Apply broker commission at trade open
10681066 self ._cash -= self ._commission (size , price )
10691067 # Create SL/TP (bracket) orders.
1070- # Make sure SL order is created first so it gets adversarially processed before TP order
1071- # in case of an ambiguous tie (both hit within a single bar).
1072- # Note, sl/tp orders are inserted at the front of the list, thus order reversed.
10731068 if tp :
10741069 trade .tp = tp
10751070 if sl :
0 commit comments