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/

    Select highest value in a range only if the bar with the highest value passes a condition?

    General Code/Help
    5
    8
    233
    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.
    • aayushkucheria
      aayushkucheria last edited by

      Hey,
      I'm making an indicator which requires me to select the highest value in a certain range only if that value is greater than the price channel high at that bar.

      I'm able to find the highest value from this code -

      # self.counter defines how far back the range starts
      self.highest = basicops.Highest(self.data.high, period=self.counter, subplot=False)
      

      But once I have the highest value, how do I get the price channel high value at that bar to check my condition?

      Thank you!

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        I'm not sure I follow you. If you get the highest high value for the period, what value is going to be higher?

        aayushkucheria 1 Reply Last reply Reply Quote 1
        • aayushkucheria
          aayushkucheria last edited by

          I meant to say that if I have a range, and the highest high value in that range -->
          Then only take it if the highest high value > Another Indicator I've defined at that bar.

          So basically, in the below picture the line is the indicator. If there's no value above it, I don't want anything.
          But if there are high values above it, then I want the highest high.

          b25ef797-adb5-401a-8980-8875d4181f5b-image.png

          1 Reply Last reply Reply Quote 0
          • aayushkucheria
            aayushkucheria @run-out last edited by

            @run-out Added a comment with further clarification. Thanks!

            1 Reply Last reply Reply Quote 0
            • mics
              mics last edited by

              If I've interpreted your query correctly, perhaps this will help. I have a condition in a wider strategy that gets set by not breaking highs but crossing top Bollinger bands. I've changed it to suit your scenario.

              
              import backtrader as bt
              import backtrader.indicators as bt.ind
              
              class TheStrategy(bt.Strategy):
              
                  params = (
                      ('highperiod', 60),
                      ('lowperiod', 60),
                      ('bbperiod', 20),
                      ('devfactor', 2),
                      )
              
              def __init__(self):
              
                  self.highest = highest = bt.ind.Highest(self.data, period=self.p.highperiod, plot=True, subplot=False)
                  self.lowest = lowest = bt.ind.Lowest(self.data, period=self.p.lowperiod, plot=True, subplot=False)
              
                  self.boll = bt.indicators.BollingerBands(self.data, period=self.p.bbperiod, devfactor=self.p.devfactor, plot=True)
              
              def next(self):
                  
                      if self.data.high[0] > self.boll.lines.top[0]:
                          if self.data.high[0] > self.highest:
                              self.buy()  # ENTER LONG
              
              1 Reply Last reply Reply Quote 0
              • D
                dasch last edited by

                this will work too:

                import backtrader as bt
                
                class TheStrategy(bt.Strategy):
                
                
                   params = (
                       ('highperiod', 5),
                       ('lowperiod', 5),
                   )
                
                   def __init__(self):
                
                       self.highest = bt.ind.Highest(
                           self.data, period=self.p.highperiod, plot=True, subplot=False)
                       self.lowest = bt.ind.Lowest(
                           self.data, period=self.p.lowperiod, plot=True, subplot=False)
                       self.signal_high = self.data.high > self.highest
                       self.signal_low = self.data.low < self.lowest
                
                   def next(self):
                       print(self.signal_high[0])
                       print(self.signal_low[0])
                
                1 Reply Last reply Reply Quote 0
                • A
                  ab_trader last edited by

                  my 5 cents:

                  • Script from @dasch will generate signals when high or low prices will break the boundaries of
                    channel built on close price.

                  • This

                  I'm making an indicator which requires me to select the highest value in a certain range only if that value is greater than the price channel high at that bar.

                  highest value of what? of the price? of the another indicator?

                  talking about prices - if you built the price channel on lows and highs, you can only check if current high/low is higher/lower than previous value because current value will be within the channel by definition:

                  self.highest = bt.ind.Highest(self.data.high, period=self.p.highperiod)
                  self.up = self.data.high > self.highest(-1)
                  
                  • This is a different story

                  I meant to say that if I have a range, and the highest high value in that range -->
                  Then only take it if the highest high value > Another Indicator I've defined at that bar.
                  So basically, in the below picture the line is the indicator. If there's no value above it, I don't want anything.
                  But if there are high values above it, then I want the highest high.

                  self.indicator = bt.ind.whatever_indicator_is_wanted()
                  self.up = self.data.high > self.indicator
                  
                  1 Reply Last reply Reply Quote 0
                  • D
                    dasch last edited by

                    @ab_trader i see your point, thanks for correcting this and pointing this out.

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                    $(document).ready(function () { app.coldLoad(); }); }