@@ -801,11 +801,8 @@ def new_order(self,
801
801
f"TP ({ tp } ) < LIMIT ({ limit or stop or adjusted_price } ) < SL ({ sl } )" )
802
802
803
803
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 :
809
806
# If exclusive orders (each new order auto-closes previous orders/position),
810
807
# cancel all non-contingent orders and close all open trades beforehand
811
808
if self ._exclusive_orders :
@@ -815,7 +812,8 @@ def new_order(self,
815
812
for t in self .trades :
816
813
t .close ()
817
814
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 )
819
817
820
818
return order
821
819
@@ -1067,9 +1065,6 @@ def _open_trade(self, price: float, size: int,
1067
1065
# Apply broker commission at trade open
1068
1066
self ._cash -= self ._commission (size , price )
1069
1067
# 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.
1073
1068
if tp :
1074
1069
trade .tp = tp
1075
1070
if sl :
0 commit comments