Merge pull request #5003 from rokups/rk/plotting

Indicator plotting improvement
This commit is contained in:
Matthias 2021-05-30 18:44:54 +01:00 committed by GitHub
commit f9bcf19f9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 9 deletions

View File

@ -170,9 +170,15 @@ Additional features when using plot_config include:
* Specify additional subplots * Specify additional subplots
* Specify indicator pairs to fill area in between * Specify indicator pairs to fill area in between
The sample plot configuration below specifies fixed colors for the indicators. Otherwise consecutive plots may produce different colorschemes each time, making comparisons difficult. The sample plot configuration below specifies fixed colors for the indicators. Otherwise, consecutive plots may produce different color schemes each time, making comparisons difficult.
It also allows multiple subplots to display both MACD and RSI at the same time. It also allows multiple subplots to display both MACD and RSI at the same time.
Plot type can be configured using `type` key. Possible types are:
* `scatter` corresponding to `plotly.graph_objects.Scatter` class (default).
* `bar` corresponding to `plotly.graph_objects.Bar` class.
Extra parameters to `plotly.graph_objects.*` constructor can be specified in `plotly` dict.
Sample configuration with inline comments explaining the process: Sample configuration with inline comments explaining the process:
``` python ``` python
@ -198,7 +204,8 @@ Sample configuration with inline comments explaining the process:
# Create subplot MACD # Create subplot MACD
"MACD": { "MACD": {
'macd': {'color': 'blue', 'fill_to': 'macdhist'}, 'macd': {'color': 'blue', 'fill_to': 'macdhist'},
'macdsignal': {'color': 'orange'} 'macdsignal': {'color': 'orange'},
'macdhist': {'type': 'bar', 'plotly': {'opacity': 0.9}}
}, },
# Additional subplot RSI # Additional subplot RSI
"RSI": { "RSI": {
@ -213,6 +220,9 @@ Sample configuration with inline comments explaining the process:
The above configuration assumes that `ema10`, `ema50`, `senkou_a`, `senkou_b`, The above configuration assumes that `ema10`, `ema50`, `senkou_a`, `senkou_b`,
`macd`, `macdsignal`, `macdhist` and `rsi` are columns in the DataFrame created by the strategy. `macd`, `macdsignal`, `macdhist` and `rsi` are columns in the DataFrame created by the strategy.
!!! Warning
`plotly` arguments are only supported with plotly library and will not work with freq-ui.
## Plot profit ## Plot profit
![plot-profit](assets/plot-profit.png) ![plot-profit](assets/plot-profit.png)

View File

@ -96,20 +96,34 @@ def add_indicators(fig, row, indicators: Dict[str, Dict], data: pd.DataFrame) ->
Dict key must correspond to dataframe column. Dict key must correspond to dataframe column.
:param data: candlestick DataFrame :param data: candlestick DataFrame
""" """
plot_kinds = {
'scatter': go.Scatter,
'bar': go.Bar,
}
for indicator, conf in indicators.items(): for indicator, conf in indicators.items():
logger.debug(f"indicator {indicator} with config {conf}") logger.debug(f"indicator {indicator} with config {conf}")
if indicator in data: if indicator in data:
kwargs = {'x': data['date'], kwargs = {'x': data['date'],
'y': data[indicator].values, 'y': data[indicator].values,
'mode': 'lines',
'name': indicator 'name': indicator
} }
if 'color' in conf:
kwargs.update({'line': {'color': conf['color']}}) plot_type = conf.get('type', 'scatter')
scatter = go.Scatter( color = conf.get('color')
**kwargs if plot_type == 'bar':
) kwargs.update({'marker_color': color or 'DarkSlateGrey',
fig.add_trace(scatter, row, 1) 'marker_line_color': color or 'DarkSlateGrey'})
else:
if color:
kwargs.update({'line': {'color': color}})
kwargs['mode'] = 'lines'
if plot_type != 'scatter':
logger.warning(f'Indicator {indicator} has unknown plot trace kind {plot_type}'
f', assuming "scatter".')
kwargs.update(conf.get('plotly', {}))
trace = plot_kinds[plot_type](**kwargs)
fig.add_trace(trace, row, 1)
else: else:
logger.info( logger.info(
'Indicator "%s" ignored. Reason: This indicator is not found ' 'Indicator "%s" ignored. Reason: This indicator is not found '