Skip to content

Commit c21d7d9

Browse files
committed
BUG: On stop-limit order, strike should equal stop, not candle open
Fixes #174
1 parent 25cf077 commit c21d7d9

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

backtesting/backtesting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,9 @@ def _process_orders(self):
806806
continue
807807

808808
# stop_price, if set, was hit within this bar
809-
price = (min(open, order.limit, stop_price or np.inf)
809+
price = (min(stop_price or open, order.limit)
810810
if order.is_long else
811-
max(open, order.limit, stop_price or -np.inf))
811+
max(stop_price or open, order.limit))
812812
else:
813813
# Market-if-touched / market order
814814
price = prev_close if self._trade_on_close else open

backtesting/test/_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,15 @@ def coroutine(self):
479479

480480
self._Backtest(coroutine).run()
481481

482+
def test_stop_limit_order_price_is_stop_price(self):
483+
def coroutine(self):
484+
self.buy(stop=112, limit=113, size=1)
485+
self.sell(stop=107, limit=105, size=1)
486+
yield
487+
488+
stats = self._Backtest(coroutine).run()
489+
self.assertListEqual(stats._trades.filter(like='Price').stack().tolist(), [112, 107])
490+
482491

483492
class TestOptimize(TestCase):
484493
def test_optimize(self):

0 commit comments

Comments
 (0)