From e0a1e5417fbb8e36bc947e29398f05754b565959 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 28 May 2019 20:23:16 +0200 Subject: [PATCH] sanity checks before plotting, cleanup --- freqtrade/plot/plotting.py | 63 ++++++++++++++++++++------------------ scripts/plot_dataframe.py | 29 +++++++++++------- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/freqtrade/plot/plotting.py b/freqtrade/plot/plotting.py index 7e815bdd7..a18b7bf70 100644 --- a/freqtrade/plot/plotting.py +++ b/freqtrade/plot/plotting.py @@ -25,8 +25,7 @@ def generate_row(fig, row, indicators: List[str], data: pd.DataFrame) -> tools.m """ for indicator in indicators: if indicator in data: - # TODO: Replace all Scatter with Scattergl for performance!! - scattergl = go.Scatter( + scattergl = go.Scattergl( x=data['date'], y=data[indicator], mode='lines', @@ -48,7 +47,7 @@ def plot_trades(fig, trades: pd.DataFrame): Plot trades to "fig" """ # Trades can be empty - if trades is not None: + if trades is not None and len(trades) > 0: trade_buys = go.Scatter( x=trades["open_time"], y=trades["open_rate"], @@ -123,44 +122,50 @@ def generate_graph( if 'buy' in data.columns: df_buy = data[data['buy'] == 1] - buys = go.Scatter( - x=df_buy.date, - y=df_buy.close, - mode='markers', - name='buy', - marker=dict( - symbol='triangle-up-dot', - size=9, - line=dict(width=1), - color='green', + if len(df_buy) > 0: + buys = go.Scattergl( + x=df_buy.date, + y=df_buy.close, + mode='markers', + name='buy', + marker=dict( + symbol='triangle-up-dot', + size=9, + line=dict(width=1), + color='green', + ) ) - ) - fig.append_trace(buys, 1, 1) + fig.append_trace(buys, 1, 1) + else: + logger.warning("No buy-signals found.") if 'sell' in data.columns: df_sell = data[data['sell'] == 1] - sells = go.Scatter( - x=df_sell.date, - y=df_sell.close, - mode='markers', - name='sell', - marker=dict( - symbol='triangle-down-dot', - size=9, - line=dict(width=1), - color='red', + if len(df_sell) > 0: + sells = go.Scattergl( + x=df_sell.date, + y=df_sell.close, + mode='markers', + name='sell', + marker=dict( + symbol='triangle-down-dot', + size=9, + line=dict(width=1), + color='red', + ) ) - ) - fig.append_trace(sells, 1, 1) + fig.append_trace(sells, 1, 1) + else: + logger.warning("No sell-signals found.") if 'bb_lowerband' in data and 'bb_upperband' in data: - bb_lower = go.Scatter( + bb_lower = go.Scattergl( x=data.date, y=data.bb_lowerband, name='BB lower', line={'color': 'rgba(255,255,255,0)'}, ) - bb_upper = go.Scatter( + bb_upper = go.Scattergl( x=data.date, y=data.bb_upperband, name='BB upper', diff --git a/scripts/plot_dataframe.py b/scripts/plot_dataframe.py index 1d3b24449..84e18e5cd 100755 --- a/scripts/plot_dataframe.py +++ b/scripts/plot_dataframe.py @@ -51,11 +51,18 @@ _CONF: Dict[str, Any] = {} timeZone = pytz.UTC -def load_trades(args: Namespace, pair: str) -> pd.DataFrame: - trades: pd.DataFrame = pd.DataFrame() - if args.db_url: - persistence.init(args.db_url, clean_open_orders=False) +def load_trades(db_url: str = None, exportfilename: str = None) -> pd.DataFrame: + """ + Load trades, either from a DB (using dburl) or via a backtest export file. + :param db_url: Sqlite url (default format sqlite:///tradesv3.dry-run.sqlite) + :param exportfilename: Path to a file exported from backtesting + :returns: Dataframe containing Trades + """ + # TODO: Document and move to btanalysis + trades: pd.DataFrame = pd.DataFrame([], columns=BT_DATA_COLUMNS) + if db_url: + persistence.init(db_url, clean_open_orders=False) columns = ["pair", "profit", "open_time", "close_time", "open_rate", "close_rate", "duration"] @@ -68,18 +75,15 @@ def load_trades(args: Namespace, pair: str) -> pd.DataFrame: t.open_rate, t.close_rate, t.close_date.timestamp() - t.open_date.timestamp() if t.close_date else None) - for t in Trade.query.filter(Trade.pair.is_(pair)).all()], + for t in Trade.query.all()], columns=columns) - elif args.exportfilename: + elif exportfilename: - file = Path(args.exportfilename) + file = Path(exportfilename) if file.exists(): trades = load_backtest_data(file) - else: - trades = pd.DataFrame([], columns=BT_DATA_COLUMNS) - return trades @@ -181,6 +185,7 @@ def extract_trades_of_period(dataframe, trades) -> pd.DataFrame: Compare trades and backtested pair DataFrames to get trades performed on backtested period :return: the DataFrame of a trades of period """ + # TODO: Document and move to btanalysis (?) trades = trades.loc[(trades['open_time'] >= dataframe.iloc[0]['date']) & (trades['close_time'] <= dataframe.iloc[-1]['date'])] return trades @@ -211,7 +216,9 @@ def analyse_and_plot_pairs(args: Namespace): tickers[pair] = data dataframe = generate_dataframe(strategy, tickers, pair) - trades = load_trades(args, pair) + trades = load_trades(pair, db_url=args.db_url, + exportfilename=args.exportfilename) + trades = trades.loc[trades['pair'] == pair] trades = extract_trades_of_period(dataframe, trades) fig = generate_graph(