Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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 AttributeError: module 'collections' has no attribute 'Iterable'

    General Code/Help
    4
    4
    1555
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Z
      zarbov last edited by

      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!

      DruculaC 1 Reply Last reply Reply Quote 0
      • DruculaC
        DruculaC @zarbov last edited by

        @zarbov
        https://github.com/nerdocs/pydifact/issues/46#issuecomment-981102362
        Python 3.9 have been deprecated Iteralbe from collections.

        C 1 Reply Last reply Reply Quote 1
        • C
          CDubya @DruculaC last edited by

          @druculac

          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

          1 Reply Last reply Reply Quote 0
          • R
            reisenmachtfreud last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors