@@ -468,6 +468,39 @@ def ok(x):
468
468
# Give browser time to open before tempfile is removed
469
469
time .sleep (5 )
470
470
471
+ def test_wellknown (self ):
472
+ class S (Strategy ):
473
+ def init (self ):
474
+ pass
475
+
476
+ def next (self ):
477
+ date = self .data .index [- 1 ]
478
+ if date == pd .Timestamp ('Thu 19 Oct 2006' ):
479
+ self .buy (stop = 484 , limit = 466 , size = 100 )
480
+ elif date == pd .Timestamp ('Thu 30 Oct 2007' ):
481
+ self .position .close ()
482
+ elif date == pd .Timestamp ('Tue 11 Nov 2008' ):
483
+ self .sell (stop = self .data .Low ,
484
+ limit = 324.90 , # High from 14 Nov
485
+ size = 200 )
486
+
487
+ bt = Backtest (GOOG , S , margin = .1 )
488
+ stats = bt .run ()
489
+ trades = stats ['_trades' ]
490
+
491
+ self .assertAlmostEqual (stats ['Equity Peak [$]' ], 46961 )
492
+ self .assertEqual (stats ['Equity Final [$]' ], 0 )
493
+ self .assertEqual (len (trades ), 2 )
494
+ assert trades [['EntryTime' , 'ExitTime' ]].equals (
495
+ pd .DataFrame (dict (EntryTime = pd .to_datetime (['2006-11-01' , '2008-11-14' ]),
496
+ ExitTime = pd .to_datetime (['2007-10-31' , '2009-09-21' ]))))
497
+ assert trades ['PnL' ].round ().equals (pd .Series ([23469. , - 34420. ]))
498
+
499
+ with _tempfile () as f :
500
+ bt .plot (filename = f , plot_drawdown = True , smooth_equity = False )
501
+ # Give browser time to open before tempfile is removed
502
+ time .sleep (1 )
503
+
471
504
def test_indicator_color (self ):
472
505
class S (Strategy ):
473
506
def init (self ):
0 commit comments