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/

    all data always available when backtesting with a pandas dataframe feed

    Indicators/Strategies/Analyzers
    3
    3
    629
    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.
    • D
      dongiulio last edited by

      hello,

      I'm not sure what I'm doing wrong, I'm implementing an indicator and loading data from a pandas dataframe,

      here's how I load the data feed:

      data = bt.feeds.PandasData(dataname=pandas, **fxcm.candles_to_backtrader(pandas))
      
          # Add the Data Feed to Cerebro
          cerebro.adddata(data)
      
          # Run over everything
          cerebro.run()
      
      

      where fxcm.candles_to_backtrader(pandas) is defined as:

        def candles_to_backtrader(self, candles, ask=True):
              aob = "ask"
              if not ask:
                  aob = "bid"
      
              return {"name": aob,
                      "open": "%sopen" % aob,
                      "high": "%shigh" % aob,
                      "low": "%slow" % aob,
                      "close": "%sclose" % aob,
                      "volume": "tickqty"}
      

      my indicator is:

      import backtrader as bt
      
      
      class Density(bt.Indicator):
          lines = ('my_indicator',)
          params = ()
      
          def next(self):
              print("new iteration")
              for i in self.datas[0].close:
                  print("available data %f" % i)
      

      next gets called many times, and everytime the for loop prints all the close values as they were produced by candles_to_backtrader method.

      I'm quite confused, I expected that when backtesting I were to find in datas[0] only an incremental fraction of the input data at each call to next.

      So first call I'd see the oldest values, the second call I'd see the oldest and the following ones, and get the full dataset only at the latest call.

      What am I missing here?

      thanks.

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

        @dongiulio said in all data always available when backtesting with a pandas dataframe feed:

        for i in self.datas[0].close:
        

        You are printing the entire buffer instead of addressing a given index.

        Suggested reading: Docs - Platform Concepts - Section: Indexing: 0 and -1

        1 Reply Last reply Reply Quote 1
        • M
          michaelhazza last edited by

          Hi dongiulio,

          I noticed you are using fxcm here. Do you have an fxcm / fxcmpy broker implementation for backtrader that you would be happy to share? I want to use fxcm and hoping to cut down implementation time.

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