diff --git a/tests/plugins/test_pairlocks.py b/tests/plugins/test_pairlocks.py index 6b7112f98..6e209df60 100644 --- a/tests/plugins/test_pairlocks.py +++ b/tests/plugins/test_pairlocks.py @@ -1,10 +1,10 @@ from datetime import datetime, timedelta, timezone -import arrow import pytest from freqtrade.persistence import PairLocks from freqtrade.persistence.models import PairLock +from freqtrade.util import dt_now @pytest.mark.parametrize('use_db', (False, True)) @@ -20,20 +20,20 @@ def test_PairLocks(use_db): pair = 'ETH/BTC' assert not PairLocks.is_pair_locked(pair) - PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) + PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4)) # ETH/BTC locked for 4 minutes (on both sides) assert PairLocks.is_pair_locked(pair) assert PairLocks.is_pair_locked(pair, side='long') assert PairLocks.is_pair_locked(pair, side='short') pair = 'BNB/BTC' - PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime, side='long') + PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4), side='long') assert not PairLocks.is_pair_locked(pair) assert PairLocks.is_pair_locked(pair, side='long') assert not PairLocks.is_pair_locked(pair, side='short') pair = 'BNB/USDT' - PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime, side='short') + PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4), side='short') assert not PairLocks.is_pair_locked(pair) assert not PairLocks.is_pair_locked(pair, side='long') assert PairLocks.is_pair_locked(pair, side='short') @@ -44,7 +44,7 @@ def test_PairLocks(use_db): # 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) + PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4)) assert PairLocks.is_pair_locked(pair) # Get both locks from above @@ -113,20 +113,20 @@ def test_PairLocks_getlongestlock(use_db): pair = 'ETH/BTC' assert not PairLocks.is_pair_locked(pair) - PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=4).datetime) + PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=4)) # 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) + 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) - PairLocks.lock_pair(pair, arrow.utcnow().shift(minutes=15).datetime) + PairLocks.lock_pair(pair, dt_now() + timedelta(minutes=15)) 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) + assert lock.lock_end_time.replace(tzinfo=timezone.utc) > dt_now() + timedelta(minutes=14) PairLocks.reset_locks() PairLocks.use_db = True @@ -143,8 +143,8 @@ def test_PairLocks_reason(use_db): 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') + PairLocks.lock_pair('XRP/USDT', dt_now() + timedelta(minutes=4), 'TestLock1') + PairLocks.lock_pair('ETH/USDT', dt_now() + timedelta(minutes=4), 'TestLock2') assert PairLocks.is_pair_locked('XRP/USDT') assert PairLocks.is_pair_locked('ETH/USDT') diff --git a/tests/rpc/test_rpc_telegram.py b/tests/rpc/test_rpc_telegram.py index 0d8c98d29..96d56d13f 100644 --- a/tests/rpc/test_rpc_telegram.py +++ b/tests/rpc/test_rpc_telegram.py @@ -33,6 +33,7 @@ from freqtrade.persistence.models import Order from freqtrade.rpc import RPC from freqtrade.rpc.rpc import RPCException from freqtrade.rpc.telegram import Telegram, authorized_only +from freqtrade.util.datetime_helpers import dt_now from tests.conftest import (CURRENT_TEST_STRATEGY, EXMS, create_mock_trades, create_mock_trades_usdt, get_patched_freqtradebot, log_has, log_has_re, patch_exchange, patch_get_signal, patch_whitelist) @@ -1518,8 +1519,8 @@ async def test_telegram_lock_handle(default_conf, update, ticker, fee, mocker) - msg_mock.reset_mock() - PairLocks.lock_pair('ETH/BTC', arrow.utcnow().shift(minutes=4).datetime, 'randreason') - PairLocks.lock_pair('XRP/BTC', arrow.utcnow().shift(minutes=20).datetime, 'deadbeef') + PairLocks.lock_pair('ETH/BTC', dt_now() + timedelta(minutes=4), 'randreason') + PairLocks.lock_pair('XRP/BTC', dt_now() + timedelta(minutes=20), 'deadbeef') await telegram._locks(update=update, context=MagicMock()) @@ -1898,7 +1899,7 @@ def test_send_msg_enter_notification(default_conf, mocker, caplog, message_type, 'current_rate': 1.099e-05, 'amount': 1333.3333333333335, 'analyzed_candle': {'open': 1.1, 'high': 2.2, 'low': 1.0, 'close': 1.5}, - 'open_date': arrow.utcnow().shift(hours=-1) + 'open_date': dt_now() + timedelta(hours=-1) } telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf) @@ -1959,7 +1960,7 @@ def test_send_msg_protection_notification(default_conf, mocker, time_machine) -> telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) time_machine.move_to("2021-09-01 05:00:00 +00:00") - lock = PairLocks.lock_pair('ETH/BTC', arrow.utcnow().shift(minutes=6).datetime, 'randreason') + lock = PairLocks.lock_pair('ETH/BTC', dt_now() + timedelta(minutes=6), 'randreason') msg = { 'type': RPCMessageType.PROTECTION_TRIGGER, } @@ -1974,7 +1975,7 @@ def test_send_msg_protection_notification(default_conf, mocker, time_machine) -> msg = { 'type': RPCMessageType.PROTECTION_TRIGGER_GLOBAL, } - lock = PairLocks.lock_pair('*', arrow.utcnow().shift(minutes=100).datetime, 'randreason') + lock = PairLocks.lock_pair('*', dt_now() + timedelta(minutes=100), 'randreason') msg.update(lock.to_json()) telegram.send_msg(msg) assert (msg_mock.call_args[0][0] == "*Protection* triggered due to randreason. " @@ -2005,7 +2006,7 @@ def test_send_msg_entry_fill_notification(default_conf, mocker, message_type, en 'fiat_currency': 'USD', 'open_rate': 1.099e-05, 'amount': 1333.3333333333335, - 'open_date': arrow.utcnow().shift(hours=-1) + 'open_date': dt_now() - timedelta(hours=1) }) leverage_text = f'*Leverage:* `{leverage}`\n' if leverage != 1.0 else '' assert msg_mock.call_args[0][0] == ( @@ -2032,7 +2033,7 @@ def test_send_msg_entry_fill_notification(default_conf, mocker, message_type, en 'fiat_currency': 'USD', 'open_rate': 1.099e-05, 'amount': 1333.3333333333335, - 'open_date': arrow.utcnow().shift(hours=-1) + 'open_date': dt_now() - timedelta(hours=1) }) assert msg_mock.call_args[0][0] == ( @@ -2071,7 +2072,7 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None: 'fiat_currency': 'USD', 'enter_tag': 'buy_signal1', 'exit_reason': ExitType.STOP_LOSS.value, - 'open_date': arrow.utcnow().shift(hours=-1), + 'open_date': dt_now() - timedelta(hours=1), 'close_date': arrow.utcnow(), }) assert msg_mock.call_args[0][0] == ( @@ -2107,7 +2108,7 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None: 'fiat_currency': 'USD', 'enter_tag': 'buy_signal1', 'exit_reason': ExitType.STOP_LOSS.value, - 'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30), + 'open_date': dt_now() - timedelta(days=1, hours=2, minutes=30), 'close_date': arrow.utcnow(), 'stake_amount': 0.01, 'sub_trade': True, @@ -2144,7 +2145,7 @@ def test_send_msg_sell_notification(default_conf, mocker) -> None: 'stake_currency': 'ETH', 'enter_tag': 'buy_signal1', 'exit_reason': ExitType.STOP_LOSS.value, - 'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30), + 'open_date': dt_now() - timedelta(days=1, hours=2, minutes=30), 'close_date': arrow.utcnow(), }) assert msg_mock.call_args[0][0] == ( @@ -2226,7 +2227,7 @@ def test_send_msg_sell_fill_notification(default_conf, mocker, direction, 'stake_currency': 'ETH', 'enter_tag': enter_signal, 'exit_reason': ExitType.STOP_LOSS.value, - 'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30), + 'open_date': dt_now() - timedelta(days=1, hours=2, minutes=30), 'close_date': arrow.utcnow(), }) @@ -2317,7 +2318,7 @@ def test_send_msg_buy_notification_no_fiat( 'fiat_currency': None, 'current_rate': 1.099e-05, 'amount': 1333.3333333333335, - 'open_date': arrow.utcnow().shift(hours=-1) + 'open_date': dt_now() - timedelta(hours=1) }) leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else '' @@ -2363,7 +2364,7 @@ def test_send_msg_sell_notification_no_fiat( 'fiat_currency': 'USD', 'enter_tag': enter_signal, 'exit_reason': ExitType.STOP_LOSS.value, - 'open_date': arrow.utcnow().shift(hours=-2, minutes=-35, seconds=-3), + 'open_date': dt_now() - timedelta(hours=2, minutes=35, seconds=3), 'close_date': arrow.utcnow(), }) diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index 23d5e6ea3..8be0986cd 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -729,7 +729,7 @@ def test_is_pair_locked(default_conf): pair = 'ETH/BTC' assert not strategy.is_pair_locked(pair) - strategy.lock_pair(pair, arrow.now(timezone.utc).shift(minutes=4).datetime) + strategy.lock_pair(pair, dt_now() + timedelta(minutes=4)) # ETH/BTC locked for 4 minutes assert strategy.is_pair_locked(pair) @@ -747,7 +747,7 @@ def test_is_pair_locked(default_conf): # Lock with reason reason = "TestLockR" - strategy.lock_pair(pair, arrow.now(timezone.utc).shift(minutes=4).datetime, reason) + strategy.lock_pair(pair, dt_now() + timedelta(minutes=4), reason) assert strategy.is_pair_locked(pair) strategy.unlock_reason(reason) assert not strategy.is_pair_locked(pair) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 288ad58f0..9a2e5bd88 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -4,10 +4,10 @@ import logging import time from copy import deepcopy +from datetime import timedelta from typing import List from unittest.mock import ANY, MagicMock, PropertyMock, patch -import arrow import pytest from pandas import DataFrame from sqlalchemy import select @@ -446,7 +446,7 @@ def test_enter_positions_global_pairlock(default_conf_usdt, ticker_usdt, limit_b assert not log_has_re(message, caplog) caplog.clear() - PairLocks.lock_pair('*', arrow.utcnow().shift(minutes=20).datetime, 'Just because', side='*') + PairLocks.lock_pair('*', dt_now() + timedelta(minutes=20), 'Just because', side='*') n = freqtrade.enter_positions() assert n == 0 assert log_has_re(message, caplog) @@ -467,7 +467,7 @@ def test_handle_protections(mocker, default_conf_usdt, fee, is_short): freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) freqtrade.protections._protection_handlers[1].global_stop = MagicMock( - return_value=ProtectionReturn(True, arrow.utcnow().shift(hours=1).datetime, "asdf")) + return_value=ProtectionReturn(True, dt_now() + timedelta(hours=1), "asdf")) create_mock_trades(fee, is_short) freqtrade.handle_protections('ETC/BTC', '*') send_msg_mock = freqtrade.rpc.send_msg @@ -1263,7 +1263,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_ }]) trade.stoploss_order_id = "107" trade.is_open = True - trade.stoploss_last_update = arrow.utcnow().shift(hours=-1).datetime + trade.stoploss_last_update = dt_now() - timedelta(hours=1) trade.stop_loss = 24 trade.exit_reason = None trade.orders.append( @@ -1412,7 +1412,7 @@ def test_handle_stoploss_on_exchange_partial_cancel_here( }) mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_hit) mocker.patch(f'{EXMS}.cancel_stoploss_order_with_result', stoploss_order_cancel) - trade.stoploss_last_update = arrow.utcnow().shift(minutes=-10).datetime + trade.stoploss_last_update = dt_now() - timedelta(minutes=10) assert freqtrade.handle_stoploss_on_exchange(trade) is False # Canceled Stoploss filled partially ... @@ -1632,7 +1632,7 @@ def test_handle_stoploss_on_exchange_trailing( trade.is_open = True trade.open_order_id = None trade.stoploss_order_id = '100' - trade.stoploss_last_update = arrow.utcnow().shift(minutes=-20).datetime + trade.stoploss_last_update = dt_now() - timedelta(minutes=20) trade.orders.append( Order( ft_order_side='stoploss', @@ -1763,7 +1763,7 @@ def test_handle_stoploss_on_exchange_trailing_error( trade.open_order_id = None trade.stoploss_order_id = "abcd" trade.stop_loss = 0.2 - trade.stoploss_last_update = arrow.utcnow().shift(minutes=-601).datetime.replace(tzinfo=None) + trade.stoploss_last_update = (dt_now() - timedelta(minutes=601)).replace(tzinfo=None) trade.is_short = is_short stoploss_order_hanging = { @@ -1787,7 +1787,7 @@ def test_handle_stoploss_on_exchange_trailing_error( assert stoploss.call_count == 1 # Fail creating stoploss order - trade.stoploss_last_update = arrow.utcnow().shift(minutes=-601).datetime + trade.stoploss_last_update = dt_now() - timedelta(minutes=601) caplog.clear() cancel_mock = mocker.patch(f'{EXMS}.cancel_stoploss_order') mocker.patch(f'{EXMS}.create_stoploss', side_effect=ExchangeError()) @@ -1876,7 +1876,7 @@ def test_handle_stoploss_on_exchange_custom_stop( trade.is_open = True trade.open_order_id = None trade.stoploss_order_id = '100' - trade.stoploss_last_update = arrow.utcnow().shift(minutes=-601).datetime + trade.stoploss_last_update = dt_now() - timedelta(minutes=601) trade.orders.append( Order( ft_order_side='stoploss', @@ -2965,8 +2965,8 @@ def test_manage_open_orders_exit_usercustom( ) freqtrade = FreqtradeBot(default_conf_usdt) - open_trade_usdt.open_date = arrow.utcnow().shift(hours=-5).datetime - open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime + open_trade_usdt.open_date = dt_now() - timedelta(hours=5) + open_trade_usdt.close_date = dt_now() - timedelta(minutes=601) open_trade_usdt.close_profit_abs = 0.001 Trade.session.add(open_trade_usdt) @@ -3047,8 +3047,8 @@ def test_manage_open_orders_exit( ) freqtrade = FreqtradeBot(default_conf_usdt) - open_trade_usdt.open_date = arrow.utcnow().shift(hours=-5).datetime - open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime + open_trade_usdt.open_date = dt_now() - timedelta(hours=5) + open_trade_usdt.close_date = dt_now() - timedelta(minutes=601) open_trade_usdt.close_profit_abs = 0.001 open_trade_usdt.is_short = is_short @@ -3088,8 +3088,8 @@ def test_check_handle_cancelled_exit( ) freqtrade = FreqtradeBot(default_conf_usdt) - open_trade_usdt.open_date = arrow.utcnow().shift(hours=-5).datetime - open_trade_usdt.close_date = arrow.utcnow().shift(minutes=-601).datetime + open_trade_usdt.open_date = dt_now() - timedelta(hours=5) + open_trade_usdt.close_date = dt_now() - timedelta(minutes=601) open_trade_usdt.is_short = is_short Trade.session.add(open_trade_usdt) @@ -3417,7 +3417,7 @@ def test_handle_cancel_exit_limit(mocker, default_conf_usdt, fee) -> None: exchange='binance', open_rate=0.245441, open_order_id="sell_123456", - open_date=arrow.utcnow().shift(days=-2).datetime, + open_date=dt_now() - timedelta(days=2), fee_open=fee.return_value, fee_close=fee.return_value, close_rate=0.555,