2020-06-14 09:51:20 +00:00
|
|
|
from datetime import datetime, timezone
|
2020-05-12 04:38:14 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-12-25 12:56:30 +00:00
|
|
|
|
2020-05-11 15:32:28 +00:00
|
|
|
import pytest
|
2020-06-14 09:51:20 +00:00
|
|
|
from pandas import DataFrame
|
2018-12-25 12:56:30 +00:00
|
|
|
|
|
|
|
from freqtrade.data.dataprovider import DataProvider
|
2020-06-28 14:04:04 +00:00
|
|
|
from freqtrade.exceptions import ExchangeError, OperationalException
|
2020-12-23 15:54:35 +00:00
|
|
|
from freqtrade.plugins.pairlistmanager import PairListManager
|
2018-12-29 08:13:20 +00:00
|
|
|
from freqtrade.state import RunMode
|
2020-05-11 15:32:28 +00:00
|
|
|
from tests.conftest import get_patched_exchange
|
2018-12-25 12:56:30 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_ohlcv(mocker, default_conf, ohlcv_history):
|
2019-01-22 05:56:54 +00:00
|
|
|
default_conf["runmode"] = RunMode.DRY_RUN
|
2020-06-01 18:47:27 +00:00
|
|
|
timeframe = default_conf["timeframe"]
|
2018-12-25 12:56:30 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2020-03-08 10:35:31 +00:00
|
|
|
exchange._klines[("XRP/BTC", timeframe)] = ohlcv_history
|
|
|
|
exchange._klines[("UNITTEST/BTC", timeframe)] = ohlcv_history
|
2019-08-18 10:06:21 +00:00
|
|
|
|
2018-12-25 12:56:30 +00:00
|
|
|
dp = DataProvider(default_conf, exchange)
|
2018-12-29 08:13:20 +00:00
|
|
|
assert dp.runmode == RunMode.DRY_RUN
|
2020-03-08 10:35:31 +00:00
|
|
|
assert ohlcv_history.equals(dp.ohlcv("UNITTEST/BTC", timeframe))
|
2019-11-02 19:26:26 +00:00
|
|
|
assert isinstance(dp.ohlcv("UNITTEST/BTC", timeframe), DataFrame)
|
2020-03-08 10:35:31 +00:00
|
|
|
assert dp.ohlcv("UNITTEST/BTC", timeframe) is not ohlcv_history
|
|
|
|
assert dp.ohlcv("UNITTEST/BTC", timeframe, copy=False) is ohlcv_history
|
2019-11-02 19:26:26 +00:00
|
|
|
assert not dp.ohlcv("UNITTEST/BTC", timeframe).empty
|
|
|
|
assert dp.ohlcv("NONESENSE/AAA", timeframe).empty
|
2018-12-30 06:15:49 +00:00
|
|
|
|
|
|
|
# Test with and without parameter
|
2019-11-02 19:26:26 +00:00
|
|
|
assert dp.ohlcv("UNITTEST/BTC", timeframe).equals(dp.ohlcv("UNITTEST/BTC"))
|
2018-12-29 08:13:20 +00:00
|
|
|
|
2019-01-22 05:56:54 +00:00
|
|
|
default_conf["runmode"] = RunMode.LIVE
|
2018-12-29 08:13:20 +00:00
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
assert dp.runmode == RunMode.LIVE
|
2019-11-02 19:26:26 +00:00
|
|
|
assert isinstance(dp.ohlcv("UNITTEST/BTC", timeframe), DataFrame)
|
2018-12-29 08:13:20 +00:00
|
|
|
|
2019-01-22 05:56:54 +00:00
|
|
|
default_conf["runmode"] = RunMode.BACKTEST
|
2018-12-29 08:13:20 +00:00
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
assert dp.runmode == RunMode.BACKTEST
|
2019-11-02 19:26:26 +00:00
|
|
|
assert dp.ohlcv("UNITTEST/BTC", timeframe).empty
|
2018-12-25 12:56:30 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_historic_ohlcv(mocker, default_conf, ohlcv_history):
|
|
|
|
historymock = MagicMock(return_value=ohlcv_history)
|
2019-01-22 05:56:54 +00:00
|
|
|
mocker.patch("freqtrade.data.dataprovider.load_pair_history", historymock)
|
2018-12-25 12:56:30 +00:00
|
|
|
|
|
|
|
dp = DataProvider(default_conf, None)
|
2019-01-22 05:56:54 +00:00
|
|
|
data = dp.historic_ohlcv("UNITTEST/BTC", "5m")
|
2018-12-25 12:56:30 +00:00
|
|
|
assert isinstance(data, DataFrame)
|
|
|
|
assert historymock.call_count == 1
|
2019-11-02 19:19:13 +00:00
|
|
|
assert historymock.call_args_list[0][1]["timeframe"] == "5m"
|
2018-12-26 13:23:21 +00:00
|
|
|
|
|
|
|
|
2020-10-31 11:45:46 +00:00
|
|
|
def test_historic_ohlcv_dataformat(mocker, default_conf, ohlcv_history):
|
|
|
|
hdf5loadmock = MagicMock(return_value=ohlcv_history)
|
|
|
|
jsonloadmock = MagicMock(return_value=ohlcv_history)
|
|
|
|
mocker.patch("freqtrade.data.history.hdf5datahandler.HDF5DataHandler._ohlcv_load", hdf5loadmock)
|
|
|
|
mocker.patch("freqtrade.data.history.jsondatahandler.JsonDataHandler._ohlcv_load", jsonloadmock)
|
|
|
|
|
|
|
|
default_conf["runmode"] = RunMode.BACKTEST
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
data = dp.historic_ohlcv("UNITTEST/BTC", "5m")
|
|
|
|
assert isinstance(data, DataFrame)
|
|
|
|
hdf5loadmock.assert_not_called()
|
|
|
|
jsonloadmock.assert_called_once()
|
|
|
|
|
|
|
|
# Swiching to dataformat hdf5
|
|
|
|
hdf5loadmock.reset_mock()
|
|
|
|
jsonloadmock.reset_mock()
|
|
|
|
default_conf["dataformat_ohlcv"] = "hdf5"
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
data = dp.historic_ohlcv("UNITTEST/BTC", "5m")
|
|
|
|
assert isinstance(data, DataFrame)
|
|
|
|
hdf5loadmock.assert_called_once()
|
|
|
|
jsonloadmock.assert_not_called()
|
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_get_pair_dataframe(mocker, default_conf, ohlcv_history):
|
2019-08-17 09:59:27 +00:00
|
|
|
default_conf["runmode"] = RunMode.DRY_RUN
|
2020-06-01 18:47:27 +00:00
|
|
|
timeframe = default_conf["timeframe"]
|
2019-08-17 09:59:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2020-06-01 18:47:27 +00:00
|
|
|
exchange._klines[("XRP/BTC", timeframe)] = ohlcv_history
|
|
|
|
exchange._klines[("UNITTEST/BTC", timeframe)] = ohlcv_history
|
2019-08-18 10:06:21 +00:00
|
|
|
|
2019-08-17 09:59:27 +00:00
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
assert dp.runmode == RunMode.DRY_RUN
|
2020-06-01 18:47:27 +00:00
|
|
|
assert ohlcv_history.equals(dp.get_pair_dataframe("UNITTEST/BTC", timeframe))
|
|
|
|
assert isinstance(dp.get_pair_dataframe("UNITTEST/BTC", timeframe), DataFrame)
|
|
|
|
assert dp.get_pair_dataframe("UNITTEST/BTC", timeframe) is not ohlcv_history
|
|
|
|
assert not dp.get_pair_dataframe("UNITTEST/BTC", timeframe).empty
|
|
|
|
assert dp.get_pair_dataframe("NONESENSE/AAA", timeframe).empty
|
2019-08-17 09:59:27 +00:00
|
|
|
|
|
|
|
# Test with and without parameter
|
2020-06-01 18:47:27 +00:00
|
|
|
assert dp.get_pair_dataframe("UNITTEST/BTC", timeframe)\
|
2020-05-06 15:48:57 +00:00
|
|
|
.equals(dp.get_pair_dataframe("UNITTEST/BTC"))
|
2019-08-17 09:59:27 +00:00
|
|
|
|
|
|
|
default_conf["runmode"] = RunMode.LIVE
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
assert dp.runmode == RunMode.LIVE
|
2020-06-01 18:47:27 +00:00
|
|
|
assert isinstance(dp.get_pair_dataframe("UNITTEST/BTC", timeframe), DataFrame)
|
|
|
|
assert dp.get_pair_dataframe("NONESENSE/AAA", timeframe).empty
|
2019-08-17 09:59:27 +00:00
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
historymock = MagicMock(return_value=ohlcv_history)
|
2019-08-17 09:59:27 +00:00
|
|
|
mocker.patch("freqtrade.data.dataprovider.load_pair_history", historymock)
|
|
|
|
default_conf["runmode"] = RunMode.BACKTEST
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
assert dp.runmode == RunMode.BACKTEST
|
2020-06-01 18:47:27 +00:00
|
|
|
assert isinstance(dp.get_pair_dataframe("UNITTEST/BTC", timeframe), DataFrame)
|
|
|
|
# assert dp.get_pair_dataframe("NONESENSE/AAA", timeframe).empty
|
2019-08-17 09:59:27 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_available_pairs(mocker, default_conf, ohlcv_history):
|
2018-12-26 13:23:21 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2020-06-01 18:47:27 +00:00
|
|
|
timeframe = default_conf["timeframe"]
|
|
|
|
exchange._klines[("XRP/BTC", timeframe)] = ohlcv_history
|
|
|
|
exchange._klines[("UNITTEST/BTC", timeframe)] = ohlcv_history
|
2018-12-26 13:23:21 +00:00
|
|
|
|
2019-08-18 10:06:21 +00:00
|
|
|
dp = DataProvider(default_conf, exchange)
|
2018-12-26 13:23:21 +00:00
|
|
|
assert len(dp.available_pairs) == 2
|
2020-06-01 18:47:27 +00:00
|
|
|
assert dp.available_pairs == [("XRP/BTC", timeframe), ("UNITTEST/BTC", timeframe), ]
|
2019-01-22 05:55:40 +00:00
|
|
|
|
|
|
|
|
2020-03-08 10:35:31 +00:00
|
|
|
def test_refresh(mocker, default_conf, ohlcv_history):
|
2019-01-22 05:55:40 +00:00
|
|
|
refresh_mock = MagicMock()
|
2019-01-22 05:56:54 +00:00
|
|
|
mocker.patch("freqtrade.exchange.Exchange.refresh_latest_ohlcv", refresh_mock)
|
2019-01-22 05:55:40 +00:00
|
|
|
|
2019-01-22 05:56:54 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id="binance")
|
2020-06-01 18:47:27 +00:00
|
|
|
timeframe = default_conf["timeframe"]
|
|
|
|
pairs = [("XRP/BTC", timeframe), ("UNITTEST/BTC", timeframe)]
|
2019-01-22 05:55:40 +00:00
|
|
|
|
2020-06-02 07:41:42 +00:00
|
|
|
pairs_non_trad = [("ETH/USDT", timeframe), ("BTC/TUSD", "1h")]
|
2019-01-22 05:55:40 +00:00
|
|
|
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
dp.refresh(pairs)
|
|
|
|
|
|
|
|
assert refresh_mock.call_count == 1
|
|
|
|
assert len(refresh_mock.call_args[0]) == 1
|
|
|
|
assert len(refresh_mock.call_args[0][0]) == len(pairs)
|
|
|
|
assert refresh_mock.call_args[0][0] == pairs
|
|
|
|
|
|
|
|
refresh_mock.reset_mock()
|
|
|
|
dp.refresh(pairs, pairs_non_trad)
|
|
|
|
assert refresh_mock.call_count == 1
|
|
|
|
assert len(refresh_mock.call_args[0]) == 1
|
|
|
|
assert len(refresh_mock.call_args[0][0]) == len(pairs) + len(pairs_non_trad)
|
|
|
|
assert refresh_mock.call_args[0][0] == pairs + pairs_non_trad
|
2019-10-10 15:03:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_orderbook(mocker, default_conf, order_book_l2):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.fetch_l2_order_book = order_book_l2
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock=api_mock)
|
|
|
|
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
res = dp.orderbook('ETH/BTC', 5)
|
|
|
|
assert order_book_l2.call_count == 1
|
|
|
|
assert order_book_l2.call_args_list[0][0][0] == 'ETH/BTC'
|
2020-10-13 18:29:51 +00:00
|
|
|
assert order_book_l2.call_args_list[0][0][1] >= 5
|
2019-10-10 15:03:52 +00:00
|
|
|
|
|
|
|
assert type(res) is dict
|
|
|
|
assert 'bids' in res
|
|
|
|
assert 'asks' in res
|
|
|
|
|
|
|
|
|
|
|
|
def test_market(mocker, default_conf, markets):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.markets = markets
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock=api_mock)
|
|
|
|
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
res = dp.market('ETH/BTC')
|
|
|
|
|
|
|
|
assert type(res) is dict
|
|
|
|
assert 'symbol' in res
|
|
|
|
assert res['symbol'] == 'ETH/BTC'
|
|
|
|
|
|
|
|
res = dp.market('UNITTEST/BTC')
|
|
|
|
assert res is None
|
2020-05-06 15:48:57 +00:00
|
|
|
|
|
|
|
|
2020-05-15 16:19:52 +00:00
|
|
|
def test_ticker(mocker, default_conf, tickers):
|
|
|
|
ticker_mock = MagicMock(return_value=tickers()['ETH/BTC'])
|
|
|
|
mocker.patch("freqtrade.exchange.Exchange.fetch_ticker", ticker_mock)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2020-05-14 13:43:16 +00:00
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
res = dp.ticker('ETH/BTC')
|
|
|
|
assert type(res) is dict
|
|
|
|
assert 'symbol' in res
|
|
|
|
assert res['symbol'] == 'ETH/BTC'
|
|
|
|
|
2020-06-28 14:04:04 +00:00
|
|
|
ticker_mock = MagicMock(side_effect=ExchangeError('Pair not found'))
|
2020-05-15 16:19:52 +00:00
|
|
|
mocker.patch("freqtrade.exchange.Exchange.fetch_ticker", ticker_mock)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
2020-05-14 13:43:16 +00:00
|
|
|
res = dp.ticker('UNITTEST/BTC')
|
|
|
|
assert res == {}
|
|
|
|
|
|
|
|
|
2020-05-12 04:38:14 +00:00
|
|
|
def test_current_whitelist(mocker, default_conf, tickers):
|
2020-05-11 15:32:28 +00:00
|
|
|
# patch default conf to volumepairlist
|
|
|
|
default_conf['pairlists'][0] = {'method': 'VolumePairList', "number_assets": 5}
|
|
|
|
|
2020-05-12 04:38:14 +00:00
|
|
|
mocker.patch.multiple('freqtrade.exchange.Exchange',
|
|
|
|
exchange_has=MagicMock(return_value=True),
|
|
|
|
get_tickers=tickers)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
|
2020-05-11 15:32:28 +00:00
|
|
|
pairlist = PairListManager(exchange, default_conf)
|
|
|
|
dp = DataProvider(default_conf, exchange, pairlist)
|
|
|
|
|
|
|
|
# Simulate volumepairs from exchange.
|
2020-05-12 04:38:14 +00:00
|
|
|
pairlist.refresh_pairlist()
|
2020-05-11 15:32:28 +00:00
|
|
|
|
|
|
|
assert dp.current_whitelist() == pairlist._whitelist
|
2021-04-13 04:57:21 +00:00
|
|
|
# The identity of the 2 lists should not be identical, but a copy
|
|
|
|
assert dp.current_whitelist() is not pairlist._whitelist
|
2020-05-11 15:32:28 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
dp.current_whitelist()
|
2020-06-14 09:51:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_analyzed_dataframe(mocker, default_conf, ohlcv_history):
|
|
|
|
|
|
|
|
default_conf["runmode"] = RunMode.DRY_RUN
|
|
|
|
|
|
|
|
timeframe = default_conf["timeframe"]
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
|
|
|
|
dp = DataProvider(default_conf, exchange)
|
|
|
|
dp._set_cached_df("XRP/BTC", timeframe, ohlcv_history)
|
|
|
|
dp._set_cached_df("UNITTEST/BTC", timeframe, ohlcv_history)
|
|
|
|
|
|
|
|
assert dp.runmode == RunMode.DRY_RUN
|
|
|
|
dataframe, time = dp.get_analyzed_dataframe("UNITTEST/BTC", timeframe)
|
|
|
|
assert ohlcv_history.equals(dataframe)
|
|
|
|
assert isinstance(time, datetime)
|
|
|
|
|
|
|
|
dataframe, time = dp.get_analyzed_dataframe("XRP/BTC", timeframe)
|
|
|
|
assert ohlcv_history.equals(dataframe)
|
|
|
|
assert isinstance(time, datetime)
|
|
|
|
|
|
|
|
dataframe, time = dp.get_analyzed_dataframe("NOTHING/BTC", timeframe)
|
|
|
|
assert dataframe.empty
|
|
|
|
assert isinstance(time, datetime)
|
|
|
|
assert time == datetime(1970, 1, 1, tzinfo=timezone.utc)
|
2021-05-05 18:08:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_no_exchange_mode(default_conf):
|
|
|
|
dp = DataProvider(default_conf, None)
|
|
|
|
|
|
|
|
message = "Exchange is not available to DataProvider."
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=message):
|
|
|
|
dp.refresh([()])
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=message):
|
|
|
|
dp.ohlcv('XRP/USDT', '5m')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=message):
|
|
|
|
dp.market('XRP/USDT')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=message):
|
|
|
|
dp.ticker('XRP/USDT')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=message):
|
|
|
|
dp.orderbook('XRP/USDT', 20)
|