ValueError: zero-size array to reduction operation maximum which has no identity
-
I am a bit of a newb to backtrader and I have bit off a bit more than I can easily chew. I have constructed a pair trading script that uses multiple data feeds for various pairs. For some pairs, all seems to be well. for others, I get ValueError: zero-size array to reduction operation maximum which has no identity.
My init function is:
def __init__(self): # To control operation entries self.upper_limit = self.p.upper self.lower_limit = self.p.lower self.up_medium = self.p.up_medium self.low_medium = self.p.low_medium self.qty1 = dict() self.qty2 = dict() self.portfolio_value = dict() self.status = dict() self.transform = dict() self.zscore = dict() for i, d in enumerate(self.datas[::2]): # d1 = self.datas[2 * i + 1] self.orderid = None self.qty1[i] = self.p.qty1 self.qty2[i] = self.p.qty2 self.status[i] = self.p.status self.portfolio_value[i] = self.p.portfolio_value # Signals performed with PD.OLS : self.transform[i] = btind.OLS_TransformationN(self.datas[2 * i], self.datas[2 * i + 1], period=self.p.period) self.zscore[i] = self.transform[i].zscore
The full error stack is :
Traceback (most recent call last): File "C:\Users\john\Anaconda3\lib\pdb.py", line 1701, in main pdb._runscript(mainpyfile) File "C:\Users\john\Anaconda3\lib\pdb.py", line 1570, in _runscript self.run(statement) File "C:\Users\john\Anaconda3\lib\bdb.py", line 585, in run exec(cmd, globals, locals) File "<string>", line 1, in <module> File "c:\users\john\onedrive\documents\pair\models\pair_bt_test2.py", line 274, in <module> runstrategy() File "c:\users\john\onedrive\documents\pair\models\pair_bt_test2.py", line 232, in runstrategy for i,p in enumerate(pairs): File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1298, in runstrategies self._runnext(runstrats) File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1630, in _runnext strat._next() File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\strategy.py", line 347, in _next super(Strategy, self)._next() File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\lineiterator.py", line 263, in _next indicator._next() File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\lineiterator.py", line 263, in _next indicator._next() File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\lineiterator.py", line 282, in _next self.nextstart() # only called for the 1st value File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\lineiterator.py", line 347, in nextstart self.next() File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\indicators\ols.py", line 58, in next slope, intercept = sm.OLS(p0, p1).fit().params File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\regression\linear_model.py", line 838, in __init__ hasconst=hasconst, **kwargs) File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\regression\linear_model.py", line 684, in __init__ weights=weights, hasconst=hasconst, **kwargs) File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\regression\linear_model.py", line 196, in __init__ super(RegressionModel, self).__init__(endog, exog, **kwargs) File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\model.py", line 216, in __init__ super(LikelihoodModel, self).__init__(endog, exog, **kwargs) File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\model.py", line 68, in __init__ **kwargs) File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\model.py", line 91, in _handle_data data = handle_data(endog, exog, missing, hasconst, **kwargs) File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\data.py", line 635, in handle_data **kwargs) File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\data.py", line 80, in __init__ self._handle_constant(hasconst) File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\data.py", line 124, in _handle_constant ptp_ = np.ptp(self.exog, axis=0) File "C:\Users\john\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 2390, in ptp return _methods._ptp(a, axis=axis, out=out, **kwargs) File "C:\Users\john\Anaconda3\lib\site-packages\numpy\core\_methods.py", line 153, in _ptp umr_maximum(a, axis, None, out, keepdims), ValueError: zero-size array to reduction operation maximum which has no identity
-
@John-Hegstrom
and for data:DataFactory = alpaca_backtrader_api.AlpacaData#store.getdata# Develop feed cerebro.addstrategy(PairTradingStrategy, period=400, ) for i,p in enumerate(pairs): # if i != 22: continue ps = p.split('/',2) d0 = DataFactory(dataname=ps[0], historical=True, fromdate=startdat, todate=enddat, timeframe=bt.TimeFrame.Days) cerebro.adddata(d0) d1 = DataFactory(dataname=ps[1], historical=True, fromdate=startdat, todate=enddat, timeframe=bt.TimeFrame.Days) cerebro.adddata(d1) import pdb; pdb.set_trace()
-
I think I need to find a different data source
-
Upon further investigation, the data is shorter for certain symbols than others. Does anyone know of a way to test the length of the data prior to a strategy being run?
-
Did you ever resolve this issue? I'm getting the same error.
-
@benmercerdev @John-Hegstrom
Ok, I discovered the issue for me. The data I was using was from 2016-2020.The default parameters have dates that were out of range for my data
parser.add_argument('--fromdate', '-f', default='1997-01-01', help='Starting date in YYYY-MM-DD format') parser.add_argument('--todate', '-t', default='1998-06-01', help='Starting date in YYYY-MM-DD format')
So my original bash command was this:
python pairs_trade.py -d0 AAPL.csv -d1 TSM.csv
I changed to this:
python pairs_trade.py -d0 AAPL.csv -d1 TSM.csv -f 2017-01-01 -t 2018-01-01