2019-10-31 06:15:45 +00:00
|
|
|
from unittest.mock import MagicMock
|
2019-10-31 06:08:02 +00:00
|
|
|
|
2020-01-02 12:16:18 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-06-08 19:06:47 +00:00
|
|
|
from freqtrade.enums import SellType
|
2019-10-31 06:08:02 +00:00
|
|
|
from freqtrade.persistence import Trade
|
2020-01-02 12:16:18 +00:00
|
|
|
from freqtrade.rpc.rpc import RPC
|
2021-06-08 19:06:47 +00:00
|
|
|
from freqtrade.strategy.interface import SellCheckTuple
|
2019-10-31 06:15:45 +00:00
|
|
|
from tests.conftest import get_patched_freqtradebot, patch_get_signal
|
2019-10-31 06:08:02 +00:00
|
|
|
|
|
|
|
|
2021-08-26 04:53:42 +00:00
|
|
|
def test_may_execute_exit_stoploss_on_exchange_multi(default_conf, ticker, fee,
|
2019-10-31 06:26:48 +00:00
|
|
|
limit_buy_order, mocker) -> None:
|
2019-10-31 06:08:02 +00:00
|
|
|
"""
|
|
|
|
Tests workflow of selling stoploss_on_exchange.
|
|
|
|
Sells
|
|
|
|
* first trade as stoploss
|
|
|
|
* 2nd trade is kept
|
|
|
|
* 3rd trade is sold via sell-signal
|
|
|
|
"""
|
|
|
|
default_conf['max_open_trades'] = 3
|
|
|
|
default_conf['exchange']['name'] = 'binance'
|
|
|
|
|
2020-01-19 13:39:51 +00:00
|
|
|
stoploss = {
|
2019-10-31 06:08:02 +00:00
|
|
|
'id': 123,
|
|
|
|
'info': {}
|
|
|
|
}
|
|
|
|
stoploss_order_open = {
|
|
|
|
"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": 0.0,
|
|
|
|
"average": 0.0,
|
|
|
|
"filled": 0.0,
|
|
|
|
"remaining": 0.0,
|
|
|
|
"status": "open",
|
|
|
|
"fee": None,
|
|
|
|
"trades": None
|
|
|
|
}
|
|
|
|
stoploss_order_closed = stoploss_order_open.copy()
|
|
|
|
stoploss_order_closed['status'] = 'closed'
|
2020-05-13 18:25:32 +00:00
|
|
|
stoploss_order_closed['filled'] = stoploss_order_closed['amount']
|
|
|
|
|
2019-10-31 06:08:02 +00:00
|
|
|
# Sell first trade based on stoploss, keep 2nd and 3rd trade open
|
|
|
|
stoploss_order_mock = MagicMock(
|
|
|
|
side_effect=[stoploss_order_closed, stoploss_order_open, stoploss_order_open])
|
|
|
|
# Sell 3rd trade (not called for the first trade)
|
|
|
|
should_sell_mock = MagicMock(side_effect=[
|
2021-04-22 06:21:19 +00:00
|
|
|
SellCheckTuple(sell_type=SellType.NONE),
|
|
|
|
SellCheckTuple(sell_type=SellType.SELL_SIGNAL)]
|
2019-10-31 06:08:02 +00:00
|
|
|
)
|
|
|
|
cancel_order_mock = MagicMock()
|
2020-01-19 13:39:51 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Binance.stoploss', stoploss)
|
2019-10-31 06:08:02 +00:00
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2019-10-31 06:08:02 +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-06-28 14:30:24 +00:00
|
|
|
fetch_stoploss_order=stoploss_order_mock,
|
2021-05-16 12:15:24 +00:00
|
|
|
cancel_stoploss_order_with_result=cancel_order_mock,
|
2019-10-31 06:08:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.freqtradebot.FreqtradeBot',
|
|
|
|
create_stoploss_order=MagicMock(return_value=True),
|
2021-09-08 06:45:55 +00:00
|
|
|
_notify_exit=MagicMock(),
|
2019-10-31 06:08:02 +00:00
|
|
|
)
|
|
|
|
mocker.patch("freqtrade.strategy.interface.IStrategy.should_sell", should_sell_mock)
|
2019-10-31 06:11:57 +00:00
|
|
|
wallets_mock = mocker.patch("freqtrade.wallets.Wallets.update", MagicMock())
|
2019-12-18 19:16:53 +00:00
|
|
|
mocker.patch("freqtrade.wallets.Wallets.get_free", MagicMock(return_value=1000))
|
2019-10-31 06:08:02 +00:00
|
|
|
|
2019-10-31 06:11:57 +00:00
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
2019-10-31 06:08:02 +00:00
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
|
|
|
# Switch ordertype to market to close trade immediately
|
|
|
|
freqtrade.strategy.order_types['sell'] = 'market'
|
2020-06-14 08:20:23 +00:00
|
|
|
freqtrade.strategy.confirm_trade_entry = MagicMock(return_value=True)
|
|
|
|
freqtrade.strategy.confirm_trade_exit = MagicMock(return_value=True)
|
2019-10-31 06:08:02 +00:00
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
# Create some test data
|
2019-12-30 19:50:56 +00:00
|
|
|
freqtrade.enter_positions()
|
2020-06-14 08:20:23 +00:00
|
|
|
assert freqtrade.strategy.confirm_trade_entry.call_count == 3
|
|
|
|
freqtrade.strategy.confirm_trade_entry.reset_mock()
|
|
|
|
assert freqtrade.strategy.confirm_trade_exit.call_count == 0
|
2019-10-31 06:08:02 +00:00
|
|
|
wallets_mock.reset_mock()
|
|
|
|
|
|
|
|
trades = Trade.query.all()
|
|
|
|
# Make sure stoploss-order is open and trade is bought (since we mock update_trade_state)
|
|
|
|
for trade in trades:
|
|
|
|
trade.stoploss_order_id = 3
|
|
|
|
trade.open_order_id = None
|
|
|
|
|
2019-12-30 19:08:36 +00:00
|
|
|
n = freqtrade.exit_positions(trades)
|
|
|
|
assert n == 2
|
2019-10-31 06:08:02 +00:00
|
|
|
assert should_sell_mock.call_count == 2
|
2020-06-14 08:20:23 +00:00
|
|
|
assert freqtrade.strategy.confirm_trade_entry.call_count == 0
|
|
|
|
assert freqtrade.strategy.confirm_trade_exit.call_count == 1
|
|
|
|
freqtrade.strategy.confirm_trade_exit.reset_mock()
|
2019-10-31 06:08:02 +00:00
|
|
|
|
|
|
|
# Only order for 3rd trade needs to be cancelled
|
|
|
|
assert cancel_order_mock.call_count == 1
|
2020-05-01 13:35:57 +00:00
|
|
|
# Wallets must be updated between stoploss cancellation and selling, and will be updated again
|
2020-05-10 07:55:41 +00:00
|
|
|
# during update_trade_state
|
2020-05-13 18:25:32 +00:00
|
|
|
assert wallets_mock.call_count == 4
|
2019-10-31 06:08:02 +00:00
|
|
|
|
|
|
|
trade = trades[0]
|
|
|
|
assert trade.sell_reason == SellType.STOPLOSS_ON_EXCHANGE.value
|
|
|
|
assert not trade.is_open
|
|
|
|
|
|
|
|
trade = trades[1]
|
|
|
|
assert not trade.sell_reason
|
|
|
|
assert trade.is_open
|
|
|
|
|
|
|
|
trade = trades[2]
|
|
|
|
assert trade.sell_reason == SellType.SELL_SIGNAL.value
|
|
|
|
assert not trade.is_open
|
2019-10-31 09:04:28 +00:00
|
|
|
|
|
|
|
|
2020-01-02 12:16:18 +00:00
|
|
|
@pytest.mark.parametrize("balance_ratio,result1", [
|
|
|
|
(1, 200),
|
|
|
|
(0.99, 198),
|
|
|
|
])
|
2022-01-02 19:20:56 +00:00
|
|
|
def test_forcebuy_last_unlimited(default_conf, ticker, fee, mocker, balance_ratio, result1) -> None:
|
2019-10-31 09:04:28 +00:00
|
|
|
"""
|
2020-01-02 12:45:03 +00:00
|
|
|
Tests workflow unlimited stake-amount
|
|
|
|
Buy 4 trades, forcebuy a 5th trade
|
|
|
|
Sell one trade, calculated stake amount should now be lower than before since
|
|
|
|
one trade was sold at a loss.
|
2019-10-31 09:04:28 +00:00
|
|
|
"""
|
|
|
|
default_conf['max_open_trades'] = 5
|
|
|
|
default_conf['forcebuy_enable'] = True
|
|
|
|
default_conf['stake_amount'] = 'unlimited'
|
2020-01-02 12:16:18 +00:00
|
|
|
default_conf['tradable_balance_ratio'] = balance_ratio
|
2019-12-24 05:47:25 +00:00
|
|
|
default_conf['dry_run_wallet'] = 1000
|
2019-10-31 09:04:28 +00:00
|
|
|
default_conf['exchange']['name'] = 'binance'
|
|
|
|
default_conf['telegram']['enabled'] = True
|
|
|
|
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
2019-12-18 15:34:30 +00:00
|
|
|
fetch_ticker=ticker,
|
2019-10-31 09:04:28 +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,
|
2019-10-31 09:04:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.freqtradebot.FreqtradeBot',
|
|
|
|
create_stoploss_order=MagicMock(return_value=True),
|
2021-09-08 06:45:55 +00:00
|
|
|
_notify_exit=MagicMock(),
|
2019-10-31 09:04:28 +00:00
|
|
|
)
|
2019-12-24 05:47:25 +00:00
|
|
|
should_sell_mock = MagicMock(side_effect=[
|
2021-04-22 06:21:19 +00:00
|
|
|
SellCheckTuple(sell_type=SellType.NONE),
|
|
|
|
SellCheckTuple(sell_type=SellType.SELL_SIGNAL),
|
|
|
|
SellCheckTuple(sell_type=SellType.NONE),
|
|
|
|
SellCheckTuple(sell_type=SellType.NONE),
|
|
|
|
SellCheckTuple(sell_type=SellType.NONE)]
|
2019-12-24 05:47:25 +00:00
|
|
|
)
|
|
|
|
mocker.patch("freqtrade.strategy.interface.IStrategy.should_sell", should_sell_mock)
|
2019-10-31 09:04:28 +00:00
|
|
|
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf)
|
|
|
|
rpc = RPC(freqtrade)
|
|
|
|
freqtrade.strategy.order_types['stoploss_on_exchange'] = True
|
|
|
|
# Switch ordertype to market to close trade immediately
|
|
|
|
freqtrade.strategy.order_types['sell'] = 'market'
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
|
|
|
|
# Create 4 trades
|
2019-12-30 19:50:56 +00:00
|
|
|
n = freqtrade.enter_positions()
|
|
|
|
assert n == 4
|
2019-10-31 09:04:28 +00:00
|
|
|
|
|
|
|
trades = Trade.query.all()
|
|
|
|
assert len(trades) == 4
|
2021-04-21 18:01:10 +00:00
|
|
|
assert freqtrade.wallets.get_trade_stake_amount('XRP/BTC') == result1
|
2020-01-02 12:45:03 +00:00
|
|
|
|
2019-10-31 09:04:28 +00:00
|
|
|
rpc._rpc_forcebuy('TKN/BTC', None)
|
|
|
|
|
|
|
|
trades = Trade.query.all()
|
|
|
|
assert len(trades) == 5
|
|
|
|
|
|
|
|
for trade in trades:
|
2020-01-02 12:16:18 +00:00
|
|
|
assert trade.stake_amount == result1
|
2019-12-24 05:47:25 +00:00
|
|
|
# Reset trade open order id's
|
|
|
|
trade.open_order_id = None
|
|
|
|
trades = Trade.get_open_trades()
|
|
|
|
assert len(trades) == 5
|
|
|
|
bals = freqtrade.wallets.get_all_balances()
|
|
|
|
|
2019-12-30 19:08:36 +00:00
|
|
|
n = freqtrade.exit_positions(trades)
|
|
|
|
assert n == 1
|
2019-12-24 05:47:25 +00:00
|
|
|
trades = Trade.get_open_trades()
|
|
|
|
# One trade sold
|
|
|
|
assert len(trades) == 4
|
2020-01-02 12:45:03 +00:00
|
|
|
# stake-amount should now be reduced, since one trade was sold at a loss.
|
2021-04-21 18:01:10 +00:00
|
|
|
assert freqtrade.wallets.get_trade_stake_amount('XRP/BTC') < result1
|
2019-12-24 05:47:25 +00:00
|
|
|
# Validate that balance of sold trade is not in dry-run balances anymore.
|
|
|
|
bals2 = freqtrade.wallets.get_all_balances()
|
|
|
|
assert bals != bals2
|
|
|
|
assert len(bals) == 6
|
|
|
|
assert len(bals2) == 5
|
|
|
|
assert 'LTC' in bals
|
|
|
|
assert 'LTC' not in bals2
|
2022-01-02 19:20:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_dca_buying(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
|
|
|
|
default_conf_usdt['position_adjustment_enable'] = True
|
|
|
|
|
|
|
|
freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt)
|
|
|
|
mocker.patch.multiple(
|
|
|
|
'freqtrade.exchange.Exchange',
|
|
|
|
fetch_ticker=ticker_usdt,
|
|
|
|
get_fee=fee,
|
|
|
|
amount_to_precision=lambda s, x, y: y,
|
|
|
|
price_to_precision=lambda s, x, y: y,
|
|
|
|
)
|
|
|
|
|
|
|
|
patch_get_signal(freqtrade)
|
|
|
|
freqtrade.enter_positions()
|
|
|
|
|
|
|
|
assert len(Trade.get_trades().all()) == 1
|
|
|
|
trade = Trade.get_trades().first()
|
|
|
|
assert len(trade.orders) == 1
|
|
|
|
assert trade.stake_amount == 60
|
|
|
|
assert trade.open_rate == 2.0
|
|
|
|
# No adjustment
|
|
|
|
freqtrade.process()
|
|
|
|
trade = Trade.get_trades().first()
|
|
|
|
assert len(trade.orders) == 1
|
|
|
|
assert trade.stake_amount == 60
|
|
|
|
|
|
|
|
# Reduce bid amount
|
|
|
|
ticker_usdt_modif = ticker_usdt.return_value
|
|
|
|
ticker_usdt_modif['bid'] = ticker_usdt_modif['bid'] * 0.995
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', return_value=ticker_usdt_modif)
|
|
|
|
|
|
|
|
# additional buy order
|
|
|
|
freqtrade.process()
|
|
|
|
trade = Trade.get_trades().first()
|
|
|
|
assert len(trade.orders) == 2
|
|
|
|
assert trade.stake_amount == 120
|
|
|
|
|
|
|
|
# Open-rate averaged between 2.0 and 2.0 * 0.995
|
|
|
|
assert trade.open_rate < 2.0
|
|
|
|
assert trade.open_rate > 2.0 * 0.995
|
|
|
|
|
|
|
|
# No action - profit raised above 1% (the bar set in the strategy).
|
|
|
|
freqtrade.process()
|
|
|
|
trade = Trade.get_trades().first()
|
|
|
|
assert len(trade.orders) == 2
|
|
|
|
assert trade.stake_amount == 120
|
|
|
|
assert trade.orders[0].amount == 30
|
|
|
|
assert trade.orders[1].amount == 60 / ticker_usdt_modif['bid']
|
|
|
|
|
|
|
|
assert trade.amount == trade.orders[0].amount + trade.orders[1].amount
|
|
|
|
|
|
|
|
# Sell
|
|
|
|
patch_get_signal(freqtrade, value=(False, True, None, None))
|
|
|
|
freqtrade.process()
|
|
|
|
trade = Trade.get_trades().first()
|
|
|
|
assert trade.is_open is False
|
|
|
|
assert trade.orders[0].amount == 30
|
|
|
|
assert trade.orders[0].side == 'buy'
|
|
|
|
assert trade.orders[1].amount == 60 / ticker_usdt_modif['bid']
|
|
|
|
# Sold everything
|
|
|
|
assert trade.orders[-1].side == 'sell'
|
|
|
|
assert trade.orders[2].amount == trade.amount
|