2017-10-01 12:40:40 +00:00
|
|
|
# pragma pylint: disable=missing-docstring
|
2017-09-08 21:10:22 +00:00
|
|
|
from datetime import datetime
|
2017-10-01 12:25:10 +00:00
|
|
|
from unittest.mock import MagicMock
|
2017-09-08 17:25:12 +00:00
|
|
|
|
2017-10-01 08:02:47 +00:00
|
|
|
import pytest
|
2017-09-08 17:25:12 +00:00
|
|
|
from jsonschema import validate
|
|
|
|
from telegram import Bot, Update, Message, Chat
|
|
|
|
|
2017-11-05 14:21:16 +00:00
|
|
|
from freqtrade import exchange
|
2017-09-28 21:26:28 +00:00
|
|
|
from freqtrade.main import init, create_trade
|
|
|
|
from freqtrade.misc import update_state, State, get_state, CONF_SCHEMA
|
|
|
|
from freqtrade.persistence import Trade
|
2017-11-04 17:55:41 +00:00
|
|
|
from freqtrade.rpc.telegram import (
|
|
|
|
_status, _profit, _forcesell, _performance, _start, _stop, _balance
|
|
|
|
)
|
2017-09-08 17:25:12 +00:00
|
|
|
|
|
|
|
|
2017-10-01 08:02:47 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def conf():
|
|
|
|
configuration = {
|
2017-09-08 17:25:12 +00:00
|
|
|
"max_open_trades": 3,
|
|
|
|
"stake_currency": "BTC",
|
|
|
|
"stake_amount": 0.05,
|
|
|
|
"dry_run": True,
|
|
|
|
"minimal_roi": {
|
|
|
|
"2880": 0.005,
|
|
|
|
"720": 0.01,
|
|
|
|
"0": 0.02
|
|
|
|
},
|
2017-09-17 20:21:46 +00:00
|
|
|
"bid_strategy": {
|
|
|
|
"ask_last_balance": 0.0
|
|
|
|
},
|
2017-10-06 10:22:04 +00:00
|
|
|
"exchange": {
|
|
|
|
"name": "bittrex",
|
2017-09-08 17:25:12 +00:00
|
|
|
"enabled": True,
|
|
|
|
"key": "key",
|
|
|
|
"secret": "secret",
|
|
|
|
"pair_whitelist": [
|
|
|
|
"BTC_ETH"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"telegram": {
|
|
|
|
"enabled": True,
|
|
|
|
"token": "token",
|
|
|
|
"chat_id": "0"
|
2017-09-08 22:31:40 +00:00
|
|
|
},
|
|
|
|
"initial_state": "running"
|
2017-09-08 17:25:12 +00:00
|
|
|
}
|
2017-10-01 08:02:47 +00:00
|
|
|
validate(configuration, CONF_SCHEMA)
|
|
|
|
return configuration
|
2017-09-08 17:25:12 +00:00
|
|
|
|
2017-10-31 23:26:32 +00:00
|
|
|
|
2017-10-01 08:02:47 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def update():
|
|
|
|
_update = Update(0)
|
|
|
|
_update.message = Message(0, 0, datetime.utcnow(), Chat(0, 0))
|
|
|
|
return _update
|
2017-09-08 18:03:45 +00:00
|
|
|
|
|
|
|
|
2017-10-01 08:02:47 +00:00
|
|
|
class MagicBot(MagicMock, Bot):
|
|
|
|
pass
|
2017-09-08 17:25:12 +00:00
|
|
|
|
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
def test_status_handle(conf, update, mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', conf)
|
|
|
|
mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
|
|
|
|
msg_mock = MagicMock()
|
2017-11-04 17:55:41 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.telegram',
|
|
|
|
_CONF=conf,
|
|
|
|
init=MagicMock(),
|
|
|
|
send_msg=msg_mock)
|
2017-10-01 12:25:10 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.exchange',
|
2017-10-01 21:28:09 +00:00
|
|
|
validate_pairs=MagicMock(),
|
2017-10-01 12:25:10 +00:00
|
|
|
get_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.07256061,
|
|
|
|
'ask': 0.072661,
|
|
|
|
'last': 0.07256061
|
2017-11-05 14:21:16 +00:00
|
|
|
}))
|
2017-10-01 12:25:10 +00:00
|
|
|
init(conf, 'sqlite://')
|
|
|
|
|
|
|
|
# Create some test data
|
2017-10-06 10:22:04 +00:00
|
|
|
trade = create_trade(15.0)
|
2017-10-01 12:25:10 +00:00
|
|
|
assert trade
|
|
|
|
Trade.session.add(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
|
2017-10-31 23:26:32 +00:00
|
|
|
# Trigger status while we have a fulfilled order for the open trade
|
|
|
|
_status(bot=MagicBot(), update=update)
|
|
|
|
|
2017-11-05 14:21:16 +00:00
|
|
|
assert msg_mock.call_count == 2
|
2017-10-01 12:25:10 +00:00
|
|
|
assert '[BTC_ETH]' in msg_mock.call_args_list[-1][0][0]
|
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
def test_profit_handle(conf, update, mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', conf)
|
|
|
|
mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
|
|
|
|
msg_mock = MagicMock()
|
2017-11-04 17:55:41 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.telegram',
|
|
|
|
_CONF=conf,
|
|
|
|
init=MagicMock(),
|
|
|
|
send_msg=msg_mock)
|
2017-10-01 12:25:10 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.exchange',
|
2017-10-01 21:28:09 +00:00
|
|
|
validate_pairs=MagicMock(),
|
2017-10-01 12:25:10 +00:00
|
|
|
get_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.07256061,
|
|
|
|
'ask': 0.072661,
|
|
|
|
'last': 0.07256061
|
2017-11-05 14:21:16 +00:00
|
|
|
}))
|
2017-10-01 12:25:10 +00:00
|
|
|
init(conf, 'sqlite://')
|
|
|
|
|
|
|
|
# Create some test data
|
2017-10-06 10:22:04 +00:00
|
|
|
trade = create_trade(15.0)
|
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
|
|
|
|
trade.update({
|
|
|
|
'id': 'mocked_limit_buy',
|
|
|
|
'type': 'LIMIT_BUY',
|
|
|
|
'pair': 'mocked',
|
|
|
|
'opened': datetime.utcnow(),
|
|
|
|
'rate': 0.07256061,
|
|
|
|
'amount': 206.43811673387373,
|
|
|
|
'remaining': 0.0,
|
|
|
|
'closed': datetime.utcnow(),
|
|
|
|
})
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
|
|
|
trade.update({
|
|
|
|
'id': 'mocked_limit_sell',
|
|
|
|
'type': 'LIMIT_SELL',
|
|
|
|
'pair': 'mocked',
|
|
|
|
'opened': datetime.utcnow(),
|
|
|
|
'rate': 0.0802134,
|
|
|
|
'amount': 206.43811673387373,
|
|
|
|
'remaining': 0.0,
|
|
|
|
'closed': datetime.utcnow(),
|
|
|
|
})
|
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
|
|
|
Trade.session.add(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
|
|
|
|
_profit(bot=MagicBot(), update=update)
|
|
|
|
assert msg_mock.call_count == 2
|
2017-11-01 01:20:55 +00:00
|
|
|
assert '*ROI:* `1.507013 (10.05%)`' in msg_mock.call_args_list[-1][0][0]
|
|
|
|
assert 'Best Performing:* `BTC_ETH: 10.05%`' in msg_mock.call_args_list[-1][0][0]
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
def test_forcesell_handle(conf, update, mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', conf)
|
|
|
|
mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
|
|
|
|
msg_mock = MagicMock()
|
2017-11-04 17:55:41 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.telegram',
|
|
|
|
_CONF=conf,
|
|
|
|
init=MagicMock(),
|
|
|
|
send_msg=msg_mock)
|
2017-10-01 12:25:10 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.exchange',
|
2017-10-01 21:28:09 +00:00
|
|
|
validate_pairs=MagicMock(),
|
2017-10-01 12:25:10 +00:00
|
|
|
get_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.07256061,
|
|
|
|
'ask': 0.072661,
|
|
|
|
'last': 0.07256061
|
2017-11-05 14:21:16 +00:00
|
|
|
}))
|
2017-10-01 12:25:10 +00:00
|
|
|
init(conf, 'sqlite://')
|
|
|
|
|
|
|
|
# Create some test data
|
2017-10-06 10:22:04 +00:00
|
|
|
trade = create_trade(15.0)
|
2017-10-01 12:25:10 +00:00
|
|
|
assert trade
|
2017-10-31 23:26:32 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
Trade.session.add(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
|
|
|
|
update.message.text = '/forcesell 1'
|
|
|
|
_forcesell(bot=MagicBot(), update=update)
|
|
|
|
|
|
|
|
assert msg_mock.call_count == 2
|
|
|
|
assert 'Selling [BTC/ETH]' in msg_mock.call_args_list[-1][0][0]
|
2017-11-05 14:21:16 +00:00
|
|
|
assert '0.072561 (profit: ~-0.64%)' in msg_mock.call_args_list[-1][0][0]
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
def test_performance_handle(conf, update, mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', conf)
|
|
|
|
mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
|
|
|
|
msg_mock = MagicMock()
|
2017-11-04 17:55:41 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.telegram',
|
|
|
|
_CONF=conf,
|
|
|
|
init=MagicMock(),
|
|
|
|
send_msg=msg_mock)
|
2017-10-01 12:25:10 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.exchange',
|
2017-10-01 21:28:09 +00:00
|
|
|
validate_pairs=MagicMock(),
|
2017-10-01 12:25:10 +00:00
|
|
|
get_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.07256061,
|
|
|
|
'ask': 0.072661,
|
|
|
|
'last': 0.07256061
|
2017-11-05 14:21:16 +00:00
|
|
|
}))
|
2017-10-01 12:25:10 +00:00
|
|
|
init(conf, 'sqlite://')
|
|
|
|
|
|
|
|
# Create some test data
|
2017-10-06 10:22:04 +00:00
|
|
|
trade = create_trade(15.0)
|
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
|
|
|
|
trade.update({
|
|
|
|
'id': 'mocked_limit_buy',
|
|
|
|
'type': 'LIMIT_BUY',
|
|
|
|
'pair': 'mocked',
|
|
|
|
'opened': datetime.utcnow(),
|
|
|
|
'rate': 0.07256061,
|
|
|
|
'amount': 206.43811673387373,
|
|
|
|
'remaining': 0.0,
|
|
|
|
'closed': datetime.utcnow(),
|
|
|
|
})
|
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
|
|
|
trade.update({
|
|
|
|
'id': 'mocked_limit_sell',
|
|
|
|
'type': 'LIMIT_SELL',
|
|
|
|
'pair': 'mocked',
|
|
|
|
'opened': datetime.utcnow(),
|
|
|
|
'rate': 0.0802134,
|
|
|
|
'amount': 206.43811673387373,
|
|
|
|
'remaining': 0.0,
|
|
|
|
'closed': datetime.utcnow(),
|
|
|
|
})
|
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
|
|
|
Trade.session.add(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
|
|
|
|
_performance(bot=MagicBot(), update=update)
|
|
|
|
assert msg_mock.call_count == 2
|
|
|
|
assert 'Performance' in msg_mock.call_args_list[-1][0][0]
|
2017-11-01 01:20:55 +00:00
|
|
|
assert '<code>BTC_ETH\t10.05%</code>' in msg_mock.call_args_list[-1][0][0]
|
2017-10-01 12:25:10 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
def test_start_handle(conf, update, mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', conf)
|
|
|
|
msg_mock = MagicMock()
|
2017-11-04 17:55:41 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.telegram',
|
|
|
|
_CONF=conf,
|
|
|
|
init=MagicMock(),
|
|
|
|
send_msg=msg_mock)
|
|
|
|
mocker.patch.multiple('freqtrade.main.exchange',
|
|
|
|
_CONF=conf,
|
|
|
|
init=MagicMock())
|
2017-10-01 12:25:10 +00:00
|
|
|
init(conf, 'sqlite://')
|
|
|
|
|
|
|
|
update_state(State.STOPPED)
|
|
|
|
assert get_state() == State.STOPPED
|
|
|
|
_start(bot=MagicBot(), update=update)
|
|
|
|
assert get_state() == State.RUNNING
|
|
|
|
assert msg_mock.call_count == 0
|
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
def test_stop_handle(conf, update, mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', conf)
|
|
|
|
msg_mock = MagicMock()
|
2017-11-04 17:55:41 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.telegram',
|
|
|
|
_CONF=conf,
|
|
|
|
init=MagicMock(),
|
|
|
|
send_msg=msg_mock)
|
|
|
|
mocker.patch.multiple('freqtrade.main.exchange',
|
|
|
|
_CONF=conf,
|
|
|
|
init=MagicMock())
|
2017-10-01 12:25:10 +00:00
|
|
|
init(conf, 'sqlite://')
|
|
|
|
|
|
|
|
update_state(State.RUNNING)
|
|
|
|
assert get_state() == State.RUNNING
|
|
|
|
_stop(bot=MagicBot(), update=update)
|
|
|
|
assert get_state() == State.STOPPED
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert 'Stopping trader' in msg_mock.call_args_list[0][0][0]
|
2017-10-29 08:10:00 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-29 08:10:00 +00:00
|
|
|
def test_balance_handle(conf, update, mocker):
|
|
|
|
mock_balance = [{
|
|
|
|
'Currency': 'BTC',
|
|
|
|
'Balance': 10.0,
|
|
|
|
'Available': 12.0,
|
|
|
|
'Pending': 0.0,
|
|
|
|
'CryptoAddress': 'XXXX'}]
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', conf)
|
|
|
|
msg_mock = MagicMock()
|
2017-11-04 17:55:41 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.telegram',
|
|
|
|
_CONF=conf,
|
|
|
|
init=MagicMock(),
|
|
|
|
send_msg=msg_mock)
|
2017-10-29 08:10:00 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.exchange',
|
|
|
|
get_balances=MagicMock(return_value=mock_balance))
|
|
|
|
|
|
|
|
_balance(bot=MagicBot(), update=update)
|
|
|
|
assert msg_mock.call_count == 1
|
|
|
|
assert '*Currency*: BTC' in msg_mock.call_args_list[0][0][0]
|
|
|
|
assert 'Balance' in msg_mock.call_args_list[0][0][0]
|