Skip to content

Commit 3058dbe

Browse files
committed
TST: Add a more precise, well-known test for stop/limit
1 parent 9ba4882 commit 3058dbe

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

475+
def test_wellknown(self):
476+
class S(Strategy):
477+
def init(self):
478+
pass
479+
480+
def next(self):
481+
date = self.data.index[-1]
482+
if date == pd.Timestamp('Thu 19 Oct 2006'):
483+
self.buy(stop=484, limit=466, size=100)
484+
elif date == pd.Timestamp('Thu 30 Oct 2007'):
485+
self.position.close()
486+
elif date == pd.Timestamp('Tue 11 Nov 2008'):
487+
self.sell(stop=self.data.Low,
488+
limit=324.90, # High from 14 Nov
489+
size=200)
490+
491+
bt = Backtest(GOOG, S, margin=.1)
492+
stats = bt.run()
493+
trades = stats['_trades']
494+
495+
self.assertAlmostEqual(stats['Equity Peak [$]'], 46961)
496+
self.assertEqual(stats['Equity Final [$]'], 0)
497+
self.assertEqual(len(trades), 2)
498+
assert trades[['EntryTime', 'ExitTime']].equals(
499+
pd.DataFrame(dict(EntryTime=pd.to_datetime(['2006-11-01', '2008-11-14']),
500+
ExitTime=pd.to_datetime(['2007-10-31', '2009-09-21']))))
501+
assert trades['PnL'].round().equals(pd.Series([23469., -34420.]))
502+
503+
with _tempfile() as f:
504+
bt.plot(filename=f, plot_drawdown=True, smooth_equity=False)
505+
# Give browser time to open before tempfile is removed
506+
time.sleep(1)
507+
475508
def test_indicator_color(self):
476509
class S(Strategy):
477510
def init(self):

0 commit comments

Comments
 (0)