Skip to content

Commit 25cf077

Browse files
committed
BUG: Fix continuing to buy 0 units resulting in AssertionError
Fixes #179 Thanks @sdmovie
1 parent 10a6de2 commit 25cf077

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

backtesting/backtesting.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,8 @@ def _process_orders(self):
879879
# so it will only be closed partially
880880
self._reduce_trade(trade, price, need_size, time_index)
881881
need_size = 0
882+
883+
if not need_size:
882884
break
883885

884886
# If we don't have enough liquidity to cover for the order, cancel it

backtesting/test/_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,16 @@ def coroutine(self):
469469

470470
self._Backtest(coroutine).run()
471471

472+
def test_close_trade_leaves_needsize_0(self):
473+
def coroutine(self):
474+
self.buy(size=1)
475+
self.buy(size=1)
476+
yield
477+
if self.position:
478+
self.sell(size=1)
479+
480+
self._Backtest(coroutine).run()
481+
472482

473483
class TestOptimize(TestCase):
474484
def test_optimize(self):

0 commit comments

Comments
 (0)