@@ -215,7 +215,7 @@ def next(self, FIVE_DAYS=pd.Timedelta('3 days')):
215
215
bt = Backtest (GOOG , Assertive )
216
216
with self .assertWarns (UserWarning ):
217
217
stats = bt .run ()
218
- self .assertEqual (stats ['# Trades' ], 145 )
218
+ self .assertEqual (stats ['# Trades' ], 144 )
219
219
220
220
def test_broker_params (self ):
221
221
bt = Backtest (GOOG .iloc [:100 ], SmaCross ,
@@ -248,7 +248,7 @@ def test_compute_drawdown(self):
248
248
np .testing .assert_array_equal (peaks , pd .Series ([7 , 4 ], index = [3 , 5 ]).reindex (dd .index ))
249
249
250
250
def test_compute_stats (self ):
251
- stats = Backtest (GOOG , SmaCross ).run ()
251
+ stats = Backtest (GOOG , SmaCross , close_all_at_end = True ).run ()
252
252
expected = pd .Series ({
253
253
# NOTE: These values are also used on the website!
254
254
'# Trades' : 66 ,
@@ -397,7 +397,32 @@ def next(self):
397
397
elif len (self .data ) == len (SHORT_DATA ):
398
398
self .position .close ()
399
399
400
- self .assertFalse (Backtest (SHORT_DATA , S ).run ()._trades .empty )
400
+ self .assertTrue (Backtest (SHORT_DATA , S ).run ()._trades .empty )
401
+
402
+ def test_dont_close_orders_from_last_strategy_iteration (self ):
403
+ class S (Strategy ):
404
+ def init (self ): pass
405
+
406
+ def next (self ):
407
+ if not self .position :
408
+ self .buy ()
409
+ elif len (self .data ) == len (SHORT_DATA ):
410
+ self .position .close ()
411
+ self .assertEqual (len (
412
+ Backtest (SHORT_DATA , S , close_all_at_end = False ).run ()._strategy .closed_trades ), 0 )
413
+ self .assertEqual (len (
414
+ Backtest (SHORT_DATA , S , close_all_at_end = False ).run ()._strategy .trades ), 1 )
415
+
416
+ def test_dont_close_orders_trades_from_last_strategy_iteration (self ):
417
+ class S (Strategy ):
418
+ def init (self ): pass
419
+
420
+ def next (self ):
421
+ if not self .position :
422
+ self .buy ()
423
+
424
+ self .assertEqual (len (
425
+ Backtest (SHORT_DATA , S , close_all_at_end = False ).run ()._strategy .trades ), 1 )
401
426
402
427
def test_check_adjusted_price_when_placing_order (self ):
403
428
class S (Strategy ):
@@ -499,7 +524,7 @@ def test_autoclose_trades_on_finish(self):
499
524
def coroutine (self ):
500
525
yield self .buy ()
501
526
502
- stats = self ._Backtest (coroutine ).run ()
527
+ stats = self ._Backtest (coroutine , close_all_at_end = True ).run ()
503
528
self .assertEqual (len (stats ._trades ), 1 )
504
529
505
530
@@ -844,7 +869,7 @@ def init(self):
844
869
self .data .Close < sma )
845
870
846
871
stats = Backtest (GOOG , S ).run ()
847
- self .assertIn (stats ['# Trades' ], (1181 , 1182 )) # varies on different archs?
872
+ self .assertIn (stats ['# Trades' ], (1179 , 1180 )) # varies on different archs?
848
873
849
874
def test_TrailingStrategy (self ):
850
875
class S (TrailingStrategy ):
@@ -860,7 +885,7 @@ def next(self):
860
885
self .buy ()
861
886
862
887
stats = Backtest (GOOG , S ).run ()
863
- self .assertEqual (stats ['# Trades' ], 57 )
888
+ self .assertEqual (stats ['# Trades' ], 56 )
864
889
865
890
866
891
class TestUtil (TestCase ):
0 commit comments