simplify load_trades call

This commit is contained in:
Matthias 2019-08-22 20:17:36 +02:00
parent 1b374fcf7e
commit 545e5c5bc6
3 changed files with 16 additions and 10 deletions

View File

@ -112,16 +112,16 @@ def load_trades_from_db(db_url: str) -> pd.DataFrame:
return trades return trades
def load_trades(config) -> pd.DataFrame: def load_trades(source: str, db_url: str, exportfilename: str) -> 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 config["trade_source"] == "DB": if source == "DB":
return load_trades_from_db(config["db_url"]) return load_trades_from_db(db_url)
elif config["trade_source"] == "file": elif source == "file":
return load_backtest_data(Path(config["exportfilename"])) return load_backtest_data(Path(exportfilename))
def extract_trades_of_period(dataframe: pd.DataFrame, trades: pd.DataFrame) -> pd.DataFrame: def extract_trades_of_period(dataframe: pd.DataFrame, trades: pd.DataFrame) -> pd.DataFrame:

View File

@ -45,7 +45,10 @@ def init_plotscript(config):
timerange=timerange, timerange=timerange,
) )
trades = load_trades(config) trades = load_trades(config['trade_source'],
db_url=config.get('db_url'),
exportfilename=config.get('exportfilename'),
)
return {"tickers": tickers, return {"tickers": tickers,
"trades": trades, "trades": trades,
"pairs": pairs, "pairs": pairs,

View File

@ -89,17 +89,20 @@ def test_load_trades(default_conf, mocker):
db_mock = mocker.patch("freqtrade.data.btanalysis.load_trades_from_db", MagicMock()) db_mock = mocker.patch("freqtrade.data.btanalysis.load_trades_from_db", MagicMock())
bt_mock = mocker.patch("freqtrade.data.btanalysis.load_backtest_data", MagicMock()) bt_mock = mocker.patch("freqtrade.data.btanalysis.load_backtest_data", MagicMock())
default_conf['trade_source'] = "DB" load_trades("DB",
load_trades(default_conf) db_url=default_conf.get('db_url'),
exportfilename=default_conf.get('exportfilename'),
)
assert db_mock.call_count == 1 assert db_mock.call_count == 1
assert bt_mock.call_count == 0 assert bt_mock.call_count == 0
db_mock.reset_mock() db_mock.reset_mock()
bt_mock.reset_mock() bt_mock.reset_mock()
default_conf['trade_source'] = "file"
default_conf['exportfilename'] = "testfile.json" default_conf['exportfilename'] = "testfile.json"
load_trades(default_conf) load_trades("file",
db_url=default_conf.get('db_url'),
exportfilename=default_conf.get('exportfilename'),)
assert db_mock.call_count == 0 assert db_mock.call_count == 0
assert bt_mock.call_count == 1 assert bt_mock.call_count == 1