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/

    Unable to feed my custom DataFrame to backtrader

    General Code/Help
    1
    5
    32
    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.
    • Ahmad Nazari
      Ahmad Nazari last edited by

      Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:

      Ahmad Nazari 2 Replies Last reply Reply Quote 0
      • Ahmad Nazari
        Ahmad Nazari @Ahmad Nazari last edited by

        @ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:

        Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:

        from __future__ import (absolute_import, division, print_function,
                                unicode_literals)
        
        import datetime  # For datetime objects
        
        # Import the backtrader platform
        import backtrader as bt
        
        
        
        
        # data class
        class PandasData(bt.feeds.DataBase):
            '''
            The ``dataname`` parameter inherited from ``feed.DataBase`` is the pandas
            DataFrame
            '''
        
            params = (
                ('datetime', None),
                ('open', -1),
                ('high', -1),
                ('low', -1),
                ('close', -1),
                ('Volume', -1),
                ('pos_side', -1),
                ('entry', -1),
                ('exit', -1),
                ('pos_result', -1),
                ('ema1', 15),
                ('ema2', 16),
                ('ema3', 17),
            )
        
        
        # Create a Stratey
        class TestStrategy(bt.Strategy):
        
            def log(self, txt, dt=None):
                ''' Logging function for this strategy'''
                dt = dt or self.datas[0].datetime.date(0)
                print('%s, %s' % (dt.isoformat(), txt))
        
            def __init__(self):
                # Keep a reference to the "close" line in the data[0] dataseries
                self.dataclose = self.datas[0].close
        
            def next(self):
                # Simply log the closing price of the series from the reference
                self.log('Close, %.2f' % self.dataclose[0])
        
        
        
        if __name__ == '__main__':
            # Create a cerebro entity
            cerebro = bt.Cerebro()
        
            # Add a strategy
            cerebro.addstrategy(TestStrategy)
        
            # Create a Data Feed
            data_bt = PandasData(dataname=data)
            
        
            # Add the Data Feed to Cerebro
            cerebro.adddata(data_bt)
            
        
            # Set our desired cash start
            cerebro.broker.setcash(1000.0)
        
        
            # Print out the starting conditions
            print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
        
            # Run over everything
            cerebro.run()
        
            # Print out the final result
            print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
        
        Ahmad Nazari 1 Reply Last reply Reply Quote 0
        • Ahmad Nazari
          Ahmad Nazari @Ahmad Nazari last edited by

          @ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:

          @ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:

          Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:

          from __future__ import (absolute_import, division, print_function,
                                  unicode_literals)
          
          import datetime  # For datetime objects
          
          # Import the backtrader platform
          import backtrader as bt
          
          
          
          
          # data class
          class PandasData(bt.feeds.DataBase):
              '''
              The ``dataname`` parameter inherited from ``feed.DataBase`` is the pandas
              DataFrame
              '''
          
              params = (
                  ('datetime', None),
                  ('open', -1),
                  ('high', -1),
                  ('low', -1),
                  ('close', -1),
                  ('Volume', -1),
                  ('pos_side', -1),
                  ('entry', -1),
                  ('exit', -1),
                  ('pos_result', -1),
                  ('ema1', 15),
                  ('ema2', 16),
                  ('ema3', 17),
              )
          
          
          # Create a Stratey
          class TestStrategy(bt.Strategy):
          
              def log(self, txt, dt=None):
                  ''' Logging function for this strategy'''
                  dt = dt or self.datas[0].datetime.date(0)
                  print('%s, %s' % (dt.isoformat(), txt))
          
              def __init__(self):
                  # Keep a reference to the "close" line in the data[0] dataseries
                  self.dataclose = self.datas[0].close
          
              def next(self):
                  # Simply log the closing price of the series from the reference
                  self.log('Close, %.2f' % self.dataclose[0])
          
          
          
          if __name__ == '__main__':
              # Create a cerebro entity
              cerebro = bt.Cerebro()
          
              # Add a strategy
              cerebro.addstrategy(TestStrategy)
          
              # Create a Data Feed
              data_bt = PandasData(dataname=data)
              
          
              # Add the Data Feed to Cerebro
              cerebro.adddata(data_bt)
              
          
              # Set our desired cash start
              cerebro.broker.setcash(1000.0)
          
          
              # Print out the starting conditions
              print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
          
              # Run over everything
              cerebro.run()
          
              # Print out the final result
              print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
          

          But, this code doesn't work and the length of the feed data (data_bt) will be 0!

          I tried different methods from different sites for do this but I couldn't solve the problem, Please help me to feed my data to backtrader. Thank you!

          Ahmad Nazari 1 Reply Last reply Reply Quote 0
          • Ahmad Nazari
            Ahmad Nazari @Ahmad Nazari last edited by

            @ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:

            @ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:

            @ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:

            Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:

            from __future__ import (absolute_import, division, print_function,
                                    unicode_literals)
            
            import datetime  # For datetime objects
            
            # Import the backtrader platform
            import backtrader as bt
            
            
            
            
            # data class
            class PandasData(bt.feeds.DataBase):
                '''
                The ``dataname`` parameter inherited from ``feed.DataBase`` is the pandas
                DataFrame
                '''
            
                params = (
                    ('datetime', None),
                    ('open', -1),
                    ('high', -1),
                    ('low', -1),
                    ('close', -1),
                    ('Volume', -1),
                    ('pos_side', -1),
                    ('entry', -1),
                    ('exit', -1),
                    ('pos_result', -1),
                    ('ema1', 15),
                    ('ema2', 16),
                    ('ema3', 17),
                )
            
            
            # Create a Stratey
            class TestStrategy(bt.Strategy):
            
                def log(self, txt, dt=None):
                    ''' Logging function for this strategy'''
                    dt = dt or self.datas[0].datetime.date(0)
                    print('%s, %s' % (dt.isoformat(), txt))
            
                def __init__(self):
                    # Keep a reference to the "close" line in the data[0] dataseries
                    self.dataclose = self.datas[0].close
            
                def next(self):
                    # Simply log the closing price of the series from the reference
                    self.log('Close, %.2f' % self.dataclose[0])
            
            
            
            if __name__ == '__main__':
                # Create a cerebro entity
                cerebro = bt.Cerebro()
            
                # Add a strategy
                cerebro.addstrategy(TestStrategy)
            
                # Create a Data Feed
                data_bt = PandasData(dataname=data)
                
            
                # Add the Data Feed to Cerebro
                cerebro.adddata(data_bt)
                
            
                # Set our desired cash start
                cerebro.broker.setcash(1000.0)
            
            
                # Print out the starting conditions
                print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
            
                # Run over everything
                cerebro.run()
            
                # Print out the final result
                print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
            

            But, this code doesn't work and the length of the feed data (data_bt) will be 0!

            I tried different methods from different sites for do this but I couldn't solve the problem, Please help me to feed my data to backtrader. Thank you!

            @backtrader @vladisld

            1 Reply Last reply Reply Quote 0
            • Ahmad Nazari
              Ahmad Nazari @Ahmad Nazari last edited by

              @ahmad-nazari said in Unable to feed my custom DataFrame to backtrader:

              Hi, I have a custom Pandas DataFrame and I want to feed this data that has different column names to backtrader. For do this, I wrote my code according to documentation:

              90990fd0-6ab1-4791-bcd6-2c598835012e-image.png

              this is the output of the above code. (I add the print sections to better represent the problem)

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