File tree 2 files changed +17
-0
lines changed
2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -1005,6 +1005,12 @@ def _process_orders(self):
1005
1005
if order .sl or order .tp :
1006
1006
if is_market_order :
1007
1007
reprocess_orders = True
1008
+ # Order.stop and TP hit within the same bar, but SL wasn't. This case
1009
+ # is not ambiguous, because stop and TP go in the same price direction.
1010
+ elif stop_price and not order .limit and order .tp and (
1011
+ (order .is_long and order .tp <= high and (order .sl or - np .inf ) < low ) or
1012
+ (order .is_short and order .tp >= low and (order .sl or np .inf ) > high )):
1013
+ reprocess_orders = True
1008
1014
elif (low <= (order .sl or - np .inf ) <= high or
1009
1015
low <= (order .tp or - np .inf ) <= high ):
1010
1016
warnings .warn (
Original file line number Diff line number Diff line change @@ -1106,3 +1106,14 @@ def next(self):
1106
1106
1107
1107
trades = Backtest (SHORT_DATA , S ).run ()._trades
1108
1108
self .assertEqual (trades ['ExitPrice' ].iloc [0 ], 104.95 )
1109
+
1110
+ def test_stop_entry_and_tp_in_same_bar (self ):
1111
+ class S (_S ):
1112
+ def next (self ):
1113
+ i = len (self .data .index )
1114
+ if i == 3 :
1115
+ self .sell (stop = 108 , tp = 105 , sl = 113 )
1116
+
1117
+ trades = Backtest (SHORT_DATA , S ).run ()._trades
1118
+ self .assertEqual (trades ['ExitBar' ].iloc [0 ], 3 )
1119
+ self .assertEqual (trades ['ExitPrice' ].iloc [0 ], 105 )
You can’t perform that action at this time.
0 commit comments