From fe631ffd04f3cf42ee8b28135786e7eb3b41c638 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 7 Sep 2019 20:56:03 +0200 Subject: [PATCH] Use fixture to determine test_data_dir --- freqtrade/tests/conftest.py | 3 +- freqtrade/tests/data/test_btanalysis.py | 19 ++--- freqtrade/tests/data/test_converter.py | 4 +- freqtrade/tests/data/test_history.py | 87 ++++++++++---------- freqtrade/tests/optimize/test_backtesting.py | 53 ++++++------ freqtrade/tests/optimize/test_hyperopt.py | 8 +- freqtrade/tests/strategy/test_interface.py | 4 +- freqtrade/tests/test_misc.py | 8 +- freqtrade/tests/test_plotting.py | 49 ++++++----- 9 files changed, 118 insertions(+), 117 deletions(-) diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index f09181b0f..e7b8b386a 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -1049,6 +1049,7 @@ def rpc_balance(): } -def make_testdata_path(datadir) -> Path: +@pytest.fixture +def testdatadir() -> Path: """Return the path where testdata files are stored""" return (Path(__file__).parent / "testdata").resolve() diff --git a/freqtrade/tests/data/test_btanalysis.py b/freqtrade/tests/data/test_btanalysis.py index 26eff3614..e95757d4e 100644 --- a/freqtrade/tests/data/test_btanalysis.py +++ b/freqtrade/tests/data/test_btanalysis.py @@ -12,13 +12,12 @@ from freqtrade.data.btanalysis import (BT_DATA_COLUMNS, load_backtest_data, load_trades, load_trades_from_db) from freqtrade.data.history import load_data, load_pair_history -from freqtrade.tests.conftest import make_testdata_path 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'], diff --git a/freqtrade/tests/data/test_converter.py b/freqtrade/tests/data/test_converter.py index 39462bdd8..d47c3ee92 100644 --- a/freqtrade/tests/data/test_converter.py +++ b/freqtrade/tests/data/test_converter.py @@ -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', diff --git a/freqtrade/tests/data/test_history.py b/freqtrade/tests/data/test_history.py index 35b3d949b..43429031c 100644 --- a/freqtrade/tests/data/test_history.py +++ b/freqtrade/tests/data/test_history.py @@ -22,8 +22,7 @@ from freqtrade.data.history import (download_pair_history, 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, make_testdata_path) +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, @@ -159,8 +159,8 @@ def test_load_data_live_noexchange(default_conf, mocker, caplog) -> None: ) -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 ) diff --git a/freqtrade/tests/optimize/test_backtesting.py b/freqtrade/tests/optimize/test_backtesting.py index 52eae9df0..8e6459d8a 100644 --- a/freqtrade/tests/optimize/test_backtesting.py +++ b/freqtrade/tests/optimize/test_backtesting.py @@ -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' @@ -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) diff --git a/freqtrade/tests/optimize/test_hyperopt.py b/freqtrade/tests/optimize/test_hyperopt.py index 976cb3d53..4e26a71f7 100644 --- a/freqtrade/tests/optimize/test_hyperopt.py +++ b/freqtrade/tests/optimize/test_hyperopt.py @@ -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) diff --git a/freqtrade/tests/strategy/test_interface.py b/freqtrade/tests/strategy/test_interface.py index 36c9ffcd4..1bd571870 100644 --- a/freqtrade/tests/strategy/test_interface.py +++ b/freqtrade/tests/strategy/test_interface.py @@ -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) diff --git a/freqtrade/tests/test_misc.py b/freqtrade/tests/test_misc.py index c55083e64..320ed208c 100644 --- a/freqtrade/tests/test_misc.py +++ b/freqtrade/tests/test_misc.py @@ -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 diff --git a/freqtrade/tests/test_plotting.py b/freqtrade/tests/test_plotting.py index e9079fff4..a4abda98b 100644 --- a/freqtrade/tests/test_plotting.py +++ b/freqtrade/tests/test_plotting.py @@ -19,8 +19,7 @@ from freqtrade.plot.plotting import (add_indicators, add_profit, generate_profit_graph, init_plotscript, plot_profit, plot_trades, store_plot_file) from freqtrade.strategy.default_strategy import DefaultStrategy -from freqtrade.tests.conftest import (get_args, log_has, log_has_re, - make_testdata_path) +from freqtrade.tests.conftest import get_args, log_has, log_has_re def fig_generating_mock(fig, *args, **kwargs): @@ -43,12 +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"] = make_testdata_path(None) - default_conf['exportfilename'] = str(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 = 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 = 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 = 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,10 +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"] = make_testdata_path(None) - default_conf['exportfilename'] = str(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"] @@ -345,10 +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"] = make_testdata_path(None) - default_conf['exportfilename'] = str(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()