Can't add my own method to my Analyzer subclass - Metaclass behaviour?
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
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?!
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
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
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
@backtrader ah ok cool. All noted, thank-you