2018-07-30 08:37:29 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, C0103
|
2018-02-04 09:21:16 +00:00
|
|
|
# pragma pylint: disable=protected-access, too-many-lines, invalid-name, too-many-arguments
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
from copy import deepcopy
|
2019-09-26 04:48:46 +00:00
|
|
|
from math import isclose
|
2019-12-08 13:10:04 +00:00
|
|
|
from unittest.mock import ANY, MagicMock, PropertyMock
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
import arrow
|
|
|
|
import pytest
|
|
|
|
|
2020-09-28 17:43:15 +00:00
|
|
|
from freqtrade.constants import CANCEL_REASON, MATH_CLOSE_PREC, UNLIMITED_STAKE_AMOUNT
|
2021-06-09 17:51:44 +00:00
|
|
|
from freqtrade.enums import RPCMessageType, RunMode, SellType, State
|
2020-09-28 17:43:15 +00:00
|
|
|
from freqtrade.exceptions import (DependencyException, ExchangeError, InsufficientFundsError,
|
|
|
|
InvalidOrderException, OperationalException, PricingError,
|
|
|
|
TemporaryError)
|
2018-02-04 09:21:16 +00:00
|
|
|
from freqtrade.freqtradebot import FreqtradeBot
|
2020-10-24 12:46:13 +00:00
|
|
|
from freqtrade.persistence import Order, PairLocks, Trade
|
2020-10-25 09:54:30 +00:00
|
|
|
from freqtrade.persistence.models import PairLock
|
2021-06-08 19:06:47 +00:00
|
|
|
from freqtrade.strategy.interface import SellCheckTuple
|
2019-03-28 08:10:21 +00:00
|
|
|
from freqtrade.worker import Worker
|
2020-09-28 17:43:15 +00:00
|
|
|
from tests.conftest import (create_mock_trades, get_patched_freqtradebot, get_patched_worker,
|
|
|
|
log_has, log_has_re, patch_edge, patch_exchange, patch_get_signal,
|
2020-05-26 18:14:05 +00:00
|
|
|
patch_wallet, patch_whitelist)
|
2020-09-28 17:43:15 +00:00
|
|
|
from tests.conftest_trades import (MOCK_TRADE_COUNT, mock_order_1, mock_order_2, mock_order_2_sell,
|
|
|
|
mock_order_3, mock_order_3_sell, mock_order_4,
|
|
|
|
mock_order_5_stoploss, mock_order_6_sell)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def patch_RPCManager(mocker) -> MagicMock:
|
|
|
|
"""
|
|
|
|
This function mock RPC manager to avoid repeating this code in almost every tests
|
|
|
|
:param mocker: mocker to patch RPCManager class
|
|
|
|
:return: RPCManager.send_msg MagicMock to track if this method is called
|
|
|
|
"""
|
2018-06-08 21:50:38 +00:00
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
|
2018-02-04 09:21:16 +00:00
|
|
|
rpc_mock = mocker.patch('freqtrade.freqtradebot.RPCManager.send_msg', MagicMock())
|
|
|
|
return rpc_mock
|
|
|
|
|
|
|
|
|
|
|
|
# Unit tests
|
|
|
|
|
2019-04-02 18:57:52 +00:00
|
|
|
def test_freqtradebot_state(mocker, default_conf, markets) -> None:
|
2019-03-05 18:46:03 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets))
|
2018-02-04 09:21:16 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-04-06 07:57:08 +00:00
|
|
|
assert freqtrade.state is State.RUNNING
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf.pop('initial_state')
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-04-06 07:57:08 +00:00
|
|
|
assert freqtrade.state is State.STOPPED
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-05-16 09:05:34 +00:00
|
|
|
def test_process_stopped(mocker, default_conf) -> None:
|
|
|
|
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
coo_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cancel_all_open_orders')
|
|
|
|
freqtrade.process_stopped()
|
|
|
|
assert coo_mock.call_count == 0
|
|
|
|
|
|
|
|
default_conf['cancel_open_orders_on_exit'] = True
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
freqtrade.process_stopped()
|
|
|
|
assert coo_mock.call_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_bot_cleanup(mocker, default_conf, caplog) -> None:
|
2020-10-16 05:39:12 +00:00
|
|
|
mock_cleanup = mocker.patch('freqtrade.freqtradebot.cleanup_db')
|
2020-05-16 09:05:34 +00:00
|
|
|
coo_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cancel_all_open_orders')
|
2018-03-05 08:11:13 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-06-08 23:19:42 +00:00
|
|
|
freqtrade.cleanup()
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has('Cleaning up modules ...', caplog)
|
2018-03-05 08:11:13 +00:00
|
|
|
assert mock_cleanup.call_count == 1
|
2020-05-16 09:05:34 +00:00
|
|
|
assert coo_mock.call_count == 0
|
|
|
|
|
|
|
|
freqtrade.config['cancel_open_orders_on_exit'] = True
|
|
|
|
freqtrade.cleanup()
|
|
|
|
assert coo_mock.call_count == 1
|
2018-03-05 08:11:13 +00:00
|
|
|
|
|
|
|
|
2019-08-11 17:44:15 +00:00
|
|
|
def test_order_dict_dry_run(default_conf, mocker, caplog) -> None:
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
conf = default_conf.copy()
|
|
|
|
conf['runmode'] = RunMode.DRY_RUN
|
|
|
|
conf['order_types'] = {
|
|
|
|
'buy': 'market',
|
|
|
|
'sell': 'limit',
|
|
|
|
'stoploss': 'limit',
|
|
|
|
'stoploss_on_exchange': True,
|
|
|
|
}
|
2021-03-20 12:08:02 +00:00
|
|
|
conf['bid_strategy']['price_side'] = 'ask'
|
2019-08-11 17:44:15 +00:00
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(conf)
|
2019-09-01 15:48:06 +00:00
|
|
|
assert freqtrade.strategy.order_types['stoploss_on_exchange']
|
2019-08-11 17:44:15 +00:00
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
# is left untouched
|
|
|
|
conf = default_conf.copy()
|
|
|
|
conf['runmode'] = RunMode.DRY_RUN
|
|
|
|
conf['order_types'] = {
|
|
|
|
'buy': 'market',
|
|
|
|
'sell': 'limit',
|
|
|
|
'stoploss': 'limit',
|
|
|
|
'stoploss_on_exchange': False,
|
|
|
|
}
|
|
|
|
freqtrade = FreqtradeBot(conf)
|
|
|
|
assert not freqtrade.strategy.order_types['stoploss_on_exchange']
|
2019-08-12 04:34:49 +00:00
|
|
|
assert not log_has_re(".*stoploss_on_exchange .* dry-run", caplog)
|
2019-08-11 17:44:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_order_dict_live(default_conf, mocker, caplog) -> None:
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
2021-04-28 14:00:12 +00:00
|
|
|
|
2019-08-11 17:44:15 +00:00
|
|
|
conf = default_conf.copy()
|
|
|
|
conf['runmode'] = RunMode.LIVE
|
|
|
|
conf['order_types'] = {
|
|
|
|
'buy': 'market',
|
|
|
|
'sell': 'limit',
|
|
|
|
'stoploss': 'limit',
|
|
|
|
'stoploss_on_exchange': True,
|
|
|
|
}
|
2021-03-20 12:08:02 +00:00
|
|
|
conf['bid_strategy']['price_side'] = 'ask'
|
2019-08-11 17:44:15 +00:00
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(conf)
|
2019-08-12 04:34:49 +00:00
|
|
|
assert not log_has_re(".*stoploss_on_exchange .* dry-run", caplog)
|
2019-08-11 17:44:15 +00:00
|
|
|
assert freqtrade.strategy.order_types['stoploss_on_exchange']
|
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
# is left untouched
|
|
|
|
conf = default_conf.copy()
|
|
|
|
conf['runmode'] = RunMode.LIVE
|
|
|
|
conf['order_types'] = {
|
|
|
|
'buy': 'market',
|
|
|
|
'sell': 'limit',
|
|
|
|
'stoploss': 'limit',
|
|
|
|
'stoploss_on_exchange': False,
|
|
|
|
}
|
|
|
|
freqtrade = FreqtradeBot(conf)
|
|
|
|
assert not freqtrade.strategy.order_types['stoploss_on_exchange']
|
2019-08-12 04:34:49 +00:00
|
|
|
assert not log_has_re(".*stoploss_on_exchange .* dry-run", caplog)
|
2019-08-11 17:44:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_trade_stake_amount(default_conf, ticker, mocker) -> None:
|
2018-05-23 10:15:03 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-05-23 10:15:03 +00:00
|
|
|
|
2018-06-07 21:54:46 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-05-23 10:15:03 +00:00
|
|
|
|
2021-04-21 18:01:10 +00:00
|
|
|
result = freqtrade.wallets.get_trade_stake_amount('ETH/BTC')
|
2018-07-30 08:57:11 +00:00
|
|
|
assert result == default_conf['stake_amount']
|
2018-05-23 10:15:03 +00:00
|
|
|
|
|
|
|
|
2020-01-10 05:36:28 +00:00
|
|
|
@pytest.mark.parametrize("amend_last,wallet,max_open,lsamr,expected", [
|
|
|
|
(False, 0.002, 2, 0.5, [0.001, None]),
|
|
|
|
(True, 0.002, 2, 0.5, [0.001, 0.00098]),
|
|
|
|
(False, 0.003, 3, 0.5, [0.001, 0.001, None]),
|
|
|
|
(True, 0.003, 3, 0.5, [0.001, 0.001, 0.00097]),
|
|
|
|
(False, 0.0022, 3, 0.5, [0.001, 0.001, None]),
|
|
|
|
(True, 0.0022, 3, 0.5, [0.001, 0.001, 0.0]),
|
|
|
|
(True, 0.0027, 3, 0.5, [0.001, 0.001, 0.000673]),
|
|
|
|
(True, 0.0022, 3, 1, [0.001, 0.001, 0.0]),
|
2020-01-05 12:25:21 +00:00
|
|
|
])
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_check_available_stake_amount(default_conf, ticker, mocker, fee, limit_buy_order_open,
|
2020-01-10 05:36:28 +00:00
|
|
|
amend_last, wallet, max_open, lsamr, expected) -> None:
|
2020-01-05 12:25:21 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2020-01-05 12:25:21 +00:00
|
|
|
get_fee=fee
|
|
|
|
)
|
|
|
|
default_conf['dry_run_wallet'] = wallet
|
|
|
|
|
|
|
|
default_conf['amend_last_stake_amount'] = amend_last
|
2020-01-10 05:36:28 +00:00
|
|
|
default_conf['last_stake_amount_min_ratio'] = lsamr
|
|
|
|
|
2020-01-05 12:25:21 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
|
|
|
for i in range(0, max_open):
|
|
|
|
|
|
|
|
if expected[i] is not None:
|
2020-08-13 14:14:41 +00:00
|
|
|
limit_buy_order_open['id'] = str(i)
|
2021-04-21 18:01:10 +00:00
|
|
|
result = freqtrade.wallets.get_trade_stake_amount('ETH/BTC')
|
2020-01-05 12:25:21 +00:00
|
|
|
assert pytest.approx(result) == expected[i]
|
|
|
|
freqtrade.execute_buy('ETH/BTC', result)
|
|
|
|
else:
|
|
|
|
with pytest.raises(DependencyException):
|
2021-04-21 18:01:10 +00:00
|
|
|
freqtrade.wallets.get_trade_stake_amount('ETH/BTC')
|
2020-01-05 12:25:21 +00:00
|
|
|
|
|
|
|
|
2018-11-10 17:03:46 +00:00
|
|
|
def test_edge_called_in_process(mocker, edge_conf) -> None:
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_edge(mocker)
|
2018-11-10 17:09:32 +00:00
|
|
|
|
2018-11-10 17:03:46 +00:00
|
|
|
def _refresh_whitelist(list):
|
|
|
|
return ['ETH/BTC', 'LTC/BTC', 'XRP/BTC', 'NEO/BTC']
|
|
|
|
|
|
|
|
patch_exchange(mocker)
|
|
|
|
freqtrade = FreqtradeBot(edge_conf)
|
2018-12-03 19:38:15 +00:00
|
|
|
freqtrade.pairlists._validate_whitelist = _refresh_whitelist
|
2018-11-10 17:03:46 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2019-03-26 08:07:24 +00:00
|
|
|
freqtrade.process()
|
2018-11-10 17:03:46 +00:00
|
|
|
assert freqtrade.active_pair_whitelist == ['NEO/BTC', 'LTC/BTC']
|
|
|
|
|
|
|
|
|
2018-11-10 16:20:11 +00:00
|
|
|
def test_edge_overrides_stake_amount(mocker, edge_conf) -> None:
|
2018-10-04 16:05:46 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
patch_edge(mocker)
|
2019-12-15 08:37:08 +00:00
|
|
|
edge_conf['dry_run_wallet'] = 999.9
|
2018-11-10 16:20:11 +00:00
|
|
|
freqtrade = FreqtradeBot(edge_conf)
|
2018-10-04 16:05:46 +00:00
|
|
|
|
2021-02-03 19:00:33 +00:00
|
|
|
assert freqtrade.wallets.get_trade_stake_amount(
|
2021-04-21 18:01:10 +00:00
|
|
|
'NEO/BTC', freqtrade.edge) == (999.9 * 0.5 * 0.01) / 0.20
|
2021-02-03 19:00:33 +00:00
|
|
|
assert freqtrade.wallets.get_trade_stake_amount(
|
2021-04-21 18:01:10 +00:00
|
|
|
'LTC/BTC', freqtrade.edge) == (999.9 * 0.5 * 0.01) / 0.21
|
2018-10-04 16:05:46 +00:00
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_edge_overrides_stoploss(limit_buy_order, fee, caplog, mocker, edge_conf) -> None:
|
2018-11-10 16:20:11 +00:00
|
|
|
|
2018-10-04 16:05:46 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
patch_edge(mocker)
|
2019-08-13 08:12:12 +00:00
|
|
|
edge_conf['max_open_trades'] = float('inf')
|
2018-10-04 16:05:46 +00:00
|
|
|
|
|
|
|
# Strategy stoploss is -0.1 but Edge imposes a stoploss at -0.2
|
|
|
|
# Thus, if price falls 21%, stoploss should be triggered
|
|
|
|
#
|
|
|
|
# mocking the ticker: price is falling ...
|
|
|
|
buy_price = limit_buy_order['price']
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-10-04 16:05:46 +00:00
|
|
|
'bid': buy_price * 0.79,
|
|
|
|
'ask': buy_price * 0.79,
|
|
|
|
'last': buy_price * 0.79
|
|
|
|
}),
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
# Create a trade with "limit_buy_order" price
|
2018-11-10 16:20:11 +00:00
|
|
|
freqtrade = FreqtradeBot(edge_conf)
|
|
|
|
freqtrade.active_pair_whitelist = ['NEO/BTC']
|
2018-10-04 16:05:46 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-10-04 16:05:46 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
# stoploss shoud be hit
|
|
|
|
assert freqtrade.handle_trade(trade) is True
|
2020-06-27 05:08:16 +00:00
|
|
|
assert log_has('Executing Sell for NEO/BTC. Reason: stop_loss', caplog)
|
2018-10-04 16:05:46 +00:00
|
|
|
assert trade.sell_reason == SellType.STOP_LOSS.value
|
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_edge_should_ignore_strategy_stoploss(limit_buy_order, fee,
|
2018-11-10 16:20:11 +00:00
|
|
|
mocker, edge_conf) -> None:
|
2018-10-04 16:05:46 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
patch_edge(mocker)
|
2019-08-13 08:12:12 +00:00
|
|
|
edge_conf['max_open_trades'] = float('inf')
|
2018-10-04 16:05:46 +00:00
|
|
|
|
|
|
|
# Strategy stoploss is -0.1 but Edge imposes a stoploss at -0.2
|
|
|
|
# Thus, if price falls 15%, stoploss should not be triggered
|
|
|
|
#
|
|
|
|
# mocking the ticker: price is falling ...
|
|
|
|
buy_price = limit_buy_order['price']
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-10-04 16:05:46 +00:00
|
|
|
'bid': buy_price * 0.85,
|
|
|
|
'ask': buy_price * 0.85,
|
|
|
|
'last': buy_price * 0.85
|
|
|
|
}),
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
# Create a trade with "limit_buy_order" price
|
2018-11-10 16:20:11 +00:00
|
|
|
freqtrade = FreqtradeBot(edge_conf)
|
|
|
|
freqtrade.active_pair_whitelist = ['NEO/BTC']
|
2018-10-04 16:05:46 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-10-04 16:05:46 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
#############################################
|
|
|
|
|
2018-11-10 16:20:11 +00:00
|
|
|
# stoploss shoud not be hit
|
2018-10-04 16:05:46 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
|
2018-10-04 16:07:47 +00:00
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_total_open_trades_stakes(mocker, default_conf, ticker, fee) -> None:
|
2018-12-06 12:51:06 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
2019-11-26 09:07:43 +00:00
|
|
|
default_conf['stake_amount'] = 0.00098751
|
2019-08-13 08:12:12 +00:00
|
|
|
default_conf['max_open_trades'] = 2
|
2018-12-06 12:51:06 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-12-06 12:51:06 +00:00
|
|
|
get_fee=fee,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=False),
|
2018-12-06 12:51:06 +00:00
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-12-06 12:51:06 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
|
|
|
|
assert trade is not None
|
2019-11-26 09:07:43 +00:00
|
|
|
assert trade.stake_amount == 0.00098751
|
2018-12-06 12:51:06 +00:00
|
|
|
assert trade.is_open
|
|
|
|
assert trade.open_date is not None
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-12-06 12:51:06 +00:00
|
|
|
trade = Trade.query.order_by(Trade.id.desc()).first()
|
|
|
|
|
|
|
|
assert trade is not None
|
2019-11-26 09:07:43 +00:00
|
|
|
assert trade.stake_amount == 0.00098751
|
2018-12-06 12:51:06 +00:00
|
|
|
assert trade.is_open
|
|
|
|
assert trade.open_date is not None
|
|
|
|
|
2019-11-26 09:07:43 +00:00
|
|
|
assert Trade.total_open_trades_stakes() == 1.97502e-03
|
2018-12-06 12:51:06 +00:00
|
|
|
|
|
|
|
|
2019-12-29 01:38:28 +00:00
|
|
|
def test_create_trade(default_conf, ticker, limit_buy_order, fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +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,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=False),
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Save state of current whitelist
|
|
|
|
whitelist = deepcopy(default_conf['exchange']['pair_whitelist'])
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2019-12-29 01:38:28 +00:00
|
|
|
freqtrade.create_trade('ETH/BTC')
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade is not None
|
|
|
|
assert trade.stake_amount == 0.001
|
|
|
|
assert trade.is_open
|
|
|
|
assert trade.open_date is not None
|
2021-04-20 10:54:22 +00:00
|
|
|
assert trade.exchange == 'binance'
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
|
|
|
|
assert trade.open_rate == 0.00001099
|
|
|
|
assert trade.amount == 90.99181073
|
|
|
|
|
|
|
|
assert whitelist == default_conf['exchange']['pair_whitelist']
|
|
|
|
|
|
|
|
|
2019-12-29 01:38:28 +00:00
|
|
|
def test_create_trade_no_stake_amount(default_conf, ticker, limit_buy_order,
|
|
|
|
fee, mocker) -> None:
|
2018-06-16 23:23:12 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-11-23 09:17:10 +00:00
|
|
|
patch_wallet(mocker, free=default_conf['stake_amount'] * 0.5)
|
2018-06-16 23:23:12 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-23 13:50:27 +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,
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-06-16 23:23:12 +00:00
|
|
|
|
|
|
|
with pytest.raises(DependencyException, match=r'.*stake amount.*'):
|
2019-12-29 01:38:28 +00:00
|
|
|
freqtrade.create_trade('ETH/BTC')
|
2018-06-16 23:23:12 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_create_trade_minimal_amount(default_conf, ticker, limit_buy_order_open,
|
2019-12-29 01:38:28 +00:00
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2020-08-13 11:33:45 +00:00
|
|
|
buy_mock = MagicMock(return_value=limit_buy_order_open)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +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=buy_mock,
|
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['stake_amount'] = 0.0005
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-03-15 22:48:22 +00:00
|
|
|
|
2019-12-29 01:38:28 +00:00
|
|
|
freqtrade.create_trade('ETH/BTC')
|
2018-11-15 18:31:24 +00:00
|
|
|
rate, amount = buy_mock.call_args[1]['rate'], buy_mock.call_args[1]['amount']
|
2020-07-15 18:51:52 +00:00
|
|
|
assert rate * amount <= default_conf['stake_amount']
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_create_trade_too_small_stake_amount(default_conf, ticker, limit_buy_order_open,
|
2019-12-29 01:38:28 +00:00
|
|
|
fee, mocker) -> None:
|
2018-06-16 23:23:12 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2020-08-13 11:33:45 +00:00
|
|
|
buy_mock = MagicMock(return_value=limit_buy_order_open)
|
2018-06-16 23:23:12 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-23 13:50:27 +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=buy_mock,
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2019-11-23 14:20:53 +00:00
|
|
|
freqtrade.config['stake_amount'] = 0.000000005
|
|
|
|
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-06-16 23:23:12 +00:00
|
|
|
|
2019-12-29 01:38:28 +00:00
|
|
|
assert not freqtrade.create_trade('ETH/BTC')
|
2018-06-16 23:23:12 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_create_trade_limit_reached(default_conf, ticker, limit_buy_order_open,
|
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['max_open_trades'] = 0
|
2019-12-30 14:02:17 +00:00
|
|
|
default_conf['stake_amount'] = UNLIMITED_STAKE_AMOUNT
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-06-07 21:54:46 +00:00
|
|
|
|
2019-12-29 01:38:28 +00:00
|
|
|
assert not freqtrade.create_trade('ETH/BTC')
|
2021-04-21 18:01:10 +00:00
|
|
|
assert freqtrade.wallets.get_trade_stake_amount('ETH/BTC', freqtrade.edge) == 0
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_enter_positions_no_pairs_left(default_conf, ticker, limit_buy_order_open, fee,
|
2019-12-30 19:50:56 +00:00
|
|
|
mocker, caplog) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['exchange']['pair_whitelist'] = ["ETH/BTC"]
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
n = freqtrade.enter_positions()
|
|
|
|
assert n == 1
|
2019-12-29 01:38:28 +00:00
|
|
|
assert not log_has_re(r"No currency pair in active pair whitelist.*", caplog)
|
2019-12-30 19:50:56 +00:00
|
|
|
n = freqtrade.enter_positions()
|
|
|
|
assert n == 0
|
2019-12-29 01:38:28 +00:00
|
|
|
assert log_has_re(r"No currency pair in active pair whitelist.*", caplog)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
def test_enter_positions_no_pairs_in_whitelist(default_conf, ticker, limit_buy_order, fee,
|
|
|
|
mocker, caplog) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +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': limit_buy_order['id']}),
|
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2019-04-03 17:52:06 +00:00
|
|
|
default_conf['exchange']['pair_whitelist'] = []
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
n = freqtrade.enter_positions()
|
|
|
|
assert n == 0
|
2019-10-02 00:27:42 +00:00
|
|
|
assert log_has("Active pair whitelist is empty.", caplog)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-10-24 12:46:13 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_enter_positions_global_pairlock(default_conf, ticker, limit_buy_order, fee,
|
|
|
|
mocker, caplog) -> None:
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
|
|
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
n = freqtrade.enter_positions()
|
2020-11-25 10:54:11 +00:00
|
|
|
message = r"Global pairlock active until.* Not creating new trades."
|
2020-10-24 12:46:13 +00:00
|
|
|
n = freqtrade.enter_positions()
|
|
|
|
# 0 trades, but it's not because of pairlock.
|
|
|
|
assert n == 0
|
2020-11-25 10:54:11 +00:00
|
|
|
assert not log_has_re(message, caplog)
|
2020-10-24 12:46:13 +00:00
|
|
|
|
|
|
|
PairLocks.lock_pair('*', arrow.utcnow().shift(minutes=20).datetime, 'Just because')
|
|
|
|
n = freqtrade.enter_positions()
|
|
|
|
assert n == 0
|
2020-11-25 10:54:11 +00:00
|
|
|
assert log_has_re(message, caplog)
|
2020-10-24 12:46:13 +00:00
|
|
|
|
|
|
|
|
2019-12-29 01:38:28 +00:00
|
|
|
def test_create_trade_no_signal(default_conf, fee, mocker) -> None:
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['dry_run'] = True
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['stake_amount'] = 10
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, False))
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
Trade.query = MagicMock()
|
|
|
|
Trade.query.filter = MagicMock()
|
2019-12-29 01:38:28 +00:00
|
|
|
assert not freqtrade.create_trade('ETH/BTC')
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-08-13 08:34:27 +00:00
|
|
|
@pytest.mark.parametrize("max_open", range(0, 5))
|
2020-01-03 09:41:34 +00:00
|
|
|
@pytest.mark.parametrize("tradable_balance_ratio,modifier", [(1.0, 1), (0.99, 0.8), (0.5, 0.5)])
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_create_trades_multiple_trades(default_conf, ticker, fee, mocker, limit_buy_order_open,
|
2020-01-03 09:41:34 +00:00
|
|
|
max_open, tradable_balance_ratio, modifier) -> None:
|
2019-08-13 08:20:32 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
default_conf['max_open_trades'] = max_open
|
2020-01-03 09:41:34 +00:00
|
|
|
default_conf['tradable_balance_ratio'] = tradable_balance_ratio
|
|
|
|
default_conf['dry_run_wallet'] = 0.001 * max_open
|
|
|
|
|
2019-08-13 08:20:32 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2019-08-13 08:20:32 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
n = freqtrade.enter_positions()
|
2019-08-13 08:20:32 +00:00
|
|
|
trades = Trade.get_open_trades()
|
2020-01-03 09:41:34 +00:00
|
|
|
# Expected trades should be max_open * a modified value
|
|
|
|
# depending on the configured tradable_balance
|
|
|
|
assert n == max(int(max_open * modifier), 0)
|
|
|
|
assert len(trades) == max(int(max_open * modifier), 0)
|
2019-08-13 08:20:32 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_create_trades_preopen(default_conf, ticker, fee, mocker, limit_buy_order_open) -> None:
|
2019-08-14 04:21:15 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
default_conf['max_open_trades'] = 4
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2019-08-14 04:21:15 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
# Create 2 existing trades
|
|
|
|
freqtrade.execute_buy('ETH/BTC', default_conf['stake_amount'])
|
|
|
|
freqtrade.execute_buy('NEO/BTC', default_conf['stake_amount'])
|
|
|
|
|
|
|
|
assert len(Trade.get_open_trades()) == 2
|
2020-08-13 14:14:41 +00:00
|
|
|
# Change order_id for new orders
|
|
|
|
limit_buy_order_open['id'] = '123444'
|
2019-08-14 04:21:15 +00:00
|
|
|
|
|
|
|
# Create 2 new trades using create_trades
|
2019-12-29 01:38:28 +00:00
|
|
|
assert freqtrade.create_trade('ETH/BTC')
|
|
|
|
assert freqtrade.create_trade('NEO/BTC')
|
2019-08-14 04:21:15 +00:00
|
|
|
|
|
|
|
trades = Trade.get_open_trades()
|
|
|
|
assert len(trades) == 4
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_process_trade_creation(default_conf, ticker, limit_buy_order, limit_buy_order_open,
|
2019-10-31 06:26:48 +00:00
|
|
|
fee, mocker, caplog) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_buy_order),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
|
|
|
assert not trades
|
|
|
|
|
2019-08-13 07:38:21 +00:00
|
|
|
freqtrade.process()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
|
|
|
assert len(trades) == 1
|
|
|
|
trade = trades[0]
|
|
|
|
assert trade is not None
|
|
|
|
assert trade.stake_amount == default_conf['stake_amount']
|
|
|
|
assert trade.is_open
|
|
|
|
assert trade.open_date is not None
|
2021-04-20 10:54:22 +00:00
|
|
|
assert trade.exchange == 'binance'
|
2020-02-23 14:06:37 +00:00
|
|
|
assert trade.open_rate == 0.00001098
|
2020-07-15 18:51:52 +00:00
|
|
|
assert trade.amount == 91.07468123
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2021-04-10 12:52:34 +00:00
|
|
|
'Buy signal found: about create a new trade for ETH/BTC with stake_amount: 0.001 ...',
|
|
|
|
caplog
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_process_exchange_failures(default_conf, ticker, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-04-22 18:28:39 +00:00
|
|
|
buy=MagicMock(side_effect=TemporaryError)
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
|
|
|
sleep_mock = mocker.patch('time.sleep', side_effect=lambda _: None)
|
|
|
|
|
2019-03-28 18:37:50 +00:00
|
|
|
worker = Worker(args=None, config=default_conf)
|
2019-03-26 08:07:24 +00:00
|
|
|
patch_get_signal(worker.freqtrade)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2020-02-22 22:45:15 +00:00
|
|
|
worker._process_running()
|
2018-02-04 09:21:16 +00:00
|
|
|
assert sleep_mock.has_calls()
|
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_process_operational_exception(default_conf, ticker, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
msg_mock = patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-02-04 09:21:16 +00:00
|
|
|
buy=MagicMock(side_effect=OperationalException)
|
|
|
|
)
|
2019-03-28 18:37:50 +00:00
|
|
|
worker = Worker(args=None, config=default_conf)
|
2019-03-26 08:07:24 +00:00
|
|
|
patch_get_signal(worker.freqtrade)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2020-01-29 14:08:36 +00:00
|
|
|
assert worker.freqtrade.state == State.RUNNING
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2020-02-22 22:45:15 +00:00
|
|
|
worker._process_running()
|
2020-01-29 14:08:36 +00:00
|
|
|
assert worker.freqtrade.state == State.STOPPED
|
2018-06-24 22:04:27 +00:00
|
|
|
assert 'OperationalException' in msg_mock.call_args_list[-1][0][0]['status']
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_process_trade_handling(default_conf, ticker, limit_buy_order_open, fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
|
|
|
fetch_order=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
|
|
|
assert not trades
|
2019-08-13 07:36:52 +00:00
|
|
|
freqtrade.process()
|
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
trades = Trade.query.filter(Trade.is_open.is_(True)).all()
|
|
|
|
assert len(trades) == 1
|
|
|
|
|
2019-08-13 07:36:52 +00:00
|
|
|
# Nothing happened ...
|
|
|
|
freqtrade.process()
|
|
|
|
assert len(trades) == 1
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_process_trade_no_whitelist_pair(default_conf, ticker, limit_buy_order,
|
|
|
|
fee, mocker) -> None:
|
2019-03-26 08:07:24 +00:00
|
|
|
""" Test process with trade not in pair list """
|
2018-10-29 18:23:56 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-10-29 18:23:56 +00:00
|
|
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_buy_order),
|
2018-10-29 18:23:56 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
2019-12-13 05:59:10 +00:00
|
|
|
pair = 'BLK/BTC'
|
|
|
|
# Ensure the pair is not in the whitelist!
|
|
|
|
assert pair not in default_conf['exchange']['pair_whitelist']
|
|
|
|
|
2018-10-29 18:23:56 +00:00
|
|
|
# create open trade not in whitelist
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(Trade(
|
2018-10-29 18:23:56 +00:00
|
|
|
pair=pair,
|
|
|
|
stake_amount=0.001,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
is_open=True,
|
|
|
|
amount=20,
|
|
|
|
open_rate=0.01,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2018-10-29 18:23:56 +00:00
|
|
|
))
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(Trade(
|
2018-10-29 18:23:56 +00:00
|
|
|
pair='ETH/BTC',
|
|
|
|
stake_amount=0.001,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
is_open=True,
|
|
|
|
amount=12,
|
|
|
|
open_rate=0.001,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2018-10-29 18:23:56 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
assert pair not in freqtrade.active_pair_whitelist
|
2019-08-13 07:38:21 +00:00
|
|
|
freqtrade.process()
|
2018-10-29 18:23:56 +00:00
|
|
|
assert pair in freqtrade.active_pair_whitelist
|
|
|
|
# Make sure each pair is only in the list once
|
|
|
|
assert len(freqtrade.active_pair_whitelist) == len(set(freqtrade.active_pair_whitelist))
|
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_process_informative_pairs_added(default_conf, ticker, mocker) -> None:
|
2019-01-26 19:05:49 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
def _refresh_whitelist(list):
|
|
|
|
return ['ETH/BTC', 'LTC/BTC', 'XRP/BTC', 'NEO/BTC']
|
|
|
|
|
|
|
|
refresh_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2019-01-26 19:05:49 +00:00
|
|
|
buy=MagicMock(side_effect=TemporaryError),
|
|
|
|
refresh_latest_ohlcv=refresh_mock,
|
|
|
|
)
|
|
|
|
inf_pairs = MagicMock(return_value=[("BTC/ETH", '1m'), ("ETH/USDT", "1h")])
|
2020-06-13 16:06:52 +00:00
|
|
|
mocker.patch('freqtrade.strategy.interface.IStrategy.get_signal', return_value=(False, False))
|
2019-01-26 19:05:49 +00:00
|
|
|
mocker.patch('time.sleep', return_value=None)
|
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
freqtrade.pairlists._validate_whitelist = _refresh_whitelist
|
|
|
|
freqtrade.strategy.informative_pairs = inf_pairs
|
|
|
|
# patch_get_signal(freqtrade)
|
|
|
|
|
2019-03-26 08:07:24 +00:00
|
|
|
freqtrade.process()
|
2019-01-26 19:05:49 +00:00
|
|
|
assert inf_pairs.call_count == 1
|
|
|
|
assert refresh_mock.call_count == 1
|
|
|
|
assert ("BTC/ETH", "1m") in refresh_mock.call_args[0][0]
|
|
|
|
assert ("ETH/USDT", "1h") in refresh_mock.call_args[0][0]
|
2020-06-01 18:47:27 +00:00
|
|
|
assert ("ETH/BTC", default_conf["timeframe"]) in refresh_mock.call_args[0][0]
|
2019-01-26 19:05:49 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_execute_buy(mocker, default_conf, fee, limit_buy_order, limit_buy_order_open) -> None:
|
2018-10-09 05:06:11 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2020-06-14 08:27:29 +00:00
|
|
|
freqtrade.strategy.confirm_trade_entry = MagicMock(return_value=False)
|
2018-10-09 05:06:11 +00:00
|
|
|
stake_amount = 2
|
|
|
|
bid = 0.11
|
2020-01-22 13:46:28 +00:00
|
|
|
buy_rate_mock = MagicMock(return_value=bid)
|
2020-08-13 11:33:45 +00:00
|
|
|
buy_mm = MagicMock(return_value=limit_buy_order_open)
|
2018-10-09 05:06:11 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2021-06-02 09:43:47 +00:00
|
|
|
get_buy_rate=buy_rate_mock,
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-10-09 05:06:11 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
|
|
|
buy=buy_mm,
|
2021-02-02 18:47:21 +00:00
|
|
|
get_min_pair_stake_amount=MagicMock(return_value=1),
|
2018-10-09 05:06:11 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
|
2020-06-14 08:27:29 +00:00
|
|
|
assert not freqtrade.execute_buy(pair, stake_amount)
|
|
|
|
assert buy_rate_mock.call_count == 1
|
|
|
|
assert buy_mm.call_count == 0
|
|
|
|
assert freqtrade.strategy.confirm_trade_entry.call_count == 1
|
|
|
|
buy_rate_mock.reset_mock()
|
|
|
|
|
2020-08-13 14:14:41 +00:00
|
|
|
limit_buy_order_open['id'] = '22'
|
2020-06-14 08:27:29 +00:00
|
|
|
freqtrade.strategy.confirm_trade_entry = MagicMock(return_value=True)
|
2018-10-09 05:06:11 +00:00
|
|
|
assert freqtrade.execute_buy(pair, stake_amount)
|
2020-01-22 13:46:28 +00:00
|
|
|
assert buy_rate_mock.call_count == 1
|
2018-10-09 05:06:11 +00:00
|
|
|
assert buy_mm.call_count == 1
|
2018-11-15 18:31:24 +00:00
|
|
|
call_args = buy_mm.call_args_list[0][1]
|
|
|
|
assert call_args['pair'] == pair
|
|
|
|
assert call_args['rate'] == bid
|
2020-07-15 18:51:52 +00:00
|
|
|
assert call_args['amount'] == round(stake_amount / bid, 8)
|
2020-06-14 08:27:29 +00:00
|
|
|
buy_rate_mock.reset_mock()
|
2018-10-09 05:06:11 +00:00
|
|
|
|
2018-12-12 12:05:55 +00:00
|
|
|
# Should create an open trade with an open order id
|
|
|
|
# As the order is not fulfilled yet
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
assert trade.is_open is True
|
2020-08-13 14:14:41 +00:00
|
|
|
assert trade.open_order_id == '22'
|
2018-12-12 12:05:55 +00:00
|
|
|
|
2018-10-09 05:06:11 +00:00
|
|
|
# Test calling with price
|
2020-08-13 14:14:41 +00:00
|
|
|
limit_buy_order_open['id'] = '33'
|
2018-10-09 05:06:11 +00:00
|
|
|
fix_price = 0.06
|
|
|
|
assert freqtrade.execute_buy(pair, stake_amount, fix_price)
|
2020-01-22 13:46:28 +00:00
|
|
|
# Make sure get_buy_rate wasn't called again
|
2020-06-14 08:27:29 +00:00
|
|
|
assert buy_rate_mock.call_count == 0
|
2018-10-09 05:06:11 +00:00
|
|
|
|
|
|
|
assert buy_mm.call_count == 2
|
2018-11-15 18:31:24 +00:00
|
|
|
call_args = buy_mm.call_args_list[1][1]
|
|
|
|
assert call_args['pair'] == pair
|
|
|
|
assert call_args['rate'] == fix_price
|
2020-07-15 18:51:52 +00:00
|
|
|
assert call_args['amount'] == round(stake_amount / fix_price, 8)
|
2018-10-09 05:06:11 +00:00
|
|
|
|
2018-12-12 12:05:55 +00:00
|
|
|
# In case of closed order
|
|
|
|
limit_buy_order['status'] = 'closed'
|
|
|
|
limit_buy_order['price'] = 10
|
|
|
|
limit_buy_order['cost'] = 100
|
2020-08-13 14:14:41 +00:00
|
|
|
limit_buy_order['id'] = '444'
|
|
|
|
|
2018-12-12 12:05:55 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.buy', MagicMock(return_value=limit_buy_order))
|
|
|
|
assert freqtrade.execute_buy(pair, stake_amount)
|
|
|
|
trade = Trade.query.all()[2]
|
|
|
|
assert trade
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.open_rate == 10
|
|
|
|
assert trade.stake_amount == 100
|
|
|
|
|
|
|
|
# In case of rejected or expired order and partially filled
|
|
|
|
limit_buy_order['status'] = 'expired'
|
|
|
|
limit_buy_order['amount'] = 90.99181073
|
|
|
|
limit_buy_order['filled'] = 80.99181073
|
|
|
|
limit_buy_order['remaining'] = 10.00
|
|
|
|
limit_buy_order['price'] = 0.5
|
|
|
|
limit_buy_order['cost'] = 40.495905365
|
2020-08-13 14:14:41 +00:00
|
|
|
limit_buy_order['id'] = '555'
|
2018-12-12 12:05:55 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.buy', MagicMock(return_value=limit_buy_order))
|
|
|
|
assert freqtrade.execute_buy(pair, stake_amount)
|
|
|
|
trade = Trade.query.all()[3]
|
|
|
|
assert trade
|
2020-08-21 05:13:13 +00:00
|
|
|
assert trade.open_order_id == '555'
|
2018-12-12 12:05:55 +00:00
|
|
|
assert trade.open_rate == 0.5
|
|
|
|
assert trade.stake_amount == 40.495905365
|
|
|
|
|
|
|
|
# In case of the order is rejected and not filled at all
|
|
|
|
limit_buy_order['status'] = 'rejected'
|
|
|
|
limit_buy_order['amount'] = 90.99181073
|
|
|
|
limit_buy_order['filled'] = 0.0
|
|
|
|
limit_buy_order['remaining'] = 90.99181073
|
|
|
|
limit_buy_order['price'] = 0.5
|
|
|
|
limit_buy_order['cost'] = 0.0
|
2020-08-13 14:14:41 +00:00
|
|
|
limit_buy_order['id'] = '66'
|
2018-12-12 12:05:55 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.buy', MagicMock(return_value=limit_buy_order))
|
|
|
|
assert not freqtrade.execute_buy(pair, stake_amount)
|
|
|
|
|
2020-12-04 06:42:16 +00:00
|
|
|
# Fail to get price...
|
2021-06-02 09:43:47 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_buy_rate', MagicMock(return_value=0.0))
|
2020-12-04 06:42:16 +00:00
|
|
|
|
|
|
|
with pytest.raises(PricingError, match="Could not determine buy price."):
|
|
|
|
freqtrade.execute_buy(pair, stake_amount)
|
|
|
|
|
2018-10-09 05:06:11 +00:00
|
|
|
|
2020-06-14 08:49:15 +00:00
|
|
|
def test_execute_buy_confirm_error(mocker, default_conf, fee, limit_buy_order) -> None:
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value=limit_buy_order),
|
2021-06-02 09:43:47 +00:00
|
|
|
get_buy_rate=MagicMock(return_value=0.11),
|
2021-02-02 18:47:21 +00:00
|
|
|
get_min_pair_stake_amount=MagicMock(return_value=1),
|
2020-06-14 08:49:15 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
stake_amount = 2
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
|
|
|
|
freqtrade.strategy.confirm_trade_entry = MagicMock(side_effect=ValueError)
|
|
|
|
assert freqtrade.execute_buy(pair, stake_amount)
|
|
|
|
|
2020-08-13 14:14:41 +00:00
|
|
|
limit_buy_order['id'] = '222'
|
2020-06-14 08:49:15 +00:00
|
|
|
freqtrade.strategy.confirm_trade_entry = MagicMock(side_effect=Exception)
|
|
|
|
assert freqtrade.execute_buy(pair, stake_amount)
|
|
|
|
|
2020-08-13 14:14:41 +00:00
|
|
|
limit_buy_order['id'] = '2223'
|
2020-06-14 08:49:15 +00:00
|
|
|
freqtrade.strategy.confirm_trade_entry = MagicMock(return_value=True)
|
|
|
|
assert freqtrade.execute_buy(pair, stake_amount)
|
|
|
|
|
|
|
|
freqtrade.strategy.confirm_trade_entry = MagicMock(return_value=False)
|
|
|
|
assert not freqtrade.execute_buy(pair, stake_amount)
|
|
|
|
|
|
|
|
|
2018-11-23 19:28:01 +00:00
|
|
|
def test_add_stoploss_on_exchange(mocker, default_conf, limit_buy_order) -> None:
|
2018-11-22 18:25:26 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-11-30 19:15:56 +00:00
|
|
|
patch_exchange(mocker)
|
2018-11-23 19:28:01 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_trade', MagicMock(return_value=True))
|
2020-06-28 14:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=limit_buy_order)
|
2018-11-23 19:28:01 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[])
|
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount',
|
|
|
|
return_value=limit_buy_order['amount'])
|
|
|
|
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss = MagicMock(return_value={'id': 13434334})
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss)
|
2018-11-22 18:25:26 +00:00
|
|
|
|
2018-11-23 19:28:01 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-11-25 16:22:56 +00:00
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
2018-11-22 18:25:26 +00:00
|
|
|
|
2018-11-23 19:28:01 +00:00
|
|
|
trade = MagicMock()
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = None
|
|
|
|
trade.is_open = True
|
2019-10-02 00:27:42 +00:00
|
|
|
trades = [trade]
|
2018-11-22 18:25:26 +00:00
|
|
|
|
2019-12-30 19:08:36 +00:00
|
|
|
freqtrade.exit_positions(trades)
|
2018-11-23 14:17:36 +00:00
|
|
|
assert trade.stoploss_order_id == '13434334'
|
2020-01-19 13:39:51 +00:00
|
|
|
assert stoploss.call_count == 1
|
2018-11-23 19:28:01 +00:00
|
|
|
assert trade.is_open is True
|
2018-10-09 05:06:11 +00:00
|
|
|
|
2018-11-22 18:27:32 +00:00
|
|
|
|
2018-11-28 13:16:50 +00:00
|
|
|
def test_handle_stoploss_on_exchange(mocker, default_conf, fee, caplog,
|
2019-10-31 06:26:48 +00:00
|
|
|
limit_buy_order, limit_sell_order) -> None:
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss = MagicMock(return_value={'id': 13434334})
|
2018-11-24 18:12:00 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-11-24 18:12:00 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
|
|
|
sell=MagicMock(return_value={'id': limit_sell_order['id']}),
|
|
|
|
get_fee=fee,
|
2021-04-20 10:54:22 +00:00
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Binance',
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss=stoploss
|
2018-11-24 18:12:00 +00:00
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
2018-11-24 18:00:59 +00:00
|
|
|
|
|
|
|
# First case: when stoploss is not yet set but the order is open
|
|
|
|
# should get the stoploss order id immediately
|
|
|
|
# and should return false as no trade actually happened
|
|
|
|
trade = MagicMock()
|
|
|
|
trade.is_open = True
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = None
|
|
|
|
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
2020-01-19 13:39:51 +00:00
|
|
|
assert stoploss.call_count == 1
|
2018-11-24 18:00:59 +00:00
|
|
|
assert trade.stoploss_order_id == "13434334"
|
|
|
|
|
|
|
|
# Second case: when stoploss is set but it is not yet hit
|
|
|
|
# should do nothing and return false
|
|
|
|
trade.is_open = True
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = 100
|
|
|
|
|
|
|
|
hanging_stoploss_order = MagicMock(return_value={'status': 'open'})
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order', hanging_stoploss_order)
|
2018-11-24 18:00:59 +00:00
|
|
|
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
|
|
|
assert trade.stoploss_order_id == 100
|
|
|
|
|
2019-04-04 15:23:21 +00:00
|
|
|
# Third case: when stoploss was set but it was canceled for some reason
|
|
|
|
# should set a stoploss immediately and return False
|
2019-08-24 16:06:33 +00:00
|
|
|
caplog.clear()
|
2019-04-04 15:23:21 +00:00
|
|
|
trade.is_open = True
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = 100
|
|
|
|
|
|
|
|
canceled_stoploss_order = MagicMock(return_value={'status': 'canceled'})
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order', canceled_stoploss_order)
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss.reset_mock()
|
2019-04-04 15:23:21 +00:00
|
|
|
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
2020-01-19 13:39:51 +00:00
|
|
|
assert stoploss.call_count == 1
|
2019-04-04 15:23:21 +00:00
|
|
|
assert trade.stoploss_order_id == "13434334"
|
|
|
|
|
|
|
|
# Fourth case: when stoploss is set and it is hit
|
2018-11-24 18:00:59 +00:00
|
|
|
# should unset stoploss_order_id and return true
|
|
|
|
# as a trade actually happened
|
2019-08-24 16:06:33 +00:00
|
|
|
caplog.clear()
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-11-24 18:00:59 +00:00
|
|
|
trade = Trade.query.first()
|
2018-11-24 18:12:00 +00:00
|
|
|
trade.is_open = True
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = 100
|
2018-11-24 18:00:59 +00:00
|
|
|
assert trade
|
|
|
|
|
2018-11-24 18:12:00 +00:00
|
|
|
stoploss_order_hit = MagicMock(return_value={
|
2020-08-13 13:39:29 +00:00
|
|
|
'id': 100,
|
2018-11-24 18:12:00 +00:00
|
|
|
'status': 'closed',
|
|
|
|
'type': 'stop_loss_limit',
|
2018-11-28 18:35:10 +00:00
|
|
|
'price': 3,
|
2020-05-13 18:25:32 +00:00
|
|
|
'average': 2,
|
|
|
|
'amount': limit_buy_order['amount'],
|
2018-11-24 18:12:00 +00:00
|
|
|
})
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order', stoploss_order_hit)
|
2018-11-24 18:12:00 +00:00
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is True
|
2020-08-21 18:13:06 +00:00
|
|
|
assert log_has_re(r'STOP_LOSS_LIMIT is hit for Trade\(id=1, .*\)\.', caplog)
|
2019-01-09 15:23:13 +00:00
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
assert trade.is_open is False
|
|
|
|
|
2019-03-31 13:45:22 +00:00
|
|
|
mocker.patch(
|
2021-04-20 10:54:22 +00:00
|
|
|
'freqtrade.exchange.Binance.stoploss',
|
2020-06-28 14:04:04 +00:00
|
|
|
side_effect=ExchangeError()
|
2019-03-31 13:45:22 +00:00
|
|
|
)
|
2020-01-23 19:36:48 +00:00
|
|
|
trade.is_open = True
|
2019-03-31 13:45:22 +00:00
|
|
|
freqtrade.handle_stoploss_on_exchange(trade)
|
2019-08-31 14:11:04 +00:00
|
|
|
assert log_has('Unable to place a stoploss order on exchange.', caplog)
|
2019-08-24 16:06:33 +00:00
|
|
|
assert trade.stoploss_order_id is None
|
2019-03-31 13:45:22 +00:00
|
|
|
|
2020-06-28 14:27:35 +00:00
|
|
|
# Fifth case: fetch_order returns InvalidOrder
|
2019-04-05 18:20:16 +00:00
|
|
|
# It should try to add stoploss order
|
|
|
|
trade.stoploss_order_id = 100
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss.reset_mock()
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order',
|
2020-03-25 16:02:47 +00:00
|
|
|
side_effect=InvalidOrderException())
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss)
|
2019-04-05 18:20:16 +00:00
|
|
|
freqtrade.handle_stoploss_on_exchange(trade)
|
2020-01-19 13:39:51 +00:00
|
|
|
assert stoploss.call_count == 1
|
2019-04-05 18:20:16 +00:00
|
|
|
|
2020-01-23 20:07:11 +00:00
|
|
|
# Sixth case: Closed Trade
|
|
|
|
# Should not create new order
|
|
|
|
trade.stoploss_order_id = None
|
|
|
|
trade.is_open = False
|
2020-02-02 19:02:38 +00:00
|
|
|
stoploss.reset_mock()
|
2020-06-28 14:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order')
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss)
|
2020-01-23 20:07:11 +00:00
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
2020-02-02 19:02:38 +00:00
|
|
|
assert stoploss.call_count == 0
|
2020-01-23 20:07:11 +00:00
|
|
|
|
2019-01-09 15:23:13 +00:00
|
|
|
|
2019-08-24 16:06:33 +00:00
|
|
|
def test_handle_sle_cancel_cant_recreate(mocker, default_conf, fee, caplog,
|
2019-10-31 06:26:48 +00:00
|
|
|
limit_buy_order, limit_sell_order) -> None:
|
2019-08-24 16:06:33 +00:00
|
|
|
# Sixth case: stoploss order was cancelled but couldn't create new one
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2019-08-24 16:06:33 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
|
|
|
sell=MagicMock(return_value={'id': limit_sell_order['id']}),
|
|
|
|
get_fee=fee,
|
2021-04-20 10:54:22 +00:00
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Binance',
|
2020-08-13 13:54:36 +00:00
|
|
|
fetch_stoploss_order=MagicMock(return_value={'status': 'canceled', 'id': 100}),
|
2020-06-28 14:04:04 +00:00
|
|
|
stoploss=MagicMock(side_effect=ExchangeError()),
|
2019-08-24 16:06:33 +00:00
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-08-24 16:06:33 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.is_open = True
|
2020-01-23 18:38:35 +00:00
|
|
|
trade.open_order_id = None
|
2019-08-24 16:06:33 +00:00
|
|
|
trade.stoploss_order_id = 100
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
|
|
|
assert log_has_re(r'Stoploss order was cancelled, but unable to recreate one.*', caplog)
|
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
assert trade.is_open is True
|
|
|
|
|
|
|
|
|
2019-09-01 07:18:15 +00:00
|
|
|
def test_create_stoploss_order_invalid_order(mocker, default_conf, caplog, fee,
|
2020-08-13 11:33:45 +00:00
|
|
|
limit_buy_order_open, limit_sell_order):
|
2019-09-01 07:18:15 +00:00
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
sell_mock = MagicMock(return_value={'id': limit_sell_order['id']})
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2019-09-01 07:18:15 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2019-09-01 07:18:15 +00:00
|
|
|
sell=sell_mock,
|
|
|
|
get_fee=fee,
|
2021-04-20 10:54:22 +00:00
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Binance',
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value={'status': 'canceled'}),
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss=MagicMock(side_effect=InvalidOrderException()),
|
2019-09-01 07:18:15 +00:00
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-09-01 07:18:15 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
caplog.clear()
|
2020-08-12 09:21:00 +00:00
|
|
|
freqtrade.create_stoploss_order(trade, 200)
|
2019-09-01 07:18:15 +00:00
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
assert trade.sell_reason == SellType.EMERGENCY_SELL.value
|
2019-09-01 08:17:36 +00:00
|
|
|
assert log_has("Unable to place a stoploss order on exchange. ", caplog)
|
2019-09-01 07:18:15 +00:00
|
|
|
assert log_has("Selling the trade forcefully", caplog)
|
|
|
|
|
|
|
|
# Should call a market sell
|
|
|
|
assert sell_mock.call_count == 1
|
|
|
|
assert sell_mock.call_args[1]['ordertype'] == 'market'
|
|
|
|
assert sell_mock.call_args[1]['pair'] == trade.pair
|
|
|
|
assert sell_mock.call_args[1]['amount'] == trade.amount
|
|
|
|
|
|
|
|
# Rpc is sending first buy, then sell
|
|
|
|
assert rpc_mock.call_count == 2
|
|
|
|
assert rpc_mock.call_args_list[1][0][0]['sell_reason'] == SellType.EMERGENCY_SELL.value
|
|
|
|
assert rpc_mock.call_args_list[1][0][0]['order_type'] == 'market'
|
|
|
|
|
|
|
|
|
2020-09-14 15:34:13 +00:00
|
|
|
def test_create_stoploss_order_insufficient_funds(mocker, default_conf, caplog, fee,
|
|
|
|
limit_buy_order_open, limit_sell_order):
|
|
|
|
sell_mock = MagicMock(return_value={'id': limit_sell_order['id']})
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
mock_insuf = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_insufficient_funds')
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
|
|
|
sell=sell_mock,
|
|
|
|
get_fee=fee,
|
|
|
|
fetch_order=MagicMock(return_value={'status': 'canceled'}),
|
2021-04-20 10:54:22 +00:00
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Binance',
|
2020-09-14 15:34:13 +00:00
|
|
|
stoploss=MagicMock(side_effect=InsufficientFundsError()),
|
|
|
|
)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
|
|
|
|
|
|
|
freqtrade.enter_positions()
|
|
|
|
trade = Trade.query.first()
|
|
|
|
caplog.clear()
|
|
|
|
freqtrade.create_stoploss_order(trade, 200)
|
|
|
|
# stoploss_orderid was empty before
|
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
assert mock_insuf.call_count == 1
|
|
|
|
mock_insuf.reset_mock()
|
|
|
|
|
|
|
|
trade.stoploss_order_id = 'stoploss_orderid'
|
|
|
|
freqtrade.create_stoploss_order(trade, 200)
|
|
|
|
# No change to stoploss-orderid
|
|
|
|
assert trade.stoploss_order_id == 'stoploss_orderid'
|
|
|
|
assert mock_insuf.call_count == 1
|
2020-09-06 12:51:11 +00:00
|
|
|
|
|
|
|
|
2020-08-13 14:14:41 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2020-08-13 11:39:36 +00:00
|
|
|
def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee,
|
2019-10-26 08:08:23 +00:00
|
|
|
limit_buy_order, limit_sell_order) -> None:
|
2019-01-09 15:23:13 +00:00
|
|
|
# When trailing stoploss is set
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss = MagicMock(return_value={'id': 13434334})
|
2019-01-09 15:23:13 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2019-01-09 15:23:13 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
|
|
|
sell=MagicMock(return_value={'id': limit_sell_order['id']}),
|
|
|
|
get_fee=fee,
|
2021-04-20 10:54:22 +00:00
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Binance',
|
2020-01-19 19:06:04 +00:00
|
|
|
stoploss=stoploss,
|
|
|
|
stoploss_adjust=MagicMock(return_value=True),
|
2019-01-09 15:23:13 +00:00
|
|
|
)
|
2019-01-15 10:04:32 +00:00
|
|
|
|
|
|
|
# enabling TSL
|
2019-01-09 15:23:13 +00:00
|
|
|
default_conf['trailing_stop'] = True
|
2019-01-15 10:04:32 +00:00
|
|
|
|
|
|
|
# disabling ROI
|
|
|
|
default_conf['minimal_roi']['0'] = 999999999
|
|
|
|
|
2019-10-26 08:08:23 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2019-01-15 10:04:32 +00:00
|
|
|
|
2019-01-16 13:49:47 +00:00
|
|
|
# enabling stoploss on exchange
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
|
|
|
|
2019-01-15 10:04:32 +00:00
|
|
|
# setting stoploss
|
2019-01-09 15:23:13 +00:00
|
|
|
freqtrade.strategy.stoploss = -0.05
|
2019-01-15 10:04:32 +00:00
|
|
|
|
2019-01-16 10:51:54 +00:00
|
|
|
# setting stoploss_on_exchange_interval to 60 seconds
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 60
|
2019-01-15 10:04:32 +00:00
|
|
|
|
2019-01-09 15:23:13 +00:00
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-01-09 15:23:13 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.is_open = True
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = 100
|
|
|
|
|
|
|
|
stoploss_order_hanging = MagicMock(return_value={
|
|
|
|
'id': 100,
|
|
|
|
'status': 'open',
|
|
|
|
'type': 'stop_loss_limit',
|
|
|
|
'price': 3,
|
|
|
|
'average': 2,
|
2019-01-15 10:10:28 +00:00
|
|
|
'info': {
|
2019-01-16 15:15:36 +00:00
|
|
|
'stopPrice': '0.000011134'
|
2019-01-09 15:23:13 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order', stoploss_order_hanging)
|
2019-01-09 15:23:13 +00:00
|
|
|
|
|
|
|
# stoploss initially at 5%
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
2019-01-16 10:22:25 +00:00
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
2019-01-09 15:23:13 +00:00
|
|
|
|
|
|
|
# price jumped 2x
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={
|
2019-01-15 10:04:32 +00:00
|
|
|
'bid': 0.00002344,
|
|
|
|
'ask': 0.00002346,
|
|
|
|
'last': 0.00002344
|
|
|
|
}))
|
|
|
|
|
2019-01-15 14:36:41 +00:00
|
|
|
cancel_order_mock = MagicMock()
|
2020-08-13 14:18:03 +00:00
|
|
|
stoploss_order_mock = MagicMock(return_value={'id': 13434334})
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.cancel_stoploss_order', cancel_order_mock)
|
|
|
|
mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss_order_mock)
|
2019-01-09 15:23:13 +00:00
|
|
|
|
2019-01-16 10:51:54 +00:00
|
|
|
# stoploss should not be updated as the interval is 60 seconds
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
|
|
|
cancel_order_mock.assert_not_called()
|
|
|
|
stoploss_order_mock.assert_not_called()
|
|
|
|
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
2020-02-23 14:06:37 +00:00
|
|
|
assert trade.stop_loss == 0.00002346 * 0.95
|
2019-01-16 10:51:54 +00:00
|
|
|
|
|
|
|
# setting stoploss_on_exchange_interval to 0 seconds
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 0
|
|
|
|
|
2019-01-09 15:23:13 +00:00
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
2019-01-15 14:36:41 +00:00
|
|
|
|
|
|
|
cancel_order_mock.assert_called_once_with(100, 'ETH/BTC')
|
2020-07-15 18:51:52 +00:00
|
|
|
stoploss_order_mock.assert_called_once_with(amount=85.32423208,
|
2019-01-15 14:36:41 +00:00
|
|
|
pair='ETH/BTC',
|
2020-01-19 12:25:41 +00:00
|
|
|
order_types=freqtrade.strategy.order_types,
|
2020-02-23 14:06:37 +00:00
|
|
|
stop_price=0.00002346 * 0.95)
|
2018-11-24 18:00:59 +00:00
|
|
|
|
2020-01-20 19:10:06 +00:00
|
|
|
# price fell below stoploss, so dry-run sells trade.
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={
|
|
|
|
'bid': 0.00002144,
|
|
|
|
'ask': 0.00002146,
|
|
|
|
'last': 0.00002144
|
|
|
|
}))
|
|
|
|
assert freqtrade.handle_trade(trade) is True
|
|
|
|
|
2018-11-24 18:00:59 +00:00
|
|
|
|
2019-06-20 18:57:15 +00:00
|
|
|
def test_handle_stoploss_on_exchange_trailing_error(mocker, default_conf, fee, caplog,
|
2019-10-31 06:26:48 +00:00
|
|
|
limit_buy_order, limit_sell_order) -> None:
|
2019-06-20 18:57:15 +00:00
|
|
|
# When trailing stoploss is set
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss = MagicMock(return_value={'id': 13434334})
|
2019-06-20 18:57:15 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2019-06-20 18:57:15 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
|
|
|
sell=MagicMock(return_value={'id': limit_sell_order['id']}),
|
|
|
|
get_fee=fee,
|
2021-04-20 10:54:22 +00:00
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Binance',
|
2020-01-19 19:06:04 +00:00
|
|
|
stoploss=stoploss,
|
|
|
|
stoploss_adjust=MagicMock(return_value=True),
|
2019-06-20 18:57:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# enabling TSL
|
|
|
|
default_conf['trailing_stop'] = True
|
|
|
|
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
# enabling stoploss on exchange
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
|
|
|
|
|
|
|
# setting stoploss
|
|
|
|
freqtrade.strategy.stoploss = -0.05
|
|
|
|
|
|
|
|
# setting stoploss_on_exchange_interval to 60 seconds
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 60
|
|
|
|
patch_get_signal(freqtrade)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-06-20 18:57:15 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.is_open = True
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = "abcd"
|
|
|
|
trade.stop_loss = 0.2
|
|
|
|
trade.stoploss_last_update = arrow.utcnow().shift(minutes=-601).datetime.replace(tzinfo=None)
|
|
|
|
|
|
|
|
stoploss_order_hanging = {
|
|
|
|
'id': "abcd",
|
|
|
|
'status': 'open',
|
|
|
|
'type': 'stop_loss_limit',
|
|
|
|
'price': 3,
|
|
|
|
'average': 2,
|
|
|
|
'info': {
|
|
|
|
'stopPrice': '0.1'
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.cancel_stoploss_order',
|
2020-03-25 16:02:47 +00:00
|
|
|
side_effect=InvalidOrderException())
|
2021-05-16 12:15:24 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order',
|
|
|
|
return_value=stoploss_order_hanging)
|
2019-06-20 18:57:15 +00:00
|
|
|
freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging)
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has_re(r"Could not cancel stoploss order abcd for pair ETH/BTC.*", caplog)
|
2019-06-20 18:57:15 +00:00
|
|
|
|
|
|
|
# Still try to create order
|
2020-01-19 13:39:51 +00:00
|
|
|
assert stoploss.call_count == 1
|
2019-06-20 18:57:15 +00:00
|
|
|
|
|
|
|
# Fail creating stoploss order
|
|
|
|
caplog.clear()
|
2021-04-20 10:54:22 +00:00
|
|
|
cancel_mock = mocker.patch("freqtrade.exchange.Binance.cancel_stoploss_order", MagicMock())
|
|
|
|
mocker.patch("freqtrade.exchange.Binance.stoploss", side_effect=ExchangeError())
|
2019-06-20 18:57:15 +00:00
|
|
|
freqtrade.handle_trailing_stoploss_on_exchange(trade, stoploss_order_hanging)
|
|
|
|
assert cancel_mock.call_count == 1
|
2019-08-24 16:06:33 +00:00
|
|
|
assert log_has_re(r"Could not create trailing stoploss order for pair ETH/BTC\..*", caplog)
|
2019-06-20 18:57:15 +00:00
|
|
|
|
|
|
|
|
2021-01-30 19:11:18 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_handle_stoploss_on_exchange_custom_stop(mocker, default_conf, fee,
|
|
|
|
limit_buy_order, limit_sell_order) -> None:
|
|
|
|
# When trailing stoploss is set
|
|
|
|
stoploss = MagicMock(return_value={'id': 13434334})
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=MagicMock(return_value={
|
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
|
|
|
sell=MagicMock(return_value={'id': limit_sell_order['id']}),
|
|
|
|
get_fee=fee,
|
2021-04-20 10:54:22 +00:00
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Binance',
|
2021-01-30 19:11:18 +00:00
|
|
|
stoploss=stoploss,
|
|
|
|
stoploss_adjust=MagicMock(return_value=True),
|
|
|
|
)
|
|
|
|
|
|
|
|
# enabling TSL
|
|
|
|
default_conf['use_custom_stoploss'] = True
|
|
|
|
|
|
|
|
# disabling ROI
|
|
|
|
default_conf['minimal_roi']['0'] = 999999999
|
|
|
|
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
# enabling stoploss on exchange
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
|
|
|
|
|
|
|
# setting stoploss
|
|
|
|
freqtrade.strategy.custom_stoploss = lambda *args, **kwargs: -0.04
|
|
|
|
|
|
|
|
# setting stoploss_on_exchange_interval to 60 seconds
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 60
|
|
|
|
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
freqtrade.enter_positions()
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.is_open = True
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = 100
|
|
|
|
|
|
|
|
stoploss_order_hanging = MagicMock(return_value={
|
|
|
|
'id': 100,
|
|
|
|
'status': 'open',
|
|
|
|
'type': 'stop_loss_limit',
|
|
|
|
'price': 3,
|
|
|
|
'average': 2,
|
|
|
|
'info': {
|
|
|
|
'stopPrice': '0.000011134'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order', stoploss_order_hanging)
|
2021-01-30 19:11:18 +00:00
|
|
|
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
|
|
|
|
|
|
|
# price jumped 2x
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={
|
|
|
|
'bid': 0.00002344,
|
|
|
|
'ask': 0.00002346,
|
|
|
|
'last': 0.00002344
|
|
|
|
}))
|
|
|
|
|
|
|
|
cancel_order_mock = MagicMock()
|
|
|
|
stoploss_order_mock = MagicMock(return_value={'id': 13434334})
|
2021-04-20 10:54:22 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.cancel_stoploss_order', cancel_order_mock)
|
|
|
|
mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss_order_mock)
|
2021-01-30 19:11:18 +00:00
|
|
|
|
|
|
|
# stoploss should not be updated as the interval is 60 seconds
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
|
|
|
cancel_order_mock.assert_not_called()
|
|
|
|
stoploss_order_mock.assert_not_called()
|
|
|
|
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
assert trade.stop_loss == 0.00002346 * 0.96
|
|
|
|
assert trade.stop_loss_pct == -0.04
|
|
|
|
|
|
|
|
# setting stoploss_on_exchange_interval to 0 seconds
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 0
|
|
|
|
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
|
|
|
|
|
|
|
cancel_order_mock.assert_called_once_with(100, 'ETH/BTC')
|
|
|
|
stoploss_order_mock.assert_called_once_with(amount=85.32423208,
|
|
|
|
pair='ETH/BTC',
|
|
|
|
order_types=freqtrade.strategy.order_types,
|
|
|
|
stop_price=0.00002346 * 0.96)
|
|
|
|
|
|
|
|
# price fell below stoploss, so dry-run sells trade.
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={
|
|
|
|
'bid': 0.00002144,
|
|
|
|
'ask': 0.00002146,
|
|
|
|
'last': 0.00002144
|
|
|
|
}))
|
|
|
|
assert freqtrade.handle_trade(trade) is True
|
|
|
|
|
|
|
|
|
2019-01-16 17:38:20 +00:00
|
|
|
def test_tsl_on_exchange_compatible_with_edge(mocker, edge_conf, fee, caplog,
|
2019-10-31 06:26:48 +00:00
|
|
|
limit_buy_order, limit_sell_order) -> None:
|
2019-01-16 17:38:20 +00:00
|
|
|
|
|
|
|
# When trailing stoploss is set
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss = MagicMock(return_value={'id': 13434334})
|
2019-01-16 17:38:20 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
patch_edge(mocker)
|
2019-08-13 08:12:12 +00:00
|
|
|
edge_conf['max_open_trades'] = float('inf')
|
2019-12-15 08:37:08 +00:00
|
|
|
edge_conf['dry_run_wallet'] = 999.9
|
2020-01-19 19:06:04 +00:00
|
|
|
edge_conf['exchange']['name'] = 'binance'
|
2019-01-16 17:38:20 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2019-01-16 17:38:20 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
|
|
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
|
|
|
sell=MagicMock(return_value={'id': limit_sell_order['id']}),
|
|
|
|
get_fee=fee,
|
2020-01-19 19:06:04 +00:00
|
|
|
stoploss=stoploss,
|
2019-01-16 17:38:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# enabling TSL
|
|
|
|
edge_conf['trailing_stop'] = True
|
|
|
|
edge_conf['trailing_stop_positive'] = 0.01
|
|
|
|
edge_conf['trailing_stop_positive_offset'] = 0.011
|
|
|
|
|
|
|
|
# disabling ROI
|
|
|
|
edge_conf['minimal_roi']['0'] = 999999999
|
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(edge_conf)
|
|
|
|
|
|
|
|
# enabling stoploss on exchange
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
|
|
|
|
|
|
|
# setting stoploss
|
|
|
|
freqtrade.strategy.stoploss = -0.02
|
|
|
|
|
2019-10-18 17:36:04 +00:00
|
|
|
# setting stoploss_on_exchange_interval to 0 seconds
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange_interval'] = 0
|
2019-01-16 17:38:20 +00:00
|
|
|
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
freqtrade.active_pair_whitelist = freqtrade.edge.adjust(freqtrade.active_pair_whitelist)
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-01-16 17:38:20 +00:00
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.is_open = True
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = 100
|
|
|
|
|
|
|
|
stoploss_order_hanging = MagicMock(return_value={
|
|
|
|
'id': 100,
|
|
|
|
'status': 'open',
|
|
|
|
'type': 'stop_loss_limit',
|
|
|
|
'price': 3,
|
|
|
|
'average': 2,
|
|
|
|
'info': {
|
|
|
|
'stopPrice': '0.000009384'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-06-28 14:30:24 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', stoploss_order_hanging)
|
2019-01-16 17:38:20 +00:00
|
|
|
|
|
|
|
# stoploss initially at 20% as edge dictated it.
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
|
|
|
assert trade.stop_loss == 0.000009384
|
|
|
|
|
|
|
|
cancel_order_mock = MagicMock()
|
|
|
|
stoploss_order_mock = MagicMock()
|
2020-03-25 16:02:47 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order', cancel_order_mock)
|
2020-01-19 19:06:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss_order_mock)
|
2019-01-16 17:38:20 +00:00
|
|
|
|
|
|
|
# price goes down 5%
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={
|
2019-01-16 17:38:20 +00:00
|
|
|
'bid': 0.00001172 * 0.95,
|
|
|
|
'ask': 0.00001173 * 0.95,
|
|
|
|
'last': 0.00001172 * 0.95
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
|
|
|
|
|
|
|
# stoploss should remain the same
|
|
|
|
assert trade.stop_loss == 0.000009384
|
|
|
|
|
|
|
|
# stoploss on exchange should not be canceled
|
|
|
|
cancel_order_mock.assert_not_called()
|
|
|
|
|
|
|
|
# price jumped 2x
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={
|
2019-01-16 17:38:20 +00:00
|
|
|
'bid': 0.00002344,
|
|
|
|
'ask': 0.00002346,
|
|
|
|
'last': 0.00002344
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
assert freqtrade.handle_stoploss_on_exchange(trade) is False
|
|
|
|
|
|
|
|
# stoploss should be set to 1% as trailing is on
|
2020-02-23 14:06:37 +00:00
|
|
|
assert trade.stop_loss == 0.00002346 * 0.99
|
2019-01-16 17:38:20 +00:00
|
|
|
cancel_order_mock.assert_called_once_with(100, 'NEO/BTC')
|
2020-07-15 18:51:52 +00:00
|
|
|
stoploss_order_mock.assert_called_once_with(amount=2132892.49146757,
|
2019-01-16 17:38:20 +00:00
|
|
|
pair='NEO/BTC',
|
2020-01-19 12:25:41 +00:00
|
|
|
order_types=freqtrade.strategy.order_types,
|
2020-02-23 14:06:37 +00:00
|
|
|
stop_price=0.00002346 * 0.99)
|
2019-01-16 17:38:20 +00:00
|
|
|
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
def test_enter_positions(mocker, default_conf, caplog) -> None:
|
2019-10-25 12:19:02 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-02-04 09:21:16 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
2019-12-31 06:03:57 +00:00
|
|
|
mock_ct = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.create_trade',
|
|
|
|
MagicMock(return_value=False))
|
2019-12-30 19:50:56 +00:00
|
|
|
n = freqtrade.enter_positions()
|
|
|
|
assert n == 0
|
2019-08-13 07:42:22 +00:00
|
|
|
assert log_has('Found no buy signals for whitelisted currencies. Trying again...', caplog)
|
2019-12-31 06:05:21 +00:00
|
|
|
# create_trade should be called once for every pair in the whitelist.
|
|
|
|
assert mock_ct.call_count == len(default_conf['exchange']['pair_whitelist'])
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
def test_enter_positions_exception(mocker, default_conf, caplog) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
2019-12-31 06:01:58 +00:00
|
|
|
mock_ct = mocker.patch(
|
2019-12-29 01:38:28 +00:00
|
|
|
'freqtrade.freqtradebot.FreqtradeBot.create_trade',
|
2018-02-04 09:21:16 +00:00
|
|
|
MagicMock(side_effect=DependencyException)
|
|
|
|
)
|
2019-12-30 19:50:56 +00:00
|
|
|
n = freqtrade.enter_positions()
|
|
|
|
assert n == 0
|
2019-12-31 06:01:58 +00:00
|
|
|
assert mock_ct.call_count == len(default_conf['exchange']['pair_whitelist'])
|
2019-12-31 06:11:09 +00:00
|
|
|
assert log_has('Unable to create trade for ETH/BTC: ', caplog)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-12-30 19:08:36 +00:00
|
|
|
def test_exit_positions(mocker, default_conf, limit_buy_order, caplog) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_trade', MagicMock(return_value=True))
|
2020-06-28 14:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=limit_buy_order)
|
2018-06-17 19:11:10 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[])
|
2018-04-22 09:05:23 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount',
|
|
|
|
return_value=limit_buy_order['amount'])
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = MagicMock()
|
|
|
|
trade.open_order_id = '123'
|
2018-04-22 09:05:23 +00:00
|
|
|
trade.open_fee = 0.001
|
2019-10-02 00:27:42 +00:00
|
|
|
trades = [trade]
|
2019-12-30 19:08:36 +00:00
|
|
|
n = freqtrade.exit_positions(trades)
|
|
|
|
assert n == 0
|
2018-04-22 09:05:23 +00:00
|
|
|
# Test amount not modified by fee-logic
|
2018-06-03 04:20:17 +00:00
|
|
|
assert not log_has(
|
2019-08-11 18:17:39 +00:00
|
|
|
'Applying fee to amount for Trade {} from 90.99181073 to 90.81'.format(trade), caplog
|
2018-06-03 04:20:17 +00:00
|
|
|
)
|
2018-04-22 09:05:23 +00:00
|
|
|
|
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount', return_value=90.81)
|
|
|
|
# test amount modified by fee-logic
|
2019-12-30 19:08:36 +00:00
|
|
|
n = freqtrade.exit_positions(trades)
|
|
|
|
assert n == 0
|
2018-04-22 09:05:23 +00:00
|
|
|
|
2018-06-03 04:20:17 +00:00
|
|
|
|
2019-12-30 19:08:36 +00:00
|
|
|
def test_exit_positions_exception(mocker, default_conf, limit_buy_order, caplog) -> None:
|
2018-06-03 04:20:17 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2020-06-28 14:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=limit_buy_order)
|
2018-06-03 04:20:17 +00:00
|
|
|
|
|
|
|
trade = MagicMock()
|
2020-03-24 16:20:16 +00:00
|
|
|
trade.open_order_id = None
|
2018-06-03 04:20:17 +00:00
|
|
|
trade.open_fee = 0.001
|
2020-08-02 08:12:15 +00:00
|
|
|
trade.pair = 'ETH/BTC'
|
2019-10-02 00:27:42 +00:00
|
|
|
trades = [trade]
|
2018-06-03 04:20:17 +00:00
|
|
|
|
2019-03-31 13:51:45 +00:00
|
|
|
# Test raise of DependencyException exception
|
2018-06-03 04:20:17 +00:00
|
|
|
mocker.patch(
|
2020-03-24 16:20:16 +00:00
|
|
|
'freqtrade.freqtradebot.FreqtradeBot.handle_trade',
|
2019-03-31 13:51:45 +00:00
|
|
|
side_effect=DependencyException()
|
2018-06-03 04:20:17 +00:00
|
|
|
)
|
2019-12-30 19:08:36 +00:00
|
|
|
n = freqtrade.exit_positions(trades)
|
|
|
|
assert n == 0
|
2020-08-02 08:12:15 +00:00
|
|
|
assert log_has('Unable to sell trade ETH/BTC: ', caplog)
|
2018-06-03 04:20:17 +00:00
|
|
|
|
2019-03-31 13:51:45 +00:00
|
|
|
|
2019-04-02 05:12:48 +00:00
|
|
|
def test_update_trade_state(mocker, default_conf, limit_buy_order, caplog) -> None:
|
2019-03-31 14:05:00 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_trade', MagicMock(return_value=True))
|
2020-06-28 14:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=limit_buy_order)
|
2019-03-31 14:05:00 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[])
|
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount',
|
|
|
|
return_value=limit_buy_order['amount'])
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
trade = Trade(
|
|
|
|
open_order_id=123,
|
|
|
|
fee_open=0.001,
|
|
|
|
fee_close=0.001,
|
|
|
|
open_rate=0.01,
|
|
|
|
open_date=arrow.utcnow().datetime,
|
|
|
|
amount=11,
|
2021-04-19 19:32:04 +00:00
|
|
|
exchange="binance",
|
2019-12-17 05:58:10 +00:00
|
|
|
)
|
2020-09-06 13:05:47 +00:00
|
|
|
assert not freqtrade.update_trade_state(trade, None)
|
|
|
|
assert log_has_re(r'Orderid for trade .* is empty.', caplog)
|
2019-09-14 08:07:23 +00:00
|
|
|
# Add datetime explicitly since sqlalchemy defaults apply only once written to database
|
2020-08-21 05:17:52 +00:00
|
|
|
freqtrade.update_trade_state(trade, '123')
|
2019-03-31 14:05:00 +00:00
|
|
|
# Test amount not modified by fee-logic
|
2019-08-11 18:17:39 +00:00
|
|
|
assert not log_has_re(r'Applying fee to .*', caplog)
|
2019-03-31 14:05:00 +00:00
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.amount == limit_buy_order['amount']
|
|
|
|
|
|
|
|
trade.open_order_id = '123'
|
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount', return_value=90.81)
|
|
|
|
assert trade.amount != 90.81
|
|
|
|
# test amount modified by fee-logic
|
2020-08-21 05:17:52 +00:00
|
|
|
freqtrade.update_trade_state(trade, '123')
|
2019-03-31 14:05:00 +00:00
|
|
|
assert trade.amount == 90.81
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
|
|
|
|
trade.is_open = True
|
|
|
|
trade.open_order_id = None
|
|
|
|
# Assert we call handle_trade() if trade is feasible for execution
|
2020-08-21 05:17:52 +00:00
|
|
|
freqtrade.update_trade_state(trade, '123')
|
2019-03-31 14:05:00 +00:00
|
|
|
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has_re('Found open order for.*', caplog)
|
2019-03-31 14:05:00 +00:00
|
|
|
|
|
|
|
|
2019-12-17 07:53:30 +00:00
|
|
|
def test_update_trade_state_withorderdict(default_conf, trades_for_order, limit_buy_order, fee,
|
|
|
|
mocker):
|
2019-03-31 17:56:01 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
2020-06-28 14:27:35 +00:00
|
|
|
# fetch_order should not be called!!
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order', MagicMock(side_effect=ValueError))
|
2019-03-31 17:56:01 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
amount = sum(x['amount'] for x in trades_for_order)
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
2020-08-03 17:21:46 +00:00
|
|
|
open_date=arrow.utcnow().datetime,
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2019-04-22 11:31:07 +00:00
|
|
|
open_order_id="123456",
|
|
|
|
is_open=True,
|
2019-03-31 17:56:01 +00:00
|
|
|
)
|
2020-08-21 05:17:52 +00:00
|
|
|
freqtrade.update_trade_state(trade, '123456', limit_buy_order)
|
2019-03-31 17:56:01 +00:00
|
|
|
assert trade.amount != amount
|
|
|
|
assert trade.amount == limit_buy_order['amount']
|
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_update_trade_state_withorderdict_rounding_fee(default_conf, trades_for_order, fee,
|
2019-09-26 04:48:46 +00:00
|
|
|
limit_buy_order, mocker, caplog):
|
|
|
|
trades_for_order[0]['amount'] = limit_buy_order['amount'] + 1e-14
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
2020-06-28 14:27:35 +00:00
|
|
|
# fetch_order should not be called!!
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order', MagicMock(side_effect=ValueError))
|
2019-09-26 04:48:46 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
amount = sum(x['amount'] for x in trades_for_order)
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2020-08-21 05:17:52 +00:00
|
|
|
open_order_id='123456',
|
2019-09-26 04:48:46 +00:00
|
|
|
is_open=True,
|
|
|
|
open_date=arrow.utcnow().datetime,
|
|
|
|
)
|
2020-08-21 05:17:52 +00:00
|
|
|
freqtrade.update_trade_state(trade, '123456', limit_buy_order)
|
2019-09-26 04:48:46 +00:00
|
|
|
assert trade.amount != amount
|
|
|
|
assert trade.amount == limit_buy_order['amount']
|
|
|
|
assert log_has_re(r'Applying fee on amount for .*', caplog)
|
|
|
|
|
|
|
|
|
2019-04-02 05:12:48 +00:00
|
|
|
def test_update_trade_state_exception(mocker, default_conf,
|
|
|
|
limit_buy_order, caplog) -> None:
|
2019-03-31 13:51:45 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2020-06-28 14:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=limit_buy_order)
|
2019-03-31 13:51:45 +00:00
|
|
|
|
|
|
|
trade = MagicMock()
|
|
|
|
trade.open_order_id = '123'
|
|
|
|
trade.open_fee = 0.001
|
|
|
|
|
|
|
|
# Test raise of OperationalException exception
|
2018-06-03 04:20:17 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.freqtradebot.FreqtradeBot.get_real_amount',
|
2019-10-17 17:33:21 +00:00
|
|
|
side_effect=DependencyException()
|
2018-06-03 04:20:17 +00:00
|
|
|
)
|
2020-08-21 05:17:52 +00:00
|
|
|
freqtrade.update_trade_state(trade, trade.open_order_id)
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has('Could not update trade amount: ', caplog)
|
2018-06-03 04:20:17 +00:00
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2019-07-25 18:05:48 +00:00
|
|
|
def test_update_trade_state_orderexception(mocker, default_conf, caplog) -> None:
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2020-06-28 14:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order',
|
2019-07-25 18:05:48 +00:00
|
|
|
MagicMock(side_effect=InvalidOrderException))
|
|
|
|
|
|
|
|
trade = MagicMock()
|
|
|
|
trade.open_order_id = '123'
|
|
|
|
trade.open_fee = 0.001
|
|
|
|
|
|
|
|
# Test raise of OperationalException exception
|
|
|
|
grm_mock = mocker.patch("freqtrade.freqtradebot.FreqtradeBot.get_real_amount", MagicMock())
|
2020-08-21 05:17:52 +00:00
|
|
|
freqtrade.update_trade_state(trade, trade.open_order_id)
|
2019-07-25 18:05:48 +00:00
|
|
|
assert grm_mock.call_count == 0
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has(f'Unable to fetch order {trade.open_order_id}: ', caplog)
|
2019-07-25 18:05:48 +00:00
|
|
|
|
|
|
|
|
2020-08-13 13:54:36 +00:00
|
|
|
def test_update_trade_state_sell(default_conf, trades_for_order, limit_sell_order_open,
|
|
|
|
limit_sell_order, mocker):
|
2019-04-22 11:31:07 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
2020-06-28 14:27:35 +00:00
|
|
|
# fetch_order should not be called!!
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order', MagicMock(side_effect=ValueError))
|
2019-04-22 11:31:07 +00:00
|
|
|
wallet_mock = MagicMock()
|
|
|
|
mocker.patch('freqtrade.wallets.Wallets.update', wallet_mock)
|
|
|
|
|
|
|
|
patch_exchange(mocker)
|
|
|
|
amount = limit_sell_order["amount"]
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
wallet_mock.reset_mock()
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
|
|
|
fee_open=0.0025,
|
|
|
|
fee_close=0.0025,
|
2020-08-03 17:21:46 +00:00
|
|
|
open_date=arrow.utcnow().datetime,
|
2019-04-22 11:31:07 +00:00
|
|
|
open_order_id="123456",
|
|
|
|
is_open=True,
|
|
|
|
)
|
2020-08-13 14:14:41 +00:00
|
|
|
order = Order.parse_from_ccxt_object(limit_sell_order_open, 'LTC/ETH', 'sell')
|
2020-08-13 13:54:36 +00:00
|
|
|
trade.orders.append(order)
|
|
|
|
assert order.status == 'open'
|
2020-08-21 05:17:52 +00:00
|
|
|
freqtrade.update_trade_state(trade, trade.open_order_id, limit_sell_order)
|
2019-04-22 11:31:07 +00:00
|
|
|
assert trade.amount == limit_sell_order['amount']
|
|
|
|
# Wallet needs to be updated after closing a limit-sell order to reenable buying
|
|
|
|
assert wallet_mock.call_count == 1
|
2019-04-22 11:36:14 +00:00
|
|
|
assert not trade.is_open
|
2020-08-13 13:54:36 +00:00
|
|
|
# Order is updated by update_trade_state
|
|
|
|
assert order.status == 'closed'
|
2019-04-22 11:31:07 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:39:36 +00:00
|
|
|
def test_handle_trade(default_conf, limit_buy_order, limit_sell_order_open, limit_sell_order,
|
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-02-04 09:21:16 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order),
|
|
|
|
sell=MagicMock(return_value=limit_sell_order_open),
|
2018-06-16 23:23:12 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
2018-04-15 10:50:47 +00:00
|
|
|
time.sleep(0.01) # Race condition fix
|
2018-02-04 09:21:16 +00:00
|
|
|
trade.update(limit_buy_order)
|
|
|
|
assert trade.is_open is True
|
2019-12-18 19:16:53 +00:00
|
|
|
freqtrade.wallets.update()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is True
|
2018-04-15 10:48:02 +00:00
|
|
|
assert trade.open_order_id == limit_sell_order['id']
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
# Simulate fulfilled LIMIT_SELL order for trade
|
|
|
|
trade.update(limit_sell_order)
|
|
|
|
|
|
|
|
assert trade.close_rate == 0.00001173
|
2018-10-21 07:21:32 +00:00
|
|
|
assert trade.close_profit == 0.06201058
|
2018-02-04 09:21:16 +00:00
|
|
|
assert trade.calc_profit() == 0.00006217
|
|
|
|
assert trade.close_date is not None
|
|
|
|
|
|
|
|
|
2020-08-13 11:39:36 +00:00
|
|
|
def test_handle_overlapping_signals(default_conf, ticker, limit_buy_order_open,
|
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(True, True))
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
# Buy and Sell triggering, so doing nothing ...
|
|
|
|
trades = Trade.query.all()
|
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 0
|
|
|
|
|
|
|
|
# Buy is triggering, so buying ...
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(True, False))
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
trades = Trade.query.all()
|
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 1
|
|
|
|
assert trades[0].is_open is True
|
|
|
|
|
|
|
|
# Buy and Sell are not triggering, so doing nothing ...
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, False))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trades[0]) is False
|
2018-02-04 09:21:16 +00:00
|
|
|
trades = Trade.query.all()
|
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 1
|
|
|
|
assert trades[0].is_open is True
|
|
|
|
|
|
|
|
# Buy and Sell are triggering, so doing nothing ...
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(True, True))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trades[0]) is False
|
2018-02-04 09:21:16 +00:00
|
|
|
trades = Trade.query.all()
|
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 1
|
|
|
|
assert trades[0].is_open is True
|
|
|
|
|
|
|
|
# Sell is triggering, guess what : we are Selling!
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2018-02-04 09:21:16 +00:00
|
|
|
trades = Trade.query.all()
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trades[0]) is True
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_handle_trade_roi(default_conf, ticker, limit_buy_order_open,
|
2019-10-26 08:08:23 +00:00
|
|
|
fee, mocker, caplog) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
|
|
|
|
2019-10-26 08:08:23 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(True, False))
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=True)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.is_open = True
|
|
|
|
|
|
|
|
# FIX: sniffing logs, suggest handle_trade should not execute_sell
|
|
|
|
# instead that responsibility should be moved out of handle_trade(),
|
|
|
|
# we might just want to check if we are in a sell condition without
|
|
|
|
# executing
|
|
|
|
# if ROI is reached we must sell
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trade)
|
2021-04-22 06:21:19 +00:00
|
|
|
assert log_has("ETH/BTC - Required profit reached. sell_type=SellType.ROI",
|
2019-09-10 09:37:15 +00:00
|
|
|
caplog)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-10-05 10:29:59 +00:00
|
|
|
def test_handle_trade_use_sell_signal(
|
2020-08-13 11:33:45 +00:00
|
|
|
default_conf, ticker, limit_buy_order_open, fee, mocker, caplog) -> None:
|
2019-10-05 10:29:59 +00:00
|
|
|
# use_sell_signal is True buy default
|
2018-02-04 09:21:16 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
|
|
|
|
2019-10-26 08:08:23 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.is_open = True
|
|
|
|
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, False))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert not freqtrade.handle_trade(trade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trade)
|
2021-04-22 06:21:19 +00:00
|
|
|
assert log_has("ETH/BTC - Sell signal received. sell_type=SellType.SELL_SIGNAL",
|
2019-09-10 09:37:15 +00:00
|
|
|
caplog)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_close_trade(default_conf, ticker, limit_buy_order, limit_buy_order_open, limit_sell_order,
|
2019-10-31 06:26:48 +00:00
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
# Create trade and sell it
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
trade.update(limit_sell_order)
|
|
|
|
assert trade.is_open is False
|
|
|
|
|
2019-10-02 16:08:49 +00:00
|
|
|
with pytest.raises(DependencyException, match=r'.*closed trade.*'):
|
2018-03-15 22:48:22 +00:00
|
|
|
freqtrade.handle_trade(trade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-06-14 05:16:56 +00:00
|
|
|
def test_bot_loop_start_called_once(mocker, default_conf, caplog):
|
|
|
|
ftbot = get_patched_freqtradebot(mocker, default_conf)
|
2021-01-28 18:40:10 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.create_trade')
|
2020-06-14 05:16:56 +00:00
|
|
|
patch_get_signal(ftbot)
|
|
|
|
ftbot.strategy.bot_loop_start = MagicMock(side_effect=ValueError)
|
|
|
|
ftbot.strategy.analyze = MagicMock()
|
|
|
|
|
|
|
|
ftbot.process()
|
|
|
|
assert log_has_re(r'Strategy caused the following exception.*', caplog)
|
|
|
|
assert ftbot.strategy.bot_loop_start.call_count == 1
|
|
|
|
assert ftbot.strategy.analyze.call_count == 1
|
|
|
|
|
|
|
|
|
2020-02-23 12:04:40 +00:00
|
|
|
def test_check_handle_timedout_buy_usercustom(default_conf, ticker, limit_buy_order_old, open_trade,
|
|
|
|
fee, mocker) -> None:
|
|
|
|
default_conf["unfilledtimeout"] = {"buy": 1400, "sell": 30}
|
|
|
|
|
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
|
|
|
cancel_order_mock = MagicMock(return_value=limit_buy_order_old)
|
2020-08-01 13:59:50 +00:00
|
|
|
cancel_buy_order = deepcopy(limit_buy_order_old)
|
|
|
|
cancel_buy_order['status'] = 'canceled'
|
|
|
|
cancel_order_wr_mock = MagicMock(return_value=cancel_buy_order)
|
|
|
|
|
2020-02-23 12:04:40 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_buy_order_old),
|
2020-08-01 13:59:50 +00:00
|
|
|
cancel_order_with_result=cancel_order_wr_mock,
|
2020-02-23 12:04:40 +00:00
|
|
|
cancel_order=cancel_order_mock,
|
|
|
|
get_fee=fee
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2020-02-23 12:04:40 +00:00
|
|
|
|
2020-05-17 09:23:55 +00:00
|
|
|
# Ensure default is to return empty (so not mocked yet)
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
|
2020-02-23 12:04:40 +00:00
|
|
|
# Return false - trade remains open
|
|
|
|
freqtrade.strategy.check_buy_timeout = MagicMock(return_value=False)
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
|
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 1
|
|
|
|
assert freqtrade.strategy.check_buy_timeout.call_count == 1
|
|
|
|
|
|
|
|
# Raise Keyerror ... (no impact on trade)
|
|
|
|
freqtrade.strategy.check_buy_timeout = MagicMock(side_effect=KeyError)
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
|
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 1
|
|
|
|
assert freqtrade.strategy.check_buy_timeout.call_count == 1
|
|
|
|
|
|
|
|
freqtrade.strategy.check_buy_timeout = MagicMock(return_value=True)
|
|
|
|
# Trade should be closed since the function returns true
|
|
|
|
freqtrade.check_handle_timedout()
|
2020-08-01 13:59:50 +00:00
|
|
|
assert cancel_order_wr_mock.call_count == 1
|
2020-02-23 12:04:40 +00:00
|
|
|
assert rpc_mock.call_count == 1
|
|
|
|
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
|
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 0
|
|
|
|
assert freqtrade.strategy.check_buy_timeout.call_count == 1
|
|
|
|
|
|
|
|
|
2019-10-17 17:53:01 +00:00
|
|
|
def test_check_handle_timedout_buy(default_conf, ticker, limit_buy_order_old, open_trade,
|
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
2020-08-01 13:59:50 +00:00
|
|
|
limit_buy_cancel = deepcopy(limit_buy_order_old)
|
|
|
|
limit_buy_cancel['status'] = 'canceled'
|
|
|
|
cancel_order_mock = MagicMock(return_value=limit_buy_cancel)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_buy_order_old),
|
2020-04-17 15:53:56 +00:00
|
|
|
cancel_order_with_result=cancel_order_mock,
|
2018-04-15 10:09:12 +00:00
|
|
|
get_fee=fee
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2020-02-23 12:11:33 +00:00
|
|
|
freqtrade.strategy.check_buy_timeout = MagicMock(return_value=False)
|
2018-02-04 09:21:16 +00:00
|
|
|
# check it does cancel buy orders over the time limit
|
2018-06-14 01:32:52 +00:00
|
|
|
freqtrade.check_handle_timedout()
|
2018-02-04 09:21:16 +00:00
|
|
|
assert cancel_order_mock.call_count == 1
|
|
|
|
assert rpc_mock.call_count == 1
|
2019-10-17 17:53:01 +00:00
|
|
|
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
|
2018-02-04 09:21:16 +00:00
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 0
|
2020-02-23 12:11:33 +00:00
|
|
|
# Custom user buy-timeout is never called
|
|
|
|
assert freqtrade.strategy.check_buy_timeout.call_count == 0
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-10-17 17:53:01 +00:00
|
|
|
def test_check_handle_cancelled_buy(default_conf, ticker, limit_buy_order_old, open_trade,
|
2019-02-03 12:39:19 +00:00
|
|
|
fee, mocker, caplog) -> None:
|
|
|
|
""" Handle Buy order cancelled on exchange"""
|
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
|
|
|
cancel_order_mock = MagicMock()
|
|
|
|
patch_exchange(mocker)
|
2020-03-24 16:20:16 +00:00
|
|
|
limit_buy_order_old.update({"status": "canceled", 'filled': 0.0})
|
2019-02-03 12:39:19 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_buy_order_old),
|
2019-02-03 12:39:19 +00:00
|
|
|
cancel_order=cancel_order_mock,
|
|
|
|
get_fee=fee
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2019-02-03 12:39:19 +00:00
|
|
|
|
|
|
|
# check it does cancel buy orders over the time limit
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
assert rpc_mock.call_count == 1
|
2019-10-17 17:53:01 +00:00
|
|
|
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
|
2019-02-03 12:39:19 +00:00
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 0
|
2020-02-08 20:02:52 +00:00
|
|
|
assert log_has_re("Buy order cancelled on exchange for Trade.*", caplog)
|
2019-02-03 12:39:19 +00:00
|
|
|
|
|
|
|
|
2019-10-17 17:53:01 +00:00
|
|
|
def test_check_handle_timedout_buy_exception(default_conf, ticker, limit_buy_order_old, open_trade,
|
2018-09-25 18:45:01 +00:00
|
|
|
fee, mocker) -> None:
|
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
|
|
|
cancel_order_mock = MagicMock()
|
2018-09-29 11:35:48 +00:00
|
|
|
patch_exchange(mocker)
|
2018-09-25 18:45:01 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
validate_pairs=MagicMock(),
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-06-28 17:45:42 +00:00
|
|
|
fetch_order=MagicMock(side_effect=ExchangeError),
|
2018-09-25 18:45:01 +00:00
|
|
|
cancel_order=cancel_order_mock,
|
|
|
|
get_fee=fee
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2018-09-25 18:45:01 +00:00
|
|
|
|
|
|
|
# check it does cancel buy orders over the time limit
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
assert rpc_mock.call_count == 0
|
2019-10-17 17:53:01 +00:00
|
|
|
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
|
2018-09-25 18:45:01 +00:00
|
|
|
nb_trades = len(trades)
|
|
|
|
assert nb_trades == 1
|
|
|
|
|
|
|
|
|
2020-02-23 12:09:46 +00:00
|
|
|
def test_check_handle_timedout_sell_usercustom(default_conf, ticker, limit_sell_order_old, mocker,
|
|
|
|
open_trade) -> None:
|
|
|
|
default_conf["unfilledtimeout"] = {"buy": 1440, "sell": 1440}
|
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
|
|
|
cancel_order_mock = MagicMock()
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_sell_order_old),
|
2020-02-23 12:09:46 +00:00
|
|
|
cancel_order=cancel_order_mock
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
|
|
|
open_trade.open_date = arrow.utcnow().shift(hours=-5).datetime
|
|
|
|
open_trade.close_date = arrow.utcnow().shift(minutes=-601).datetime
|
2021-02-17 18:50:10 +00:00
|
|
|
open_trade.close_profit_abs = 0.001
|
2020-02-23 12:09:46 +00:00
|
|
|
open_trade.is_open = False
|
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2020-05-17 09:23:55 +00:00
|
|
|
# Ensure default is false
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 0
|
2020-02-23 12:09:46 +00:00
|
|
|
|
|
|
|
freqtrade.strategy.check_sell_timeout = MagicMock(return_value=False)
|
|
|
|
# Return false - No impact
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
assert rpc_mock.call_count == 0
|
|
|
|
assert open_trade.is_open is False
|
|
|
|
assert freqtrade.strategy.check_sell_timeout.call_count == 1
|
|
|
|
|
|
|
|
freqtrade.strategy.check_sell_timeout = MagicMock(side_effect=KeyError)
|
|
|
|
# Return Error - No impact
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
assert rpc_mock.call_count == 0
|
|
|
|
assert open_trade.is_open is False
|
|
|
|
assert freqtrade.strategy.check_sell_timeout.call_count == 1
|
|
|
|
|
|
|
|
# Return True - sells!
|
|
|
|
freqtrade.strategy.check_sell_timeout = MagicMock(return_value=True)
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 1
|
|
|
|
assert rpc_mock.call_count == 1
|
|
|
|
assert open_trade.is_open is True
|
|
|
|
assert freqtrade.strategy.check_sell_timeout.call_count == 1
|
|
|
|
|
|
|
|
|
2019-10-17 17:53:01 +00:00
|
|
|
def test_check_handle_timedout_sell(default_conf, ticker, limit_sell_order_old, mocker,
|
|
|
|
open_trade) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
|
|
|
cancel_order_mock = MagicMock()
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_sell_order_old),
|
2018-02-04 09:21:16 +00:00
|
|
|
cancel_order=cancel_order_mock
|
|
|
|
)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2019-10-17 17:53:01 +00:00
|
|
|
open_trade.open_date = arrow.utcnow().shift(hours=-5).datetime
|
|
|
|
open_trade.close_date = arrow.utcnow().shift(minutes=-601).datetime
|
2021-02-17 18:50:10 +00:00
|
|
|
open_trade.close_profit_abs = 0.001
|
2019-10-17 17:53:01 +00:00
|
|
|
open_trade.is_open = False
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2020-02-23 12:11:33 +00:00
|
|
|
freqtrade.strategy.check_sell_timeout = MagicMock(return_value=False)
|
2018-02-04 09:21:16 +00:00
|
|
|
# check it does cancel sell orders over the time limit
|
2018-06-14 01:32:52 +00:00
|
|
|
freqtrade.check_handle_timedout()
|
2018-02-04 09:21:16 +00:00
|
|
|
assert cancel_order_mock.call_count == 1
|
2021-04-20 05:57:34 +00:00
|
|
|
assert rpc_mock.call_count == 1
|
2019-10-17 17:53:01 +00:00
|
|
|
assert open_trade.is_open is True
|
2020-02-23 12:11:33 +00:00
|
|
|
# Custom user sell-timeout is never called
|
|
|
|
assert freqtrade.strategy.check_sell_timeout.call_count == 0
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-10-17 17:53:01 +00:00
|
|
|
def test_check_handle_cancelled_sell(default_conf, ticker, limit_sell_order_old, open_trade,
|
2019-02-03 12:50:15 +00:00
|
|
|
mocker, caplog) -> None:
|
2019-02-03 12:39:19 +00:00
|
|
|
""" Handle sell order cancelled on exchange"""
|
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
|
|
|
cancel_order_mock = MagicMock()
|
2020-03-24 16:20:16 +00:00
|
|
|
limit_sell_order_old.update({"status": "canceled", 'filled': 0.0})
|
2019-02-03 12:39:19 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_sell_order_old),
|
2020-04-17 15:53:56 +00:00
|
|
|
cancel_order_with_result=cancel_order_mock
|
2019-02-03 12:39:19 +00:00
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
2019-10-17 17:53:01 +00:00
|
|
|
open_trade.open_date = arrow.utcnow().shift(hours=-5).datetime
|
|
|
|
open_trade.close_date = arrow.utcnow().shift(minutes=-601).datetime
|
|
|
|
open_trade.is_open = False
|
2019-02-03 12:39:19 +00:00
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2019-02-03 12:39:19 +00:00
|
|
|
|
|
|
|
# check it does cancel sell orders over the time limit
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
assert rpc_mock.call_count == 1
|
2019-10-17 17:53:01 +00:00
|
|
|
assert open_trade.is_open is True
|
2020-02-08 20:02:52 +00:00
|
|
|
assert log_has_re("Sell order cancelled on exchange for Trade.*", caplog)
|
2019-02-03 12:39:19 +00:00
|
|
|
|
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
def test_check_handle_timedout_partial(default_conf, ticker, limit_buy_order_old_partial,
|
2019-10-17 17:53:01 +00:00
|
|
|
open_trade, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
2020-08-01 13:59:50 +00:00
|
|
|
limit_buy_canceled = deepcopy(limit_buy_order_old_partial)
|
|
|
|
limit_buy_canceled['status'] = 'canceled'
|
|
|
|
|
|
|
|
cancel_order_mock = MagicMock(return_value=limit_buy_canceled)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_buy_order_old_partial),
|
2020-04-17 15:53:56 +00:00
|
|
|
cancel_order_with_result=cancel_order_mock
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
# check it does cancel buy orders over the time limit
|
|
|
|
# note this is for a partially-complete buy order
|
2018-06-14 01:32:52 +00:00
|
|
|
freqtrade.check_handle_timedout()
|
2018-02-04 09:21:16 +00:00
|
|
|
assert cancel_order_mock.call_count == 1
|
2021-04-19 19:32:04 +00:00
|
|
|
assert rpc_mock.call_count == 2
|
2019-10-17 17:53:01 +00:00
|
|
|
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
|
2018-02-04 09:21:16 +00:00
|
|
|
assert len(trades) == 1
|
|
|
|
assert trades[0].amount == 23.0
|
2019-10-17 17:53:01 +00:00
|
|
|
assert trades[0].stake_amount == open_trade.open_rate * trades[0].amount
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-10-18 07:04:05 +00:00
|
|
|
def test_check_handle_timedout_partial_fee(default_conf, ticker, open_trade, caplog, fee,
|
2019-10-18 04:38:07 +00:00
|
|
|
limit_buy_order_old_partial, trades_for_order,
|
|
|
|
limit_buy_order_old_partial_canceled, mocker) -> None:
|
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
|
|
|
cancel_order_mock = MagicMock(return_value=limit_buy_order_old_partial_canceled)
|
2020-05-03 13:25:54 +00:00
|
|
|
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=0))
|
2019-10-18 04:38:07 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_buy_order_old_partial),
|
2020-04-17 15:53:56 +00:00
|
|
|
cancel_order_with_result=cancel_order_mock,
|
2019-10-18 04:38:07 +00:00
|
|
|
get_trades_for_order=MagicMock(return_value=trades_for_order),
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2019-10-18 04:38:07 +00:00
|
|
|
assert open_trade.amount == limit_buy_order_old_partial['amount']
|
|
|
|
|
2019-10-18 07:04:05 +00:00
|
|
|
open_trade.fee_open = fee()
|
|
|
|
open_trade.fee_close = fee()
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2019-10-18 04:38:07 +00:00
|
|
|
# cancelling a half-filled order should update the amount to the bought amount
|
|
|
|
# and apply fees if necessary.
|
|
|
|
freqtrade.check_handle_timedout()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2020-05-03 08:50:59 +00:00
|
|
|
assert log_has_re(r"Applying fee on amount for Trade.*", caplog)
|
2019-10-18 04:38:07 +00:00
|
|
|
|
|
|
|
assert cancel_order_mock.call_count == 1
|
2021-04-19 19:32:04 +00:00
|
|
|
assert rpc_mock.call_count == 2
|
2019-10-18 04:38:07 +00:00
|
|
|
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
|
|
|
|
assert len(trades) == 1
|
2020-03-24 15:16:10 +00:00
|
|
|
# Verify that trade has been updated
|
2019-10-18 07:04:05 +00:00
|
|
|
assert trades[0].amount == (limit_buy_order_old_partial['amount'] -
|
2020-05-01 18:17:22 +00:00
|
|
|
limit_buy_order_old_partial['remaining']) - 0.023
|
2019-10-18 04:38:07 +00:00
|
|
|
assert trades[0].open_order_id is None
|
2020-05-01 18:17:22 +00:00
|
|
|
assert trades[0].fee_updated('buy')
|
|
|
|
assert pytest.approx(trades[0].fee_open) == 0.001
|
2019-10-18 04:38:07 +00:00
|
|
|
|
|
|
|
|
2019-10-18 07:04:05 +00:00
|
|
|
def test_check_handle_timedout_partial_except(default_conf, ticker, open_trade, caplog, fee,
|
|
|
|
limit_buy_order_old_partial, trades_for_order,
|
|
|
|
limit_buy_order_old_partial_canceled, mocker) -> None:
|
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
|
|
|
cancel_order_mock = MagicMock(return_value=limit_buy_order_old_partial_canceled)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(return_value=limit_buy_order_old_partial),
|
2020-04-17 15:53:56 +00:00
|
|
|
cancel_order_with_result=cancel_order_mock,
|
2019-10-18 07:04:05 +00:00
|
|
|
get_trades_for_order=MagicMock(return_value=trades_for_order),
|
|
|
|
)
|
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.get_real_amount',
|
|
|
|
MagicMock(side_effect=DependencyException))
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
|
|
|
assert open_trade.amount == limit_buy_order_old_partial['amount']
|
|
|
|
|
|
|
|
open_trade.fee_open = fee()
|
|
|
|
open_trade.fee_close = fee()
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2019-10-18 07:04:05 +00:00
|
|
|
# cancelling a half-filled order should update the amount to the bought amount
|
|
|
|
# and apply fees if necessary.
|
|
|
|
freqtrade.check_handle_timedout()
|
|
|
|
|
|
|
|
assert log_has_re(r"Could not update trade amount: .*", caplog)
|
|
|
|
|
|
|
|
assert cancel_order_mock.call_count == 1
|
2021-04-19 19:32:04 +00:00
|
|
|
assert rpc_mock.call_count == 2
|
2019-10-18 07:04:05 +00:00
|
|
|
trades = Trade.query.filter(Trade.open_order_id.is_(open_trade.open_order_id)).all()
|
|
|
|
assert len(trades) == 1
|
2020-03-24 15:16:10 +00:00
|
|
|
# Verify that trade has been updated
|
2019-10-18 07:04:05 +00:00
|
|
|
|
|
|
|
assert trades[0].amount == (limit_buy_order_old_partial['amount'] -
|
|
|
|
limit_buy_order_old_partial['remaining'])
|
|
|
|
assert trades[0].open_order_id is None
|
|
|
|
assert trades[0].fee_open == fee()
|
|
|
|
|
|
|
|
|
2019-10-17 17:53:01 +00:00
|
|
|
def test_check_handle_timedout_exception(default_conf, ticker, open_trade, mocker, caplog) -> None:
|
2018-03-05 08:11:13 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-03-05 08:11:13 +00:00
|
|
|
cancel_order_mock = MagicMock()
|
|
|
|
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.freqtradebot.FreqtradeBot',
|
2020-04-24 22:16:52 +00:00
|
|
|
handle_cancel_buy=MagicMock(),
|
|
|
|
handle_cancel_sell=MagicMock(),
|
2018-03-05 08:11:13 +00:00
|
|
|
)
|
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-06-28 14:27:35 +00:00
|
|
|
fetch_order=MagicMock(side_effect=ExchangeError('Oh snap')),
|
2018-03-05 08:11:13 +00:00
|
|
|
cancel_order=cancel_order_mock
|
|
|
|
)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-03-05 08:11:13 +00:00
|
|
|
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(open_trade)
|
2018-03-05 08:11:13 +00:00
|
|
|
|
2018-06-14 01:32:52 +00:00
|
|
|
freqtrade.check_handle_timedout()
|
2019-09-11 07:56:02 +00:00
|
|
|
assert log_has_re(r"Cannot query order for Trade\(id=1, pair=ETH/BTC, amount=90.99181073, "
|
|
|
|
r"open_rate=0.00001099, open_since="
|
2019-10-17 17:53:01 +00:00
|
|
|
f"{open_trade.open_date.strftime('%Y-%m-%d %H:%M:%S')}"
|
2019-09-12 19:53:54 +00:00
|
|
|
r"\) due to Traceback \(most recent call last\):\n*",
|
2019-09-10 09:37:15 +00:00
|
|
|
caplog)
|
2018-03-05 08:11:13 +00:00
|
|
|
|
|
|
|
|
2020-05-16 11:09:38 +00:00
|
|
|
def test_handle_cancel_buy(mocker, caplog, default_conf, limit_buy_order) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2020-08-01 13:59:50 +00:00
|
|
|
cancel_buy_order = deepcopy(limit_buy_order)
|
|
|
|
cancel_buy_order['status'] = 'canceled'
|
|
|
|
del cancel_buy_order['filled']
|
|
|
|
|
|
|
|
cancel_order_mock = MagicMock(return_value=cancel_buy_order)
|
2020-04-17 15:53:56 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.cancel_order_with_result', cancel_order_mock)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2020-04-24 22:16:52 +00:00
|
|
|
freqtrade._notify_buy_cancel = MagicMock()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = MagicMock()
|
2021-05-21 17:32:26 +00:00
|
|
|
trade.pair = 'LTC/USDT'
|
|
|
|
trade.open_rate = 200
|
2020-05-07 04:51:02 +00:00
|
|
|
limit_buy_order['filled'] = 0.0
|
2020-05-05 05:12:49 +00:00
|
|
|
limit_buy_order['status'] = 'open'
|
2020-04-24 22:16:52 +00:00
|
|
|
reason = CANCEL_REASON['TIMEOUT']
|
|
|
|
assert freqtrade.handle_cancel_buy(trade, limit_buy_order, reason)
|
2018-02-04 09:21:16 +00:00
|
|
|
assert cancel_order_mock.call_count == 1
|
2019-10-17 17:53:01 +00:00
|
|
|
|
2021-05-21 17:32:26 +00:00
|
|
|
cancel_order_mock.reset_mock()
|
|
|
|
caplog.clear()
|
|
|
|
limit_buy_order['filled'] = 0.01
|
|
|
|
assert not freqtrade.handle_cancel_buy(trade, limit_buy_order, reason)
|
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
assert log_has_re("Order .* for .* not cancelled, as the filled amount.* unsellable.*", caplog)
|
|
|
|
|
|
|
|
caplog.clear()
|
2019-10-17 17:53:01 +00:00
|
|
|
cancel_order_mock.reset_mock()
|
2020-05-05 05:07:42 +00:00
|
|
|
limit_buy_order['filled'] = 2
|
2020-04-24 22:16:52 +00:00
|
|
|
assert not freqtrade.handle_cancel_buy(trade, limit_buy_order, reason)
|
2018-02-04 09:21:16 +00:00
|
|
|
assert cancel_order_mock.call_count == 1
|
|
|
|
|
2020-08-01 13:59:50 +00:00
|
|
|
# Order remained open for some reason (cancel failed)
|
|
|
|
cancel_buy_order['status'] = 'open'
|
|
|
|
cancel_order_mock = MagicMock(return_value=cancel_buy_order)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.cancel_order_with_result', cancel_order_mock)
|
2020-04-24 22:16:52 +00:00
|
|
|
assert not freqtrade.handle_cancel_buy(trade, limit_buy_order, reason)
|
2020-08-01 13:59:50 +00:00
|
|
|
assert log_has_re(r"Order .* for .* not cancelled.", caplog)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-05-05 05:07:42 +00:00
|
|
|
@pytest.mark.parametrize("limit_buy_order_canceled_empty", ['binance', 'ftx', 'kraken', 'bittrex'],
|
|
|
|
indirect=['limit_buy_order_canceled_empty'])
|
2020-05-16 11:09:38 +00:00
|
|
|
def test_handle_cancel_buy_exchanges(mocker, caplog, default_conf,
|
|
|
|
limit_buy_order_canceled_empty) -> None:
|
2020-05-05 05:07:42 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
cancel_order_mock = mocker.patch(
|
|
|
|
'freqtrade.exchange.Exchange.cancel_order_with_result',
|
|
|
|
return_value=limit_buy_order_canceled_empty)
|
2020-05-16 11:09:38 +00:00
|
|
|
nofiy_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot._notify_buy_cancel')
|
2020-05-05 05:07:42 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
2020-05-16 11:09:38 +00:00
|
|
|
reason = CANCEL_REASON['TIMEOUT']
|
2020-05-05 05:07:42 +00:00
|
|
|
trade = MagicMock()
|
|
|
|
trade.pair = 'LTC/ETH'
|
2020-05-16 11:09:38 +00:00
|
|
|
assert freqtrade.handle_cancel_buy(trade, limit_buy_order_canceled_empty, reason)
|
2020-05-05 05:07:42 +00:00
|
|
|
assert cancel_order_mock.call_count == 0
|
|
|
|
assert log_has_re(r'Buy order fully cancelled. Removing .* from database\.', caplog)
|
2020-05-16 11:09:38 +00:00
|
|
|
assert nofiy_mock.call_count == 1
|
2020-05-05 05:07:42 +00:00
|
|
|
|
|
|
|
|
2020-03-19 18:42:51 +00:00
|
|
|
@pytest.mark.parametrize('cancelorder', [
|
|
|
|
{},
|
2020-04-09 17:32:10 +00:00
|
|
|
{'remaining': None},
|
2020-03-19 18:42:51 +00:00
|
|
|
'String Return value',
|
|
|
|
123
|
|
|
|
])
|
2020-05-16 11:09:38 +00:00
|
|
|
def test_handle_cancel_buy_corder_empty(mocker, default_conf, limit_buy_order,
|
|
|
|
cancelorder) -> None:
|
2019-11-20 19:37:46 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
2020-03-19 18:42:51 +00:00
|
|
|
cancel_order_mock = MagicMock(return_value=cancelorder)
|
2019-11-20 19:37:46 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
cancel_order=cancel_order_mock
|
|
|
|
)
|
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2020-04-24 22:16:52 +00:00
|
|
|
freqtrade._notify_buy_cancel = MagicMock()
|
2019-11-20 19:37:46 +00:00
|
|
|
|
|
|
|
trade = MagicMock()
|
2021-05-21 17:32:26 +00:00
|
|
|
trade.pair = 'LTC/USDT'
|
|
|
|
trade.open_rate = 200
|
2020-05-05 04:41:01 +00:00
|
|
|
limit_buy_order['filled'] = 0.0
|
2020-05-05 05:12:49 +00:00
|
|
|
limit_buy_order['status'] = 'open'
|
2020-04-24 22:16:52 +00:00
|
|
|
reason = CANCEL_REASON['TIMEOUT']
|
|
|
|
assert freqtrade.handle_cancel_buy(trade, limit_buy_order, reason)
|
2019-11-20 19:37:46 +00:00
|
|
|
assert cancel_order_mock.call_count == 1
|
|
|
|
|
|
|
|
cancel_order_mock.reset_mock()
|
2020-05-05 04:41:01 +00:00
|
|
|
limit_buy_order['filled'] = 1.0
|
2020-04-24 22:16:52 +00:00
|
|
|
assert not freqtrade.handle_cancel_buy(trade, limit_buy_order, reason)
|
2019-11-20 19:37:46 +00:00
|
|
|
assert cancel_order_mock.call_count == 1
|
|
|
|
|
|
|
|
|
2020-05-17 09:12:30 +00:00
|
|
|
def test_handle_cancel_sell_limit(mocker, default_conf, fee) -> None:
|
|
|
|
send_msg_mock = patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
cancel_order_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2020-05-17 09:12:30 +00:00
|
|
|
cancel_order=cancel_order_mock,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2021-06-02 09:39:18 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_sell_rate', return_value=0.245441)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2020-05-17 09:12:30 +00:00
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=2,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
|
|
|
open_order_id="123456",
|
|
|
|
open_date=arrow.utcnow().datetime,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
)
|
2018-02-04 09:21:16 +00:00
|
|
|
order = {'remaining': 1,
|
2019-02-03 12:39:19 +00:00
|
|
|
'amount': 1,
|
|
|
|
'status': "open"}
|
2020-04-24 22:16:52 +00:00
|
|
|
reason = CANCEL_REASON['TIMEOUT']
|
|
|
|
assert freqtrade.handle_cancel_sell(trade, order, reason)
|
2018-02-04 09:21:16 +00:00
|
|
|
assert cancel_order_mock.call_count == 1
|
2020-05-17 09:12:30 +00:00
|
|
|
assert send_msg_mock.call_count == 1
|
|
|
|
|
|
|
|
send_msg_mock.reset_mock()
|
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
order['amount'] = 2
|
2020-08-26 20:24:45 +00:00
|
|
|
assert freqtrade.handle_cancel_sell(trade, order, reason
|
|
|
|
) == CANCEL_REASON['PARTIALLY_FILLED_KEEP_OPEN']
|
2018-02-04 09:21:16 +00:00
|
|
|
# Assert cancel_order was not called (callcount remains unchanged)
|
|
|
|
assert cancel_order_mock.call_count == 1
|
2020-05-17 09:12:30 +00:00
|
|
|
assert send_msg_mock.call_count == 1
|
2020-08-26 20:24:45 +00:00
|
|
|
assert freqtrade.handle_cancel_sell(trade, order, reason
|
|
|
|
) == CANCEL_REASON['PARTIALLY_FILLED_KEEP_OPEN']
|
2020-05-17 09:12:30 +00:00
|
|
|
# Message should not be iterated again
|
2020-08-26 20:17:43 +00:00
|
|
|
assert trade.sell_order_status == CANCEL_REASON['PARTIALLY_FILLED_KEEP_OPEN']
|
2020-05-17 09:12:30 +00:00
|
|
|
assert send_msg_mock.call_count == 1
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-05-16 11:09:38 +00:00
|
|
|
def test_handle_cancel_sell_cancel_exception(mocker, default_conf) -> None:
|
2020-05-10 14:24:00 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch(
|
2021-02-06 08:23:10 +00:00
|
|
|
'freqtrade.exchange.Exchange.cancel_order_with_result', side_effect=InvalidOrderException())
|
2020-05-10 14:24:00 +00:00
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
|
|
|
trade = MagicMock()
|
2020-05-16 11:09:38 +00:00
|
|
|
reason = CANCEL_REASON['TIMEOUT']
|
2020-05-10 14:24:00 +00:00
|
|
|
order = {'remaining': 1,
|
|
|
|
'amount': 1,
|
|
|
|
'status': "open"}
|
2020-05-16 11:09:38 +00:00
|
|
|
assert freqtrade.handle_cancel_sell(trade, order, reason) == 'error cancelling order'
|
2020-05-10 14:24:00 +00:00
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +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,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=False),
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2019-10-26 08:08:23 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2020-06-14 08:23:49 +00:00
|
|
|
freqtrade.strategy.confirm_trade_exit = MagicMock(return_value=False)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2020-06-14 08:23:49 +00:00
|
|
|
rpc_mock.reset_mock()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
2020-06-14 08:23:49 +00:00
|
|
|
assert freqtrade.strategy.confirm_trade_exit.call_count == 0
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
# Increase the price and sell it
|
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker_sell_up
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2020-06-14 08:23:49 +00:00
|
|
|
# Prevented sell ...
|
2021-04-21 07:11:54 +00:00
|
|
|
freqtrade.execute_sell(trade=trade, limit=ticker_sell_up()['bid'],
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_reason=SellCheckTuple(sell_type=SellType.ROI))
|
2020-06-14 08:23:49 +00:00
|
|
|
assert rpc_mock.call_count == 0
|
|
|
|
assert freqtrade.strategy.confirm_trade_exit.call_count == 1
|
|
|
|
|
|
|
|
# Repatch with true
|
|
|
|
freqtrade.strategy.confirm_trade_exit = MagicMock(return_value=True)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2021-04-21 07:11:54 +00:00
|
|
|
freqtrade.execute_sell(trade=trade, limit=ticker_sell_up()['bid'],
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_reason=SellCheckTuple(sell_type=SellType.ROI))
|
2020-06-14 08:23:49 +00:00
|
|
|
assert freqtrade.strategy.confirm_trade_exit.call_count == 1
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2020-06-14 08:23:49 +00:00
|
|
|
assert rpc_mock.call_count == 1
|
2018-07-03 18:26:48 +00:00
|
|
|
last_msg = rpc_mock.call_args_list[-1][0][0]
|
|
|
|
assert {
|
2020-07-20 17:50:29 +00:00
|
|
|
'trade_id': 1,
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2018-07-03 18:26:48 +00:00
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'profit',
|
|
|
|
'limit': 1.172e-05,
|
2020-07-15 18:51:52 +00:00
|
|
|
'amount': 91.07468123,
|
2019-06-17 04:55:54 +00:00
|
|
|
'order_type': 'limit',
|
2020-02-23 14:06:37 +00:00
|
|
|
'open_rate': 1.098e-05,
|
|
|
|
'current_rate': 1.173e-05,
|
|
|
|
'profit_amount': 6.223e-05,
|
2020-03-05 11:27:12 +00:00
|
|
|
'profit_ratio': 0.0620716,
|
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.ROI.value,
|
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': ANY,
|
2021-04-19 19:32:04 +00:00
|
|
|
'close_rate': ANY,
|
2018-07-03 18:26:48 +00:00
|
|
|
} == last_msg
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_execute_sell_down(default_conf, ticker, fee, ticker_sell_down, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +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,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=False),
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2019-10-26 08:08:23 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2018-06-07 03:27:55 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
# Decrease the price and sell it
|
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker_sell_down
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2018-12-01 09:50:41 +00:00
|
|
|
|
2018-07-11 17:57:20 +00:00
|
|
|
freqtrade.execute_sell(trade=trade, limit=ticker_sell_down()['bid'],
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_reason=SellCheckTuple(sell_type=SellType.STOP_LOSS))
|
2018-02-04 09:21:16 +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 {
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2020-07-20 17:50:29 +00:00
|
|
|
'trade_id': 1,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2018-07-03 18:26:48 +00:00
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'loss',
|
|
|
|
'limit': 1.044e-05,
|
2020-07-15 18:51:52 +00:00
|
|
|
'amount': 91.07468123,
|
2019-06-17 04:55:54 +00:00
|
|
|
'order_type': 'limit',
|
2020-02-23 14:06:37 +00:00
|
|
|
'open_rate': 1.098e-05,
|
|
|
|
'current_rate': 1.043e-05,
|
|
|
|
'profit_amount': -5.406e-05,
|
2020-03-05 11:27:12 +00:00
|
|
|
'profit_ratio': -0.05392257,
|
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.STOP_LOSS.value,
|
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': ANY,
|
2021-04-19 19:32:04 +00:00
|
|
|
'close_rate': ANY,
|
2018-07-03 18:26:48 +00:00
|
|
|
} == last_msg
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2019-09-10 21:18:07 +00:00
|
|
|
def test_execute_sell_down_stoploss_on_exchange_dry_run(default_conf, ticker, fee,
|
2019-10-31 06:26:48 +00:00
|
|
|
ticker_sell_down, mocker) -> None:
|
2018-11-25 19:16:53 +00:00
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2018-11-25 19:16:53 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-11-25 19:16:53 +00:00
|
|
|
get_fee=fee,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=False),
|
2018-11-25 19:16:53 +00:00
|
|
|
)
|
2019-10-26 08:08:23 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2018-11-25 19:16:53 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-11-25 19:16:53 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
# Decrease the price and sell it
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker_sell_down
|
2018-11-25 19:16:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
default_conf['dry_run'] = True
|
2018-12-01 09:43:26 +00:00
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
2018-11-25 19:16:53 +00:00
|
|
|
# Setting trade stoploss to 0.01
|
|
|
|
|
2018-12-01 09:43:26 +00:00
|
|
|
trade.stop_loss = 0.00001099 * 0.99
|
2018-11-25 19:16:53 +00:00
|
|
|
freqtrade.execute_sell(trade=trade, limit=ticker_sell_down()['bid'],
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_reason=SellCheckTuple(sell_type=SellType.STOP_LOSS))
|
2018-11-25 19:16:53 +00:00
|
|
|
|
|
|
|
assert rpc_mock.call_count == 2
|
|
|
|
last_msg = rpc_mock.call_args_list[-1][0][0]
|
2018-12-01 09:43:26 +00:00
|
|
|
|
2018-11-25 19:16:53 +00:00
|
|
|
assert {
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2020-07-20 17:50:29 +00:00
|
|
|
'trade_id': 1,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2018-11-25 19:16:53 +00:00
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'loss',
|
|
|
|
'limit': 1.08801e-05,
|
2020-07-15 18:51:52 +00:00
|
|
|
'amount': 91.07468123,
|
2019-06-17 04:55:54 +00:00
|
|
|
'order_type': 'limit',
|
2020-02-23 14:06:37 +00:00
|
|
|
'open_rate': 1.098e-05,
|
|
|
|
'current_rate': 1.043e-05,
|
|
|
|
'profit_amount': -1.408e-05,
|
2020-03-05 11:27:12 +00:00
|
|
|
'profit_ratio': -0.01404051,
|
2018-11-25 19:16:53 +00:00
|
|
|
'stake_currency': 'BTC',
|
|
|
|
'fiat_currency': 'USD',
|
2019-12-08 13:10:04 +00:00
|
|
|
'sell_reason': SellType.STOP_LOSS.value,
|
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': ANY,
|
2021-04-19 19:32:04 +00:00
|
|
|
'close_rate': ANY,
|
2018-11-25 19:16:53 +00:00
|
|
|
} == last_msg
|
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_execute_sell_sloe_cancel_exception(mocker, default_conf, ticker, fee, caplog) -> None:
|
2019-06-20 18:36:39 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2020-03-25 16:02:47 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.cancel_stoploss_order',
|
|
|
|
side_effect=InvalidOrderException())
|
2019-12-13 06:06:54 +00:00
|
|
|
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=300))
|
2021-03-22 18:39:06 +00:00
|
|
|
sellmock = MagicMock(return_value={'id': '12345555'})
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2019-06-20 18:36:39 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2019-06-20 18:36:39 +00:00
|
|
|
get_fee=fee,
|
|
|
|
sell=sellmock
|
|
|
|
)
|
|
|
|
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
|
|
|
patch_get_signal(freqtrade)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-06-20 18:36:39 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
2020-10-17 09:28:34 +00:00
|
|
|
PairLock.session = MagicMock()
|
2019-06-20 18:36:39 +00:00
|
|
|
|
|
|
|
freqtrade.config['dry_run'] = False
|
|
|
|
trade.stoploss_order_id = "abcd"
|
|
|
|
|
|
|
|
freqtrade.execute_sell(trade=trade, limit=1234,
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_reason=SellCheckTuple(sell_type=SellType.STOP_LOSS))
|
2019-06-20 18:36:39 +00:00
|
|
|
assert sellmock.call_count == 1
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has('Could not cancel stoploss order abcd', caplog)
|
2019-06-20 18:36:39 +00:00
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_execute_sell_with_stoploss_on_exchange(default_conf, ticker, fee, ticker_sell_up,
|
|
|
|
mocker) -> None:
|
2018-11-22 20:01:39 +00:00
|
|
|
|
|
|
|
default_conf['exchange']['name'] = 'binance'
|
2018-11-22 20:12:49 +00:00
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss = MagicMock(return_value={
|
2018-11-22 20:01:39 +00:00
|
|
|
'id': 123,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
cancel_order = MagicMock(return_value=True)
|
2019-10-03 04:23:58 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2019-10-03 04:23:58 +00:00
|
|
|
get_fee=fee,
|
2020-01-12 13:55:05 +00:00
|
|
|
amount_to_precision=lambda s, x, y: y,
|
|
|
|
price_to_precision=lambda s, x, y: y,
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss=stoploss,
|
2020-03-25 16:02:47 +00:00
|
|
|
cancel_stoploss_order=cancel_order,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(side_effect=[True, False]),
|
2019-10-03 04:23:58 +00:00
|
|
|
)
|
2018-11-22 20:01:39 +00:00
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-11-25 16:22:56 +00:00
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
2018-11-22 20:01:39 +00:00
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-11-22 20:01:39 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
2019-10-02 00:27:42 +00:00
|
|
|
trades = [trade]
|
2018-11-22 20:01:39 +00:00
|
|
|
|
2020-03-24 18:54:13 +00:00
|
|
|
freqtrade.check_handle_timedout()
|
2019-12-30 19:08:36 +00:00
|
|
|
freqtrade.exit_positions(trades)
|
2018-11-23 19:28:01 +00:00
|
|
|
|
2018-11-22 20:01:39 +00:00
|
|
|
# Increase the price and sell it
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker_sell_up
|
2018-11-22 20:01:39 +00:00
|
|
|
)
|
|
|
|
|
2018-11-23 14:17:36 +00:00
|
|
|
freqtrade.execute_sell(trade=trade, limit=ticker_sell_up()['bid'],
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_reason=SellCheckTuple(sell_type=SellType.STOP_LOSS))
|
2018-11-22 20:01:39 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
assert cancel_order.call_count == 1
|
2021-04-19 19:32:04 +00:00
|
|
|
assert rpc_mock.call_count == 3
|
2018-11-22 20:01:39 +00:00
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_may_execute_sell_after_stoploss_on_exchange_hit(default_conf, ticker, fee,
|
2020-08-13 11:33:45 +00:00
|
|
|
mocker) -> None:
|
2018-11-23 14:17:36 +00:00
|
|
|
default_conf['exchange']['name'] = 'binance'
|
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2018-11-23 14:17:36 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-11-23 14:17:36 +00:00
|
|
|
get_fee=fee,
|
2020-01-12 13:55:05 +00:00
|
|
|
amount_to_precision=lambda s, x, y: y,
|
|
|
|
price_to_precision=lambda s, x, y: y,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(side_effect=[False, True]),
|
2018-11-23 14:17:36 +00:00
|
|
|
)
|
|
|
|
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss = MagicMock(return_value={
|
2018-11-23 14:17:36 +00:00
|
|
|
'id': 123,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-01-19 13:39:51 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss)
|
2018-11-23 14:17:36 +00:00
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-11-25 16:22:56 +00:00
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
2018-11-23 14:17:36 +00:00
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2020-03-24 16:20:16 +00:00
|
|
|
freqtrade.check_handle_timedout()
|
2018-11-23 14:17:36 +00:00
|
|
|
trade = Trade.query.first()
|
2019-10-02 00:27:42 +00:00
|
|
|
trades = [trade]
|
2020-03-24 16:20:16 +00:00
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
|
2019-12-30 19:08:36 +00:00
|
|
|
freqtrade.exit_positions(trades)
|
2018-11-23 14:17:36 +00:00
|
|
|
assert trade
|
|
|
|
assert trade.stoploss_order_id == '123'
|
2018-11-23 19:28:01 +00:00
|
|
|
assert trade.open_order_id is None
|
2018-11-23 14:17:36 +00:00
|
|
|
|
|
|
|
# Assuming stoploss on exchnage is hit
|
|
|
|
# stoploss_order_id should become None
|
|
|
|
# and trade should be sold at the price of stoploss
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss_executed = MagicMock(return_value={
|
2018-11-23 14:17:36 +00:00
|
|
|
"id": "123",
|
|
|
|
"timestamp": 1542707426845,
|
|
|
|
"datetime": "2018-11-20T09:50:26.845Z",
|
|
|
|
"lastTradeTimestamp": None,
|
|
|
|
"symbol": "BTC/USDT",
|
|
|
|
"type": "stop_loss_limit",
|
|
|
|
"side": "sell",
|
|
|
|
"price": 1.08801,
|
|
|
|
"amount": 90.99181074,
|
|
|
|
"cost": 99.0000000032274,
|
|
|
|
"average": 1.08801,
|
|
|
|
"filled": 90.99181074,
|
|
|
|
"remaining": 0.0,
|
|
|
|
"status": "closed",
|
|
|
|
"fee": None,
|
|
|
|
"trades": None
|
|
|
|
})
|
2020-06-28 14:30:24 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_stoploss_order', stoploss_executed)
|
2018-11-23 14:17:36 +00:00
|
|
|
|
2019-12-30 19:08:36 +00:00
|
|
|
freqtrade.exit_positions(trades)
|
2018-11-23 14:17:36 +00:00
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
assert trade.is_open is False
|
2018-11-26 17:28:13 +00:00
|
|
|
assert trade.sell_reason == SellType.STOPLOSS_ON_EXCHANGE.value
|
2021-04-19 19:32:04 +00:00
|
|
|
assert rpc_mock.call_count == 3
|
2021-04-20 04:41:58 +00:00
|
|
|
assert rpc_mock.call_args_list[0][0][0]['type'] == RPCMessageType.BUY
|
|
|
|
assert rpc_mock.call_args_list[1][0][0]['type'] == RPCMessageType.BUY_FILL
|
|
|
|
assert rpc_mock.call_args_list[2][0][0]['type'] == RPCMessageType.SELL
|
2018-11-23 14:17:36 +00:00
|
|
|
|
|
|
|
|
2019-09-10 21:18:07 +00:00
|
|
|
def test_execute_sell_market_order(default_conf, ticker, fee,
|
2019-10-31 06:26:48 +00:00
|
|
|
ticker_sell_up, mocker) -> None:
|
2019-08-12 14:47:00 +00:00
|
|
|
rpc_mock = patch_RPCManager(mocker)
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2019-08-12 14:47:00 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2019-08-12 14:47:00 +00:00
|
|
|
get_fee=fee,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=False),
|
2019-08-12 14:47:00 +00:00
|
|
|
)
|
2019-10-26 08:08:23 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2019-08-12 14:47:00 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-08-12 14:47:00 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
# Increase the price and sell it
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker_sell_up
|
2019-08-12 14:47:00 +00:00
|
|
|
)
|
|
|
|
freqtrade.config['order_types']['sell'] = 'market'
|
|
|
|
|
2021-04-21 07:11:54 +00:00
|
|
|
freqtrade.execute_sell(trade=trade, limit=ticker_sell_up()['bid'],
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_reason=SellCheckTuple(sell_type=SellType.ROI))
|
2019-08-12 14:47:00 +00:00
|
|
|
|
|
|
|
assert not trade.is_open
|
2020-02-23 14:06:37 +00:00
|
|
|
assert trade.close_profit == 0.0620716
|
2019-08-12 14:47:00 +00:00
|
|
|
|
2021-04-19 19:32:04 +00:00
|
|
|
assert rpc_mock.call_count == 3
|
2019-08-12 14:47:00 +00:00
|
|
|
last_msg = rpc_mock.call_args_list[-1][0][0]
|
|
|
|
assert {
|
2021-04-20 04:41:58 +00:00
|
|
|
'type': RPCMessageType.SELL,
|
2020-07-20 17:50:29 +00:00
|
|
|
'trade_id': 1,
|
2021-04-20 10:54:22 +00:00
|
|
|
'exchange': 'Binance',
|
2019-08-12 14:47:00 +00:00
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'gain': 'profit',
|
|
|
|
'limit': 1.172e-05,
|
2020-07-15 18:51:52 +00:00
|
|
|
'amount': 91.07468123,
|
2019-08-12 14:47:00 +00:00
|
|
|
'order_type': 'market',
|
2020-02-23 14:06:37 +00:00
|
|
|
'open_rate': 1.098e-05,
|
|
|
|
'current_rate': 1.173e-05,
|
|
|
|
'profit_amount': 6.223e-05,
|
2020-03-05 11:27:12 +00:00
|
|
|
'profit_ratio': 0.0620716,
|
2019-08-12 14:47:00 +00:00
|
|
|
'stake_currency': 'BTC',
|
|
|
|
'fiat_currency': 'USD',
|
2019-12-08 13:10:04 +00:00
|
|
|
'sell_reason': SellType.ROI.value,
|
|
|
|
'open_date': ANY,
|
|
|
|
'close_date': ANY,
|
2021-04-19 19:32:04 +00:00
|
|
|
'close_rate': ANY,
|
2019-08-12 14:47:00 +00:00
|
|
|
|
|
|
|
} == last_msg
|
|
|
|
|
|
|
|
|
2020-09-06 12:59:30 +00:00
|
|
|
def test_execute_sell_insufficient_funds_error(default_conf, ticker, fee,
|
|
|
|
ticker_sell_up, mocker) -> None:
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
mock_insuf = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_insufficient_funds')
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker,
|
|
|
|
get_fee=fee,
|
|
|
|
sell=MagicMock(side_effect=InsufficientFundsError())
|
|
|
|
)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
# Create some test data
|
|
|
|
freqtrade.enter_positions()
|
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
# Increase the price and sell it
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker_sell_up
|
|
|
|
)
|
|
|
|
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_reason = SellCheckTuple(sell_type=SellType.ROI)
|
2020-09-06 12:59:30 +00:00
|
|
|
assert not freqtrade.execute_sell(trade=trade, limit=ticker_sell_up()['bid'],
|
2021-04-21 07:11:54 +00:00
|
|
|
sell_reason=sell_reason)
|
2020-09-06 12:59:30 +00:00
|
|
|
assert mock_insuf.call_count == 1
|
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_sell_profit_only_enable_profit(default_conf, limit_buy_order, limit_buy_order_open,
|
2019-10-31 06:26:48 +00:00
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2021-02-02 07:06:19 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
2018-02-04 09:21:16 +00:00
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2019-10-05 10:29:59 +00:00
|
|
|
default_conf['ask_strategy'] = {
|
2018-02-04 09:21:16 +00:00
|
|
|
'use_sell_signal': True,
|
|
|
|
'sell_profit_only': True,
|
2021-01-11 18:42:44 +00:00
|
|
|
'sell_profit_offset': 0.1,
|
2018-02-04 09:21:16 +00:00
|
|
|
}
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
2019-12-18 19:16:53 +00:00
|
|
|
freqtrade.wallets.update()
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2021-01-11 18:42:44 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
|
|
|
|
freqtrade.config['ask_strategy']['sell_profit_offset'] = 0.0
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is True
|
2021-01-11 18:42:44 +00:00
|
|
|
|
2018-07-11 18:20:44 +00:00
|
|
|
assert trade.sell_reason == SellType.SELL_SIGNAL.value
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_sell_profit_only_disable_profit(default_conf, limit_buy_order, limit_buy_order_open,
|
2019-10-31 06:26:48 +00:00
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-02-04 09:21:16 +00:00
|
|
|
'bid': 0.00002172,
|
|
|
|
'ask': 0.00002173,
|
|
|
|
'last': 0.00002172
|
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2019-10-05 10:29:59 +00:00
|
|
|
default_conf['ask_strategy'] = {
|
2018-02-04 09:21:16 +00:00
|
|
|
'use_sell_signal': True,
|
|
|
|
'sell_profit_only': False,
|
|
|
|
}
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
2019-12-18 19:16:53 +00:00
|
|
|
freqtrade.wallets.update()
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is True
|
2018-07-11 18:20:44 +00:00
|
|
|
assert trade.sell_reason == SellType.SELL_SIGNAL.value
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_sell_profit_only_enable_loss(default_conf, limit_buy_order, limit_buy_order_open,
|
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-02-04 09:21:16 +00:00
|
|
|
'bid': 0.00000172,
|
|
|
|
'ask': 0.00000173,
|
|
|
|
'last': 0.00000172
|
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2019-10-05 10:29:59 +00:00
|
|
|
default_conf['ask_strategy'] = {
|
2018-02-04 09:21:16 +00:00
|
|
|
'use_sell_signal': True,
|
|
|
|
'sell_profit_only': True,
|
|
|
|
}
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2019-03-17 14:39:05 +00:00
|
|
|
freqtrade.strategy.stop_loss_reached = MagicMock(return_value=SellCheckTuple(
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_type=SellType.NONE))
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is False
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_sell_profit_only_disable_loss(default_conf, limit_buy_order, limit_buy_order_open,
|
|
|
|
fee, mocker) -> None:
|
2018-02-04 09:21:16 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch.multiple(
|
2018-06-17 19:11:10 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-06-22 19:24:21 +00:00
|
|
|
'bid': 0.0000172,
|
|
|
|
'ask': 0.0000173,
|
|
|
|
'last': 0.0000172
|
2018-02-04 09:21:16 +00:00
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2019-10-05 10:29:59 +00:00
|
|
|
default_conf['ask_strategy'] = {
|
2018-02-04 09:21:16 +00:00
|
|
|
'use_sell_signal': True,
|
|
|
|
'sell_profit_only': False,
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
2019-12-18 19:16:53 +00:00
|
|
|
freqtrade.wallets.update()
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is True
|
2018-07-11 18:20:44 +00:00
|
|
|
assert trade.sell_reason == SellType.SELL_SIGNAL.value
|
2018-04-15 17:38:58 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_sell_not_enough_balance(default_conf, limit_buy_order, limit_buy_order_open,
|
2019-12-13 06:06:54 +00:00
|
|
|
fee, mocker, caplog) -> None:
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-28 08:35:45 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2019-12-13 06:06:54 +00:00
|
|
|
'bid': 0.00002172,
|
|
|
|
'ask': 0.00002173,
|
|
|
|
'last': 0.00002172
|
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2019-12-13 06:06:54 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-12-13 06:06:54 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
amnt = trade.amount
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
|
|
|
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=trade.amount * 0.985))
|
|
|
|
|
|
|
|
assert freqtrade.handle_trade(trade) is True
|
|
|
|
assert log_has_re(r'.*Falling back to wallet-amount.', caplog)
|
|
|
|
assert trade.amount != amnt
|
|
|
|
|
|
|
|
|
2019-12-18 19:16:53 +00:00
|
|
|
def test__safe_sell_amount(default_conf, fee, caplog, mocker):
|
2019-12-13 06:06:54 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
amount = 95.33
|
|
|
|
amount_wallet = 95.29
|
|
|
|
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=amount_wallet))
|
2020-01-15 20:53:04 +00:00
|
|
|
wallet_update = mocker.patch('freqtrade.wallets.Wallets.update')
|
2019-12-13 06:06:54 +00:00
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
2019-12-18 19:16:53 +00:00
|
|
|
open_order_id="123456",
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2019-12-13 06:06:54 +00:00
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
2020-01-15 20:53:04 +00:00
|
|
|
wallet_update.reset_mock()
|
2019-12-13 06:06:54 +00:00
|
|
|
assert freqtrade._safe_sell_amount(trade.pair, trade.amount) == amount_wallet
|
|
|
|
assert log_has_re(r'.*Falling back to wallet-amount.', caplog)
|
2020-01-15 20:53:04 +00:00
|
|
|
assert wallet_update.call_count == 1
|
2020-01-15 18:59:08 +00:00
|
|
|
caplog.clear()
|
2020-01-15 20:53:04 +00:00
|
|
|
wallet_update.reset_mock()
|
2020-01-15 18:59:08 +00:00
|
|
|
assert freqtrade._safe_sell_amount(trade.pair, amount_wallet) == amount_wallet
|
|
|
|
assert not log_has_re(r'.*Falling back to wallet-amount.', caplog)
|
2020-01-15 20:53:04 +00:00
|
|
|
assert wallet_update.call_count == 1
|
2019-12-13 06:06:54 +00:00
|
|
|
|
|
|
|
|
2019-12-18 19:16:53 +00:00
|
|
|
def test__safe_sell_amount_error(default_conf, fee, caplog, mocker):
|
2019-12-13 06:06:54 +00:00
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
amount = 95.33
|
|
|
|
amount_wallet = 91.29
|
|
|
|
mocker.patch('freqtrade.wallets.Wallets.get_free', MagicMock(return_value=amount_wallet))
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
2019-12-18 19:16:53 +00:00
|
|
|
open_order_id="123456",
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2019-12-13 06:06:54 +00:00
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
with pytest.raises(DependencyException, match=r"Not enough amount to sell."):
|
|
|
|
assert freqtrade._safe_sell_amount(trade.pair, trade.amount)
|
|
|
|
|
|
|
|
|
2019-10-26 08:08:23 +00:00
|
|
|
def test_locked_pairs(default_conf, ticker, fee, ticker_sell_down, mocker, caplog) -> None:
|
2019-08-12 18:48:21 +00:00
|
|
|
patch_RPCManager(mocker)
|
2019-09-10 21:18:07 +00:00
|
|
|
patch_exchange(mocker)
|
2019-08-12 18:48:21 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2019-08-12 18:48:21 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-08-12 18:48:21 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
|
|
|
|
|
|
|
# Decrease the price and sell it
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker_sell_down
|
2019-08-12 18:48:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
freqtrade.execute_sell(trade=trade, limit=ticker_sell_down()['bid'],
|
2021-04-22 06:21:19 +00:00
|
|
|
sell_reason=SellCheckTuple(sell_type=SellType.STOP_LOSS))
|
2019-08-12 18:48:21 +00:00
|
|
|
trade.close(ticker_sell_down()['bid'])
|
|
|
|
assert freqtrade.strategy.is_pair_locked(trade.pair)
|
|
|
|
|
|
|
|
# reinit - should buy other pair.
|
|
|
|
caplog.clear()
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-08-12 18:48:21 +00:00
|
|
|
|
2020-11-25 10:54:11 +00:00
|
|
|
assert log_has_re(f"Pair {trade.pair} is still locked.*", caplog)
|
2019-08-12 18:48:21 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_ignore_roi_if_buy_signal(default_conf, limit_buy_order, limit_buy_order_open,
|
|
|
|
fee, mocker) -> None:
|
2018-06-22 18:10:05 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-06-22 18:10:05 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-06-22 19:24:21 +00:00
|
|
|
'bid': 0.0000172,
|
|
|
|
'ask': 0.0000173,
|
|
|
|
'last': 0.0000172
|
2018-06-22 18:10:05 +00:00
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-06-22 18:10:05 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
2019-10-05 10:29:59 +00:00
|
|
|
default_conf['ask_strategy'] = {
|
2018-06-22 18:10:05 +00:00
|
|
|
'ignore_roi_if_buy_signal': True
|
|
|
|
}
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=True)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-06-22 18:10:05 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
2019-12-18 19:16:53 +00:00
|
|
|
freqtrade.wallets.update()
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(True, True))
|
2018-06-22 18:10:05 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
|
|
|
|
# Test if buy-signal is absent (should sell due to roi = true)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2018-06-22 18:10:05 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is True
|
2018-07-11 18:20:44 +00:00
|
|
|
assert trade.sell_reason == SellType.ROI.value
|
2018-06-22 18:10:05 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_trailing_stop_loss(default_conf, limit_buy_order_open, limit_buy_order,
|
|
|
|
fee, caplog, mocker) -> None:
|
2018-06-26 21:40:36 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-06-26 21:40:36 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2019-06-02 11:27:31 +00:00
|
|
|
'bid': 0.00001099,
|
|
|
|
'ask': 0.00001099,
|
|
|
|
'last': 0.00001099
|
2018-06-26 21:40:36 +00:00
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-06-26 21:40:36 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['trailing_stop'] = True
|
2019-10-26 08:08:23 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-06-26 21:40:36 +00:00
|
|
|
trade = Trade.query.first()
|
2019-06-02 11:27:31 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
|
|
|
|
# Raise ticker above buy price
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
2019-06-02 11:27:31 +00:00
|
|
|
MagicMock(return_value={
|
|
|
|
'bid': 0.00001099 * 1.5,
|
|
|
|
'ask': 0.00001099 * 1.5,
|
|
|
|
'last': 0.00001099 * 1.5
|
|
|
|
}))
|
|
|
|
|
|
|
|
# Stoploss should be adjusted
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
|
|
|
|
# Price fell
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
2019-06-02 11:27:31 +00:00
|
|
|
MagicMock(return_value={
|
|
|
|
'bid': 0.00001099 * 1.1,
|
|
|
|
'ask': 0.00001099 * 1.1,
|
|
|
|
'last': 0.00001099 * 1.1
|
|
|
|
}))
|
|
|
|
|
2018-06-26 21:40:36 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-06-26 22:16:19 +00:00
|
|
|
# Sell as trailing-stop is reached
|
2018-06-26 21:40:36 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is True
|
2020-05-18 09:40:25 +00:00
|
|
|
assert log_has("ETH/BTC - HIT STOP: current price at 0.000012, stoploss is 0.000015, "
|
|
|
|
"initial stoploss was at 0.000010, trade opened at 0.000011", caplog)
|
2018-07-11 18:20:44 +00:00
|
|
|
assert trade.sell_reason == SellType.TRAILING_STOP_LOSS.value
|
2018-06-26 21:40:36 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_trailing_stop_loss_positive(default_conf, limit_buy_order, limit_buy_order_open, fee,
|
2018-07-22 12:24:41 +00:00
|
|
|
caplog, mocker) -> None:
|
2018-06-27 04:51:48 +00:00
|
|
|
buy_price = limit_buy_order['price']
|
2018-06-26 22:16:19 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-06-26 22:16:19 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-06-27 04:51:48 +00:00
|
|
|
'bid': buy_price - 0.000001,
|
|
|
|
'ask': buy_price - 0.000001,
|
|
|
|
'last': buy_price - 0.000001
|
2018-06-26 22:16:19 +00:00
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-06-26 22:16:19 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['trailing_stop'] = True
|
|
|
|
default_conf['trailing_stop_positive'] = 0.01
|
2019-10-26 08:08:23 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2019-10-26 11:28:04 +00:00
|
|
|
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-06-26 22:16:19 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
# stop-loss not reached
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
2018-06-27 04:51:48 +00:00
|
|
|
|
|
|
|
# Raise ticker above buy price
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
2018-06-27 04:51:48 +00:00
|
|
|
MagicMock(return_value={
|
|
|
|
'bid': buy_price + 0.000003,
|
|
|
|
'ask': buy_price + 0.000003,
|
|
|
|
'last': buy_price + 0.000003
|
|
|
|
}))
|
|
|
|
# stop-loss not reached, adjusted stoploss
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
2020-05-18 09:40:25 +00:00
|
|
|
assert log_has("ETH/BTC - Using positive stoploss: 0.01 offset: 0 profit: 0.2666%", caplog)
|
|
|
|
assert log_has("ETH/BTC - Adjusting stoploss...", caplog)
|
2018-07-16 19:23:35 +00:00
|
|
|
assert trade.stop_loss == 0.0000138501
|
|
|
|
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
2018-07-16 19:23:35 +00:00
|
|
|
MagicMock(return_value={
|
|
|
|
'bid': buy_price + 0.000002,
|
|
|
|
'ask': buy_price + 0.000002,
|
|
|
|
'last': buy_price + 0.000002
|
|
|
|
}))
|
|
|
|
# Lower price again (but still positive)
|
|
|
|
assert freqtrade.handle_trade(trade) is True
|
|
|
|
assert log_has(
|
2019-09-11 20:32:08 +00:00
|
|
|
f"ETH/BTC - HIT STOP: current price at {buy_price + 0.000002:.6f}, "
|
2019-09-10 07:43:15 +00:00
|
|
|
f"stoploss is {trade.stop_loss:.6f}, "
|
|
|
|
f"initial stoploss was at 0.000010, trade opened at 0.000011", caplog)
|
2018-07-16 19:23:35 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_trailing_stop_loss_offset(default_conf, limit_buy_order, limit_buy_order_open, fee,
|
2019-10-26 08:08:23 +00:00
|
|
|
caplog, mocker) -> None:
|
2018-07-16 19:23:35 +00:00
|
|
|
buy_price = limit_buy_order['price']
|
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-07-16 19:23:35 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-07-16 19:23:35 +00:00
|
|
|
'bid': buy_price - 0.000001,
|
|
|
|
'ask': buy_price - 0.000001,
|
|
|
|
'last': buy_price - 0.000001
|
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-07-16 19:23:35 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
2019-10-26 08:08:23 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2018-07-30 12:40:52 +00:00
|
|
|
default_conf['trailing_stop'] = True
|
|
|
|
default_conf['trailing_stop_positive'] = 0.01
|
|
|
|
default_conf['trailing_stop_positive_offset'] = 0.011
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-19 18:36:49 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-07-16 19:23:35 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
# stop-loss not reached
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
|
|
|
|
# Raise ticker above buy price
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
2018-07-16 19:23:35 +00:00
|
|
|
MagicMock(return_value={
|
|
|
|
'bid': buy_price + 0.000003,
|
|
|
|
'ask': buy_price + 0.000003,
|
|
|
|
'last': buy_price + 0.000003
|
|
|
|
}))
|
|
|
|
# stop-loss not reached, adjusted stoploss
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
2020-05-18 09:40:25 +00:00
|
|
|
assert log_has("ETH/BTC - Using positive stoploss: 0.01 offset: 0.011 profit: 0.2666%", caplog)
|
|
|
|
assert log_has("ETH/BTC - Adjusting stoploss...", caplog)
|
2018-06-27 04:51:48 +00:00
|
|
|
assert trade.stop_loss == 0.0000138501
|
|
|
|
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
2018-06-26 22:16:19 +00:00
|
|
|
MagicMock(return_value={
|
2018-06-27 04:51:48 +00:00
|
|
|
'bid': buy_price + 0.000002,
|
|
|
|
'ask': buy_price + 0.000002,
|
|
|
|
'last': buy_price + 0.000002
|
|
|
|
}))
|
|
|
|
# Lower price again (but still positive)
|
2018-06-26 22:16:19 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is True
|
|
|
|
assert log_has(
|
2019-09-11 20:32:08 +00:00
|
|
|
f"ETH/BTC - HIT STOP: current price at {buy_price + 0.000002:.6f}, "
|
2019-09-10 07:43:15 +00:00
|
|
|
f"stoploss is {trade.stop_loss:.6f}, "
|
|
|
|
f"initial stoploss was at 0.000010, trade opened at 0.000011", caplog)
|
2018-07-11 18:20:44 +00:00
|
|
|
assert trade.sell_reason == SellType.TRAILING_STOP_LOSS.value
|
2018-06-26 21:40:36 +00:00
|
|
|
|
2018-06-27 04:51:48 +00:00
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_tsl_only_offset_reached(default_conf, limit_buy_order, limit_buy_order_open, fee,
|
2019-10-26 08:08:23 +00:00
|
|
|
caplog, mocker) -> None:
|
2019-03-10 16:11:28 +00:00
|
|
|
buy_price = limit_buy_order['price']
|
|
|
|
# buy_price: 0.00001099
|
|
|
|
|
|
|
|
patch_RPCManager(mocker)
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2019-03-10 16:11:28 +00:00
|
|
|
'bid': buy_price,
|
|
|
|
'ask': buy_price,
|
|
|
|
'last': buy_price
|
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2019-03-10 16:11:28 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
2019-10-26 08:08:23 +00:00
|
|
|
patch_whitelist(mocker, default_conf)
|
2019-03-10 16:11:28 +00:00
|
|
|
default_conf['trailing_stop'] = True
|
|
|
|
default_conf['trailing_stop_positive'] = 0.05
|
|
|
|
default_conf['trailing_stop_positive_offset'] = 0.055
|
|
|
|
default_conf['trailing_only_offset_is_reached'] = True
|
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=False)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2019-03-10 16:11:28 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
# stop-loss not reached
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
assert trade.stop_loss == 0.0000098910
|
|
|
|
|
|
|
|
# Raise ticker above buy price
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
2019-03-10 16:11:28 +00:00
|
|
|
MagicMock(return_value={
|
|
|
|
'bid': buy_price + 0.0000004,
|
|
|
|
'ask': buy_price + 0.0000004,
|
|
|
|
'last': buy_price + 0.0000004
|
|
|
|
}))
|
|
|
|
|
|
|
|
# stop-loss should not be adjusted as offset is not reached yet
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
|
|
|
|
2020-05-18 09:40:25 +00:00
|
|
|
assert not log_has("ETH/BTC - Adjusting stoploss...", caplog)
|
2019-03-10 16:11:28 +00:00
|
|
|
assert trade.stop_loss == 0.0000098910
|
|
|
|
|
|
|
|
# price rises above the offset (rises 12% when the offset is 5.5%)
|
2019-12-18 15:34:30 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
2019-03-10 16:11:28 +00:00
|
|
|
MagicMock(return_value={
|
|
|
|
'bid': buy_price + 0.0000014,
|
|
|
|
'ask': buy_price + 0.0000014,
|
|
|
|
'last': buy_price + 0.0000014
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert freqtrade.handle_trade(trade) is False
|
2020-05-18 09:40:25 +00:00
|
|
|
assert log_has("ETH/BTC - Using positive stoploss: 0.05 offset: 0.055 profit: 0.1218%", caplog)
|
|
|
|
assert log_has("ETH/BTC - Adjusting stoploss...", caplog)
|
2019-03-10 16:11:28 +00:00
|
|
|
assert trade.stop_loss == 0.0000117705
|
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_disable_ignore_roi_if_buy_signal(default_conf, limit_buy_order, limit_buy_order_open,
|
2019-10-31 06:26:48 +00:00
|
|
|
fee, mocker) -> None:
|
2018-06-22 18:10:05 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-06-22 18:10:05 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-02-04 09:21:16 +00:00
|
|
|
'bid': 0.00000172,
|
|
|
|
'ask': 0.00000173,
|
|
|
|
'last': 0.00000172
|
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-04-21 17:39:18 +00:00
|
|
|
get_fee=fee,
|
2021-06-05 13:22:52 +00:00
|
|
|
_is_dry_limit_order_filled=MagicMock(return_value=False),
|
2018-02-04 09:21:16 +00:00
|
|
|
)
|
2019-10-05 10:29:59 +00:00
|
|
|
default_conf['ask_strategy'] = {
|
2018-06-22 18:10:05 +00:00
|
|
|
'ignore_roi_if_buy_signal': False
|
2018-02-04 09:21:16 +00:00
|
|
|
}
|
2018-07-30 12:40:52 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade)
|
2018-11-25 13:31:46 +00:00
|
|
|
freqtrade.strategy.min_roi_reached = MagicMock(return_value=True)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-02-04 09:21:16 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.update(limit_buy_order)
|
2018-06-22 18:10:05 +00:00
|
|
|
# Sell due to min_roi_reached
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(True, True))
|
2018-06-22 18:10:05 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is True
|
|
|
|
|
|
|
|
# Test if buy-signal is absent
|
2018-07-16 05:11:17 +00:00
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
2018-03-15 22:48:22 +00:00
|
|
|
assert freqtrade.handle_trade(trade) is True
|
2020-11-27 08:18:03 +00:00
|
|
|
assert trade.sell_reason == SellType.SELL_SIGNAL.value
|
2018-04-15 17:38:58 +00:00
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_quote(default_conf, trades_for_order, buy_order_fee, fee, caplog, mocker):
|
2018-06-17 19:11:10 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
2018-04-15 17:56:33 +00:00
|
|
|
amount = sum(x['amount'] for x in trades_for_order)
|
2018-04-15 17:38:58 +00:00
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
2018-04-15 17:56:33 +00:00
|
|
|
amount=amount,
|
2018-04-15 17:38:58 +00:00
|
|
|
exchange='binance',
|
2018-04-25 06:52:08 +00:00
|
|
|
open_rate=0.245441,
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-04-15 17:38:58 +00:00
|
|
|
open_order_id="123456"
|
2018-10-04 16:07:47 +00:00
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-04-15 17:56:33 +00:00
|
|
|
# Amount is reduced by "fee"
|
2018-04-25 06:52:08 +00:00
|
|
|
assert freqtrade.get_real_amount(trade, buy_order_fee) == amount - (amount * 0.001)
|
|
|
|
assert log_has('Applying fee on amount for Trade(id=None, pair=LTC/ETH, amount=8.00000000, '
|
2020-05-03 08:50:59 +00:00
|
|
|
'open_rate=0.24544100, open_since=closed) (from 8.0 to 7.992).',
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2018-04-15 17:56:33 +00:00
|
|
|
|
|
|
|
|
2020-05-03 09:28:29 +00:00
|
|
|
def test_get_real_amount_quote_dust(default_conf, trades_for_order, buy_order_fee, fee,
|
|
|
|
caplog, mocker):
|
2020-05-03 09:13:59 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
|
|
|
walletmock = mocker.patch('freqtrade.wallets.Wallets.update')
|
|
|
|
mocker.patch('freqtrade.wallets.Wallets.get_free', return_value=8.1122)
|
|
|
|
amount = sum(x['amount'] for x in trades_for_order)
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
open_order_id="123456"
|
|
|
|
)
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
walletmock.reset_mock()
|
|
|
|
# Amount is kept as is
|
|
|
|
assert freqtrade.get_real_amount(trade, buy_order_fee) == amount
|
|
|
|
assert walletmock.call_count == 1
|
|
|
|
assert log_has_re(r'Fee amount for Trade.* was in base currency '
|
2020-05-03 09:28:29 +00:00
|
|
|
'- Eating Fee 0.008 into dust', caplog)
|
2020-05-03 09:13:59 +00:00
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_no_trade(default_conf, buy_order_fee, caplog, mocker, fee):
|
2018-06-17 19:11:10 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[])
|
2018-04-25 07:08:02 +00:00
|
|
|
|
|
|
|
amount = buy_order_fee['amount']
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-04-25 07:08:02 +00:00
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-04-25 07:08:02 +00:00
|
|
|
# Amount is reduced by "fee"
|
|
|
|
assert freqtrade.get_real_amount(trade, buy_order_fee) == amount
|
|
|
|
assert log_has('Applying fee on amount for Trade(id=None, pair=LTC/ETH, amount=8.00000000, '
|
|
|
|
'open_rate=0.24544100, open_since=closed) failed: myTrade-Dict empty found',
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2018-04-25 07:08:02 +00:00
|
|
|
|
2018-04-25 07:13:56 +00:00
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_stake(default_conf, trades_for_order, buy_order_fee, fee, mocker):
|
2018-04-15 17:56:33 +00:00
|
|
|
trades_for_order[0]['fee']['currency'] = 'ETH'
|
2018-04-15 17:38:58 +00:00
|
|
|
|
2018-06-17 19:11:10 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
2018-04-15 17:56:33 +00:00
|
|
|
amount = sum(x['amount'] for x in trades_for_order)
|
2018-04-15 17:38:58 +00:00
|
|
|
trade = Trade(
|
2018-04-15 17:56:33 +00:00
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
2018-04-15 17:38:58 +00:00
|
|
|
exchange='binance',
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-04-25 06:52:08 +00:00
|
|
|
open_rate=0.245441,
|
2018-04-15 17:38:58 +00:00
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-04-15 17:56:33 +00:00
|
|
|
# Amount does not change
|
2018-04-25 06:52:08 +00:00
|
|
|
assert freqtrade.get_real_amount(trade, buy_order_fee) == amount
|
2018-04-15 17:56:33 +00:00
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_no_currency_in_fee(default_conf, trades_for_order, buy_order_fee,
|
|
|
|
fee, mocker):
|
2019-06-27 04:29:18 +00:00
|
|
|
|
|
|
|
limit_buy_order = deepcopy(buy_order_fee)
|
|
|
|
limit_buy_order['fee'] = {'cost': 0.004, 'currency': None}
|
|
|
|
trades_for_order[0]['fee']['currency'] = None
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
|
|
|
amount = sum(x['amount'] for x in trades_for_order)
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2019-06-27 04:29:18 +00:00
|
|
|
open_rate=0.245441,
|
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2019-06-27 04:29:18 +00:00
|
|
|
|
|
|
|
# Amount does not change
|
|
|
|
assert freqtrade.get_real_amount(trade, limit_buy_order) == amount
|
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_BNB(default_conf, trades_for_order, buy_order_fee, fee, mocker):
|
2018-04-15 17:56:33 +00:00
|
|
|
trades_for_order[0]['fee']['currency'] = 'BNB'
|
|
|
|
trades_for_order[0]['fee']['cost'] = 0.00094518
|
2018-04-15 17:38:58 +00:00
|
|
|
|
2018-06-17 19:11:10 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
2018-04-15 17:56:33 +00:00
|
|
|
amount = sum(x['amount'] for x in trades_for_order)
|
2018-04-15 17:38:58 +00:00
|
|
|
trade = Trade(
|
2018-04-15 17:56:33 +00:00
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
2018-04-15 17:38:58 +00:00
|
|
|
exchange='binance',
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-04-25 06:52:08 +00:00
|
|
|
open_rate=0.245441,
|
2018-04-15 17:38:58 +00:00
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-04-15 17:56:33 +00:00
|
|
|
# Amount does not change
|
2018-04-25 06:52:08 +00:00
|
|
|
assert freqtrade.get_real_amount(trade, buy_order_fee) == amount
|
2018-04-15 17:56:33 +00:00
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_multi(default_conf, trades_for_order2, buy_order_fee, caplog, fee, mocker):
|
2018-06-17 19:11:10 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order2)
|
2018-04-15 17:56:33 +00:00
|
|
|
amount = float(sum(x['amount'] for x in trades_for_order2))
|
2018-04-15 17:38:58 +00:00
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
2018-04-15 17:56:33 +00:00
|
|
|
amount=amount,
|
2018-04-15 17:38:58 +00:00
|
|
|
exchange='binance',
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-04-25 06:52:08 +00:00
|
|
|
open_rate=0.245441,
|
2018-04-15 17:38:58 +00:00
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-04-15 17:56:33 +00:00
|
|
|
# Amount is reduced by "fee"
|
2018-04-25 06:52:08 +00:00
|
|
|
assert freqtrade.get_real_amount(trade, buy_order_fee) == amount - (amount * 0.001)
|
|
|
|
assert log_has('Applying fee on amount for Trade(id=None, pair=LTC/ETH, amount=8.00000000, '
|
2020-05-03 08:50:59 +00:00
|
|
|
'open_rate=0.24544100, open_since=closed) (from 8.0 to 7.992).',
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2018-04-25 06:52:08 +00:00
|
|
|
|
2020-12-12 10:43:47 +00:00
|
|
|
assert trade.fee_open == 0.001
|
|
|
|
assert trade.fee_close == 0.001
|
|
|
|
assert trade.fee_open_cost is not None
|
|
|
|
assert trade.fee_open_currency is not None
|
|
|
|
assert trade.fee_close_cost is None
|
|
|
|
assert trade.fee_close_currency is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_real_amount_multi2(default_conf, trades_for_order3, buy_order_fee, caplog, fee,
|
|
|
|
mocker, markets):
|
|
|
|
# Different fee currency on both trades
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order3)
|
|
|
|
amount = float(sum(x['amount'] for x in trades_for_order3))
|
|
|
|
default_conf['stake_currency'] = 'ETH'
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
open_rate=0.245441,
|
|
|
|
open_order_id="123456"
|
|
|
|
)
|
|
|
|
# Fake markets entry to enable fee parsing
|
|
|
|
markets['BNB/ETH'] = markets['ETH/BTC']
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker',
|
|
|
|
return_value={'ask': 0.19, 'last': 0.2})
|
|
|
|
|
|
|
|
# Amount is reduced by "fee"
|
|
|
|
assert freqtrade.get_real_amount(trade, buy_order_fee) == amount - (amount * 0.0005)
|
|
|
|
assert log_has('Applying fee on amount for Trade(id=None, pair=LTC/ETH, amount=8.00000000, '
|
|
|
|
'open_rate=0.24544100, open_since=closed) (from 8.0 to 7.996).',
|
|
|
|
caplog)
|
|
|
|
# Overall fee is average of both trade's fee
|
|
|
|
assert trade.fee_open == 0.001518575
|
|
|
|
assert trade.fee_open_cost is not None
|
|
|
|
assert trade.fee_open_currency is not None
|
|
|
|
assert trade.fee_close_cost is None
|
|
|
|
assert trade.fee_close_currency is None
|
|
|
|
|
2018-04-25 06:52:08 +00:00
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_fromorder(default_conf, trades_for_order, buy_order_fee, fee,
|
|
|
|
caplog, mocker):
|
2018-04-25 06:52:08 +00:00
|
|
|
limit_buy_order = deepcopy(buy_order_fee)
|
|
|
|
limit_buy_order['fee'] = {'cost': 0.004, 'currency': 'LTC'}
|
|
|
|
|
2018-06-17 19:11:10 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order',
|
|
|
|
return_value=[trades_for_order])
|
2018-04-25 06:52:08 +00:00
|
|
|
amount = float(sum(x['amount'] for x in trades_for_order))
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-04-25 06:52:08 +00:00
|
|
|
open_rate=0.245441,
|
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2021-01-28 18:40:10 +00:00
|
|
|
# Ticker rate cannot be found for this to work.
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', side_effect=ExchangeError)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-04-25 06:52:08 +00:00
|
|
|
# Amount is reduced by "fee"
|
|
|
|
assert freqtrade.get_real_amount(trade, limit_buy_order) == amount - 0.004
|
|
|
|
assert log_has('Applying fee on amount for Trade(id=None, pair=LTC/ETH, amount=8.00000000, '
|
2020-05-03 08:50:59 +00:00
|
|
|
'open_rate=0.24544100, open_since=closed) (from 8.0 to 7.996).',
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2018-05-15 17:49:47 +00:00
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_invalid_order(default_conf, trades_for_order, buy_order_fee, fee, mocker):
|
2018-05-15 18:13:43 +00:00
|
|
|
limit_buy_order = deepcopy(buy_order_fee)
|
|
|
|
limit_buy_order['fee'] = {'cost': 0.004}
|
|
|
|
|
2018-06-17 19:11:10 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=[])
|
2018-05-15 18:13:43 +00:00
|
|
|
amount = float(sum(x['amount'] for x in trades_for_order))
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-05-15 18:13:43 +00:00
|
|
|
open_rate=0.245441,
|
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-07-16 05:11:17 +00:00
|
|
|
|
2018-05-15 18:13:43 +00:00
|
|
|
# Amount does not change
|
|
|
|
assert freqtrade.get_real_amount(trade, limit_buy_order) == amount
|
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_wrong_amount(default_conf, trades_for_order, buy_order_fee, fee, mocker):
|
2019-09-26 04:26:25 +00:00
|
|
|
limit_buy_order = deepcopy(buy_order_fee)
|
2019-09-26 04:48:46 +00:00
|
|
|
limit_buy_order['amount'] = limit_buy_order['amount'] - 0.001
|
2019-09-26 04:26:25 +00:00
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
|
|
|
amount = float(sum(x['amount'] for x in trades_for_order))
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2019-09-26 04:26:25 +00:00
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2019-09-26 04:26:25 +00:00
|
|
|
|
|
|
|
# Amount does not change
|
2019-10-17 17:33:21 +00:00
|
|
|
with pytest.raises(DependencyException, match=r"Half bought\? Amounts don't match"):
|
2019-09-26 04:26:25 +00:00
|
|
|
freqtrade.get_real_amount(trade, limit_buy_order)
|
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_wrong_amount_rounding(default_conf, trades_for_order, buy_order_fee, fee,
|
2019-09-26 04:48:46 +00:00
|
|
|
mocker):
|
|
|
|
# Floats should not be compared directly.
|
|
|
|
limit_buy_order = deepcopy(buy_order_fee)
|
|
|
|
trades_for_order[0]['amount'] = trades_for_order[0]['amount'] + 1e-15
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
|
|
|
amount = float(sum(x['amount'] for x in trades_for_order))
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2019-09-26 04:48:46 +00:00
|
|
|
open_rate=0.245441,
|
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2019-09-26 04:48:46 +00:00
|
|
|
|
|
|
|
# Amount changes by fee amount.
|
|
|
|
assert isclose(freqtrade.get_real_amount(trade, limit_buy_order), amount - (amount * 0.001),
|
2019-09-28 08:30:12 +00:00
|
|
|
abs_tol=MATH_CLOSE_PREC,)
|
2019-09-26 04:48:46 +00:00
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_invalid(default_conf, trades_for_order, buy_order_fee, fee, mocker):
|
2018-05-15 17:49:47 +00:00
|
|
|
# Remove "Currency" from fee dict
|
|
|
|
trades_for_order[0]['fee'] = {'cost': 0.008}
|
|
|
|
|
2018-06-17 19:11:10 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_trades_for_order', return_value=trades_for_order)
|
2018-05-15 17:49:47 +00:00
|
|
|
amount = sum(x['amount'] for x in trades_for_order)
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
|
2018-05-15 17:49:47 +00:00
|
|
|
open_order_id="123456"
|
|
|
|
)
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-05-15 17:49:47 +00:00
|
|
|
# Amount does not change
|
|
|
|
assert freqtrade.get_real_amount(trade, buy_order_fee) == amount
|
2018-06-03 05:06:17 +00:00
|
|
|
|
|
|
|
|
2019-12-17 05:58:10 +00:00
|
|
|
def test_get_real_amount_open_trade(default_conf, fee, mocker):
|
2018-06-03 05:06:17 +00:00
|
|
|
amount = 12345
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
2019-12-17 05:58:10 +00:00
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
2018-06-03 05:06:17 +00:00
|
|
|
open_order_id="123456"
|
|
|
|
)
|
|
|
|
order = {
|
|
|
|
'id': 'mocked_order',
|
|
|
|
'amount': amount,
|
|
|
|
'status': 'open',
|
2020-05-01 18:03:06 +00:00
|
|
|
'side': 'buy',
|
2018-06-03 05:06:17 +00:00
|
|
|
}
|
2020-04-30 18:15:35 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2018-06-03 05:06:17 +00:00
|
|
|
assert freqtrade.get_real_amount(trade, order) == amount
|
2018-08-15 04:05:56 +00:00
|
|
|
|
|
|
|
|
2020-05-03 09:28:29 +00:00
|
|
|
@pytest.mark.parametrize('amount,fee_abs,wallet,amount_exp', [
|
|
|
|
(8.0, 0.0, 10, 8),
|
|
|
|
(8.0, 0.0, 0, 8),
|
|
|
|
(8.0, 0.1, 0, 7.9),
|
|
|
|
(8.0, 0.1, 10, 8),
|
|
|
|
(8.0, 0.1, 8.0, 8.0),
|
|
|
|
(8.0, 0.1, 7.9, 7.9),
|
|
|
|
])
|
|
|
|
def test_apply_fee_conditional(default_conf, fee, caplog, mocker,
|
|
|
|
amount, fee_abs, wallet, amount_exp):
|
|
|
|
walletmock = mocker.patch('freqtrade.wallets.Wallets.update')
|
|
|
|
mocker.patch('freqtrade.wallets.Wallets.get_free', return_value=wallet)
|
|
|
|
trade = Trade(
|
|
|
|
pair='LTC/ETH',
|
|
|
|
amount=amount,
|
|
|
|
exchange='binance',
|
|
|
|
open_rate=0.245441,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
open_order_id="123456"
|
|
|
|
)
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
walletmock.reset_mock()
|
|
|
|
# Amount is kept as is
|
|
|
|
assert freqtrade.apply_fee_conditional(trade, 'LTC', amount, fee_abs) == amount_exp
|
|
|
|
assert walletmock.call_count == 1
|
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_order_book_depth_of_market(default_conf, ticker, limit_buy_order_open, limit_buy_order,
|
|
|
|
fee, mocker, order_book_l2):
|
2018-08-29 09:38:43 +00:00
|
|
|
default_conf['bid_strategy']['check_depth_of_market']['enabled'] = True
|
|
|
|
default_conf['bid_strategy']['check_depth_of_market']['bids_to_ask_delta'] = 0.1
|
2018-08-05 04:41:06 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2020-05-26 18:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2)
|
2018-08-05 04:41:06 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2018-08-05 04:41:06 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Save state of current whitelist
|
|
|
|
whitelist = deepcopy(default_conf['exchange']['pair_whitelist'])
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-08-05 04:41:06 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade is not None
|
|
|
|
assert trade.stake_amount == 0.001
|
|
|
|
assert trade.is_open
|
|
|
|
assert trade.open_date is not None
|
2021-04-20 10:54:22 +00:00
|
|
|
assert trade.exchange == 'binance'
|
2018-08-05 04:41:06 +00:00
|
|
|
|
2019-11-05 20:03:06 +00:00
|
|
|
assert len(Trade.query.all()) == 1
|
|
|
|
|
2018-08-05 04:41:06 +00:00
|
|
|
# Simulate fulfilled LIMIT_BUY order for trade
|
|
|
|
trade.update(limit_buy_order)
|
|
|
|
|
|
|
|
assert trade.open_rate == 0.00001099
|
|
|
|
assert whitelist == default_conf['exchange']['pair_whitelist']
|
2018-08-05 14:41:58 +00:00
|
|
|
|
|
|
|
|
2018-08-14 10:12:44 +00:00
|
|
|
def test_order_book_depth_of_market_high_delta(default_conf, ticker, limit_buy_order,
|
2019-10-31 06:26:48 +00:00
|
|
|
fee, mocker, order_book_l2):
|
2018-08-29 09:38:43 +00:00
|
|
|
default_conf['bid_strategy']['check_depth_of_market']['enabled'] = True
|
2018-08-07 10:29:37 +00:00
|
|
|
# delta is 100 which is impossible to reach. hence check_depth_of_market will return false
|
2018-08-29 09:38:43 +00:00
|
|
|
default_conf['bid_strategy']['check_depth_of_market']['bids_to_ask_delta'] = 100
|
2018-08-05 14:41:58 +00:00
|
|
|
patch_RPCManager(mocker)
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2020-05-26 18:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2)
|
2018-08-05 14:41:58 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2018-08-05 14:41:58 +00:00
|
|
|
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
|
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
# Save state of current whitelist
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-08-05 14:41:58 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade is None
|
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_order_book_bid_strategy1(mocker, default_conf, order_book_l2) -> None:
|
2018-08-07 10:29:37 +00:00
|
|
|
"""
|
2020-01-22 13:46:28 +00:00
|
|
|
test if function get_buy_rate will return the order book price
|
2018-08-07 10:29:37 +00:00
|
|
|
instead of the ask rate
|
|
|
|
"""
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2019-02-14 06:27:13 +00:00
|
|
|
ticker_mock = MagicMock(return_value={'ask': 0.045, 'last': 0.046})
|
2018-08-25 11:21:10 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2020-05-26 18:27:35 +00:00
|
|
|
fetch_l2_order_book=order_book_l2,
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker_mock,
|
2019-02-14 06:27:13 +00:00
|
|
|
|
2018-08-25 11:21:10 +00:00
|
|
|
)
|
2018-08-05 14:41:58 +00:00
|
|
|
default_conf['exchange']['name'] = 'binance'
|
2018-08-29 09:38:43 +00:00
|
|
|
default_conf['bid_strategy']['use_order_book'] = True
|
|
|
|
default_conf['bid_strategy']['order_book_top'] = 2
|
2018-08-05 14:56:14 +00:00
|
|
|
default_conf['bid_strategy']['ask_last_balance'] = 0
|
2018-08-05 14:41:58 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2021-06-02 09:43:47 +00:00
|
|
|
assert freqtrade.exchange.get_buy_rate('ETH/BTC', True) == 0.043935
|
2019-02-14 06:27:13 +00:00
|
|
|
assert ticker_mock.call_count == 0
|
2018-08-05 14:41:58 +00:00
|
|
|
|
|
|
|
|
2020-05-26 18:14:05 +00:00
|
|
|
def test_order_book_bid_strategy_exception(mocker, default_conf, caplog) -> None:
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2019-02-14 06:27:13 +00:00
|
|
|
ticker_mock = MagicMock(return_value={'ask': 0.042, 'last': 0.046})
|
2018-08-25 11:21:10 +00:00
|
|
|
mocker.patch.multiple(
|
2018-10-04 16:07:47 +00:00
|
|
|
'freqtrade.exchange.Exchange',
|
2020-05-26 18:27:35 +00:00
|
|
|
fetch_l2_order_book=MagicMock(return_value={'bids': [[]], 'asks': [[]]}),
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker_mock,
|
2018-08-14 10:12:44 +00:00
|
|
|
|
2018-08-25 11:21:10 +00:00
|
|
|
)
|
2018-08-07 10:29:37 +00:00
|
|
|
default_conf['exchange']['name'] = 'binance'
|
2018-08-29 09:38:43 +00:00
|
|
|
default_conf['bid_strategy']['use_order_book'] = True
|
2020-05-26 18:14:05 +00:00
|
|
|
default_conf['bid_strategy']['order_book_top'] = 1
|
2018-08-07 10:29:37 +00:00
|
|
|
default_conf['bid_strategy']['ask_last_balance'] = 0
|
2018-08-05 14:41:58 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
2018-08-07 10:29:37 +00:00
|
|
|
|
2018-08-05 14:41:58 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
2019-09-11 20:32:08 +00:00
|
|
|
# orderbook shall be used even if tickers would be lower.
|
2020-05-26 18:35:11 +00:00
|
|
|
with pytest.raises(PricingError):
|
2021-06-02 09:43:47 +00:00
|
|
|
freqtrade.exchange.get_buy_rate('ETH/BTC', refresh=True)
|
2020-05-26 18:14:05 +00:00
|
|
|
assert log_has_re(r'Buy Price from orderbook could not be determined.', caplog)
|
2018-08-05 14:41:58 +00:00
|
|
|
|
|
|
|
|
2019-10-31 06:26:48 +00:00
|
|
|
def test_check_depth_of_market_buy(default_conf, mocker, order_book_l2) -> None:
|
2018-08-14 10:12:44 +00:00
|
|
|
"""
|
|
|
|
test check depth of market
|
|
|
|
"""
|
2018-09-10 18:19:28 +00:00
|
|
|
patch_exchange(mocker)
|
2018-08-25 11:21:10 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2020-05-26 18:27:35 +00:00
|
|
|
fetch_l2_order_book=order_book_l2
|
2018-08-25 11:21:10 +00:00
|
|
|
)
|
2018-08-05 14:41:58 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
default_conf['exchange']['name'] = 'binance'
|
2018-08-29 09:38:43 +00:00
|
|
|
default_conf['bid_strategy']['check_depth_of_market']['enabled'] = True
|
2018-08-07 10:29:37 +00:00
|
|
|
# delta is 100 which is impossible to reach. hence function will return false
|
2018-08-29 09:38:43 +00:00
|
|
|
default_conf['bid_strategy']['check_depth_of_market']['bids_to_ask_delta'] = 100
|
2018-08-05 14:41:58 +00:00
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
|
2018-08-29 09:38:43 +00:00
|
|
|
conf = default_conf['bid_strategy']['check_depth_of_market']
|
2018-08-07 10:29:37 +00:00
|
|
|
assert freqtrade._check_depth_of_market_buy('ETH/BTC', conf) is False
|
2018-08-05 14:41:58 +00:00
|
|
|
|
|
|
|
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_order_book_ask_strategy(default_conf, limit_buy_order_open, limit_buy_order, fee,
|
|
|
|
limit_sell_order_open, mocker, order_book_l2, caplog) -> None:
|
2018-08-14 10:12:44 +00:00
|
|
|
"""
|
|
|
|
test order book ask strategy
|
|
|
|
"""
|
2020-05-26 18:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2)
|
2018-08-05 14:41:58 +00:00
|
|
|
default_conf['exchange']['name'] = 'binance'
|
2018-08-29 09:38:43 +00:00
|
|
|
default_conf['ask_strategy']['use_order_book'] = True
|
|
|
|
default_conf['ask_strategy']['order_book_min'] = 1
|
|
|
|
default_conf['ask_strategy']['order_book_max'] = 2
|
2018-08-05 14:41:58 +00:00
|
|
|
default_conf['telegram']['enabled'] = False
|
|
|
|
patch_RPCManager(mocker)
|
2018-09-11 17:59:01 +00:00
|
|
|
patch_exchange(mocker)
|
2018-08-05 14:41:58 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=MagicMock(return_value={
|
2018-08-05 14:41:58 +00:00
|
|
|
'bid': 0.00001172,
|
|
|
|
'ask': 0.00001173,
|
|
|
|
'last': 0.00001172
|
|
|
|
}),
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
|
|
|
sell=MagicMock(return_value=limit_sell_order_open),
|
2018-08-05 14:41:58 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
freqtrade = FreqtradeBot(default_conf)
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2018-08-05 14:41:58 +00:00
|
|
|
|
|
|
|
trade = Trade.query.first()
|
|
|
|
assert trade
|
2018-08-05 14:56:14 +00:00
|
|
|
|
|
|
|
time.sleep(0.01) # Race condition fix
|
|
|
|
trade.update(limit_buy_order)
|
2019-12-18 19:16:53 +00:00
|
|
|
freqtrade.wallets.update()
|
2018-08-05 14:56:14 +00:00
|
|
|
assert trade.is_open is True
|
|
|
|
|
|
|
|
patch_get_signal(freqtrade, value=(False, True))
|
|
|
|
assert freqtrade.handle_trade(trade) is True
|
2020-05-26 18:24:44 +00:00
|
|
|
assert trade.close_rate_requested == order_book_l2.return_value['asks'][0][0]
|
|
|
|
|
2020-05-26 18:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book',
|
2020-05-26 18:24:44 +00:00
|
|
|
return_value={'bids': [[]], 'asks': [[]]})
|
2020-05-26 18:35:11 +00:00
|
|
|
with pytest.raises(PricingError):
|
2020-05-26 18:24:44 +00:00
|
|
|
freqtrade.handle_trade(trade)
|
|
|
|
assert log_has('Sell Price at location 1 from orderbook could not be determined.', caplog)
|
2018-08-29 17:32:44 +00:00
|
|
|
|
|
|
|
|
2019-05-20 17:35:48 +00:00
|
|
|
def test_startup_state(default_conf, mocker):
|
2018-12-04 19:23:03 +00:00
|
|
|
default_conf['pairlist'] = {'method': 'VolumePairList',
|
|
|
|
'config': {'number_assets': 20}
|
|
|
|
}
|
2018-12-02 21:12:12 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True))
|
2019-03-26 08:07:24 +00:00
|
|
|
worker = get_patched_worker(mocker, default_conf)
|
2020-01-29 14:08:36 +00:00
|
|
|
assert worker.freqtrade.state is State.RUNNING
|
2019-05-20 17:35:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_startup_trade_reinit(default_conf, edge_conf, mocker):
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True))
|
|
|
|
reinit_mock = MagicMock()
|
|
|
|
mocker.patch('freqtrade.persistence.Trade.stoploss_reinitialization', reinit_mock)
|
|
|
|
|
|
|
|
ftbot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
ftbot.startup()
|
|
|
|
assert reinit_mock.call_count == 1
|
|
|
|
|
|
|
|
reinit_mock.reset_mock()
|
|
|
|
|
|
|
|
ftbot = get_patched_freqtradebot(mocker, edge_conf)
|
|
|
|
ftbot.startup()
|
|
|
|
assert reinit_mock.call_count == 0
|
2019-10-25 13:00:16 +00:00
|
|
|
|
|
|
|
|
2019-12-15 09:26:56 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2020-08-13 11:33:45 +00:00
|
|
|
def test_sync_wallet_dry_run(mocker, default_conf, ticker, fee, limit_buy_order_open, caplog):
|
2019-12-15 09:26:56 +00:00
|
|
|
default_conf['dry_run'] = True
|
|
|
|
# Initialize to 2 times stake amount
|
|
|
|
default_conf['dry_run_wallet'] = 0.002
|
|
|
|
default_conf['max_open_trades'] = 2
|
2020-01-03 09:41:10 +00:00
|
|
|
default_conf['tradable_balance_ratio'] = 1.0
|
2019-12-15 09:26:56 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2020-08-13 11:33:45 +00:00
|
|
|
buy=MagicMock(return_value=limit_buy_order_open),
|
2019-12-15 09:26:56 +00:00
|
|
|
get_fee=fee,
|
|
|
|
)
|
|
|
|
|
|
|
|
bot = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
patch_get_signal(bot)
|
|
|
|
assert bot.wallets.get_free('BTC') == 0.002
|
|
|
|
|
2019-12-30 19:50:56 +00:00
|
|
|
n = bot.enter_positions()
|
|
|
|
assert n == 2
|
2019-12-15 09:26:56 +00:00
|
|
|
trades = Trade.query.all()
|
|
|
|
assert len(trades) == 2
|
|
|
|
|
|
|
|
bot.config['max_open_trades'] = 3
|
2019-12-30 19:50:56 +00:00
|
|
|
n = bot.enter_positions()
|
|
|
|
assert n == 0
|
2019-12-31 06:11:09 +00:00
|
|
|
assert log_has_re(r"Unable to create trade for XRP/BTC: "
|
2020-01-03 09:41:10 +00:00
|
|
|
r"Available balance \(0.0 BTC\) is lower than stake amount \(0.001 BTC\)",
|
2019-12-29 01:38:28 +00:00
|
|
|
caplog)
|
2020-05-16 10:40:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_cancel_all_open_orders(mocker, default_conf, fee, limit_buy_order, limit_sell_order):
|
|
|
|
default_conf['cancel_open_orders_on_exit'] = True
|
2020-06-28 14:27:35 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order',
|
2020-09-10 06:19:40 +00:00
|
|
|
side_effect=[
|
|
|
|
ExchangeError(), limit_sell_order, limit_buy_order, limit_sell_order])
|
2020-05-16 10:40:25 +00:00
|
|
|
buy_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_cancel_buy')
|
|
|
|
sell_mock = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.handle_cancel_sell')
|
|
|
|
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
create_mock_trades(fee)
|
|
|
|
trades = Trade.query.all()
|
2020-09-10 05:40:19 +00:00
|
|
|
assert len(trades) == MOCK_TRADE_COUNT
|
2020-05-16 10:40:25 +00:00
|
|
|
freqtrade.cancel_all_open_orders()
|
|
|
|
assert buy_mock.call_count == 1
|
2020-09-10 05:40:19 +00:00
|
|
|
assert sell_mock.call_count == 2
|
2020-06-30 05:16:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2020-09-06 17:32:00 +00:00
|
|
|
def test_check_for_open_trades(mocker, default_conf, fee):
|
2020-06-30 05:16:08 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
freqtrade.check_for_open_trades()
|
|
|
|
assert freqtrade.rpc.send_msg.call_count == 0
|
|
|
|
|
|
|
|
create_mock_trades(fee)
|
|
|
|
trade = Trade.query.first()
|
|
|
|
trade.is_open = True
|
|
|
|
|
|
|
|
freqtrade.check_for_open_trades()
|
|
|
|
assert freqtrade.rpc.send_msg.call_count == 1
|
|
|
|
assert 'Handle these trades manually' in freqtrade.rpc.send_msg.call_args[0][0]['status']
|
2020-09-06 17:32:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_update_open_orders(mocker, default_conf, fee, caplog):
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
create_mock_trades(fee)
|
|
|
|
|
|
|
|
freqtrade.update_open_orders()
|
2021-01-06 08:57:36 +00:00
|
|
|
assert not log_has_re(r"Error updating Order .*", caplog)
|
|
|
|
|
|
|
|
freqtrade.config['dry_run'] = False
|
|
|
|
freqtrade.update_open_orders()
|
|
|
|
|
2020-09-06 17:32:00 +00:00
|
|
|
assert log_has_re(r"Error updating Order .*", caplog)
|
|
|
|
caplog.clear()
|
|
|
|
|
2020-09-10 05:40:19 +00:00
|
|
|
assert len(Order.get_open_orders()) == 3
|
2020-09-07 04:49:30 +00:00
|
|
|
matching_buy_order = mock_order_4()
|
|
|
|
matching_buy_order.update({
|
2020-09-06 17:32:00 +00:00
|
|
|
'status': 'closed',
|
2020-09-07 04:49:30 +00:00
|
|
|
})
|
2020-09-06 17:32:00 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order', return_value=matching_buy_order)
|
|
|
|
freqtrade.update_open_orders()
|
2020-09-10 05:40:19 +00:00
|
|
|
# Only stoploss and sell orders are kept open
|
|
|
|
assert len(Order.get_open_orders()) == 2
|
2020-09-07 05:47:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_update_closed_trades_without_assigned_fees(mocker, default_conf, fee):
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
|
|
|
|
def patch_with_fee(order):
|
2020-12-12 10:43:47 +00:00
|
|
|
order.update({'fee': {'cost': 0.1, 'rate': 0.01,
|
2020-09-07 05:47:38 +00:00
|
|
|
'currency': order['symbol'].split('/')[0]}})
|
|
|
|
return order
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order',
|
|
|
|
side_effect=[
|
|
|
|
patch_with_fee(mock_order_2_sell()),
|
|
|
|
patch_with_fee(mock_order_3_sell()),
|
|
|
|
patch_with_fee(mock_order_1()),
|
|
|
|
patch_with_fee(mock_order_2()),
|
|
|
|
patch_with_fee(mock_order_3()),
|
|
|
|
patch_with_fee(mock_order_4()),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
create_mock_trades(fee)
|
|
|
|
trades = Trade.get_trades().all()
|
2020-09-10 05:40:19 +00:00
|
|
|
assert len(trades) == MOCK_TRADE_COUNT
|
2020-09-07 05:47:38 +00:00
|
|
|
for trade in trades:
|
|
|
|
assert trade.fee_open_cost is None
|
|
|
|
assert trade.fee_open_currency is None
|
|
|
|
assert trade.fee_close_cost is None
|
|
|
|
assert trade.fee_close_currency is None
|
|
|
|
|
|
|
|
freqtrade.update_closed_trades_without_assigned_fees()
|
|
|
|
|
2021-01-17 19:31:27 +00:00
|
|
|
# Does nothing for dry-run
|
|
|
|
trades = Trade.get_trades().all()
|
|
|
|
assert len(trades) == MOCK_TRADE_COUNT
|
|
|
|
for trade in trades:
|
|
|
|
assert trade.fee_open_cost is None
|
|
|
|
assert trade.fee_open_currency is None
|
|
|
|
assert trade.fee_close_cost is None
|
|
|
|
assert trade.fee_close_currency is None
|
|
|
|
|
|
|
|
freqtrade.config['dry_run'] = False
|
|
|
|
|
|
|
|
freqtrade.update_closed_trades_without_assigned_fees()
|
|
|
|
|
2020-09-09 05:01:43 +00:00
|
|
|
trades = Trade.get_trades().all()
|
2020-09-10 05:40:19 +00:00
|
|
|
assert len(trades) == MOCK_TRADE_COUNT
|
2020-09-07 05:47:38 +00:00
|
|
|
|
|
|
|
for trade in trades:
|
|
|
|
if trade.is_open:
|
|
|
|
# Exclude Trade 4 - as the order is still open.
|
2020-09-11 05:12:10 +00:00
|
|
|
if trade.select_order('buy', False):
|
2020-09-07 05:47:38 +00:00
|
|
|
assert trade.fee_open_cost is not None
|
|
|
|
assert trade.fee_open_currency is not None
|
|
|
|
else:
|
|
|
|
assert trade.fee_open_cost is None
|
|
|
|
assert trade.fee_open_currency is None
|
|
|
|
|
|
|
|
else:
|
|
|
|
assert trade.fee_close_cost is not None
|
|
|
|
assert trade.fee_close_currency is not None
|
2020-09-09 04:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_reupdate_buy_order_fees(mocker, default_conf, fee, caplog):
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
mock_uts = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.update_trade_state')
|
|
|
|
|
|
|
|
create_mock_trades(fee)
|
|
|
|
trades = Trade.get_trades().all()
|
|
|
|
|
|
|
|
freqtrade.reupdate_buy_order_fees(trades[0])
|
|
|
|
assert log_has_re(r"Trying to reupdate buy fees for .*", caplog)
|
|
|
|
assert mock_uts.call_count == 1
|
|
|
|
assert mock_uts.call_args_list[0][0][0] == trades[0]
|
2020-09-09 04:46:38 +00:00
|
|
|
assert mock_uts.call_args_list[0][0][1] == mock_order_1()['id']
|
2020-09-09 04:40:40 +00:00
|
|
|
assert log_has_re(r"Updating buy-fee on trade .* for order .*\.", caplog)
|
|
|
|
mock_uts.reset_mock()
|
|
|
|
caplog.clear()
|
|
|
|
|
|
|
|
# Test with trade without orders
|
|
|
|
trade = Trade(
|
|
|
|
pair='XRP/ETH',
|
|
|
|
stake_amount=0.001,
|
|
|
|
fee_open=fee.return_value,
|
|
|
|
fee_close=fee.return_value,
|
|
|
|
open_date=arrow.utcnow().datetime,
|
|
|
|
is_open=True,
|
|
|
|
amount=20,
|
|
|
|
open_rate=0.01,
|
2021-04-20 10:54:22 +00:00
|
|
|
exchange='binance',
|
2020-09-09 04:40:40 +00:00
|
|
|
)
|
2021-04-05 05:38:07 +00:00
|
|
|
Trade.query.session.add(trade)
|
2020-09-09 04:40:40 +00:00
|
|
|
|
|
|
|
freqtrade.reupdate_buy_order_fees(trade)
|
|
|
|
assert log_has_re(r"Trying to reupdate buy fees for .*", caplog)
|
|
|
|
assert mock_uts.call_count == 0
|
|
|
|
assert not log_has_re(r"Updating buy-fee on trade .* for order .*\.", caplog)
|
2020-09-09 04:46:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_handle_insufficient_funds(mocker, default_conf, fee):
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
mock_rlo = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.refind_lost_order')
|
|
|
|
mock_bof = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.reupdate_buy_order_fees')
|
|
|
|
create_mock_trades(fee)
|
|
|
|
trades = Trade.get_trades().all()
|
|
|
|
|
|
|
|
# Trade 0 has only a open buy order, no closed order
|
|
|
|
freqtrade.handle_insufficient_funds(trades[0])
|
|
|
|
assert mock_rlo.call_count == 0
|
|
|
|
assert mock_bof.call_count == 1
|
|
|
|
|
|
|
|
mock_rlo.reset_mock()
|
|
|
|
mock_bof.reset_mock()
|
|
|
|
|
|
|
|
# Trade 1 has closed buy and sell orders
|
|
|
|
freqtrade.handle_insufficient_funds(trades[1])
|
|
|
|
assert mock_rlo.call_count == 1
|
|
|
|
assert mock_bof.call_count == 0
|
|
|
|
|
|
|
|
mock_rlo.reset_mock()
|
|
|
|
mock_bof.reset_mock()
|
|
|
|
|
|
|
|
# Trade 2 has closed buy and sell orders
|
|
|
|
freqtrade.handle_insufficient_funds(trades[2])
|
|
|
|
assert mock_rlo.call_count == 1
|
|
|
|
assert mock_bof.call_count == 0
|
|
|
|
|
|
|
|
mock_rlo.reset_mock()
|
|
|
|
mock_bof.reset_mock()
|
|
|
|
|
|
|
|
# Trade 3 has an opne buy order
|
|
|
|
freqtrade.handle_insufficient_funds(trades[3])
|
|
|
|
assert mock_rlo.call_count == 0
|
|
|
|
assert mock_bof.call_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
|
|
|
def test_refind_lost_order(mocker, default_conf, fee, caplog):
|
2020-09-19 07:46:32 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2020-09-09 04:46:38 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2020-09-09 05:50:52 +00:00
|
|
|
mock_uts = mocker.patch('freqtrade.freqtradebot.FreqtradeBot.update_trade_state')
|
2020-09-09 04:46:38 +00:00
|
|
|
|
2020-09-10 06:03:26 +00:00
|
|
|
mock_fo = mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order',
|
|
|
|
return_value={'status': 'open'})
|
|
|
|
|
|
|
|
def reset_open_orders(trade):
|
|
|
|
trade.open_order_id = None
|
|
|
|
trade.stoploss_order_id = None
|
2020-09-09 04:46:38 +00:00
|
|
|
|
|
|
|
create_mock_trades(fee)
|
|
|
|
trades = Trade.get_trades().all()
|
|
|
|
|
2020-09-09 05:50:52 +00:00
|
|
|
caplog.clear()
|
|
|
|
|
|
|
|
# No open order
|
2020-09-10 06:03:26 +00:00
|
|
|
trade = trades[0]
|
|
|
|
reset_open_orders(trade)
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
|
|
|
|
freqtrade.refind_lost_order(trade)
|
2020-09-09 05:50:52 +00:00
|
|
|
order = mock_order_1()
|
|
|
|
assert log_has_re(r"Order Order(.*order_id=" + order['id'] + ".*) is no longer open.", caplog)
|
|
|
|
assert mock_fo.call_count == 0
|
|
|
|
assert mock_uts.call_count == 0
|
2020-09-10 06:03:26 +00:00
|
|
|
# No change to orderid - as update_trade_state is mocked
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.stoploss_order_id is None
|
2020-09-09 04:46:38 +00:00
|
|
|
|
2020-09-09 05:50:52 +00:00
|
|
|
caplog.clear()
|
|
|
|
mock_fo.reset_mock()
|
|
|
|
|
|
|
|
# Open buy order
|
2020-09-10 06:03:26 +00:00
|
|
|
trade = trades[3]
|
|
|
|
reset_open_orders(trade)
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
|
|
|
|
freqtrade.refind_lost_order(trade)
|
2020-09-09 05:50:52 +00:00
|
|
|
order = mock_order_4()
|
|
|
|
assert log_has_re(r"Trying to refind Order\(.*", caplog)
|
|
|
|
assert mock_fo.call_count == 0
|
|
|
|
assert mock_uts.call_count == 0
|
2020-09-10 06:03:26 +00:00
|
|
|
# No change to orderid - as update_trade_state is mocked
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.stoploss_order_id is None
|
2020-09-09 05:50:52 +00:00
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
mock_fo.reset_mock()
|
|
|
|
|
|
|
|
# Open stoploss order
|
2020-09-10 06:03:26 +00:00
|
|
|
trade = trades[4]
|
|
|
|
reset_open_orders(trade)
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
|
|
|
|
freqtrade.refind_lost_order(trade)
|
2020-09-09 05:50:52 +00:00
|
|
|
order = mock_order_5_stoploss()
|
|
|
|
assert log_has_re(r"Trying to refind Order\(.*", caplog)
|
|
|
|
assert mock_fo.call_count == 1
|
|
|
|
assert mock_uts.call_count == 1
|
2020-09-10 06:03:26 +00:00
|
|
|
# stoploss_order_id is "refound" and added to the trade
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.stoploss_order_id is not None
|
|
|
|
|
|
|
|
caplog.clear()
|
|
|
|
mock_fo.reset_mock()
|
|
|
|
mock_uts.reset_mock()
|
|
|
|
|
|
|
|
# Open sell order
|
|
|
|
trade = trades[5]
|
|
|
|
reset_open_orders(trade)
|
|
|
|
assert trade.open_order_id is None
|
|
|
|
assert trade.stoploss_order_id is None
|
|
|
|
|
|
|
|
freqtrade.refind_lost_order(trade)
|
|
|
|
order = mock_order_6_sell()
|
|
|
|
assert log_has_re(r"Trying to refind Order\(.*", caplog)
|
|
|
|
assert mock_fo.call_count == 1
|
|
|
|
assert mock_uts.call_count == 1
|
|
|
|
# sell-orderid is "refound" and added to the trade
|
|
|
|
assert trade.open_order_id == order['id']
|
|
|
|
assert trade.stoploss_order_id is None
|
2020-09-09 05:50:52 +00:00
|
|
|
|
2020-09-09 05:57:02 +00:00
|
|
|
caplog.clear()
|
|
|
|
|
2020-09-09 05:50:52 +00:00
|
|
|
# Test error case
|
|
|
|
mock_fo = mocker.patch('freqtrade.exchange.Exchange.fetch_order_or_stoploss_order',
|
|
|
|
side_effect=ExchangeError())
|
2020-09-10 06:03:26 +00:00
|
|
|
order = mock_order_5_stoploss()
|
|
|
|
|
2020-09-09 05:50:52 +00:00
|
|
|
freqtrade.refind_lost_order(trades[4])
|
|
|
|
assert log_has(f"Error updating {order['id']}.", caplog)
|