1
1
import inspect
2
+ import multiprocessing as mp
2
3
import os
3
4
import sys
4
5
import time
@@ -287,7 +288,7 @@ def test_compute_drawdown(self):
287
288
def test_compute_stats (self ):
288
289
stats = Backtest (GOOG , SmaCross , finalize_trades = True ).run ()
289
290
expected = pd .Series ({
290
- # NOTE: These values are also used on the website!
291
+ # NOTE: These values are also used on the website! # noqa: E126
291
292
'# Trades' : 66 ,
292
293
'Avg. Drawdown Duration' : pd .Timedelta ('41 days 00:00:00' ),
293
294
'Avg. Drawdown [%]' : - 5.925851581948801 ,
@@ -629,7 +630,8 @@ def test_optimize_speed(self):
629
630
bt .optimize (fast = range (2 , 20 , 2 ), slow = range (10 , 40 , 2 ))
630
631
end = time .process_time ()
631
632
print (end - start )
632
- self .assertLess (end - start , .3 )
633
+ handicap = 5 if 'win' in sys .platform else .1
634
+ self .assertLess (end - start , .3 + handicap )
633
635
634
636
635
637
class TestPlot (TestCase ):
@@ -932,7 +934,7 @@ def next(self):
932
934
self .assertEqual (stats ['# Trades' ], 56 )
933
935
934
936
def test_FractionalBacktest (self ):
935
- ubtc_bt = FractionalBacktest (BTCUSD ['2015' :], SmaCross , fractional_unit = 1 / 1e6 , cash = 100 )
937
+ ubtc_bt = FractionalBacktest (BTCUSD ['2015' :], SmaCross , fractional_unit = 1 / 1e6 , cash = 100 )
936
938
stats = ubtc_bt .run (fast = 2 , slow = 3 )
937
939
self .assertEqual (stats ['# Trades' ], 41 )
938
940
trades = stats ['_trades' ]
@@ -942,13 +944,20 @@ def test_FractionalBacktest(self):
942
944
self .assertAlmostEqual (stats ['_strategy' ]._indicators [0 ][trade ['EntryBar' ]], 234.14 )
943
945
944
946
def test_MultiBacktest (self ):
945
- btm = MultiBacktest ([GOOG , EURUSD , BTCUSD ], SmaCross , cash = 100_000 )
946
- res = btm .run (fast = 2 )
947
- self .assertIsInstance (res , pd .DataFrame )
948
- self .assertEqual (res .columns .tolist (), [0 , 1 , 2 ])
949
- heatmap = btm .optimize (fast = [2 , 4 ], slow = [10 , 20 ])
950
- self .assertIsInstance (heatmap , pd .DataFrame )
951
- self .assertEqual (heatmap .columns .tolist (), [0 , 1 , 2 ])
947
+ import backtesting
948
+ assert callable (getattr (backtesting , 'Pool' , None )), backtesting .__dict__
949
+ for start_method in mp .get_all_start_methods ():
950
+ with self .subTest (start_method = start_method ), \
951
+ patch (backtesting , 'Pool' , mp .get_context (start_method ).Pool ):
952
+ start_time = time .monotonic ()
953
+ btm = MultiBacktest ([GOOG , EURUSD , BTCUSD ], SmaCross , cash = 100_000 )
954
+ res = btm .run (fast = 2 )
955
+ self .assertIsInstance (res , pd .DataFrame )
956
+ self .assertEqual (res .columns .tolist (), [0 , 1 , 2 ])
957
+ heatmap = btm .optimize (fast = [2 , 4 ], slow = [10 , 20 ])
958
+ self .assertIsInstance (heatmap , pd .DataFrame )
959
+ self .assertEqual (heatmap .columns .tolist (), [0 , 1 , 2 ])
960
+ print (start_method , time .monotonic () - start_time )
952
961
plot_heatmaps (heatmap .mean (axis = 1 ), open_browser = False )
953
962
954
963
@@ -1009,7 +1018,6 @@ def test_indicators_picklable(self):
1009
1018
class TestDocs (TestCase ):
1010
1019
DOCS_DIR = os .path .join (os .path .dirname (__file__ ), '..' , '..' , 'doc' )
1011
1020
1012
- @unittest .skipIf ('win' in sys .platform , "Locks up with `ModuleNotFoundError: No module named '<run_path>'`" )
1013
1021
@unittest .skipUnless (os .path .isdir (DOCS_DIR ), "docs dir doesn't exist" )
1014
1022
def test_examples (self ):
1015
1023
examples = glob (os .path .join (self .DOCS_DIR , 'examples' , '*.py' ))
@@ -1127,3 +1135,9 @@ def next(self):
1127
1135
trades = Backtest (SHORT_DATA , S ).run ()._trades
1128
1136
self .assertEqual (trades ['ExitBar' ].iloc [0 ], 3 )
1129
1137
self .assertEqual (trades ['ExitPrice' ].iloc [0 ], 105 )
1138
+
1139
+ def test_optimize_datetime_index_with_timezone (self ):
1140
+ data : pd .DataFrame = GOOG .iloc [:100 ]
1141
+ data .index = data .index .tz_localize ('Asia/Kolkata' )
1142
+ res = Backtest (data , SmaCross ).optimize (fast = range (2 , 3 ), slow = range (4 , 5 ))
1143
+ self .assertGreater (res ['# Trades' ], 0 )
0 commit comments