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/

    Compression using daily and minute timeframes unsuccessful during replaydata()

    General Code/Help
    5
    10
    357
    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.
    • Soham Jain
      Soham Jain last edited by

      Tried running the illustration given in the docs.
      It works well for destination timeframe 'weekly', compression 1
      But fails for timeframes 'daily' and 'minute' using either compression.

      Here's the code that was executed.
      Note:
      -The handy dictionary for timeframe conversion is updated for 'minute'

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import argparse
      
      import backtrader as bt
      import backtrader.feeds as btfeeds
      import backtrader.indicators as btind
      from datetime import datetime
      
      
      class SMAStrategy(bt.Strategy):
          params = (
              ('period', 10),
              ('onlydaily', False),
          )
      
          def __init__(self):
              self.sma = btind.SMA(self.data, period=self.p.period)
      
          def start(self):
              self.counter = 0
      
          def prenext(self):
              self.counter += 1
              print('prenext len %d - counter %d' % (len(self), self.counter))
      
          def next(self):
              self.counter += 1
              print('---next len %d - counter %d' % (len(self), self.counter))
      
      
      def runstrat():
          args = parse_args()
      
          # Create a cerebro entity
          cerebro = bt.Cerebro(stdstats=False)
      
          cerebro.addstrategy(
              SMAStrategy,
              # args for the strategy
              period=args.period,
          )
      
          # Load the Data
          datapath = args.dataname or "nifty19-20.csv"
          data = btfeeds.BacktraderCSVData(dataname=datapath, fromdate=datetime.now().replace(year=2020, month=8, day=17, hour=0, minute=0, second=0), timeframe=bt.TimeFrame.Minutes)
      
          # Handy dictionary for the argument timeframe conversion
          tframes = dict(
              minute=bt.TimeFrame.Minutes,
              daily=bt.TimeFrame.Days,
              weekly=bt.TimeFrame.Weeks,
              monthly=bt.TimeFrame.Months)
      
          # First add the original data - smaller timeframe
          cerebro.replaydata(data,
                             timeframe=tframes['minute'],
                             compression=5)
      
          # Run over everything
          cerebro.run()
      
          # Plot the result
          cerebro.plot(style='bar')
      
      
      def parse_args():
          parser = argparse.ArgumentParser(
              description='Pandas test script')
      
          parser.add_argument('--dataname', default='', required=False,
                              help='File Data to Load')
      
          parser.add_argument('--timeframe', default='minute', required=False,
                              choices=['minute', 'daily', 'weekly', 'monhtly'],
                              help='Timeframe to resample to')
      
          parser.add_argument('--compression', default=10, required=False, type=int,
                              help='Compress n bars into 1')
      
          parser.add_argument('--period', default=10, required=False, type=int,
                              help='Period to apply to indicator')
      
          return parser.parse_args()
      
      
      if __name__ == '__main__':
          runstrat()
      
      Output:
      ---next len 1476 - counter 1476
      ---next len 1477 - counter 1477
      ---next len 1478 - counter 1478
      ---next len 1479 - counter 1479
      ---next len 1480 - counter 1480
      ---next len 1481 - counter 1481
      ---next len 1482 - counter 1482
      ---next len 1483 - counter 1483
      ---next len 1484 - counter 1484
      ---next len 1485 - counter 1485
      ---next len 1486 - counter 1486
      ---next len 1487 - counter 1487
      ---next len 1488 - counter 1488
      ---next len 1489 - counter 1489
      ---next len 1490 - counter 1490
      ---next len 1491 - counter 1491
      ---next len 1492 - counter 1492
      ---next len 1493 - counter 1493
      ---next len 1494 - counter 1494
      ---next len 1495 - counter 1495
      ---next len 1496 - counter 1496
      ---next len 1497 - counter 1497
      ---next len 1498 - counter 1498
      ---next len 1499 - counter 1499
      ---next len 1500 - counter 1500
      

      Am I missing something?

      1 Reply Last reply Reply Quote 3
      • Jinisha Sheth
        Jinisha Sheth last edited by

        I faced a similar problem:(
        I guess the issue is how backtrader comprehends time frame of the smaller data feed provided. Unless explicitly mentioned, it defaults to daily and hence it can not build a bar with a tf smaller than daily.

        1 Reply Last reply Reply Quote 0
        • Soham Jain
          Soham Jain last edited by

          Already done that. Still no Luck.

          1 Reply Last reply Reply Quote 2
          • R
            rajanprabu last edited by

            @Soham-Jain said in Compression using daily and minute timeframes unsuccessful during replaydata():

            data = btfeeds.BacktraderCSVData(dataname=datapath, fromdate=datetime.now().replace(year=2020, month=8, day=17, hour=0, minute=0, second=0), timeframe=bt.TimeFrame.Minutes)

            @Soham-Jain I think you need to give compression along with timeframe. say compression=1 to indicate its 1 minute timeframe. BT by default takes it as 1 day but not sure if it takes 1 minutes for bt.TimeFrame.Minutes.

            1 Reply Last reply Reply Quote 0
            • Soham Jain
              Soham Jain last edited by

              @rajanprabu said in Compression using daily and minute timeframes unsuccessful during replaydata():

              te timefr

              I tried it with the additional param compression=1 but the result is still the same.

              from __future__ import (absolute_import, division, print_function,
                                      unicode_literals)
              
              import argparse
              
              import backtrader as bt
              import backtrader.feeds as btfeeds
              import backtrader.indicators as btind
              from datetime import datetime
              
              
              class SMAStrategy(bt.Strategy):
                  params = (
                      ('period', 10),
                      ('onlydaily', False),
                  )
              
                  def __init__(self):
                      pass
              
                  def start(self):
                      self.counter = 0
              
                  def prenext(self):
                      self.counter += 1
                      print(f'{self.datas[0].datetime.datetime(0)} prenext len %d - counter %d' % (len(self), self.counter))
              
                  def next(self):
                      self.counter += 1
                      print(f'{self.datas[0].datetime.datetime(0)} ---next len %d - counter %d' % (len(self), self.counter))
              
              
              def runstrat():
                  args = parse_args()
              
                  # Create a cerebro entity
                  cerebro = bt.Cerebro(stdstats=False)
              
                  cerebro.addstrategy(
                      SMAStrategy,
                      # args for the strategy
                      period=args.period,
                  )
              
                  # Load the Data
                  datapath = args.dataname or "nifty19-20.csv"
                  data = btfeeds.BacktraderCSVData(dataname=datapath, fromdate=datetime.now().replace(year=2020, month=8, day=17, hour=0, minute=0, second=0), timeframe=bt.TimeFrame.Minutes, compression=1)
              
                  # Handy dictionary for the argument timeframe conversion
                  tframes = dict(
                      minute=bt.TimeFrame.Minutes,
                      daily=bt.TimeFrame.Days,
                      weekly=bt.TimeFrame.Weeks,
                      monthly=bt.TimeFrame.Months)
              
                  # First add the original data - smaller timeframe
                  cerebro.replaydata(data,
                                     timeframe=tframes['daily'],
                                     compression=1)
              
                  # Run over everything
                  cerebro.run()
              
                  # Plot the result
                  cerebro.plot(style='bar')
              
              
              def parse_args():
                  parser = argparse.ArgumentParser(
                      description='Pandas test script')
              
                  parser.add_argument('--dataname', default='', required=False,
                                      help='File Data to Load')
              
                  parser.add_argument('--timeframe', default='minute', required=False,
                                      choices=['minute', 'daily', 'weekly', 'monhtly'],
                                      help='Timeframe to resample to')
              
                  parser.add_argument('--compression', default=10, required=False, type=int,
                                      help='Compress n bars into 1')
              
                  parser.add_argument('--period', default=10, required=False, type=int,
                                      help='Period to apply to indicator')
              
                  return parser.parse_args()
              
              
              if __name__ == '__main__':
                  runstrat()
              
              2020-08-20 23:59:59.999989 ---next len 1482 - counter 1482
              2020-08-20 23:59:59.999989 ---next len 1483 - counter 1483
              2020-08-20 23:59:59.999989 ---next len 1484 - counter 1484
              2020-08-20 23:59:59.999989 ---next len 1485 - counter 1485
              2020-08-20 23:59:59.999989 ---next len 1486 - counter 1486
              2020-08-20 23:59:59.999989 ---next len 1487 - counter 1487
              2020-08-20 23:59:59.999989 ---next len 1488 - counter 1488
              2020-08-20 23:59:59.999989 ---next len 1489 - counter 1489
              2020-08-20 23:59:59.999989 ---next len 1490 - counter 1490
              2020-08-20 23:59:59.999989 ---next len 1491 - counter 1491
              2020-08-20 23:59:59.999989 ---next len 1492 - counter 1492
              2020-08-20 23:59:59.999989 ---next len 1493 - counter 1493
              2020-08-20 23:59:59.999989 ---next len 1494 - counter 1494
              2020-08-20 23:59:59.999989 ---next len 1495 - counter 1495
              2020-08-20 23:59:59.999989 ---next len 1496 - counter 1496
              2020-08-20 23:59:59.999989 ---next len 1497 - counter 1497
              2020-08-20 23:59:59.999989 ---next len 1498 - counter 1498
              2020-08-20 23:59:59.999989 ---next len 1499 - counter 1499
              2020-08-20 23:59:59.999989 ---next len 1500 - counter 1500
              

              :(

              I still can;t figure out where exactly am I going wrong!
              Btw, Thanks Rajan for the help.

              1 Reply Last reply Reply Quote 2
              • C
                crunchypickle last edited by

                might be just easier to compress it yourself with Pandas

                1 Reply Last reply Reply Quote 0
                • Soham Jain
                  Soham Jain last edited by

                  1. But will it simulate replay data?
                  2. pandas will just help resample the minute data to a higher timeframe. Am I right?
                  C 1 Reply Last reply Reply Quote 2
                  • C
                    crunchypickle @Soham Jain last edited by

                    @Soham-Jain I haven't worked on resampling for a while but I'm quite sure pandas should support downsampling. That is a very common data science task.

                    1 Reply Last reply Reply Quote 0
                    • A
                      ab_trader last edited by

                      try to add sessionstart and sessionend parameters. seems logical to have them.

                      • If my answer helped, hit reputation up arrow at lower right corner of the post.
                      • Python Debugging With Pdb
                      • New to python and bt - check this out
                      1 Reply Last reply Reply Quote 1
                      • Soham Jain
                        Soham Jain last edited by

                        try to add sessionstart and sessionend parameters. seems logical to have them.

                        It worked. Can't thank you enough @ab_trader

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