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 = [self.data(-i) < self.data(-i - 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(self.data.open, period=5)  # 5 consecutive higher opens
    h5 = ConsecutiveIncreases(self.data.high)  # 3 consecutive higher highs
    

    Of course the indicator can be directly specialized to use self.data.high rather than the generic self.data



  • @backtrader

    Will test it out. Thanks once again


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.