Hm, I pretty much read trough all the example but I couldn't find one where not a line is defined recursive, but a value from which an indicator gets calculated. I'm unsure how to initialize those. This is also a version I had but I sadly always got the same error and I couldn't fix it:
def __init__(self):
self.m = bt.ind.MACDHisto(self.data.close, period_me1=self.p.fastLength, period_me2=self.p.slowLength)
self.v1 = bt.ind.Lowest(self.m, period=self.p.length)
self.v2 = bt.ind.Highest(self.m, period=self.p.length) - self.v1
def next(self):
self.f1[0] = (self.m[0] - self.v1[0]) / self.v2[0] * 100 if self.v2[0] > 0 else self.f1[-1]
self.pf[0] = self.pf[-1] + (self.p.factor * (self.f1[0] - self.pf[-1]))
self.v3 = bt.ind.Lowest(self.pf, period=self.p.length)
self.v4 = bt.ind.Highest(self.pf, period=self.p.length) - self.v3
self.f2[0] = ((self.pf[0] - self.v3[0]) / self.v4[0]) * 100 if self.v4[0] > 0 else self.f2[-1]
self.l.schaff_cycle[0] = self.l.schaff_cycle[-1] + (
self.p.factor * (self.f2[0] - self.l.schaff_cycle[-1]))
Traceback (most recent call last):
File "/home/vincenzot/Documents/fx_xd/fx_xd/fx_xd_bot.py", line 209, in <module>
opt_runs = cerebro.run(runonce=False)
File "/home/vincenzot/anaconda3/envs/fx_xd/lib/python3.7/site-packages/backtrader/cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "/home/vincenzot/anaconda3/envs/fx_xd/lib/python3.7/site-packages/backtrader/cerebro.py", line 1298, in runstrategies
self._runnext(runstrats)
File "/home/vincenzot/anaconda3/envs/fx_xd/lib/python3.7/site-packages/backtrader/cerebro.py", line 1630, in _runnext
strat._next()
File "/home/vincenzot/anaconda3/envs/fx_xd/lib/python3.7/site-packages/backtrader/strategy.py", line 347, in _next
super(Strategy, self)._next()
File "/home/vincenzot/anaconda3/envs/fx_xd/lib/python3.7/site-packages/backtrader/lineiterator.py", line 263, in _next
indicator._next()
File "/home/vincenzot/anaconda3/envs/fx_xd/lib/python3.7/site-packages/backtrader/lineiterator.py", line 282, in _next
self.nextstart() # only called for the 1st value
File "/home/vincenzot/anaconda3/envs/fx_xd/lib/python3.7/site-packages/backtrader/lineiterator.py", line 347, in nextstart
self.next()
File "/home/vincenzot/Documents/fx_xd/fx_xd/custom_indicators.py", line 37, in next
self.f1[0] = (self.m[0] - self.v1[0]) / self.v2[0] * 100 if self.v2[0] > 0 else self.f1[-1]
File "/home/vincenzot/anaconda3/envs/fx_xd/lib/python3.7/site-packages/backtrader/lineseries.py", line 461, in __getattr__
return getattr(self.lines, name)
AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute 'f1'
Process finished with exit code 1
The only thing missing is how to initialize this, where to give f1 the first value. I can't just define it in nextstart with a float because it has to be an array.
self.f1[0] = (self.m[0] - self.v1[0]) / self.v2[0] * 100 if self.v2[0] > 0 else self.f1[-1]
Thanks so much for your help btw. I think I still don't 100% how next, nextstart, and init work together.