For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
optstrategy() get error, help me, thanks.
-
my code :
import backtrader as bt cerebro = bt.Cerebro() # ---read Data Path = "C:\\Users\\i2011\\OneDrive\\Book_Code&Data\\量化投资以python为工具\\数据及源代码\\033" CJSecurities = pd.read_csv(Path + '\\CJSecurities.csv', index_col=1, parse_dates=True) CJSecurities = CJSecurities.iloc[:, 1:] data0 = CJSecurities if "openinterest" not in data0.columns: data0['openinterest'] = 0 data = bt.feeds.PandasData(dataname=data0) # --- cerebro.adddata(data) cerebro.broker.setcash(10000.0) # --- class TestStrategy(bt.Strategy): params = ( ('maperiod', 15), ) def __init__(self): self.dataclose = self.datas[0].close self.sma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.maperiod) self.bars_executed = 0 def next(self): if not self.position: if self.dataclose[0] > self.sma[0]: self.buy() else: if len(self) >= self.bars_executed + 5: self.sell() def notify_order(self, order): self.bars_executed = len(self) def stop(self): print(self.params.maperiod, self.broker.getvalue()) # ------ # cerebro.addstrategy(TestStrategy) --> addstrategy() can work with run(). cerebro.optstrategy(TestStrategy,maperiod=range(5, 10)) # --> This optstrategy() get error cerebro.run()
the error:
Process SpawnPoolWorker-4: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)> Process SpawnPoolWorker-2: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)> Process SpawnPoolWorker-5: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)> Process SpawnPoolWorker-3: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)> Process SpawnPoolWorker-1: Traceback (most recent call last): File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, **self._kwargs) File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\pool.py", line 110, in worker task = get() File "C:\Users\i2011\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 354, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (built-in)>
-
Your strategy is not defined when being unpickled in a slave process due to the "strange" ordering in your code.
I would suggest you look at the sample and use a conventional ordering, with "declarations" first and "execution" afterwards.