Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    3 Consecutive Highs / Low?

    Indicators/Strategies/Analyzers
    2
    5
    1780
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      Taewoo Kim last edited by

      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)..?

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        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.

        T 1 Reply Last reply Reply Quote 0
        • T
          Taewoo Kim @backtrader last edited by

          now that's elegant..
          thank you

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by backtrader

            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

            T 1 Reply Last reply Reply Quote 0
            • T
              Taewoo Kim @backtrader last edited by

              @backtrader

              Will test it out. Thanks once again

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors