2018-07-29 14:09:44 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, C0103
|
2018-02-13 03:45:59 +00:00
|
|
|
# pragma pylint: disable=protected-access, unused-argument, invalid-name
|
|
|
|
# pragma pylint: disable=too-many-lines, too-many-arguments
|
|
|
|
|
2021-06-17 04:57:35 +00:00
|
|
|
import logging
|
2017-11-05 15:26:03 +00:00
|
|
|
import re
|
2022-01-15 23:19:21 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2021-04-14 21:20:10 +00:00
|
|
|
from functools import reduce
|
2019-04-08 17:59:54 +00:00
|
|
|
from random import choice, randint
|
|
|
|
from string import ascii_uppercase
|
2020-12-24 06:39:46 +00:00
|
|
|
from unittest.mock import ANY, MagicMock
|
2017-09-08 17:25:12 +00:00
|
|
|
|
2018-07-12 15:27:40 +00:00
|
|
|
import arrow
|
2018-07-12 15:50:11 +00:00
|
|
|
import pytest
|
2020-12-20 16:22:23 +00:00
|
|
|
from telegram import Chat, Message, ReplyKeyboardMarkup, Update
|
2021-06-19 07:31:34 +00:00
|
|
|
from telegram.error import BadRequest, NetworkError, TelegramError
|
2017-09-08 17:25:12 +00:00
|
|
|
|
2017-11-09 22:51:32 +00:00
|
|
|
from freqtrade import __version__
|
2020-08-26 20:17:43 +00:00
|
|
|
from freqtrade.constants import CANCEL_REASON
|
2019-03-27 15:04:05 +00:00
|
|
|
from freqtrade.edge import PairInfo
|
2022-03-25 07:36:03 +00:00
|
|
|
from freqtrade.enums import ExitType, RPCMessageType, RunMode, SignalDirection, State
|
2020-12-22 12:01:01 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2018-02-13 03:45:59 +00:00
|
|
|
from freqtrade.freqtradebot import FreqtradeBot
|
2020-08-14 17:36:12 +00:00
|
|
|
from freqtrade.loggers import setup_logging
|
2020-10-25 09:54:30 +00:00
|
|
|
from freqtrade.persistence import PairLocks, Trade
|
2022-02-03 18:41:45 +00:00
|
|
|
from freqtrade.persistence.models import Order
|
2021-06-09 17:51:44 +00:00
|
|
|
from freqtrade.rpc import RPC
|
2021-11-27 08:53:05 +00:00
|
|
|
from freqtrade.rpc.rpc import RPCException
|
2018-07-04 07:31:35 +00:00
|
|
|
from freqtrade.rpc.telegram import Telegram, authorized_only
|
2021-09-21 18:18:14 +00:00
|
|
|
from tests.conftest import (CURRENT_TEST_STRATEGY, create_mock_trades, get_patched_freqtradebot,
|
|
|
|
log_has, log_has_re, patch_exchange, patch_get_signal, patch_whitelist)
|
2018-02-01 06:05:23 +00:00
|
|
|
|
2017-09-08 17:25:12 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
class DummyCls(Telegram):
|
|
|
|
"""
|
|
|
|
Dummy class for testing the Telegram @authorized_only decorator
|
|
|
|
"""
|
2021-09-20 02:24:22 +00:00
|
|
|
|
2020-12-24 08:01:53 +00:00
|
|
|
def __init__(self, rpc: RPC, config) -> None:
|
|
|
|
super().__init__(rpc, config)
|
2018-02-13 03:45:59 +00:00
|
|
|
self.state = {'called': False}
|
2017-09-08 17:25:12 +00:00
|
|
|
|
2018-06-08 22:15:04 +00:00
|
|
|
def _init(self):
|
|
|
|
pass
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
@authorized_only
|
|
|
|
def dummy_handler(self, *args, **kwargs) -> None:
|
|
|
|
"""
|
|
|
|
Fake method that only change the state of the object
|
|
|
|
"""
|
|
|
|
self.state['called'] = True
|
2017-11-09 19:02:41 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
@authorized_only
|
|
|
|
def dummy_exception(self, *args, **kwargs) -> None:
|
|
|
|
"""
|
|
|
|
Fake method that throw an exception
|
|
|
|
"""
|
|
|
|
raise Exception('test')
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
|
2021-07-23 13:43:27 +00:00
|
|
|
def get_telegram_testobject(mocker, default_conf, mock=True, ftbot=None):
|
2020-12-24 06:29:26 +00:00
|
|
|
msg_mock = MagicMock()
|
|
|
|
if mock:
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
2021-07-23 11:39:18 +00:00
|
|
|
if not ftbot:
|
|
|
|
ftbot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
rpc = RPC(ftbot)
|
2020-12-24 08:01:53 +00:00
|
|
|
telegram = Telegram(rpc, default_conf)
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2021-07-23 11:39:18 +00:00
|
|
|
return telegram, ftbot, msg_mock
|
2020-12-24 06:10:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_telegram__init__(default_conf, mocker) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock())
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, _ = get_telegram_testobject(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert telegram._config == default_conf
|
2017-11-07 21:27:44 +00:00
|
|
|
|
|
|
|
|
2020-07-20 17:11:15 +00:00
|
|
|
def test_telegram_init(default_conf, mocker, caplog) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
start_polling = MagicMock()
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock(return_value=start_polling))
|
2017-11-07 21:27:44 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
get_telegram_testobject(mocker, default_conf, mock=False)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert start_polling.call_count == 0
|
2017-11-07 21:27:44 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
# number of handles registered
|
2018-06-09 02:29:48 +00:00
|
|
|
assert start_polling.dispatcher.add_handler.call_count > 0
|
2018-02-13 03:45:59 +00:00
|
|
|
assert start_polling.start_polling.call_count == 1
|
2017-11-07 21:27:44 +00:00
|
|
|
|
2020-06-06 15:28:00 +00:00
|
|
|
message_str = ("rpc.telegram is listening for following commands: [['status'], ['profit'], "
|
2022-01-27 05:31:45 +00:00
|
|
|
"['balance'], ['start'], ['stop'], "
|
|
|
|
"['forcesell', 'forceexit'], ['forcebuy', 'forcelong'], ['forceshort'], "
|
|
|
|
"['trades'], ['delete'], ['performance'], "
|
2022-04-03 08:39:35 +00:00
|
|
|
"['buys', 'entries'], ['sells', 'exits'], ['mix_tags'], "
|
2021-11-04 19:47:01 +00:00
|
|
|
"['stats'], ['daily'], ['weekly'], ['monthly'], "
|
|
|
|
"['count'], ['locks'], ['unlock', 'delete_locks'], "
|
|
|
|
"['reload_config', 'reload_conf'], ['show_config', 'show_conf'], "
|
2021-12-11 15:09:20 +00:00
|
|
|
"['stopbuy'], ['whitelist'], ['blacklist'], ['blacklist_delete', 'bl_delete'], "
|
2022-01-24 11:56:52 +00:00
|
|
|
"['logs'], ['edge'], ['health'], ['help'], ['version']"
|
2020-12-09 19:26:11 +00:00
|
|
|
"]")
|
2017-11-07 21:27:44 +00:00
|
|
|
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has(message_str, caplog)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
2020-10-12 17:44:13 +00:00
|
|
|
def test_cleanup(default_conf, mocker, ) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
updater_mock = MagicMock()
|
|
|
|
updater_mock.stop = MagicMock()
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Updater', updater_mock)
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, _ = get_telegram_testobject(mocker, default_conf, mock=False)
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram.cleanup()
|
|
|
|
assert telegram._updater.stop.call_count == 1
|
|
|
|
|
|
|
|
|
2020-10-12 17:44:13 +00:00
|
|
|
def test_authorized_only(default_conf, mocker, caplog, update) -> None:
|
2019-09-11 06:52:09 +00:00
|
|
|
patch_exchange(mocker)
|
2021-06-17 04:57:35 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
2021-07-23 11:39:18 +00:00
|
|
|
bot = FreqtradeBot(default_conf)
|
|
|
|
rpc = RPC(bot)
|
2020-12-24 08:01:53 +00:00
|
|
|
dummy = DummyCls(rpc, default_conf)
|
|
|
|
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(bot)
|
2019-09-02 18:42:39 +00:00
|
|
|
dummy.dummy_handler(update=update, context=MagicMock())
|
2018-02-13 03:45:59 +00:00
|
|
|
assert dummy.state['called'] is True
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has('Executing handler: dummy_handler for chat_id: 0', caplog)
|
|
|
|
assert not log_has('Rejected unauthorized message from: 0', caplog)
|
|
|
|
assert not log_has('Exception occurred within Telegram module', caplog)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2017-11-07 21:27:44 +00:00
|
|
|
|
2018-03-02 14:48:18 +00:00
|
|
|
def test_authorized_only_unauthorized(default_conf, mocker, caplog) -> None:
|
2019-09-11 06:52:09 +00:00
|
|
|
patch_exchange(mocker)
|
2021-06-17 04:57:35 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-02-13 03:45:59 +00:00
|
|
|
chat = Chat(0xdeadbeef, 0)
|
|
|
|
update = Update(randint(1, 100))
|
2020-10-12 17:44:13 +00:00
|
|
|
update.message = Message(randint(1, 100), datetime.utcnow(), chat)
|
2017-11-07 21:27:44 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
2021-07-23 11:39:18 +00:00
|
|
|
bot = FreqtradeBot(default_conf)
|
|
|
|
rpc = RPC(bot)
|
2020-12-24 08:01:53 +00:00
|
|
|
dummy = DummyCls(rpc, default_conf)
|
|
|
|
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(bot)
|
2019-09-02 18:42:39 +00:00
|
|
|
dummy.dummy_handler(update=update, context=MagicMock())
|
2018-02-13 03:45:59 +00:00
|
|
|
assert dummy.state['called'] is False
|
2019-08-11 18:17:22 +00:00
|
|
|
assert not log_has('Executing handler: dummy_handler for chat_id: 3735928559', caplog)
|
|
|
|
assert log_has('Rejected unauthorized message from: 3735928559', caplog)
|
|
|
|
assert not log_has('Exception occurred within Telegram module', caplog)
|
2017-11-07 21:27:44 +00:00
|
|
|
|
2017-11-09 19:02:41 +00:00
|
|
|
|
2020-10-12 17:44:13 +00:00
|
|
|
def test_authorized_only_exception(default_conf, mocker, caplog, update) -> None:
|
2018-06-17 19:46:56 +00:00
|
|
|
patch_exchange(mocker)
|
2018-03-02 14:48:18 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2021-07-23 11:39:18 +00:00
|
|
|
bot = FreqtradeBot(default_conf)
|
|
|
|
rpc = RPC(bot)
|
2020-12-24 08:01:53 +00:00
|
|
|
dummy = DummyCls(rpc, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(bot)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
dummy.dummy_exception(update=update, context=MagicMock())
|
2018-02-13 03:45:59 +00:00
|
|
|
assert dummy.state['called'] is False
|
2019-08-11 18:17:22 +00:00
|
|
|
assert not log_has('Executing handler: dummy_handler for chat_id: 0', caplog)
|
|
|
|
assert not log_has('Rejected unauthorized message from: 0', caplog)
|
|
|
|
assert log_has('Exception occurred within Telegram module', caplog)
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
|
2020-10-12 17:44:13 +00:00
|
|
|
def test_telegram_status(default_conf, update, mocker) -> None:
|
2019-11-23 14:20:53 +00:00
|
|
|
update.message.chat.id = "123"
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
2019-11-23 14:20:53 +00:00
|
|
|
default_conf['telegram']['chat_id'] = "123"
|
2017-11-09 19:02:41 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
status_table = MagicMock()
|
2020-12-24 08:01:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._status_table', status_table)
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2020-12-24 08:01:53 +00:00
|
|
|
'freqtrade.rpc.rpc.RPC',
|
2018-06-22 01:54:10 +00:00
|
|
|
_rpc_trade_status=MagicMock(return_value=[{
|
|
|
|
'trade_id': 1,
|
|
|
|
'pair': 'ETH/BTC',
|
2019-04-03 14:28:44 +00:00
|
|
|
'base_currency': 'BTC',
|
2019-05-06 04:56:07 +00:00
|
|
|
'open_date': arrow.utcnow(),
|
|
|
|
'close_date': None,
|
2018-06-22 01:54:10 +00:00
|
|
|
'open_rate': 1.099e-05,
|
|
|
|
'close_rate': None,
|
|
|
|
'current_rate': 1.098e-05,
|
|
|
|
'amount': 90.99181074,
|
2019-04-03 14:28:44 +00:00
|
|
|
'stake_amount': 90.99181074,
|
2021-07-21 18:53:15 +00:00
|
|
|
'buy_tag': None,
|
2021-11-21 08:51:16 +00:00
|
|
|
'enter_tag': None,
|
2021-11-11 11:58:38 +00:00
|
|
|
'close_profit_ratio': None,
|
2020-11-03 07:34:12 +00:00
|
|
|
'profit': -0.0059,
|
2021-11-11 11:58:38 +00:00
|
|
|
'profit_ratio': -0.0059,
|
2020-11-03 07:34:12 +00:00
|
|
|
'initial_stop_loss_abs': 1.098e-05,
|
|
|
|
'stop_loss_abs': 1.099e-05,
|
2022-04-03 09:17:01 +00:00
|
|
|
'exit_order_status': None,
|
2021-11-11 11:58:38 +00:00
|
|
|
'initial_stop_loss_ratio': -0.0005,
|
2020-07-14 18:16:18 +00:00
|
|
|
'stoploss_current_dist': 1e-08,
|
2021-11-11 11:58:38 +00:00
|
|
|
'stoploss_current_dist_ratio': -0.0002,
|
|
|
|
'stop_loss_ratio': -0.0001,
|
2020-11-03 07:34:12 +00:00
|
|
|
'open_order': '(limit buy rem=0.00000000)',
|
2021-12-29 13:24:12 +00:00
|
|
|
'is_open': True,
|
2022-02-11 16:02:04 +00:00
|
|
|
'is_short': False,
|
|
|
|
'filled_entry_orders': [],
|
2022-02-27 15:54:14 +00:00
|
|
|
'orders': []
|
2018-06-22 01:54:10 +00:00
|
|
|
}]),
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._status(update=update, context=MagicMock())
|
2018-06-22 01:54:10 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
context = MagicMock()
|
2021-01-19 18:44:16 +00:00
|
|
|
# /status table
|
|
|
|
context.args = ["table"]
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._status(update=update, context=context)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert status_table.call_count == 1
|
|
|
|
|
|
|
|
|
2022-02-03 18:41:45 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_telegram_status_multi_entry(default_conf, update, mocker, fee) -> None:
|
|
|
|
update.message.chat.id = "123"
|
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
default_conf['telegram']['chat_id'] = "123"
|
|
|
|
default_conf['position_adjustment_enable'] = True
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_order=MagicMock(return_value=None),
|
|
|
|
get_rate=MagicMock(return_value=0.22),
|
|
|
|
)
|
|
|
|
|
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
|
|
|
|
create_mock_trades(fee)
|
|
|
|
trades = Trade.get_open_trades()
|
|
|
|
trade = trades[0]
|
2022-02-26 07:19:45 +00:00
|
|
|
# Average may be empty on some exchanges
|
|
|
|
trade.orders[0].average = 0
|
2022-02-03 18:41:45 +00:00
|
|
|
trade.orders.append(Order(
|
|
|
|
order_id='5412vbb',
|
|
|
|
ft_order_side='buy',
|
|
|
|
ft_pair=trade.pair,
|
|
|
|
ft_is_open=False,
|
|
|
|
status="closed",
|
|
|
|
symbol=trade.pair,
|
|
|
|
order_type="market",
|
|
|
|
side="buy",
|
|
|
|
price=trade.open_rate * 0.95,
|
2022-02-26 07:19:45 +00:00
|
|
|
average=0,
|
2022-02-03 18:41:45 +00:00
|
|
|
filled=trade.amount,
|
|
|
|
remaining=0,
|
|
|
|
cost=trade.amount,
|
|
|
|
order_date=trade.open_date,
|
|
|
|
order_filled_date=trade.open_date,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
trade.recalc_trade_from_orders()
|
|
|
|
Trade.commit()
|
|
|
|
|
|
|
|
telegram._status(update=update, context=MagicMock())
|
|
|
|
assert msg_mock.call_count == 4
|
|
|
|
msg = msg_mock.call_args_list[0][0][0]
|
2022-02-07 07:31:35 +00:00
|
|
|
assert re.search(r'Number of Entries.*2', msg)
|
|
|
|
assert re.search(r'Average Entry Price', msg)
|
2022-02-03 18:41:45 +00:00
|
|
|
assert re.search(r'Order filled at', msg)
|
2022-02-06 15:26:00 +00:00
|
|
|
assert re.search(r'Close Date:', msg) is None
|
|
|
|
assert re.search(r'Close Profit:', msg) is None
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_telegram_status_closed_trade(default_conf, update, mocker, fee) -> None:
|
|
|
|
update.message.chat.id = "123"
|
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
default_conf['telegram']['chat_id'] = "123"
|
|
|
|
default_conf['position_adjustment_enable'] = True
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_order=MagicMock(return_value=None),
|
|
|
|
get_rate=MagicMock(return_value=0.22),
|
|
|
|
)
|
|
|
|
|
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
|
|
|
|
create_mock_trades(fee)
|
|
|
|
trades = Trade.get_trades([Trade.is_open.is_(False)])
|
|
|
|
trade = trades[0]
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = [str(trade.id)]
|
|
|
|
telegram._status(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
msg = msg_mock.call_args_list[0][0][0]
|
|
|
|
assert re.search(r'Close Date:', msg)
|
|
|
|
assert re.search(r'Close Profit:', msg)
|
2022-02-03 18:41:45 +00:00
|
|
|
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
def test_status_handle(default_conf, update, ticker, fee, mocker) -> None:
|
2021-01-19 18:44:16 +00:00
|
|
|
default_conf['max_open_trades'] = 3
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=True),
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
status_table = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_status_table=status_table,
|
|
|
|
)
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2019-10-26 11:24:26 +00:00
|
|
|
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
2018-10-10 18:58:21 +00:00
|
|
|
# Status is also enabled when stopped
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._status(update=update, context=MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2018-10-10 18:58:21 +00:00
|
|
|
assert 'no active trade' in msg_mock.call_args_list[0][0][0]
|
2017-11-09 19:02:41 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._status(update=update, context=MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'no active trade' in msg_mock.call_args_list[0][0][0]
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2017-10-31 23:26:32 +00:00
|
|
|
# Trigger status while we have a fulfilled order for the open trade
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._status(update=update, context=MagicMock())
|
2017-10-31 23:26:32 +00:00
|
|
|
|
2019-03-27 20:12:57 +00:00
|
|
|
# close_rate should not be included in the message as the trade is not closed
|
|
|
|
# and no line should be empty
|
|
|
|
lines = msg_mock.call_args_list[0][0][0].split('\n')
|
|
|
|
assert '' not in lines
|
|
|
|
assert 'Close Rate' not in ''.join(lines)
|
2019-03-27 20:32:56 +00:00
|
|
|
assert 'Close Profit' not in ''.join(lines)
|
2019-03-27 20:12:57 +00:00
|
|
|
|
2021-01-19 18:44:16 +00:00
|
|
|
assert msg_mock.call_count == 3
|
2019-02-25 19:17:27 +00:00
|
|
|
assert 'ETH/BTC' in msg_mock.call_args_list[0][0][0]
|
2021-01-19 18:44:16 +00:00
|
|
|
assert 'LTC/BTC' in msg_mock.call_args_list[1][0][0]
|
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["2", "3"]
|
|
|
|
|
|
|
|
telegram._status(update=update, context=context)
|
|
|
|
|
|
|
|
lines = msg_mock.call_args_list[0][0][0].split('\n')
|
|
|
|
assert '' not in lines
|
|
|
|
assert 'Close Rate' not in ''.join(lines)
|
|
|
|
assert 'Close Profit' not in ''.join(lines)
|
|
|
|
|
|
|
|
assert msg_mock.call_count == 2
|
|
|
|
assert 'LTC/BTC' in msg_mock.call_args_list[0][0][0]
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2017-11-05 15:26:03 +00:00
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
def test_status_table_handle(default_conf, update, ticker, fee, mocker) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['stake_amount'] = 15.0
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
2018-10-10 18:58:21 +00:00
|
|
|
# Status table is also enabled when stopped
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._status_table(update=update, context=MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2019-12-29 18:51:47 +00:00
|
|
|
assert 'no active trade' in msg_mock.call_args_list[0][0][0]
|
2017-11-09 19:02:41 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._status_table(update=update, context=MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2019-12-29 18:51:47 +00:00
|
|
|
assert 'no active trade' in msg_mock.call_args_list[0][0][0]
|
2017-11-09 19:02:41 +00:00
|
|
|
msg_mock.reset_mock()
|
2017-11-05 15:26:03 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2017-11-05 15:26:03 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._status_table(update=update, context=MagicMock())
|
2017-11-05 15:26:03 +00:00
|
|
|
|
2017-11-07 19:12:56 +00:00
|
|
|
text = re.sub('</?pre>', '', msg_mock.call_args_list[-1][0][0])
|
2017-11-05 15:26:03 +00:00
|
|
|
line = text.split("\n")
|
|
|
|
fields = re.sub('[ ]+', ' ', line[2].strip()).split(' ')
|
|
|
|
|
|
|
|
assert int(fields[0]) == 1
|
2021-12-29 13:48:50 +00:00
|
|
|
assert 'L' in fields[1]
|
|
|
|
assert 'ETH/BTC' in fields[2]
|
2017-11-18 20:30:31 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2017-11-05 15:26:03 +00:00
|
|
|
|
|
|
|
|
2018-04-15 10:09:12 +00:00
|
|
|
def test_daily_handle(default_conf, update, ticker, limit_buy_order, fee,
|
2019-10-26 11:24:26 +00:00
|
|
|
limit_sell_order, mocker) -> None:
|
2019-08-13 08:15:31 +00:00
|
|
|
default_conf['max_open_trades'] = 1
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch(
|
2018-07-21 18:49:57 +00:00
|
|
|
'freqtrade.rpc.rpc.CryptoToFiatConverter._find_price',
|
2018-02-13 03:45:59 +00:00
|
|
|
return_value=15000.0
|
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2018-02-13 03:45:59 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_buy_order, limit_buy_order['symbol'], 'buy')
|
|
|
|
trade.update_trade(oobj)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobjs = Order.parse_from_ccxt_object(limit_sell_order, limit_sell_order['symbol'], 'sell')
|
|
|
|
trade.update_trade(oobjs)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
|
|
|
|
|
|
|
# Try valid data
|
2019-09-02 18:42:39 +00:00
|
|
|
# /daily 2
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["2"]
|
|
|
|
telegram._daily(update=update, context=context)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2021-11-05 19:24:40 +00:00
|
|
|
assert "Daily Profit over the last 2 days</b>:" in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'Day ' in msg_mock.call_args_list[0][0][0]
|
2018-02-13 03:45:59 +00:00
|
|
|
assert str(datetime.utcnow().date()) in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.00006217 BTC') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.933 USD') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 1 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
2020-12-05 07:16:40 +00:00
|
|
|
# Reset msg_mock
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
context.args = []
|
|
|
|
telegram._daily(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
2021-11-05 19:24:40 +00:00
|
|
|
assert "Daily Profit over the last 7 days</b>:" in msg_mock.call_args_list[0][0][0]
|
2020-12-05 07:16:40 +00:00
|
|
|
assert str(datetime.utcnow().date()) in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.00006217 BTC') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.933 USD') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 1 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0 trade') in msg_mock.call_args_list[0][0][0]
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
# Reset msg_mock
|
|
|
|
msg_mock.reset_mock()
|
2019-08-13 08:15:31 +00:00
|
|
|
freqtradebot.config['max_open_trades'] = 2
|
2018-02-13 03:45:59 +00:00
|
|
|
# Add two other trades
|
2019-12-30 19:50:56 +00:00
|
|
|
n = freqtradebot.enter_positions()
|
|
|
|
assert n == 2
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
trades = Trade.query.all()
|
|
|
|
for trade in trades:
|
2022-02-11 06:33:46 +00:00
|
|
|
trade.update_trade(oobj)
|
|
|
|
trade.update_trade(oobjs)
|
2018-02-13 03:45:59 +00:00
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
# /daily 1
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["1"]
|
|
|
|
telegram._daily(update=update, context=context)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert str(' 0.00018651 BTC') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 2.798 USD') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 3 trades') in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
|
|
|
def test_daily_wrong_input(default_conf, update, ticker, mocker) -> None:
|
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
# Try invalid data
|
|
|
|
msg_mock.reset_mock()
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2019-09-02 18:42:39 +00:00
|
|
|
# /daily -2
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["-2"]
|
|
|
|
telegram._daily(update=update, context=context)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'must be an integer greater than 0' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
# Try invalid data
|
|
|
|
msg_mock.reset_mock()
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2019-09-02 18:42:39 +00:00
|
|
|
# /daily today
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["today"]
|
|
|
|
telegram._daily(update=update, context=context)
|
2021-11-05 19:24:40 +00:00
|
|
|
assert str('Daily Profit over the last 7 days</b>:') in msg_mock.call_args_list[0][0][0]
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
2021-11-04 19:47:01 +00:00
|
|
|
def test_weekly_handle(default_conf, update, ticker, limit_buy_order, fee,
|
|
|
|
limit_sell_order, mocker) -> None:
|
|
|
|
default_conf['max_open_trades'] = 1
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.rpc.rpc.CryptoToFiatConverter._find_price',
|
|
|
|
return_value=15000.0
|
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
|
|
|
|
patch_get_signal(freqtradebot)
|
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
freqtradebot.enter_positions()
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_buy_order, limit_buy_order['symbol'], 'buy')
|
|
|
|
trade.update_trade(oobj)
|
2021-11-04 19:47:01 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobjs = Order.parse_from_ccxt_object(limit_sell_order, limit_sell_order['symbol'], 'sell')
|
|
|
|
trade.update_trade(oobjs)
|
2021-11-04 19:47:01 +00:00
|
|
|
|
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
|
|
|
|
|
|
|
# Try valid data
|
|
|
|
# /weekly 2
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["2"]
|
|
|
|
telegram._weekly(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
2021-11-05 19:24:40 +00:00
|
|
|
assert "Weekly Profit over the last 2 weeks (starting from Monday)</b>:" \
|
|
|
|
in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'Monday ' in msg_mock.call_args_list[0][0][0]
|
|
|
|
today = datetime.utcnow().date()
|
|
|
|
first_iso_day_of_current_week = today - timedelta(days=today.weekday())
|
|
|
|
assert str(first_iso_day_of_current_week) in msg_mock.call_args_list[0][0][0]
|
2021-11-04 19:47:01 +00:00
|
|
|
assert str(' 0.00006217 BTC') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.933 USD') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 1 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
# Reset msg_mock
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
context.args = []
|
|
|
|
telegram._weekly(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
2021-11-05 19:24:40 +00:00
|
|
|
assert "Weekly Profit over the last 8 weeks (starting from Monday)</b>:" \
|
|
|
|
in msg_mock.call_args_list[0][0][0]
|
2021-11-04 19:47:01 +00:00
|
|
|
assert 'Weekly' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.00006217 BTC') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.933 USD') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 1 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
# Reset msg_mock
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
freqtradebot.config['max_open_trades'] = 2
|
|
|
|
# Add two other trades
|
|
|
|
n = freqtradebot.enter_positions()
|
|
|
|
assert n == 2
|
|
|
|
|
|
|
|
trades = Trade.query.all()
|
|
|
|
for trade in trades:
|
2022-02-11 06:33:46 +00:00
|
|
|
trade.update_trade(oobj)
|
|
|
|
trade.update_trade(oobjs)
|
2021-11-04 19:47:01 +00:00
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
|
|
|
|
2021-11-05 21:33:06 +00:00
|
|
|
# /weekly 1
|
2021-11-04 19:47:01 +00:00
|
|
|
# By default, the 8 previous weeks are shown
|
|
|
|
# So the previous modified trade should be excluded from the stats
|
|
|
|
context = MagicMock()
|
2021-11-05 21:33:06 +00:00
|
|
|
context.args = ["1"]
|
2021-11-04 19:47:01 +00:00
|
|
|
telegram._weekly(update=update, context=context)
|
2021-11-05 21:33:06 +00:00
|
|
|
assert str(' 0.00018651 BTC') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 2.798 USD') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 3 trades') in msg_mock.call_args_list[0][0][0]
|
2021-11-04 19:47:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_weekly_wrong_input(default_conf, update, ticker, mocker) -> None:
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker
|
|
|
|
)
|
|
|
|
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
patch_get_signal(freqtradebot)
|
|
|
|
|
|
|
|
# Try invalid data
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
freqtradebot.state = State.RUNNING
|
2021-11-05 21:33:06 +00:00
|
|
|
# /weekly -3
|
2021-11-04 19:47:01 +00:00
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["-3"]
|
|
|
|
telegram._weekly(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'must be an integer greater than 0' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
# Try invalid data
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
freqtradebot.state = State.RUNNING
|
2021-11-05 19:24:40 +00:00
|
|
|
# /weekly this week
|
2021-11-04 19:47:01 +00:00
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["this week"]
|
|
|
|
telegram._weekly(update=update, context=context)
|
2021-11-05 19:24:40 +00:00
|
|
|
assert str('Weekly Profit over the last 8 weeks (starting from Monday)</b>:') \
|
2022-03-03 19:51:52 +00:00
|
|
|
in msg_mock.call_args_list[0][0][0]
|
2021-11-04 19:47:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_monthly_handle(default_conf, update, ticker, limit_buy_order, fee,
|
|
|
|
limit_sell_order, mocker) -> None:
|
|
|
|
default_conf['max_open_trades'] = 1
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.rpc.rpc.CryptoToFiatConverter._find_price',
|
|
|
|
return_value=15000.0
|
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
|
|
|
|
patch_get_signal(freqtradebot)
|
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
freqtradebot.enter_positions()
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_buy_order, limit_buy_order['symbol'], 'buy')
|
|
|
|
trade.update_trade(oobj)
|
2021-11-04 19:47:01 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobjs = Order.parse_from_ccxt_object(limit_sell_order, limit_sell_order['symbol'], 'sell')
|
|
|
|
trade.update_trade(oobjs)
|
2021-11-04 19:47:01 +00:00
|
|
|
|
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
|
|
|
|
|
|
|
# Try valid data
|
|
|
|
# /monthly 2
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["2"]
|
|
|
|
telegram._monthly(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
2021-11-05 19:24:40 +00:00
|
|
|
assert 'Monthly Profit over the last 2 months</b>:' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'Month ' in msg_mock.call_args_list[0][0][0]
|
|
|
|
today = datetime.utcnow().date()
|
2022-01-01 09:37:43 +00:00
|
|
|
current_month = f"{today.year}-{today.month:02} "
|
2021-11-05 19:24:40 +00:00
|
|
|
assert current_month in msg_mock.call_args_list[0][0][0]
|
2021-11-04 19:47:01 +00:00
|
|
|
assert str(' 0.00006217 BTC') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.933 USD') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 1 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
# Reset msg_mock
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
context.args = []
|
|
|
|
telegram._monthly(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
2021-11-05 21:33:06 +00:00
|
|
|
# Default to 6 months
|
2021-11-05 19:24:40 +00:00
|
|
|
assert 'Monthly Profit over the last 6 months</b>:' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'Month ' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert current_month in msg_mock.call_args_list[0][0][0]
|
2021-11-04 19:47:01 +00:00
|
|
|
assert str(' 0.00006217 BTC') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.933 USD') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 1 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0 trade') in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
# Reset msg_mock
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
freqtradebot.config['max_open_trades'] = 2
|
|
|
|
# Add two other trades
|
|
|
|
n = freqtradebot.enter_positions()
|
|
|
|
assert n == 2
|
|
|
|
|
|
|
|
trades = Trade.query.all()
|
|
|
|
for trade in trades:
|
2022-02-11 06:33:46 +00:00
|
|
|
trade.update_trade(oobj)
|
|
|
|
trade.update_trade(oobjs)
|
2021-11-04 19:47:01 +00:00
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
|
|
|
|
2021-11-05 21:33:06 +00:00
|
|
|
# /monthly 12
|
2021-11-04 19:47:01 +00:00
|
|
|
context = MagicMock()
|
2021-11-05 21:33:06 +00:00
|
|
|
context.args = ["12"]
|
2021-11-04 19:47:01 +00:00
|
|
|
telegram._monthly(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
2021-11-05 21:33:06 +00:00
|
|
|
assert 'Monthly Profit over the last 12 months</b>:' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 0.00018651 BTC') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 2.798 USD') in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert str(' 3 trades') in msg_mock.call_args_list[0][0][0]
|
2021-11-04 19:47:01 +00:00
|
|
|
|
2021-11-05 21:33:06 +00:00
|
|
|
# The one-digit months should contain a zero, Eg: September 2021 = "2021-09"
|
|
|
|
# Since we loaded the last 12 months, any month should appear
|
|
|
|
assert str('-09') in msg_mock.call_args_list[0][0][0]
|
2021-11-04 19:47:01 +00:00
|
|
|
|
|
|
|
|
2021-11-05 19:24:40 +00:00
|
|
|
def test_monthly_wrong_input(default_conf, update, ticker, mocker) -> None:
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker
|
|
|
|
)
|
|
|
|
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
patch_get_signal(freqtradebot)
|
|
|
|
|
|
|
|
# Try invalid data
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
freqtradebot.state = State.RUNNING
|
2021-11-05 21:33:06 +00:00
|
|
|
# /monthly -3
|
2021-11-05 19:24:40 +00:00
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["-3"]
|
|
|
|
telegram._monthly(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'must be an integer greater than 0' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
# Try invalid data
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
freqtradebot.state = State.RUNNING
|
|
|
|
# /monthly february
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["february"]
|
|
|
|
telegram._monthly(update=update, context=context)
|
|
|
|
assert str('Monthly Profit over the last 6 months</b>:') in msg_mock.call_args_list[0][0][0]
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
2018-04-15 10:09:12 +00:00
|
|
|
def test_profit_handle(default_conf, update, ticker, ticker_sell_up, fee,
|
2019-10-26 11:24:26 +00:00
|
|
|
limit_buy_order, limit_sell_order, mocker) -> None:
|
2018-07-21 18:49:57 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._profit(update=update, context=MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2020-05-30 17:42:09 +00:00
|
|
|
assert 'No trades yet.' in msg_mock.call_args_list[0][0][0]
|
2017-11-09 19:02:41 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2017-11-22 19:51:25 +00:00
|
|
|
trade = Trade.query.first()
|
2017-10-31 23:26:32 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_buy_order, limit_buy_order['symbol'], 'buy')
|
|
|
|
trade.update_trade(oobj)
|
|
|
|
|
2021-06-08 18:35:25 +00:00
|
|
|
context = MagicMock()
|
|
|
|
# Test with invalid 2nd argument (should silently pass)
|
|
|
|
context.args = ["aaa"]
|
|
|
|
telegram._profit(update=update, context=context)
|
2017-11-18 20:30:31 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2020-05-29 07:38:12 +00:00
|
|
|
assert 'No closed trade' in msg_mock.call_args_list[-1][0][0]
|
|
|
|
assert '*ROI:* All trades' in msg_mock.call_args_list[-1][0][0]
|
2021-07-14 18:59:47 +00:00
|
|
|
mocker.patch('freqtrade.wallets.Wallets.get_starting_balance', return_value=0.01)
|
2022-02-19 15:39:47 +00:00
|
|
|
assert ('∙ `-0.000005 BTC (-0.50%) (-0.0 \N{GREEK CAPITAL LETTER SIGMA}%)`'
|
2020-06-03 17:40:49 +00:00
|
|
|
in msg_mock.call_args_list[-1][0][0])
|
2017-11-09 19:02:41 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
|
2017-12-17 21:07:56 +00:00
|
|
|
# Update the ticker with a market going up
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', ticker_sell_up)
|
2022-02-11 06:33:46 +00:00
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
|
|
|
oobj = Order.parse_from_ccxt_object(limit_sell_order, limit_sell_order['symbol'], 'sell')
|
|
|
|
trade.update_trade(oobj)
|
2017-10-31 23:26:32 +00:00
|
|
|
|
2022-01-15 23:19:21 +00:00
|
|
|
trade.close_date = datetime.now(timezone.utc)
|
2017-10-01 12:25:10 +00:00
|
|
|
trade.is_open = False
|
2022-01-15 23:19:21 +00:00
|
|
|
Trade.commit()
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2022-01-15 23:19:21 +00:00
|
|
|
context.args = [3]
|
|
|
|
telegram._profit(update=update, context=context)
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2020-05-30 17:28:30 +00:00
|
|
|
assert '*ROI:* Closed trades' in msg_mock.call_args_list[-1][0][0]
|
2021-07-14 18:59:47 +00:00
|
|
|
assert ('∙ `0.00006217 BTC (6.20%) (0.62 \N{GREEK CAPITAL LETTER SIGMA}%)`'
|
2020-06-03 17:40:49 +00:00
|
|
|
in msg_mock.call_args_list[-1][0][0])
|
2017-12-25 07:51:41 +00:00
|
|
|
assert '∙ `0.933 USD`' in msg_mock.call_args_list[-1][0][0]
|
|
|
|
assert '*ROI:* All trades' in msg_mock.call_args_list[-1][0][0]
|
2021-07-14 18:59:47 +00:00
|
|
|
assert ('∙ `0.00006217 BTC (6.20%) (0.62 \N{GREEK CAPITAL LETTER SIGMA}%)`'
|
2020-06-03 17:40:49 +00:00
|
|
|
in msg_mock.call_args_list[-1][0][0])
|
2017-12-25 07:51:41 +00:00
|
|
|
assert '∙ `0.933 USD`' in msg_mock.call_args_list[-1][0][0]
|
|
|
|
|
2018-03-24 19:59:25 +00:00
|
|
|
assert '*Best Performing:* `ETH/BTC: 6.20%`' in msg_mock.call_args_list[-1][0][0]
|
2017-10-01 12:25:10 +00:00
|
|
|
|
|
|
|
|
2021-09-20 02:24:22 +00:00
|
|
|
@pytest.mark.parametrize('is_short', [True, False])
|
2020-12-05 13:48:56 +00:00
|
|
|
def test_telegram_stats(default_conf, update, ticker, ticker_sell_up, fee,
|
2021-09-20 02:24:22 +00:00
|
|
|
limit_buy_order, limit_sell_order, mocker, is_short) -> None:
|
2020-12-05 13:48:56 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2020-12-05 13:48:56 +00:00
|
|
|
|
|
|
|
telegram._stats(update=update, context=MagicMock())
|
|
|
|
assert msg_mock.call_count == 1
|
2021-11-14 09:20:04 +00:00
|
|
|
assert 'No trades yet.' in msg_mock.call_args_list[0][0][0]
|
2020-12-05 13:48:56 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
|
|
|
|
# Create some test data
|
2021-11-16 06:03:33 +00:00
|
|
|
create_mock_trades(fee, is_short=is_short)
|
2020-12-05 13:48:56 +00:00
|
|
|
|
|
|
|
telegram._stats(update=update, context=MagicMock())
|
|
|
|
assert msg_mock.call_count == 1
|
2022-03-24 19:33:47 +00:00
|
|
|
assert 'Exit Reason' in msg_mock.call_args_list[-1][0][0]
|
2020-12-05 13:48:56 +00:00
|
|
|
assert 'ROI' in msg_mock.call_args_list[-1][0][0]
|
|
|
|
assert 'Avg. Duration' in msg_mock.call_args_list[-1][0][0]
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
|
|
|
|
|
2019-11-14 19:12:41 +00:00
|
|
|
def test_telegram_balance_handle(default_conf, update, mocker, rpc_balance, tickers) -> None:
|
2019-12-15 09:39:52 +00:00
|
|
|
default_conf['dry_run'] = False
|
2019-05-11 07:10:54 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value=rpc_balance)
|
2019-11-14 19:12:41 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_tickers', tickers)
|
2019-07-03 18:07:26 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_valid_pair_combination',
|
|
|
|
side_effect=lambda a, b: f"{a}/{b}")
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._balance(update=update, context=MagicMock())
|
2018-02-13 03:45:59 +00:00
|
|
|
result = msg_mock.call_args_list[0][0][0]
|
|
|
|
assert msg_mock.call_count == 1
|
2018-05-15 10:37:34 +00:00
|
|
|
assert '*BTC:*' in result
|
|
|
|
assert '*ETH:*' not in result
|
2021-07-03 19:44:48 +00:00
|
|
|
assert '*USDT:*' not in result
|
|
|
|
assert '*EUR:*' not in result
|
|
|
|
assert '*LTC:*' in result
|
|
|
|
assert '*XRP:*' not in result
|
2018-05-15 10:37:34 +00:00
|
|
|
assert 'Balance:' in result
|
|
|
|
assert 'Est. BTC:' in result
|
2022-02-19 15:39:47 +00:00
|
|
|
assert 'BTC: 12' in result
|
2021-07-04 14:08:46 +00:00
|
|
|
assert "*3 Other Currencies (< 0.0001 BTC):*" in result
|
2021-07-03 19:44:48 +00:00
|
|
|
assert 'BTC: 0.00000309' in result
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
2018-07-29 14:09:44 +00:00
|
|
|
def test_balance_handle_empty_response(default_conf, update, mocker) -> None:
|
2019-12-15 09:39:52 +00:00
|
|
|
default_conf['dry_run'] = False
|
2018-06-17 19:46:56 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value={})
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2019-06-27 05:06:35 +00:00
|
|
|
freqtradebot.config['dry_run'] = False
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._balance(update=update, context=MagicMock())
|
2018-02-13 03:45:59 +00:00
|
|
|
result = msg_mock.call_args_list[0][0][0]
|
|
|
|
assert msg_mock.call_count == 1
|
2022-02-22 19:07:41 +00:00
|
|
|
assert 'Starting capital: `0 BTC' in result
|
2019-06-27 05:06:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_balance_handle_empty_response_dry(default_conf, update, mocker) -> None:
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value={})
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2019-06-27 05:06:35 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._balance(update=update, context=MagicMock())
|
2019-06-27 05:06:35 +00:00
|
|
|
result = msg_mock.call_args_list[0][0][0]
|
|
|
|
assert msg_mock.call_count == 1
|
2019-12-24 05:58:30 +00:00
|
|
|
assert "*Warning:* Simulated balances in Dry Mode." in result
|
2022-02-19 15:39:47 +00:00
|
|
|
assert "Starting capital: `1000 BTC`" in result
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2019-04-08 17:59:54 +00:00
|
|
|
|
|
|
|
def test_balance_handle_too_large_response(default_conf, update, mocker) -> None:
|
|
|
|
balances = []
|
|
|
|
for i in range(100):
|
|
|
|
curr = choice(ascii_uppercase) + choice(ascii_uppercase) + choice(ascii_uppercase)
|
|
|
|
balances.append({
|
|
|
|
'currency': curr,
|
2019-09-05 18:02:01 +00:00
|
|
|
'free': 1.0,
|
|
|
|
'used': 0.5,
|
2019-04-08 17:59:54 +00:00
|
|
|
'balance': i,
|
2019-11-15 05:33:07 +00:00
|
|
|
'est_stake': 1,
|
|
|
|
'stake': 'BTC',
|
2022-02-22 18:30:07 +00:00
|
|
|
'is_position': False,
|
|
|
|
'leverage': 1.0,
|
|
|
|
'position': 0.0,
|
|
|
|
'side': 'long',
|
2019-04-08 17:59:54 +00:00
|
|
|
})
|
|
|
|
mocker.patch('freqtrade.rpc.rpc.RPC._rpc_balance', return_value={
|
|
|
|
'currencies': balances,
|
|
|
|
'total': 100.0,
|
|
|
|
'symbol': 100.0,
|
|
|
|
'value': 1000.0,
|
2021-09-19 23:44:12 +00:00
|
|
|
'starting_capital': 1000,
|
|
|
|
'starting_capital_fiat': 1000,
|
2019-04-08 17:59:54 +00:00
|
|
|
})
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2019-04-08 17:59:54 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._balance(update=update, context=MagicMock())
|
2019-04-08 17:59:54 +00:00
|
|
|
assert msg_mock.call_count > 1
|
2019-04-10 04:59:10 +00:00
|
|
|
# Test if wrap happens around 4000 -
|
|
|
|
# and each single currency-output is around 120 characters long so we need
|
|
|
|
# an offset to avoid random test failures
|
|
|
|
assert len(msg_mock.call_args_list[0][0][0]) < 4096
|
|
|
|
assert len(msg_mock.call_args_list[0][0][0]) > (4096 - 120)
|
2019-04-08 17:59:54 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
def test_start_handle(default_conf, update, mocker) -> None:
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
|
|
|
assert freqtradebot.state == State.STOPPED
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._start(update=update, context=MagicMock())
|
2018-03-20 19:09:42 +00:00
|
|
|
assert freqtradebot.state == State.RUNNING
|
2018-06-08 02:52:50 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_start_handle_already_running(default_conf, update, mocker) -> None:
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
|
|
|
assert freqtradebot.state == State.RUNNING
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._start(update=update, context=MagicMock())
|
2018-03-20 19:09:42 +00:00
|
|
|
assert freqtradebot.state == State.RUNNING
|
2018-02-13 03:45:59 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'already running' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
|
|
|
def test_stop_handle(default_conf, update, mocker) -> None:
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
|
|
|
assert freqtradebot.state == State.RUNNING
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._stop(update=update, context=MagicMock())
|
2018-03-20 19:09:42 +00:00
|
|
|
assert freqtradebot.state == State.STOPPED
|
2018-02-13 03:45:59 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2018-06-22 01:32:45 +00:00
|
|
|
assert 'stopping trader' in msg_mock.call_args_list[0][0][0]
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_stop_handle_already_stopped(default_conf, update, mocker) -> None:
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
|
|
|
assert freqtradebot.state == State.STOPPED
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._stop(update=update, context=MagicMock())
|
2018-03-20 19:09:42 +00:00
|
|
|
assert freqtradebot.state == State.STOPPED
|
2018-02-13 03:45:59 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'already stopped' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
2019-03-17 18:36:02 +00:00
|
|
|
def test_stopbuy_handle(default_conf, update, mocker) -> None:
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2019-03-17 18:36:02 +00:00
|
|
|
|
|
|
|
assert freqtradebot.config['max_open_trades'] != 0
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._stopbuy(update=update, context=MagicMock())
|
2019-03-17 18:36:02 +00:00
|
|
|
assert freqtradebot.config['max_open_trades'] == 0
|
|
|
|
assert msg_mock.call_count == 1
|
2020-06-09 21:03:55 +00:00
|
|
|
assert 'No more buy will occur from now. Run /reload_config to reset.' \
|
2019-03-17 18:36:02 +00:00
|
|
|
in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
2020-06-09 21:03:55 +00:00
|
|
|
def test_reload_config_handle(default_conf, update, mocker) -> None:
|
2018-06-09 02:29:48 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-06-09 02:29:48 +00:00
|
|
|
|
|
|
|
freqtradebot.state = State.RUNNING
|
|
|
|
assert freqtradebot.state == State.RUNNING
|
2020-06-09 21:03:55 +00:00
|
|
|
telegram._reload_config(update=update, context=MagicMock())
|
|
|
|
assert freqtradebot.state == State.RELOAD_CONFIG
|
2018-06-09 02:29:48 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2020-09-05 14:44:23 +00:00
|
|
|
assert 'Reloading config' in msg_mock.call_args_list[0][0][0]
|
2018-06-09 02:29:48 +00:00
|
|
|
|
|
|
|
|
2020-07-15 18:51:52 +00:00
|
|
|
def test_telegram_forcesell_handle(default_conf, update, ticker, fee,
|
2020-07-15 19:02:31 +00:00
|
|
|
ticker_sell_up, mocker) -> None:
|
2018-07-21 18:49:57 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
2020-12-24 06:39:46 +00:00
|
|
|
msg_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2019-10-26 11:24:26 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=True),
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2020-12-24 08:01:53 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
|
|
|
telegram = Telegram(rpc, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2017-10-01 12:25:10 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2017-10-31 23:26:32 +00:00
|
|
|
|
2017-11-22 19:51:25 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2017-12-17 21:07:56 +00:00
|
|
|
# Increase the price and sell it
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', ticker_sell_up)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
# /forcesell 1
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["1"]
|
2022-01-26 19:17:00 +00:00
|
|
|
telegram._forceexit(update=update, context=context)
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2021-04-19 19:32:04 +00:00
|
|
|
assert msg_mock.call_count == 4
|
2021-11-30 19:19:59 +00:00
|
|
|
last_msg = msg_mock.call_args_list[-2][0][0]
|
2018-07-03 18:26:48 +00:00
|
|
|
assert {
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2020-07-20 17:50:29 +00:00
|
|
|
'trade_id': 1,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2018-07-03 18:26:48 +00:00
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'profit',
|
2021-12-29 13:24:12 +00:00
|
|
|
'leverage': 1.0,
|
2020-02-23 13:27:03 +00:00
|
|
|
'limit': 1.173e-05,
|
2020-07-15 18:51:52 +00:00
|
|
|
'amount': 91.07468123,
|
2019-06-17 04:55:54 +00:00
|
|
|
'order_type': 'limit',
|
2020-02-23 13:27:03 +00:00
|
|
|
'open_rate': 1.098e-05,
|
|
|
|
'current_rate': 1.173e-05,
|
2021-12-29 13:24:12 +00:00
|
|
|
'direction': 'Long',
|
2020-02-23 13:27:03 +00:00
|
|
|
'profit_amount': 6.314e-05,
|
2020-03-05 11:27:12 +00:00
|
|
|
'profit_ratio': 0.0629778,
|
2018-07-03 18:26:48 +00:00
|
|
|
'stake_currency': 'BTC',
|
2021-12-16 19:18:01 +00:00
|
|
|
'base_currency': 'ETH',
|
2018-07-03 18:26:48 +00:00
|
|
|
'fiat_currency': 'USD',
|
2021-10-21 14:25:38 +00:00
|
|
|
'buy_tag': ANY,
|
2021-11-21 08:51:16 +00:00
|
|
|
'enter_tag': ANY,
|
2022-04-04 14:59:27 +00:00
|
|
|
'sell_reason': ExitType.FORCE_EXIT.value,
|
|
|
|
'exit_reason': ExitType.FORCE_EXIT.value,
|
2019-12-08 13:10:04 +00:00
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': ANY,
|
2021-04-19 19:32:04 +00:00
|
|
|
'close_rate': ANY,
|
2018-07-03 18:26:48 +00:00
|
|
|
} == last_msg
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2020-07-15 18:51:52 +00:00
|
|
|
def test_telegram_forcesell_down_handle(default_conf, update, ticker, fee,
|
|
|
|
ticker_sell_down, mocker) -> None:
|
2018-12-11 19:27:54 +00:00
|
|
|
mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price',
|
|
|
|
return_value=15000.0)
|
2020-12-24 06:39:46 +00:00
|
|
|
msg_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2019-10-26 11:24:26 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=True),
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2020-12-24 08:01:53 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
|
|
|
telegram = Telegram(rpc, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2017-12-19 05:58:02 +00:00
|
|
|
# Decrease the price and sell it
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker_sell_down
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
# /forcesell 1
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["1"]
|
2022-01-26 19:17:00 +00:00
|
|
|
telegram._forceexit(update=update, context=context)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-04-19 19:32:04 +00:00
|
|
|
assert msg_mock.call_count == 4
|
2018-07-03 18:26:48 +00:00
|
|
|
|
2021-11-30 19:19:59 +00:00
|
|
|
last_msg = msg_mock.call_args_list[-2][0][0]
|
2018-07-03 18:26:48 +00:00
|
|
|
assert {
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2020-07-20 17:50:29 +00:00
|
|
|
'trade_id': 1,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2018-07-03 18:26:48 +00:00
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'loss',
|
2021-12-29 13:24:12 +00:00
|
|
|
'leverage': 1.0,
|
2020-02-23 13:27:03 +00:00
|
|
|
'limit': 1.043e-05,
|
2020-07-15 18:51:52 +00:00
|
|
|
'amount': 91.07468123,
|
2019-06-17 04:55:54 +00:00
|
|
|
'order_type': 'limit',
|
2020-02-23 13:27:03 +00:00
|
|
|
'open_rate': 1.098e-05,
|
|
|
|
'current_rate': 1.043e-05,
|
2021-12-29 13:24:12 +00:00
|
|
|
'direction': 'Long',
|
2020-02-23 13:27:03 +00:00
|
|
|
'profit_amount': -5.497e-05,
|
2020-03-05 11:27:12 +00:00
|
|
|
'profit_ratio': -0.05482878,
|
2018-07-03 18:26:48 +00:00
|
|
|
'stake_currency': 'BTC',
|
2021-12-16 19:18:01 +00:00
|
|
|
'base_currency': 'ETH',
|
2018-07-03 18:26:48 +00:00
|
|
|
'fiat_currency': 'USD',
|
2021-10-21 14:25:38 +00:00
|
|
|
'buy_tag': ANY,
|
2021-11-21 08:51:16 +00:00
|
|
|
'enter_tag': ANY,
|
2022-04-04 14:59:27 +00:00
|
|
|
'sell_reason': ExitType.FORCE_EXIT.value,
|
|
|
|
'exit_reason': ExitType.FORCE_EXIT.value,
|
2019-12-08 13:10:04 +00:00
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': ANY,
|
2021-04-19 19:32:04 +00:00
|
|
|
'close_rate': ANY,
|
2018-07-03 18:26:48 +00:00
|
|
|
} == last_msg
|
2017-10-01 12:25:10 +00:00
|
|
|
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
def test_forcesell_all_handle(default_conf, update, ticker, fee, mocker) -> None:
|
2018-09-11 17:59:01 +00:00
|
|
|
patch_exchange(mocker)
|
2018-12-11 19:27:54 +00:00
|
|
|
mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price',
|
|
|
|
return_value=15000.0)
|
2020-12-24 06:39:46 +00:00
|
|
|
msg_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2019-10-26 11:24:26 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=True),
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
2019-08-13 08:15:31 +00:00
|
|
|
default_conf['max_open_trades'] = 4
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2020-12-24 08:01:53 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
|
|
|
telegram = Telegram(rpc, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2017-11-09 21:08:28 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2020-12-24 06:39:46 +00:00
|
|
|
msg_mock.reset_mock()
|
2017-11-09 21:08:28 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
# /forcesell all
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["all"]
|
2022-01-26 19:17:00 +00:00
|
|
|
telegram._forceexit(update=update, context=context)
|
2017-11-09 21:08:28 +00:00
|
|
|
|
2021-06-05 13:22:52 +00:00
|
|
|
# Called for each trade 2 times
|
|
|
|
assert msg_mock.call_count == 8
|
2021-11-30 19:19:59 +00:00
|
|
|
msg = msg_mock.call_args_list[0][0][0]
|
2018-07-03 18:26:48 +00:00
|
|
|
assert {
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2020-07-20 17:50:29 +00:00
|
|
|
'trade_id': 1,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2018-07-03 18:26:48 +00:00
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'loss',
|
2021-12-29 13:24:12 +00:00
|
|
|
'leverage': 1.0,
|
2020-02-23 13:27:03 +00:00
|
|
|
'limit': 1.099e-05,
|
2020-07-15 18:51:52 +00:00
|
|
|
'amount': 91.07468123,
|
2019-06-17 04:55:54 +00:00
|
|
|
'order_type': 'limit',
|
2020-02-23 13:27:03 +00:00
|
|
|
'open_rate': 1.098e-05,
|
|
|
|
'current_rate': 1.099e-05,
|
2021-12-29 13:24:12 +00:00
|
|
|
'direction': 'Long',
|
2020-02-23 13:27:03 +00:00
|
|
|
'profit_amount': -4.09e-06,
|
2020-03-05 11:27:12 +00:00
|
|
|
'profit_ratio': -0.00408133,
|
2018-07-03 18:26:48 +00:00
|
|
|
'stake_currency': 'BTC',
|
2021-12-16 19:18:01 +00:00
|
|
|
'base_currency': 'ETH',
|
2018-07-03 18:26:48 +00:00
|
|
|
'fiat_currency': 'USD',
|
2021-10-21 14:25:38 +00:00
|
|
|
'buy_tag': ANY,
|
2021-11-21 08:51:16 +00:00
|
|
|
'enter_tag': ANY,
|
2022-04-04 14:59:27 +00:00
|
|
|
'sell_reason': ExitType.FORCE_EXIT.value,
|
|
|
|
'exit_reason': ExitType.FORCE_EXIT.value,
|
2019-12-08 13:10:04 +00:00
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': ANY,
|
2021-04-19 19:32:04 +00:00
|
|
|
'close_rate': ANY,
|
2018-07-03 18:26:48 +00:00
|
|
|
} == msg
|
2017-11-09 21:08:28 +00:00
|
|
|
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def test_forcesell_handle_invalid(default_conf, update, mocker) -> None:
|
2018-12-11 19:27:54 +00:00
|
|
|
mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price',
|
|
|
|
return_value=15000.0)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
# Trader is not running
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
2019-09-02 18:42:39 +00:00
|
|
|
# /forcesell 1
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["1"]
|
2022-01-26 19:17:00 +00:00
|
|
|
telegram._forceexit(update=update, context=context)
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'not running' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
# No argument
|
|
|
|
msg_mock.reset_mock()
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2019-09-02 18:42:39 +00:00
|
|
|
context = MagicMock()
|
|
|
|
context.args = []
|
2022-01-26 19:17:00 +00:00
|
|
|
telegram._forceexit(update=update, context=context)
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2020-12-01 18:57:43 +00:00
|
|
|
assert "You must specify a trade-id or 'all'." in msg_mock.call_args_list[0][0][0]
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
# Invalid argument
|
|
|
|
msg_mock.reset_mock()
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2019-09-02 18:42:39 +00:00
|
|
|
# /forcesell 123456
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["123456"]
|
2022-01-26 19:17:00 +00:00
|
|
|
telegram._forceexit(update=update, context=context)
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2018-06-22 01:37:19 +00:00
|
|
|
assert 'invalid argument' in msg_mock.call_args_list[0][0][0]
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
|
2022-01-27 05:31:45 +00:00
|
|
|
def test_forceenter_handle(default_conf, update, mocker) -> None:
|
2018-10-09 18:04:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
2020-12-24 06:39:46 +00:00
|
|
|
|
2018-10-09 18:04:53 +00:00
|
|
|
fbuy_mock = MagicMock(return_value=None)
|
2022-01-26 18:08:37 +00:00
|
|
|
mocker.patch('freqtrade.rpc.RPC._rpc_force_entry', fbuy_mock)
|
2018-10-09 18:04:53 +00:00
|
|
|
|
2020-12-24 06:39:46 +00:00
|
|
|
telegram, freqtradebot, _ = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2018-10-09 18:04:53 +00:00
|
|
|
|
2022-01-27 05:40:41 +00:00
|
|
|
# /forcelong ETH/BTC
|
2019-09-02 18:42:39 +00:00
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["ETH/BTC"]
|
2022-01-27 05:31:45 +00:00
|
|
|
telegram._forceenter(update=update, context=context, order_side=SignalDirection.LONG)
|
2018-10-09 18:04:53 +00:00
|
|
|
|
|
|
|
assert fbuy_mock.call_count == 1
|
|
|
|
assert fbuy_mock.call_args_list[0][0][0] == 'ETH/BTC'
|
|
|
|
assert fbuy_mock.call_args_list[0][0][1] is None
|
2022-01-27 05:40:41 +00:00
|
|
|
assert fbuy_mock.call_args_list[0][1]['order_side'] == SignalDirection.LONG
|
2018-10-09 18:04:53 +00:00
|
|
|
|
|
|
|
# Reset and retry with specified price
|
|
|
|
fbuy_mock = MagicMock(return_value=None)
|
2022-01-26 18:08:37 +00:00
|
|
|
mocker.patch('freqtrade.rpc.RPC._rpc_force_entry', fbuy_mock)
|
2022-01-27 05:40:41 +00:00
|
|
|
# /forcelong ETH/BTC 0.055
|
2019-09-02 18:42:39 +00:00
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["ETH/BTC", "0.055"]
|
2022-01-27 05:31:45 +00:00
|
|
|
telegram._forceenter(update=update, context=context, order_side=SignalDirection.LONG)
|
2018-10-09 18:04:53 +00:00
|
|
|
|
|
|
|
assert fbuy_mock.call_count == 1
|
|
|
|
assert fbuy_mock.call_args_list[0][0][0] == 'ETH/BTC'
|
|
|
|
assert isinstance(fbuy_mock.call_args_list[0][0][1], float)
|
|
|
|
assert fbuy_mock.call_args_list[0][0][1] == 0.055
|
|
|
|
|
|
|
|
|
2022-01-27 05:31:45 +00:00
|
|
|
def test_forceenter_handle_exception(default_conf, update, mocker) -> None:
|
2018-10-09 18:04:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
2020-12-24 06:39:46 +00:00
|
|
|
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2018-10-09 18:04:53 +00:00
|
|
|
|
|
|
|
update.message.text = '/forcebuy ETH/Nonepair'
|
2022-01-27 05:31:45 +00:00
|
|
|
telegram._forceenter(update=update, context=MagicMock(), order_side=SignalDirection.LONG)
|
2018-10-09 18:04:53 +00:00
|
|
|
|
2020-12-24 06:39:46 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2022-01-26 18:24:01 +00:00
|
|
|
assert msg_mock.call_args_list[0][0][0] == 'Forceentry not enabled.'
|
2018-10-09 18:04:53 +00:00
|
|
|
|
|
|
|
|
2022-01-27 05:31:45 +00:00
|
|
|
def test_forceenter_no_pair(default_conf, update, mocker) -> None:
|
2021-04-10 23:13:04 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
|
|
|
|
|
|
|
fbuy_mock = MagicMock(return_value=None)
|
2022-01-26 18:08:37 +00:00
|
|
|
mocker.patch('freqtrade.rpc.RPC._rpc_force_entry', fbuy_mock)
|
2021-04-10 23:13:04 +00:00
|
|
|
|
2021-06-13 18:04:24 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2021-04-10 23:13:04 +00:00
|
|
|
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = []
|
2022-01-27 05:31:45 +00:00
|
|
|
telegram._forceenter(update=update, context=context, order_side=SignalDirection.LONG)
|
2021-04-10 23:13:04 +00:00
|
|
|
|
|
|
|
assert fbuy_mock.call_count == 0
|
2021-06-13 18:04:24 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert msg_mock.call_args_list[0][1]['msg'] == 'Which pair?'
|
|
|
|
# assert msg_mock.call_args_list[0][1]['callback_query_handler'] == 'forcebuy'
|
|
|
|
keyboard = msg_mock.call_args_list[0][1]['keyboard']
|
2022-02-11 18:37:35 +00:00
|
|
|
# One additional button - cancel
|
|
|
|
assert reduce(lambda acc, x: acc + len(x), keyboard, 0) == 5
|
2021-05-23 14:01:49 +00:00
|
|
|
update = MagicMock()
|
|
|
|
update.callback_query = MagicMock()
|
2022-01-27 05:31:45 +00:00
|
|
|
update.callback_query.data = 'XRP/USDT_||_long'
|
2022-01-27 05:40:41 +00:00
|
|
|
telegram._forceenter_inline(update, None)
|
2021-05-23 14:01:49 +00:00
|
|
|
assert fbuy_mock.call_count == 1
|
2021-04-10 23:13:04 +00:00
|
|
|
|
|
|
|
|
2021-11-27 08:53:05 +00:00
|
|
|
def test_telegram_performance_handle(default_conf, update, ticker, fee,
|
|
|
|
limit_buy_order, limit_sell_order, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2017-10-01 12:25:10 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2017-11-22 19:51:25 +00:00
|
|
|
trade = Trade.query.first()
|
2017-10-01 12:25:10 +00:00
|
|
|
assert trade
|
2017-10-31 23:26:32 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_buy_order, limit_buy_order['symbol'], 'buy')
|
|
|
|
trade.update_trade(oobj)
|
2017-10-31 23:26:32 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_sell_order, limit_sell_order['symbol'], 'sell')
|
|
|
|
trade.update_trade(oobj)
|
2017-10-31 23:26:32 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._performance(update=update, context=MagicMock())
|
2017-11-18 20:30:31 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'Performance' in msg_mock.call_args_list[0][0][0]
|
2021-05-15 17:49:21 +00:00
|
|
|
assert '<code>ETH/BTC\t0.00006217 BTC (6.20%) (1)</code>' in msg_mock.call_args_list[0][0][0]
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2022-04-03 08:39:35 +00:00
|
|
|
def test_telegram_entry_tag_performance_handle(
|
|
|
|
default_conf, update, ticker, fee, limit_buy_order, limit_sell_order, mocker) -> None:
|
2021-10-25 20:43:22 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
patch_get_signal(freqtradebot)
|
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
freqtradebot.enter_positions()
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_buy_order, limit_buy_order['symbol'], 'buy')
|
|
|
|
trade.update_trade(oobj)
|
2021-10-25 20:43:22 +00:00
|
|
|
|
2021-11-21 08:51:16 +00:00
|
|
|
trade.enter_tag = "TESTBUY"
|
2021-10-25 20:43:22 +00:00
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_sell_order, limit_sell_order['symbol'], 'sell')
|
|
|
|
trade.update_trade(oobj)
|
2021-10-25 20:43:22 +00:00
|
|
|
|
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
2021-11-27 08:53:05 +00:00
|
|
|
context = MagicMock()
|
2021-12-01 06:11:11 +00:00
|
|
|
telegram._enter_tag_performance(update=update, context=context)
|
2021-10-25 20:43:22 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2022-04-03 08:39:35 +00:00
|
|
|
assert 'Entry Tag Performance' in msg_mock.call_args_list[0][0][0]
|
2021-10-28 18:39:42 +00:00
|
|
|
assert '<code>TESTBUY\t0.00006217 BTC (6.20%) (1)</code>' in msg_mock.call_args_list[0][0][0]
|
2021-10-25 20:43:22 +00:00
|
|
|
|
2021-11-27 08:53:05 +00:00
|
|
|
context.args = [trade.pair]
|
2021-12-01 06:11:11 +00:00
|
|
|
telegram._enter_tag_performance(update=update, context=context)
|
2021-11-27 08:53:05 +00:00
|
|
|
assert msg_mock.call_count == 2
|
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
2021-12-01 06:11:11 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.RPC._rpc_enter_tag_performance',
|
2021-11-27 08:53:05 +00:00
|
|
|
side_effect=RPCException('Error'))
|
2021-12-01 06:11:11 +00:00
|
|
|
telegram._enter_tag_performance(update=update, context=MagicMock())
|
2021-10-25 20:43:22 +00:00
|
|
|
|
2021-11-27 08:53:05 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert "Error" in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
2022-04-03 08:39:35 +00:00
|
|
|
def test_telegram_exit_reason_performance_handle(default_conf, update, ticker, fee,
|
2021-11-27 08:53:05 +00:00
|
|
|
limit_buy_order, limit_sell_order, mocker) -> None:
|
2021-10-25 20:43:22 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
patch_get_signal(freqtradebot)
|
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
freqtradebot.enter_positions()
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
2022-03-24 19:33:47 +00:00
|
|
|
trade.exit_reason = 'TESTSELL'
|
2021-10-25 20:43:22 +00:00
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_buy_order, limit_buy_order['symbol'], 'buy')
|
|
|
|
trade.update_trade(oobj)
|
2021-10-25 20:43:22 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_sell_order, limit_sell_order['symbol'], 'sell')
|
|
|
|
trade.update_trade(oobj)
|
2021-10-25 20:43:22 +00:00
|
|
|
|
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
2021-11-27 08:53:05 +00:00
|
|
|
context = MagicMock()
|
2022-04-03 08:39:35 +00:00
|
|
|
telegram._exit_reason_performance(update=update, context=context)
|
2021-10-25 20:43:22 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2022-04-03 08:39:35 +00:00
|
|
|
assert 'Exit Reason Performance' in msg_mock.call_args_list[0][0][0]
|
2021-10-28 18:39:42 +00:00
|
|
|
assert '<code>TESTSELL\t0.00006217 BTC (6.20%) (1)</code>' in msg_mock.call_args_list[0][0][0]
|
2021-11-27 08:53:05 +00:00
|
|
|
context.args = [trade.pair]
|
2021-10-25 20:43:22 +00:00
|
|
|
|
2022-04-03 08:39:35 +00:00
|
|
|
telegram._exit_reason_performance(update=update, context=context)
|
2021-11-27 08:53:05 +00:00
|
|
|
assert msg_mock.call_count == 2
|
2021-10-25 20:43:22 +00:00
|
|
|
|
2021-11-27 08:53:05 +00:00
|
|
|
msg_mock.reset_mock()
|
2022-04-03 08:39:35 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.RPC._rpc_exit_reason_performance',
|
2021-11-27 08:53:05 +00:00
|
|
|
side_effect=RPCException('Error'))
|
2022-04-03 08:39:35 +00:00
|
|
|
telegram._exit_reason_performance(update=update, context=MagicMock())
|
2021-11-27 08:53:05 +00:00
|
|
|
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert "Error" in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
|
|
|
def test_telegram_mix_tag_performance_handle(default_conf, update, ticker, fee,
|
|
|
|
limit_buy_order, limit_sell_order, mocker) -> None:
|
2021-10-25 20:43:22 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
patch_get_signal(freqtradebot)
|
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
freqtradebot.enter_positions()
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
2021-11-21 08:51:16 +00:00
|
|
|
trade.enter_tag = "TESTBUY"
|
2022-03-24 19:33:47 +00:00
|
|
|
trade.exit_reason = "TESTSELL"
|
2021-10-28 18:39:42 +00:00
|
|
|
|
2022-02-11 06:33:46 +00:00
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
|
|
|
oobj = Order.parse_from_ccxt_object(limit_buy_order, limit_buy_order['symbol'], 'buy')
|
|
|
|
trade.update_trade(oobj)
|
|
|
|
|
2021-10-25 20:43:22 +00:00
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
2022-02-11 06:33:46 +00:00
|
|
|
oobj = Order.parse_from_ccxt_object(limit_sell_order, limit_sell_order['symbol'], 'sell')
|
|
|
|
trade.update_trade(oobj)
|
2021-10-25 20:43:22 +00:00
|
|
|
|
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
2021-10-28 18:39:42 +00:00
|
|
|
|
2021-11-27 08:53:05 +00:00
|
|
|
context = MagicMock()
|
|
|
|
telegram._mix_tag_performance(update=update, context=context)
|
2021-10-25 20:43:22 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2021-10-28 18:39:42 +00:00
|
|
|
assert 'Mix Tag Performance' in msg_mock.call_args_list[0][0][0]
|
2021-10-29 05:04:20 +00:00
|
|
|
assert ('<code>TESTBUY TESTSELL\t0.00006217 BTC (6.20%) (1)</code>'
|
|
|
|
in msg_mock.call_args_list[0][0][0])
|
2017-12-17 21:07:56 +00:00
|
|
|
|
2021-11-27 08:53:05 +00:00
|
|
|
context.args = [trade.pair]
|
|
|
|
telegram._mix_tag_performance(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 2
|
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
mocker.patch('freqtrade.rpc.rpc.RPC._rpc_mix_tag_performance',
|
|
|
|
side_effect=RPCException('Error'))
|
|
|
|
telegram._mix_tag_performance(update=update, context=MagicMock())
|
|
|
|
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert "Error" in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
def test_count_handle(default_conf, update, ticker, fee, mocker) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:46:56 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2019-10-26 11:24:26 +00:00
|
|
|
get_fee=fee,
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._count(update=update, context=MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'not running' in msg_mock.call_args_list[0][0][0]
|
|
|
|
msg_mock.reset_mock()
|
2018-03-20 19:09:42 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2017-11-05 15:26:03 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2017-11-09 22:45:03 +00:00
|
|
|
msg_mock.reset_mock()
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._count(update=update, context=MagicMock())
|
2017-11-09 22:45:03 +00:00
|
|
|
|
2020-06-06 15:28:00 +00:00
|
|
|
msg = ('<pre> current max total stake\n--------- ----- -------------\n'
|
|
|
|
' 1 {} {}</pre>').format(
|
2021-09-20 02:24:22 +00:00
|
|
|
default_conf['max_open_trades'],
|
|
|
|
default_conf['stake_amount']
|
|
|
|
)
|
2017-11-09 22:45:03 +00:00
|
|
|
assert msg in msg_mock.call_args_list[0][0][0]
|
2017-11-05 15:26:03 +00:00
|
|
|
|
|
|
|
|
2020-10-17 13:15:35 +00:00
|
|
|
def test_telegram_lock_handle(default_conf, update, ticker, fee, mocker) -> None:
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2021-08-24 18:51:41 +00:00
|
|
|
patch_get_signal(freqtradebot)
|
2021-05-15 08:50:00 +00:00
|
|
|
telegram._locks(update=update, context=MagicMock())
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'No active locks.' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
2020-10-17 13:15:35 +00:00
|
|
|
|
2020-10-25 09:54:30 +00:00
|
|
|
PairLocks.lock_pair('ETH/BTC', arrow.utcnow().shift(minutes=4).datetime, 'randreason')
|
|
|
|
PairLocks.lock_pair('XRP/BTC', arrow.utcnow().shift(minutes=20).datetime, 'deadbeef')
|
2020-10-17 13:15:35 +00:00
|
|
|
|
|
|
|
telegram._locks(update=update, context=MagicMock())
|
|
|
|
|
|
|
|
assert 'Pair' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'Until' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'Reason\n' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'ETH/BTC' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'XRP/BTC' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'deadbeef' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'randreason' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
2021-03-01 19:08:49 +00:00
|
|
|
context = MagicMock()
|
|
|
|
context.args = ['XRP/BTC']
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
telegram._delete_locks(update=update, context=context)
|
|
|
|
|
|
|
|
assert 'ETH/BTC' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'randreason' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'XRP/BTC' not in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'deadbeef' not in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
2020-10-17 13:15:35 +00:00
|
|
|
|
2018-11-10 19:15:06 +00:00
|
|
|
def test_whitelist_static(default_conf, update, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-11-10 19:15:06 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._whitelist(update=update, context=MagicMock())
|
2018-11-10 19:15:06 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2019-11-09 13:15:47 +00:00
|
|
|
assert ("Using whitelist `['StaticPairList']` with 4 pairs\n"
|
|
|
|
"`ETH/BTC, LTC/BTC, XRP/BTC, NEO/BTC`" in msg_mock.call_args_list[0][0][0])
|
2018-11-10 19:15:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_whitelist_dynamic(default_conf, update, mocker) -> None:
|
2018-12-02 21:12:12 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True))
|
2019-11-09 13:15:47 +00:00
|
|
|
default_conf['pairlists'] = [{'method': 'VolumePairList',
|
2019-11-19 05:34:54 +00:00
|
|
|
'number_assets': 4
|
2019-11-09 13:15:47 +00:00
|
|
|
}]
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-11-10 19:15:06 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._whitelist(update=update, context=MagicMock())
|
2018-11-10 19:15:06 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2019-11-09 13:15:47 +00:00
|
|
|
assert ("Using whitelist `['VolumePairList']` with 4 pairs\n"
|
|
|
|
"`ETH/BTC, LTC/BTC, XRP/BTC, NEO/BTC`" in msg_mock.call_args_list[0][0][0])
|
2018-11-10 19:15:06 +00:00
|
|
|
|
|
|
|
|
2019-03-24 15:09:20 +00:00
|
|
|
def test_blacklist_static(default_conf, update, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2019-03-24 15:09:20 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._blacklist(update=update, context=MagicMock())
|
2019-03-24 15:09:20 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2019-03-24 15:30:11 +00:00
|
|
|
assert ("Blacklist contains 2 pairs\n`DOGE/BTC, HOT/BTC`"
|
2019-03-24 15:09:20 +00:00
|
|
|
in msg_mock.call_args_list[0][0][0])
|
|
|
|
|
2019-03-24 15:30:11 +00:00
|
|
|
msg_mock.reset_mock()
|
2019-09-02 18:42:39 +00:00
|
|
|
|
|
|
|
# /blacklist ETH/BTC
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["ETH/BTC"]
|
|
|
|
telegram._blacklist(update=update, context=context)
|
2019-03-24 15:30:11 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert ("Blacklist contains 3 pairs\n`DOGE/BTC, HOT/BTC, ETH/BTC`"
|
|
|
|
in msg_mock.call_args_list[0][0][0])
|
|
|
|
assert freqtradebot.pairlists.blacklist == ["DOGE/BTC", "HOT/BTC", "ETH/BTC"]
|
|
|
|
|
2020-05-28 05:04:06 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
context = MagicMock()
|
2020-12-30 08:57:31 +00:00
|
|
|
context.args = ["XRP/.*"]
|
2020-05-28 05:04:06 +00:00
|
|
|
telegram._blacklist(update=update, context=context)
|
2020-12-30 08:57:31 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2020-05-28 05:04:06 +00:00
|
|
|
|
2020-12-30 08:57:31 +00:00
|
|
|
assert ("Blacklist contains 4 pairs\n`DOGE/BTC, HOT/BTC, ETH/BTC, XRP/.*`"
|
|
|
|
in msg_mock.call_args_list[0][0][0])
|
|
|
|
assert freqtradebot.pairlists.blacklist == ["DOGE/BTC", "HOT/BTC", "ETH/BTC", "XRP/.*"]
|
2020-05-28 05:04:06 +00:00
|
|
|
|
2021-12-11 15:09:20 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
context.args = ["DOGE/BTC"]
|
|
|
|
telegram._blacklist_delete(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert ("Blacklist contains 3 pairs\n`HOT/BTC, ETH/BTC, XRP/.*`"
|
|
|
|
in msg_mock.call_args_list[0][0][0])
|
|
|
|
|
2019-03-24 15:30:11 +00:00
|
|
|
|
2020-08-14 17:36:12 +00:00
|
|
|
def test_telegram_logs(default_conf, update, mocker) -> None:
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
)
|
|
|
|
setup_logging(default_conf)
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-08-14 17:36:12 +00:00
|
|
|
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = []
|
|
|
|
telegram._logs(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
2020-08-15 18:15:02 +00:00
|
|
|
assert "freqtrade\\.rpc\\.telegram" in msg_mock.call_args_list[0][0][0]
|
2020-08-14 17:36:12 +00:00
|
|
|
|
2020-08-14 18:12:59 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
context.args = ["1"]
|
|
|
|
telegram._logs(update=update, context=context)
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
# Test with changed MaxMessageLength
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.MAX_TELEGRAM_MESSAGE_LENGTH', 200)
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = []
|
|
|
|
telegram._logs(update=update, context=context)
|
2020-09-28 17:50:22 +00:00
|
|
|
# Called at least 2 times. Exact times will change with unrelated changes to setup messages
|
2020-08-14 18:12:59 +00:00
|
|
|
# Therefore we don't test for this explicitly.
|
2020-09-28 17:50:22 +00:00
|
|
|
assert msg_mock.call_count >= 2
|
2020-08-14 18:12:59 +00:00
|
|
|
|
2020-08-14 17:36:12 +00:00
|
|
|
|
2019-03-27 15:04:05 +00:00
|
|
|
def test_edge_disabled(default_conf, update, mocker) -> None:
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2019-03-27 15:04:05 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._edge(update=update, context=MagicMock())
|
2019-03-27 15:04:05 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert "Edge is not enabled." in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
|
|
|
def test_edge_enabled(edge_conf, update, mocker) -> None:
|
|
|
|
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
|
|
|
|
return_value={
|
|
|
|
'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60),
|
|
|
|
}
|
|
|
|
))
|
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, edge_conf)
|
2019-03-27 15:04:05 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._edge(update=update, context=MagicMock())
|
2019-03-27 15:04:05 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert '<b>Edge only validated following pairs:</b>\n<pre>' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'Pair Winrate Expectancy Stoploss' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
2021-05-08 17:43:31 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
|
|
|
|
return_value={}))
|
|
|
|
telegram._edge(update=update, context=MagicMock())
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert '<b>Edge only validated following pairs:</b>' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'Winrate' not in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
2019-03-27 15:58:53 +00:00
|
|
|
|
2021-11-16 06:03:33 +00:00
|
|
|
@pytest.mark.parametrize('is_short,regex_pattern',
|
|
|
|
[(True, r"just now[ ]*XRP\/BTC \(#3\) -1.00% \("),
|
|
|
|
(False, r"just now[ ]*XRP\/BTC \(#3\) 1.00% \(")])
|
|
|
|
def test_telegram_trades(mocker, update, default_conf, fee, is_short, regex_pattern):
|
2020-07-23 05:12:14 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2020-07-23 05:12:14 +00:00
|
|
|
context = MagicMock()
|
|
|
|
context.args = []
|
|
|
|
|
|
|
|
telegram._trades(update=update, context=context)
|
|
|
|
assert "<b>0 recent trades</b>:" in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert "<pre>" not in msg_mock.call_args_list[0][0][0]
|
2020-12-02 06:51:59 +00:00
|
|
|
msg_mock.reset_mock()
|
2020-07-23 05:12:14 +00:00
|
|
|
|
2020-12-02 06:51:59 +00:00
|
|
|
context.args = ['hello']
|
|
|
|
telegram._trades(update=update, context=context)
|
|
|
|
assert "<b>0 recent trades</b>:" in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert "<pre>" not in msg_mock.call_args_list[0][0][0]
|
2020-07-23 05:12:14 +00:00
|
|
|
msg_mock.reset_mock()
|
2020-12-02 06:51:59 +00:00
|
|
|
|
2021-11-16 06:03:33 +00:00
|
|
|
create_mock_trades(fee, is_short=is_short)
|
2020-07-23 05:12:14 +00:00
|
|
|
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = [5]
|
|
|
|
telegram._trades(update=update, context=context)
|
|
|
|
msg_mock.call_count == 1
|
2020-07-23 05:50:45 +00:00
|
|
|
assert "2 recent trades</b>:" in msg_mock.call_args_list[0][0][0]
|
2020-07-23 05:12:14 +00:00
|
|
|
assert "Profit (" in msg_mock.call_args_list[0][0][0]
|
2021-03-08 21:21:56 +00:00
|
|
|
assert "Close Date" in msg_mock.call_args_list[0][0][0]
|
2020-07-23 05:12:14 +00:00
|
|
|
assert "<pre>" in msg_mock.call_args_list[0][0][0]
|
2021-11-14 13:04:24 +00:00
|
|
|
assert bool(re.search(regex_pattern, msg_mock.call_args_list[0][0][0]))
|
2020-07-23 05:12:14 +00:00
|
|
|
|
|
|
|
|
2021-11-14 01:36:49 +00:00
|
|
|
@pytest.mark.parametrize('is_short', [True, False])
|
|
|
|
def test_telegram_delete_trade(mocker, update, default_conf, fee, is_short):
|
2020-08-04 12:49:59 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-08-04 12:49:59 +00:00
|
|
|
context = MagicMock()
|
|
|
|
context.args = []
|
|
|
|
|
|
|
|
telegram._delete_trade(update=update, context=context)
|
2020-12-01 18:57:43 +00:00
|
|
|
assert "Trade-id not set." in msg_mock.call_args_list[0][0][0]
|
2020-08-04 12:49:59 +00:00
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
2021-11-16 06:03:33 +00:00
|
|
|
create_mock_trades(fee, is_short=is_short)
|
2020-08-04 12:49:59 +00:00
|
|
|
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = [1]
|
|
|
|
telegram._delete_trade(update=update, context=context)
|
|
|
|
msg_mock.call_count == 1
|
|
|
|
assert "Deleted trade 1." in msg_mock.call_args_list[0][0][0]
|
2020-08-04 17:56:49 +00:00
|
|
|
assert "Please make sure to take care of this asset" in msg_mock.call_args_list[0][0][0]
|
2020-08-04 12:49:59 +00:00
|
|
|
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def test_help_handle(default_conf, update, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2017-11-09 19:02:41 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._help(update=update, context=MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert '*/help:* `This help message`' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def test_version_handle(default_conf, update, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
2021-12-04 13:40:05 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
|
|
|
|
telegram._version(update=update, context=MagicMock())
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert '*Version:* `{}`'.format(__version__) in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
freqtradebot.strategy.version = lambda: '1.1.1'
|
2017-11-09 22:51:32 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._version(update=update, context=MagicMock())
|
2017-11-09 22:51:32 +00:00
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert '*Version:* `{}`'.format(__version__) in msg_mock.call_args_list[0][0][0]
|
2021-12-04 13:40:05 +00:00
|
|
|
assert '*Strategy version: * `1.1.1`' in msg_mock.call_args_list[0][0][0]
|
2017-11-09 22:51:32 +00:00
|
|
|
|
|
|
|
|
2019-11-17 14:12:53 +00:00
|
|
|
def test_show_config_handle(default_conf, update, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
2020-11-08 10:26:02 +00:00
|
|
|
default_conf['runmode'] = RunMode.DRY_RUN
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2019-11-17 14:12:53 +00:00
|
|
|
|
|
|
|
telegram._show_config(update=update, context=MagicMock())
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert '*Mode:* `{}`'.format('Dry-run') in msg_mock.call_args_list[0][0][0]
|
2021-04-20 10:54:22 +00:00
|
|
|
assert '*Exchange:* `binance`' in msg_mock.call_args_list[0][0][0]
|
2021-09-21 18:18:14 +00:00
|
|
|
assert f'*Strategy:* `{CURRENT_TEST_STRATEGY}`' in msg_mock.call_args_list[0][0][0]
|
2019-12-13 19:27:39 +00:00
|
|
|
assert '*Stoploss:* `-0.1`' in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
freqtradebot.config['trailing_stop'] = True
|
|
|
|
telegram._show_config(update=update, context=MagicMock())
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert '*Mode:* `{}`'.format('Dry-run') in msg_mock.call_args_list[0][0][0]
|
2021-04-20 10:54:22 +00:00
|
|
|
assert '*Exchange:* `binance`' in msg_mock.call_args_list[0][0][0]
|
2021-09-21 18:18:14 +00:00
|
|
|
assert f'*Strategy:* `{CURRENT_TEST_STRATEGY}`' in msg_mock.call_args_list[0][0][0]
|
2019-12-13 19:27:39 +00:00
|
|
|
assert '*Initial Stoploss:* `-0.1`' in msg_mock.call_args_list[0][0][0]
|
2019-11-17 14:12:53 +00:00
|
|
|
|
|
|
|
|
2021-12-30 16:18:46 +00:00
|
|
|
@pytest.mark.parametrize('message_type,enter,enter_signal,leverage', [
|
|
|
|
(RPCMessageType.BUY, 'Long', 'long_signal_01', None),
|
2021-12-30 16:32:36 +00:00
|
|
|
(RPCMessageType.BUY, 'Long', 'long_signal_01', 1.0),
|
|
|
|
(RPCMessageType.BUY, 'Long', 'long_signal_01', 5.0),
|
2021-12-30 16:18:46 +00:00
|
|
|
(RPCMessageType.SHORT, 'Short', 'short_signal_01', 2.0)])
|
2021-12-29 13:24:12 +00:00
|
|
|
def test_send_msg_buy_notification(default_conf, mocker, caplog, message_type,
|
|
|
|
enter, enter_signal, leverage) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
2020-09-19 18:04:12 +00:00
|
|
|
msg = {
|
2021-12-29 13:24:12 +00:00
|
|
|
'type': message_type,
|
2021-03-05 23:07:37 +00:00
|
|
|
'trade_id': 1,
|
2021-12-29 13:24:12 +00:00
|
|
|
'enter_tag': enter_signal,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2018-07-12 15:50:11 +00:00
|
|
|
'pair': 'ETH/BTC',
|
2021-12-29 13:24:12 +00:00
|
|
|
'leverage': leverage,
|
2018-07-12 15:50:11 +00:00
|
|
|
'limit': 1.099e-05,
|
2019-06-17 04:55:30 +00:00
|
|
|
'order_type': 'limit',
|
2022-02-18 14:17:45 +00:00
|
|
|
'stake_amount': 0.01465333,
|
2018-07-12 15:50:11 +00:00
|
|
|
'stake_amount_fiat': 0.0,
|
|
|
|
'stake_currency': 'BTC',
|
2020-02-08 20:02:52 +00:00
|
|
|
'fiat_currency': 'USD',
|
|
|
|
'current_rate': 1.099e-05,
|
|
|
|
'amount': 1333.3333333333335,
|
|
|
|
'open_date': arrow.utcnow().shift(hours=-1)
|
2020-09-19 18:04:12 +00:00
|
|
|
}
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, freqtradebot, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2020-09-19 18:04:12 +00:00
|
|
|
telegram.send_msg(msg)
|
2021-12-30 16:32:36 +00:00
|
|
|
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
2021-12-29 13:24:12 +00:00
|
|
|
|
2021-12-30 16:32:36 +00:00
|
|
|
assert msg_mock.call_args[0][0] == (
|
|
|
|
f'\N{LARGE BLUE CIRCLE} *Binance:* {enter} ETH/BTC (#1)\n'
|
|
|
|
f'*Enter Tag:* `{enter_signal}`\n'
|
|
|
|
'*Amount:* `1333.33333333`\n'
|
|
|
|
f'{leverage_text}'
|
|
|
|
'*Open Rate:* `0.00001099`\n'
|
|
|
|
'*Current Rate:* `0.00001099`\n'
|
2022-02-23 05:27:56 +00:00
|
|
|
'*Total:* `(0.01465333 BTC, 180.895 USD)`'
|
2021-12-30 16:32:36 +00:00
|
|
|
)
|
2020-02-08 20:02:52 +00:00
|
|
|
|
2020-09-19 18:04:12 +00:00
|
|
|
freqtradebot.config['telegram']['notification_settings'] = {'buy': 'off'}
|
|
|
|
caplog.clear()
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
telegram.send_msg(msg)
|
|
|
|
msg_mock.call_count == 0
|
|
|
|
log_has("Notification 'buy' not sent.", caplog)
|
|
|
|
|
|
|
|
freqtradebot.config['telegram']['notification_settings'] = {'buy': 'silent'}
|
|
|
|
caplog.clear()
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
|
|
|
|
telegram.send_msg(msg)
|
|
|
|
msg_mock.call_count == 1
|
|
|
|
msg_mock.call_args_list[0][1]['disable_notification'] is True
|
|
|
|
|
2020-02-08 20:02:52 +00:00
|
|
|
|
2021-12-30 16:34:45 +00:00
|
|
|
@pytest.mark.parametrize('message_type,enter_signal', [
|
|
|
|
(RPCMessageType.BUY_CANCEL, 'long_signal_01'),
|
|
|
|
(RPCMessageType.SHORT_CANCEL, 'short_signal_01')])
|
|
|
|
def test_send_msg_buy_cancel_notification(default_conf, mocker, message_type, enter_signal) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2020-02-08 20:02:52 +00:00
|
|
|
telegram.send_msg({
|
2021-12-29 13:24:12 +00:00
|
|
|
'type': message_type,
|
|
|
|
'enter_tag': enter_signal,
|
2021-03-06 13:07:47 +00:00
|
|
|
'trade_id': 1,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2020-02-08 20:02:52 +00:00
|
|
|
'pair': 'ETH/BTC',
|
2020-08-26 20:17:43 +00:00
|
|
|
'reason': CANCEL_REASON['TIMEOUT']
|
2020-02-08 20:02:52 +00:00
|
|
|
})
|
2021-04-20 10:54:22 +00:00
|
|
|
assert (msg_mock.call_args[0][0] == '\N{WARNING SIGN} *Binance:* '
|
2021-12-29 13:24:12 +00:00
|
|
|
'Cancelling enter Order for ETH/BTC (#1). '
|
2021-03-06 13:07:47 +00:00
|
|
|
'Reason: cancelled due to timeout.')
|
2018-07-12 15:50:11 +00:00
|
|
|
|
2018-07-03 18:26:48 +00:00
|
|
|
|
2021-09-20 17:12:59 +00:00
|
|
|
def test_send_msg_protection_notification(default_conf, mocker, time_machine) -> None:
|
|
|
|
|
2021-09-20 18:08:48 +00:00
|
|
|
default_conf['telegram']['notification_settings']['protection_trigger'] = 'on'
|
|
|
|
|
2021-09-20 17:12:59 +00:00
|
|
|
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')
|
|
|
|
msg = {
|
|
|
|
'type': RPCMessageType.PROTECTION_TRIGGER,
|
|
|
|
}
|
|
|
|
msg.update(lock.to_json())
|
|
|
|
telegram.send_msg(msg)
|
|
|
|
assert (msg_mock.call_args[0][0] == "*Protection* triggered due to randreason. "
|
2021-09-20 17:49:18 +00:00
|
|
|
"`ETH/BTC` will be locked until `2021-09-01 05:10:00`.")
|
2021-09-20 17:12:59 +00:00
|
|
|
|
2021-09-20 17:23:40 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
# Test global protection
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
'type': RPCMessageType.PROTECTION_TRIGGER_GLOBAL,
|
|
|
|
}
|
|
|
|
lock = PairLocks.lock_pair('*', arrow.utcnow().shift(minutes=100).datetime, 'randreason')
|
|
|
|
msg.update(lock.to_json())
|
|
|
|
telegram.send_msg(msg)
|
|
|
|
assert (msg_mock.call_args[0][0] == "*Protection* triggered due to randreason. "
|
2021-09-20 17:49:18 +00:00
|
|
|
"*All pairs* will be locked until `2021-09-01 06:45:00`.")
|
2021-09-20 17:23:40 +00:00
|
|
|
|
2021-09-20 17:12:59 +00:00
|
|
|
|
2021-12-30 16:18:46 +00:00
|
|
|
@pytest.mark.parametrize('message_type,entered,enter_signal,leverage', [
|
2021-12-30 16:32:36 +00:00
|
|
|
(RPCMessageType.BUY_FILL, 'Longed', 'long_signal_01', 1.0),
|
|
|
|
(RPCMessageType.BUY_FILL, 'Longed', 'long_signal_02', 2.0),
|
|
|
|
(RPCMessageType.SHORT_FILL, 'Shorted', 'short_signal_01', 2.0),
|
2022-03-03 19:51:52 +00:00
|
|
|
])
|
2021-12-29 13:24:12 +00:00
|
|
|
def test_send_msg_buy_fill_notification(default_conf, mocker, message_type, entered,
|
|
|
|
enter_signal, leverage) -> None:
|
2021-04-20 17:36:30 +00:00
|
|
|
|
|
|
|
default_conf['telegram']['notification_settings']['buy_fill'] = 'on'
|
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
|
|
|
|
telegram.send_msg({
|
2021-12-29 13:24:12 +00:00
|
|
|
'type': message_type,
|
2021-04-20 17:36:30 +00:00
|
|
|
'trade_id': 1,
|
2021-12-29 13:24:12 +00:00
|
|
|
'enter_tag': enter_signal,
|
2021-04-20 17:36:30 +00:00
|
|
|
'exchange': 'Binance',
|
2021-11-13 02:49:07 +00:00
|
|
|
'pair': 'ETH/BTC',
|
2021-12-29 13:24:12 +00:00
|
|
|
'leverage': leverage,
|
2022-02-18 14:17:45 +00:00
|
|
|
'stake_amount': 0.01465333,
|
2021-11-13 02:49:07 +00:00
|
|
|
# 'stake_amount_fiat': 0.0,
|
|
|
|
'stake_currency': 'BTC',
|
|
|
|
'fiat_currency': 'USD',
|
|
|
|
'open_rate': 1.099e-05,
|
|
|
|
'amount': 1333.3333333333335,
|
|
|
|
'open_date': arrow.utcnow().shift(hours=-1)
|
2021-04-20 17:36:30 +00:00
|
|
|
})
|
2021-12-30 16:32:36 +00:00
|
|
|
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage != 1.0 else ''
|
2021-12-30 16:18:46 +00:00
|
|
|
assert msg_mock.call_args[0][0] == (
|
|
|
|
f'\N{CHECK MARK} *Binance:* {entered} ETH/BTC (#1)\n'
|
|
|
|
f'*Enter Tag:* `{enter_signal}`\n'
|
|
|
|
'*Amount:* `1333.33333333`\n'
|
2021-12-30 16:32:36 +00:00
|
|
|
f"{leverage_text}"
|
2021-12-30 16:18:46 +00:00
|
|
|
'*Open Rate:* `0.00001099`\n'
|
2022-02-23 05:27:56 +00:00
|
|
|
'*Total:* `(0.01465333 BTC, 180.895 USD)`'
|
2022-03-03 19:51:52 +00:00
|
|
|
)
|
2018-07-12 15:50:11 +00:00
|
|
|
|
2018-07-03 18:26:48 +00:00
|
|
|
|
2018-07-12 15:50:11 +00:00
|
|
|
def test_send_msg_sell_notification(default_conf, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2020-12-24 08:01:53 +00:00
|
|
|
old_convamount = telegram._rpc._fiat_converter.convert_amount
|
|
|
|
telegram._rpc._fiat_converter.convert_amount = lambda a, b, c: -24.812
|
2018-07-12 15:50:11 +00:00
|
|
|
telegram.send_msg({
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2021-03-05 23:07:37 +00:00
|
|
|
'trade_id': 1,
|
2018-07-12 15:50:11 +00:00
|
|
|
'exchange': 'Binance',
|
|
|
|
'pair': 'KEY/ETH',
|
2021-12-29 13:24:12 +00:00
|
|
|
'leverage': 1.0,
|
|
|
|
'direction': 'Long',
|
2018-07-12 15:50:11 +00:00
|
|
|
'gain': 'loss',
|
|
|
|
'limit': 3.201e-05,
|
|
|
|
'amount': 1333.3333333333335,
|
2019-06-17 04:55:54 +00:00
|
|
|
'order_type': 'market',
|
2018-07-12 15:50:11 +00:00
|
|
|
'open_rate': 7.5e-05,
|
|
|
|
'current_rate': 3.201e-05,
|
|
|
|
'profit_amount': -0.05746268,
|
2020-02-28 09:36:39 +00:00
|
|
|
'profit_ratio': -0.57405275,
|
2018-07-12 15:50:11 +00:00
|
|
|
'stake_currency': 'ETH',
|
2018-12-04 18:58:43 +00:00
|
|
|
'fiat_currency': 'USD',
|
2021-11-21 09:05:56 +00:00
|
|
|
'enter_tag': 'buy_signal1',
|
2022-03-24 19:33:47 +00:00
|
|
|
'exit_reason': ExitType.STOP_LOSS.value,
|
2019-12-08 13:10:04 +00:00
|
|
|
'open_date': arrow.utcnow().shift(hours=-1),
|
|
|
|
'close_date': arrow.utcnow(),
|
2018-07-12 15:50:11 +00:00
|
|
|
})
|
2021-12-30 16:18:46 +00:00
|
|
|
assert msg_mock.call_args[0][0] == (
|
|
|
|
'\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n'
|
|
|
|
'*Unrealized Profit:* `-57.41% (loss: -0.05746268 ETH / -24.812 USD)`\n'
|
|
|
|
'*Enter Tag:* `buy_signal1`\n'
|
|
|
|
'*Exit Reason:* `stop_loss`\n'
|
|
|
|
'*Duration:* `1:00:00 (60.0 min)`\n'
|
|
|
|
'*Direction:* `Long`\n'
|
|
|
|
'*Amount:* `1333.33333333`\n'
|
|
|
|
'*Open Rate:* `0.00007500`\n'
|
|
|
|
'*Current Rate:* `0.00003201`\n'
|
|
|
|
'*Close Rate:* `0.00003201`'
|
2022-03-03 19:51:52 +00:00
|
|
|
)
|
2018-07-03 18:26:48 +00:00
|
|
|
|
2018-07-12 15:50:11 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
telegram.send_msg({
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2021-03-05 23:07:37 +00:00
|
|
|
'trade_id': 1,
|
2018-07-12 15:50:11 +00:00
|
|
|
'exchange': 'Binance',
|
|
|
|
'pair': 'KEY/ETH',
|
2021-12-29 13:24:12 +00:00
|
|
|
'direction': 'Long',
|
2018-07-12 15:50:11 +00:00
|
|
|
'gain': 'loss',
|
|
|
|
'limit': 3.201e-05,
|
|
|
|
'amount': 1333.3333333333335,
|
2019-06-17 04:55:54 +00:00
|
|
|
'order_type': 'market',
|
2018-07-12 15:50:11 +00:00
|
|
|
'open_rate': 7.5e-05,
|
|
|
|
'current_rate': 3.201e-05,
|
|
|
|
'profit_amount': -0.05746268,
|
2020-02-28 09:36:39 +00:00
|
|
|
'profit_ratio': -0.57405275,
|
2018-07-12 15:50:11 +00:00
|
|
|
'stake_currency': 'ETH',
|
2021-11-21 09:05:56 +00:00
|
|
|
'enter_tag': 'buy_signal1',
|
2022-03-24 19:33:47 +00:00
|
|
|
'exit_reason': ExitType.STOP_LOSS.value,
|
2019-12-10 14:12:36 +00:00
|
|
|
'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30),
|
2019-12-08 13:10:04 +00:00
|
|
|
'close_date': arrow.utcnow(),
|
2018-07-12 15:50:11 +00:00
|
|
|
})
|
2021-12-30 16:18:46 +00:00
|
|
|
assert msg_mock.call_args[0][0] == (
|
|
|
|
'\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n'
|
|
|
|
'*Unrealized Profit:* `-57.41%`\n'
|
|
|
|
'*Enter Tag:* `buy_signal1`\n'
|
|
|
|
'*Exit Reason:* `stop_loss`\n'
|
|
|
|
'*Duration:* `1 day, 2:30:00 (1590.0 min)`\n'
|
|
|
|
'*Direction:* `Long`\n'
|
|
|
|
'*Amount:* `1333.33333333`\n'
|
|
|
|
'*Open Rate:* `0.00007500`\n'
|
|
|
|
'*Current Rate:* `0.00003201`\n'
|
|
|
|
'*Close Rate:* `0.00003201`'
|
2022-03-03 19:51:52 +00:00
|
|
|
)
|
2018-07-24 21:53:10 +00:00
|
|
|
# Reset singleton function to avoid random breaks
|
2020-12-24 08:01:53 +00:00
|
|
|
telegram._rpc._fiat_converter.convert_amount = old_convamount
|
2018-07-12 15:50:11 +00:00
|
|
|
|
2018-07-24 22:08:40 +00:00
|
|
|
|
2020-02-08 20:02:52 +00:00
|
|
|
def test_send_msg_sell_cancel_notification(default_conf, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2020-12-24 08:01:53 +00:00
|
|
|
old_convamount = telegram._rpc._fiat_converter.convert_amount
|
|
|
|
telegram._rpc._fiat_converter.convert_amount = lambda a, b, c: -24.812
|
2020-02-08 20:02:52 +00:00
|
|
|
telegram.send_msg({
|
2022-04-04 17:07:20 +00:00
|
|
|
'type': RPCMessageType.EXIT_CANCEL,
|
2021-03-06 13:07:47 +00:00
|
|
|
'trade_id': 1,
|
2020-02-08 20:02:52 +00:00
|
|
|
'exchange': 'Binance',
|
|
|
|
'pair': 'KEY/ETH',
|
2020-03-24 16:12:24 +00:00
|
|
|
'reason': 'Cancelled on exchange'
|
2020-02-08 20:02:52 +00:00
|
|
|
})
|
2021-12-30 16:18:46 +00:00
|
|
|
assert msg_mock.call_args[0][0] == (
|
|
|
|
'\N{WARNING SIGN} *Binance:* Cancelling exit Order for KEY/ETH (#1).'
|
|
|
|
' Reason: Cancelled on exchange.')
|
2020-02-08 20:02:52 +00:00
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
telegram.send_msg({
|
2022-04-04 17:07:20 +00:00
|
|
|
'type': RPCMessageType.EXIT_CANCEL,
|
2021-03-06 13:07:47 +00:00
|
|
|
'trade_id': 1,
|
2020-02-08 20:02:52 +00:00
|
|
|
'exchange': 'Binance',
|
|
|
|
'pair': 'KEY/ETH',
|
2020-03-24 16:12:24 +00:00
|
|
|
'reason': 'timeout'
|
2020-02-08 20:02:52 +00:00
|
|
|
})
|
2021-12-30 16:18:46 +00:00
|
|
|
assert msg_mock.call_args[0][0] == (
|
|
|
|
'\N{WARNING SIGN} *Binance:* Cancelling exit Order for KEY/ETH (#1). Reason: timeout.')
|
2020-02-08 20:02:52 +00:00
|
|
|
# Reset singleton function to avoid random breaks
|
2020-12-24 08:01:53 +00:00
|
|
|
telegram._rpc._fiat_converter.convert_amount = old_convamount
|
2020-02-08 20:02:52 +00:00
|
|
|
|
|
|
|
|
2021-12-30 16:18:46 +00:00
|
|
|
@pytest.mark.parametrize('direction,enter_signal,leverage', [
|
|
|
|
('Long', 'long_signal_01', None),
|
2021-12-30 16:32:36 +00:00
|
|
|
('Long', 'long_signal_01', 1.0),
|
|
|
|
('Long', 'long_signal_01', 5.0),
|
2021-12-30 16:18:46 +00:00
|
|
|
('Short', 'short_signal_01', 2.0)])
|
2021-12-29 13:24:12 +00:00
|
|
|
def test_send_msg_sell_fill_notification(default_conf, mocker, direction,
|
|
|
|
enter_signal, leverage) -> None:
|
2021-04-22 15:13:22 +00:00
|
|
|
|
|
|
|
default_conf['telegram']['notification_settings']['sell_fill'] = 'on'
|
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.SELL_FILL,
|
|
|
|
'trade_id': 1,
|
|
|
|
'exchange': 'Binance',
|
2021-11-13 02:49:07 +00:00
|
|
|
'pair': 'KEY/ETH',
|
2021-12-29 13:24:12 +00:00
|
|
|
'leverage': leverage,
|
|
|
|
'direction': direction,
|
2021-04-22 15:13:22 +00:00
|
|
|
'gain': 'loss',
|
|
|
|
'limit': 3.201e-05,
|
2021-11-13 02:49:07 +00:00
|
|
|
'amount': 1333.3333333333335,
|
2021-04-22 15:13:22 +00:00
|
|
|
'order_type': 'market',
|
2021-11-13 02:49:07 +00:00
|
|
|
'open_rate': 7.5e-05,
|
|
|
|
'close_rate': 3.201e-05,
|
2021-04-22 15:13:22 +00:00
|
|
|
'profit_amount': -0.05746268,
|
|
|
|
'profit_ratio': -0.57405275,
|
|
|
|
'stake_currency': 'ETH',
|
2021-12-29 13:24:12 +00:00
|
|
|
'enter_tag': enter_signal,
|
2022-03-24 19:33:47 +00:00
|
|
|
'exit_reason': ExitType.STOP_LOSS.value,
|
2021-11-13 02:49:07 +00:00
|
|
|
'open_date': arrow.utcnow().shift(days=-1, hours=-2, minutes=-30),
|
2021-04-22 15:13:22 +00:00
|
|
|
'close_date': arrow.utcnow(),
|
|
|
|
})
|
2021-12-30 16:18:46 +00:00
|
|
|
|
2021-12-30 16:32:36 +00:00
|
|
|
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
2021-12-30 16:18:46 +00:00
|
|
|
assert msg_mock.call_args[0][0] == (
|
|
|
|
'\N{WARNING SIGN} *Binance:* Exited KEY/ETH (#1)\n'
|
|
|
|
'*Profit:* `-57.41%`\n'
|
|
|
|
f'*Enter Tag:* `{enter_signal}`\n'
|
|
|
|
'*Exit Reason:* `stop_loss`\n'
|
|
|
|
'*Duration:* `1 day, 2:30:00 (1590.0 min)`\n'
|
|
|
|
f"*Direction:* `{direction}`\n"
|
|
|
|
f"{leverage_text}"
|
|
|
|
'*Amount:* `1333.33333333`\n'
|
|
|
|
'*Open Rate:* `0.00007500`\n'
|
|
|
|
'*Close Rate:* `0.00003201`'
|
|
|
|
)
|
2021-04-22 15:13:22 +00:00
|
|
|
|
|
|
|
|
2018-07-12 15:50:11 +00:00
|
|
|
def test_send_msg_status_notification(default_conf, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
|
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-07-12 15:50:11 +00:00
|
|
|
telegram.send_msg({
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.STATUS,
|
2018-07-12 15:50:11 +00:00
|
|
|
'status': 'running'
|
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] == '*Status:* `running`'
|
2018-07-03 18:26:48 +00:00
|
|
|
|
|
|
|
|
2018-08-15 04:05:56 +00:00
|
|
|
def test_warning_notification(default_conf, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-08-15 04:05:56 +00:00
|
|
|
telegram.send_msg({
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.WARNING,
|
2018-08-15 04:05:56 +00:00
|
|
|
'status': 'message'
|
|
|
|
})
|
2020-06-05 17:08:54 +00:00
|
|
|
assert msg_mock.call_args[0][0] == '\N{WARNING SIGN} *Warning:* `message`'
|
2018-08-15 04:05:56 +00:00
|
|
|
|
|
|
|
|
2020-09-19 15:51:31 +00:00
|
|
|
def test_startup_notification(default_conf, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2018-08-15 04:05:56 +00:00
|
|
|
telegram.send_msg({
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.STARTUP,
|
2018-08-15 04:05:56 +00:00
|
|
|
'status': '*Custom:* `Hello World`'
|
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] == '*Custom:* `Hello World`'
|
|
|
|
|
|
|
|
|
2018-07-12 15:50:11 +00:00
|
|
|
def test_send_msg_unknown_type(default_conf, mocker) -> None:
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, _ = get_telegram_testobject(mocker, default_conf)
|
2018-07-12 15:50:11 +00:00
|
|
|
with pytest.raises(NotImplementedError, match=r'Unknown message type: None'):
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': None,
|
|
|
|
})
|
2018-07-03 18:26:48 +00:00
|
|
|
|
|
|
|
|
2021-12-30 16:18:46 +00:00
|
|
|
@pytest.mark.parametrize('message_type,enter,enter_signal,leverage', [
|
|
|
|
(RPCMessageType.BUY, 'Long', 'long_signal_01', None),
|
2021-12-30 16:32:36 +00:00
|
|
|
(RPCMessageType.BUY, 'Long', 'long_signal_01', 2.0),
|
2021-12-30 16:18:46 +00:00
|
|
|
(RPCMessageType.SHORT, 'Short', 'short_signal_01', 2.0)])
|
|
|
|
def test_send_msg_buy_notification_no_fiat(
|
|
|
|
default_conf, mocker, message_type, enter, enter_signal, leverage) -> None:
|
2018-07-22 12:52:58 +00:00
|
|
|
del default_conf['fiat_display_currency']
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
2020-12-24 06:10:01 +00:00
|
|
|
|
2018-07-22 12:49:07 +00:00
|
|
|
telegram.send_msg({
|
2021-12-29 13:24:12 +00:00
|
|
|
'type': message_type,
|
|
|
|
'enter_tag': enter_signal,
|
2021-03-05 23:07:37 +00:00
|
|
|
'trade_id': 1,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2018-07-22 12:49:07 +00:00
|
|
|
'pair': 'ETH/BTC',
|
2021-12-29 13:24:12 +00:00
|
|
|
'leverage': leverage,
|
2018-07-22 12:49:07 +00:00
|
|
|
'limit': 1.099e-05,
|
2019-06-17 04:55:30 +00:00
|
|
|
'order_type': 'limit',
|
2022-02-18 14:02:37 +00:00
|
|
|
'stake_amount': 0.01465333,
|
2018-07-22 12:49:07 +00:00
|
|
|
'stake_amount_fiat': 0.0,
|
|
|
|
'stake_currency': 'BTC',
|
2020-02-08 20:02:52 +00:00
|
|
|
'fiat_currency': None,
|
|
|
|
'current_rate': 1.099e-05,
|
|
|
|
'amount': 1333.3333333333335,
|
|
|
|
'open_date': arrow.utcnow().shift(hours=-1)
|
2018-07-22 12:49:07 +00:00
|
|
|
})
|
|
|
|
|
2021-12-30 16:32:36 +00:00
|
|
|
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
2021-12-30 16:18:46 +00:00
|
|
|
assert msg_mock.call_args[0][0] == (
|
|
|
|
f'\N{LARGE BLUE CIRCLE} *Binance:* {enter} ETH/BTC (#1)\n'
|
|
|
|
f'*Enter Tag:* `{enter_signal}`\n'
|
|
|
|
'*Amount:* `1333.33333333`\n'
|
2021-12-30 16:32:36 +00:00
|
|
|
f'{leverage_text}'
|
2021-12-30 16:18:46 +00:00
|
|
|
'*Open Rate:* `0.00001099`\n'
|
|
|
|
'*Current Rate:* `0.00001099`\n'
|
2022-02-23 05:27:56 +00:00
|
|
|
'*Total:* `(0.01465333 BTC)`'
|
2021-12-30 16:18:46 +00:00
|
|
|
)
|
2021-12-29 13:24:12 +00:00
|
|
|
|
|
|
|
|
2021-12-30 16:18:46 +00:00
|
|
|
@pytest.mark.parametrize('direction,enter_signal,leverage', [
|
|
|
|
('Long', 'long_signal_01', None),
|
2021-12-30 16:32:36 +00:00
|
|
|
('Long', 'long_signal_01', 1.0),
|
|
|
|
('Long', 'long_signal_01', 5.0),
|
|
|
|
('Short', 'short_signal_01', 2.0),
|
2022-03-03 19:51:52 +00:00
|
|
|
])
|
2021-12-30 16:18:46 +00:00
|
|
|
def test_send_msg_sell_notification_no_fiat(
|
|
|
|
default_conf, mocker, direction, enter_signal, leverage) -> None:
|
2018-07-22 12:52:58 +00:00
|
|
|
del default_conf['fiat_display_currency']
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
|
|
|
|
|
2018-07-22 12:49:07 +00:00
|
|
|
telegram.send_msg({
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2021-03-05 23:07:37 +00:00
|
|
|
'trade_id': 1,
|
2018-07-22 12:49:07 +00:00
|
|
|
'exchange': 'Binance',
|
|
|
|
'pair': 'KEY/ETH',
|
|
|
|
'gain': 'loss',
|
2021-12-29 13:24:12 +00:00
|
|
|
'leverage': leverage,
|
|
|
|
'direction': direction,
|
2018-07-22 12:49:07 +00:00
|
|
|
'limit': 3.201e-05,
|
|
|
|
'amount': 1333.3333333333335,
|
2019-06-17 04:55:54 +00:00
|
|
|
'order_type': 'limit',
|
2018-07-22 12:49:07 +00:00
|
|
|
'open_rate': 7.5e-05,
|
|
|
|
'current_rate': 3.201e-05,
|
|
|
|
'profit_amount': -0.05746268,
|
2020-02-28 09:36:39 +00:00
|
|
|
'profit_ratio': -0.57405275,
|
2018-07-22 12:49:07 +00:00
|
|
|
'stake_currency': 'ETH',
|
2018-12-04 18:58:43 +00:00
|
|
|
'fiat_currency': 'USD',
|
2021-12-29 13:24:12 +00:00
|
|
|
'enter_tag': enter_signal,
|
2022-03-24 19:33:47 +00:00
|
|
|
'exit_reason': ExitType.STOP_LOSS.value,
|
2019-12-08 13:10:04 +00:00
|
|
|
'open_date': arrow.utcnow().shift(hours=-2, minutes=-35, seconds=-3),
|
|
|
|
'close_date': arrow.utcnow(),
|
2018-07-22 12:49:07 +00:00
|
|
|
})
|
2021-12-29 13:24:12 +00:00
|
|
|
|
2021-12-30 16:32:36 +00:00
|
|
|
leverage_text = f'*Leverage:* `{leverage}`\n' if leverage and leverage != 1.0 else ''
|
2021-12-30 16:18:46 +00:00
|
|
|
assert msg_mock.call_args[0][0] == (
|
|
|
|
'\N{WARNING SIGN} *Binance:* Exiting KEY/ETH (#1)\n'
|
|
|
|
'*Unrealized Profit:* `-57.41%`\n'
|
|
|
|
f'*Enter Tag:* `{enter_signal}`\n'
|
|
|
|
'*Exit Reason:* `stop_loss`\n'
|
|
|
|
'*Duration:* `2:35:03 (155.1 min)`\n'
|
|
|
|
f'*Direction:* `{direction}`\n'
|
|
|
|
f'{leverage_text}'
|
|
|
|
'*Amount:* `1333.33333333`\n'
|
|
|
|
'*Open Rate:* `0.00007500`\n'
|
|
|
|
'*Current Rate:* `0.00003201`\n'
|
|
|
|
'*Close Rate:* `0.00003201`'
|
2022-03-03 19:51:52 +00:00
|
|
|
)
|
2018-07-22 12:49:07 +00:00
|
|
|
|
|
|
|
|
2020-06-06 11:32:06 +00:00
|
|
|
@pytest.mark.parametrize('msg,expected', [
|
2022-03-24 19:33:47 +00:00
|
|
|
({'profit_percent': 20.1, 'exit_reason': 'roi'}, "\N{ROCKET}"),
|
|
|
|
({'profit_percent': 5.1, 'exit_reason': 'roi'}, "\N{ROCKET}"),
|
|
|
|
({'profit_percent': 2.56, 'exit_reason': 'roi'}, "\N{EIGHT SPOKED ASTERISK}"),
|
|
|
|
({'profit_percent': 1.0, 'exit_reason': 'roi'}, "\N{EIGHT SPOKED ASTERISK}"),
|
|
|
|
({'profit_percent': 0.0, 'exit_reason': 'roi'}, "\N{EIGHT SPOKED ASTERISK}"),
|
|
|
|
({'profit_percent': -5.0, 'exit_reason': 'stop_loss'}, "\N{WARNING SIGN}"),
|
|
|
|
({'profit_percent': -2.0, 'exit_reason': 'sell_signal'}, "\N{CROSS MARK}"),
|
2020-06-06 11:32:06 +00:00
|
|
|
])
|
|
|
|
def test__sell_emoji(default_conf, mocker, msg, expected):
|
|
|
|
del default_conf['fiat_display_currency']
|
2020-12-24 06:29:26 +00:00
|
|
|
|
|
|
|
telegram, _, _ = get_telegram_testobject(mocker, default_conf)
|
2020-06-06 11:32:06 +00:00
|
|
|
|
2020-06-06 13:46:19 +00:00
|
|
|
assert telegram._get_sell_emoji(msg) == expected
|
2020-06-06 11:32:06 +00:00
|
|
|
|
|
|
|
|
2021-06-19 07:31:34 +00:00
|
|
|
def test_telegram__send_msg(default_conf, mocker, caplog) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
bot = MagicMock()
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, _ = get_telegram_testobject(mocker, default_conf, mock=False)
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._updater = MagicMock()
|
|
|
|
telegram._updater.bot = bot
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
telegram._config['telegram']['enabled'] = True
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._send_msg('test')
|
2017-11-09 19:02:41 +00:00
|
|
|
assert len(bot.method_calls) == 1
|
|
|
|
|
2021-06-19 07:31:34 +00:00
|
|
|
# Test update
|
|
|
|
query = MagicMock()
|
|
|
|
telegram._send_msg('test', callback_path="DeadBeef", query=query, reload_able=True)
|
|
|
|
edit_message_text = telegram._updater.bot.edit_message_text
|
|
|
|
assert edit_message_text.call_count == 1
|
|
|
|
assert "Updated: " in edit_message_text.call_args_list[0][1]['text']
|
|
|
|
|
|
|
|
telegram._updater.bot.edit_message_text = MagicMock(side_effect=BadRequest("not modified"))
|
|
|
|
telegram._send_msg('test', callback_path="DeadBeef", query=query)
|
|
|
|
assert telegram._updater.bot.edit_message_text.call_count == 1
|
|
|
|
assert not log_has_re(r"TelegramError: .*", caplog)
|
|
|
|
|
|
|
|
telegram._updater.bot.edit_message_text = MagicMock(side_effect=BadRequest(""))
|
|
|
|
telegram._send_msg('test2', callback_path="DeadBeef", query=query)
|
|
|
|
assert telegram._updater.bot.edit_message_text.call_count == 1
|
|
|
|
assert log_has_re(r"TelegramError: .*", caplog)
|
|
|
|
|
|
|
|
telegram._updater.bot.edit_message_text = MagicMock(side_effect=TelegramError("DeadBEEF"))
|
|
|
|
telegram._send_msg('test3', callback_path="DeadBeef", query=query)
|
|
|
|
|
|
|
|
assert log_has_re(r"TelegramError: DeadBEEF! Giving up.*", caplog)
|
|
|
|
|
2017-11-09 19:02:41 +00:00
|
|
|
|
2018-07-03 18:26:48 +00:00
|
|
|
def test__send_msg_network_error(default_conf, mocker, caplog) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
bot = MagicMock()
|
|
|
|
bot.send_message = MagicMock(side_effect=NetworkError('Oh snap'))
|
2020-12-24 06:29:26 +00:00
|
|
|
telegram, _, _ = get_telegram_testobject(mocker, default_conf, mock=False)
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._updater = MagicMock()
|
|
|
|
telegram._updater.bot = bot
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
telegram._config['telegram']['enabled'] = True
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._send_msg('test')
|
2017-11-09 19:02:41 +00:00
|
|
|
|
|
|
|
# Bot should've tried to send it twice
|
|
|
|
assert len(bot.method_calls) == 2
|
2019-08-11 18:17:22 +00:00
|
|
|
assert log_has('Telegram NetworkError: Oh snap! Trying one more time.', caplog)
|
2020-12-20 15:35:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test__send_msg_keyboard(default_conf, mocker, caplog) -> None:
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
|
|
|
bot = MagicMock()
|
|
|
|
bot.send_message = MagicMock()
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2020-12-24 08:01:53 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
2020-12-20 15:35:54 +00:00
|
|
|
|
|
|
|
invalid_keys_list = [['/not_valid', '/profit'], ['/daily'], ['/alsoinvalid']]
|
|
|
|
default_keys_list = [['/daily', '/profit', '/balance'],
|
|
|
|
['/status', '/status table', '/performance'],
|
|
|
|
['/count', '/start', '/stop', '/help']]
|
|
|
|
default_keyboard = ReplyKeyboardMarkup(default_keys_list)
|
|
|
|
|
2021-05-29 06:12:25 +00:00
|
|
|
custom_keys_list = [['/daily', '/stats', '/balance', '/profit', '/profit 5'],
|
2020-12-20 15:35:54 +00:00
|
|
|
['/count', '/start', '/reload_config', '/help']]
|
|
|
|
custom_keyboard = ReplyKeyboardMarkup(custom_keys_list)
|
|
|
|
|
2020-12-21 08:52:10 +00:00
|
|
|
def init_telegram(freqtradebot):
|
2020-12-24 08:01:53 +00:00
|
|
|
telegram = Telegram(rpc, default_conf)
|
2020-12-21 08:52:10 +00:00
|
|
|
telegram._updater = MagicMock()
|
|
|
|
telegram._updater.bot = bot
|
|
|
|
return telegram
|
|
|
|
|
2020-12-20 15:50:42 +00:00
|
|
|
# no keyboard in config -> default keyboard
|
2020-12-21 08:52:10 +00:00
|
|
|
freqtradebot.config['telegram']['enabled'] = True
|
|
|
|
telegram = init_telegram(freqtradebot)
|
2020-12-20 15:35:54 +00:00
|
|
|
telegram._send_msg('test')
|
2020-12-20 16:13:03 +00:00
|
|
|
used_keyboard = bot.send_message.call_args[1]['reply_markup']
|
2020-12-20 15:35:54 +00:00
|
|
|
assert used_keyboard == default_keyboard
|
|
|
|
|
2020-12-20 15:50:42 +00:00
|
|
|
# invalid keyboard in config -> default keyboard
|
2020-12-20 21:36:56 +00:00
|
|
|
freqtradebot.config['telegram']['enabled'] = True
|
|
|
|
freqtradebot.config['telegram']['keyboard'] = invalid_keys_list
|
2020-12-23 15:00:01 +00:00
|
|
|
err_msg = re.escape("config.telegram.keyboard: Invalid commands for custom "
|
|
|
|
"Telegram keyboard: ['/not_valid', '/alsoinvalid']"
|
|
|
|
"\nvalid commands are: ") + r"*"
|
2020-12-22 12:01:01 +00:00
|
|
|
with pytest.raises(OperationalException, match=err_msg):
|
|
|
|
telegram = init_telegram(freqtradebot)
|
2020-12-20 15:35:54 +00:00
|
|
|
|
2020-12-20 15:50:42 +00:00
|
|
|
# valid keyboard in config -> custom keyboard
|
2020-12-20 21:36:56 +00:00
|
|
|
freqtradebot.config['telegram']['enabled'] = True
|
|
|
|
freqtradebot.config['telegram']['keyboard'] = custom_keys_list
|
2020-12-21 08:52:10 +00:00
|
|
|
telegram = init_telegram(freqtradebot)
|
2020-12-20 15:35:54 +00:00
|
|
|
telegram._send_msg('test')
|
2020-12-20 16:13:03 +00:00
|
|
|
used_keyboard = bot.send_message.call_args[1]['reply_markup']
|
2020-12-20 15:35:54 +00:00
|
|
|
assert used_keyboard == custom_keyboard
|
2020-12-22 12:01:01 +00:00
|
|
|
assert log_has("using custom keyboard from config.json: "
|
2021-05-29 06:12:25 +00:00
|
|
|
"[['/daily', '/stats', '/balance', '/profit', '/profit 5'], ['/count', "
|
2020-12-20 15:35:54 +00:00
|
|
|
"'/start', '/reload_config', '/help']]", caplog)
|