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



  • 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()
    

  • administrators

    @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.



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


Log in to reply
 

});