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/

    Drawdown Length Calculation

    Indicators/Strategies/Analyzers
    drawdown observers
    5
    16
    5331
    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.
    • A
      ab_trader last edited by ab_trader

      Hi all! I've coded an observer for calculation of the current drawdown length and maximum length of drawdown over the backtest. Any critique is welcomed.

      class DrawDownLength(bt.observers.DrawDown):
      
          lines = ('length', 'maxlength')
          plotinfo = dict(plotinfo=True, subplot=True)
      
          plotlines = dict(drawdown=dict(_plotskip='True',))
          plotlines = dict(maxlength=dict(_plotskip='True',))
      
          def __init__(self):
      
              super(DrawDownLength, self).__init__()
              self.length = 0
      
          def next(self):
      
              super(DrawDownLength, self).next()
      
              if self.lines.drawdown[0] != 0:
                  self.length += 1
              else:
                  self.length = 0
      
              self.lines.length[0] = self.length
              self.lines.maxlength[0] = max(self.length, self.lines.maxlength[-1])
      

      @backtrader

      If possible could you please include it as a standard bt package observer?

      1 Reply Last reply Reply Quote 2
      • tim3lord
        tim3lord last edited by

        Nice! Was looking at trying to make something like this myself. Thanks for sharing

        1 Reply Last reply Reply Quote 0
        • RandyT
          RandyT last edited by

          Nice work @ab_trader

          This was on my list of things to do.

          Oh! I have not enough reputation points to reply. :smile:
          Bad reputation more likely...

          1 Reply Last reply Reply Quote 0
          • J
            jaikumarm last edited by

            this is awesome, thanks for adding this!

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

              Thx for sharing. Since your were asking for comments I would suggest implementing the code as an analyzer. The observer simply initializes the analyzer and feeds its lines with the values generated by the analyzer.

              Some people may only want the analyzer and not the observer, because the final values may be sought for statistical purposes.

              Check the TimeReturn observer, which relies on the TimeReturn analyzer.

              A 1 Reply Last reply Reply Quote 0
              • A
                ab_trader @backtrader last edited by ab_trader

                @backtrader Thank you. I got your idea, but need to do more homework on observer - analyzer relationship.

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

                  In any case let me suggest that you:

                  • Create a pull request in the backtrader repository for integration

                  It is not because the code cannot be copied and pasted: it is to ensure attribution

                  You may also want to add a couple of lines with a docstring

                  Note

                  The DrawDown observer is in need of a due update to be re-implemented like the TimeReturn. Reasons:

                  • Separate the logic in Observer and Analyzer
                  • Be able to observe/analyze a timeframe different than the timeframe which the data has (data is in Days, but you want to get the maximum drawdown length in full weeks for example)

                  As such you may want to wait and then add your changes to the re-implementation.

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    ab_trader @backtrader last edited by

                    @backtrader

                    I've already coded new analyzer for drawdown calcs, and will use it from new Drawdown observer. Need to test it little bit before put here.

                    If you would be so kind and explain me how to create pull request, and what to do with it further, then I probably can do it. :) I am not a professional software developer. I believe that copy - paste in my case will be much faster. Anyway it will be new version of Drawdown analyzer & observer.

                    1 Reply Last reply Reply Quote 0
                    • RandyT
                      RandyT last edited by

                      @ab_trader to help with the burden placed on @backtrader, I'd be happy to help walk you through getting started with github and the pull request if that is of interest. Let me know in chat if I can help.

                      You'll need a github account and a fork of backtrader.

                      A 1 Reply Last reply Reply Quote 0
                      • A
                        ab_trader @RandyT last edited by

                        @RandyT i've created chat with you. Can you see it?

                        1 Reply Last reply Reply Quote 0
                        • A
                          ab_trader last edited by

                          @backtrader
                          I've added pull request for drawdown analyzer.

                          About observer - can I separate lines from one observer to several plots? Or it works as one observer = one plot?

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

                            Objects (observers, indicators, data feeds) are meant to be plotted on a single subplot. It can be influenced whether that's an own subplot or someone else's (that's why something like Stochastic defaults to a separate subplot and an SMA defaults to plot on the data on which is calculated)

                            Individual lines are not meant (and there is no support for it) to be plotted on its own at some to-be-chosen subplot. But depending on your needs all you may need is to change the way you plot things.

                            The BTFD blog post (sample is in the sources) doesn't plot the data or indicators: it simply plots an observer which constructs its values.

                            See here: https://www.backtrader.com/blog/posts/2016-12-26-btfd/btfd.html

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

                              The pull-request was integrated. Length and money ideas were added, but it has been reworked using the style of the TradeAnalyzer, because the values to keep pointed in that direction.

                              The DrawDown observer has been updated to use it (the old version is still available under DrawDown_Old. A chart comparing both (running the updated sample)

                              0_1484477884960_upload-c2341ec9-1b50-477e-af06-3162bd641308

                              A 1 Reply Last reply Reply Quote 0
                              • A
                                ab_trader @backtrader last edited by

                                @backtrader thank you!
                                I've actually created two separate observers: one fir new dd and one for length. But somehow my changes for them appeared in my fork but no pull requests were created. I'll try again to add drawdown length observer.

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

                                  Maybe you want to have a look at the current new DrawDown observer, which is extremely simplified and simply observes and from there derive the observer for the length.

                                  The commit in the development branch would take you here: https://github.com/mementum/backtrader/blob/6ac3ea989288a461c0b3e49c9d773e5680256155/backtrader/observers/drawdown.py

                                  A 1 Reply Last reply Reply Quote 0
                                  • A
                                    ab_trader @backtrader last edited by

                                    @backtrader I've looked on it, and I am going to use it as a baseline for dd length observer.

                                    1 Reply Last reply Reply Quote 0
                                    • 1 / 1
                                    • First post
                                      Last post
                                    Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                                    $(document).ready(function () { app.coldLoad(); }); }