Drawdown Length Calculation
-
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])
If possible could you please include it as a standard
bt
package observer? -
Nice! Was looking at trying to make something like this myself. Thanks for sharing
-
Nice work @ab_trader
This was on my list of things to do.
Oh! I have not enough reputation points to reply.
Bad reputation more likely... -
this is awesome, thanks for adding this!
-
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 theTimeReturn
analyzer. -
@backtrader Thank you. I got your idea, but need to do more homework on observer - analyzer relationship.
-
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 theTimeReturn
. Reasons:- Separate the logic in
Observer
andAnalyzer
- 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.
-
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.
-
@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.
-
@RandyT i've created chat with you. Can you see it?
-
@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?
-
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 anSMA
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
-
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 underDrawDown_Old
. A chart comparing both (running the updated sample) -
@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. -
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 -
@backtrader I've looked on it, and I am going to use it as a baseline for dd length observer.