For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Error Calculating EMA
-
Hello,
I have an issue when calculating the EMA for all my stocks, but not when only calculating some. Here is the code:
class MovingAverageCrossover(bt.Strategy): params = ( ('ma_short_length', 50), ('ma_long_length', 200), ) def __init__(self): for data in self.datas: data.ma_short = btind.ExponentialMovingAverage(data.close, period=self.params.ma_short_length) data.ma_long = btind.ExponentialMovingAverage(data.close, period=self.params.ma_long_length)
The error is occurring during cerebro.run, but it is related to the EMA... Here is the error:
Traceback (most recent call last): File "c:\Users\Z\Desktop\Python\Backtest\main.py", line 15, in <module> results = cerebro.run() ^^^^^^^^^^^^^ File "C:\Users\Z\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1132, in run runstrat = self.runstrategies(iterstrat) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Z\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1298, in runstrategies self._runonce(runstrats) File "C:\Users\Z\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\cerebro.py", line 1657, in _runonce strat._once() File "C:\Users\Z\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\lineiterator.py", line 297, in _once indicator._once() File "C:\Users\Z\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\lineiterator.py", line 297, in _once indicator._once() File "C:\Users\Z\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\lineiterator.py", line 317, in _once self.oncestart(self._minperiod - 1, self._minperiod) File "C:\Users\Z\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\indicators\basicops.py", line 401, in oncestart super(ExponentialSmoothing, self).once(start, end) File "C:\Users\Z\AppData\Local\Programs\Python\Python311\Lib\site-packages\backtrader\indicators\basicops.py", line 364, in once dst[i] = math.fsum(src[i - period + 1:i + 1]) / period ~~~^^^ IndexError: array assignment index out of range
We get through the init method, but never get the prenext or next methods. I am clueless. Thanks for any help!