Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /freqtrade_freqtrade /tests /exchange /test_exchange_utils.py
| # pragma pylint: disable=missing-docstring, protected-access, invalid-name | |
| from datetime import timedelta | |
| from math import isnan, nan | |
| from unittest.mock import MagicMock | |
| import pytest | |
| from ccxt import ( | |
| DECIMAL_PLACES, | |
| ROUND, | |
| ROUND_DOWN, | |
| ROUND_UP, | |
| SIGNIFICANT_DIGITS, | |
| TICK_SIZE, | |
| TRUNCATE, | |
| ) | |
| from freqtrade.enums import RunMode | |
| from freqtrade.exceptions import OperationalException | |
| from freqtrade.exchange import ( | |
| amount_to_contract_precision, | |
| amount_to_precision, | |
| date_minus_candles, | |
| price_to_precision, | |
| timeframe_to_minutes, | |
| timeframe_to_msecs, | |
| timeframe_to_next_date, | |
| timeframe_to_prev_date, | |
| timeframe_to_resample_freq, | |
| timeframe_to_seconds, | |
| ) | |
| from freqtrade.exchange.check_exchange import check_exchange | |
| from freqtrade.exchange.exchange_utils import _exchange_has_helper | |
| from freqtrade.util import dt_from_ts, dt_now, dt_utc | |
| from tests.conftest import log_has_re | |
| def test_check_exchange(default_conf, caplog) -> None: | |
| # Test an officially supported by Freqtrade team exchange | |
| default_conf["runmode"] = RunMode.DRY_RUN | |
| default_conf.get("exchange").update({"name": "BINANCE"}) | |
| assert check_exchange(default_conf) | |
| assert log_has_re( | |
| r"Exchange .* is officially supported by the Freqtrade development team\.", caplog | |
| ) | |
| caplog.clear() | |
| # Test an officially supported by Freqtrade team exchange | |
| default_conf.get("exchange").update({"name": "binance"}) | |
| assert check_exchange(default_conf) | |
| assert log_has_re( | |
| r"Exchange \"binance\" is officially supported by the Freqtrade development team\.", caplog | |
| ) | |
| caplog.clear() | |
| # Test an officially supported by Freqtrade team exchange | |
| default_conf.get("exchange").update({"name": "binanceus"}) | |
| assert check_exchange(default_conf) | |
| assert log_has_re( | |
| r"Exchange \"binanceus\" is officially supported by the Freqtrade development team\.", | |
| caplog, | |
| ) | |
| caplog.clear() | |
| # Test an officially supported by Freqtrade team exchange - with remapping | |
| default_conf.get("exchange").update({"name": "okx"}) | |
| assert check_exchange(default_conf) | |
| assert log_has_re( | |
| r"Exchange \"okx\" is officially supported by the Freqtrade development team\.", caplog | |
| ) | |
| caplog.clear() | |
| # Test an available exchange, supported by ccxt | |
| default_conf.get("exchange").update({"name": "bittrade"}) | |
| assert check_exchange(default_conf) | |
| assert log_has_re( | |
| r"Exchange .* is known to the ccxt library, available for the bot, " | |
| r"but not officially supported " | |
| r"by the Freqtrade development team\. .*", | |
| caplog, | |
| ) | |
| caplog.clear() | |
| # Test a 'bad' exchange, which known to have serious problems | |
| default_conf.get("exchange").update({"name": "bitmex"}) | |
| with pytest.raises(OperationalException, match=r"Exchange .* will not work with Freqtrade\..*"): | |
| check_exchange(default_conf) | |
| caplog.clear() | |
| # Test a 'bad' exchange with check_for_bad=False | |
| default_conf.get("exchange").update({"name": "bitmex"}) | |
| assert check_exchange(default_conf, False) | |
| assert log_has_re( | |
| r"Exchange .* is known to the ccxt library, available for the bot, " | |
| r"but not officially supported " | |
| r"by the Freqtrade development team\. .*", | |
| caplog, | |
| ) | |
| caplog.clear() | |
| # Test an invalid exchange | |
| default_conf.get("exchange").update({"name": "unknown_exchange"}) | |
| with pytest.raises( | |
| OperationalException, | |
| match=r'Exchange "unknown_exchange" is not known to the ccxt library ' | |
| r"and therefore not available for the bot.*", | |
| ): | |
| check_exchange(default_conf) | |
| # Test no exchange... | |
| default_conf.get("exchange").update({"name": ""}) | |
| default_conf["runmode"] = RunMode.PLOT | |
| assert check_exchange(default_conf) | |
| # Test no exchange... | |
| default_conf.get("exchange").update({"name": ""}) | |
| default_conf["runmode"] = RunMode.UTIL_EXCHANGE | |
| with pytest.raises( | |
| OperationalException, match=r"This command requires a configured exchange.*" | |
| ): | |
| check_exchange(default_conf) | |
| def test_date_minus_candles(): | |
| date = dt_utc(2019, 8, 12, 13, 25, 0) | |
| assert date_minus_candles("5m", 3, date) == date - timedelta(minutes=15) | |
| assert date_minus_candles("5m", 5, date) == date - timedelta(minutes=25) | |
| assert date_minus_candles("1m", 6, date) == date - timedelta(minutes=6) | |
| assert date_minus_candles("1h", 3, date) == date - timedelta(hours=3, minutes=25) | |
| assert date_minus_candles("1h", 3) == timeframe_to_prev_date("1h") - timedelta(hours=3) | |
| def test_timeframe_to_minutes(): | |
| assert timeframe_to_minutes("5m") == 5 | |
| assert timeframe_to_minutes("10m") == 10 | |
| assert timeframe_to_minutes("1h") == 60 | |
| assert timeframe_to_minutes("1d") == 1440 | |
| def test_timeframe_to_seconds(): | |
| assert timeframe_to_seconds("5m") == 300 | |
| assert timeframe_to_seconds("10m") == 600 | |
| assert timeframe_to_seconds("1h") == 3600 | |
| assert timeframe_to_seconds("1d") == 86400 | |
| def test_timeframe_to_msecs(): | |
| assert timeframe_to_msecs("5m") == 300000 | |
| assert timeframe_to_msecs("10m") == 600000 | |
| assert timeframe_to_msecs("1h") == 3600000 | |
| assert timeframe_to_msecs("1d") == 86400000 | |
| def test_timeframe_to_resample_freq(timeframe, expected): | |
| assert timeframe_to_resample_freq(timeframe) == expected | |
| def test_timeframe_to_prev_date(): | |
| # 2019-08-12 13:22:08 | |
| date = dt_utc(2019, 8, 12, 13, 22, 8) | |
| tf_list = [ | |
| # 5m -> 2019-08-12 13:20:00 | |
| ("5m", dt_utc(2019, 8, 12, 13, 20, 0)), | |
| # 10m -> 2019-08-12 13:20:00 | |
| ("10m", dt_utc(2019, 8, 12, 13, 20, 0)), | |
| # 1h -> 2019-08-12 13:00:00 | |
| ("1h", dt_utc(2019, 8, 12, 13, 00, 0)), | |
| # 2h -> 2019-08-12 12:00:00 | |
| ("2h", dt_utc(2019, 8, 12, 12, 00, 0)), | |
| # 4h -> 2019-08-12 12:00:00 | |
| ("4h", dt_utc(2019, 8, 12, 12, 00, 0)), | |
| # 1d -> 2019-08-12 00:00:00 | |
| ("1d", dt_utc(2019, 8, 12, 00, 00, 0)), | |
| ] | |
| for interval, result in tf_list: | |
| assert timeframe_to_prev_date(interval, date) == result | |
| date = dt_now() | |
| assert timeframe_to_prev_date("5m") < date | |
| # Does not round | |
| time = dt_utc(2019, 8, 12, 13, 20, 0) | |
| assert timeframe_to_prev_date("5m", time) == time | |
| time = dt_utc(2019, 8, 12, 13, 0, 0) | |
| assert timeframe_to_prev_date("1h", time) == time | |
| def test_timeframe_to_next_date(): | |
| # 2019-08-12 13:22:08 | |
| date = dt_from_ts(1565616128) | |
| tf_list = [ | |
| # 5m -> 2019-08-12 13:25:00 | |
| ("5m", dt_utc(2019, 8, 12, 13, 25, 0)), | |
| # 10m -> 2019-08-12 13:30:00 | |
| ("10m", dt_utc(2019, 8, 12, 13, 30, 0)), | |
| # 1h -> 2019-08-12 14:00:00 | |
| ("1h", dt_utc(2019, 8, 12, 14, 00, 0)), | |
| # 2h -> 2019-08-12 14:00:00 | |
| ("2h", dt_utc(2019, 8, 12, 14, 00, 0)), | |
| # 4h -> 2019-08-12 14:00:00 | |
| ("4h", dt_utc(2019, 8, 12, 16, 00, 0)), | |
| # 1d -> 2019-08-13 00:00:00 | |
| ("1d", dt_utc(2019, 8, 13, 0, 0, 0)), | |
| ] | |
| for interval, result in tf_list: | |
| assert timeframe_to_next_date(interval, date) == result | |
| date = dt_now() | |
| assert timeframe_to_next_date("5m") > date | |
| date = dt_utc(2019, 8, 12, 13, 30, 0) | |
| assert timeframe_to_next_date("5m", date) == date + timedelta(minutes=5) | |
| def test_amount_to_precision( | |
| amount, | |
| precision_mode, | |
| precision, | |
| expected, | |
| ): | |
| """ | |
| Test rounds down | |
| """ | |
| # digits counting mode | |
| # DECIMAL_PLACES = 2 | |
| # SIGNIFICANT_DIGITS = 3 | |
| # TICK_SIZE = 4 | |
| assert amount_to_precision(amount, precision, precision_mode) == expected | |
| def test_price_to_precision(price, precision_mode, precision, expected, rounding_mode): | |
| result = price_to_precision(price, precision, precision_mode, rounding_mode=rounding_mode) | |
| if not isnan(expected): | |
| assert result == expected | |
| else: | |
| assert isnan(result) | |
| def test_amount_to_contract_precision_standalone( | |
| amount, precision, precision_mode, contract_size, expected | |
| ): | |
| res = amount_to_contract_precision(amount, precision, precision_mode, contract_size) | |
| assert pytest.approx(res) == expected | |
| def test_exchange__exchange_has_helper(): | |
| e_mod = MagicMock() | |
| e_mod.has = { | |
| "fetchTicker": True, | |
| "fetchOHLCV": False, | |
| "fetchTrades": True, | |
| "fetchMyTrades": False, | |
| "fetchOrder": True, | |
| } | |
| required = { | |
| "fetchOHLCV": [], | |
| "fetchTicker": [], | |
| "fetchMyTrades": ["fetchTrades"], | |
| "fetchOrder": ["fetchOpenOrder", "fetchClosedOrder"], | |
| } | |
| missing = _exchange_has_helper(e_mod, required) | |
| assert set(missing) == {"fetchOHLCV"} | |
| e_mod.has = { | |
| "fetchTicker": True, | |
| "fetchOHLCV": False, | |
| "fetchTrades": False, | |
| "fetchMyTrades": False, | |
| "fetchOrder": True, | |
| } | |
| missing = _exchange_has_helper(e_mod, required) | |
| assert set(missing) == {"fetchOHLCV", "fetchMyTrades"} | |
| e_mod.has = { | |
| "fetchTicker": True, | |
| "fetchOHLCV": False, | |
| "fetchTrades": False, | |
| "fetchMyTrades": False, | |
| "fetchOrder": False, | |
| } | |
| missing = _exchange_has_helper(e_mod, required) | |
| assert set(missing) == {"fetchOHLCV", "fetchMyTrades", "fetchOrder"} | |
Xet Storage Details
- Size:
- 15.9 kB
- Xet hash:
- 88a28c6d4eb605689ad4dbbf1c36f61dc37ef42055aefa6bd78ee50eceefe3bd
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.