@@ -71,6 +71,11 @@ def next(self):
71
71
self .sell ()
72
72
73
73
74
+ class _S (Strategy ):
75
+ def init (self ):
76
+ super ().init ()
77
+
78
+
74
79
class TestBacktest (TestCase ):
75
80
def test_run (self ):
76
81
bt = Backtest (EURUSD , SmaCross )
@@ -354,18 +359,12 @@ def next(self):
354
359
if self .position :
355
360
self .position .close ()
356
361
357
- class SinglePosition (Strategy ):
358
- def init (self ):
359
- pass
360
-
362
+ class SinglePosition (_S ):
361
363
def next (self ):
362
364
if not self .position :
363
365
self .buy ()
364
366
365
- class NoTrade (Strategy ):
366
- def init (self ):
367
- pass
368
-
367
+ class NoTrade (_S ):
369
368
def next (self ):
370
369
pass
371
370
@@ -383,16 +382,14 @@ def next(self):
383
382
def test_trade_enter_hit_sl_on_same_day (self ):
384
383
the_day = pd .Timestamp ("2012-10-17 00:00:00" )
385
384
386
- class S (Strategy ):
387
- def init (self ): pass
388
-
385
+ class S (_S ):
389
386
def next (self ):
390
387
if self .data .index [- 1 ] == the_day :
391
388
self .buy (sl = 720 )
392
389
393
390
self .assertEqual (Backtest (GOOG , S ).run ()._trades .iloc [0 ].ExitPrice , 720 )
394
391
395
- class S (S ):
392
+ class S (_S ):
396
393
def next (self ):
397
394
if self .data .index [- 1 ] == the_day :
398
395
self .buy (stop = 758 , sl = 720 )
@@ -401,9 +398,7 @@ def next(self):
401
398
self .assertEqual (Backtest (GOOG , S ).run ()._trades .iloc [0 ].ExitPrice , 705.58 )
402
399
403
400
def test_stop_price_between_sl_tp (self ):
404
- class S (Strategy ):
405
- def init (self ): pass
406
-
401
+ class S (_S ):
407
402
def next (self ):
408
403
if self .data .index [- 1 ] == pd .Timestamp ("2004-09-09 00:00:00" ):
409
404
self .buy (stop = 104 , sl = 103 , tp = 110 )
@@ -427,9 +422,7 @@ def next(self):
427
422
bt .run ()
428
423
429
424
def test_close_orders_from_last_strategy_iteration (self ):
430
- class S (Strategy ):
431
- def init (self ): pass
432
-
425
+ class S (_S ):
433
426
def next (self ):
434
427
if not self .position :
435
428
self .buy ()
@@ -440,9 +433,7 @@ def next(self):
440
433
self .assertFalse (Backtest (SHORT_DATA , S , finalize_trades = True ).run ()._trades .empty )
441
434
442
435
def test_check_adjusted_price_when_placing_order (self ):
443
- class S (Strategy ):
444
- def init (self ): pass
445
-
436
+ class S (_S ):
446
437
def next (self ):
447
438
self .buy (tp = self .data .Close * 1.01 )
448
439
@@ -722,10 +713,7 @@ def ok(x):
722
713
time .sleep (5 )
723
714
724
715
def test_wellknown (self ):
725
- class S (Strategy ):
726
- def init (self ):
727
- pass
728
-
716
+ class S (_S ):
729
717
def next (self ):
730
718
date = self .data .index [- 1 ]
731
719
if date == pd .Timestamp ('Thu 19 Oct 2006' ):
@@ -1037,9 +1025,7 @@ def test_readme_contains_stats_keys(self):
1037
1025
1038
1026
class TestRegressions (TestCase ):
1039
1027
def test_gh_521 (self ):
1040
- class S (Strategy ):
1041
- def init (self ): pass
1042
-
1028
+ class S (_S ):
1043
1029
def next (self ):
1044
1030
if self .data .Close [- 1 ] == 100 :
1045
1031
self .buy (size = 1 , sl = 90 )
@@ -1056,9 +1042,7 @@ def test_stats_annualized(self):
1056
1042
self .assertEqual (round (stats ['Return (Ann.) [%]' ]), - 3 )
1057
1043
1058
1044
def test_cancel_orders (self ):
1059
- class S (Strategy ):
1060
- def init (self ): pass
1061
-
1045
+ class S (_S ):
1062
1046
def next (self ):
1063
1047
self .buy (sl = 1 , tp = 1e3 )
1064
1048
if self .position :
@@ -1110,9 +1094,7 @@ def test_trades_dates_match_prices(self):
1110
1094
trades ['ExitPrice' ].tolist ())
1111
1095
1112
1096
def test_sl_always_before_tp (self ):
1113
- class S (Strategy ):
1114
- def init (self ): pass
1115
-
1097
+ class S (_S ):
1116
1098
def next (self ):
1117
1099
i = len (self .data .index )
1118
1100
if i == 4 :
@@ -1123,6 +1105,4 @@ def next(self):
1123
1105
t .tp = 107.9
1124
1106
1125
1107
trades = Backtest (SHORT_DATA , S ).run ()._trades
1126
- (bt := Backtest (SHORT_DATA , S )).run ()
1127
- bt .plot ()
1128
1108
self .assertEqual (trades ['ExitPrice' ].iloc [0 ], 104.95 )
0 commit comments