Add load_trades abstraction (to load trades from either DB or file)

This commit is contained in:
Matthias 2019-06-29 20:50:31 +02:00
parent 4218d569de
commit 8aa327cb8a
3 changed files with 39 additions and 8 deletions

View File

@ -101,6 +101,20 @@ def load_trades_from_db(db_url: str) -> pd.DataFrame:
return trades
def load_trades(config) -> pd.DataFrame:
"""
Based on configuration option "trade_source":
* loads data from DB (using `db_url`)
* loads data from backtestfile (`using exportfilename`)
"""
if config["trade_source"] == "DB":
return load_trades_from_db(config["db_url"])
elif config["trade_source"] == "file":
return load_backtest_data(Path(config["exportfilename"]))
else:
return None
def extract_trades_of_period(dataframe: pd.DataFrame, trades: pd.DataFrame) -> pd.DataFrame:
"""
Compare trades and backtested pair DataFrames to get trades performed on backtested period

View File

@ -4,10 +4,11 @@ import pytest
from arrow import Arrow
from pandas import DataFrame, to_datetime
from freqtrade.arguments import TimeRange, Arguments
from freqtrade.arguments import Arguments, TimeRange
from freqtrade.data.btanalysis import (BT_DATA_COLUMNS, create_cum_profit,
extract_trades_of_period,
load_backtest_data, load_trades_from_db)
load_backtest_data, load_trades,
load_trades_from_db)
from freqtrade.data.history import load_pair_history, make_testdata_path
from freqtrade.tests.test_persistence import create_mock_trades
@ -76,6 +77,26 @@ def test_extract_trades_of_period():
assert trades1.iloc[-1].close_time == Arrow(2017, 11, 14, 15, 25, 0).datetime
def test_load_trades(default_conf, mocker):
db_mock = mocker.patch("freqtrade.data.btanalysis.load_trades_from_db", MagicMock())
bt_mock = mocker.patch("freqtrade.data.btanalysis.load_backtest_data", MagicMock())
default_conf['trade_source'] = "DB"
load_trades(default_conf)
assert db_mock.call_count == 1
assert bt_mock.call_count == 0
db_mock.reset_mock()
bt_mock.reset_mock()
default_conf['trade_source'] = "file"
default_conf['exportfilename'] = "testfile.json"
load_trades(default_conf)
assert db_mock.call_count == 0
assert bt_mock.call_count == 1
def test_create_cum_profit():
filename = make_testdata_path(None) / "backtest-result_test.json"
bt_data = load_backtest_data(filename)

View File

@ -21,8 +21,7 @@ import pandas as pd
from freqtrade.arguments import ARGS_PLOT_DATAFRAME, Arguments
from freqtrade.data import history
from freqtrade.data.btanalysis import (extract_trades_of_period,
load_backtest_data, load_trades_from_db)
from freqtrade.data.btanalysis import extract_trades_of_period, load_trades
from freqtrade.optimize import setup_configuration
from freqtrade.plot.plotting import (generate_candlestick_graph,
generate_plot_file,
@ -80,10 +79,7 @@ def analyse_and_plot_pairs(config: Dict[str, Any]):
live=config.get("live", False),
)
if config["trade_source"] == "DB":
trades = load_trades_from_db(config["db_url"])
elif config["trade_source"] == "file":
trades = load_backtest_data(Path(config["exportfilename"]))
trades = load_trades(config)
pair_counter = 0
for pair, data in tickers.items():