Fix if no file exists

This commit is contained in:
Fredrik81 2020-03-14 22:15:03 +01:00
parent 5d1b1573b7
commit 27faf12fde
5 changed files with 31 additions and 8 deletions

View File

@ -59,7 +59,7 @@ ARGS_DOWNLOAD_DATA = ["pairs", "pairs_file", "days", "download_trades", "exchang
ARGS_PLOT_DATAFRAME = ["pairs", "indicators1", "indicators2", "plot_limit", ARGS_PLOT_DATAFRAME = ["pairs", "indicators1", "indicators2", "plot_limit",
"db_url", "trade_source", "export", "exportfilename", "db_url", "trade_source", "export", "exportfilename",
"timerange", "ticker_interval"] "timerange", "ticker_interval", "skip_trades"]
ARGS_PLOT_PROFIT = ["pairs", "timerange", "export", "exportfilename", "db_url", ARGS_PLOT_PROFIT = ["pairs", "timerange", "export", "exportfilename", "db_url",
"trade_source", "ticker_interval"] "trade_source", "ticker_interval"]

View File

@ -413,6 +413,11 @@ AVAILABLE_CLI_OPTIONS = {
metavar='INT', metavar='INT',
default=750, default=750,
), ),
"skip_trades": Arg(
'--skip-trades',
help='Skip using trades file from backtesting and DB.',
action='store_true',
),
"trade_source": Arg( "trade_source": Arg(
'--trade-source', '--trade-source',
help='Specify the source for trades (Can be DB or file (backtest file)) ' help='Specify the source for trades (Can be DB or file (backtest file)) '

View File

@ -111,7 +111,7 @@ def load_trades_from_db(db_url: str) -> pd.DataFrame:
t.calc_profit(), t.calc_profit_ratio(), t.calc_profit(), t.calc_profit_ratio(),
t.open_rate, t.close_rate, t.amount, t.open_rate, t.close_rate, t.amount,
(round((t.close_date.timestamp() - t.open_date.timestamp()) / 60, 2) (round((t.close_date.timestamp() - t.open_date.timestamp()) / 60, 2)
if t.close_date else None), if t.close_date else None),
t.sell_reason, t.sell_reason,
t.fee_open, t.fee_close, t.fee_open, t.fee_close,
t.open_rate_requested, t.open_rate_requested,
@ -129,12 +129,16 @@ def load_trades_from_db(db_url: str) -> pd.DataFrame:
return trades return trades
def load_trades(source: str, db_url: str, exportfilename: str) -> pd.DataFrame: def load_trades(source: str, db_url: str, exportfilename: str, skip_trades: bool) -> pd.DataFrame:
""" """
Based on configuration option "trade_source": Based on configuration option "trade_source":
* loads data from DB (using `db_url`) * loads data from DB (using `db_url`)
* loads data from backtestfile (using `exportfilename`) * loads data from backtestfile (using `exportfilename`)
""" """
if skip_trades:
df = pd.DataFrame(columns=BT_DATA_COLUMNS)
return df
if source == "DB": if source == "DB":
return load_trades_from_db(db_url) return load_trades_from_db(db_url)
elif source == "file": elif source == "file":

View File

@ -3,6 +3,7 @@ from pathlib import Path
from typing import Any, Dict, List from typing import Any, Dict, List
import pandas as pd import pandas as pd
from os.path import isfile
from freqtrade.configuration import TimeRange from freqtrade.configuration import TimeRange
from freqtrade.data.btanalysis import (calculate_max_drawdown, from freqtrade.data.btanalysis import (calculate_max_drawdown,
@ -48,11 +49,21 @@ def init_plotscript(config):
data_format=config.get('dataformat_ohlcv', 'json'), data_format=config.get('dataformat_ohlcv', 'json'),
) )
trades = load_trades(config['trade_source'], skip_trades = False
db_url=config.get('db_url'), if not isfile(config.get('exportfilename')) and config['trade_source'] == 'file':
exportfilename=config.get('exportfilename'), logger.info("Backtest file is missing skipping trades.")
) skip_trades = True
elif config.get('skip_trades', False):
skip_trades = True
trades = load_trades(
config['trade_source'],
db_url=config.get('db_url'),
exportfilename=config.get('exportfilename'),
skip_trades=skip_trades
)
trades = trim_dataframe(trades, timerange, 'open_time') trades = trim_dataframe(trades, timerange, 'open_time')
return {"ohlcv": data, return {"ohlcv": data,
"trades": trades, "trades": trades,
"pairs": pairs, "pairs": pairs,

View File

@ -104,6 +104,7 @@ def test_load_trades(default_conf, mocker):
load_trades("DB", load_trades("DB",
db_url=default_conf.get('db_url'), db_url=default_conf.get('db_url'),
exportfilename=default_conf.get('exportfilename'), exportfilename=default_conf.get('exportfilename'),
skip_trades=False
) )
assert db_mock.call_count == 1 assert db_mock.call_count == 1
@ -114,7 +115,9 @@ def test_load_trades(default_conf, mocker):
default_conf['exportfilename'] = "testfile.json" default_conf['exportfilename'] = "testfile.json"
load_trades("file", load_trades("file",
db_url=default_conf.get('db_url'), db_url=default_conf.get('db_url'),
exportfilename=default_conf.get('exportfilename'),) exportfilename=default_conf.get('exportfilename'),
skip_trades=False
)
assert db_mock.call_count == 0 assert db_mock.call_count == 0
assert bt_mock.call_count == 1 assert bt_mock.call_count == 1