Backtrader Community

    • 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/

    line value behaviour

    Indicators/Strategies/Analyzers
    2
    6
    436
    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.
    • kam dorra
      kam dorra last edited by

      BV = value created with bt.If as a line in __init__
      every value in this line should be < float('Inf')

      buy = if c(0) > BV

      scenario 1:
      data = 5min ohlc
      BV occurs at 9:05 am
      c(0) > BV occurs at 9:10 am
      buy signal occurs

      scenario 2:
      data = 5min ohlc
      BV occurs at 9:05 am
      c(0) > BV occurs at 9:35 am
      (all prices between 9:05 and 9:35 < BV)
      buy signal does NOT occur

      If c(0) > BV occurs in the 5min bar that comes immediately after BV then buy signal happens.
      but if c(0) > BV occurs in a 5min bar not immediately after BV the buy signal does not happen.

      Shouldn't c(0) be compared to the last value created in the BV line, no matter how long ago it
      was created?
      Why does scenario 2 happen?
      How to enable a buy signal in scenario 2?

      Thanks.

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

        Sorry but the magical powers of recreating your code out of thin air and seeing your data in a crystal ball are far beyond what my humble human condition allows to me do.

        • No idea what BV is (even if you think you have completely specified it)
        • No idea what c(x) is
        • No idea what your data is.
        • No idea where c(x) and BV are being compared.

        @kam-dorra said in line value behaviour:

        If c(0) > BV occurs in the 5min bar that comes immediately after BV then buy signal happens.
        but if c(0) > BV occurs in a 5min bar not immediately after BV the buy signal does not happen.

        Even if you believe those sentences make sense, they don't.

        @kam-dorra said in line value behaviour:

        Shouldn't c(0) be compared to the last value created in the BV line, no matter how long ago it was created?

        Apparently BV is also a line created with bt.If. The last value is NOT created long ago. It has a value for each iteration.

        1 Reply Last reply Reply Quote 1
        • kam dorra
          kam dorra last edited by

          class tester(bt.Strategy):
          
              def __init__(self):
          
                  h = self.data.high
                  l= self.data.low
                  c = self.data.close
          
                  BBBV = bt.And(h(-3) > h(-2), h(-2) > h(-1)) # 3 highs down in row
                  BBV = bt.If(BBBV, h(-1), float('Inf')) # returns h(-1) or Inf
                  BV = bt.If(BBV < float('Inf'), BBV, float('Inf'))  # returns only non-Inf BBV? or does this do something else?
                  self.buy = c(0) > BV
          
          B 1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators @kam dorra last edited by backtrader

            @kam-dorra said in line value behaviour:

            self.buy = c(0) > BV
            

            Simpler writing does help ... try ...

            self.buy = self.data.close > BV
            

            @kam-dorra said in line value behaviour:

            BV = bt.If(BBV < float('Inf'), BBV, float('Inf'))  # returns only non-Inf BBV? or does this do something else?
            

            It's an if condition. It will return the current value of BBV (which can be float('Inf')) or float('Inf').

            Rather than asking yourself that question, a print(BV[0]) in each next iteration will show you the actual values.

            1 Reply Last reply Reply Quote 0
            • kam dorra
              kam dorra last edited by

              when I add print (BV[0]), i get:
              IndexError: array index out of range

              when I add print (BV), i get:
              <backtrader.functions.If object at 0x7f1565ab8310>

              • an object somewhere in memory?
              B 1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators @kam dorra last edited by

                @kam-dorra said in line value behaviour:

                when I add print (BV[0]), i get:
                IndexError: array index out of range

                And you expect us to know where you do that ... because we will see it using tea leaves?

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