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/

    How can I use Pyfolio with backtrader?

    Indicators/Strategies/Analyzers
    3
    4
    2914
    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.
    • Michael Scofield
      Michael Scofield last edited by

      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?

      Thanks!

      1 Reply Last reply Reply Quote 0
      • L
        lvaghi last edited by

        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
            cerebro.addstrategy(TestStrategy,df_alloc)
            # Analyzer
            cerebro.addanalyzer(btanalyzers.SharpeRatio, _name='mysharpe')
            cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')
              # Set our desired cash start
            cerebro.broker.setcash(initcash)
            cerebro.broker.setcommission(commission=0.0)
        
            # Print out the starting conditions
            print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
        
            # Run over everything
            strat = cerebro.run()
            #cerebro.plot(numfigs = 1,grid = True,iplot=True)
            # Print out the final result
            print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
            print('Sharpe Ratio:', strat[0].analyzers.mysharpe.get_analysis())
            strat[0].analyzers.mysharpe.pprint()
            pyfoliozer = strat[0].analyzers.getbyname('pyfolio')
            returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()
            import pyfolio as pf
            pf.create_full_tear_sheet(
                returns,
                positions=positions,
                transactions=transactions,
                #gross_lev=gross_lev,
                live_start_date='2008-01-01',  # This date is sample specific
                round_trips=True)
        
            pf.create_simple_tear_sheet(returns)
            pf.create_returns_tear_sheet(returns)
        
        

        Of course you will have to install pyfolio! The instruction are here: https://quantopian.github.io/pyfolio/

        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:
        https://community.backtrader.com/topic/670/it-s-here-a-beta-you-can-use-right-now-essential-trade-statistics-all-in-one-place
        Only after screening with this tool I would adventure into the fine tuning of a strategy with pyfolio.
        Good luck...

        赵锴 2 Replies Last reply Reply Quote 0
        • 赵锴
          赵锴 @lvaghi last edited by

          @lvaghi
          Thank you for your answer ,But when I run these codes below, my notebook cannot finish the excutation:

          pf.create_full_tear_sheet(
                  returns,
                  positions=positions,
                  transactions=transactions,
                  #gross_lev=gross_lev,
                  live_start_date='2008-01-01',  # This date is sample specific
                  round_trips=True)
          
              pf.create_simple_tear_sheet(returns)
              pf.create_returns_tear_sheet(returns)
          

          The figures will not come out ,only statistics tables show below like this :
          ![alt text](98b4e554-3d0b-45d1-aab7-ae2817d28085-image.png image url)

          1 Reply Last reply Reply Quote 0
          • 赵锴
            赵锴 @lvaghi last edited by

            @lvaghi
            After ten thousand seconds, Finally the excutation finish with Error below:

            ValueError                                Traceback (most recent call last)
            <ipython-input-24-96657bf07fb3> in <module>
                 10     #gross_lev = gross_lev,
                 11     live_start_date='2010-05-01',
            ---> 12     round_trips=True)
            
            E:\anaconda\lib\site-packages\pyfolio\tears.py in create_full_tear_sheet(returns, positions, transactions, market_data, benchmark_rets, slippage, live_start_date, sector_mappings, bayesian, round_trips, estimate_intraday, hide_positions, cone_std, bootstrap, unadjusted_returns, style_factor_panel, sectors, caps, shares_held, volumes, percentile, turnover_denom, set_context, factor_returns, factor_loadings, pos_in_dollars, header_rows, factor_partitions)
                233                     transactions=transactions,
                234                     sector_mappings=sector_mappings,
            --> 235                     estimate_intraday=False)
                236 
                237             if market_data is not None:
            
            E:\anaconda\lib\site-packages\pyfolio\plotting.py in call_w_context(*args, **kwargs)
                 50         if set_context:
                 51             with plotting_context(), axes_style():
            ---> 52                 return func(*args, **kwargs)
                 53         else:
                 54             return func(*args, **kwargs)
            
            E:\anaconda\lib\site-packages\pyfolio\tears.py in create_round_trip_tear_sheet(returns, positions, transactions, sector_mappings, estimate_intraday, return_fig)
                850         return
                851 
            --> 852     round_trips.print_round_trip_stats(trades)
                853 
                854     plotting.show_profit_attribution(trades)
            
            E:\anaconda\lib\site-packages\pyfolio\round_trips.py in print_round_trip_stats(round_trips, hide_pos)
                397     """
                398 
            --> 399     stats = gen_round_trip_stats(round_trips)
                400 
                401     print_table(stats['summary'], float_format='{:.2f}'.format,
            
            E:\anaconda\lib\site-packages\pyfolio\round_trips.py in gen_round_trip_stats(round_trips)
                368 
                369     stats = {}
            --> 370     stats['pnl'] = agg_all_long_short(round_trips, 'pnl', PNL_STATS)
                371     stats['summary'] = agg_all_long_short(round_trips, 'pnl',
                372                                           SUMMARY_STATS)
            
            E:\anaconda\lib\site-packages\pyfolio\round_trips.py in agg_all_long_short(round_trips, col, stats_dict)
                 81                  .T
                 82                  .rename_axis({1.0: 'All trades'},
            ---> 83                               axis='columns'))
                 84     stats_long_short = (round_trips
                 85                         .groupby('long')[col]
            
            E:\anaconda\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
                219         @wraps(func)
                220         def wrapper(*args, **kwargs):
            --> 221             return func(*args, **kwargs)
                222 
                223         kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
            
            E:\anaconda\lib\site-packages\pandas\core\generic.py in rename_axis(self, mapper, **kwargs)
               1326                 return self._set_axis_name(mapper, axis=axis, inplace=inplace)
               1327             else:
            -> 1328                 raise ValueError("Use `.rename` to alter labels " "with a mapper.")
               1329         else:
               1330             # Use new behavior.  Means that index and/or columns
            
            ValueError: Use `.rename` to alter labels with a mapper.
            
            
            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors