1. 代碼問題
idx = cerebro.optstrategy(SwingStrategy, bar_executed_exist=days_step, drop_down_exist=-3, target_price_exist=8, rebound_exist=3, swing_period=days_step) for ucode, data0 in underlying.items(): cerebro.adddata(data0, name=ucode) thestrats = cerebro.run(maxcpus=8)
a. 使用optstrategy優化策略
b. 插入adddata多組數據
c. 執行maxcpus=8多線程
2. 報錯
multiprocessing.pool.MaybeEncodingError: Error sending result: '[<backtrader.cerebro.OptReturn object at 0x00000245B18776D8>]'. Reason: 'PicklingError("Can't pickle <class 'backtrader.metabase.AutoInfoClass_7555baaa62804eda80c93bd4267445d7_2800.HK'>: attribute lookup AutoInfoClass_7555baaa62804eda80c93bd4267445d7_2800.HK on backtrader.metabase failed",)'
3. 解決
thestrats = cerebro.run(maxcpus=1)
由於多線程時執行多組數據會出錯,所以強制禁止多線程
Update 2022-12-06
Backtrader - multiprocessing.pool.MaybeEncodingError: Error sending result