2017-10-01 12:40:40 +00:00
|
|
|
# pragma pylint: disable=missing-docstring
|
2017-10-01 08:02:47 +00:00
|
|
|
import copy
|
2017-10-01 12:25:10 +00:00
|
|
|
from unittest.mock import MagicMock, call
|
2017-09-08 13:51:00 +00:00
|
|
|
|
2017-10-06 10:22:04 +00:00
|
|
|
from freqtrade.exchange import Exchanges
|
2017-09-28 21:26:28 +00:00
|
|
|
from freqtrade.main import create_trade, handle_trade, close_trade_if_fulfilled, init, \
|
|
|
|
get_target_bid
|
|
|
|
from freqtrade.persistence import Trade
|
2017-09-08 13:51:00 +00:00
|
|
|
|
|
|
|
|
2017-11-07 19:12:56 +00:00
|
|
|
def test_create_trade(default_conf, ticker, limit_buy_order, mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
2017-10-01 12:25:10 +00:00
|
|
|
buy_signal = mocker.patch('freqtrade.main.get_buy_signal', side_effect=lambda _: True)
|
|
|
|
mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock())
|
|
|
|
mocker.patch.multiple('freqtrade.main.exchange',
|
2017-10-01 21:28:09 +00:00
|
|
|
validate_pairs=MagicMock(),
|
2017-11-07 19:12:56 +00:00
|
|
|
get_ticker=ticker,
|
2017-10-31 23:26:32 +00:00
|
|
|
buy=MagicMock(return_value='mocked_limit_buy'))
|
2017-10-01 12:25:10 +00:00
|
|
|
# Save state of current whitelist
|
2017-11-07 19:12:56 +00:00
|
|
|
whitelist = copy.deepcopy(default_conf['exchange']['pair_whitelist'])
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-11-07 19:12:56 +00:00
|
|
|
init(default_conf, 'sqlite://')
|
2017-10-31 23:26:32 +00:00
|
|
|
for _ in ['BTC_ETH', 'BTC_TKN', 'BTC_TRST', 'BTC_SWT']:
|
2017-10-06 10:22:04 +00:00
|
|
|
trade = create_trade(15.0)
|
2017-10-01 12:25:10 +00:00
|
|
|
Trade.session.add(trade)
|
|
|
|
Trade.session.flush()
|
|
|
|
assert trade is not None
|
|
|
|
assert trade.stake_amount == 15.0
|
2017-10-01 12:45:21 +00:00
|
|
|
assert trade.is_open
|
2017-10-01 12:25:10 +00:00
|
|
|
assert trade.open_date is not None
|
2017-10-31 23:26:32 +00:00
|
|
|
assert trade.exchange == Exchanges.BITTREX.name
|
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
2017-11-07 19:12:56 +00:00
|
|
|
trade.update(limit_buy_order)
|
|
|
|
|
|
|
|
assert trade.open_rate == 0.07256061
|
2017-10-31 23:26:32 +00:00
|
|
|
assert trade.amount == 206.43811673387373
|
|
|
|
|
2017-11-07 19:12:56 +00:00
|
|
|
assert whitelist == default_conf['exchange']['pair_whitelist']
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
buy_signal.assert_has_calls(
|
|
|
|
[call('BTC_ETH'), call('BTC_TKN'), call('BTC_TRST'), call('BTC_SWT')]
|
|
|
|
)
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-11-07 19:12:56 +00:00
|
|
|
def test_handle_trade(default_conf, limit_sell_order, mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
2017-10-01 12:25:10 +00:00
|
|
|
mocker.patch.multiple('freqtrade.main.telegram', init=MagicMock(), send_msg=MagicMock())
|
|
|
|
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.17256061,
|
|
|
|
'ask': 0.172661,
|
|
|
|
'last': 0.17256061
|
|
|
|
}),
|
2017-10-31 23:26:32 +00:00
|
|
|
sell=MagicMock(return_value='mocked_limit_sell'))
|
2017-10-01 12:25:10 +00:00
|
|
|
trade = Trade.query.filter(Trade.is_open.is_(True)).first()
|
|
|
|
assert trade
|
2017-10-31 23:26:32 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
handle_trade(trade)
|
2017-10-31 23:26:32 +00:00
|
|
|
assert trade.open_order_id == 'mocked_limit_sell'
|
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
2017-11-07 19:12:56 +00:00
|
|
|
trade.update(limit_sell_order)
|
|
|
|
|
|
|
|
assert trade.close_rate == 0.0802134
|
|
|
|
assert trade.close_profit == 0.10046755
|
2017-10-01 12:25:10 +00:00
|
|
|
assert trade.close_date is not None
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-11-07 19:12:56 +00:00
|
|
|
def test_close_trade(default_conf, mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', default_conf)
|
2017-10-01 12:25:10 +00:00
|
|
|
trade = Trade.query.filter(Trade.is_open.is_(True)).first()
|
|
|
|
assert trade
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
# Simulate that there is no open order
|
|
|
|
trade.open_order_id = None
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
closed = close_trade_if_fulfilled(trade)
|
|
|
|
assert closed
|
2017-10-01 12:45:21 +00:00
|
|
|
assert not trade.is_open
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
def test_balance_fully_ask_side(mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 0.0}})
|
|
|
|
assert get_target_bid({'ask': 20, 'last': 10}) == 20
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-10-01 12:25:10 +00:00
|
|
|
def test_balance_fully_last_side(mocker):
|
|
|
|
mocker.patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 1.0}})
|
|
|
|
assert get_target_bid({'ask': 20, 'last': 10}) == 10
|
2017-10-01 08:02:47 +00:00
|
|
|
|
2017-10-30 23:36:35 +00:00
|
|
|
|
2017-11-07 19:12:56 +00:00
|
|
|
def test_balance_bigger_last_ask(mocker):
|
2017-10-01 12:25:10 +00:00
|
|
|
mocker.patch.dict('freqtrade.main._CONF', {'bid_strategy': {'ask_last_balance': 1.0}})
|
|
|
|
assert get_target_bid({'ask': 5, 'last': 10}) == 5
|