Skip to content

Commit b01efe5

Browse files
author
goblincomet
committed
TST: Add a more precise, well-known test for stop/limit
1 parent 50ddd38 commit b01efe5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

backtesting/test/_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,39 @@ def ok(x):
502502
# Give browser time to open before tempfile is removed
503503
time.sleep(5)
504504

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+
505538
def test_indicator_color(self):
506539
class S(Strategy):
507540
def init(self):

0 commit comments

Comments
 (0)