optstrategy AttributeError: module 'collections' has no attribute 'Iterable'
-
Hi
i'm trying to optimize my strategy but keeps getting this error and i can't find anything on the web showing why I'm getting it.
My code is simple and loosely based on the getting started examples:from alpaca_trade_api.rest import REST, TimeFrame, TimeFrameUnit import backtrader as bt from config import API_KEY, SECRET_KEY class EMACross(bt.Strategy): params = dict( ema_short_period=5, ema_long_period=10 ) def __init__(self): self.order = None self.short_ma = bt.indicators.ExponentialMovingAverage(period=self.p.ema_short_period) self.long_ma = bt.indicators.ExponentialMovingAverage(period=self.p.ema_long_period) self.crossover = bt.ind.CrossOver(self.short_ma, self.long_ma) # crossover signal self.crossdown = bt.ind.CrossDown(self.short_ma, self.long_ma) self.crossdown.plotinfo.subplot = False self.crossover.plotinfo.subplot = False def next(self): self.log('Close, %.2f' % self.data.close[0]) if self.position.size > 0: if self.crossdown > 0: self.log('SELL CREATE, %.2f' % self.data.close[0]) self.close() else: if self.crossover > 0: self.log('BUY CREATE, %.2f' % self.data.close[0]) self.buy() def log(self, txt, dt=None): dt = dt or self.data.datetime.datetime() print('%s, %s' % (dt.isoformat(), txt)) def stop(self): self.log('(short EMA Period %2d) (long EMA Period %2d) Ending Value %.2f' % (self.p.ema_short_period, self.p.ema_long_period, self.broker.getvalue())) rest_api = REST(API_KEY, SECRET_KEY, 'https://paper-api.alpaca.markets') def run_backtest(strategy, symbols, start, end, timeframe, cash=100000): # initialize backtrader broker cerebro = bt.Cerebro() cerebro.broker.setcash(cash) cerebro.addsizer(bt.sizers.PercentSizer, percents=90) cerebro.optstrategy(strategy, ema_short_period=4, ema_long_period=6) # historical data request if type(symbols) == str: symbol = symbols alpaca_data = rest_api.get_bars(symbol, timeframe, start, end, adjustment='all').df data = bt.feeds.PandasData(dataname=alpaca_data, name=symbol) cerebro.adddata(data) elif type(symbols) == list or type(symbols) == set: for symbol in symbols: alpaca_data = rest_api.get_bars(symbol, timeframe, start, end, adjustment='all').df data = bt.feeds.PandasData(dataname=alpaca_data, name=symbol) cerebro.adddata(data) # run initial_portfolio_value = cerebro.broker.getvalue() print(f'Starting Portfolio Value: {initial_portfolio_value}') results = cerebro.run() final_portfolio_value = cerebro.broker.getvalue() print( f'Final Portfolio Value: {final_portfolio_value} ---> Return: {(final_portfolio_value / initial_portfolio_value - 1) * 100}%') run_backtest(EMACross, 'QQQ', '2018-01-01', '2022-01-01', TimeFrame(1, TimeFrameUnit.Day))
Running the script, i get this error:
Traceback (most recent call last): File "/Users/shohamshaulian/PycharmProjects/test3/main.py", line 79, in <module> run_backtest(EMACross, 'QQQ', '2018-01-01', '2022-01-01', TimeFrame(1, TimeFrameUnit.Day)) File "/Users/shohamshaulian/PycharmProjects/test3/main.py", line 54, in run_backtest cerebro.optstrategy(strategy, ema_short_period=4, ema_long_period=6) File "/Users/shohamshaulian/PycharmProjects/test3/venv/lib/python3.10/site-packages/backtrader/cerebro.py", line 893, in optstrategy vals = self.iterize(kwargs.values()) File "/Users/shohamshaulian/PycharmProjects/test3/venv/lib/python3.10/site-packages/backtrader/cerebro.py", line 333, in iterize elif not isinstance(elem, collections.Iterable): AttributeError: module 'collections' has no attribute 'Iterable' Process finished with exit code 1
When running the script without optstrategy but rather with addstrategy, evrything is working great. Only when changing to optstrategy is when i'm getting this error.
I also tried to run the same code on google colab (with optstrategy) and everything worked great there, so this got me really puzzled...
I'm running python 3.10 with PyCharm CE on macOS. Please let me know if any additional information needed in order to solve this issue.
Thanks in advance for any help!
-
@zarbov
https://github.com/nerdocs/pydifact/issues/46#issuecomment-981102362
Python 3.9 have been deprecated Iteralbe from collections. -
OK, dunno what I'm missing on this. It seems I am unclear on how to "fix" the collections.iterable problem.
I put in
from collections.abc import Callable, Iterable
and nothing changed. Python 3.1 and latest backtrader
and all of this still comes our screaming in RED at me.
File "C:/Users/cawort01/AppData/Local/Programs/Python/Python310/BT-Rookie_A.py", line 31, in <module>
cerebro.optstrategy(firstStrategy, period=range(14,21))
File "C:\Users\cawort01\AppData\Local\Programs\Python\Python310\lib\site-packages\backtrader\cerebro.py", line 895, in optstrategy
vals = self.iterize(kwargs.values())
File "C:\Users\cawort01\AppData\Local\Programs\Python\Python310\lib\site-packages\backtrader\cerebro.py", line 335, in iterize
elif not isinstance(elem, collections.Iterable):
AttributeError: module 'collections' has no attribute 'Iterable'what the hell am I misunderstanding??
Chris
-
Have the same issue. Since no one updates backtraders code anymore, I will install a python3.9 virtual environment using anaconda. On the long run, I need am actively maintained replacement for backtrader.