11import logging
22import os
33import platform
4+ import time
45import traceback
56from multiprocessing import Pool , cpu_count , set_start_method
67from multiprocessing .pool import ApplyResult
@@ -26,8 +27,8 @@ def _start_with_param_data(config: StrategyConfig, data: BacktestData, strategy:
2627 return _start (config , data , strategy , bk_config )
2728
2829
29- def _start (config : StrategyConfig , data : BacktestData , strategy : Strategy , bk_config : BacktestConfig ) -> Actuator :
30- logger .info (f"Process id: { os .getpid ()} , id of data object { id (data )} " )
30+ def _start (config : StrategyConfig , data : BacktestData , strategy : Strategy , bk_config : BacktestConfig ):
31+ logger .info (f"Start with process id: { os .getpid ()} , id of data object { id (data )} " )
3132 actuator = Actuator ()
3233 for market in config .markets :
3334 # add market to broker
@@ -42,9 +43,6 @@ def _start(config: StrategyConfig, data: BacktestData, strategy: Strategy, bk_co
4243 actuator .print_action = bk_config .print_actions
4344 actuator .interval = bk_config .interval
4445 actuator .run (bk_config .print_result )
45- if bk_config .callback is not None :
46- bk_config .callback (actuator )
47- return actuator
4846
4947
5048def e_callback (e ):
@@ -78,14 +76,14 @@ def run(self):
7876 raise RuntimeError ("Config has not set" )
7977 if self .data is None :
8078 raise RuntimeError ("Data has not set" )
81-
79+ start_time = time . time () # 1681718968.267463
8280 if len (self .strategies ) < 1 :
8381 return
8482 elif len (self .strategies ) == 1 or self .threads == 1 :
8583 # start in single thread by default
8684 for strategy in self .strategies :
87- _start_with_param_data (self .config , self .data , strategy , self .backtest_config )
88-
85+ actuator = _start_with_param_data (self .config , self .data , strategy , self .backtest_config )
86+ e_callback ( actuator )
8987 else :
9088 if self .threads > cpu_count ():
9189 raise RuntimeError ("Threads should lower than " + cpu_count ())
@@ -105,7 +103,7 @@ def run(self):
105103 tasks .append (result1 )
106104 [x .wait () for x in tasks ]
107105 else :
108- set_start_method ("fork" ) # ensure linux and macos have the same behavior
106+ set_start_method ("fork" ) # ensure linux and macos have the same behavior
109107 global global_data
110108 global_data = self .data # to keep there only one instance among processes
111109 with Pool (processes = self .threads ) as pool :
@@ -123,3 +121,4 @@ def run(self):
123121 tasks .append (result1 )
124122 [x .wait () for x in tasks ]
125123 pass
124+ logger .info (f"All backtest finished, total execute time { (time .time () - start_time ):.3f} s" )
0 commit comments