Fix sequence of loading trades

This commit is contained in:
Matthias 2019-06-22 15:45:20 +02:00
parent d8286d7a98
commit de38aea164
4 changed files with 30 additions and 18 deletions

View File

@ -73,37 +73,45 @@ def evaluate_result_multi(results: pd.DataFrame, freq: str, max_open_trades: int
return df_final[df_final['pair'] > max_open_trades]
def load_trades(db_url: str = None, exportfilename: str = None) -> pd.DataFrame:
def load_trades_from_db(db_url: str) -> pd.DataFrame:
"""
Load trades, either from a DB (using dburl) or via a backtest export file.
Load trades from a DB (using dburl)
: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)
persistence.init(db_url, clean_open_orders=False)
columns = ["pair", "profit", "open_time", "close_time",
"open_rate", "close_rate", "duration"]
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))
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_date.replace(tzinfo=pytz.UTC),
t.close_date.replace(tzinfo=pytz.UTC) 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:
return trades
def load_trades(exportfilename: str = None, db_url: str = None) -> pd.DataFrame:
"""
Load trades, either from a DB (using dburl) or via a backtest export file.
:param exportfilename: Path to a file exported from backtesting
:param db_url: Sqlite url (default format sqlite:///tradesv3.dry-run.sqlite)
:returns: Dataframe containing Trades
"""
trades: pd.DataFrame = pd.DataFrame([], columns=BT_DATA_COLUMNS)
if exportfilename:
trades = load_backtest_data(Path(exportfilename))
elif db_url:
trades = load_trades_from_db(db_url)
return trades

View File

@ -81,6 +81,8 @@ def plot_trades(fig, trades: pd.DataFrame):
)
fig.append_trace(trade_buys, 1, 1)
fig.append_trace(trade_sells, 1, 1)
else:
logger.warning("No trades found.")
return fig

View File

@ -67,11 +67,12 @@ def test_generate_row(default_conf, caplog):
assert log_has_re(r'Indicator "no_indicator" ignored\..*', caplog.record_tuples)
def test_plot_trades():
def test_plot_trades(caplog):
fig1 = generage_empty_figure()
# nothing happens when no trades are available
fig = plot_trades(fig1, None)
assert fig == fig1
assert log_has("No trades found.", caplog.record_tuples)
pair = "ADA/BTC"
filename = history.make_testdata_path(None) / "backtest-result_test.json"
trades = load_backtest_data(filename)

View File

@ -125,8 +125,9 @@ def analyse_and_plot_pairs(config: Dict[str, Any]):
tickers[pair] = data
dataframe = generate_dataframe(strategy, tickers, pair)
trades = load_trades(db_url=config["db_url"],
exportfilename=config["exportfilename"])
trades = load_trades(exportfilename=config["exportfilename"],
db_url=config["db_url"],
)
trades = trades.loc[trades['pair'] == pair]
trades = extract_trades_of_period(dataframe, trades)