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

Cerebro Backtest Time Frame

  • How do we set the time frame within which Cerebro simulates a Strategy?

    I understand we can supply fromdate and todate while fetching data but that specifies data time frame that includes unstable period of indicators, etc. and not backtest time frame.

  • administrators

    fromdate and todate specify time spans and not time frames and apply only to data feeds.

    There is no such thing as time frame (nor time span) for Cerebro.

    Some extra elaboration is really needed to understand the question.

  • @backtrader - Thank you again for the prompt response.

    Lets say we want to simulate/backtest a Strategy between any given dates eg. 2nd Jan 2012 to 1st Jan 2013, we need to calculate fromdate and todate for the DataFeed in such a way that indicator lookback periods are taken into account and Backtest starts from exact startdate we wanted it to.

    This is cumbersome and may be we can bake this login into Cerebro such that irrespective of the Data supplied to it, it can calculate indicators applying the relevant lookbak period or from the beginning of data feed and start the backtest from the date we want it to start.

  • administrators

    Don't take any action in next until the 2nd Jan 2012 is met.

  • Good idea!! We can do that! But that is hacky!

    The right pattern would be - Cerebro should not call until startdate and after enddate

  • administrators

    Subclass cerebro.

  • Ok... Thank you for the suggestion.

    Here is how I am doing it.

    # Cerebro Sub Class
    class Cerebro(bt.Cerebro):
        params = (("startdate", None), ("enddate", None))
    class MyStrategy(bt.Strategy):
        def next(self):
            if self.env.p.startdate is not None:
                if self.env.p.startdate > self.datas[0]
            if self.env.p.enddate is not None:
                if self.env.p.enddate < self.datas[0]
           # Your Logic goes here

    This works as intended but not as efficient as it can be since the indicators are calculated inside the strategy from the beginning of the data feed.

Log in to reply