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/

    Can't add my own method to my Analyzer subclass - Metaclass behaviour?

    General Code/Help
    2
    5
    1732
    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.
    • Richard O'Regan
      Richard O'Regan last edited by Richard O'Regan

      Hey guys,
      So I'm working on another Analyzer to calculate basic statistics. It works great, just when I move some of the code out of notify_trade() to a new method I defined called 'calculate_statistics'

      def calculate_statistics(self):
              # Stats for winning trades..
              self.rets.won.closedTrades = np.size(self._won_pnl_list)
              ...
      

      When I call

      self.calculate_statistics()
      

      I get an error AttributeError: 'BasicTradeStats' object has no attribute 'calculate_statistics'

      I've checked obvious things, tried defining other functions e.g.

      def hello(self):
          print('Hello')
      

      in other Analyzer code , like sqn.py e.t.c.. and I get same error trying to call.

      I don't know about meta classes and assuming there is some restriction deliberately in place? Or am I just missing a trick somewhere?!

      Thanks

      You can take the man out of the army, but you can't take the army out of the man.

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

        Adding methods to a subclass of analyzer is of course possible.

        The problem here is that understanding and diagnosing your problem is close to impossible. See:

        • Title: "... metaclass behavior" but there is nothing about metaclasses in the message
        • There is no indication as to where def calculate_statistics(self) is actually being declared and where it is being called
        • There is no hint as to how the subclass is actually being created
        1 Reply Last reply Reply Quote 0
        • Richard O'Regan
          Richard O'Regan last edited by Richard O'Regan

          Hey Backtrader, back to coding after partying all weekend.

          I didn't put my whole code up because it was in prototype mode and messy - though in hindsight I didn't give nearly enough information. Apologies for my lack of clarity.

          I loaded up everything just now, and it all works fine?? I think must have been something to do with reloading modules and Atom editor Beta I'm using..

          I subclassed Analyzer (which uses metaclasses), I honestly could not use any functions I defined, only 'notify_trade' etc, I figured there must be some restrictions with metaclasses (which I have never used before) but I couldnt find anything on Google so was confused..

          All sorted now - funny how restarting everything can often fix problem. Hopefully will have a new useful Analyzer up later today/tomorrow. Stay tuned
          Thank-you

          You can take the man out of the army, but you can't take the army out of the man.

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

            Although metaclasses can be used to enforce restrictions, it's not the usage pattern in backtrader. They are in place to make things lighter (in the humble opinion of the author) both for the internals and the externals

            Richard O'Regan 1 Reply Last reply Reply Quote 0
            • Richard O'Regan
              Richard O'Regan @backtrader last edited by

              @backtrader ah ok cool. All noted, thank-you

              You can take the man out of the army, but you can't take the army out of the man.

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