@@ -218,7 +218,7 @@ def next(self, _FEW_DAYS=pd.Timedelta('3 days')): # noqa: N803
218
218
bt = Backtest (GOOG , Assertive )
219
219
with self .assertWarns (UserWarning ):
220
220
stats = bt .run ()
221
- self .assertEqual (stats ['# Trades' ], 145 )
221
+ self .assertEqual (stats ['# Trades' ], 144 )
222
222
223
223
def test_broker_params (self ):
224
224
bt = Backtest (GOOG .iloc [:100 ], SmaCross ,
@@ -251,7 +251,7 @@ def test_compute_drawdown(self):
251
251
np .testing .assert_array_equal (peaks , pd .Series ([7 , 4 ], index = [3 , 5 ]).reindex (dd .index ))
252
252
253
253
def test_compute_stats (self ):
254
- stats = Backtest (GOOG , SmaCross ).run ()
254
+ stats = Backtest (GOOG , SmaCross , close_all_at_end = True ).run ()
255
255
expected = pd .Series ({
256
256
# NOTE: These values are also used on the website!
257
257
'# Trades' : 66 ,
@@ -401,7 +401,32 @@ def next(self):
401
401
elif len (self .data ) == len (SHORT_DATA ):
402
402
self .position .close ()
403
403
404
- self .assertFalse (Backtest (SHORT_DATA , S ).run ()._trades .empty )
404
+ self .assertTrue (Backtest (SHORT_DATA , S ).run ()._trades .empty )
405
+
406
+ def test_dont_close_orders_from_last_strategy_iteration (self ):
407
+ class S (Strategy ):
408
+ def init (self ): pass
409
+
410
+ def next (self ):
411
+ if not self .position :
412
+ self .buy ()
413
+ elif len (self .data ) == len (SHORT_DATA ):
414
+ self .position .close ()
415
+ self .assertEqual (len (
416
+ Backtest (SHORT_DATA , S , close_all_at_end = False ).run ()._strategy .closed_trades ), 0 )
417
+ self .assertEqual (len (
418
+ Backtest (SHORT_DATA , S , close_all_at_end = False ).run ()._strategy .trades ), 1 )
419
+
420
+ def test_dont_close_orders_trades_from_last_strategy_iteration (self ):
421
+ class S (Strategy ):
422
+ def init (self ): pass
423
+
424
+ def next (self ):
425
+ if not self .position :
426
+ self .buy ()
427
+
428
+ self .assertEqual (len (
429
+ Backtest (SHORT_DATA , S , close_all_at_end = False ).run ()._strategy .trades ), 1 )
405
430
406
431
def test_check_adjusted_price_when_placing_order (self ):
407
432
class S (Strategy ):
@@ -503,7 +528,7 @@ def test_autoclose_trades_on_finish(self):
503
528
def coroutine (self ):
504
529
yield self .buy ()
505
530
506
- stats = self ._Backtest (coroutine ).run ()
531
+ stats = self ._Backtest (coroutine , close_all_at_end = True ).run ()
507
532
self .assertEqual (len (stats ._trades ), 1 )
508
533
509
534
def test_order_tag (self ):
@@ -873,7 +898,7 @@ def init(self):
873
898
self .data .Close < sma )
874
899
875
900
stats = Backtest (GOOG , S ).run ()
876
- self .assertIn (stats ['# Trades' ], (1181 , 1182 )) # varies on different archs?
901
+ self .assertIn (stats ['# Trades' ], (1179 , 1180 )) # varies on different archs?
877
902
878
903
def test_TrailingStrategy (self ):
879
904
class S (TrailingStrategy ):
@@ -889,7 +914,7 @@ def next(self):
889
914
self .buy ()
890
915
891
916
stats = Backtest (GOOG , S ).run ()
892
- self .assertEqual (stats ['# Trades' ], 57 )
917
+ self .assertEqual (stats ['# Trades' ], 56 )
893
918
894
919
895
920
class TestUtil (TestCase ):
0 commit comments