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

timeframe parameter for Analyzers and weekly data

  • Hello,

    I am running some backtest with a very simple system using only weekly data (pandas) and I added a few analyzers:

    cerebro.addanalyzer(bt.analyzers.Returns, _name='returns')
    cerebro.addanalyzer(bt.analyzers.DrawDown, _name='drawdown')
    cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpe', riskfreerate=0.0, annualize=True)

    Portfolio results for return didn't look right to me:

    Total Net Profit 1582338.10
    CAGR 58.0894%
    Max Drawdown 22.6135%
    Max Drawdown Len 91
    Sharpe Ratio 0.8554

    I modified the code to add the analyzer adding the timeframe parameter:

    cerebro.addanalyzer(bt.analyzers.Returns, _name='returns', timeframe=bt.TimeFrame.Weeks)
    cerebro.addanalyzer(bt.analyzers.DrawDown, _name='drawdown')
    cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpe', riskfreerate=0.0, annualize=True, timeframe=bt.TimeFrame.Weeks)

    Stats now look right:

    Total Net Profit 1582338.10
    CAGR 9.9116%
    Max Drawdown 22.6135%
    Max Drawdown Len 91
    Sharpe Ratio 0.6601

    In the Analyzers/Reference/Returns documentation I read:

    timeframe (default: None)

    If None the timeframe of the 1st data in the system will be used

    Pass TimeFrame.NoTimeFrame to consider the entire dataset with no time constraints

    It's not a big deal to add a parameter but maybe I am missing something here: my data (pandas) are all with weekly compression, shouldn't backtrader infer the weekly timeframe from the 1st data? Am I misunderstanding the docs?



Log in to reply