Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
verybadsoldier edited this page Feb 22, 2020 · 15 revisions

Welcome to the backtrader_plotting wiki!

Bokeh Options

The class Bokeh 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.

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

Example:

b = Bokeh(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 Bokeh 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.
  • tabs - the tab mode. Available modes are single and multi. In single mode all datas and indicators will be placed in one tab titled Plots. In multi mode they will go into individual tabs titled Datas and Indicators. Nevertheless tab group can still be modified using option plottab
  • 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-character strings. Each character has to be one of:

  • d- data feed
  • i - indicator
  • o - 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 di 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 dd,id,do. 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: backtrader_plotting 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 or Indicators). Only allowed for data and indicator object. Does only apply for plot master objects.
  • 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. So either use my backtrader fork (which knows about the additional parameters and won't complain) or 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 backrader_plotting 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 = Bokeh(style='bar', plot_mode='single', 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 i for indicators, o for observers or d 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 = Bokeh(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 = Bokeh(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 = Bokeh(style='bar', scheme=Tradimo())
browser = OptBrowser(b, optres, usercolumns=usercolumns, sortcolumn='Profit & Loss', sortasc=False)

browser.start()
Clone this wiki locally