'How to use multiprocessing in optimize backtesting.py library

I'm using backtesting.py python library for my trading strategies assessments. There is a great function from library that allows you to optimize a combination of trading parameters.

stats, heatmap = bt.optimize(take_profit=np.arange(1, 8, 1).tolist(),
                                 deviation=np.arange(1, 8, 1).tolist(),
                                 percent=np.arange(5, 20, 5).tolist(),
                                 maximize="Equity Final [$]",
                                 method="skopt",
                                 max_tries=200,
                                 return_heatmap=True)

but when the dataset is large, it takes a lot of time to give the result. I think multiprocessing can help a lot but don't know how to make it work with library. I think multiprocessing is implemented inside source code but it needs some configuration to be on. this is from source code:

try:
            # If multiprocessing start method is 'fork' (i.e. on POSIX), use
            # a pool of processes to compute results in parallel.
            # Otherwise (i.e. on Windos), sequential computation will be "faster".
            if mp.get_start_method(allow_none=False) == 'fork':
                with ProcessPoolExecutor() as executor:
                    futures = [executor.submit(Backtest._mp_task, backtest_uuid, i)
                               for i in range(len(param_batches))]
                    for future in _tqdm(as_completed(futures), total=len(futures),
                                        desc='Backtest.optimize'):
                        batch_index, values = future.result()
                        for value, params in zip(values, param_batches[batch_index]):
                            heatmap[tuple(params.values())] = value
            else:
                if os.name == 'posix':
                    warnings.warn("For multiprocessing support in `Backtest.optimize()` "
                                  "set multiprocessing start method to 'fork'.")
                for batch_index in _tqdm(range(len(param_batches))):
                    _, values = Backtest._mp_task(backtest_uuid, batch_index)
                    for value, params in zip(values, param_batches[batch_index]):
                        heatmap[tuple(params.values())] = value
        finally:
            del Backtest._mp_backtests[backtest_uuid]

can anyone help with this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source