2018-02-13 03:45:59 +00:00
|
|
|
# pragma pylint: disable=invalid-sequence-index, invalid-name, too-many-arguments
|
|
|
|
|
|
|
|
"""
|
|
|
|
Unit test file for rpc/rpc.py
|
|
|
|
"""
|
|
|
|
|
2018-02-01 06:05:23 +00:00
|
|
|
from datetime import datetime
|
2017-11-20 21:26:32 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
import pytest
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
from freqtrade.freqtradebot import FreqtradeBot
|
2018-02-01 06:05:23 +00:00
|
|
|
from freqtrade.persistence import Trade
|
2018-06-08 02:52:50 +00:00
|
|
|
from freqtrade.rpc.rpc import RPC, RPCException
|
2018-02-13 03:45:59 +00:00
|
|
|
from freqtrade.state import State
|
2018-03-17 23:42:24 +00:00
|
|
|
from freqtrade.tests.test_freqtradebot import patch_get_signal, patch_coinmarketcap
|
2018-02-01 06:05:23 +00:00
|
|
|
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
# Functions for recurrent object patching
|
|
|
|
def prec_satoshi(a, b) -> float:
|
2018-02-01 06:05:23 +00:00
|
|
|
"""
|
|
|
|
:return: True if A and B differs less than one satoshi.
|
|
|
|
"""
|
|
|
|
return abs(a - b) < 0.00000001
|
2017-11-18 20:30:31 +00:00
|
|
|
|
|
|
|
|
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:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Test rpc_trade_status() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
2018-03-17 23:42:24 +00:00
|
|
|
patch_coinmarketcap(mocker)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
2018-03-25 20:59:58 +00:00
|
|
|
get_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
|
|
|
get_markets=markets
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
2018-06-08 02:52:50 +00:00
|
|
|
with pytest.raises(RPCException, match=r'.*trader is not running*'):
|
|
|
|
rpc._rpc_trade_status()
|
2018-02-01 06:05:23 +00:00
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
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()
|
2018-02-01 06:05:23 +00:00
|
|
|
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtradebot.create_trade()
|
2018-06-08 02:52:50 +00:00
|
|
|
trades = rpc._rpc_trade_status()
|
|
|
|
trade = trades[0]
|
2018-02-01 06:05:23 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
result_message = [
|
|
|
|
'*Trade ID:* `1`\n'
|
|
|
|
'*Current Pair:* '
|
2018-03-24 19:59:09 +00:00
|
|
|
'[ETH/BTC](https://bittrex.com/Market/Index?MarketName=BTC-ETH)\n'
|
2018-02-13 03:45:59 +00:00
|
|
|
'*Open Since:* `just now`\n'
|
|
|
|
'*Amount:* `90.99181074`\n'
|
|
|
|
'*Open Rate:* `0.00001099`\n'
|
|
|
|
'*Close Rate:* `None`\n'
|
|
|
|
'*Current Rate:* `0.00001098`\n'
|
|
|
|
'*Close Profit:* `None`\n'
|
|
|
|
'*Current Profit:* `-0.59%`\n'
|
2018-03-25 20:25:26 +00:00
|
|
|
'*Open Order:* `(limit buy rem=0.00000000)`'
|
2018-02-13 03:45:59 +00:00
|
|
|
]
|
2018-06-08 02:52:50 +00:00
|
|
|
assert trades == result_message
|
2018-03-24 19:59:09 +00:00
|
|
|
assert trade.find('[ETH/BTC]') >= 0
|
2018-02-01 06:05:23 +00:00
|
|
|
|
|
|
|
|
2018-06-16 23:23:12 +00:00
|
|
|
def test_rpc_status_table(default_conf, ticker, fee, markets, mocker) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Test rpc_status_table() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
2018-03-17 23:42:24 +00:00
|
|
|
patch_coinmarketcap(mocker)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
2018-03-25 20:59:58 +00:00
|
|
|
get_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
|
|
|
get_markets=markets
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
2018-06-08 02:52:50 +00:00
|
|
|
with pytest.raises(RPCException, match=r'.*\*Status:\* `trader is not running``*'):
|
|
|
|
rpc._rpc_status_table()
|
2018-02-01 06:05:23 +00:00
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2018-06-08 02:52:50 +00:00
|
|
|
with pytest.raises(RPCException, match=r'.*\*Status:\* `no active order`*'):
|
|
|
|
rpc._rpc_status_table()
|
2018-02-01 06:05:23 +00:00
|
|
|
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtradebot.create_trade()
|
2018-06-08 02:52:50 +00:00
|
|
|
result = rpc._rpc_status_table()
|
2018-02-13 03:45:59 +00:00
|
|
|
assert 'just now' 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()
|
2018-02-01 06:05:23 +00:00
|
|
|
|
|
|
|
|
2018-04-15 10:09:12 +00:00
|
|
|
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:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Test rpc_daily_profit() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
2018-03-17 23:42:24 +00:00
|
|
|
patch_coinmarketcap(mocker, value={'price_usd': 15000.0})
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
2018-03-25 20:59:58 +00:00
|
|
|
get_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
|
|
|
get_markets=markets
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-01 06:05:23 +00:00
|
|
|
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-02-01 06:05:23 +00:00
|
|
|
# Create some test data
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtradebot.create_trade()
|
2018-02-01 06:05:23 +00:00
|
|
|
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)
|
2018-02-01 06:05:23 +00:00
|
|
|
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)
|
2018-02-01 06:05:23 +00:00
|
|
|
|
|
|
|
|
2018-04-15 10:09:12 +00:00
|
|
|
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
|
|
|
"""
|
|
|
|
Test rpc_trade_statistics() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
|
|
|
mocker.patch.multiple(
|
2018-03-17 23:42:24 +00:00
|
|
|
'freqtrade.fiat_convert.Market',
|
2018-02-13 03:45:59 +00:00
|
|
|
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
|
|
|
)
|
2018-02-01 06:05:23 +00:00
|
|
|
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
2018-03-25 20:59:58 +00:00
|
|
|
get_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
|
|
|
get_markets=markets
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-01 06:05:23 +00:00
|
|
|
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-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)
|
2018-02-01 06:05:23 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtradebot.create_trade()
|
2018-02-01 06:05:23 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
|
|
|
trade.update(limit_buy_order)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-02-01 06:05:23 +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
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
get_ticker=ticker_sell_up
|
|
|
|
)
|
2018-02-01 06:05:23 +00:00
|
|
|
trade.update(limit_sell_order)
|
|
|
|
trade.close_date = datetime.utcnow()
|
|
|
|
trade.is_open = False
|
|
|
|
|
2018-06-07 18:52:03 +00:00
|
|
|
freqtradebot.create_trade()
|
|
|
|
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',
|
2018-06-07 18:52:03 +00:00
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
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)
|
2018-02-01 06:05:23 +00:00
|
|
|
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)
|
2018-06-07 18:52:03 +00:00
|
|
|
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
|
2018-02-01 06:05:23 +00:00
|
|
|
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'
|
2018-02-01 06:05:23 +00:00
|
|
|
assert prec_satoshi(stats['best_rate'], 6.2)
|
|
|
|
|
|
|
|
|
|
|
|
# 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,
|
2018-04-15 10:09:12 +00:00
|
|
|
ticker_sell_up, limit_buy_order, limit_sell_order):
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Test rpc_trade_statistics() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
|
|
|
mocker.patch.multiple(
|
2018-03-17 23:42:24 +00:00
|
|
|
'freqtrade.fiat_convert.Market',
|
2018-02-13 03:45:59 +00:00
|
|
|
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
|
|
|
)
|
2018-02-01 06:05:23 +00:00
|
|
|
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
2018-03-25 20:59:58 +00:00
|
|
|
get_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
|
|
|
get_markets=markets
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-01 06:05:23 +00:00
|
|
|
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-02-01 06:05:23 +00:00
|
|
|
# Create some test data
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtradebot.create_trade()
|
2018-02-01 06:05:23 +00:00
|
|
|
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',
|
2018-02-13 03:45:59 +00:00
|
|
|
validate_pairs=MagicMock(),
|
2018-03-25 20:59:58 +00:00
|
|
|
get_ticker=ticker_sell_up,
|
2018-04-15 10:09:12 +00:00
|
|
|
get_fee=fee
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
2018-02-01 06:05:23 +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)
|
2018-02-01 06:05:23 +00:00
|
|
|
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'
|
2018-02-01 06:05:23 +00:00
|
|
|
assert prec_satoshi(stats['best_rate'], 6.2)
|
|
|
|
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def test_rpc_balance_handle(default_conf, mocker):
|
|
|
|
"""
|
|
|
|
Test rpc_balance() method
|
|
|
|
"""
|
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': {
|
|
|
|
'free': 0.0,
|
|
|
|
'total': 0.0,
|
|
|
|
'used': 0.0,
|
2018-02-13 03:45:59 +00:00
|
|
|
}
|
2018-05-14 21:31:56 +00:00
|
|
|
}
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
patch_get_signal(mocker, (True, False))
|
|
|
|
mocker.patch.multiple(
|
2018-03-17 23:42:24 +00:00
|
|
|
'freqtrade.fiat_convert.Market',
|
2018-02-13 03:45:59 +00:00
|
|
|
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
|
|
|
)
|
|
|
|
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
get_balances=MagicMock(return_value=mock_balance)
|
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
2018-02-01 06:05:23 +00:00
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
output, total, symbol, value = rpc._rpc_balance(default_conf['fiat_display_currency'])
|
|
|
|
assert prec_satoshi(total, 12)
|
|
|
|
assert prec_satoshi(value, 180000)
|
|
|
|
assert 'USD' in symbol
|
|
|
|
assert len(output) == 1
|
|
|
|
assert 'BTC' in output[0]['currency']
|
|
|
|
assert prec_satoshi(output[0]['available'], 10)
|
|
|
|
assert prec_satoshi(output[0]['balance'], 12)
|
|
|
|
assert prec_satoshi(output[0]['pending'], 2)
|
|
|
|
assert prec_satoshi(output[0]['est_btc'], 12)
|
2018-02-01 06:05:23 +00:00
|
|
|
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def test_rpc_start(mocker, default_conf) -> None:
|
|
|
|
"""
|
|
|
|
Test rpc_start() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
2018-03-17 23:42:24 +00:00
|
|
|
patch_coinmarketcap(mocker)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
get_ticker=MagicMock()
|
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
2018-04-06 07:57:08 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
result = rpc._rpc_start()
|
2018-02-13 03:45:59 +00:00
|
|
|
assert '`Starting trader ...`' in result
|
2018-04-06 07:57:08 +00:00
|
|
|
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()
|
2018-02-13 03:45:59 +00:00
|
|
|
assert '*Status:* `already running`' in result
|
2018-04-06 07:57:08 +00:00
|
|
|
assert freqtradebot.state == State.RUNNING
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_rpc_stop(mocker, default_conf) -> None:
|
|
|
|
"""
|
|
|
|
Test rpc_stop() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
2018-03-17 23:42:24 +00:00
|
|
|
patch_coinmarketcap(mocker)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
get_ticker=MagicMock()
|
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
2018-04-06 07:57:08 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
result = rpc._rpc_stop()
|
2018-02-13 03:45:59 +00:00
|
|
|
assert '`Stopping trader ...`' in result
|
2018-04-06 07:57:08 +00:00
|
|
|
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()
|
2018-02-13 03:45:59 +00:00
|
|
|
assert '*Status:* `already stopped`' in result
|
2018-04-06 07:57:08 +00:00
|
|
|
assert freqtradebot.state == State.STOPPED
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
2018-06-16 23:23:12 +00:00
|
|
|
def test_rpc_forcesell(default_conf, ticker, fee, mocker, markets) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Test rpc_forcesell() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
2018-03-17 23:42:24 +00:00
|
|
|
patch_coinmarketcap(mocker)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
get_ticker=ticker,
|
|
|
|
cancel_order=cancel_order_mock,
|
|
|
|
get_order=MagicMock(
|
|
|
|
return_value={
|
2018-03-25 20:25:26 +00:00
|
|
|
'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,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_markets=markets
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
2018-06-08 02:52:50 +00:00
|
|
|
with pytest.raises(RPCException, match=r'.*`trader is not running`*'):
|
|
|
|
rpc._rpc_forcesell(None)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
freqtradebot.state = State.RUNNING
|
2018-06-08 02:52:50 +00:00
|
|
|
with pytest.raises(RPCException, match=r'.*Invalid argument.*'):
|
|
|
|
rpc._rpc_forcesell(None)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
rpc._rpc_forcesell('all')
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtradebot.create_trade()
|
2018-06-08 02:52:50 +00:00
|
|
|
rpc._rpc_forcesell('all')
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
rpc._rpc_forcesell('1')
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
freqtradebot.state = State.STOPPED
|
2018-06-08 02:52:50 +00:00
|
|
|
with pytest.raises(RPCException, match=r'.*`trader is not running`*'):
|
|
|
|
rpc._rpc_forcesell(None)
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
with pytest.raises(RPCException, match=r'.*`trader is not running`*'):
|
|
|
|
rpc._rpc_forcesell('all')
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-04-06 07:57:08 +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
|
2018-05-25 14:29:06 +00:00
|
|
|
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={
|
2018-03-25 20:25:26 +00:00
|
|
|
'status': 'open',
|
|
|
|
'type': 'limit',
|
2018-05-25 14:29:06 +00:00
|
|
|
'side': 'buy',
|
|
|
|
'filled': filled_amount
|
2018-02-13 03:45:59 +00:00
|
|
|
}
|
|
|
|
)
|
2018-05-25 14:29:06 +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
|
2018-05-25 14:29:06 +00:00
|
|
|
assert trade.amount == filled_amount
|
|
|
|
|
|
|
|
freqtradebot.create_trade()
|
|
|
|
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',
|
2018-05-25 14:29:06 +00:00
|
|
|
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
|
2018-06-08 02:52:50 +00:00
|
|
|
rpc._rpc_forcesell('2')
|
2018-05-25 14:29:06 +00:00
|
|
|
assert cancel_order_mock.call_count == 2
|
|
|
|
assert trade.amount == amount
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtradebot.create_trade()
|
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={
|
2018-03-25 20:25:26 +00:00
|
|
|
'status': 'open',
|
|
|
|
'type': 'limit',
|
|
|
|
'side': 'sell'
|
2018-02-13 03:45:59 +00:00
|
|
|
}
|
|
|
|
)
|
2018-06-08 02:52:50 +00:00
|
|
|
rpc._rpc_forcesell('3')
|
2018-02-13 03:45:59 +00:00
|
|
|
# status quo, no exchange calls
|
2018-05-25 14:29:06 +00:00
|
|
|
assert cancel_order_mock.call_count == 2
|
2018-02-13 03:45:59 +00:00
|
|
|
|
|
|
|
|
2018-04-15 10:09:12 +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:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Test rpc_performance() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
2018-03-17 23:42:24 +00:00
|
|
|
patch_coinmarketcap(mocker)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
get_balances=MagicMock(return_value=ticker),
|
2018-03-25 20:59:58 +00:00
|
|
|
get_ticker=ticker,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
|
|
|
get_markets=markets
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
2018-02-01 06:05:23 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtradebot.create_trade()
|
2018-02-01 06:05:23 +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
|
2018-06-08 02:52:50 +00:00
|
|
|
res = rpc._rpc_performance()
|
2018-02-01 06:05:23 +00:00
|
|
|
assert len(res) == 1
|
2018-03-24 19:59:09 +00:00
|
|
|
assert res[0]['pair'] == 'ETH/BTC'
|
2018-02-01 06:05:23 +00:00
|
|
|
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:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Test rpc_count() method
|
|
|
|
"""
|
|
|
|
patch_get_signal(mocker, (True, False))
|
2018-03-17 23:42:24 +00:00
|
|
|
patch_coinmarketcap(mocker)
|
2018-06-08 21:50:38 +00:00
|
|
|
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
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
get_balances=MagicMock(return_value=ticker),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_ticker=ticker,
|
|
|
|
get_fee=fee,
|
2018-06-16 23:23:12 +00:00
|
|
|
get_markets=markets
|
2018-02-13 03:45:59 +00:00
|
|
|
)
|
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtradebot = FreqtradeBot(default_conf)
|
2018-02-13 03:45:59 +00:00
|
|
|
rpc = RPC(freqtradebot)
|
|
|
|
|
2018-06-08 02:52:50 +00:00
|
|
|
trades = rpc._rpc_count()
|
2018-02-13 03:45:59 +00:00
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 0
|
|
|
|
|
|
|
|
# Create some test data
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtradebot.create_trade()
|
2018-06-08 02:52:50 +00:00
|
|
|
trades = rpc._rpc_count()
|
2018-02-13 03:45:59 +00:00
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 1
|