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/

    TypeError: 'float' object is not callable in custom gap indicator

    General Code/Help
    indicator
    2
    5
    2325
    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.
    • F
      favetelinguis last edited by favetelinguis

      Im trying to make a gap indicator as my first test with backtrader but must be missing something.

      class Gap(bt.Indicator):
          lines = ('gap',)
      
          def next(self):
              self.l.gap = (self.data.open[0] / self.data.close[-1]) - 1
      

      Generates:
      ...

      self.l.gap = (self.data.open[0] / self.data.close[-1]) - 1
      value = value(0)
      TypeError: 'float' object is not callable
      

      Could someone explain why im getting this error and what I can do to fix it?

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

        @favetelinguis said in TypeError: 'float' object is not callable in custom gap indicator:

        value = value(0)
        TypeError: 'float' object is not callable
        

        It is unclear what value is and where it comes from ... but it is clearly a float and therefore not callable

        1 Reply Last reply Reply Quote -1
        • F
          favetelinguis last edited by

          I dont know what value is the three last lines I have cut from the stacktrace i get when executing my indicator. Not sure where/why backtrader is calling value.

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

            The problem here:

            • Three (3) isolated lines from a stack trace cannot for sure help anyone.

            If you have an indicator and its application in a script generates an error, the minimum would be:

            • How you are using it
            • The (mostly) complete generated error and not 3 lines which by themselves make no sense

            Note
            In any case the indicator would break because you

            • access self.data.close[-1] without having set a minimum period to let the indicator know it has to at least access the past day
            • Assign a float to a lines object in next

            Either (or some together9

            • Add the minimum period manually (see examples with addminperiod)
            • Create the operation in a declarative manner during __init__
            • Assign to [0] during next

            You probably want to read this - Docs - Platform Concepts - about operator and delayed indexing.

            You may also have a look at the code of indicators like PercentChange which is in percentagechange.py

            1 Reply Last reply Reply Quote 1
            • F
              favetelinguis last edited by favetelinguis

              Thanks your reply did solve my problem both using the __init_method and using the next metod. Thanks for pointing out the link.

              However the next issue I ran into is the limitiation of buying at open and sell att close. As I understand there is no way to check if there is a gap and if there is buy at the same opening price that was used to calculate if there is a gap and then sell at close? Buy default this strategy would check for a gap at day 0 and then execute the order at day 1?
              Is this correct and is it possible sidestep this and implement a strategy that buy at the current open in backtrader?

              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(); }); }