Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    IndexError: array assignment index out of range

    General Code/Help
    2
    3
    9952
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      cortex last edited by backtrader

      Hello I 'm using "analyzer-annualreturn.py" that I have modified to use generic data .
      the problem is that I have a data of gold that doesn't have neither volume nor openinterest.

      *when I try to use the program I have the following error:

      C:\Users\bobwilson\Anaconda2\python.exe C:/Users/bobwilson/PycharmProjects/PYondaSMA/goldcross.py --plot
      Traceback (most recent call last):
        File "C:/Users/bobwilson/PycharmProjects/PYondaSMA/goldcross.py", line 273, in <module>
          runstrategy()
        File "C:/Users/bobwilson/PycharmProjects/PYondaSMA/goldcross.py", line 206, in runstrategy
          cerebro.run()
        File "C:\Users\bobwilson\Anaconda2\lib\site-packages\backtrader\cerebro.py", line 809, in run
          runstrat = self.runstrategies(iterstrat)
        File "C:\Users\bobwilson\Anaconda2\lib\site-packages\backtrader\cerebro.py", line 926, in runstrategies
          self._runonce(runstrats)
        File "C:\Users\bobwilson\Anaconda2\lib\site-packages\backtrader\cerebro.py", line 1245, in _runonce
          strat._once()
        File "C:\Users\bobwilson\Anaconda2\lib\site-packages\backtrader\lineiterator.py", line 274, in _once
          indicator._once()
        File "C:\Users\bobwilson\Anaconda2\lib\site-packages\backtrader\lineiterator.py", line 274, in _once
          indicator._once()
        File "C:\Users\bobwilson\Anaconda2\lib\site-packages\backtrader\lineiterator.py", line 294, in _once
          self.oncestart(self._minperiod - 1, self._minperiod)
        File "C:\Users\bobwilson\Anaconda2\lib\site-packages\backtrader\indicators\basicops.py", line 371, in oncestart
          super(ExponentialSmoothing, self).once(start, end)
        File "C:\Users\bobwilson\Anaconda2\lib\site-packages\backtrader\indicators\basicops.py", line 334, in once
          dst[i] = math.fsum(src[i - period + 1:i + 1]) / period
      IndexError: array assignment index out of range*
      
      # Create the 1st data
          data = btfeeds.GenericCSVData(
              dataname='C:/Users/bobwilson/PycharmProjects/PYondaSMA/eurusd24122016.csv',
      
              fromdate=datetime.datetime(2016, 1, 8),
              todate=datetime.datetime(2016, 1, 23),
      
              nullvalue=0.0,
      
              dtformat=('%Y-%m-%d %H:%M:%S'),
              # tmformat=('%H:%M:%S'),
              datetime=0,
              high=2,
              low=3,
              open=4,
              close=1,
              volume=5,
              openinterest=-1, )
          # Add the 1st data to cerebro
          cerebro.adddata(data)
      

      this is the sample of my data :

      Date,PX_OPEN,PX_HIGH,PX_LOW,PX_SETTLE
      10/09/2010,1243.75,1251.25,1236.8,1246.25
      13/09/2010,1244.25,1249.1,1241.32,1245.6
      14/09/2010,1245.65,1274.95,1244.82,1268.15
      15/09/2010,1268.2,1271.97,1263.2,1268.05
      16/09/2010,1268.1,1278.02,1264.93,1275.25
      17/09/2010,1275.1,1282.97,1272,1274.3
      20/09/2010,1277.15,1283.8,1274.35,1278.35
      21/09/2010,1278.25,1291,1270.9,1287.15
      22/09/2010,1287.25,1296.3,1286.45,1291.35
      23/09/2010,1291.45,1296.1,1287.05,1292.45
      24/09/2010,1292.35,1300.07,1291.4,1295.95
      27/09/2010,1297.8,1300.15,1294.25,1294.35
      28/09/2010,1294.35,1310.2,1283.15,1309.05
      29/09/2010,1309.2,1313.45,1305.73,1309.85
      30/09/2010,1309.95,1315.95,1295.85,1308.35
      01/10/2010,1308.53,1320.7,1306.15,1319.1
      04/10/2010,1319.95,1320.63,1312.58,1315.25
      05/10/2010,1315.35,1341.81,1312.55,1340.65
      06/10/2010,1340.75,1349.8,1339.03,1349.05
      07/10/2010,1349,1364.77,1325.88,1333.55
      08/10/2010,1333.6,1349.8,1325.2,1346.74
      11/10/2010,1346.6,1355.65,1340.35,1354.05
      12/10/2010,1353.95,1355.77,1340.8,1350.35
      13/10/2010,1350.45,1374.35,1349,1372.15
      14/10/2010,1372.05,1387.35,1370.6,1381.15
      15/10/2010,1381.1,1385.65,1362.38,1368.4
      18/10/2010,1370.07,1375.14,1353.18,1368.45
      19/10/2010,1368.65,1371.2,1328.3,1332.05
      20/10/2010,1331.95,1347.35,1330.38,1346.25
      21/10/2010,1346.15,1349.25,1318,1325.68
      22/10/2010,1325.75,1328.65,1315.45,1328.45
      25/10/2010,1329.3,1349.3,1329.15,1339.85
      26/10/2010,1339.95,1343.75,1328.25,1340.45
      27/10/2010,1340.55,1343.8,1319.2,1325.35
      28/10/2010,1325.35,1346.1,1322.93,1344.05
      29/10/2010,1344.15,1359.7,1335.35,1359.4
      01/11/2010,1361.22,1365.85,1349.05,1353.85
      02/11/2010,1353.47,1359.75,1350.52,1357.45
      03/11/2010,1357.35,1364.6,1325.82,1348.55
      04/11/2010,1348.65,1393.55,1346.95,1392.5
      05/11/2010,1392.55,1398.27,1374.05,1393.65
      08/11/2010,1394.35,1410.6,1386.82,1409.55
      09/11/2010,1409.65,1424.6,1382.8,1392.9
      

      Does anyone knows why I have this problem ?

      1 Reply Last reply Reply Quote 0
      • C
        cortex last edited by

        My period of observation was to small. it does work when I put more days .

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          Although not shown in your code snippet, the error trace indicates that a moving average (directly or over another indicator which makes use of it) is in play.

          Apparently the minimum data, as you already realized, was not enough for the batch calculation.

          In this case running with runonce=False would have also not generated the error (each calculation would have been on a step by step basis)

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors