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

Custom Chop Index/Fractal Energy Indicator returns Nan

  • So I made a custom indicator which is the Chop Index/Fractal energy indicator however it only returns nan.

    class gamma1(bt.Indicator):
        lines = ('dummyline',)
        params = (('value', 5),)
        def __init__(self):
            self.m  = bt.If( >,,
            self.n = bt.If( <,,
            self.a = self.m - self.n
            self.b = btind.SumN(self.a, period=8)
            self.hl = btind.Highest(, period=8)-btind.Lowest(, period=8)
        def next(self):
            z = self.b[0]/self.hl[0]
            y = math.log(z,10)
            r = math.log(8, 10)
            k = bt.DivByZero(y,r)
            self.lines.dummyline = k
    class SMAStrategy(bt.Strategy):
        params = (
            ('period', 10), ('period2', 10),
        def __init__(self):
            self.gamma = gamma1(
        def next(self): 

    When I divide y and r by hand there is no issue. Therefore the issue must be with bt.divbyzero. I can't figure out why it return nan. Can anyone provide some guidance?

    Much appreciated.

  • @Paul-Park said in Custom Chop Index/Fractal Energy Indicator returns Nan:

    y = math.log(z,10)
    Need square brackets on z.

    y = math.log(z[0],10)

    I answered your other question on the dummyline but the way you've done it here is fine too.

  • @Paul-Park said in Custom Chop Index/Fractal Energy Indicator returns Nan:

    k = bt.DivByZero(y,r)

    bt.DivByZero should be used with the bt lines, but not with numbers. In your case it should be simply k=y/r. Also your r is a constant value of 0.903 so no need to check it against zero.

  • @run-out I removed [0] because I get a non subscriptable error.

  • @ab_trader I originally tried with normal division, y/r, but when I try to set self.lines equal to it then I get a float object is not callable error.

  • @Paul-Park if under "originally tried" you means this post, than you did it incorrectly at that time. Try this

    self.lines.dummyline[0] = y/r

    You need to get that you can operate with lines in the __init()__, and you operate with the numbers and line elements in the next(). Maybe try to make couple simple indicators before the deep dive. Or maybe just read the manual.

  • @ab_trader Ahhh. Strange, past custom indicators I made did not have issues with self.lines.dummyline[0] not having the brackets, so I assumed that the self.lines.dummyline was an exception.

  • @Paul-Park if you just call self.lines.dummyline or self.lines.dummyline[0] in the next(), than it can treat it as last element of the line. But you can't initiate it without [0]. I would recommend always use [n] in the next() to avoid unnecessary issues.

  • @ab_trader Thanks for the tip! So why does the strategy throw the error "lists object has no attribute dummyline" when I call self.gamma.lines.dummyline[0] in the next() portion of the strategy? I defined dummyline it in my indicator and specifically initialized it in my strategy's init.

  • @ab_trader Ok so everything is working now. So the reason for the errors was that in the init for my strategy I initialized the self.gamma variable with gamma1( That was my mistake I should have deleted that .dummyline.

    I think I go confused for a bit and accidentally put that there in the init of my strategy. I also used self.line.dummyline[0] in the next() of my strat. which should be used in my indicator class not strat class.

    Bunch of dumb mistakes I made. HUGE thanks to @ab_trader and @run-out for helping me out, and clarifying many aspects of BT for me.

Log in to reply