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/

    Tearsheets - QSTrader

    General Code/Help
    4
    6
    2832
    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.
    • O
      Osofuego last edited by Osofuego

      Re: Getting to portfolio performance data.

      Since pyfolio is iffy with its tear sheets and QSTrader's tearsheet seems to be pretty nice, is there any update on this?

      I find tearsheets, esp monthly PnL's to be helpful. I am not that experienced but would gladly help either test or code some of this port unleass this is already done?

      Richard O'Regan 1 Reply Last reply Reply Quote 1
      • Richard O'Regan
        Richard O'Regan @Osofuego last edited by Richard O'Regan

        @Osofuego Hi, I googled the QST tearsheet. Obviously would save time to just be able to connect to this. But also, it wouldn't be that hard for us to code our own. Basic charts and basic stats. What do most people do for viewing performance figures currently?

        You can take the man out of the army, but you can't take the army out of the man.

        1 Reply Last reply Reply Quote 0
        • C
          CptanPanic last edited by

          I'm just wondering what you mean by pyfolio tear-sheets are 'iffy'?

          O 1 Reply Last reply Reply Quote 1
          • O
            Osofuego @CptanPanic last edited by

            @CptanPanic
            The output is rather clunky and provides so much useless information. Also, the API has changed and the integration with backtrader is now broken from what I can tell.

            @Richard-O-Regan I currently use some of the backtrader stats but find they are missing some useful things (ie. returns by month) and bm performance.

            B 1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators @Osofuego last edited by

              @Osofuego said in Tearsheets - QSTrader:

              returns by month

              Use the TimeReturn analyzer with bt.TimeFrame.Months. You can also calculate weekly or daily returns if you wish. Or LogReturnsRolling which does the same but using a logarithmic approach.

              Docs - Analyzers Reference

              1 Reply Last reply Reply Quote 1
              • O
                Osofuego last edited by Osofuego

                Thx @backtrader for the guidance.

                I have adapted the package monthly_returns to backtrader using backtraders TimeReturn to create a heatmap of monthly returns. Maybe this will help somebody.

                Do you know if pyfolio is going to be re-integrated in backtrader anytime soon? Thx for all your work!

                I have only included the relevant parts (imports, adding analyzer, and then the code i adapted).

                import matplotlib.pyplot as plt
                import seaborn as sns
                import collections
                import pandas as pd
                
                ......
                #add analyzer
                cerebro.addanalyzer(bt.analyzers.TimeReturn, _name='myreturntme',timeframe=bt.TimeFrame.Months)
                .....
                
                #Get analyzer analysis
                tmereturn = strat.analyzers.myreturntme.get_analysis()
                
                
                ....
                # empty list for datetime index
                    lst_indx = []
                    # empty list for monthly retuns
                    lst_mnth_ret = []
                
                    #iterate through returns
                    for key, value in tmereturn.items():
                        lst_indx.append(key)
                        lst_mnth_ret.append(value)
                
                
                    #Create dataframe for returns
                    df_ret_mnth = pd.DataFrame(lst_mnth_ret, index=lst_indx)
                    #Change label of column
                    df_ret_mnth.columns = ['Returns']
                    #multiply returns by 100 
                    df_ret_mnth['Returns'] = df_ret_mnth['Returns'] *100
                    #Add year month info
                    df_ret_mnth['Year'] = df_ret_mnth.index.strftime('%Y')
                    df_ret_mnth['Month'] = df_ret_mnth.index.strftime('%b')
                    # make pivot table
                    df_ret_mnth = df_ret_mnth.pivot('Year', 'Month', 'Returns').fillna(0)
                    df_ret_mnth = df_ret_mnth[['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                                       'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']]
                
                    #Get Plot size
                    size = list(plt.gcf().get_size_inches())
                    figsize = (size[0], size[0] // 2)
                    plt.close()
                    #Create subplot
                    fig, ax = plt.subplots(figsize=figsize)
                    #create heatmap
                    ax = sns.heatmap(df_ret_mnth, ax=ax, annot=True,
                                     annot_kws={"size": 10}, fmt="0.2f", linewidths=0.5,
                                     square=False, cbar=True, cmap='RdYlGn')
                    ax.set_title('Monthly Returns (%)', fontsize=12, color='black', fontweight="bold")
                
                    fig.subplots_adjust(hspace=0)
                    plt.yticks(rotation=0)
                    plt.show()
                    plt.close()
                

                Here is the result 0_1506527091752_Backtrader Heatmap.png

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