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