Skip to content

Commit 56a01c0

Browse files
committed
TST: Add a more precise, well-known test for stop/limit
1 parent 8071168 commit 56a01c0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

backtesting/test/_test.py

+33
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,39 @@ def ok(x):
501501
# Give browser time to open before tempfile is removed
502502
time.sleep(5)
503503

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

0 commit comments

Comments
 (0)