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

Plotting Problems

  • Re: Plotting inside JupyterLab

    I am trying to teach myself backtrader, after doing the quickstart I jumped into the Momemtum Strategy blog by Teddy Kokker.

    There are two things I would like help with:

    1. Everything works until I try to plot and get the following :

       *ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running*

      I have tried numerous different iterations / suggestions of trying to import libraries in the right order and to add matplotlib.use('agg') or matplotlib.use('TkAgg'). All result in the same exact error.

    Any help with this would be appreciated.

    1. Since i got stuck on that and it was stated in one of the blogs that backtrader is not intended as a plotting platform I thought fine. I would just plot the final portfolio series directly with matplotlib how hard could this be? I however am having trouble figuring out how to access the time series of the portfolio data. It obviously exists, i.e. the cerebero.plot function is accessing it. The momentum example puts results into results
    results =

    using various print functions I can figure out how to get various other information. How do I get access to the portfolio time series information directly? It does not appear to be in results.

    print(f"Sharpe: {results[0].analyzers.sharperatio.get_analysis()['sharperatio']:.3f}")
    print(f"Norm. Annual Return: {results[0].analyzers.returns.get_analysis()['rnorm100']:.2f}%")
    print(f"Max Drawdown: {results[0].analyzers.drawdown.get_analysis()['max']['drawdown']:.2f}%")
    print(f"Max Drawdown Days: {results[0].analyzers.drawdown.get_analysis()['max']['len']:.0f}")
    print(f"Final Portfolio Value: ${}")
    for x in results[0].analyzers:

  • administrators

    @Tim-Brug said in Plotting Problems:

    How do I get access to the portfolio time series information directly?

    What's exactly the meaning of portfolio time series? A "per-timestamp record of the portfolio value"? If so, that's not stored.

    You would need to plug in an analyzer which records that.

Log in to reply