【问题标题】:How to use multiprocessing in optimize backtesting.py library如何在优化 backtesting.py 库中使用多处理
【发布时间】:2023-01-28 22:05:36
【问题描述】:

我正在使用 backtesting.py python 库进行交易策略评估。 库中有一个很棒的函数,可以让您优化交易参数的组合。

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)

但是当数据集很大时,给出结果需要很多时间。 我认为 multiprocessing 可以提供很多帮助,但不知道如何让它与库一起工作。 我认为多处理是在源代码中实现的,但它需要一些配置才能启用。 这是来自源代码:

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]

有人能帮忙吗?

【问题讨论】:

    标签: python algorithmic-trading back-testing


    【解决方案1】:

    您可以在顶部添加这些行:

    import multiprocessing as mp
    mp.set_start_method('fork')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      相关资源
      最近更新 更多