-
Notifications
You must be signed in to change notification settings - Fork 131
Home
Welcome to the backtrader_plotting wiki!
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 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 aresingle
andmulti
. Insingle
mode all datas and indicators will be placed in one tab titledPlots
. Inmulti
mode they will go into individual tabs titledDatas
andIndicators
. Nevertheless tab group can still be modified using optionplottab
-
show_headline
- Places a headline on top of the result page
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.
Available options: Lots of backtrader
's built-in options are supported. A (probably incomplete) list:
subplot
plotmaster
plotname
-
plot
/plotskip
-
- not supported. Please useplotabove
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
orIndicators
). 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 to0
. 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'
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 specifiedplotid
-
#:<type>-<n>
- Selects the n-th object of type. Type can beii
for indicators,o
for observers ord
for datas -
r:<regex>
- A regular expression matching the label of one or more objects
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).
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()
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()
Trading domains are basically used to group entities that belong together to plot them together on one page. A trading domain is a single string. All entities having an identical string belong to the same trading domain.
Per default each data feed creates one trading domain which is derived from its _name
attribute. All entities that are based on this data (like e.g. indiators) will inherit the trading domain of that data. So by default each data and its corresponding indicators and others entities do form a separate trading domain.
Trading domain values can be manually overridden though to change the automtically created grouping. This is done by passing the parameter tradingdomain
to the initializer of an entity.
To use live plotting you need the backtrader
package with a custom modification which extends backtrader
with an interface that allows to retrieve accurate real-time data.
To enable that you have to merge this commit into your backtrader
:
https://github.com/verybadsoldier/backtrader/commit/794a7eca5c0335e8b8f4d0d39561fb3af4c4f1b5
This might be a bit inconvenient but it was done intentionally that way as it assures precise data processing in live mode.