Skip to content

Commit 6d5e993

Browse files
committed
TST: Add a more precise, well-known test for stop/limit
1 parent a0f075b commit 6d5e993

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
@@ -468,6 +468,39 @@ def ok(x):
468468
# Give browser time to open before tempfile is removed
469469
time.sleep(5)
470470

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+
471504
def test_indicator_color(self):
472505
class S(Strategy):
473506
def init(self):

0 commit comments

Comments
 (0)