Changes to unlock_reason:

- introducing filter
	- replaced get_all_locks with a query for speed
	. removed logging in backtesting mode for speed
	. replaced for-loop with map-function for speed

Changes to models.py:
	- changed string representation of Pairlock to also contain reason and active-state
This commit is contained in:
incrementby1 2021-10-28 15:16:07 +02:00
parent dc605e29aa
commit 02e69e1667
2 changed files with 19 additions and 7 deletions

View File

@ -896,7 +896,7 @@ class PairLock(_DECL_BASE):
lock_time = self.lock_time.strftime(DATETIME_PRINT_FORMAT)
lock_end_time = self.lock_end_time.strftime(DATETIME_PRINT_FORMAT)
return (f'PairLock(id={self.id}, pair={self.pair}, lock_time={lock_time}, '
f'lock_end_time={lock_end_time})')
f'lock_end_time={lock_end_time}, reason={self.reason}, active={self.active})')
@staticmethod
def query_pair_locks(pair: Optional[str], now: datetime) -> Query:
@ -905,7 +905,6 @@ class PairLock(_DECL_BASE):
:param pair: Pair to check for. Returns all current locks if pair is empty
:param now: Datetime object (generated via datetime.now(timezone.utc)).
"""
filters = [PairLock.lock_end_time > now,
# Only active locks
PairLock.active.is_(True), ]

View File

@ -113,13 +113,26 @@ class PairLocks():
"""
if not now:
now = datetime.now(timezone.utc)
logger.info(f"Releasing all locks with reason \'{reason}\'.")
locks = PairLocks.get_all_locks()
for lock in locks:
if lock.reason == reason:
lock.active = False
def local_unlock(lock):
lock.active = False
if PairLocks.use_db:
# used in live modes
logger.info(f"Releasing all locks with reason \'{reason}\':")
filters = [PairLock.lock_end_time > now,
PairLock.active.is_(True),
PairLock.reason == reason
]
locks = PairLock.query.filter(*filters)
for lock in locks:
logger.info(f"Releasing lock for \'{lock.pair}\' with reason \'{reason}\'.")
lock.active = False
PairLock.query.session.commit()
else:
# no logging in backtesting to increase speed
locks = filter(lambda reason: reason == reason, PairLocks.locks)
locks = map(local_unlock, locks)
@staticmethod
def is_global_lock(now: Optional[datetime] = None) -> bool: