From c7643e142b7e6ba0c7e9cbc422409caa38608952 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 16 Jun 2019 10:41:05 +0200 Subject: [PATCH] Move load_trades to bt_anlaysis --- freqtrade/data/btanalysis.py | 43 ++++++++++++++++++++++++++++++++++++ scripts/plot_dataframe.py | 43 +----------------------------------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/freqtrade/data/btanalysis.py b/freqtrade/data/btanalysis.py index 6fce4361b..fb120e521 100644 --- a/freqtrade/data/btanalysis.py +++ b/freqtrade/data/btanalysis.py @@ -1,12 +1,18 @@ """ Helpers when analyzing backtest data """ +import logging from pathlib import Path import numpy as np import pandas as pd +import pytz +from freqtrade import persistence from freqtrade.misc import json_load +from freqtrade.persistence import Trade + +logger = logging.getLogger(__name__) # must align with columns in backtest.py BT_DATA_COLUMNS = ["pair", "profitperc", "open_time", "close_time", "index", "duration", @@ -65,3 +71,40 @@ def evaluate_result_multi(results: pd.DataFrame, freq: str, max_open_trades: int df2 = df2.set_index('date') df_final = df2.resample(freq)[['pair']].count() return df_final[df_final['pair'] > max_open_trades] + + +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 + """ + timeZone = pytz.UTC + + 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"] + + for x in Trade.query.all(): + logger.info("date: {}".format(x.open_date)) + + trades = pd.DataFrame([(t.pair, t.calc_profit(), + t.open_date.replace(tzinfo=timeZone), + t.close_date.replace(tzinfo=timeZone) if t.close_date else None, + 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.all()], + columns=columns) + + elif exportfilename: + + file = Path(exportfilename) + if file.exists(): + trades = load_backtest_data(file) + + return trades diff --git a/scripts/plot_dataframe.py b/scripts/plot_dataframe.py index 8bed81985..005148d3b 100755 --- a/scripts/plot_dataframe.py +++ b/scripts/plot_dataframe.py @@ -31,60 +31,19 @@ from pathlib import Path from typing import Any, Dict, List import pandas as pd -import pytz -from freqtrade import persistence from freqtrade.arguments import Arguments, TimeRange from freqtrade.data import history -from freqtrade.data.btanalysis import BT_DATA_COLUMNS, load_backtest_data +from freqtrade.data.btanalysis import load_trades from freqtrade.plot.plotting import generate_graph, generate_plot_file from freqtrade.exchange import Exchange from freqtrade.optimize import setup_configuration -from freqtrade.persistence import Trade from freqtrade.resolvers import StrategyResolver from freqtrade.state import RunMode logger = logging.getLogger(__name__) _CONF: Dict[str, Any] = {} -timeZone = pytz.UTC - - -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"] - - for x in Trade.query.all(): - logger.info("date: {}".format(x.open_date)) - - trades = pd.DataFrame([(t.pair, t.calc_profit(), - t.open_date.replace(tzinfo=timeZone), - t.close_date.replace(tzinfo=timeZone) if t.close_date else None, - 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.all()], - columns=columns) - - elif exportfilename: - - file = Path(exportfilename) - if file.exists(): - trades = load_backtest_data(file) - - return trades - def get_trading_env(args: Namespace): """