For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Backtrader giving IndexError: array assignment index out of range
-
I am using the following strategy:
def max_n(array, n): return np.argpartition(array, -n)[-n:] class CrossSectionalMR(bt.Strategy): params = ( ('num_positions', 100), ) def __init__(self, temp): self.inds = {} for d in self.datas: self.inds[d] = {} self.inds[d]["pct"] = bt.indicators.PercentChange(d.close, period=1) def prenext(self): self.next() def next(self): available = list(filter(lambda d: len(d), self.datas)) # only look at data that existed yesterday rets = np.zeros(len(available)) for i, d in enumerate(available): rets[i] = self.inds[d]['pct'][0] market_ret = np.mean(rets) weights = -(rets - market_ret) max_weights_index = max_n(np.abs(weights), self.params.num_positions) max_weights = weights[max_weights_index] weights = weights / np.sum(np.abs(max_weights)) for i, d in enumerate(available): if i in max_weights_index: self.order_target_percent(d, target=weights[i]) else: self.order_target_percent(d, 0)
and I get the following error:
Traceback (most recent call last): File "/home/poblivsig/Software/pycharm-2020.3.1/plugins/python/helpers/pydev/pydevd.py", line 1477, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/home/poblivsig/Software/pycharm-2020.3.1/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/poblivsig/Dropbox/meanrev/main.py", line 190, in <module> dd, cagr, sharpe = backtest(datas, CrossSectionalMR, plot=True, num_positions=100) File "/home/poblivsig/Dropbox/meanrev/main.py", line 181, in backtest results = cerebro.run() File "/home/poblivsig/Dropbox/meanrev/venv/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/home/poblivsig/Dropbox/meanrev/venv/lib/python3.8/site-packages/backtrader/cerebro.py", line 1293, in runstrategies self._runonce(runstrats) File "/home/poblivsig/Dropbox/meanrev/venv/lib/python3.8/site-packages/backtrader/cerebro.py", line 1652, in _runonce strat._once() File "/home/poblivsig/Dropbox/meanrev/venv/lib/python3.8/site-packages/backtrader/lineiterator.py", line 297, in _once indicator._once() File "/home/poblivsig/Dropbox/meanrev/venv/lib/python3.8/site-packages/backtrader/lineiterator.py", line 297, in _once indicator._once() File "/home/poblivsig/Dropbox/meanrev/venv/lib/python3.8/site-packages/backtrader/linebuffer.py", line 630, in _once self.oncestart(self._minperiod - 1, self._minperiod) File "/home/poblivsig/Dropbox/meanrev/venv/lib/python3.8/site-packages/backtrader/lineroot.py", line 165, in oncestart self.once(start, end) File "/home/poblivsig/Dropbox/meanrev/venv/lib/python3.8/site-packages/backtrader/linebuffer.py", line 672, in once dst[i] = src[i + ago] IndexError: array assignment index out of range python-BaseExceptio
Any ideas what I am doing wrong?
-
I could probably be wrong of cause but your code here:
rets[i] = self.inds[d]['pct'][0]
tries to access the 'pct' indicator when it could probably has no values yet. This may happen because despite the correctly defined period, you are still forcing the
next
call to your strategy from theprenext
.Normally the
next
will only be called if all the data lines have values (including indicators).Please correct me if I'm wrong.
-
vladisld,
Thanks very much. I will have a look at that later and let you know.
Cheers,
Paul