@@ -502,6 +502,39 @@ def ok(x):
502
502
# Give browser time to open before tempfile is removed
503
503
time .sleep (5 )
504
504
505
+ def test_wellknown (self ):
506
+ class S (Strategy ):
507
+ def init (self ):
508
+ pass
509
+
510
+ def next (self ):
511
+ date = self .data .index [- 1 ]
512
+ if date == pd .Timestamp ('Thu 19 Oct 2006' ):
513
+ self .buy (stop = 484 , limit = 466 , size = 100 )
514
+ elif date == pd .Timestamp ('Thu 30 Oct 2007' ):
515
+ self .position .close ()
516
+ elif date == pd .Timestamp ('Tue 11 Nov 2008' ):
517
+ self .sell (stop = self .data .Low ,
518
+ limit = 324.90 , # High from 14 Nov
519
+ size = 200 )
520
+
521
+ bt = Backtest (GOOG , S , margin = .1 )
522
+ stats = bt .run ()
523
+ trades = stats ['_trades' ]
524
+
525
+ self .assertAlmostEqual (stats ['Equity Peak [$]' ], 46961 )
526
+ self .assertEqual (stats ['Equity Final [$]' ], 0 )
527
+ self .assertEqual (len (trades ), 2 )
528
+ assert trades [['EntryTime' , 'ExitTime' ]].equals (
529
+ pd .DataFrame (dict (EntryTime = pd .to_datetime (['2006-11-01' , '2008-11-14' ]),
530
+ ExitTime = pd .to_datetime (['2007-10-31' , '2009-09-21' ]))))
531
+ assert trades ['PnL' ].round ().equals (pd .Series ([23469. , - 34420. ]))
532
+
533
+ with _tempfile () as f :
534
+ bt .plot (filename = f , plot_drawdown = True , smooth_equity = False )
535
+ # Give browser time to open before tempfile is removed
536
+ time .sleep (1 )
537
+
505
538
def test_indicator_color (self ):
506
539
class S (Strategy ):
507
540
def init (self ):
0 commit comments