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/

    Loop Multiple Conditions in Custom Indicator

    Indicators/Strategies/Analyzers
    2
    3
    270
    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.
    • mics
      mics last edited by

      QUESTION

      How do I add multiple conditions in the for loop?

      PROBLEM

      I want to check if both open and close are above bollinger-mid. I know the below code is incorrect but hopefully it shows what I am trying to achieve.

      INDICATOR

      class OpenAndCloseAboveBBmid(bt.Indicator):
      
          lines = ('consecutive',)
          params = (
              ('period', 15),  # how many to consider
              ('ago', 0),  # offset to start in the past
              ('bbperiod', 20),  # offset to start in the past
              ('devfactor', 2),  # offset to start in the past
          )
      
          def __init__(self):
              
              self.bollband = bt.indicators.BollingerBands(self.data, period=self.p.bbperiod, devfactor=self.p.devfactor, plot=False)
              
              ### I KNOW THIS NEXT LINE WON'T WORK BUT IT SHOWS WHAT I AM TRYING TO ACHIEVE
              incs = [self.data.open(-i) > self.bollband.lines.mid(-i) and self.data.close(-i) > self.bollband.lines.mid(-i) for i in range(self.p.ago, self.p.ago + self.p.period)]
              
              self.lines.consecutive = bt.All(*incs)
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        bt.If, bt.And, bt.Or

        Docs - Platform Concepts

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        mics 1 Reply Last reply Reply Quote 3
        • mics
          mics @ab_trader last edited by

          @ab_trader Thanks for the info.

          Working indicator with two conditions (open and close > bb.mid).

          import backtrader as bt
          import backtrader.indicators as btind
          
          
          class AboveBBmid(bt.Indicator):
          
              lines = ('open', 'close')
              params = (
                  ('period', 15),  # how many to consider
                  ('ago', 0),  # offset to start in the past
                  ('bbperiod', 20),  # offset to start in the past
                  ('devfactor', 2),  # offset to start in the past
              )
          
              def __init__(self):
                  
                  self.bollband = bt.indicators.BollingerBands(self.data, period=self.p.bbperiod, devfactor=self.p.devfactor, plot=False)
                  
                  bar_open = [self.data.open(-i) > self.bollband.lines.mid(-i) for i in range(self.p.ago, self.p.ago + self.p.period)]
                  
                  self.lines.open = bt.All(*bar_open)
          
                  bar_close = [self.data.close(-i) > self.bollband.lines.mid(-i) for i in range(self.p.ago, self.p.ago + self.p.period)]
                  
                  self.lines.close = bt.All(*bar_close)
                  
                  self.combined = bt.And(self.lines.open, self.lines.close)
          
          1 Reply Last reply Reply Quote 2
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors