Example #1
0
 def lock_pair(pair: str,
               until: datetime,
               reason: str = None,
               *,
               now: datetime = None) -> PairLock:
     """
     Create PairLock from now to "until".
     Uses database by default, unless PairLocks.use_db is set to False,
     in which case a list is maintained.
     :param pair: pair to lock. use '*' to lock all pairs
     :param until: End time of the lock. Will be rounded up to the next candle.
     :param reason: Reason string that will be shown as reason for the lock
     :param now: Current timestamp. Used to determine lock start time.
     """
     lock = PairLock(pair=pair,
                     lock_time=now or datetime.now(timezone.utc),
                     lock_end_time=timeframe_to_next_date(
                         PairLocks.timeframe, until),
                     reason=reason,
                     active=True)
     if PairLocks.use_db:
         PairLock.query.session.add(lock)
         PairLock.query.session.commit()
     else:
         PairLocks.locks.append(lock)
     return lock
Example #2
0
 def lock_pair(pair: str, until: datetime, reason: str = None) -> None:
     lock = PairLock(pair=pair,
                     lock_time=datetime.now(timezone.utc),
                     lock_end_time=timeframe_to_next_date(
                         PairLocks.timeframe, until),
                     reason=reason,
                     active=True)
     if PairLocks.use_db:
         PairLock.session.add(lock)
         PairLock.session.flush()
     else:
         PairLocks.locks.append(lock)
    def get_pair_locks(pair: Optional[str], now: Optional[datetime] = None) -> List[PairLock]:
        """
        Get all currently active locks for this pair
        :param pair: Pair to check for. Returns all current locks if pair is empty
        :param now: Datetime object (generated via datetime.now(timezone.utc)).
                    defaults to datetime.now(timezone.utc)
        """
        if not now:
            now = datetime.now(timezone.utc)

        if PairLocks.use_db:
            return PairLock.query_pair_locks(pair, now).all()
        else:
            locks = [lock for lock in PairLocks.locks if (
                lock.lock_end_time >= now
                and lock.active is True
                and (pair is None or lock.pair == pair)
            )]
            return locks