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
|
|
|
|
|
2017-11-05 15:26:03 +00:00
|
|
|
import re
|
2017-12-19 05:58:02 +00:00
|
|
|
from datetime import datetime
|
2019-04-08 17:59:54 +00:00
|
|
|
from random import choice, randint
|
|
|
|
from string import ascii_uppercase
|
2019-12-08 13:10:04 +00:00
|
|
|
from unittest.mock import ANY, MagicMock, PropertyMock
|
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
|
2018-07-04 07:31:35 +00:00
|
|
|
from telegram import Chat, Message, Update
|
2017-11-09 19:02:41 +00:00
|
|
|
from telegram.error import NetworkError
|
2017-09-08 17:25:12 +00:00
|
|
|
|
2017-11-09 22:51:32 +00:00
|
|
|
from freqtrade import __version__
|
2019-03-27 15:04:05 +00:00
|
|
|
from freqtrade.edge import PairInfo
|
2018-02-13 03:45:59 +00:00
|
|
|
from freqtrade.freqtradebot import FreqtradeBot
|
2017-09-28 21:26:28 +00:00
|
|
|
from freqtrade.persistence import Trade
|
2018-07-03 18:26:48 +00:00
|
|
|
from freqtrade.rpc import RPCMessageType
|
2018-07-04 07:31:35 +00:00
|
|
|
from freqtrade.rpc.telegram import Telegram, authorized_only
|
2018-02-13 03:45:59 +00:00
|
|
|
from freqtrade.state import State
|
2019-03-27 15:04:05 +00:00
|
|
|
from freqtrade.strategy.interface import SellType
|
2019-09-08 07:54:15 +00:00
|
|
|
from tests.conftest import (get_patched_freqtradebot, log_has, patch_exchange,
|
2019-10-26 11:24:26 +00:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
def __init__(self, freqtrade) -> None:
|
|
|
|
super().__init__(freqtrade)
|
|
|
|
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
|
|
|
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def test__init__(default_conf, mocker) -> None:
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock())
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2017-11-09 19:02:41 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(get_patched_freqtradebot(mocker, default_conf))
|
|
|
|
assert telegram._updater is None
|
|
|
|
assert telegram._config == default_conf
|
2017-11-07 21:27:44 +00:00
|
|
|
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def test_init(default_conf, mocker, caplog) -> None:
|
|
|
|
start_polling = MagicMock()
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock(return_value=start_polling))
|
2017-11-07 21:27:44 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
Telegram(get_patched_freqtradebot(mocker, default_conf))
|
|
|
|
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
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
message_str = "rpc.telegram is listening for following commands: [['status'], ['profit'], " \
|
2018-10-09 18:04:53 +00:00
|
|
|
"['balance'], ['start'], ['stop'], ['forcesell'], ['forcebuy'], " \
|
2019-11-17 14:22:14 +00:00
|
|
|
"['performance'], ['daily'], ['count'], ['reload_conf'], ['show_config'], " \
|
2019-03-27 11:59:59 +00:00
|
|
|
"['stopbuy'], ['whitelist'], ['blacklist'], ['edge'], ['help'], ['version']]"
|
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
|
|
|
|
|
|
|
|
|
|
|
def test_cleanup(default_conf, mocker) -> None:
|
|
|
|
updater_mock = MagicMock()
|
|
|
|
updater_mock.stop = MagicMock()
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Updater', updater_mock)
|
|
|
|
|
2018-06-08 22:15:04 +00:00
|
|
|
telegram = Telegram(get_patched_freqtradebot(mocker, default_conf))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram.cleanup()
|
|
|
|
assert telegram._updater.stop.call_count == 1
|
|
|
|
|
|
|
|
|
2018-03-02 14:48:18 +00:00
|
|
|
def test_authorized_only(default_conf, mocker, caplog) -> None:
|
2019-09-11 06:52:09 +00:00
|
|
|
patch_exchange(mocker)
|
2018-03-02 14:48:18 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
chat = Chat(0, 0)
|
2017-11-07 21:27:44 +00:00
|
|
|
update = Update(randint(1, 100))
|
|
|
|
update.message = Message(randint(1, 100), 0, datetime.utcnow(), chat)
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
bot = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(bot, (True, False))
|
|
|
|
dummy = DummyCls(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)
|
2018-02-13 03:45:59 +00:00
|
|
|
chat = Chat(0xdeadbeef, 0)
|
|
|
|
update = Update(randint(1, 100))
|
|
|
|
update.message = Message(randint(1, 100), 0, datetime.utcnow(), chat)
|
2017-11-07 21:27:44 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
bot = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(bot, (True, False))
|
|
|
|
dummy = DummyCls(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
|
|
|
|
2018-03-02 14:48:18 +00:00
|
|
|
def test_authorized_only_exception(default_conf, mocker, caplog) -> None:
|
2018-06-17 19:46:56 +00:00
|
|
|
patch_exchange(mocker)
|
2018-03-02 14:48:18 +00:00
|
|
|
|
2017-11-09 19:02:41 +00:00
|
|
|
update = Update(randint(1, 100))
|
|
|
|
update.message = Message(randint(1, 100), 0, datetime.utcnow(), Chat(0, 0))
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
bot = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(bot, (True, False))
|
|
|
|
dummy = DummyCls(bot)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
def test_status(default_conf, update, mocker, fee, ticker,) -> 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
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
msg_mock = MagicMock()
|
2018-02-13 03:45:59 +00:00
|
|
|
status_table = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
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(),
|
|
|
|
'open_date_hum': arrow.utcnow().humanize,
|
|
|
|
'close_date': None,
|
|
|
|
'close_date_hum': 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,
|
2020-05-26 17:25:03 +00:00
|
|
|
'close_profit_pct': None,
|
2020-05-24 06:47:10 +00:00
|
|
|
'current_profit': -0.0059,
|
2020-05-26 17:25:03 +00:00
|
|
|
'current_profit_pct': -0.59,
|
2019-04-01 17:13:45 +00:00
|
|
|
'initial_stop_loss': 1.098e-05,
|
2019-03-28 11:09:07 +00:00
|
|
|
'stop_loss': 1.099e-05,
|
2020-05-17 08:52:20 +00:00
|
|
|
'sell_order_status': None,
|
2019-04-01 17:13:45 +00:00
|
|
|
'initial_stop_loss_pct': -0.05,
|
|
|
|
'stop_loss_pct': -0.01,
|
2018-06-22 01:54:10 +00:00
|
|
|
'open_order': '(limit buy rem=0.00000000)'
|
|
|
|
}]),
|
2018-02-13 03:45:59 +00:00
|
|
|
_status_table=status_table,
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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()
|
|
|
|
# /status table 2 3
|
|
|
|
context.args = ["table", "2", "3"]
|
|
|
|
telegram._status(update=update, context=context)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert status_table.call_count == 1
|
|
|
|
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
def test_status_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
|
|
|
)
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
status_table = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_status_table=status_table,
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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
|
|
|
|
2017-11-18 20:30:31 +00:00
|
|
|
assert msg_mock.call_count == 1
|
2019-02-25 19:17:27 +00:00
|
|
|
assert 'ETH/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
|
|
|
buy=MagicMock(return_value={'id': 'mocked_order_id'}),
|
|
|
|
get_fee=fee,
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
2017-11-05 15:26:03 +00:00
|
|
|
msg_mock = MagicMock()
|
2017-11-06 17:01:13 +00:00
|
|
|
mocker.patch.multiple(
|
2018-02-13 03:45:59 +00:00
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['stake_amount'] = 15.0
|
2019-10-26 11:24:26 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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
|
2020-02-08 20:02:52 +00:00
|
|
|
assert 'ETH/BTC' in fields[1]
|
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
|
|
|
)
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
|
|
|
trade.update(limit_sell_order)
|
|
|
|
|
|
|
|
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
|
|
|
|
assert 'Daily' in msg_mock.call_args_list[0][0][0]
|
|
|
|
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]
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
trade.update(limit_sell_order)
|
|
|
|
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
|
|
|
)
|
2017-10-01 12:25:10 +00:00
|
|
|
msg_mock = MagicMock()
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
|
|
|
# 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)
|
2018-02-13 03:45:59 +00:00
|
|
|
assert str('Daily Profit over the last 7 days') in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(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-29 07:38:12 +00:00
|
|
|
assert 'No closed trade' 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
|
2017-11-07 19:12:56 +00:00
|
|
|
trade.update(limit_buy_order)
|
2017-11-09 19:02:41 +00:00
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._profit(update=update, context=MagicMock())
|
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]
|
|
|
|
assert '∙ `-0.00000500 BTC (-0.50%)`' 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)
|
2017-11-07 19:12:56 +00:00
|
|
|
trade.update(limit_sell_order)
|
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._profit(update=update, context=MagicMock())
|
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]
|
2017-12-25 07:51:41 +00:00
|
|
|
assert '∙ `0.00006217 BTC (6.20%)`' in msg_mock.call_args_list[-1][0][0]
|
|
|
|
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]
|
|
|
|
assert '∙ `0.00006217 BTC (6.20%)`' in msg_mock.call_args_list[-1][0][0]
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-17 19:46:56 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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
|
|
|
|
assert '*USDT:*' in result
|
2019-05-11 13:27:09 +00:00
|
|
|
assert '*EUR:*' in result
|
2018-05-15 10:37:34 +00:00
|
|
|
assert 'Balance:' in result
|
|
|
|
assert 'Est. BTC:' in result
|
2018-10-10 20:01:22 +00:00
|
|
|
assert 'BTC: 12.00000000' in result
|
|
|
|
assert '*XRP:* not showing <1$ amount' 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
|
|
|
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-17 19:46:56 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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
|
2019-06-27 05:06:35 +00:00
|
|
|
assert 'All balances are zero.' in result
|
|
|
|
|
|
|
|
|
|
|
|
def test_balance_handle_empty_response_dry(default_conf, update, mocker) -> None:
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value={})
|
|
|
|
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
|
|
|
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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
|
2019-12-15 09:39:52 +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',
|
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,
|
|
|
|
})
|
|
|
|
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
|
|
|
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-17 19:46:56 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-17 19:46:56 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-17 19:46:56 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-17 19:46:56 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
|
|
|
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
|
2019-03-18 05:28:58 +00:00
|
|
|
assert 'No more buy will occur from now. Run /reload_conf to reset.' \
|
2019-03-17 18:36:02 +00:00
|
|
|
in msg_mock.call_args_list[0][0][0]
|
|
|
|
|
|
|
|
|
2018-06-09 02:29:48 +00:00
|
|
|
def test_reload_conf_handle(default_conf, update, mocker) -> None:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-13 13:29:27 +00:00
|
|
|
_send_msg=msg_mock
|
2018-06-09 02:29:48 +00:00
|
|
|
)
|
|
|
|
|
2018-06-17 19:46:56 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-06-09 02:29:48 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
|
|
|
freqtradebot.state = State.RUNNING
|
|
|
|
assert freqtradebot.state == State.RUNNING
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._reload_conf(update=update, context=MagicMock())
|
2018-06-09 02:29:48 +00:00
|
|
|
assert freqtradebot.state == State.RELOAD_CONF
|
|
|
|
assert msg_mock.call_count == 1
|
2018-06-22 01:32:45 +00:00
|
|
|
assert 'reloading config' in msg_mock.call_args_list[0][0][0]
|
2018-06-09 02:29:48 +00:00
|
|
|
|
|
|
|
|
2019-09-10 21:18:07 +00:00
|
|
|
def test_forcesell_handle(default_conf, update, ticker, fee,
|
2019-10-26 11:24:26 +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)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
|
|
|
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,
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(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"]
|
|
|
|
telegram._forcesell(update=update, context=context)
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2017-11-18 20:30:31 +00:00
|
|
|
assert rpc_mock.call_count == 2
|
2018-07-03 18:26:48 +00:00
|
|
|
last_msg = rpc_mock.call_args_list[-1][0][0]
|
|
|
|
assert {
|
|
|
|
'type': RPCMessageType.SELL_NOTIFICATION,
|
|
|
|
'exchange': 'Bittrex',
|
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'profit',
|
2020-02-23 13:27:03 +00:00
|
|
|
'limit': 1.173e-05,
|
|
|
|
'amount': 91.07468123861567,
|
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,
|
|
|
|
'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',
|
|
|
|
'fiat_currency': 'USD',
|
2019-12-08 13:10:04 +00:00
|
|
|
'sell_reason': SellType.FORCE_SELL.value,
|
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': ANY,
|
2018-07-03 18:26:48 +00:00
|
|
|
} == last_msg
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
|
2019-09-10 21:18:07 +00:00
|
|
|
def test_forcesell_down_handle(default_conf, update, ticker, fee,
|
2019-10-26 11:24:26 +00:00
|
|
|
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)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
|
|
|
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,
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(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"]
|
|
|
|
telegram._forcesell(update=update, context=context)
|
2017-12-17 21:07:56 +00:00
|
|
|
|
|
|
|
assert rpc_mock.call_count == 2
|
2018-07-03 18:26:48 +00:00
|
|
|
|
|
|
|
last_msg = rpc_mock.call_args_list[-1][0][0]
|
|
|
|
assert {
|
|
|
|
'type': RPCMessageType.SELL_NOTIFICATION,
|
|
|
|
'exchange': 'Bittrex',
|
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'loss',
|
2020-02-23 13:27:03 +00:00
|
|
|
'limit': 1.043e-05,
|
|
|
|
'amount': 91.07468123861567,
|
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,
|
|
|
|
'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',
|
|
|
|
'fiat_currency': 'USD',
|
2019-12-08 13:10:04 +00:00
|
|
|
'sell_reason': SellType.FORCE_SELL.value,
|
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': 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)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock())
|
|
|
|
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,
|
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)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
2017-11-09 21:08:28 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtradebot.enter_positions()
|
2017-11-18 20:30:31 +00:00
|
|
|
rpc_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"]
|
|
|
|
telegram._forcesell(update=update, context=context)
|
2017-11-09 21:08:28 +00:00
|
|
|
|
2017-11-18 20:30:31 +00:00
|
|
|
assert rpc_mock.call_count == 4
|
2018-07-03 18:26:48 +00:00
|
|
|
msg = rpc_mock.call_args_list[0][0][0]
|
|
|
|
assert {
|
|
|
|
'type': RPCMessageType.SELL_NOTIFICATION,
|
|
|
|
'exchange': 'Bittrex',
|
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'loss',
|
2020-02-23 13:27:03 +00:00
|
|
|
'limit': 1.099e-05,
|
|
|
|
'amount': 91.07468123861567,
|
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,
|
|
|
|
'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',
|
|
|
|
'fiat_currency': 'USD',
|
2019-12-08 13:10:04 +00:00
|
|
|
'sell_reason': SellType.FORCE_SELL.value,
|
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': 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)
|
2017-11-09 19:02:41 +00:00
|
|
|
msg_mock = MagicMock()
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(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"]
|
|
|
|
telegram._forcesell(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 = []
|
|
|
|
telegram._forcesell(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
|
|
|
|
|
|
|
# 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"]
|
|
|
|
telegram._forcesell(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
|
|
|
|
|
|
|
|
2019-09-10 21:18:07 +00:00
|
|
|
def test_forcebuy_handle(default_conf, update, markets, mocker) -> None:
|
2018-10-09 18:04:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._send_msg', MagicMock())
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2018-10-09 18:04:53 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-03-05 18:46:03 +00:00
|
|
|
markets=PropertyMock(markets),
|
2018-10-09 18:04:53 +00:00
|
|
|
)
|
|
|
|
fbuy_mock = MagicMock(return_value=None)
|
|
|
|
mocker.patch('freqtrade.rpc.RPC._rpc_forcebuy', fbuy_mock)
|
|
|
|
|
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
2019-09-02 18:42:39 +00:00
|
|
|
# /forcebuy ETH/BTC
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["ETH/BTC"]
|
|
|
|
telegram._forcebuy(update=update, context=context)
|
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
|
|
|
|
|
|
|
|
# Reset and retry with specified price
|
|
|
|
fbuy_mock = MagicMock(return_value=None)
|
|
|
|
mocker.patch('freqtrade.rpc.RPC._rpc_forcebuy', fbuy_mock)
|
2019-09-02 18:42:39 +00:00
|
|
|
# /forcebuy ETH/BTC 0.055
|
|
|
|
context = MagicMock()
|
|
|
|
context.args = ["ETH/BTC", "0.055"]
|
|
|
|
telegram._forcebuy(update=update, context=context)
|
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
|
|
|
|
|
|
|
|
|
2019-09-10 21:18:07 +00:00
|
|
|
def test_forcebuy_handle_exception(default_conf, update, markets, mocker) -> None:
|
2018-10-09 18:04:53 +00:00
|
|
|
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
|
|
|
|
rpc_mock = mocker.patch('freqtrade.rpc.telegram.Telegram._send_msg', MagicMock())
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock())
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2018-10-09 18:04:53 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-03-05 18:46:03 +00:00
|
|
|
markets=PropertyMock(markets),
|
2018-10-09 18:04:53 +00:00
|
|
|
)
|
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
|
|
|
update.message.text = '/forcebuy ETH/Nonepair'
|
2019-09-02 18:42:39 +00:00
|
|
|
telegram._forcebuy(update=update, context=MagicMock())
|
2018-10-09 18:04:53 +00:00
|
|
|
|
|
|
|
assert rpc_mock.call_count == 1
|
|
|
|
assert rpc_mock.call_args_list[0][0][0] == 'Forcebuy not enabled.'
|
|
|
|
|
|
|
|
|
2018-04-15 10:09:12 +00:00
|
|
|
def test_performance_handle(default_conf, update, ticker, fee,
|
2019-10-26 11:24:26 +00:00
|
|
|
limit_buy_order, limit_sell_order, mocker) -> None:
|
2017-10-01 12:25:10 +00:00
|
|
|
msg_mock = MagicMock()
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
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
|
|
|
)
|
2019-10-26 11:24:26 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(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
|
2017-11-07 19:12:56 +00:00
|
|
|
trade.update(limit_buy_order)
|
2017-10-31 23:26:32 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
2017-11-07 19:12:56 +00:00
|
|
|
trade.update(limit_sell_order)
|
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]
|
2018-03-24 19:59:25 +00:00
|
|
|
assert '<code>ETH/BTC\t6.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
|
|
|
|
2019-10-26 11:24:26 +00:00
|
|
|
def test_count_handle(default_conf, update, ticker, fee, mocker) -> None:
|
2017-11-05 15:26:03 +00:00
|
|
|
msg_mock = MagicMock()
|
2017-11-06 17:01:13 +00:00
|
|
|
mocker.patch.multiple(
|
2018-02-13 03:45:59 +00:00
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
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
|
|
|
buy=MagicMock(return_value={'id': 'mocked_order_id'}),
|
2019-10-26 11:24:26 +00:00
|
|
|
get_fee=fee,
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
2019-10-26 11:24:26 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtradebot, (True, False))
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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
|
|
|
|
2018-05-03 09:18:35 +00:00
|
|
|
msg = '<pre> current max total stake\n--------- ----- -------------\n' \
|
|
|
|
' 1 {} {}</pre>'\
|
|
|
|
.format(
|
|
|
|
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
|
|
|
|
|
|
|
|
2018-11-10 19:15:06 +00:00
|
|
|
def test_whitelist_static(default_conf, update, mocker) -> None:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
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
|
|
|
}]
|
2018-11-10 19:15:06 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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"]
|
|
|
|
|
|
|
|
|
2019-03-27 15:04:05 +00:00
|
|
|
def test_edge_disabled(default_conf, update, mocker) -> None:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
))
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, edge_conf)
|
|
|
|
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
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]
|
|
|
|
|
2019-03-27 15:58:53 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def test_help_handle(default_conf, update, mocker) -> None:
|
2018-01-29 03:55:42 +00:00
|
|
|
msg_mock = MagicMock()
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
2018-06-17 19:46:56 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
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:
|
2017-11-09 22:51:32 +00:00
|
|
|
msg_mock = MagicMock()
|
2018-02-13 03:45:59 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
2018-06-08 01:49:09 +00:00
|
|
|
_send_msg=msg_mock
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
2018-06-17 19:46:56 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
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]
|
|
|
|
|
|
|
|
|
2019-11-17 14:12:53 +00:00
|
|
|
def test_show_config_handle(default_conf, update, mocker) -> None:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
|
|
|
|
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]
|
|
|
|
assert '*Exchange:* `bittrex`' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert '*Strategy:* `DefaultStrategy`' 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]
|
|
|
|
assert '*Exchange:* `bittrex`' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert '*Strategy:* `DefaultStrategy`' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert '*Initial Stoploss:* `-0.1`' in msg_mock.call_args_list[0][0][0]
|
2019-11-17 14:12:53 +00:00
|
|
|
|
|
|
|
|
2018-07-12 15:50:11 +00:00
|
|
|
def test_send_msg_buy_notification(default_conf, mocker) -> None:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.BUY_NOTIFICATION,
|
|
|
|
'exchange': 'Bittrex',
|
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'limit': 1.099e-05,
|
2019-06-17 04:55:30 +00:00
|
|
|
'order_type': 'limit',
|
2018-07-12 15:50:11 +00:00
|
|
|
'stake_amount': 0.001,
|
|
|
|
'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)
|
2018-07-12 15:50:11 +00:00
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] \
|
2019-02-25 19:17:27 +00:00
|
|
|
== '*Bittrex:* Buying ETH/BTC\n' \
|
2020-02-08 20:02:52 +00:00
|
|
|
'*Amount:* `1333.33333333`\n' \
|
|
|
|
'*Open Rate:* `0.00001099`\n' \
|
|
|
|
'*Current Rate:* `0.00001099`\n' \
|
2020-03-07 12:01:26 +00:00
|
|
|
'*Total:* `(0.001000 BTC, 12.345 USD)`'
|
2020-02-08 20:02:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_send_msg_buy_cancel_notification(default_conf, mocker) -> None:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.BUY_CANCEL_NOTIFICATION,
|
|
|
|
'exchange': 'Bittrex',
|
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] \
|
2020-02-11 14:58:40 +00:00
|
|
|
== ('*Bittrex:* Cancelling Open Buy Order for ETH/BTC')
|
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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
2018-07-24 21:53:10 +00:00
|
|
|
old_convamount = telegram._fiat_converter.convert_amount
|
2018-07-22 12:35:59 +00:00
|
|
|
telegram._fiat_converter.convert_amount = lambda a, b, c: -24.812
|
2018-07-12 15:50:11 +00:00
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.SELL_NOTIFICATION,
|
|
|
|
'exchange': 'Binance',
|
|
|
|
'pair': 'KEY/ETH',
|
|
|
|
'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',
|
2019-12-08 13:10:04 +00:00
|
|
|
'sell_reason': SellType.STOP_LOSS.value,
|
|
|
|
'open_date': arrow.utcnow().shift(hours=-1),
|
|
|
|
'close_date': arrow.utcnow(),
|
2018-07-12 15:50:11 +00:00
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] \
|
2019-02-25 19:17:27 +00:00
|
|
|
== ('*Binance:* Selling KEY/ETH\n'
|
2018-12-04 18:58:43 +00:00
|
|
|
'*Amount:* `1333.33333333`\n'
|
|
|
|
'*Open Rate:* `0.00007500`\n'
|
|
|
|
'*Current Rate:* `0.00003201`\n'
|
2020-02-08 20:02:52 +00:00
|
|
|
'*Close Rate:* `0.00003201`\n'
|
2018-12-04 18:58:43 +00:00
|
|
|
'*Sell Reason:* `stop_loss`\n'
|
2019-12-11 05:48:40 +00:00
|
|
|
'*Duration:* `1:00:00 (60.0 min)`\n'
|
2020-02-08 20:02:52 +00:00
|
|
|
'*Profit:* `-57.41%` `(loss: -0.05746268 ETH / -24.812 USD)`')
|
2018-07-03 18:26:48 +00:00
|
|
|
|
2018-07-12 15:50:11 +00:00
|
|
|
msg_mock.reset_mock()
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.SELL_NOTIFICATION,
|
|
|
|
'exchange': 'Binance',
|
|
|
|
'pair': 'KEY/ETH',
|
|
|
|
'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',
|
2019-12-08 13:10:04 +00:00
|
|
|
'sell_reason': SellType.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
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] \
|
2019-02-25 19:17:27 +00:00
|
|
|
== ('*Binance:* Selling KEY/ETH\n'
|
2018-12-04 18:58:43 +00:00
|
|
|
'*Amount:* `1333.33333333`\n'
|
|
|
|
'*Open Rate:* `0.00007500`\n'
|
|
|
|
'*Current Rate:* `0.00003201`\n'
|
2020-02-08 20:02:52 +00:00
|
|
|
'*Close Rate:* `0.00003201`\n'
|
2018-12-04 18:58:43 +00:00
|
|
|
'*Sell Reason:* `stop_loss`\n'
|
2019-12-11 05:48:40 +00:00
|
|
|
'*Duration:* `1 day, 2:30:00 (1590.0 min)`\n'
|
2018-12-04 18:58:43 +00:00
|
|
|
'*Profit:* `-57.41%`')
|
2018-07-24 21:53:10 +00:00
|
|
|
# Reset singleton function to avoid random breaks
|
|
|
|
telegram._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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
old_convamount = telegram._fiat_converter.convert_amount
|
|
|
|
telegram._fiat_converter.convert_amount = lambda a, b, c: -24.812
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.SELL_CANCEL_NOTIFICATION,
|
|
|
|
'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
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] \
|
2020-03-24 16:12:24 +00:00
|
|
|
== ('*Binance:* Cancelling Open Sell Order for KEY/ETH. Reason: Cancelled on exchange')
|
2020-02-08 20:02:52 +00:00
|
|
|
|
|
|
|
msg_mock.reset_mock()
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.SELL_CANCEL_NOTIFICATION,
|
|
|
|
'exchange': 'Binance',
|
|
|
|
'pair': 'KEY/ETH',
|
2020-03-24 16:12:24 +00:00
|
|
|
'reason': 'timeout'
|
2020-02-08 20:02:52 +00:00
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] \
|
2020-03-24 16:12:24 +00:00
|
|
|
== ('*Binance:* Cancelling Open Sell Order for KEY/ETH. Reason: timeout')
|
2020-02-08 20:02:52 +00:00
|
|
|
# Reset singleton function to avoid random breaks
|
|
|
|
telegram._fiat_converter.convert_amount = old_convamount
|
|
|
|
|
|
|
|
|
2018-07-12 15:50:11 +00:00
|
|
|
def test_send_msg_status_notification(default_conf, mocker) -> None:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.STATUS_NOTIFICATION,
|
|
|
|
'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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.WARNING_NOTIFICATION,
|
|
|
|
'status': 'message'
|
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] == '*Warning:* `message`'
|
|
|
|
|
|
|
|
|
|
|
|
def test_custom_notification(default_conf, mocker) -> None:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.CUSTOM_NOTIFICATION,
|
|
|
|
'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:
|
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
with pytest.raises(NotImplementedError, match=r'Unknown message type: None'):
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': None,
|
|
|
|
})
|
2018-07-03 18:26:48 +00:00
|
|
|
|
|
|
|
|
2018-07-22 12:49:07 +00:00
|
|
|
def test_send_msg_buy_notification_no_fiat(default_conf, mocker) -> None:
|
2018-07-22 12:52:58 +00:00
|
|
|
del default_conf['fiat_display_currency']
|
2018-07-22 12:49:07 +00:00
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.BUY_NOTIFICATION,
|
|
|
|
'exchange': 'Bittrex',
|
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'limit': 1.099e-05,
|
2019-06-17 04:55:30 +00:00
|
|
|
'order_type': 'limit',
|
2018-07-22 12:49:07 +00:00
|
|
|
'stake_amount': 0.001,
|
|
|
|
'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
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] \
|
2019-02-25 19:17:27 +00:00
|
|
|
== '*Bittrex:* Buying ETH/BTC\n' \
|
2020-02-08 20:02:52 +00:00
|
|
|
'*Amount:* `1333.33333333`\n' \
|
|
|
|
'*Open Rate:* `0.00001099`\n' \
|
|
|
|
'*Current Rate:* `0.00001099`\n' \
|
|
|
|
'*Total:* `(0.001000 BTC)`'
|
2018-07-22 12:49:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_send_msg_sell_notification_no_fiat(default_conf, mocker) -> None:
|
2018-07-22 12:52:58 +00:00
|
|
|
del default_conf['fiat_display_currency']
|
2018-07-22 12:49:07 +00:00
|
|
|
msg_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.rpc.telegram.Telegram',
|
|
|
|
_init=MagicMock(),
|
|
|
|
_send_msg=msg_mock
|
|
|
|
)
|
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
telegram = Telegram(freqtradebot)
|
|
|
|
telegram.send_msg({
|
|
|
|
'type': RPCMessageType.SELL_NOTIFICATION,
|
|
|
|
'exchange': 'Binance',
|
|
|
|
'pair': 'KEY/ETH',
|
|
|
|
'gain': 'loss',
|
|
|
|
'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',
|
2019-12-08 13:10:04 +00:00
|
|
|
'sell_reason': SellType.STOP_LOSS.value,
|
|
|
|
'open_date': arrow.utcnow().shift(hours=-2, minutes=-35, seconds=-3),
|
|
|
|
'close_date': arrow.utcnow(),
|
2018-07-22 12:49:07 +00:00
|
|
|
})
|
|
|
|
assert msg_mock.call_args[0][0] \
|
2019-02-25 19:17:27 +00:00
|
|
|
== '*Binance:* Selling KEY/ETH\n' \
|
2018-07-22 12:49:07 +00:00
|
|
|
'*Amount:* `1333.33333333`\n' \
|
|
|
|
'*Open Rate:* `0.00007500`\n' \
|
|
|
|
'*Current Rate:* `0.00003201`\n' \
|
2020-02-08 20:02:52 +00:00
|
|
|
'*Close Rate:* `0.00003201`\n' \
|
2018-12-04 18:58:43 +00:00
|
|
|
'*Sell Reason:* `stop_loss`\n' \
|
2019-12-11 05:48:40 +00:00
|
|
|
'*Duration:* `2:35:03 (155.1 min)`\n' \
|
2018-07-22 12:49:07 +00:00
|
|
|
'*Profit:* `-57.41%`'
|
|
|
|
|
|
|
|
|
2018-07-03 18:26:48 +00:00
|
|
|
def test__send_msg(default_conf, mocker) -> 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()
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
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
|
|
|
|
|
|
|
|
|
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'))
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtradebot = get_patched_freqtradebot(mocker, default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
telegram = Telegram(freqtradebot)
|
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)
|