Merge pull request #2234 from freqtrade/test_datadir_no_default

datadir should not default to freqtrade/tests/testdata
This commit is contained in:
hroff-1902 2019-09-08 04:06:12 +03:00 committed by GitHub
commit 623e8f6984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 143 additions and 149 deletions

View File

@ -66,8 +66,7 @@ class DataProvider():
return load_pair_history(pair=pair,
ticker_interval=ticker_interval or self._config['ticker_interval'],
refresh_pairs=False,
datadir=Path(self._config['datadir']) if self._config.get(
'datadir') else None
datadir=Path(self._config['datadir'])
)
def get_pair_dataframe(self, pair: str, ticker_interval: str = None) -> DataFrame:

View File

@ -57,7 +57,7 @@ def trim_tickerlist(tickerlist: List[Dict], timerange: TimeRange) -> List[Dict]:
return tickerlist[start_index:stop_index]
def load_tickerdata_file(datadir: Optional[Path], pair: str, ticker_interval: str,
def load_tickerdata_file(datadir: Path, pair: str, ticker_interval: str,
timerange: Optional[TimeRange] = None) -> Optional[list]:
"""
Load a pair from file, either .json.gz or .json
@ -73,7 +73,7 @@ def load_tickerdata_file(datadir: Optional[Path], pair: str, ticker_interval: st
return pairdata
def store_tickerdata_file(datadir: Optional[Path], pair: str,
def store_tickerdata_file(datadir: Path, pair: str,
ticker_interval: str, data: list, is_zip: bool = False):
"""
Stores tickerdata to file
@ -84,7 +84,7 @@ def store_tickerdata_file(datadir: Optional[Path], pair: str,
def load_pair_history(pair: str,
ticker_interval: str,
datadir: Optional[Path],
datadir: Path,
timerange: TimeRange = TimeRange(None, None, 0, 0),
refresh_pairs: bool = False,
exchange: Optional[Exchange] = None,
@ -135,7 +135,7 @@ def load_pair_history(pair: str,
return None
def load_data(datadir: Optional[Path],
def load_data(datadir: Path,
ticker_interval: str,
pairs: List[str],
refresh_pairs: bool = False,
@ -172,19 +172,13 @@ def load_data(datadir: Optional[Path],
return result
def make_testdata_path(datadir: Optional[Path]) -> Path:
"""Return the path where testdata files are stored"""
return datadir or (Path(__file__).parent.parent / "tests" / "testdata").resolve()
def pair_data_filename(datadir: Optional[Path], pair: str, ticker_interval: str) -> Path:
path = make_testdata_path(datadir)
def pair_data_filename(datadir: Path, pair: str, ticker_interval: str) -> Path:
pair_s = pair.replace("/", "_")
filename = path.joinpath(f'{pair_s}-{ticker_interval}.json')
filename = datadir.joinpath(f'{pair_s}-{ticker_interval}.json')
return filename
def load_cached_data_for_updating(datadir: Optional[Path], pair: str, ticker_interval: str,
def load_cached_data_for_updating(datadir: Path, pair: str, ticker_interval: str,
timerange: Optional[TimeRange]) -> Tuple[List[Any],
Optional[int]]:
"""
@ -224,7 +218,7 @@ def load_cached_data_for_updating(datadir: Optional[Path], pair: str, ticker_int
return (data, since_ms)
def download_pair_history(datadir: Optional[Path],
def download_pair_history(datadir: Path,
exchange: Optional[Exchange],
pair: str,
ticker_interval: str = '5m',

View File

@ -93,7 +93,7 @@ class Edge():
logger.info('Using local backtesting data (using whitelist in given config) ...')
data = history.load_data(
datadir=Path(self.config['datadir']) if self.config.get('datadir') else None,
datadir=Path(self.config['datadir']),
pairs=pairs,
ticker_interval=self.strategy.ticker_interval,
refresh_pairs=self._refresh_pairs,

View File

@ -407,7 +407,7 @@ class Backtesting(object):
timerange = TimeRange.parse_timerange(None if self.config.get(
'timerange') is None else str(self.config.get('timerange')))
data = history.load_data(
datadir=Path(self.config['datadir']) if self.config.get('datadir') else None,
datadir=Path(self.config['datadir']),
pairs=pairs,
ticker_interval=self.ticker_interval,
refresh_pairs=self.config.get('refresh_pairs', False),

View File

@ -349,7 +349,7 @@ class Hyperopt:
timerange = TimeRange.parse_timerange(None if self.config.get(
'timerange') is None else str(self.config.get('timerange')))
data = load_data(
datadir=Path(self.config['datadir']) if self.config.get('datadir') else None,
datadir=Path(self.config['datadir']),
pairs=self.config['exchange']['pair_whitelist'],
ticker_interval=self.backtesting.ticker_interval,
refresh_pairs=self.config.get('refresh_pairs', False),

View File

@ -182,7 +182,7 @@ def init_persistence(default_conf):
@pytest.fixture(scope="function")
def default_conf():
def default_conf(testdatadir):
""" Returns validated configuration suitable for most tests """
configuration = {
"max_open_trades": 1,
@ -237,6 +237,7 @@ def default_conf():
"token": "token",
"chat_id": "0"
},
"datadir": str(testdatadir),
"initial_state": "running",
"db_url": "sqlite://",
"user_data_dir": Path("user_data"),
@ -1047,3 +1048,9 @@ def rpc_balance():
'used': 0.0
},
}
@pytest.fixture
def testdatadir() -> Path:
"""Return the path where testdata files are stored"""
return (Path(__file__).parent / "testdata").resolve()

View File

@ -11,14 +11,13 @@ from freqtrade.data.btanalysis import (BT_DATA_COLUMNS,
extract_trades_of_period,
load_backtest_data, load_trades,
load_trades_from_db)
from freqtrade.data.history import (load_data, load_pair_history,
make_testdata_path)
from freqtrade.data.history import load_data, load_pair_history
from freqtrade.tests.test_persistence import create_mock_trades
def test_load_backtest_data():
def test_load_backtest_data(testdatadir):
filename = make_testdata_path(None) / "backtest-result_test.json"
filename = testdatadir / "backtest-result_test.json"
bt_data = load_backtest_data(filename)
assert isinstance(bt_data, DataFrame)
assert list(bt_data.columns) == BT_DATA_COLUMNS + ["profitabs"]
@ -52,12 +51,12 @@ def test_load_trades_db(default_conf, fee, mocker):
assert col in trades.columns
def test_extract_trades_of_period():
def test_extract_trades_of_period(testdatadir):
pair = "UNITTEST/BTC"
timerange = TimeRange(None, 'line', 0, -1000)
data = load_pair_history(pair=pair, ticker_interval='1m',
datadir=None, timerange=timerange)
datadir=testdatadir, timerange=timerange)
# timerange = 2017-11-14 06:07 - 2017-11-14 22:58:00
trades = DataFrame(
@ -108,9 +107,9 @@ def test_load_trades(default_conf, mocker):
assert bt_mock.call_count == 1
def test_combine_tickers_with_mean():
def test_combine_tickers_with_mean(testdatadir):
pairs = ["ETH/BTC", "XLM/BTC"]
tickers = load_data(datadir=None,
tickers = load_data(datadir=testdatadir,
pairs=pairs,
ticker_interval='5m'
)
@ -121,13 +120,13 @@ def test_combine_tickers_with_mean():
assert "mean" in df.columns
def test_create_cum_profit():
filename = make_testdata_path(None) / "backtest-result_test.json"
def test_create_cum_profit(testdatadir):
filename = testdatadir / "backtest-result_test.json"
bt_data = load_backtest_data(filename)
timerange = TimeRange.parse_timerange("20180110-20180112")
df = load_pair_history(pair="POWR/BTC", ticker_interval='5m',
datadir=None, timerange=timerange)
datadir=testdatadir, timerange=timerange)
cum_profits = create_cum_profit(df.set_index('date'),
bt_data[bt_data["pair"] == 'POWR/BTC'],

View File

@ -21,8 +21,8 @@ def test_parse_ticker_dataframe(ticker_history_list, caplog):
assert log_has('Parsing tickerlist to dataframe', caplog)
def test_ohlcv_fill_up_missing_data(caplog):
data = load_pair_history(datadir=None,
def test_ohlcv_fill_up_missing_data(testdatadir, caplog):
data = load_pair_history(datadir=testdatadir,
ticker_interval='1m',
refresh_pairs=False,
pair='UNITTEST/BTC',

View File

@ -45,7 +45,6 @@ def test_historic_ohlcv(mocker, default_conf, ticker_history):
data = dp.historic_ohlcv("UNITTEST/BTC", "5m")
assert isinstance(data, DataFrame)
assert historymock.call_count == 1
assert historymock.call_args_list[0][1]["datadir"] is None
assert historymock.call_args_list[0][1]["refresh_pairs"] is False
assert historymock.call_args_list[0][1]["ticker_interval"] == "5m"

View File

@ -16,14 +16,13 @@ from freqtrade.configuration import TimeRange
from freqtrade.data import history
from freqtrade.data.history import (download_pair_history,
load_cached_data_for_updating,
load_tickerdata_file, make_testdata_path,
load_tickerdata_file,
refresh_backtest_ohlcv_data,
trim_tickerlist)
from freqtrade.exchange import timeframe_to_minutes
from freqtrade.misc import file_dump_json
from freqtrade.strategy.default_strategy import DefaultStrategy
from freqtrade.tests.conftest import (get_patched_exchange, log_has,
patch_exchange)
from freqtrade.tests.conftest import get_patched_exchange, log_has, log_has_re, patch_exchange
# Change this if modifying UNITTEST/BTC testdatafile
_BTC_UNITTEST_LENGTH = 13681
@ -60,8 +59,8 @@ def _clean_test_file(file: str) -> None:
os.rename(file_swp, file)
def test_load_data_30min_ticker(mocker, caplog, default_conf) -> None:
ld = history.load_pair_history(pair='UNITTEST/BTC', ticker_interval='30m', datadir=None)
def test_load_data_30min_ticker(mocker, caplog, default_conf, testdatadir) -> None:
ld = history.load_pair_history(pair='UNITTEST/BTC', ticker_interval='30m', datadir=testdatadir)
assert isinstance(ld, DataFrame)
assert not log_has(
'Download history data for pair: "UNITTEST/BTC", interval: 30m '
@ -69,8 +68,8 @@ def test_load_data_30min_ticker(mocker, caplog, default_conf) -> None:
)
def test_load_data_7min_ticker(mocker, caplog, default_conf) -> None:
ld = history.load_pair_history(pair='UNITTEST/BTC', ticker_interval='7m', datadir=None)
def test_load_data_7min_ticker(mocker, caplog, default_conf, testdatadir) -> None:
ld = history.load_pair_history(pair='UNITTEST/BTC', ticker_interval='7m', datadir=testdatadir)
assert not isinstance(ld, DataFrame)
assert ld is None
assert log_has(
@ -80,11 +79,11 @@ def test_load_data_7min_ticker(mocker, caplog, default_conf) -> None:
)
def test_load_data_1min_ticker(ticker_history, mocker, caplog) -> None:
def test_load_data_1min_ticker(ticker_history, mocker, caplog, testdatadir) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ticker_history)
file = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'UNITTEST_BTC-1m.json')
_backup_file(file, copy_file=True)
history.load_data(datadir=None, ticker_interval='1m', pairs=['UNITTEST/BTC'])
history.load_data(datadir=testdatadir, ticker_interval='1m', pairs=['UNITTEST/BTC'])
assert os.path.isfile(file) is True
assert not log_has(
'Download history data for pair: "UNITTEST/BTC", interval: 1m '
@ -93,7 +92,8 @@ def test_load_data_1min_ticker(ticker_history, mocker, caplog) -> None:
_clean_test_file(file)
def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, default_conf) -> None:
def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog,
default_conf, testdatadir) -> None:
"""
Test load_pair_history() with 1 min ticker
"""
@ -103,7 +103,7 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, defau
_backup_file(file)
# do not download a new pair if refresh_pairs isn't set
history.load_pair_history(datadir=None,
history.load_pair_history(datadir=testdatadir,
ticker_interval='1m',
refresh_pairs=False,
pair='MEME/BTC')
@ -115,18 +115,18 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, defau
)
# download a new pair if refresh_pairs is set
history.load_pair_history(datadir=None,
history.load_pair_history(datadir=testdatadir,
ticker_interval='1m',
refresh_pairs=True,
exchange=exchange,
pair='MEME/BTC')
assert os.path.isfile(file) is True
assert log_has(
assert log_has_re(
'Download history data for pair: "MEME/BTC", interval: 1m '
'and store in None.', caplog
'and store in .*', caplog
)
with pytest.raises(OperationalException, match=r'Exchange needs to be initialized when.*'):
history.load_pair_history(datadir=None,
history.load_pair_history(datadir=testdatadir,
ticker_interval='1m',
refresh_pairs=True,
exchange=None,
@ -134,12 +134,12 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, defau
_clean_test_file(file)
def test_load_data_live(default_conf, mocker, caplog) -> None:
def test_load_data_live(default_conf, mocker, caplog, testdatadir) -> None:
refresh_mock = MagicMock()
mocker.patch("freqtrade.exchange.Exchange.refresh_latest_ohlcv", refresh_mock)
exchange = get_patched_exchange(mocker, default_conf)
history.load_data(datadir=None, ticker_interval='5m',
history.load_data(datadir=testdatadir, ticker_interval='5m',
pairs=['UNITTEST/BTC', 'UNITTEST2/BTC'],
live=True,
exchange=exchange)
@ -148,19 +148,19 @@ def test_load_data_live(default_conf, mocker, caplog) -> None:
assert log_has('Live: Downloading data for all defined pairs ...', caplog)
def test_load_data_live_noexchange(default_conf, mocker, caplog) -> None:
def test_load_data_live_noexchange(default_conf, mocker, caplog, testdatadir) -> None:
with pytest.raises(OperationalException,
match=r'Exchange needs to be initialized when using live data.'):
history.load_data(datadir=None, ticker_interval='5m',
history.load_data(datadir=testdatadir, ticker_interval='5m',
pairs=['UNITTEST/BTC', 'UNITTEST2/BTC'],
exchange=None,
live=True,
)
def test_testdata_path() -> None:
assert str(Path('freqtrade') / 'tests' / 'testdata') in str(make_testdata_path(None))
def test_testdata_path(testdatadir) -> None:
assert str(Path('freqtrade') / 'tests' / 'testdata') in str(testdatadir)
def test_load_cached_data_for_updating(mocker) -> None:
@ -248,7 +248,7 @@ def test_load_cached_data_for_updating(mocker) -> None:
assert start_ts is None
def test_download_pair_history(ticker_history_list, mocker, default_conf) -> None:
def test_download_pair_history(ticker_history_list, mocker, default_conf, testdatadir) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ticker_history_list)
exchange = get_patched_exchange(mocker, default_conf)
file1_1 = os.path.join(os.path.dirname(__file__), '..', 'testdata', 'MEME_BTC-1m.json')
@ -264,10 +264,10 @@ def test_download_pair_history(ticker_history_list, mocker, default_conf) -> Non
assert os.path.isfile(file1_1) is False
assert os.path.isfile(file2_1) is False
assert download_pair_history(datadir=None, exchange=exchange,
assert download_pair_history(datadir=testdatadir, exchange=exchange,
pair='MEME/BTC',
ticker_interval='1m')
assert download_pair_history(datadir=None, exchange=exchange,
assert download_pair_history(datadir=testdatadir, exchange=exchange,
pair='CFI/BTC',
ticker_interval='1m')
assert not exchange._pairs_last_refresh_time
@ -281,10 +281,10 @@ def test_download_pair_history(ticker_history_list, mocker, default_conf) -> Non
assert os.path.isfile(file1_5) is False
assert os.path.isfile(file2_5) is False
assert download_pair_history(datadir=None, exchange=exchange,
assert download_pair_history(datadir=testdatadir, exchange=exchange,
pair='MEME/BTC',
ticker_interval='5m')
assert download_pair_history(datadir=None, exchange=exchange,
assert download_pair_history(datadir=testdatadir, exchange=exchange,
pair='CFI/BTC',
ticker_interval='5m')
assert not exchange._pairs_last_refresh_time
@ -296,7 +296,7 @@ def test_download_pair_history(ticker_history_list, mocker, default_conf) -> Non
_clean_test_file(file2_5)
def test_download_pair_history2(mocker, default_conf) -> None:
def test_download_pair_history2(mocker, default_conf, testdatadir) -> None:
tick = [
[1509836520000, 0.00162008, 0.00162008, 0.00162008, 0.00162008, 108.14853839],
[1509836580000, 0.00161, 0.00161, 0.00161, 0.00161, 82.390199]
@ -304,12 +304,13 @@ def test_download_pair_history2(mocker, default_conf) -> None:
json_dump_mock = mocker.patch('freqtrade.misc.file_dump_json', return_value=None)
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=tick)
exchange = get_patched_exchange(mocker, default_conf)
download_pair_history(None, exchange, pair="UNITTEST/BTC", ticker_interval='1m')
download_pair_history(None, exchange, pair="UNITTEST/BTC", ticker_interval='3m')
download_pair_history(testdatadir, exchange, pair="UNITTEST/BTC", ticker_interval='1m')
download_pair_history(testdatadir, exchange, pair="UNITTEST/BTC", ticker_interval='3m')
assert json_dump_mock.call_count == 2
def test_download_backtesting_data_exception(ticker_history, mocker, caplog, default_conf) -> None:
def test_download_backtesting_data_exception(ticker_history, mocker, caplog,
default_conf, testdatadir) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv',
side_effect=Exception('File Error'))
@ -320,7 +321,7 @@ def test_download_backtesting_data_exception(ticker_history, mocker, caplog, def
_backup_file(file1_1)
_backup_file(file1_5)
assert not download_pair_history(datadir=None, exchange=exchange,
assert not download_pair_history(datadir=testdatadir, exchange=exchange,
pair='MEME/BTC',
ticker_interval='1m')
# clean files freshly downloaded
@ -332,22 +333,22 @@ def test_download_backtesting_data_exception(ticker_history, mocker, caplog, def
)
def test_load_tickerdata_file() -> None:
def test_load_tickerdata_file(testdatadir) -> None:
# 7 does not exist in either format.
assert not load_tickerdata_file(None, 'UNITTEST/BTC', '7m')
assert not load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '7m')
# 1 exists only as a .json
tickerdata = load_tickerdata_file(None, 'UNITTEST/BTC', '1m')
tickerdata = load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '1m')
assert _BTC_UNITTEST_LENGTH == len(tickerdata)
# 8 .json is empty and will fail if it's loaded. .json.gz is a copy of 1.json
tickerdata = load_tickerdata_file(None, 'UNITTEST/BTC', '8m')
tickerdata = load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '8m')
assert _BTC_UNITTEST_LENGTH == len(tickerdata)
def test_load_partial_missing(caplog) -> None:
def test_load_partial_missing(testdatadir, caplog) -> None:
# Make sure we start fresh - test missing data at start
start = arrow.get('2018-01-01T00:00:00')
end = arrow.get('2018-01-11T00:00:00')
tickerdata = history.load_data(None, '5m', ['UNITTEST/BTC'],
tickerdata = history.load_data(testdatadir, '5m', ['UNITTEST/BTC'],
refresh_pairs=False,
timerange=TimeRange('date', 'date',
start.timestamp, end.timestamp))
@ -362,7 +363,7 @@ def test_load_partial_missing(caplog) -> None:
caplog.clear()
start = arrow.get('2018-01-10T00:00:00')
end = arrow.get('2018-02-20T00:00:00')
tickerdata = history.load_data(datadir=None, ticker_interval='5m',
tickerdata = history.load_data(datadir=testdatadir, ticker_interval='5m',
pairs=['UNITTEST/BTC'], refresh_pairs=False,
timerange=TimeRange('date', 'date',
start.timestamp, end.timestamp))
@ -502,13 +503,13 @@ def test_file_dump_json_tofile() -> None:
_clean_test_file(file)
def test_get_timeframe(default_conf, mocker) -> None:
def test_get_timeframe(default_conf, mocker, testdatadir) -> None:
patch_exchange(mocker)
strategy = DefaultStrategy(default_conf)
data = strategy.tickerdata_to_dataframe(
history.load_data(
datadir=None,
datadir=testdatadir,
ticker_interval='1m',
pairs=['UNITTEST/BTC']
)
@ -518,13 +519,13 @@ def test_get_timeframe(default_conf, mocker) -> None:
assert max_date.isoformat() == '2017-11-14T22:58:00+00:00'
def test_validate_backtest_data_warn(default_conf, mocker, caplog) -> None:
def test_validate_backtest_data_warn(default_conf, mocker, caplog, testdatadir) -> None:
patch_exchange(mocker)
strategy = DefaultStrategy(default_conf)
data = strategy.tickerdata_to_dataframe(
history.load_data(
datadir=None,
datadir=testdatadir,
ticker_interval='1m',
pairs=['UNITTEST/BTC'],
fill_up_missing=False
@ -540,14 +541,14 @@ def test_validate_backtest_data_warn(default_conf, mocker, caplog) -> None:
caplog)
def test_validate_backtest_data(default_conf, mocker, caplog) -> None:
def test_validate_backtest_data(default_conf, mocker, caplog, testdatadir) -> None:
patch_exchange(mocker)
strategy = DefaultStrategy(default_conf)
timerange = TimeRange('index', 'index', 200, 250)
data = strategy.tickerdata_to_dataframe(
history.load_data(
datadir=None,
datadir=testdatadir,
ticker_interval='5m',
pairs=['UNITTEST/BTC'],
timerange=timerange
@ -561,7 +562,7 @@ def test_validate_backtest_data(default_conf, mocker, caplog) -> None:
assert len(caplog.record_tuples) == 0
def test_refresh_backtest_ohlcv_data(mocker, default_conf, markets, caplog):
def test_refresh_backtest_ohlcv_data(mocker, default_conf, markets, caplog, testdatadir):
dl_mock = mocker.patch('freqtrade.data.history.download_pair_history', MagicMock())
mocker.patch(
'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets)
@ -572,7 +573,7 @@ def test_refresh_backtest_ohlcv_data(mocker, default_conf, markets, caplog):
ex = get_patched_exchange(mocker, default_conf)
timerange = TimeRange.parse_timerange("20190101-20190102")
refresh_backtest_ohlcv_data(exchange=ex, pairs=["ETH/BTC", "XRP/BTC"],
timeframes=["1m", "5m"], dl_path=make_testdata_path(None),
timeframes=["1m", "5m"], dl_path=testdatadir,
timerange=timerange, erase=True
)
@ -582,7 +583,7 @@ def test_refresh_backtest_ohlcv_data(mocker, default_conf, markets, caplog):
assert log_has("Downloading pair ETH/BTC, interval 1m.", caplog)
def test_download_data_no_markets(mocker, default_conf, caplog):
def test_download_data_no_markets(mocker, default_conf, caplog, testdatadir):
dl_mock = mocker.patch('freqtrade.data.history.download_pair_history', MagicMock())
mocker.patch(
'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={})
@ -591,7 +592,7 @@ def test_download_data_no_markets(mocker, default_conf, caplog):
timerange = TimeRange.parse_timerange("20190101-20190102")
unav_pairs = refresh_backtest_ohlcv_data(exchange=ex, pairs=["ETH/BTC", "XRP/BTC"],
timeframes=["1m", "5m"],
dl_path=make_testdata_path(None),
dl_path=testdatadir,
timerange=timerange, erase=False
)

View File

@ -291,7 +291,6 @@ def mocked_load_data(datadir, pairs=[], ticker_interval='0m', refresh_pairs=Fals
def test_edge_process_downloaded_data(mocker, edge_conf):
edge_conf['datadir'] = None
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001))
mocker.patch('freqtrade.data.history.load_data', mocked_load_data)
@ -303,7 +302,6 @@ def test_edge_process_downloaded_data(mocker, edge_conf):
def test_edge_process_no_data(mocker, edge_conf, caplog):
edge_conf['datadir'] = None
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001))
mocker.patch('freqtrade.data.history.load_data', MagicMock(return_value={}))
@ -316,7 +314,6 @@ def test_edge_process_no_data(mocker, edge_conf, caplog):
def test_edge_process_no_trades(mocker, edge_conf, caplog):
edge_conf['datadir'] = None
freqtrade = get_patched_freqtradebot(mocker, edge_conf)
mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001))
mocker.patch('freqtrade.data.history.load_data', mocked_load_data)

View File

@ -34,9 +34,9 @@ def trim_dictlist(dict_list, num):
return new
def load_data_test(what):
def load_data_test(what, testdatadir):
timerange = TimeRange(None, 'line', 0, -101)
pair = history.load_tickerdata_file(None, ticker_interval='1m',
pair = history.load_tickerdata_file(testdatadir, ticker_interval='1m',
pair='UNITTEST/BTC', timerange=timerange)
datalen = len(pair)
@ -79,12 +79,12 @@ def load_data_test(what):
fill_missing=True)}
def simple_backtest(config, contour, num_results, mocker) -> None:
def simple_backtest(config, contour, num_results, mocker, testdatadir) -> None:
patch_exchange(mocker)
config['ticker_interval'] = '1m'
backtesting = Backtesting(config)
data = load_data_test(contour)
data = load_data_test(contour, testdatadir)
processed = backtesting.strategy.tickerdata_to_dataframe(data)
min_date, max_date = get_timeframe(processed)
assert isinstance(processed, dict)
@ -118,8 +118,8 @@ def _load_pair_as_ticks(pair, tickfreq):
# FIX: fixturize this?
def _make_backtest_conf(mocker, conf=None, pair='UNITTEST/BTC', record=None):
data = history.load_data(datadir=None, ticker_interval='1m', pairs=[pair])
def _make_backtest_conf(mocker, datadir, conf=None, pair='UNITTEST/BTC', record=None):
data = history.load_data(datadir=datadir, ticker_interval='1m', pairs=[pair])
data = trim_dictlist(data, -201)
patch_exchange(mocker)
backtesting = Backtesting(conf)
@ -339,10 +339,10 @@ def test_backtesting_init_no_ticker_interval(mocker, default_conf, caplog) -> No
"or as cli argument `--ticker-interval 5m`", caplog)
def test_tickerdata_to_dataframe_bt(default_conf, mocker) -> None:
def test_tickerdata_to_dataframe_bt(default_conf, mocker, testdatadir) -> None:
patch_exchange(mocker)
timerange = TimeRange(None, 'line', 0, -100)
tick = history.load_tickerdata_file(None, 'UNITTEST/BTC', '1m', timerange=timerange)
tick = history.load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '1m', timerange=timerange)
tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', pair="UNITTEST/BTC",
fill_missing=True)}
@ -456,7 +456,7 @@ def test_generate_text_table_strategyn(default_conf, mocker):
assert backtesting._generate_text_table_strategy(all_results=results) == result_str
def test_backtesting_start(default_conf, mocker, caplog) -> None:
def test_backtesting_start(default_conf, mocker, testdatadir, caplog) -> None:
def get_timeframe(input1):
return Arrow(2017, 11, 14, 21, 17), Arrow(2017, 11, 14, 22, 59)
@ -472,7 +472,7 @@ def test_backtesting_start(default_conf, mocker, caplog) -> None:
default_conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
default_conf['ticker_interval'] = '1m'
default_conf['datadir'] = None
default_conf['datadir'] = testdatadir
default_conf['export'] = None
default_conf['timerange'] = '-100'
@ -489,7 +489,7 @@ def test_backtesting_start(default_conf, mocker, caplog) -> None:
assert log_has(line, caplog)
def test_backtesting_start_no_data(default_conf, mocker, caplog) -> None:
def test_backtesting_start_no_data(default_conf, mocker, caplog, testdatadir) -> None:
def get_timeframe(input1):
return Arrow(2017, 11, 14, 21, 17), Arrow(2017, 11, 14, 22, 59)
@ -505,7 +505,7 @@ def test_backtesting_start_no_data(default_conf, mocker, caplog) -> None:
default_conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
default_conf['ticker_interval'] = "1m"
default_conf['datadir'] = None
default_conf['datadir'] = testdatadir
default_conf['export'] = None
default_conf['timerange'] = '20180101-20180102'
@ -516,13 +516,13 @@ def test_backtesting_start_no_data(default_conf, mocker, caplog) -> None:
assert log_has('No data found. Terminating.', caplog)
def test_backtest(default_conf, fee, mocker) -> None:
def test_backtest(default_conf, fee, mocker, testdatadir) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
patch_exchange(mocker)
backtesting = Backtesting(default_conf)
pair = 'UNITTEST/BTC'
timerange = TimeRange(None, 'line', 0, -201)
data = history.load_data(datadir=None, ticker_interval='5m', pairs=['UNITTEST/BTC'],
data = history.load_data(datadir=testdatadir, ticker_interval='5m', pairs=['UNITTEST/BTC'],
timerange=timerange)
data_processed = backtesting.strategy.tickerdata_to_dataframe(data)
min_date, max_date = get_timeframe(data_processed)
@ -570,14 +570,14 @@ def test_backtest(default_conf, fee, mocker) -> None:
t["close_rate"], 6) < round(ln.iloc[0]["high"], 6))
def test_backtest_1min_ticker_interval(default_conf, fee, mocker) -> None:
def test_backtest_1min_ticker_interval(default_conf, fee, mocker, testdatadir) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
patch_exchange(mocker)
backtesting = Backtesting(default_conf)
# Run a backtesting for an exiting 1min ticker_interval
timerange = TimeRange(None, 'line', 0, -200)
data = history.load_data(datadir=None, ticker_interval='1m', pairs=['UNITTEST/BTC'],
data = history.load_data(datadir=testdatadir, ticker_interval='1m', pairs=['UNITTEST/BTC'],
timerange=timerange)
processed = backtesting.strategy.tickerdata_to_dataframe(data)
min_date, max_date = get_timeframe(processed)
@ -595,11 +595,11 @@ def test_backtest_1min_ticker_interval(default_conf, fee, mocker) -> None:
assert len(results) == 1
def test_processed(default_conf, mocker) -> None:
def test_processed(default_conf, mocker, testdatadir) -> None:
patch_exchange(mocker)
backtesting = Backtesting(default_conf)
dict_of_tickerrows = load_data_test('raise')
dict_of_tickerrows = load_data_test('raise', testdatadir)
dataframes = backtesting.strategy.tickerdata_to_dataframe(dict_of_tickerrows)
dataframe = dataframes['UNITTEST/BTC']
cols = dataframe.columns
@ -609,7 +609,7 @@ def test_processed(default_conf, mocker) -> None:
assert col in cols
def test_backtest_pricecontours(default_conf, fee, mocker) -> None:
def test_backtest_pricecontours(default_conf, fee, mocker, testdatadir) -> None:
# TODO: Evaluate usefullness of this, the patterns and buy-signls are unrealistic
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
tests = [['raise', 19], ['lower', 0], ['sine', 35]]
@ -617,17 +617,17 @@ def test_backtest_pricecontours(default_conf, fee, mocker) -> None:
default_conf['experimental'] = {"use_sell_signal": True}
for [contour, numres] in tests:
simple_backtest(default_conf, contour, numres, mocker)
simple_backtest(default_conf, contour, numres, mocker, testdatadir)
def test_backtest_clash_buy_sell(mocker, default_conf):
def test_backtest_clash_buy_sell(mocker, default_conf, testdatadir):
# Override the default buy trend function in our default_strategy
def fun(dataframe=None, pair=None):
buy_value = 1
sell_value = 1
return _trend(dataframe, buy_value, sell_value)
backtest_conf = _make_backtest_conf(mocker, conf=default_conf)
backtest_conf = _make_backtest_conf(mocker, conf=default_conf, datadir=testdatadir)
backtesting = Backtesting(default_conf)
backtesting.advise_buy = fun # Override
backtesting.advise_sell = fun # Override
@ -635,14 +635,14 @@ def test_backtest_clash_buy_sell(mocker, default_conf):
assert results.empty
def test_backtest_only_sell(mocker, default_conf):
def test_backtest_only_sell(mocker, default_conf, testdatadir):
# Override the default buy trend function in our default_strategy
def fun(dataframe=None, pair=None):
buy_value = 0
sell_value = 1
return _trend(dataframe, buy_value, sell_value)
backtest_conf = _make_backtest_conf(mocker, conf=default_conf)
backtest_conf = _make_backtest_conf(mocker, conf=default_conf, datadir=testdatadir)
backtesting = Backtesting(default_conf)
backtesting.advise_buy = fun # Override
backtesting.advise_sell = fun # Override
@ -650,10 +650,11 @@ def test_backtest_only_sell(mocker, default_conf):
assert results.empty
def test_backtest_alternate_buy_sell(default_conf, fee, mocker):
def test_backtest_alternate_buy_sell(default_conf, fee, mocker, testdatadir):
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
mocker.patch('freqtrade.optimize.backtesting.file_dump_json', MagicMock())
backtest_conf = _make_backtest_conf(mocker, conf=default_conf, pair='UNITTEST/BTC')
backtest_conf = _make_backtest_conf(mocker, conf=default_conf,
pair='UNITTEST/BTC', datadir=testdatadir)
# We need to enable sell-signal - otherwise it sells on ROI!!
default_conf['experimental'] = {"use_sell_signal": True}
default_conf['ticker_interval'] = '1m'
@ -672,7 +673,7 @@ def test_backtest_alternate_buy_sell(default_conf, fee, mocker):
@pytest.mark.parametrize("pair", ['ADA/BTC', 'LTC/BTC'])
@pytest.mark.parametrize("tres", [0, 20, 30])
def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair):
def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair, testdatadir):
def _trend_alternate_hold(dataframe=None, metadata=None):
"""
@ -690,7 +691,7 @@ def test_backtest_multi_pair(default_conf, fee, mocker, tres, pair):
patch_exchange(mocker)
pairs = ['ADA/BTC', 'DASH/BTC', 'ETH/BTC', 'LTC/BTC', 'NXT/BTC']
data = history.load_data(datadir=None, ticker_interval='5m', pairs=pairs)
data = history.load_data(datadir=testdatadir, ticker_interval='5m', pairs=pairs)
# Only use 500 lines to increase performance
data = trim_dictlist(data, -500)

View File

@ -485,8 +485,8 @@ def test_has_space(hyperopt):
assert hyperopt.has_space('buy')
def test_populate_indicators(hyperopt) -> None:
tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m')
def test_populate_indicators(hyperopt, testdatadir) -> None:
tick = load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '1m')
tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', pair="UNITTEST/BTC",
fill_missing=True)}
dataframes = hyperopt.backtesting.strategy.tickerdata_to_dataframe(tickerlist)
@ -499,8 +499,8 @@ def test_populate_indicators(hyperopt) -> None:
assert 'rsi' in dataframe
def test_buy_strategy_generator(hyperopt) -> None:
tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m')
def test_buy_strategy_generator(hyperopt, testdatadir) -> None:
tick = load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '1m')
tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', pair="UNITTEST/BTC",
fill_missing=True)}
dataframes = hyperopt.backtesting.strategy.tickerdata_to_dataframe(tickerlist)

View File

@ -103,11 +103,11 @@ def test_get_signal_handles_exceptions(mocker, default_conf):
assert _STRATEGY.get_signal(exchange, 'ETH/BTC', '5m') == (False, False)
def test_tickerdata_to_dataframe(default_conf) -> None:
def test_tickerdata_to_dataframe(default_conf, testdatadir) -> None:
strategy = DefaultStrategy(default_conf)
timerange = TimeRange(None, 'line', 0, -100)
tick = load_tickerdata_file(None, 'UNITTEST/BTC', '1m', timerange=timerange)
tick = load_tickerdata_file(testdatadir, 'UNITTEST/BTC', '1m', timerange=timerange)
tickerlist = {'UNITTEST/BTC': parse_ticker_dataframe(tick, '1m', pair="UNITTEST/BTC",
fill_missing=True)}
data = strategy.tickerdata_to_dataframe(tickerlist)

View File

@ -45,16 +45,16 @@ def test_file_dump_json(mocker) -> None:
assert json_dump.call_count == 1
def test_file_load_json(mocker) -> None:
def test_file_load_json(mocker, testdatadir) -> None:
# 7m .json does not exist
ret = file_load_json(pair_data_filename(None, 'UNITTEST/BTC', '7m'))
ret = file_load_json(pair_data_filename(testdatadir, 'UNITTEST/BTC', '7m'))
assert not ret
# 1m json exists (but no .gz exists)
ret = file_load_json(pair_data_filename(None, 'UNITTEST/BTC', '1m'))
ret = file_load_json(pair_data_filename(testdatadir, 'UNITTEST/BTC', '1m'))
assert ret
# 8 .json is empty and will fail if it's loaded. .json.gz is a copy of 1.json
ret = file_load_json(pair_data_filename(None, 'UNITTEST/BTC', '8m'))
ret = file_load_json(pair_data_filename(testdatadir, 'UNITTEST/BTC', '8m'))
assert ret

View File

@ -42,13 +42,12 @@ def generage_empty_figure():
)
def test_init_plotscript(default_conf, mocker):
def test_init_plotscript(default_conf, mocker, testdatadir):
default_conf['timerange'] = "20180110-20180112"
default_conf['trade_source'] = "file"
default_conf['ticker_interval'] = "5m"
default_conf["datadir"] = history.make_testdata_path(None)
default_conf['exportfilename'] = str(
history.make_testdata_path(None) / "backtest-result_test.json")
default_conf["datadir"] = testdatadir
default_conf['exportfilename'] = str(testdatadir / "backtest-result_test.json")
ret = init_plotscript(default_conf)
assert "tickers" in ret
assert "trades" in ret
@ -61,12 +60,12 @@ def test_init_plotscript(default_conf, mocker):
assert "XLM/BTC" in ret["tickers"]
def test_add_indicators(default_conf, caplog):
def test_add_indicators(default_conf, testdatadir, caplog):
pair = "UNITTEST/BTC"
timerange = TimeRange(None, 'line', 0, -1000)
data = history.load_pair_history(pair=pair, ticker_interval='1m',
datadir=None, timerange=timerange)
datadir=testdatadir, timerange=timerange)
indicators1 = ["ema10"]
indicators2 = ["macd"]
@ -94,14 +93,14 @@ def test_add_indicators(default_conf, caplog):
assert log_has_re(r'Indicator "no_indicator" ignored\..*', caplog)
def test_plot_trades(caplog):
def test_plot_trades(testdatadir, 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)
pair = "ADA/BTC"
filename = history.make_testdata_path(None) / "backtest-result_test.json"
filename = testdatadir / "backtest-result_test.json"
trades = load_backtest_data(filename)
trades = trades.loc[trades['pair'] == pair]
@ -122,7 +121,7 @@ def test_plot_trades(caplog):
assert trade_sell.marker.color == 'red'
def test_generate_candlestick_graph_no_signals_no_trades(default_conf, mocker, caplog):
def test_generate_candlestick_graph_no_signals_no_trades(default_conf, mocker, testdatadir, caplog):
row_mock = mocker.patch('freqtrade.plot.plotting.add_indicators',
MagicMock(side_effect=fig_generating_mock))
trades_mock = mocker.patch('freqtrade.plot.plotting.plot_trades',
@ -131,7 +130,7 @@ def test_generate_candlestick_graph_no_signals_no_trades(default_conf, mocker, c
pair = "UNITTEST/BTC"
timerange = TimeRange(None, 'line', 0, -1000)
data = history.load_pair_history(pair=pair, ticker_interval='1m',
datadir=None, timerange=timerange)
datadir=testdatadir, timerange=timerange)
data['buy'] = 0
data['sell'] = 0
@ -158,7 +157,7 @@ def test_generate_candlestick_graph_no_signals_no_trades(default_conf, mocker, c
assert log_has("No sell-signals found.", caplog)
def test_generate_candlestick_graph_no_trades(default_conf, mocker):
def test_generate_candlestick_graph_no_trades(default_conf, mocker, testdatadir):
row_mock = mocker.patch('freqtrade.plot.plotting.add_indicators',
MagicMock(side_effect=fig_generating_mock))
trades_mock = mocker.patch('freqtrade.plot.plotting.plot_trades',
@ -166,7 +165,7 @@ def test_generate_candlestick_graph_no_trades(default_conf, mocker):
pair = 'UNITTEST/BTC'
timerange = TimeRange(None, 'line', 0, -1000)
data = history.load_pair_history(pair=pair, ticker_interval='1m',
datadir=None, timerange=timerange)
datadir=testdatadir, timerange=timerange)
# Generate buy/sell signals and indicators
strat = DefaultStrategy(default_conf)
@ -224,13 +223,13 @@ def test_generate_plot_file(mocker, caplog):
caplog)
def test_add_profit():
filename = history.make_testdata_path(None) / "backtest-result_test.json"
def test_add_profit(testdatadir):
filename = testdatadir / "backtest-result_test.json"
bt_data = load_backtest_data(filename)
timerange = TimeRange.parse_timerange("20180110-20180112")
df = history.load_pair_history(pair="POWR/BTC", ticker_interval='5m',
datadir=None, timerange=timerange)
datadir=testdatadir, timerange=timerange)
fig = generage_empty_figure()
cum_profits = create_cum_profit(df.set_index('date'),
@ -244,13 +243,13 @@ def test_add_profit():
assert profits.yaxis == "y2"
def test_generate_profit_graph():
filename = history.make_testdata_path(None) / "backtest-result_test.json"
def test_generate_profit_graph(testdatadir):
filename = testdatadir / "backtest-result_test.json"
trades = load_backtest_data(filename)
timerange = TimeRange.parse_timerange("20180110-20180112")
pairs = ["POWR/BTC", "XLM/BTC"]
tickers = history.load_data(datadir=None,
tickers = history.load_data(datadir=testdatadir,
pairs=pairs,
ticker_interval='5m',
timerange=timerange
@ -294,11 +293,10 @@ def test_start_plot_dataframe(mocker):
assert called_config['pairs'] == ["ETH/BTC"]
def test_analyse_and_plot_pairs(default_conf, mocker, caplog):
def test_analyse_and_plot_pairs(default_conf, mocker, caplog, testdatadir):
default_conf['trade_source'] = 'file'
default_conf["datadir"] = history.make_testdata_path(None)
default_conf['exportfilename'] = str(
history.make_testdata_path(None) / "backtest-result_test.json")
default_conf["datadir"] = testdatadir
default_conf['exportfilename'] = str(testdatadir / "backtest-result_test.json")
default_conf['indicators1'] = ["sma5", "ema10"]
default_conf['indicators2'] = ["macd"]
default_conf['pairs'] = ["ETH/BTC", "LTC/BTC"]
@ -346,11 +344,10 @@ def test_start_plot_profit_error(mocker):
start_plot_profit(get_args(args))
def test_plot_profit(default_conf, mocker, caplog):
def test_plot_profit(default_conf, mocker, testdatadir, caplog):
default_conf['trade_source'] = 'file'
default_conf["datadir"] = history.make_testdata_path(None)
default_conf['exportfilename'] = str(
history.make_testdata_path(None) / "backtest-result_test.json")
default_conf["datadir"] = testdatadir
default_conf['exportfilename'] = str(testdatadir / "backtest-result_test.json")
default_conf['pairs'] = ["ETH/BTC", "LTC/BTC"]
profit_mock = MagicMock()