For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

3 Consecutive Highs / Low?

  • I was doing peak detection but I just realized BT has a wide array of indicators that I am not too 100% familiar with.

    Is there an indicator that determines 3 consecutive highs (each higher than the previous)..?

  • administrators

    There is no such thing, but bt.All(high > high(-1), high(-1) > high(-2), high(-2) > high(-3)) (or And if you prefer) should do the trick.

  • now that's elegant..
    thank you

  • administrators

    You may alternatively pack it in an indicator, which will allow you to decide how many highs to consider by means of a parameter

    This is untested, but the general idea should be something like it.

    class ConsecutiveIncreases(bt.Indicator):
        lines = ('consecutive',)
        params = (
            ('period', 3),  # how many to consider
            ('ago', 0),  # offset to start in the past
        def __init__(self):
            incs = [ < - 1) for i in range(self.p.ago, self.p.ago + self.p.period)]
            self.lines.consecutive = bt.All(*incs)

    Which you can later use with any of the defined fields of a data feed

    o5 = ConsecutiveIncreases(, period=5)  # 5 consecutive higher opens
    h5 = ConsecutiveIncreases(  # 3 consecutive higher highs

    Of course the indicator can be directly specialized to use rather than the generic

  • @backtrader

    Will test it out. Thanks once again

Log in to reply