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: self.length += 1 else: self.length = 0 self.lines.length = self.length self.lines.maxlength = max(self.length, self.lines.maxlength[-1])
If possible could you please include it as a standard
tim3lord last edited by
Nice! Was looking at trying to make something like this myself. Thanks for sharing
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...
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
lineswith 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.
TimeReturnobserver, which relies on the
@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
DrawDownobserver is in need of a due update to be re-implemented like the
- Separate the logic in
- 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.
RandyT last edited by
@RandyT i've created chat with you. Can you see it?
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
Stochasticdefaults to a separate subplot and an
SMAdefaults 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.
BTFDblog post (sample is in the sources) doesn't plot the data or indicators: it simply plots an observer which constructs its values.
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.
DrawDownobserver has been updated to use it (the old version is still available under
DrawDown_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
DrawDownobserver, which is extremely simplified and simply observes and from there derive the observer for the length.
The commit in the
developmentbranch 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.