For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

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],
                self.zscore[i] = self.transform[i].zscore

    The full error stack is :

    Traceback (most recent call last):
      File "C:\Users\john\Anaconda3\lib\", line 1701, in main
      File "C:\Users\john\Anaconda3\lib\", line 1570, in _runscript
      File "C:\Users\john\Anaconda3\lib\", line 585, in run
        exec(cmd, globals, locals)
      File "<string>", line 1, in <module>
      File "c:\users\john\onedrive\documents\pair\models\", line 274, in <module>
      File "c:\users\john\onedrive\documents\pair\models\", line 232, in runstrategy
        for i,p in enumerate(pairs):
      File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\", line 1298, in runstrategies
      File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\", line 1630, in _runnext
      File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\", line 347, in _next
        super(Strategy, self)._next()
      File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\", line 263, in _next
      File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\", line 263, in _next
      File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\", line 282, in _next
        self.nextstart()  # only called for the 1st value
      File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\", line 347, in nextstart
      File "C:\Users\john\Anaconda3\lib\site-packages\backtrader\indicators\", line 58, in next
        slope, intercept = sm.OLS(p0, p1).fit().params
      File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\regression\", line 838, in __init__
        hasconst=hasconst, **kwargs)
      File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\regression\", line 684, in __init__
        weights=weights, hasconst=hasconst, **kwargs)
      File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\regression\", line 196, in __init__
        super(RegressionModel, self).__init__(endog, exog, **kwargs)
      File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\", line 216, in __init__
        super(LikelihoodModel, self).__init__(endog, exog, **kwargs)
      File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\", line 68, in __init__
      File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\", line 91, in _handle_data
        data = handle_data(endog, exog, missing, hasconst, **kwargs)
      File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\", line 635, in handle_data
      File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\", line 80, in __init__
      File "C:\Users\john\Anaconda3\lib\site-packages\statsmodels\base\", line 124, in _handle_constant
        ptp_ = np.ptp(self.exog, axis=0)
      File "C:\Users\john\Anaconda3\lib\site-packages\numpy\core\", line 2390, in ptp
        return _methods._ptp(a, axis=axis, out=out, **kwargs)
      File "C:\Users\john\Anaconda3\lib\site-packages\numpy\core\", 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    
        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)
            d1 = DataFactory(dataname=ps[1], historical=True, fromdate=startdat, todate=enddat, timeframe=bt.TimeFrame.Days)
            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',
                            help='Starting date in YYYY-MM-DD format')
        parser.add_argument('--todate', '-t',
                            help='Starting date in YYYY-MM-DD format')

    So my original bash command was this:

    python -d0 AAPL.csv -d1 TSM.csv

    I changed to this:

    python -d0 AAPL.csv -d1 TSM.csv -f 2017-01-01 -t 2018-01-01

Log in to reply