freqtrade_origin/freqtrade/tests/rpc/test_rpc.py

814 lines
27 KiB
Python
Raw Normal View History

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=invalid-sequence-index, invalid-name, too-many-arguments
from datetime import datetime
from unittest.mock import ANY, MagicMock, PropertyMock
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
import pytest
2018-10-10 20:03:54 +00:00
from numpy import isnan
2018-06-08 02:52:50 +00:00
from freqtrade import DependencyException, TemporaryError
from freqtrade.edge import PairInfo
2018-02-13 03:45:59 +00:00
from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.persistence import Trade
from freqtrade.rpc import RPC, RPCException
from freqtrade.rpc.fiat_convert import CryptoToFiatConverter
2018-02-13 03:45:59 +00:00
from freqtrade.state import State
2019-05-11 07:42:30 +00:00
from freqtrade.tests.conftest import patch_exchange, patch_get_signal
2018-02-13 03:45:59 +00:00
# Functions for recurrent object patching
def prec_satoshi(a, b) -> float:
"""
:return: True if A and B differs less than one satoshi.
"""
return abs(a - b) < 0.00000001
2018-02-13 03:45:59 +00:00
# Unit tests
2018-06-16 23:23:12 +00:00
def test_rpc_trade_status(default_conf, ticker, fee, markets, mocker) -> None:
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
_load_markets=MagicMock(return_value={}),
get_ticker=ticker,
2018-06-16 23:23:12 +00:00
get_fee=fee,
2019-03-05 18:46:03 +00:00
markets=PropertyMock(return_value=markets)
2018-02-13 03:45:59 +00:00
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
freqtradebot.state = State.RUNNING
2018-06-08 02:52:50 +00:00
with pytest.raises(RPCException, match=r'.*no active trade*'):
rpc._rpc_trade_status()
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
results = rpc._rpc_trade_status()
assert {
'trade_id': 1,
'pair': 'ETH/BTC',
'base_currency': 'BTC',
2019-05-06 04:56:07 +00:00
'open_date': ANY,
'open_date_hum': ANY,
'close_date': None,
'close_date_hum': None,
'open_rate': 1.099e-05,
'close_rate': None,
'current_rate': 1.098e-05,
'amount': 90.99181074,
2019-04-04 10:06:45 +00:00
'stake_amount': 0.001,
'close_profit': None,
'current_profit': -0.59,
'stop_loss': 0.0,
'initial_stop_loss': 0.0,
'initial_stop_loss_pct': None,
'stop_loss_pct': None,
'open_order': '(limit buy rem=0.00000000)'
} == results[0]
mocker.patch('freqtrade.exchange.Exchange.get_ticker',
MagicMock(side_effect=DependencyException(f"Pair 'ETH/BTC' not available")))
# invalidate ticker cache
rpc._freqtrade.exchange._cached_ticker = {}
results = rpc._rpc_trade_status()
assert isnan(results[0]['current_profit'])
assert isnan(results[0]['current_rate'])
assert {
'trade_id': 1,
'pair': 'ETH/BTC',
'base_currency': 'BTC',
2019-05-06 04:56:07 +00:00
'open_date': ANY,
'open_date_hum': ANY,
'close_date': None,
'close_date_hum': None,
'open_rate': 1.099e-05,
'close_rate': None,
'current_rate': ANY,
'amount': 90.99181074,
2019-04-04 10:06:45 +00:00
'stake_amount': 0.001,
'close_profit': None,
'current_profit': ANY,
'stop_loss': 0.0,
'initial_stop_loss': 0.0,
'initial_stop_loss_pct': None,
'stop_loss_pct': None,
'open_order': '(limit buy rem=0.00000000)'
} == results[0]
2018-06-16 23:23:12 +00:00
def test_rpc_status_table(default_conf, ticker, fee, markets, mocker) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
get_ticker=ticker,
2018-06-16 23:23:12 +00:00
get_fee=fee,
2019-03-05 18:46:03 +00:00
markets=PropertyMock(return_value=markets)
2018-02-13 03:45:59 +00:00
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
freqtradebot.state = State.RUNNING
with pytest.raises(RPCException, match=r'.*no active order*'):
2018-06-08 02:52:50 +00:00
rpc._rpc_status_table()
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
2018-06-08 02:52:50 +00:00
result = rpc._rpc_status_table()
2019-05-20 17:48:12 +00:00
assert 'instantly' in result['Since'].all()
2018-03-24 19:59:09 +00:00
assert 'ETH/BTC' in result['Pair'].all()
2018-02-13 03:45:59 +00:00
assert '-0.59%' in result['Profit'].all()
mocker.patch('freqtrade.exchange.Exchange.get_ticker',
MagicMock(side_effect=DependencyException(f"Pair 'ETH/BTC' not available")))
# invalidate ticker cache
rpc._freqtrade.exchange._cached_ticker = {}
result = rpc._rpc_status_table()
2019-05-20 17:48:12 +00:00
assert 'instantly' in result['Since'].all()
assert 'ETH/BTC' in result['Pair'].all()
assert 'nan%' in result['Profit'].all()
def test_rpc_daily_profit(default_conf, update, ticker, fee,
2018-06-16 23:23:12 +00:00
limit_buy_order, limit_sell_order, markets, mocker) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
get_ticker=ticker,
2018-06-16 23:23:12 +00:00
get_fee=fee,
2019-03-05 18:46:03 +00:00
markets=PropertyMock(return_value=markets)
2018-02-13 03:45:59 +00:00
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
stake_currency = default_conf['stake_currency']
fiat_display_currency = default_conf['fiat_display_currency']
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
2018-07-21 18:44:38 +00:00
rpc._fiat_converter = CryptoToFiatConverter()
# Create some test data
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
trade = Trade.query.first()
assert trade
# Simulate buy & sell
trade.update(limit_buy_order)
trade.update(limit_sell_order)
trade.close_date = datetime.utcnow()
trade.is_open = False
# Try valid data
update.message.text = '/daily 2'
2018-06-08 02:52:50 +00:00
days = rpc._rpc_daily_profit(7, stake_currency, fiat_display_currency)
assert len(days) == 7
for day in days:
# [datetime.date(2018, 1, 11), '0.00000000 BTC', '0.000 USD']
assert (day[1] == '0.00000000 BTC' or
day[1] == '0.00006217 BTC')
assert (day[2] == '0.000 USD' or
day[2] == '0.933 USD')
# ensure first day is current date
assert str(days[0][0]) == str(datetime.utcnow().date())
# Try invalid data
2018-06-08 02:52:50 +00:00
with pytest.raises(RPCException, match=r'.*must be an integer greater than 0*'):
rpc._rpc_daily_profit(0, stake_currency, fiat_display_currency)
def test_rpc_trade_statistics(default_conf, ticker, ticker_sell_up, fee,
2018-06-16 23:23:12 +00:00
limit_buy_order, limit_sell_order, markets, mocker) -> None:
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
'freqtrade.rpc.fiat_convert.Market',
2018-02-13 03:45:59 +00:00
ticker=MagicMock(return_value={'price_usd': 15000.0}),
)
patch_exchange(mocker)
2018-07-21 18:44:38 +00:00
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
get_ticker=ticker,
2018-06-16 23:23:12 +00:00
get_fee=fee,
2019-03-05 18:46:03 +00:00
markets=PropertyMock(return_value=markets)
2018-02-13 03:45:59 +00:00
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
stake_currency = default_conf['stake_currency']
fiat_display_currency = default_conf['fiat_display_currency']
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
2018-07-21 18:44:38 +00:00
rpc._fiat_converter = CryptoToFiatConverter()
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
with pytest.raises(RPCException, match=r'.*no closed trade*'):
rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
# Create some test data
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
trade = Trade.query.first()
# Simulate fulfilled LIMIT_BUY order for trade
trade.update(limit_buy_order)
2018-02-13 03:45:59 +00:00
# Update the ticker with a market going up
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
2018-02-13 03:45:59 +00:00
get_ticker=ticker_sell_up
)
trade.update(limit_sell_order)
trade.close_date = datetime.utcnow()
trade.is_open = False
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
trade = Trade.query.first()
# Simulate fulfilled LIMIT_BUY order for trade
trade.update(limit_buy_order)
# Update the ticker with a market going up
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
get_ticker=ticker_sell_up
)
trade.update(limit_sell_order)
trade.close_date = datetime.utcnow()
trade.is_open = False
2018-06-08 02:52:50 +00:00
stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
assert prec_satoshi(stats['profit_closed_coin'], 6.217e-05)
assert prec_satoshi(stats['profit_closed_percent'], 6.2)
assert prec_satoshi(stats['profit_closed_fiat'], 0.93255)
assert prec_satoshi(stats['profit_all_coin'], 5.632e-05)
assert prec_satoshi(stats['profit_all_percent'], 2.81)
assert prec_satoshi(stats['profit_all_fiat'], 0.8448)
assert stats['trade_count'] == 2
assert stats['first_trade_date'] == 'just now'
assert stats['latest_trade_date'] == 'just now'
assert stats['avg_duration'] == '0:00:00'
2018-03-24 19:59:09 +00:00
assert stats['best_pair'] == 'ETH/BTC'
assert prec_satoshi(stats['best_rate'], 6.2)
# Test non-available pair
mocker.patch('freqtrade.exchange.Exchange.get_ticker',
MagicMock(side_effect=DependencyException(f"Pair 'ETH/BTC' not available")))
# invalidate ticker cache
rpc._freqtrade.exchange._cached_ticker = {}
stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
assert stats['trade_count'] == 2
assert stats['first_trade_date'] == 'just now'
assert stats['latest_trade_date'] == 'just now'
assert stats['avg_duration'] == '0:00:00'
assert stats['best_pair'] == 'ETH/BTC'
assert prec_satoshi(stats['best_rate'], 6.2)
assert isnan(stats['profit_all_coin'])
2018-10-10 20:03:54 +00:00
# Test that rpc_trade_statistics can handle trades that lacks
# trade.open_rate (it is set to None)
2018-06-16 23:23:12 +00:00
def test_rpc_trade_statistics_closed(mocker, default_conf, ticker, fee, markets,
ticker_sell_up, limit_buy_order, limit_sell_order):
patch_exchange(mocker)
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
'freqtrade.rpc.fiat_convert.Market',
2018-02-13 03:45:59 +00:00
ticker=MagicMock(return_value={'price_usd': 15000.0}),
)
mocker.patch('freqtrade.rpc.fiat_convert.CryptoToFiatConverter._find_price',
return_value=15000.0)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
get_ticker=ticker,
2018-06-16 23:23:12 +00:00
get_fee=fee,
2019-03-05 18:46:03 +00:00
markets=PropertyMock(return_value=markets)
2018-02-13 03:45:59 +00:00
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
stake_currency = default_conf['stake_currency']
fiat_display_currency = default_conf['fiat_display_currency']
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
# Create some test data
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
trade = Trade.query.first()
# Simulate fulfilled LIMIT_BUY order for trade
trade.update(limit_buy_order)
# Update the ticker with a market going up
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
get_ticker=ticker_sell_up,
get_fee=fee
2018-02-13 03:45:59 +00:00
)
trade.update(limit_sell_order)
trade.close_date = datetime.utcnow()
trade.is_open = False
for trade in Trade.query.order_by(Trade.id).all():
trade.open_rate = None
2018-06-08 02:52:50 +00:00
stats = rpc._rpc_trade_statistics(stake_currency, fiat_display_currency)
assert prec_satoshi(stats['profit_closed_coin'], 0)
assert prec_satoshi(stats['profit_closed_percent'], 0)
assert prec_satoshi(stats['profit_closed_fiat'], 0)
assert prec_satoshi(stats['profit_all_coin'], 0)
assert prec_satoshi(stats['profit_all_percent'], 0)
assert prec_satoshi(stats['profit_all_fiat'], 0)
assert stats['trade_count'] == 1
assert stats['first_trade_date'] == 'just now'
assert stats['latest_trade_date'] == 'just now'
assert stats['avg_duration'] == '0:00:00'
2018-03-24 19:59:09 +00:00
assert stats['best_pair'] == 'ETH/BTC'
assert prec_satoshi(stats['best_rate'], 6.2)
2019-07-03 18:07:26 +00:00
def test_rpc_balance_handle_error(default_conf, mocker):
2018-05-14 21:31:56 +00:00
mock_balance = {
'BTC': {
'free': 10.0,
'total': 12.0,
'used': 2.0,
2018-02-13 03:45:59 +00:00
},
2018-05-14 21:31:56 +00:00
'ETH': {
2018-08-08 19:55:48 +00:00
'free': 1.0,
'total': 5.0,
'used': 4.0,
2018-02-13 03:45:59 +00:00
}
2018-05-14 21:31:56 +00:00
}
2018-08-08 19:55:48 +00:00
# ETH will be skipped due to mocked Error below
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
'freqtrade.rpc.fiat_convert.Market',
2018-02-13 03:45:59 +00:00
ticker=MagicMock(return_value={'price_usd': 15000.0}),
)
patch_exchange(mocker)
2018-07-21 18:44:38 +00:00
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
2018-08-08 19:55:48 +00:00
get_balances=MagicMock(return_value=mock_balance),
get_ticker=MagicMock(side_effect=TemporaryError('Could not load ticker due to xxx'))
2018-02-13 03:45:59 +00:00
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
2018-07-21 18:44:38 +00:00
rpc._fiat_converter = CryptoToFiatConverter()
2018-06-22 02:08:51 +00:00
result = rpc._rpc_balance(default_conf['fiat_display_currency'])
assert prec_satoshi(result['total'], 12)
assert prec_satoshi(result['value'], 180000)
assert 'USD' == result['symbol']
assert result['currencies'] == [{
'currency': 'BTC',
'available': 10.0,
'balance': 12.0,
'pending': 2.0,
'est_btc': 12.0,
}]
2018-08-08 19:54:52 +00:00
assert result['total'] == 12.0
2019-07-03 18:07:26 +00:00
def test_rpc_balance_handle(default_conf, mocker):
mock_balance = {
'BTC': {
'free': 10.0,
'total': 12.0,
'used': 2.0,
},
'ETH': {
'free': 1.0,
'total': 5.0,
'used': 4.0,
},
'PAX': {
'free': 5.0,
'total': 10.0,
'used': 5.0,
}
}
mocker.patch.multiple(
'freqtrade.rpc.fiat_convert.Market',
ticker=MagicMock(return_value={'price_usd': 15000.0}),
)
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=mock_balance),
get_ticker=MagicMock(
side_effect=lambda p, r: {'bid': 100} if p == "BTC/PAX" else {'bid': 0.01}),
get_valid_pair_combination=MagicMock(
side_effect=lambda a, b: f"{b}/{a}" if a == "PAX" else f"{a}/{b}")
)
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
rpc._fiat_converter = CryptoToFiatConverter()
result = rpc._rpc_balance(default_conf['fiat_display_currency'])
assert prec_satoshi(result['total'], 12.15)
assert prec_satoshi(result['value'], 182250)
assert 'USD' == result['symbol']
assert result['currencies'] == [
{'currency': 'BTC',
'available': 10.0,
'balance': 12.0,
'pending': 2.0,
'est_btc': 12.0,
},
{'available': 1.0,
'balance': 5.0,
'currency': 'ETH',
'est_btc': 0.05,
'pending': 4.0
},
{'available': 5.0,
'balance': 10.0,
'currency': 'PAX',
'est_btc': 0.1,
'pending': 5.0}
]
assert result['total'] == 12.15
2018-02-13 03:45:59 +00:00
def test_rpc_start(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
2018-02-13 03:45:59 +00:00
get_ticker=MagicMock()
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
freqtradebot.state = State.STOPPED
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
result = rpc._rpc_start()
assert {'status': 'starting trader ...'} == result
assert freqtradebot.state == State.RUNNING
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
result = rpc._rpc_start()
assert {'status': 'already running'} == result
assert freqtradebot.state == State.RUNNING
2018-02-13 03:45:59 +00:00
def test_rpc_stop(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
2018-02-13 03:45:59 +00:00
get_ticker=MagicMock()
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
freqtradebot.state = State.RUNNING
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
result = rpc._rpc_stop()
assert {'status': 'stopping trader ...'} == result
assert freqtradebot.state == State.STOPPED
2018-02-13 03:45:59 +00:00
2018-06-08 02:52:50 +00:00
result = rpc._rpc_stop()
assert {'status': 'already stopped'} == result
assert freqtradebot.state == State.STOPPED
2018-02-13 03:45:59 +00:00
2019-03-17 18:36:02 +00:00
def test_rpc_stopbuy(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_ticker=MagicMock()
)
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
freqtradebot.state = State.RUNNING
assert freqtradebot.config['max_open_trades'] != 0
result = rpc._rpc_stopbuy()
2019-03-18 05:28:58 +00:00
assert {'status': 'No more buy will occur from now. Run /reload_conf to reset.'} == result
2019-03-17 18:36:02 +00:00
assert freqtradebot.config['max_open_trades'] == 0
2018-06-16 23:23:12 +00:00
def test_rpc_forcesell(default_conf, ticker, fee, mocker, markets) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
cancel_order_mock = MagicMock()
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
2018-02-13 03:45:59 +00:00
get_ticker=ticker,
cancel_order=cancel_order_mock,
get_order=MagicMock(
return_value={
'status': 'closed',
'type': 'limit',
'side': 'buy'
2018-02-13 03:45:59 +00:00
}
2018-04-21 17:39:18 +00:00
),
get_fee=fee,
2019-03-05 18:46:03 +00:00
markets=PropertyMock(return_value=markets)
2018-02-13 03:45:59 +00:00
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
freqtradebot.state = State.STOPPED
with pytest.raises(RPCException, match=r'.*trader is not running*'):
2018-06-08 02:52:50 +00:00
rpc._rpc_forcesell(None)
2018-02-13 03:45:59 +00:00
freqtradebot.state = State.RUNNING
with pytest.raises(RPCException, match=r'.*invalid argument*'):
2018-06-08 02:52:50 +00:00
rpc._rpc_forcesell(None)
2018-02-13 03:45:59 +00:00
2019-04-30 04:25:02 +00:00
msg = rpc._rpc_forcesell('all')
assert msg == {'result': 'Created sell orders for all open trades.'}
2018-02-13 03:45:59 +00:00
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
2019-04-30 04:25:02 +00:00
msg = rpc._rpc_forcesell('all')
assert msg == {'result': 'Created sell orders for all open trades.'}
2018-02-13 03:45:59 +00:00
2019-04-30 04:25:02 +00:00
msg = rpc._rpc_forcesell('1')
2019-05-01 14:21:14 +00:00
assert msg == {'result': 'Created sell order for trade 1.'}
2018-02-13 03:45:59 +00:00
freqtradebot.state = State.STOPPED
with pytest.raises(RPCException, match=r'.*trader is not running*'):
2018-06-08 02:52:50 +00:00
rpc._rpc_forcesell(None)
2018-02-13 03:45:59 +00:00
with pytest.raises(RPCException, match=r'.*trader is not running*'):
2018-06-08 02:52:50 +00:00
rpc._rpc_forcesell('all')
2018-02-13 03:45:59 +00:00
freqtradebot.state = State.RUNNING
2018-02-13 03:45:59 +00:00
assert cancel_order_mock.call_count == 0
# make an limit-buy open trade
trade = Trade.query.filter(Trade.id == '1').first()
filled_amount = trade.amount / 2
2018-02-13 03:45:59 +00:00
mocker.patch(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange.get_order',
2018-02-13 03:45:59 +00:00
return_value={
'status': 'open',
'type': 'limit',
'side': 'buy',
'filled': filled_amount
2018-02-13 03:45:59 +00:00
}
)
# check that the trade is called, which is done by ensuring exchange.cancel_order is called
# and trade amount is updated
2018-06-08 02:52:50 +00:00
rpc._rpc_forcesell('1')
2018-02-13 03:45:59 +00:00
assert cancel_order_mock.call_count == 1
assert trade.amount == filled_amount
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
trade = Trade.query.filter(Trade.id == '2').first()
amount = trade.amount
# make an limit-buy open trade, if there is no 'filled', don't sell it
mocker.patch(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange.get_order',
return_value={
'status': 'open',
'type': 'limit',
'side': 'buy',
'filled': None
}
)
# check that the trade is called, which is done by ensuring exchange.cancel_order is called
2019-04-30 04:25:02 +00:00
msg = rpc._rpc_forcesell('2')
2019-05-01 14:21:14 +00:00
assert msg == {'result': 'Created sell order for trade 2.'}
assert cancel_order_mock.call_count == 2
assert trade.amount == amount
2018-02-13 03:45:59 +00:00
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
2018-02-13 03:45:59 +00:00
# make an limit-sell open trade
mocker.patch(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange.get_order',
2018-02-13 03:45:59 +00:00
return_value={
'status': 'open',
'type': 'limit',
'side': 'sell'
2018-02-13 03:45:59 +00:00
}
)
2019-04-30 04:25:02 +00:00
msg = rpc._rpc_forcesell('3')
2019-05-01 14:21:14 +00:00
assert msg == {'result': 'Created sell order for trade 3.'}
2018-02-13 03:45:59 +00:00
# status quo, no exchange calls
assert cancel_order_mock.call_count == 2
2018-02-13 03:45:59 +00:00
def test_performance_handle(default_conf, ticker, limit_buy_order, fee,
2018-06-16 23:23:12 +00:00
limit_sell_order, markets, mocker) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
2018-02-13 03:45:59 +00:00
get_balances=MagicMock(return_value=ticker),
get_ticker=ticker,
2018-06-16 23:23:12 +00:00
get_fee=fee,
2019-03-05 18:46:03 +00:00
markets=PropertyMock(return_value=markets)
2018-02-13 03:45:59 +00:00
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
# Create some test data
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
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
2018-06-08 02:52:50 +00:00
res = rpc._rpc_performance()
assert len(res) == 1
2018-03-24 19:59:09 +00:00
assert res[0]['pair'] == 'ETH/BTC'
assert res[0]['count'] == 1
assert prec_satoshi(res[0]['profit'], 6.2)
2018-02-13 03:45:59 +00:00
2018-06-16 23:23:12 +00:00
def test_rpc_count(mocker, default_conf, ticker, fee, markets) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
2018-02-13 03:45:59 +00:00
mocker.patch.multiple(
2018-06-17 19:24:51 +00:00
'freqtrade.exchange.Exchange',
2018-02-13 03:45:59 +00:00
get_balances=MagicMock(return_value=ticker),
2018-04-21 17:39:18 +00:00
get_ticker=ticker,
get_fee=fee,
2019-03-05 18:46:03 +00:00
markets=PropertyMock(return_value=markets)
2018-02-13 03:45:59 +00:00
)
2018-06-07 03:27:55 +00:00
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
2018-02-13 03:45:59 +00:00
rpc = RPC(freqtradebot)
2019-04-06 18:01:29 +00:00
counts = rpc._rpc_count()
assert counts["current"] == 0
2018-02-13 03:45:59 +00:00
# Create some test data
2019-08-14 04:16:43 +00:00
freqtradebot.create_trades()
2019-04-06 18:01:29 +00:00
counts = rpc._rpc_count()
assert counts["current"] == 1
2018-10-09 18:04:53 +00:00
def test_rpcforcebuy(mocker, default_conf, ticker, fee, markets, limit_buy_order) -> None:
default_conf['forcebuy_enable'] = True
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
buy_mm = MagicMock(return_value={'id': limit_buy_order['id']})
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker),
get_ticker=ticker,
get_fee=fee,
2019-03-05 18:46:03 +00:00
markets=PropertyMock(return_value=markets),
2018-10-09 18:04:53 +00:00
buy=buy_mm
)
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
pair = 'ETH/BTC'
trade = rpc._rpc_forcebuy(pair, None)
assert isinstance(trade, Trade)
assert trade.pair == pair
assert trade.open_rate == ticker()['ask']
# Test buy duplicate
with pytest.raises(RPCException, match=r'position for ETH/BTC already open - id: 1'):
rpc._rpc_forcebuy(pair, 0.0001)
pair = 'XRP/BTC'
trade = rpc._rpc_forcebuy(pair, 0.0001)
assert isinstance(trade, Trade)
assert trade.pair == pair
assert trade.open_rate == 0.0001
# Test buy pair not with stakes
with pytest.raises(RPCException, match=r'Wrong pair selected. Please pairs with stake.*'):
rpc._rpc_forcebuy('XRP/ETH', 0.0001)
pair = 'XRP/BTC'
# Test not buying
default_conf['stake_amount'] = 0.0000001
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
pair = 'TKN/BTC'
trade = rpc._rpc_forcebuy(pair, None)
assert trade is None
def test_rpcforcebuy_stopped(mocker, default_conf) -> None:
default_conf['forcebuy_enable'] = True
default_conf['initial_state'] = 'stopped'
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
pair = 'ETH/BTC'
with pytest.raises(RPCException, match=r'trader is not running'):
rpc._rpc_forcebuy(pair, None)
def test_rpcforcebuy_disabled(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
patch_get_signal(freqtradebot, (True, False))
rpc = RPC(freqtradebot)
pair = 'ETH/BTC'
with pytest.raises(RPCException, match=r'Forcebuy not enabled.'):
rpc._rpc_forcebuy(pair, None)
2018-11-10 19:07:09 +00:00
def test_rpc_whitelist(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
rpc = RPC(freqtradebot)
ret = rpc._rpc_whitelist()
2018-12-03 19:38:15 +00:00
assert ret['method'] == 'StaticPairList'
2018-11-10 19:07:09 +00:00
assert ret['whitelist'] == default_conf['exchange']['pair_whitelist']
def test_rpc_whitelist_dynamic(mocker, default_conf) -> None:
patch_exchange(mocker)
2018-12-04 19:23:03 +00:00
default_conf['pairlist'] = {'method': 'VolumePairList',
'config': {'number_assets': 4}
}
2018-12-02 21:12:12 +00:00
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True))
2018-11-10 19:07:09 +00:00
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
rpc = RPC(freqtradebot)
ret = rpc._rpc_whitelist()
2018-12-03 19:31:25 +00:00
assert ret['method'] == 'VolumePairList'
assert ret['length'] == 4
2018-11-10 19:07:09 +00:00
assert ret['whitelist'] == default_conf['exchange']['pair_whitelist']
2019-03-24 15:09:20 +00:00
def test_rpc_blacklist(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
rpc = RPC(freqtradebot)
ret = rpc._rpc_blacklist(None)
2019-03-24 15:09:20 +00:00
assert ret['method'] == 'StaticPairList'
assert len(ret['blacklist']) == 2
2019-03-24 15:09:20 +00:00
assert ret['blacklist'] == default_conf['exchange']['pair_blacklist']
assert ret['blacklist'] == ['DOGE/BTC', 'HOT/BTC']
ret = rpc._rpc_blacklist(["ETH/BTC"])
assert ret['method'] == 'StaticPairList'
assert len(ret['blacklist']) == 3
assert ret['blacklist'] == default_conf['exchange']['pair_blacklist']
assert ret['blacklist'] == ['DOGE/BTC', 'HOT/BTC', 'ETH/BTC']
2019-03-27 13:04:33 +00:00
def test_rpc_edge_disabled(mocker, default_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
freqtradebot = FreqtradeBot(default_conf)
rpc = RPC(freqtradebot)
with pytest.raises(RPCException, match=r'Edge is not enabled.'):
2019-03-27 13:04:33 +00:00
rpc._rpc_edge()
def test_rpc_edge_enabled(mocker, edge_conf) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock(
return_value={
'E/F': PairInfo(-0.02, 0.66, 3.71, 0.50, 1.71, 10, 60),
}
))
freqtradebot = FreqtradeBot(edge_conf)
rpc = RPC(freqtradebot)
ret = rpc._rpc_edge()
assert len(ret) == 1
assert ret[0]['Pair'] == 'E/F'
assert ret[0]['Winrate'] == 0.66
assert ret[0]['Expectancy'] == 1.71
2019-03-27 13:04:33 +00:00
assert ret[0]['Stoploss'] == -0.02