2020-10-25 09:54:30 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from freqtrade.persistence import PairLocks
|
|
|
|
from freqtrade.persistence.models import PairLock
|
2023-05-14 16:14:35 +00:00
|
|
|
from freqtrade.util import dt_now
|
2020-10-25 09:54:30 +00:00
|
|
|
|
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
@pytest.mark.parametrize("use_db", (False, True))
|
2020-10-25 09:54:30 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_PairLocks(use_db):
|
2024-05-12 13:57:44 +00:00
|
|
|
PairLocks.timeframe = "5m"
|
2021-01-19 19:49:28 +00:00
|
|
|
PairLocks.use_db = use_db
|
2020-10-25 09:54:30 +00:00
|
|
|
# No lock should be present
|
|
|
|
if use_db:
|
2023-03-16 06:25:04 +00:00
|
|
|
assert len(PairLock.get_all_locks().all()) == 0
|
2020-10-25 09:54:30 +00:00
|
|
|
|
|
|
|
assert PairLocks.use_db == use_db
|
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
pair = "ETH/BTC"
|
2020-10-25 09:54:30 +00:00
|
|
|
assert not PairLocks.is_pair_locked(pair)
|
2023-05-14 16:14:35 +00:00
|
|
|
PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4))
|
2022-04-24 09:47:28 +00:00
|
|
|
# ETH/BTC locked for 4 minutes (on both sides)
|
2020-10-25 09:54:30 +00:00
|
|
|
assert PairLocks.is_pair_locked(pair)
|
2024-05-12 13:57:44 +00:00
|
|
|
assert PairLocks.is_pair_locked(pair, side="long")
|
|
|
|
assert PairLocks.is_pair_locked(pair, side="short")
|
2022-04-24 09:47:28 +00:00
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
pair = "BNB/BTC"
|
|
|
|
PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4), side="long")
|
2022-04-24 09:47:28 +00:00
|
|
|
assert not PairLocks.is_pair_locked(pair)
|
2024-05-12 13:57:44 +00:00
|
|
|
assert PairLocks.is_pair_locked(pair, side="long")
|
|
|
|
assert not PairLocks.is_pair_locked(pair, side="short")
|
2022-04-24 09:47:28 +00:00
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
pair = "BNB/USDT"
|
|
|
|
PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4), side="short")
|
2022-04-24 09:47:28 +00:00
|
|
|
assert not PairLocks.is_pair_locked(pair)
|
2024-05-12 13:57:44 +00:00
|
|
|
assert not PairLocks.is_pair_locked(pair, side="long")
|
|
|
|
assert PairLocks.is_pair_locked(pair, side="short")
|
2020-10-25 09:54:30 +00:00
|
|
|
|
|
|
|
# XRP/BTC should not be locked now
|
2024-05-12 13:57:44 +00:00
|
|
|
pair = "XRP/BTC"
|
2020-10-25 09:54:30 +00:00
|
|
|
assert not PairLocks.is_pair_locked(pair)
|
|
|
|
# Unlocking a pair that's not locked should not raise an error
|
|
|
|
PairLocks.unlock_pair(pair)
|
|
|
|
|
2023-05-14 16:14:35 +00:00
|
|
|
PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4))
|
2020-10-25 09:54:30 +00:00
|
|
|
assert PairLocks.is_pair_locked(pair)
|
|
|
|
|
|
|
|
# Get both locks from above
|
|
|
|
locks = PairLocks.get_pair_locks(None)
|
|
|
|
assert len(locks) == 2
|
|
|
|
|
|
|
|
# Unlock original pair
|
2024-05-12 13:57:44 +00:00
|
|
|
pair = "ETH/BTC"
|
2020-10-25 09:54:30 +00:00
|
|
|
PairLocks.unlock_pair(pair)
|
|
|
|
assert not PairLocks.is_pair_locked(pair)
|
|
|
|
assert not PairLocks.is_global_lock()
|
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
pair = "BTC/USDT"
|
2020-10-25 09:54:30 +00:00
|
|
|
# Lock until 14:30
|
|
|
|
lock_time = datetime(2020, 5, 1, 14, 30, 0, tzinfo=timezone.utc)
|
|
|
|
PairLocks.lock_pair(pair, lock_time)
|
|
|
|
|
|
|
|
assert not PairLocks.is_pair_locked(pair)
|
|
|
|
assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-10))
|
|
|
|
assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-10))
|
|
|
|
assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-50))
|
|
|
|
assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-50))
|
|
|
|
|
|
|
|
# Should not be locked after time expired
|
|
|
|
assert not PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=10))
|
|
|
|
|
|
|
|
locks = PairLocks.get_pair_locks(pair, lock_time + timedelta(minutes=-2))
|
|
|
|
assert len(locks) == 1
|
2024-05-12 13:57:44 +00:00
|
|
|
assert "PairLock" in str(locks[0])
|
2020-10-25 09:54:30 +00:00
|
|
|
|
|
|
|
# Unlock all
|
|
|
|
PairLocks.unlock_pair(pair, lock_time + timedelta(minutes=-2))
|
|
|
|
assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-50))
|
|
|
|
|
|
|
|
# Global lock
|
2024-05-12 13:57:44 +00:00
|
|
|
PairLocks.lock_pair("*", lock_time)
|
2020-10-25 09:54:30 +00:00
|
|
|
assert PairLocks.is_global_lock(lock_time + timedelta(minutes=-50))
|
2021-09-11 15:52:47 +00:00
|
|
|
# Global lock also locks every pair separately
|
2020-10-25 09:54:30 +00:00
|
|
|
assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-50))
|
2024-05-12 13:57:44 +00:00
|
|
|
assert PairLocks.is_pair_locked("XRP/USDT", lock_time + timedelta(minutes=-50))
|
2020-10-25 09:54:30 +00:00
|
|
|
|
|
|
|
if use_db:
|
2021-02-27 09:07:02 +00:00
|
|
|
locks = PairLocks.get_all_locks()
|
2023-03-16 06:25:04 +00:00
|
|
|
locks_db = PairLock.get_all_locks().all()
|
2021-02-27 09:07:02 +00:00
|
|
|
assert len(locks) == len(locks_db)
|
|
|
|
assert len(locks_db) > 0
|
2020-10-25 09:54:30 +00:00
|
|
|
else:
|
|
|
|
# Nothing was pushed to the database
|
2021-02-27 09:07:02 +00:00
|
|
|
assert len(PairLocks.get_all_locks()) > 0
|
2023-03-16 06:25:04 +00:00
|
|
|
assert len(PairLock.get_all_locks().all()) == 0
|
2020-10-25 09:54:30 +00:00
|
|
|
# Reset use-db variable
|
2020-12-07 15:01:29 +00:00
|
|
|
PairLocks.reset_locks()
|
2020-10-25 09:54:30 +00:00
|
|
|
PairLocks.use_db = True
|
2020-11-25 10:54:11 +00:00
|
|
|
|
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
@pytest.mark.parametrize("use_db", (False, True))
|
2020-11-25 10:54:11 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_PairLocks_getlongestlock(use_db):
|
2024-05-12 13:57:44 +00:00
|
|
|
PairLocks.timeframe = "5m"
|
2020-11-25 10:54:11 +00:00
|
|
|
# No lock should be present
|
2021-01-19 19:49:28 +00:00
|
|
|
PairLocks.use_db = use_db
|
2020-11-25 10:54:11 +00:00
|
|
|
if use_db:
|
2023-03-16 17:07:06 +00:00
|
|
|
assert len(PairLock.get_all_locks().all()) == 0
|
2020-11-25 10:54:11 +00:00
|
|
|
|
|
|
|
assert PairLocks.use_db == use_db
|
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
pair = "ETH/BTC"
|
2020-11-25 10:54:11 +00:00
|
|
|
assert not PairLocks.is_pair_locked(pair)
|
2023-05-14 16:14:35 +00:00
|
|
|
PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4))
|
2020-11-25 10:54:11 +00:00
|
|
|
# ETH/BTC locked for 4 minutes
|
|
|
|
assert PairLocks.is_pair_locked(pair)
|
|
|
|
lock = PairLocks.get_pair_longest_lock(pair)
|
|
|
|
|
2023-05-14 16:14:35 +00:00
|
|
|
assert lock.lock_end_time.replace(tzinfo=timezone.utc) > dt_now() + timedelta(minutes=3)
|
|
|
|
assert lock.lock_end_time.replace(tzinfo=timezone.utc) < dt_now() + timedelta(minutes=14)
|
2020-11-25 10:54:11 +00:00
|
|
|
|
2023-05-14 16:14:35 +00:00
|
|
|
PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=15))
|
2020-11-25 10:54:11 +00:00
|
|
|
assert PairLocks.is_pair_locked(pair)
|
|
|
|
|
|
|
|
lock = PairLocks.get_pair_longest_lock(pair)
|
|
|
|
# Must be longer than above
|
2023-05-14 16:14:35 +00:00
|
|
|
assert lock.lock_end_time.replace(tzinfo=timezone.utc) > dt_now() + timedelta(minutes=14)
|
2020-11-25 10:54:11 +00:00
|
|
|
|
2020-12-07 15:01:29 +00:00
|
|
|
PairLocks.reset_locks()
|
2020-11-25 10:54:11 +00:00
|
|
|
PairLocks.use_db = True
|
2021-10-30 07:39:40 +00:00
|
|
|
|
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
@pytest.mark.parametrize("use_db", (False, True))
|
2021-10-30 07:39:40 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_PairLocks_reason(use_db):
|
2024-05-12 13:57:44 +00:00
|
|
|
PairLocks.timeframe = "5m"
|
2021-10-30 07:39:40 +00:00
|
|
|
PairLocks.use_db = use_db
|
|
|
|
# No lock should be present
|
|
|
|
if use_db:
|
2023-03-16 17:07:06 +00:00
|
|
|
assert len(PairLock.get_all_locks().all()) == 0
|
2021-10-30 07:39:40 +00:00
|
|
|
|
|
|
|
assert PairLocks.use_db == use_db
|
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
PairLocks.lock_pair("XRP/USDT", dt_now() + timedelta(minutes=4), "TestLock1")
|
|
|
|
PairLocks.lock_pair("ETH/USDT", dt_now() + timedelta(minutes=4), "TestLock2")
|
2021-10-30 07:39:40 +00:00
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
assert PairLocks.is_pair_locked("XRP/USDT")
|
|
|
|
assert PairLocks.is_pair_locked("ETH/USDT")
|
2021-10-30 07:39:40 +00:00
|
|
|
|
2024-05-12 13:57:44 +00:00
|
|
|
PairLocks.unlock_reason("TestLock1")
|
|
|
|
assert not PairLocks.is_pair_locked("XRP/USDT")
|
|
|
|
assert PairLocks.is_pair_locked("ETH/USDT")
|
2021-10-30 07:39:40 +00:00
|
|
|
|
|
|
|
PairLocks.reset_locks()
|
|
|
|
PairLocks.use_db = True
|