Navigation

    Backtrader Community

    • Register
    • 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/

    [BC Break] IndexError: index out of range after commit cdb80f8472fc1aeb05d84c5b52b86412e3f89632

    General Code/Help
    2
    4
    1090
    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.
    • B
      borodiliz last edited by borodiliz

      Hi @backtrader,

      This code has stopped working for me after commit db80f8472fc1aeb05d84c5b52b86412e3f89632

      pypy bc-break.py --adddata1m -> OK
      pypy bc-break.py --addresample10m -> OK
      pypy bc-break.py --addresample15m -> OK
      pypy bc-break.py --adddata1m --addresample10m --addresample15m -> FAIL

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      
      import argparse
      import datetime
      
      import backtrader as bt
      
      
      class St(bt.Strategy):
          params = dict(
      
                        talib=False
                        )
      
          def __init__(self):
              ''''''
             
      
          def next(self):
              txt = []
              txt.append('{:04d}'.format(len(self)))
              txt.append('{:04d}'.format(len(self.data0)))
              txt.append(self.data0.datetime.datetime())
              txt.append('{:.2f}'.format(self.data0.close[0]))
      
              try:
                  if len(self.datas[1]):
                      txt.append('{:04d}'.format(len(self.data1)))
                      txt.append(self.data1.datetime.datetime())
                      txt.append('{:.2f}'.format(self.data1.close[0]))
      
              except IndexError:
                  False #Nothing to do
      
              try:
                  if len(self.datas[2]):
                      txt.append('{:04d}'.format(len(self.data2)))
                      txt.append(self.data2.datetime.datetime())
                      txt.append('{:.2f}'.format(self.data2.close[0]))
              except IndexError:
                  False #Nothing to do
      
              print(','.join(str(x) for x in txt))
      
      
      def runstrat(args=None):
          args = parse_args(args)
      
          cerebro = bt.Cerebro()
      
          # Data feed kwargs
          kwargs = dict(
                        timeframe=bt.TimeFrame.Minutes,
                        compression=5,
                        )
      
          # 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)
      
          # Data feed
          data0 = bt.feeds.BacktraderCSVData(dataname=args.data0, ** kwargs)
      
          if args.adddata1m:
              cerebro.adddata(data0)
      
          if args.addresample10m:
              cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=10)
      
          if args.addresample15m:
              cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=15)
      
          # 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
          cerebro.run(** eval('dict(' + args.cerebro + ')'))
      
          if args.plot:  # Plot if requested to
              cerebro.plot(** eval('dict(' + args.plot + ')'))
      
      
      def parse_args(pargs=None):
          parser = argparse.ArgumentParser(
                                           formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                           description=(
                                           'Sample Skeleton'
                                           )
                                           )
      
          parser.add_argument('--data0', default='../../datas//2006-min-005.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('--adddata1m', action='store_true', required=False,
                              help='Add 1 minute data')
      
          parser.add_argument('--addresample10m', action='store_true', required=False,
                              help='Add resample for 10 minute data')
      
          parser.add_argument('--addresample15m', action='store_true', required=False,
                              help='Add resample for 15 minute data')
      
      
          return parser.parse_args(pargs)
      
      
      if __name__ == '__main__':
          runstrat()
      

      It seems a problem while combining adddata and resampledata

      Thanks in advance!

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

        That's the development branch. It's not guaranteed to work and in this case it doesn't.

        The latest commit for the stable and working (as good as possible) master branch: Latest commit 61ae6d9 17 days ago

        1 Reply Last reply Reply Quote 0
        • B
          borodiliz last edited by

          I realized that it is only in the development branch. Sometimes I sync this branch and test my code.
          Should we contact you if we detect any problems on this branch?

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

            You can always post anything you may consider relevant, but in general a development branch should only be used to check the functionality of a specific commit which may solve something of your interest.

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