@@ -214,7 +214,7 @@ def next(self, FIVE_DAYS=pd.Timedelta('3 days')):
214
214
bt = Backtest (GOOG , Assertive )
215
215
with self .assertWarns (UserWarning ):
216
216
stats = bt .run ()
217
- self .assertEqual (stats ['# Trades' ], 144 )
217
+ self .assertEqual (stats ['# Trades' ], 145 )
218
218
219
219
def test_broker_params (self ):
220
220
bt = Backtest (GOOG .iloc [:100 ], SmaCross ,
@@ -248,47 +248,46 @@ def test_compute_drawdown(self):
248
248
249
249
def test_compute_stats (self ):
250
250
stats = Backtest (GOOG , SmaCross ).run ()
251
- # Pandas compares in 'almost equal' manner
252
- from pandas .testing import assert_series_equal
253
- assert_series_equal (
254
- stats .filter (regex = '^[^_]' ).sort_index (),
255
- pd .Series ({
251
+ expected = pd .Series ({
256
252
# NOTE: These values are also used on the website!
257
- '# Trades' : 65 ,
253
+ '# Trades' : 66 ,
258
254
'Avg. Drawdown Duration' : pd .Timedelta ('41 days 00:00:00' ),
259
255
'Avg. Drawdown [%]' : - 5.925851581948801 ,
260
256
'Avg. Trade Duration' : pd .Timedelta ('46 days 00:00:00' ),
261
- 'Avg. Trade [%]' : 2.3537113951143773 ,
257
+ 'Avg. Trade [%]' : 2.531715975158555 ,
262
258
'Best Trade [%]' : 53.59595229490424 ,
263
259
'Buy & Hold Return [%]' : 703.4582419772772 ,
264
- 'Calmar Ratio' : 0.4445179349739874 ,
260
+ 'Calmar Ratio' : 0.4414380935608377 ,
265
261
'Duration' : pd .Timedelta ('3116 days 00:00:00' ),
266
262
'End' : pd .Timestamp ('2013-03-01 00:00:00' ),
267
- 'Equity Final [$]' : 51959.94999999997 ,
263
+ 'Equity Final [$]' : 51422.98999999996 ,
268
264
'Equity Peak [$]' : 75787.44 ,
269
- 'Expectancy [%]' : 3.097629974370269 ,
270
- 'Exposure Time [%]' : 93.99441340782123 ,
265
+ 'Expectancy [%]' : 3.2748078066748834 ,
266
+ 'Exposure Time [%]' : 96.74115456238361 ,
271
267
'Max. Drawdown Duration' : pd .Timedelta ('584 days 00:00:00' ),
272
268
'Max. Drawdown [%]' : - 47.98012705007589 ,
273
269
'Max. Trade Duration' : pd .Timedelta ('183 days 00:00:00' ),
274
- 'Profit Factor' : 2.0880175388920286 ,
275
- 'Return (Ann.) [%]' : 21.32802699608929 ,
276
- 'Return [%]' : 419.59949999999964 ,
277
- 'Volatility (Ann.) [%]' : 36.53825234483751 ,
278
- 'SQN' : 0.916892986080858 ,
279
- 'Sharpe Ratio' : 0.5837177650097084 ,
280
- 'Sortino Ratio' : 1.0923863161583591 ,
270
+ 'Profit Factor' : 2.167945974262033 ,
271
+ 'Return (Ann.) [%]' : 21.180255813792282 ,
272
+ 'Return [%]' : 414.2298999999996 ,
273
+ 'Volatility (Ann.) [%]' : 36.49390889140787 ,
274
+ 'SQN' : 1.0766187356697705 ,
275
+ 'Sharpe Ratio' : 0.5803778344714113 ,
276
+ 'Sortino Ratio' : 1.0847880675854096 ,
281
277
'Start' : pd .Timestamp ('2004-08-19 00:00:00' ),
282
- 'Win Rate [%]' : 46.15384615384615 ,
278
+ 'Win Rate [%]' : 46.96969696969697 ,
283
279
'Worst Trade [%]' : - 18.39887353835481 ,
284
- }).sort_index ()
285
- )
280
+ })
281
+ diff = {key : print (key ) or value
282
+ for key , value in stats .filter (regex = '^[^_]' ).items ()
283
+ if value != expected [key ]}
284
+ self .assertDictEqual (diff , {})
286
285
287
286
self .assertSequenceEqual (
288
287
sorted (stats ['_equity_curve' ].columns ),
289
288
sorted (['Equity' , 'DrawdownPct' , 'DrawdownDuration' ]))
290
289
291
- self .assertEqual (len (stats ['_trades' ]), 65 )
290
+ self .assertEqual (len (stats ['_trades' ]), 66 )
292
291
293
292
self .assertSequenceEqual (
294
293
sorted (stats ['_trades' ].columns ),
@@ -488,6 +487,13 @@ def coroutine(self):
488
487
stats = self ._Backtest (coroutine ).run ()
489
488
self .assertListEqual (stats ._trades .filter (like = 'Price' ).stack ().tolist (), [112 , 107 ])
490
489
490
+ def test_autoclose_trades_on_finish (self ):
491
+ def coroutine (self ):
492
+ yield self .buy ()
493
+
494
+ stats = self ._Backtest (coroutine ).run ()
495
+ self .assertEqual (len (stats ._trades ), 1 )
496
+
491
497
492
498
class TestOptimize (TestCase ):
493
499
def test_optimize (self ):
@@ -786,7 +792,7 @@ def init(self):
786
792
self .data .Close < sma )
787
793
788
794
stats = Backtest (GOOG , S ).run ()
789
- self .assertEqual (stats ['# Trades' ], 1180 )
795
+ self .assertEqual (stats ['# Trades' ], 1182 )
790
796
791
797
def test_TrailingStrategy (self ):
792
798
class S (TrailingStrategy ):
@@ -802,7 +808,7 @@ def next(self):
802
808
self .buy ()
803
809
804
810
stats = Backtest (GOOG , S ).run ()
805
- self .assertEqual (stats ['# Trades' ], 50 )
811
+ self .assertEqual (stats ['# Trades' ], 51 )
806
812
807
813
808
814
class TestUtil (TestCase ):
0 commit comments