Real World UsageΒΆ

Finally it seems it pays having gotten down to developing backtrader.

Following what seemed like the end of the world when looking at the European markets in the last weeks, a friend asked if I could have a look at the data in our charting package to see how the falling range compared against previous similar occurrences.

Of course I could, but I said I could do more than looking into the charts, because I could quickly:

  • Create a quick LegDown indicator to measure the range of the fall. It could also have gotten the name of HighLowRange or HiLoRange. Luckily and in case this would be deemed needed, it can be solved via alias
  • Create a LegDownAnalyzer that would gather the results and sort them

This led to an addtional request:

  • Recovery after the falls in the next 5, 10, 15, 20 days (trading ...)

    Solved with a LegUp indicator which writes the values back for alignment with the corresponding``LegDown``

The work was quickly done (within the allowance of my free time) and results shared with the requester. But ... being the only problem tha I saw potential for:

  • Improvements in the automation
    • Multiple strategies/observers/analyzers with separated kwargs
    • Injection of indicators directly into Strategies with kwargs for each indicator
    • single plot argument accepting also kwargs
  • Improvements in in the Analyzer API to have automated printing capabilities for the results (which are returned as a dict -like instance) and have direct data access aliases

Notwithstanding that:

  • An obscure bug showed up due to the implementation combination I wrote to align the LegDown and LegUp values by mixing declaration of and the additional use of next

    The bug had been introduced to simplify the passing of a single data with multiple Lines, so that Indicators can operate on each of the lines as individual datas

The latter pushing me into:

  • Adding a background object opposite to LineDelay to “look” into the “future”

    That actually means that actual values are written into past array positions

Once all of the above were in place, it was time to retest how nicely the (small?) challenges posed by the above request could be solved more easily and faster (in implementation time).

Finally the execution and outcomes for the Eurostoxx 50 Future from 1998 up until today: \
    --csvformat vchartcsv \
    --data ../datas/sample/1998-2015-estx50-vchart.txt \
    --analyzer legdownup \
    --pranalyzer \
    --nostdstats \

== Analyzers

The August 2015 leg down shows up at place 13th. Obviously a non-common ocurrence although greater have happened.

What to do out of the follow up legs that point upwards is a lot more for staticians and bright math minds than for me.

Details about the implementation (see the entire module code at the end) of the LegUpDownAnalyzer:

  • It creates indicators in __init__ just as other objects do: Strategies, Indicators being usually the usual suspects

    These indicators get automatically registered to the strategy to which the analyzer is attached

  • Just like strategies the Analyzer has self.datas (an array of datas) and aliases to it:, self.data0, self.data1 ...

  • Again like strategies: nexstart and stop hooks (those are not present in indicators)

    In this case used to:

    • nextstart: record the initial starting point of the strategy
    • stop: making the final calculations because things are done
  • Note: other methods like start, prenext and next are not needed in this case

  • The LegDownUpAnalyzer method print has been overriden to no longer call the pprint method but to create a CSV printout of the calculations

After much talk and since we added --plot to the mix ... the chart.

Finally the legupdown module which is being loaded by bt-run.

from __future__ import (absolute_import, division, print_function,

import itertools
import operator

import six
from six.moves import map, xrange, zip

import backtrader as bt
import backtrader.indicators as btind
from backtrader.utils import OrderedDict

class LegDown(bt.Indicator):
    Calculates what the current legdown has been using:
      - Current low
      - High from ``period`` bars ago
    lines = ('legdown',)
    params = (('period', 10),)

    def __init__(self):
        self.lines.legdown = -

class LegUp(bt.Indicator):
    Calculates what the current legup has been using:
      - Current high
      - Low from ``period`` bars ago

    If param ``writeback`` is True the value will be written
    backwards ``period`` bars ago
    lines = ('legup',)
    params = (('period', 10), ('writeback', True),)

    def __init__(self): = -
        self.lines.legup = * self.p.writeback)

class LegDownUpAnalyzer(bt.Analyzer):
    params = (
        # If created indicators have to be plotteda along the data
        ('plotind', True),
        # period to consider for a legdown
        ('ldown', 10),
        # periods for the following legups after a legdown
        ('lups', [5, 10, 15, 20]),
        # How to sort: date-asc, date-desc, legdown-asc, legdown-desc
        ('sort', 'legdown-desc'),

    sort_options = ['date-asc', 'date-des', 'legdown-desc', 'legdown-asc']

    def __init__(self):
        # Create the legdown indicator
        self.ldown = LegDown(, period=self.p.ldown)
        self.ldown.plotinfo.plot = self.p.plotind

        # Create the legup indicators indicator - writeback is not touched
        # so the values will be written back the selected period and therefore
        # be aligned with the end of the legdown
        self.lups = list()
        for lup in self.p.lups:
            legup = LegUp(, period=lup)
            legup.plotinfo.plot = self.p.plotind

    def nextstart(self):
        self.start = len( - 1

    def stop(self):
        # Calculate start and ending points with values
        start = self.start
        end = len(
        size = end - start

        # Prepare dates (key in the returned dictionary)
        dtnumslice =, size)
        dtslice = map(lambda x: bt.num2date(x).date(), dtnumslice)
        keys = dtslice

        # Prepare the values, a list for each key item
        # leg down
        ldown = self.ldown.legdown.getzero(start, size)
        # as many legs up as requested
        lups = [up.legup.getzero(start, size) for up in self.lups]

        # put legs down/up together and interleave (zip)
        vals = [ldown] + lups
        zvals = zip(*vals)

        # Prepare sorting options
        if self.p.sort == 'date-asc':
            reverse, item = False, 0
        elif self.p.sort == 'date-desc':
            reverse, item = True, 0
        elif self.p.sort == 'legdown-asc':
            reverse, item = False, 1
        elif self.p.sort == 'legdown-desc':
            reverse, item = True, 1
            # Default ordering - date-asc
            reverse, item = False, 0

        # Prepare a sorted array of 2-tuples
        keyvals_sorted = sorted(zip(keys, zvals),

        # Use it to build an ordereddict
        self.ret = OrderedDict(keyvals_sorted)

    def get_analysis(self):
        return self.ret

    def print(self, *args, **kwargs):
        # Overriden to change default behavior (call pprint)
        # provides a CSV printout of the legs down/up
        header_items = ['Date', 'LegDown']
        header_items.extend(['LegUp_%d' % x for x in self.p.lups])
        header_txt = ','.join(header_items)

        for key, vals in six.iteritems(self.ret):
            keytxt = key.strftime('%Y-%m-%d')
            txt = ','.join(itertools.chain([keytxt], map(str, vals)))


Fork me on GitHub