From 84ef588163e95adaa3359200ddaa3a7ef64d0b8c Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 3 Jan 2020 13:27:22 +0100 Subject: [PATCH] support dicts as indicators --- freqtrade/plot/plotting.py | 66 +++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/freqtrade/plot/plotting.py b/freqtrade/plot/plotting.py index e1989b249..2bf4afcfb 100644 --- a/freqtrade/plot/plotting.py +++ b/freqtrade/plot/plotting.py @@ -1,6 +1,6 @@ import logging from pathlib import Path -from typing import Any, Dict, List +from typing import Any, Dict, List, Union import pandas as pd from freqtrade.configuration import TimeRange @@ -13,6 +13,8 @@ from freqtrade.resolvers import StrategyResolver logger = logging.getLogger(__name__) +IndicatorType = Union[List[str], Dict[str, Dict]] + try: from plotly.subplots import make_subplots from plotly.offline import plot @@ -54,9 +56,9 @@ def init_plotscript(config): } -def add_indicators(fig, row, indicators: List[str], data: pd.DataFrame) -> make_subplots: +def _add_indicators_list(fig, row, indicators: List[str], data: pd.DataFrame) -> make_subplots: """ - Generator all the indicator selected by the user for a specific row + Generate all the indicator selected by the user for a specific row :param fig: Plot figure to append to :param row: row number for this plot :param indicators: List of indicators present in the dataframe @@ -81,6 +83,51 @@ def add_indicators(fig, row, indicators: List[str], data: pd.DataFrame) -> make_ return fig +def _add_indicators_dict(fig, row, indicators: Dict[str, Dict], + data: pd.DataFrame) -> make_subplots: + """ + Generate all the indicators selected by the user for a specific row, based on the configuration + :param fig: Plot figure to append to + :param row: row number for this plot + :param indicators: Dict of Indicators with configuration options. + Dict key must correspond to dataframe column. + :param data: candlestick DataFrame + """ + for indicator, conf in indicators.items(): + print(conf) + if indicator in data: + scatter = go.Scatter( + x=data['date'], + y=data[indicator].values, + mode='lines', + name=indicator + ) + fig.add_trace(scatter, row, 1) + else: + logger.info( + 'Indicator "%s" ignored. Reason: This indicator is not found ' + 'in your strategy.', + indicator + ) + + return fig + + +def add_indicators(fig, row, indicators: IndicatorType, data: pd.DataFrame) -> make_subplots: + """ + Generate all the indicator selected by the user for a specific row + :param fig: Plot figure to append to + :param row: row number for this plot + :param indicators: List of indicators present in the dataframe, Or Dict of Indicators + with configuration options. Dict key must correspond to dataframe column. + :param data: candlestick DataFrame + """ + if isinstance(indicators, list): + _add_indicators_list(fig, row, indicators, data) + else: + _add_indicators_dict(fig, row, indicators, data) + + def add_profit(fig, row, data: pd.DataFrame, column: str, name: str) -> make_subplots: """ Add profit-plot @@ -144,8 +191,8 @@ def plot_trades(fig, trades: pd.DataFrame) -> make_subplots: def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFrame = None, - indicators1: List[str] = [], - indicators2: List[str] = [],) -> go.Figure: + indicators1: IndicatorType = [], + indicators2: IndicatorType = [],) -> go.Figure: """ Generate the graph from the data generated by Backtesting or from DB Volume will always be ploted in row2, so Row 1 and 3 are to our disposal for custom indicators @@ -254,7 +301,7 @@ def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFra name='Volume', marker_color='DarkSlateGrey', marker_line_color='DarkSlateGrey' - ) + ) fig.add_trace(volume, 2, 1) # Add indicators to separate row @@ -355,12 +402,15 @@ def load_and_plot_trades(config: Dict[str, Any]): trades_pair = trades.loc[trades['pair'] == pair] trades_pair = extract_trades_of_period(dataframe, trades_pair) + indicators1 = config["indicators1"] + indicators2 = config["indicators2"] + fig = generate_candlestick_graph( pair=pair, data=dataframe, trades=trades_pair, - indicators1=config["indicators1"], - indicators2=config["indicators2"], + indicators1=indicators1, + indicators2=indicators2, ) store_plot_file(fig, filename=generate_plot_filename(pair, config['ticker_interval']),