Pyfolio integration in backtrader 1.9.36



  • It seems there been a change to pyfolio integration in Backtrader 1.9.36 that's not yet updated in the documentation.

    I was able to put together an analyzer for pyfolio using similar code to the pyfolio integration doc:

        ...
        results = cerebro.run()
            
        strat = results[0]
        pyfoliozer = strat.analyzers.getbyname('pyfolio')
        returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()
        
        return returns, positions, transactions
    if __name__ == '__main__':
        r, p, t = runstrat()
    

    But after updating to 1.9.36, the above code throws the following error:

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-24-01fb6dde8296> in <module>()
         72 
         73 if __name__ == '__main__':
    ---> 74     r, p, t = run_strat()
         75     #run_strat()
    
    <ipython-input-24-01fb6dde8296> in run_strat()
         66     cerebro.plot()
         67     strat = results[0]
    ---> 68     pyfoliozer = strat.analyzers.getbyname('pyfolio')
         69     returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()
         70 
    
    C:\Anaconda3\envs\backtrader\lib\site-packages\backtrader\metabase.py in getbyname(self, name)
        322 
        323     def getbyname(self, name):
    --> 324         idx = self._names.index(name)
        325         return self._items[idx]
    
    ValueError: 'pyfolio' is not in list
    

    I noticed that pyfolio.py has been added to the analyzers directory. So I made some change to the code using bt.analyzers.

        cerebro.addanalyzer(bt.analyzers.pyfolio, _name='pyfolio')
        
        results = cerebro.run()
            
        cerebro.plot()
        strat = results[0]
        returns, positions, transactions, gross_lev = strat.analyzers.pyfolio.get_pf_items()
        
        return returns, positions, transactions
    
    if __name__ == '__main__':
        r, p, t = run_strat()
    

    However, the modifed code throws me the following error. It seems that backtrader is having trouble adding bt.analyzers.pyfolio to the list of analyzers? What's the right way to add pyfolio analyzer?

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-4-96f6582b3ef5> in <module>()
         70 
         71 if __name__ == '__main__':
    ---> 72     r, p, t = run_strat()
         73     #run_strat()
    
    <ipython-input-4-96f6582b3ef5> in run_strat()
         61     cerebro.addanalyzer(bt.analyzers.pyfolio, _name='pyfolio')
         62 
    ---> 63     results = cerebro.run()
         64 
         65     cerebro.plot()
    
    C:\Anaconda3\envs\backtrader\lib\site-packages\backtrader\cerebro.py in run(self, **kwargs)
        792             # let's skip process "spawning"
        793             for iterstrat in iterstrats:
    --> 794                 runstrat = self.runstrategies(iterstrat)
        795                 self.runstrats.append(runstrat)
        796         else:
    
    C:\Anaconda3\envs\backtrader\lib\site-packages\backtrader\cerebro.py in runstrategies(self, iterstrat, predata)
        894 
        895                 for ancls, anargs, ankwargs in self.analyzers:
    --> 896                     strat._addanalyzer(ancls, *anargs, **ankwargs)
        897 
        898                 sizer, sargs, skwargs = self.sizers.get(idx, defaultsizer)
    
    C:\Anaconda3\envs\backtrader\lib\site-packages\backtrader\strategy.py in _addanalyzer(self, ancls, *anargs, **ankwargs)
        220         nsuffix = next(self._alnames[anname])
        221         anname += str(nsuffix or '')  # 0 (first instance) gets no suffix
    --> 222         analyzer = ancls(*anargs, **ankwargs)
        223         self.analyzers.append(analyzer, anname)
        224 
    
    TypeError: 'module' object is not callable
    

  • administrators

    It seems there been a change to pyfolio integration in Backtrader 1.9.36 that's not yet updated in the documentation.

    Which change?

    What's the right way to add pyfolio analyzer?

    Using the name of the analyzer PyFolioand not that of the internal module pyfolio. From the docs Docs - PyFolio Integration

    cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')
    

Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.