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/

    bt.If Help

    General Code/Help
    3
    5
    48
    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.
    • Akhil Juluru
      Akhil Juluru last edited by

      I want to know how can I get this condition in the next block.

      self.datas[0].datetime.date(-3) < self.datas[0].datetime.date(-2) and self.datas[0].close(-2) > self.datas[0].open(-2) and self.datas[0].close(-1) > self.datas[0].open(-1) and self.datas[0].close(0) > self.datas[0].open(0)
      

      I tried this, but it returns a bt.If object instead of the values.

      result = bt.If(bt.And(bt.And(bt.And(bt.And(self.datas[0].datetime.date(-3) < self.datas[0].datetime.date(-2)), self.datas[0].close(-2) > self.datas[0].open(-2)), self.datas[0].close(-1) > self.datas[0].open(-1)), self.datas[0].close(0) > self.datas[0].open(0)), 1, 0)
      
      print(result)
      

      Any help regarding how to properly implement it?

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

        I think you are mixing [] and (). try [] (slicing) brackets instead of () (function) brackets.

        self.datas[0].close[-2] > self.datas[0].open[-2] and self.datas[0].close[-1] > self.datas[0].open[-1] and self.datas[0].close[0] > self.datas[0].open[0]

        for the first part if you are comparing dates, you should slice first then check dates.

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

          @Akhil-Juluru said in bt.If Help:

          self.datas[0].close(-2) > self.datas[0].open(-2) and self.datas[0].close(-1) > self.datas[0].open(-1) and self.datas[0].close(0) > self.datas[0].open(0)

          I'm not sure what your goal is with the date component, but you could tackle the last n periods with close over open as follows:

          def __init__(self):
                self.close_over_open = self.datas[0].close > self.datas[0].open
                self.close_over_open_over_nper = bt.ind.SumN(self.close_over_open, period=3) == 3
          
          Akhil Juluru 1 Reply Last reply Reply Quote 0
          • Akhil Juluru
            Akhil Juluru @jay last edited by

            @jay Thank you that was the problem! fixed it.

            1 Reply Last reply Reply Quote 1
            • Akhil Juluru
              Akhil Juluru @run-out last edited by

              @run-out I was trying to see if the first three candles in a trading day have been green. Your method is good just implemented it! Thank you.

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