Backtrader Community

    • 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/

    Exception when use optstrategy together addwriter

    General Code/Help
    2
    3
    529
    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.
    • phxz
      phxz last edited by

      I'm trying to use the bt.WriterFile but for some reason when I add this option together with cerebro.optstrategy I receive the following exception:

      Traceback (most recent call last):
        File "/Users/phxz/Dropbox/Trading/backtrader/training/sample.py", line 24, in <module>
          cerebro.run()
        File "/Users/phxz/Dropbox/Trading/backtrader/venv/lib/python3.7/site-packages/backtrader-1.9.70.122-py3.7.egg/backtrader/cerebro.py", line 1143, in run
          for r in pool.imap(self, iterstrats):
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 774, in next
          raise value
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 457, in _handle_tasks
          put(task)
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/connection.py", line 206, in send
          self._send_bytes(_ForkingPickler.dumps(obj))
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/reduction.py", line 51, in dumps
          cls(buf, protocol).dump(obj)
      TypeError: cannot serialize '_io.TextIOWrapper' object
      

      Commenting both optstrategy or addwriter the code works perfectly!

      import backtrader as bt
      from datetime import datetime
      
      class SmaCross(bt.SignalStrategy):
          params = (('pfast', 10), ('pslow', 30),)
      
          def __init__(self):
              sma1, sma2 = bt.ind.SMA(period=self.p.pfast), bt.ind.SMA(period=self.p.pslow)
              self.signal_add(bt.SIGNAL_LONG, bt.ind.CrossOver(sma1, sma2))
      
      
      cerebro = bt.Cerebro()
      
      data = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2011, 1, 1),
                                       todate=datetime(2012, 12, 31))
      cerebro.adddata(data)
      cerebro.optstrategy(
          SmaCross,
          pfast=range(6, 16, 2),
          pslow=range(15, 40, 5),
      )
      cerebro.addstrategy(SmaCross)
      #cerebro.addwriter(bt.WriterFile, csv=True, out="test")
      cerebro.run()
      
      import backtrader as bt
      from datetime import datetime
      
      class SmaCross(bt.SignalStrategy):
          params = (('pfast', 10), ('pslow', 30),)
      
          def __init__(self):
              sma1, sma2 = bt.ind.SMA(period=self.p.pfast), bt.ind.SMA(period=self.p.pslow)
              self.signal_add(bt.SIGNAL_LONG, bt.ind.CrossOver(sma1, sma2))
      
      
      cerebro = bt.Cerebro()
      
      data = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2011, 1, 1),
                                       todate=datetime(2012, 12, 31))
      cerebro.adddata(data)
      cerebro.optstrategy(
          SmaCross,
          pfast=range(6, 16, 2),
          pslow=range(15, 40, 5),
      )
      cerebro.addstrategy(SmaCross)
      cerebro.addwriter(bt.WriterFile, csv=True, out="test")
      cerebro.run()
      
      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @phxz last edited by

        @phxz said in Exception when use optstrategy together addwriter:

        I'm trying to use the bt.WriterFile but for some reason

        It's not for some reason.

        @phxz said in Exception when use optstrategy together addwriter:

        TypeError: cannot serialize '_io.TextIOWrapper' object
        

        @phxz said in Exception when use optstrategy together addwriter:

        cerebro.addwriter(bt.WriterFile, csv=True, out="test")
        

        Multiple processes would be writing to the same file ... you'd better complain to the devils of multiple concurrent processes or to the operating system vendor.

        1 Reply Last reply Reply Quote 0
        • phxz
          phxz last edited by

          import backtrader as bt
          from datetime import datetime
          
          class SmaCross(bt.SignalStrategy):
              params = (('pfast', 10), ('pslow', 30),)
          
              def __init__(self):
                  sma1, sma2 = bt.ind.SMA(period=self.p.pfast), bt.ind.SMA(period=self.p.pslow)
                  self.signal_add(bt.SIGNAL_LONG, bt.ind.CrossOver(sma1, sma2))
          
          
          cerebro = bt.Cerebro(maxcpus=1, writer=True)
          
          data = bt.feeds.YahooFinanceData(dataname='MSFT', 
                                           fromdate=datetime(2011, 1, 1),
                                           todate=datetime(2012, 12, 31))
          cerebro.adddata(data)
          cerebro.optstrategy(
              SmaCross,
              pfast=range(6, 16, 2),
              pslow=range(15, 40, 5),
          )
          cerebro.addstrategy(SmaCross)
          cerebro.addwriter(bt.WriterFile, csv=True, out="test")
          cerebro.run()
          

          I was able to save the data after enable maxcpus=1 and set the writer opt as a True.

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