From c7a4a16eec3778ccc6b8b2cdab66e0c3434ad242 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 30 Jun 2019 10:31:36 +0200 Subject: [PATCH] Create generate_plot_graph --- freqtrade/plot/plotting.py | 35 +++++++++++++++++++++++++++++++++-- scripts/plot_profit.py | 33 ++------------------------------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/freqtrade/plot/plotting.py b/freqtrade/plot/plotting.py index 4c45c0375..04e246371 100644 --- a/freqtrade/plot/plotting.py +++ b/freqtrade/plot/plotting.py @@ -5,9 +5,10 @@ from typing import Any, Dict, List, Optional import pandas as pd from freqtrade.arguments import Arguments -from freqtrade.exchange import Exchange from freqtrade.data import history -from freqtrade.data.btanalysis import load_trades +from freqtrade.data.btanalysis import (combine_tickers_with_mean, + create_cum_profit, load_trades) +from freqtrade.exchange import Exchange from freqtrade.resolvers import ExchangeResolver, StrategyResolver logger = logging.getLogger(__name__) @@ -28,6 +29,7 @@ class FTPlots(): self._config = config self.exchange: Optional[Exchange] = None + # Exchange is only needed when downloading data! if self._config.get("live", False) or self._config.get("refresh_pairs", False): self.exchange = ExchangeResolver(self._config.get('exchange', {}).get('name'), self._config).exchange @@ -258,6 +260,35 @@ def generate_candlestick_graph(pair: str, data: pd.DataFrame, trades: pd.DataFra return fig +def generate_profit_graph(pairs: str, tickers: Dict[str, pd.DataFrame], trades: pd.DataFrame = None, + ) -> go.Figure: + # Combine close-values for all pairs, rename columns to "pair" + df_comb = combine_tickers_with_mean(tickers, "close") + + # Add combined cumulative profit + df_comb = create_cum_profit(df_comb, trades, 'cum_profit') + + # Plot the pairs average close prices, and total profit growth + avgclose = go.Scattergl( + x=df_comb.index, + y=df_comb['mean'], + name='Avg close price', + ) + + fig = tools.make_subplots(rows=3, cols=1, shared_xaxes=True, row_width=[1, 1, 1]) + + fig.append_trace(avgclose, 1, 1) + fig = add_profit(fig, 2, df_comb, 'cum_profit', 'Profit') + + for pair in pairs: + profit_col = f'cum_profit_{pair}' + df_comb = create_cum_profit(df_comb, trades[trades['pair'] == pair], profit_col) + + fig = add_profit(fig, 3, df_comb, profit_col, f"Profit {pair}") + + store_plot_file(fig, filename='freqtrade-profit-plot.html', auto_open=True) + + def generate_plot_filename(pair, ticker_interval) -> str: """ Generate filenames per pair/ticker_interval to be used for storing plots diff --git a/scripts/plot_profit.py b/scripts/plot_profit.py index ad135b5e6..f1cf99828 100755 --- a/scripts/plot_profit.py +++ b/scripts/plot_profit.py @@ -8,13 +8,9 @@ import logging import sys from typing import Any, Dict, List -import plotly.graph_objs as go -from plotly import tools - from freqtrade.arguments import ARGS_PLOT_PROFIT, Arguments -from freqtrade.data.btanalysis import create_cum_profit, combine_tickers_with_mean from freqtrade.optimize import setup_configuration -from freqtrade.plot.plotting import FTPlots, store_plot_file, add_profit +from freqtrade.plot.plotting import FTPlots, generate_profit_graph from freqtrade.state import RunMode logger = logging.getLogger(__name__) @@ -33,32 +29,7 @@ def plot_profit(config: Dict[str, Any]) -> None: # Create an average close price of all the pairs that were involved. # this could be useful to gauge the overall market trend - - # Combine close-values for all pairs, rename columns to "pair" - df_comb = combine_tickers_with_mean(plot.tickers, "close") - - # Add combined cumulative profit - df_comb = create_cum_profit(df_comb, trades, 'cum_profit') - - # Plot the pairs average close prices, and total profit growth - avgclose = go.Scattergl( - x=df_comb.index, - y=df_comb['mean'], - name='Avg close price', - ) - - fig = tools.make_subplots(rows=3, cols=1, shared_xaxes=True, row_width=[1, 1, 1]) - - fig.append_trace(avgclose, 1, 1) - fig = add_profit(fig, 2, df_comb, 'cum_profit', 'Profit') - - for pair in plot.pairs: - profit_col = f'cum_profit_{pair}' - df_comb = create_cum_profit(df_comb, trades[trades['pair'] == pair], profit_col) - - fig = add_profit(fig, 3, df_comb, profit_col, f"Profit {pair}") - - store_plot_file(fig, filename='freqtrade-profit-plot.html', auto_open=True) + generate_profit_graph(plot.pairs, plot.tickers, trades) def plot_parse_args(args: List[str]) -> Dict[str, Any]: