TypeError: 'float' object is not callable in custom gap indicator
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 / self.data.close[-1]) - 1
self.l.gap = (self.data.open / 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?
value = value(0) TypeError: 'float' object is not callable
It is unclear what
valueis and where it comes from ... but it is clearly a
floatand therefore not
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.
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
In any case the indicator would break because you
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
Either (or some together9
- Add the minimum period manually (see examples with
- Create the operation in a declarative manner during
- Assign to
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
PercentChangewhich is in
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?