2023-06-17 18:00:24 +00:00
|
|
|
from unittest.mock import MagicMock, PropertyMock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from freqtrade.configuration.config_setup import setup_utils_configuration
|
|
|
|
from freqtrade.data.history.history_utils import download_data_main
|
|
|
|
from freqtrade.enums import RunMode
|
|
|
|
from freqtrade.exceptions import OperationalException
|
|
|
|
from tests.conftest import EXMS, log_has, patch_exchange
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_data_main_no_markets(mocker, caplog):
|
2024-05-12 14:00:45 +00:00
|
|
|
dl_mock = mocker.patch(
|
|
|
|
"freqtrade.data.history.history_utils.refresh_backtest_ohlcv_data",
|
|
|
|
MagicMock(return_value=["ETH/BTC", "XRP/BTC"]),
|
|
|
|
)
|
2024-07-05 07:10:55 +00:00
|
|
|
patch_exchange(mocker, exchange="binance")
|
2024-05-12 14:00:45 +00:00
|
|
|
mocker.patch(f"{EXMS}.get_markets", return_value={})
|
2023-06-17 18:00:24 +00:00
|
|
|
config = setup_utils_configuration({"exchange": "binance"}, RunMode.UTIL_EXCHANGE)
|
2024-05-12 14:00:45 +00:00
|
|
|
config.update({"days": 20, "pairs": ["ETH/BTC", "XRP/BTC"], "timeframes": ["5m", "1h"]})
|
2023-06-17 18:00:24 +00:00
|
|
|
download_data_main(config)
|
2024-05-12 14:00:45 +00:00
|
|
|
assert dl_mock.call_args[1]["timerange"].starttype == "date"
|
2023-06-17 18:00:24 +00:00
|
|
|
assert log_has("Pairs [ETH/BTC,XRP/BTC] not available on exchange Binance.", caplog)
|
|
|
|
|
|
|
|
|
|
|
|
def test_download_data_main_all_pairs(mocker, markets):
|
2024-05-12 14:00:45 +00:00
|
|
|
dl_mock = mocker.patch(
|
|
|
|
"freqtrade.data.history.history_utils.refresh_backtest_ohlcv_data",
|
|
|
|
MagicMock(return_value=["ETH/BTC", "XRP/BTC"]),
|
|
|
|
)
|
2023-06-17 18:00:24 +00:00
|
|
|
patch_exchange(mocker)
|
2024-05-12 14:00:45 +00:00
|
|
|
mocker.patch(f"{EXMS}.markets", PropertyMock(return_value=markets))
|
2023-06-17 18:00:24 +00:00
|
|
|
|
|
|
|
config = setup_utils_configuration({"exchange": "binance"}, RunMode.UTIL_EXCHANGE)
|
2024-05-12 14:00:45 +00:00
|
|
|
config.update({"pairs": [".*/USDT"], "timeframes": ["5m", "1h"]})
|
2023-06-17 18:00:24 +00:00
|
|
|
download_data_main(config)
|
2024-05-12 14:00:45 +00:00
|
|
|
expected = set(["BTC/USDT", "ETH/USDT", "XRP/USDT", "NEO/USDT", "TKN/USDT"])
|
|
|
|
assert set(dl_mock.call_args_list[0][1]["pairs"]) == expected
|
2023-06-17 18:00:24 +00:00
|
|
|
assert dl_mock.call_count == 1
|
|
|
|
|
|
|
|
dl_mock.reset_mock()
|
|
|
|
|
2024-05-12 14:00:45 +00:00
|
|
|
config.update({"pairs": [".*/USDT"], "timeframes": ["5m", "1h"], "include_inactive": True})
|
2023-06-17 18:00:24 +00:00
|
|
|
download_data_main(config)
|
2024-05-12 14:00:45 +00:00
|
|
|
expected = set(["BTC/USDT", "ETH/USDT", "LTC/USDT", "XRP/USDT", "NEO/USDT", "TKN/USDT"])
|
|
|
|
assert set(dl_mock.call_args_list[0][1]["pairs"]) == expected
|
2023-06-17 18:00:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_download_data_main_trades(mocker):
|
2024-05-12 14:00:45 +00:00
|
|
|
dl_mock = mocker.patch(
|
|
|
|
"freqtrade.data.history.history_utils.refresh_backtest_trades_data",
|
|
|
|
MagicMock(return_value=[]),
|
|
|
|
)
|
|
|
|
convert_mock = mocker.patch(
|
|
|
|
"freqtrade.data.history.history_utils.convert_trades_to_ohlcv", MagicMock(return_value=[])
|
|
|
|
)
|
2023-06-17 18:00:24 +00:00
|
|
|
patch_exchange(mocker)
|
2024-05-12 14:00:45 +00:00
|
|
|
mocker.patch(f"{EXMS}.get_markets", return_value={})
|
2023-06-17 18:00:24 +00:00
|
|
|
config = setup_utils_configuration({"exchange": "binance"}, RunMode.UTIL_EXCHANGE)
|
2024-05-12 14:00:45 +00:00
|
|
|
config.update(
|
|
|
|
{
|
|
|
|
"days": 20,
|
|
|
|
"pairs": ["ETH/BTC", "XRP/BTC"],
|
|
|
|
"timeframes": ["5m", "1h"],
|
|
|
|
"download_trades": True,
|
|
|
|
}
|
|
|
|
)
|
2023-06-17 18:00:24 +00:00
|
|
|
download_data_main(config)
|
|
|
|
|
2024-05-12 14:00:45 +00:00
|
|
|
assert dl_mock.call_args[1]["timerange"].starttype == "date"
|
2023-06-17 18:00:24 +00:00
|
|
|
assert dl_mock.call_count == 1
|
2024-05-18 18:15:02 +00:00
|
|
|
assert convert_mock.call_count == 0
|
|
|
|
dl_mock.reset_mock()
|
|
|
|
|
2024-05-12 14:00:45 +00:00
|
|
|
config.update(
|
|
|
|
{
|
2024-05-18 18:15:02 +00:00
|
|
|
"convert_trades": True,
|
2024-05-12 14:00:45 +00:00
|
|
|
}
|
|
|
|
)
|
2024-05-18 18:15:02 +00:00
|
|
|
download_data_main(config)
|
|
|
|
|
|
|
|
assert dl_mock.call_args[1]["timerange"].starttype == "date"
|
|
|
|
assert dl_mock.call_count == 1
|
|
|
|
assert convert_mock.call_count == 1
|
2023-06-17 18:00:24 +00:00
|
|
|
|
2024-06-20 16:29:17 +00:00
|
|
|
# Exchange that doesn't support historic downloads
|
|
|
|
config["exchange"]["name"] = "bybit"
|
|
|
|
with pytest.raises(OperationalException, match=r"Trade history not available for .*"):
|
|
|
|
config
|
|
|
|
download_data_main(config)
|
|
|
|
|
2023-06-17 18:00:24 +00:00
|
|
|
|
|
|
|
def test_download_data_main_data_invalid(mocker):
|
2024-07-05 07:10:55 +00:00
|
|
|
patch_exchange(mocker, exchange="kraken")
|
2024-05-12 14:00:45 +00:00
|
|
|
mocker.patch(f"{EXMS}.get_markets", return_value={})
|
2023-06-17 18:00:24 +00:00
|
|
|
config = setup_utils_configuration({"exchange": "kraken"}, RunMode.UTIL_EXCHANGE)
|
2024-05-12 14:00:45 +00:00
|
|
|
config.update(
|
|
|
|
{
|
|
|
|
"days": 20,
|
|
|
|
"pairs": ["ETH/BTC", "XRP/BTC"],
|
|
|
|
"timeframes": ["5m", "1h"],
|
|
|
|
}
|
|
|
|
)
|
2023-06-17 18:00:24 +00:00
|
|
|
with pytest.raises(OperationalException, match=r"Historic klines not available for .*"):
|
|
|
|
download_data_main(config)
|