@@ -71,6 +71,11 @@ def next(self):
7171 self .sell ()
7272
7373
74+ class _S (Strategy ):
75+ def init (self ):
76+ super ().init ()
77+
78+
7479class TestBacktest (TestCase ):
7580 def test_run (self ):
7681 bt = Backtest (EURUSD , SmaCross )
@@ -354,18 +359,12 @@ def next(self):
354359 if self .position :
355360 self .position .close ()
356361
357- class SinglePosition (Strategy ):
358- def init (self ):
359- pass
360-
362+ class SinglePosition (_S ):
361363 def next (self ):
362364 if not self .position :
363365 self .buy ()
364366
365- class NoTrade (Strategy ):
366- def init (self ):
367- pass
368-
367+ class NoTrade (_S ):
369368 def next (self ):
370369 pass
371370
@@ -383,16 +382,14 @@ def next(self):
383382 def test_trade_enter_hit_sl_on_same_day (self ):
384383 the_day = pd .Timestamp ("2012-10-17 00:00:00" )
385384
386- class S (Strategy ):
387- def init (self ): pass
388-
385+ class S (_S ):
389386 def next (self ):
390387 if self .data .index [- 1 ] == the_day :
391388 self .buy (sl = 720 )
392389
393390 self .assertEqual (Backtest (GOOG , S ).run ()._trades .iloc [0 ].ExitPrice , 720 )
394391
395- class S (S ):
392+ class S (_S ):
396393 def next (self ):
397394 if self .data .index [- 1 ] == the_day :
398395 self .buy (stop = 758 , sl = 720 )
@@ -401,9 +398,7 @@ def next(self):
401398 self .assertEqual (Backtest (GOOG , S ).run ()._trades .iloc [0 ].ExitPrice , 705.58 )
402399
403400 def test_stop_price_between_sl_tp (self ):
404- class S (Strategy ):
405- def init (self ): pass
406-
401+ class S (_S ):
407402 def next (self ):
408403 if self .data .index [- 1 ] == pd .Timestamp ("2004-09-09 00:00:00" ):
409404 self .buy (stop = 104 , sl = 103 , tp = 110 )
@@ -427,9 +422,7 @@ def next(self):
427422 bt .run ()
428423
429424 def test_close_orders_from_last_strategy_iteration (self ):
430- class S (Strategy ):
431- def init (self ): pass
432-
425+ class S (_S ):
433426 def next (self ):
434427 if not self .position :
435428 self .buy ()
@@ -440,9 +433,7 @@ def next(self):
440433 self .assertFalse (Backtest (SHORT_DATA , S , finalize_trades = True ).run ()._trades .empty )
441434
442435 def test_check_adjusted_price_when_placing_order (self ):
443- class S (Strategy ):
444- def init (self ): pass
445-
436+ class S (_S ):
446437 def next (self ):
447438 self .buy (tp = self .data .Close * 1.01 )
448439
@@ -722,10 +713,7 @@ def ok(x):
722713 time .sleep (5 )
723714
724715 def test_wellknown (self ):
725- class S (Strategy ):
726- def init (self ):
727- pass
728-
716+ class S (_S ):
729717 def next (self ):
730718 date = self .data .index [- 1 ]
731719 if date == pd .Timestamp ('Thu 19 Oct 2006' ):
@@ -1037,9 +1025,7 @@ def test_readme_contains_stats_keys(self):
10371025
10381026class TestRegressions (TestCase ):
10391027 def test_gh_521 (self ):
1040- class S (Strategy ):
1041- def init (self ): pass
1042-
1028+ class S (_S ):
10431029 def next (self ):
10441030 if self .data .Close [- 1 ] == 100 :
10451031 self .buy (size = 1 , sl = 90 )
@@ -1056,9 +1042,7 @@ def test_stats_annualized(self):
10561042 self .assertEqual (round (stats ['Return (Ann.) [%]' ]), - 3 )
10571043
10581044 def test_cancel_orders (self ):
1059- class S (Strategy ):
1060- def init (self ): pass
1061-
1045+ class S (_S ):
10621046 def next (self ):
10631047 self .buy (sl = 1 , tp = 1e3 )
10641048 if self .position :
@@ -1110,9 +1094,7 @@ def test_trades_dates_match_prices(self):
11101094 trades ['ExitPrice' ].tolist ())
11111095
11121096 def test_sl_always_before_tp (self ):
1113- class S (Strategy ):
1114- def init (self ): pass
1115-
1097+ class S (_S ):
11161098 def next (self ):
11171099 i = len (self .data .index )
11181100 if i == 4 :
@@ -1123,6 +1105,4 @@ def next(self):
11231105 t .tp = 107.9
11241106
11251107 trades = Backtest (SHORT_DATA , S ).run ()._trades
1126- (bt := Backtest (SHORT_DATA , S )).run ()
1127- bt .plot ()
11281108 self .assertEqual (trades ['ExitPrice' ].iloc [0 ], 104.95 )
0 commit comments