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 index out of range (Renko) with 1min Dataframe



  • Here the source code:

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    import argparse
    import datetime
    
    import backtrader as bt
    import backtrader.feeds as btfeed
    
    
    class St(bt.Strategy):
        params = dict(
        )
    
        def __init__(self):
            for d in self.datas:
                bt.ind.RSI(d)
    
        def next(self):
            pass
    
    
    def runstrat(args=None):
        args = parse_args(args)
    
        cerebro = bt.Cerebro()
    
        # Data feed kwargs
        kwargs = dict()
    
        # Parse from/to-date
        dtfmt, tmfmt = '%Y.%m.%d', 'T%H:%M:%S'
    
        for a, d in ((getattr(args, x), x) for x in ['fromdate', 'todate']):
            if a:
                strpfmt = dtfmt + tmfmt * ('T' in a)
                kwargs[d] = datetime.datetime.strptime(a, strpfmt)
    
        data0 = bt.feeds.GenericCSVData(dataname="new.csv",
        datetime=1,
        open=2,
        high=3,
        low=4,
        close=5,
        volume=-1,
        openinterest=-1,
        **kwargs
        )
    
        fkwargs = dict()
        fkwargs.update(**eval('dict(' + args.renko + ')'))
    
        if not args.dual:
            data0.addfilter(bt.filters.Renko, **fkwargs)
            cerebro.adddata(data0)
        else:
            cerebro.adddata(data0)
            data1 = data0.clone()
            data1.addfilter(bt.filters.Renko, **fkwargs)
            cerebro.adddata(data1)
    
        # Broker
        cerebro.broker = bt.brokers.BackBroker(**eval('dict(' + args.broker + ')'))
    
        # Sizer
        cerebro.addsizer(bt.sizers.FixedSize, **eval('dict(' + args.sizer + ')'))
    
        # Strategy
        cerebro.addstrategy(St, **eval('dict(' + args.strat + ')'))
    
        # Execute
        kwargs = dict(stdstats=False)
        kwargs.update(**eval('dict(' + args.cerebro + ')'))
        cerebro.run(**kwargs)
    
        if args.plot:  # Plot if requested to
            kwargs = dict(style='candle')
            kwargs.update(**eval('dict(' + args.plot + ')'))
            cerebro.plot(**kwargs)
    
    
    def parse_args(pargs=None):
        parser = argparse.ArgumentParser(
            formatter_class=argparse.ArgumentDefaultsHelpFormatter,
            description=(
                'Renko bricks sample'
            )
        )
    
        parser.add_argument('--data0', default='../../datas/2005-2006-day-001.txt',
                            required=False, help='Data to read in')
    
        # Defaults for dates
        parser.add_argument('--fromdate', required=False, default='',
                            help='Date[time] in YYYY-MM-DD[THH:MM:SS] format')
    
        parser.add_argument('--todate', required=False, default='',
                            help='Date[time] in YYYY-MM-DD[THH:MM:SS] format')
    
        parser.add_argument('--cerebro', required=False, default='',
                            metavar='kwargs', help='kwargs in key=value format')
    
        parser.add_argument('--broker', required=False, default='',
                            metavar='kwargs', help='kwargs in key=value format')
    
        parser.add_argument('--sizer', required=False, default='',
                            metavar='kwargs', help='kwargs in key=value format')
    
        parser.add_argument('--strat', required=False, default='',
                            metavar='kwargs', help='kwargs in key=value format')
    
        parser.add_argument('--plot', required=False, default='',
                            nargs='?', const='{}',
                            metavar='kwargs', help='kwargs in key=value format')
    
        parser.add_argument('--renko', required=False, default='',
                            metavar='kwargs', help='kwargs in key=value format')
    
        parser.add_argument('--dual', required=False, action='store_true',
                            help='put the filter on a second version of the data')
    
        return parser.parse_args(pargs)
    
    
    if __name__ == '__main__':
        runstrat()
    
    

    Command:

    btrenko.py --renko size=35,align=10.0 --plot --dual
    

    Here errors:

    Traceback (most recent call last):
      File "btrenko.py", line 126, in <module>
        runstrat()
      File "btrenko.py", line 74, in runstrat
        cerebro.run(**kwargs)
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader-1.9.74.123-py3.8.egg/backtrader/cerebro.py", line 1127, in run
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader-1.9.74.123-py3.8.egg/backtrader/cerebro.py", line 1293, in runstrategies
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader-1.9.74.123-py3.8.egg/backtrader/cerebro.py", line 1652, in _runonce
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader-1.9.74.123-py3.8.egg/backtrader/lineiterator.py", line 297, in _once
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader-1.9.74.123-py3.8.egg/backtrader/lineiterator.py", line 297, in _once
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader-1.9.74.123-py3.8.egg/backtrader/lineiterator.py", line 297, in _once
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader-1.9.74.123-py3.8.egg/backtrader/linebuffer.py", line 630, in _once
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader-1.9.74.123-py3.8.egg/backtrader/lineroot.py", line 165, in oncestart
      File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/backtrader-1.9.74.123-py3.8.egg/backtrader/linebuffer.py", line 672, in once
    IndexError: array index out of range
    

    My dataset got 65k rows. 1min data.

    alt text

    I don't really understand why this doesn't work. I've already checked data for NaN values or errors, but they are clean.

    Can you help me? Thank you!



  • Ok problem was renko brick size. I set 1 instead of 35 and it worked.



  • But it seems to work only with 1 as brick size.


Log in to reply
 

});