For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

How can I use Pyfolio with backtrader?

  • So, as the document mentioned that the API of pyfolio changed.

    I would like to know how can I integrate pyfolio with backtrader.

    Can someone give me an example to explain the procedure of integrating pyfolio with backtrader?


  • Hi Michael,
    I see that nobody answered your question. So I think the following lines of coding
    could be a starting point for you:

        # Add a strategy
        # Analyzer
        cerebro.addanalyzer(btanalyzers.SharpeRatio, _name='mysharpe')
        cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')
          # Set our desired cash start
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' %
        # Run over everything
        strat =
        #cerebro.plot(numfigs = 1,grid = True,iplot=True)
        # Print out the final result
        print('Final Portfolio Value: %.2f' %
        print('Sharpe Ratio:', strat[0].analyzers.mysharpe.get_analysis())
        pyfoliozer = strat[0].analyzers.getbyname('pyfolio')
        returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()
        import pyfolio as pf
            live_start_date='2008-01-01',  # This date is sample specific

    Of course you will have to install pyfolio! The instruction are here:

    Pyfolio will give you lots of information (not all of unquestionable utility). I think it is more important to
    get first a good synthetic view, and for this I like (and strongly suggest) the one contributed by Richard O' Regan:
    Only after screening with this tool I would adventure into the fine tuning of a strategy with pyfolio.
    Good luck...