Plotting JUST the account's value

  • Hello all. I'm new to backtrader and I'm excited to learn it.

    I've managed to set up a Simple Moving Average Crossover strategy to get started, that processes multiple stock symbols. The backtest does in fact work, which made me happy, but in a Jupyter notebook the resulting plot is cramped to the point that it's hard to read it. Most of the time I am interested only in the performance of the account. How can I get a plot of JUST what's happening in the account (it's overall value, cash available, like what's normally the top-most plot)? Is there a way to restrict, after the fact (so not by setting each symbol's plot parameter, and not redoing the backtest every time I want a new plot), which symbols actually get plotted? Is there a way to resize the plot? I've been looking through the documentation (excellent, by the way), and I'm struggling to see how these are controlled.

  • administrators

    The plotting functionality was designed with data as the centre of the plot.

    The easiest way is to set data.plotinfo.plot = False for data feeds right after creation or passing data = MyDataFeed(dataname=xxx, plot=False) during creation.

    For large numbers of symbols the 2nd approach seems easier if you pack that argument into kwargs and end up doing data = MyDataFeed(dataname=xxx, **kwargs). Thosekwargsmay contain other arguments likefromdate,todateandtimeframeandcompression`

  • @backtrader This reply was helpful, and thank you for that! I've also been looking at your multiple series example as a reference. I figured out how to get all the series to plot together like in your example and I have a few questions.

    • In the legend the plot produces only the last series is shown in the legend (in your example you only see d2; in mine, out of AAPL, GOOG, and MSFT, only MSFT appears in the legend). Is there some way to either show all series or suppress this aspect of the legend all together?
    • The default statistics (stdstats=True when a Cerebro instance is created) gets cluttered, so I turned it off (that is, set stdstats=False), then create my own Broker observer, then add in the BuySell observer. What's strange is that, while you get an indication for every series when a buy or sell was made (that is, there is a plot for every series), I don't get this when I turn off stdstats and add in BuySell myself; it will work for the first data series, but not the rest. Why is this happening, and how can I fix it?

    Also, check the formatting of your reply. :)

    EDIT: I managed to fix the second problem I mentioned by putting self._addobserver(True, bt.observers.BuySell) in the __init__() method of my strategy (your source code suggested that could be done).

  • administrators

    The actual documented way to do it:


    The source code uses internal methods to do things which may break at any time.

    See Docs - Cerebro and look for addoservermulti (the reference is close to the end) It will add the observer as many times as data feeds are present in the system (with each observer in charge of the things happening on one data feed)

Log in to reply

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.