For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

all data always available when backtesting with a pandas dataframe feed

  • 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
        # Run over everything

    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?


  • administrators

    @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

  • 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.

Log in to reply