Skip to content

Commit d419732

Browse files
committed
TST: Add a more precise, well-known test for stop/limit
1 parent 506ebc6 commit d419732

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

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

0 commit comments

Comments
 (0)