def test_api_locks(botclient): ftbot, client = botclient rc = client_get(client, f"{BASE_URI}/locks") assert_response(rc) assert 'locks' in rc.json() assert rc.json()['lock_count'] == 0 assert rc.json()['lock_count'] == len(rc.json()['locks']) PairLocks.lock_pair('ETH/BTC', datetime.now(timezone.utc) + timedelta(minutes=4), 'randreason') PairLocks.lock_pair('XRP/BTC', datetime.now(timezone.utc) + timedelta(minutes=20), 'deadbeef') rc = client_get(client, f"{BASE_URI}/locks") assert_response(rc) assert rc.json()['lock_count'] == 2 assert rc.json()['lock_count'] == len(rc.json()['locks']) assert 'ETH/BTC' in (rc.json()['locks'][0]['pair'], rc.json()['locks'][1]['pair']) assert 'randreason' in (rc.json()['locks'][0]['reason'], rc.json()['locks'][1]['reason']) assert 'deadbeef' in (rc.json()['locks'][0]['reason'], rc.json()['locks'][1]['reason']) # Test deletions rc = client_delete(client, f"{BASE_URI}/locks/1") assert_response(rc) assert rc.json()['lock_count'] == 1 rc = client_post(client, f"{BASE_URI}/locks/delete", data='{"pair": "XRP/BTC"}') assert_response(rc) assert rc.json()['lock_count'] == 0
def test_PairLocks_getlongestlock(use_db): PairLocks.timeframe = '5m' # No lock should be present PairLocks.use_db = use_db if use_db: assert len(PairLock.query.all()) == 0 assert PairLocks.use_db == use_db pair = 'ETH/BTC' assert not PairLocks.is_pair_locked(pair) PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) # ETH/BTC locked for 4 minutes assert PairLocks.is_pair_locked(pair) lock = PairLocks.get_pair_longest_lock(pair) assert lock.lock_end_time.replace( tzinfo=timezone.utc) > arrow.utcnow().shift(minutes=3) assert lock.lock_end_time.replace( tzinfo=timezone.utc) < arrow.utcnow().shift(minutes=14) PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=15).datetime) assert PairLocks.is_pair_locked(pair) lock = PairLocks.get_pair_longest_lock(pair) # Must be longer than above assert lock.lock_end_time.replace( tzinfo=timezone.utc) > arrow.utcnow().shift(minutes=14) PairLocks.reset_locks() PairLocks.use_db = True
def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float, time_in_force: str, **kwargs) -> bool: bot_id = 1234567890 coin, currency = pair.split('/') p3cw = Py3CW( key='1', secret='2', ) logger.info(f"3Commas: Sending buy signal for {pair} to 3commas bot_id={bot_id}") error, data = p3cw.request( entity='bots', action='start_new_deal', action_id=f'{bot_id}', payload={ "bot_id": bot_id, "pair": f"{currency}_{coin}{currency}", }, ) if error: logger.error(f"3Commas: {error['msg']}") PairLocks.lock_pair( pair=pair, until=datetime.now(timezone.utc) + timedelta(minutes=1), reason="Send 3c buy order" ) return False
def stop_per_pair(self, pair, now: Optional[datetime] = None) -> bool: if not now: now = datetime.now(timezone.utc) result = False for protection_handler in self._protection_handlers: if protection_handler.has_local_stop: result, until, reason = protection_handler.stop_per_pair(pair, now) if result and until: if not PairLocks.is_pair_locked(pair, until): PairLocks.lock_pair(pair, until, reason, now=now) result = True return result
def lock_pair(self, pair: str, until: datetime, reason: str = None) -> None: """ Locks pair until a given timestamp happens. Locked pairs are not analyzed, and are prevented from opening new trades. Locks can only count up (allowing users to lock pairs for a longer period of time). To remove a lock from a pair, use `unlock_pair()` :param pair: Pair to lock :param until: datetime in UTC until the pair should be blocked from opening new trades. Needs to be timezone aware `datetime.now(timezone.utc)` :param reason: Optional string explaining why the pair was locked. """ PairLocks.lock_pair(pair, until, reason)
def global_stop(self, now: Optional[datetime] = None) -> bool: if not now: now = datetime.now(timezone.utc) result = False for protection_handler in self._protection_handlers: if protection_handler.has_global_stop: result, until, reason = protection_handler.global_stop(now) # Early stopping - first positive result blocks further trades if result and until: if not PairLocks.is_global_lock(until): PairLocks.lock_pair('*', until, reason, now=now) result = True return result
def test_PairLocks_reason(use_db): PairLocks.timeframe = '5m' PairLocks.use_db = use_db # No lock should be present if use_db: assert len(PairLock.query.all()) == 0 assert PairLocks.use_db == use_db PairLocks.lock_pair('XRP/USDT', arrow.utcnow().shift(minutes=4).datetime, 'TestLock1') PairLocks.lock_pair('ETH/USDT', arrow.utcnow().shift(minutes=4).datetime, 'TestLock2') assert PairLocks.is_pair_locked('XRP/USDT') assert PairLocks.is_pair_locked('ETH/USDT') PairLocks.unlock_reason('TestLock1') assert not PairLocks.is_pair_locked('XRP/USDT') assert PairLocks.is_pair_locked('ETH/USDT') PairLocks.reset_locks() PairLocks.use_db = True
def test_PairLocks(use_db): PairLocks.timeframe = '5m' # No lock should be present if use_db: assert len(PairLock.query.all()) == 0 else: PairLocks.use_db = False assert PairLocks.use_db == use_db pair = 'ETH/BTC' assert not PairLocks.is_pair_locked(pair) PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) # ETH/BTC locked for 4 minutes assert PairLocks.is_pair_locked(pair) # XRP/BTC should not be locked now pair = 'XRP/BTC' assert not PairLocks.is_pair_locked(pair) # Unlocking a pair that's not locked should not raise an error PairLocks.unlock_pair(pair) PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) assert PairLocks.is_pair_locked(pair) # Get both locks from above locks = PairLocks.get_pair_locks(None) assert len(locks) == 2 # Unlock original pair pair = 'ETH/BTC' PairLocks.unlock_pair(pair) assert not PairLocks.is_pair_locked(pair) assert not PairLocks.is_global_lock() pair = 'BTC/USDT' # 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 assert 'PairLock' in str(locks[0]) # Unlock all PairLocks.unlock_pair(pair, lock_time + timedelta(minutes=-2)) assert not PairLocks.is_global_lock(lock_time + timedelta(minutes=-50)) # Global lock PairLocks.lock_pair('*', lock_time) assert PairLocks.is_global_lock(lock_time + timedelta(minutes=-50)) # Global lock also locks every pair seperately assert PairLocks.is_pair_locked(pair, lock_time + timedelta(minutes=-50)) assert PairLocks.is_pair_locked('XRP/USDT', lock_time + timedelta(minutes=-50)) if use_db: assert len(PairLock.query.all()) > 0 else: # Nothing was pushed to the database assert len(PairLock.query.all()) == 0 # Reset use-db variable PairLocks.use_db = True