Skip to content
happydasch edited this page Jul 18, 2020 · 4 revisions

Welcome to the btplotting wiki!

BacktraderPlotting Options

The class BacktraderPlotting accepts the following special options:

  • scheme - a scheme object for rendering
  • filename - output filename
  • plotconfig - a Python dictionary with individual plot options as described below
  • output_mode - one of 'show', 'save' or 'memory'. memory will not save to file but return the model. save will save the model to disk. show will save the model to disk and automatically launch in browser.
  • tabs - A list with tabs to add
  • use_default_tabs - If False, then no tabs will be added by default, if set to True, default tabs will be added

All other options will be interpreted as scheme parameters and will be directly forwarded to modify the currently used scheme.

Example:

b = BacktraderPlotting(style='line', scheme=Blackly(), output_mode='memory', legend_text_color='#ff0000')

Scheme Options

Scheme options change the way the resulting web pages are rendered. These include style option (like the color and background) but also structural options (like will tabs be automatically used). These options are part of the scheme in use (e.g. Blackly or Tradimo). Individual options can also be passed as parameters to the BacktraderPlotting object.

Structural Options:

  • hover_config - controls a comma separated list of strings that control which data is shown in the hover tooltips. Please refer to the hover tooltip section for details.
  • mutliple_tabs - If set to False all datas, indicators and observers will be placed in one tab titled Plots. If set to True they will go into individual tabs titled Datas, Indicators and Observers. Nevertheless tab group can still be modified using option plottab. A setted plottab will ignore the multiple_tabs option.
  • show_headline - Places a headline on top of the result page

Hover Tooltips

By default all data lines in a figure are also placed in the corresponding hover tooltip. More data lines can be added using the scheme option hover_tooltip_config. It consists of a comma separated list of 2 object type descriptions connected with a -. Each has to be one of:

  • DATA- data feed
  • IND - indicator
  • OBS - observer

It configures which types should be added to which other types. The first character is the input type and the second character the target type. So DATA-IND would mean: Place tooltip lines of all data feeds in the charts of all indicator figures. You can have multiple of those rules separated by commas. Example DATA-DATA,IND-DATA,DATA-OBS. This would place all data feed lines to all other data feeds, would place all indicator lines to all data feeds and all data feeds to all observers.

Plotting Options

Available options: Lots of backtrader's built-in options are supported. A (probably incomplete) list:

  • subplot
  • plotmaster
  • plotname
  • plot/ plotskip
  • plotabove - not supported. Please use plotorder instead.
  • plothlines
  • plotyhlines
  • plotyticks

Additional Parameters: btplotting also features some extra plotting parameters:

  • plotid - assigns an identifier to a plot object. Used to reference it in the late plot configuration.
  • plottab - name of a tab this object should be plotted to. Allows to define custom tabs. If not provided then objects will be plotted by category (Datas, Indicators or Observers).
  • plotaspectratio - assigns a custom aspect ratio
  • plotorder - an integer defining the order of the objects inside a tab. All objects default to 0. Can only be used on data masters. Objects are ordered with smaller numbers first.

NOTE: Those additional parameters are not known to vanilla backtrader so when using them regularly in constructor calls will lead to errors about unknown parameters. Set the parameters manually after creating the object like so:

ind = bt.indicators.EMA(40)
ind.plotinfo.plotid = 'myema'

Plot Configuration

Normally in backtader the plotting configuration is done inside the strategy code to apply plotting options directly to your python objects. When using btplotting then plotting configuration can be done separated from the strategy code. The plotting can be configured in the code after the backtest finished running like this:

    plotconfig = {
        'id:IND#0': dict(
            subplot=True,
        ),
    }
    b = BacktraderPlotting(style='bar', scheme=Tradimo(), plotconfig=plotconfig)
    cerebro.plot(b)

plotconfig is a dictionary. Every key is an expression that selects one or more entities which plot configuration has to be configured. Each value is another dictionary with attributes that will be assigned to the selected objects.

The key has to be in one of these formats:

  • id:<plotid> - Selects the entitiy with the specified plotid
  • #:<type>-<n> - Selects the n-th object of type. Type can bei IND for indicators, OBS for observers or DATA for datas
  • r:<regex> - A regular expression matching the label of one or more objects

Optimization Browser

The OptBrowser will start a web server to run an interactive web app to browse optimization results.

Example:

result = cerebro.run(optreturn=True)

b = BacktraderPlotting(style='bar', scheme=Tradimo())
browser = OptBrowser(b, result)
browser.start()

After running this open a web browser to your local address http://localhost. You will be presented a list of optimization results and a plot of the currently selected result. When clicking another result in the list a new plot will be loaded which takes some seconds (no busy indicator yet).

User Columns

Custom-defined columns can be added to the result list to show special properties of interest from the results. To use it pass a dict where they keys are labels for the column and the value is a callable which expects a single optimization result to calculate a property from.

Example:

def get_pnl_gross(strats):
    a = strats[0].analyzers.tradeanalyzer.get_analysis()
    return a.pnl.gross.total if 'pnl' in a else 0

b = BacktraderPlotting(style='bar', scheme=Tradimo())
browser = OptBrowser(b, result, usercolumns=dict(pnl=get_pnl_gross), sortcolumn='pnl', sortasc=False)
browser.start()

Ordered Results

When dealing with a huge set of optimization results then the number of displayed results can be limited by using the parameter num_result_limit. Also the list of results can be sorted by column by providing the parameter sortcolumn with a name of a column. Parameter sortasc control if sorting will be ascending or descending.

Example:

def df(optresults):
    a = [x.analyzers.tradeanalyzer.get_analysis() for x in optresults]
    return sum([x.pnl.gross.total if 'pnl' in x else 0 for x in a])

usercolumns = {'Profit & Loss': df}

b = BacktraderPlotting(style='bar', scheme=Tradimo())
browser = OptBrowser(b, optres, usercolumns=usercolumns, sortcolumn='Profit & Loss', sortasc=False)

browser.start()

Datadomains

Datadomains are basically used to group entities that belong together to plot them together on one page. A datadomain is a single string. All entities having an identical string belong to the same datadomain.

Per default each data feed creates one datadomain which is derived from its _name attribute. All entities that are based on this data (like e.g. indiators) will inherit the datadomain of that data. So by default each data and its corresponding indicators and others entities do form a separate datadomain.

Datadomain values can be manually overridden though to change the automtically created grouping. This is done by passing the parameter datadomain to the initializer of an entity.