For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

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):
   = ([0] /[-1]) - 1

    ... = ([0] /[-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?

  • administrators

    @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

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

  • administrators

    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

    • access[-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

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

Log in to reply

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.