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.data.high > self.data.close(-1), self.data.high, self.data.close(-1)) self.n = bt.If(self.data.low < self.data.close(-1), self.data.low, self.data.close(-1)) self.a = self.m - self.n self.b = btind.SumN(self.a, period=8) self.hl = btind.Highest(self.data.high, period=8)-btind.Lowest(self.data.low, period=8) def next(self): z = self.b/self.hl y = math.log(z,10) r = math.log(8, 10) print(y) print(r) k = bt.DivByZero(y,r) self.lines.dummyline = k class SMAStrategy(bt.Strategy): params = ( ('period', 10), ('period2', 10), ) def __init__(self): self.gamma = gamma1(self.data) def next(self): print(self.gamma.lines.dummyline)
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?
run-out last edited by
y = math.log(z,10)
Need square brackets on z.
y = math.log(z,10)
I answered your other question on the dummyline but the way you've done it here is fine too.
k = bt.DivByZero(y,r)
bt.DivByZeroshould be used with the
btlines, but not with numbers. In your case it should be simply
k=y/r. Also your
ris a constant value of
0.903so no need to check it against zero.
@run-out I removed  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.
self.lines.dummyline = 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 not having the brackets, so I assumed that the self.lines.dummyline was an exception.
@Paul-Park if you just call
next(), than it can treat it as last element of the line. But you can't initiate it without
. I would recommend always use
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 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(self.data).dummyline. 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 in the next() of my strat. which should be used in my indicator class not strat class.