2019-08-25 08:08:06 +00:00
|
|
|
from random import randint
|
2021-09-06 04:27:14 +00:00
|
|
|
from unittest.mock import MagicMock, PropertyMock
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
import ccxt
|
|
|
|
import pytest
|
|
|
|
|
2020-09-28 17:43:15 +00:00
|
|
|
from freqtrade.exceptions import DependencyException, InvalidOrderException, OperationalException
|
2019-09-08 07:54:15 +00:00
|
|
|
from tests.conftest import get_patched_exchange
|
2020-06-28 09:56:29 +00:00
|
|
|
from tests.exchange.test_exchange import ccxt_exceptionhandlers
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
|
2021-09-10 19:39:42 +00:00
|
|
|
@pytest.mark.parametrize('limitratio,expected,side', [
|
|
|
|
(None, 220 * 0.99, "sell"),
|
|
|
|
(0.99, 220 * 0.99, "sell"),
|
|
|
|
(0.98, 220 * 0.98, "sell"),
|
|
|
|
(None, 220 * 1.01, "buy"),
|
|
|
|
(0.99, 220 * 1.01, "buy"),
|
|
|
|
(0.98, 220 * 1.02, "buy"),
|
2020-05-15 06:09:53 +00:00
|
|
|
])
|
2021-09-09 20:24:07 +00:00
|
|
|
def test_stoploss_order_binance(
|
|
|
|
default_conf,
|
|
|
|
mocker,
|
|
|
|
limitratio,
|
|
|
|
expected,
|
|
|
|
side
|
|
|
|
):
|
2019-08-25 08:08:06 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
|
|
|
order_type = 'stop_loss_limit'
|
|
|
|
|
|
|
|
api_mock.create_order = MagicMock(return_value={
|
|
|
|
'id': order_id,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
default_conf['dry_run'] = False
|
2020-01-12 13:55:05 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
2021-09-09 20:24:07 +00:00
|
|
|
order = exchange.stoploss(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
amount=1,
|
|
|
|
stop_price=190,
|
|
|
|
side=side,
|
2021-09-10 19:39:42 +00:00
|
|
|
order_types={'stoploss_on_exchange_limit_ratio': 1.05}
|
2021-09-09 20:24:07 +00:00
|
|
|
)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
api_mock.create_order.reset_mock()
|
2020-05-15 06:09:53 +00:00
|
|
|
order_types = {} if limitratio is None else {'stoploss_on_exchange_limit_ratio': limitratio}
|
2021-07-26 06:01:57 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220,
|
2021-09-09 20:24:07 +00:00
|
|
|
order_types=order_types, side=side)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
2020-01-19 13:07:59 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['type'] == order_type
|
2021-09-09 20:24:07 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['side'] == side
|
2020-01-19 13:07:59 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
2020-05-15 06:04:14 +00:00
|
|
|
# Price should be 1% below stopprice
|
2020-05-15 06:09:53 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['price'] == expected
|
2020-01-19 13:07:59 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['params'] == {'stopPrice': 220}
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
# test exception handling
|
|
|
|
with pytest.raises(DependencyException):
|
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance"))
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
2021-09-09 20:24:07 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side=side)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
2019-09-01 07:08:35 +00:00
|
|
|
with pytest.raises(InvalidOrderException):
|
|
|
|
api_mock.create_order = MagicMock(
|
|
|
|
side_effect=ccxt.InvalidOrder("binance Order would trigger immediately."))
|
2019-08-25 08:08:06 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
2021-09-09 20:24:07 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side=side)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
2020-06-28 09:56:29 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, "binance",
|
|
|
|
"stoploss", "create_order", retries=1,
|
2021-09-09 20:24:07 +00:00
|
|
|
pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side=side)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
|
2020-01-19 13:07:59 +00:00
|
|
|
def test_stoploss_order_dry_run_binance(default_conf, mocker):
|
2019-08-25 08:08:06 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
order_type = 'stop_loss_limit'
|
|
|
|
default_conf['dry_run'] = True
|
2020-01-12 13:55:05 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
2021-07-26 06:01:57 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190, side="sell",
|
2020-01-19 12:30:56 +00:00
|
|
|
order_types={'stoploss_on_exchange_limit_ratio': 1.05})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
api_mock.create_order.reset_mock()
|
|
|
|
|
2021-07-26 06:01:57 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side="sell")
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert 'type' in order
|
|
|
|
|
|
|
|
assert order['type'] == order_type
|
|
|
|
assert order['price'] == 220
|
|
|
|
assert order['amount'] == 1
|
2020-01-19 18:54:30 +00:00
|
|
|
|
|
|
|
|
2021-09-09 20:24:07 +00:00
|
|
|
@pytest.mark.parametrize('sl1,sl2,sl3,side', [
|
|
|
|
(1501, 1499, 1501, "sell"),
|
|
|
|
(1499, 1501, 1499, "buy")
|
|
|
|
])
|
|
|
|
def test_stoploss_adjust_binance(mocker, default_conf, sl1, sl2, sl3, side):
|
2020-01-19 18:54:30 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id='binance')
|
|
|
|
order = {
|
|
|
|
'type': 'stop_loss_limit',
|
|
|
|
'price': 1500,
|
|
|
|
'info': {'stopPrice': 1500},
|
|
|
|
}
|
2021-09-09 20:24:07 +00:00
|
|
|
assert exchange.stoploss_adjust(sl1, order, side=side)
|
|
|
|
assert not exchange.stoploss_adjust(sl2, order, side=side)
|
2020-01-19 18:54:30 +00:00
|
|
|
# Test with invalid order case
|
|
|
|
order['type'] = 'stop_loss'
|
2021-09-09 20:24:07 +00:00
|
|
|
assert not exchange.stoploss_adjust(sl3, order, side=side)
|
2021-08-20 08:40:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('pair,nominal_value,max_lev', [
|
|
|
|
("BNB/BUSD", 0.0, 40.0),
|
|
|
|
("BNB/USDT", 100.0, 153.84615384615384),
|
|
|
|
("BTC/USDT", 170.30, 250.0),
|
|
|
|
("BNB/BUSD", 999999.9, 10.0),
|
|
|
|
("BNB/USDT", 5000000.0, 6.666666666666667),
|
|
|
|
("BTC/USDT", 300000000.1, 2.0),
|
|
|
|
])
|
2021-09-04 01:25:16 +00:00
|
|
|
def test_get_max_leverage_binance(default_conf, mocker, pair, nominal_value, max_lev):
|
2021-08-20 08:40:22 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id="binance")
|
|
|
|
exchange._leverage_brackets = {
|
|
|
|
'BNB/BUSD': [[0.0, 0.025],
|
|
|
|
[100000.0, 0.05],
|
|
|
|
[500000.0, 0.1],
|
|
|
|
[1000000.0, 0.15],
|
|
|
|
[2000000.0, 0.25],
|
|
|
|
[5000000.0, 0.5]],
|
|
|
|
'BNB/USDT': [[0.0, 0.0065],
|
|
|
|
[10000.0, 0.01],
|
|
|
|
[50000.0, 0.02],
|
|
|
|
[250000.0, 0.05],
|
|
|
|
[1000000.0, 0.1],
|
|
|
|
[2000000.0, 0.125],
|
|
|
|
[5000000.0, 0.15],
|
|
|
|
[10000000.0, 0.25]],
|
|
|
|
'BTC/USDT': [[0.0, 0.004],
|
|
|
|
[50000.0, 0.005],
|
|
|
|
[250000.0, 0.01],
|
|
|
|
[1000000.0, 0.025],
|
|
|
|
[5000000.0, 0.05],
|
|
|
|
[20000000.0, 0.1],
|
|
|
|
[50000000.0, 0.125],
|
|
|
|
[100000000.0, 0.15],
|
|
|
|
[200000000.0, 0.25],
|
|
|
|
[300000000.0, 0.5]],
|
|
|
|
}
|
|
|
|
assert exchange.get_max_leverage(pair, nominal_value) == max_lev
|
2021-09-04 02:30:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_fill_leverage_brackets_binance(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
2021-09-05 01:16:17 +00:00
|
|
|
api_mock.load_leverage_brackets = MagicMock(return_value={
|
2021-09-06 04:27:14 +00:00
|
|
|
'ADA/BUSD': [[0.0, 0.025],
|
|
|
|
[100000.0, 0.05],
|
|
|
|
[500000.0, 0.1],
|
|
|
|
[1000000.0, 0.15],
|
|
|
|
[2000000.0, 0.25],
|
|
|
|
[5000000.0, 0.5]],
|
|
|
|
'BTC/USDT': [[0.0, 0.004],
|
|
|
|
[50000.0, 0.005],
|
|
|
|
[250000.0, 0.01],
|
|
|
|
[1000000.0, 0.025],
|
|
|
|
[5000000.0, 0.05],
|
|
|
|
[20000000.0, 0.1],
|
|
|
|
[50000000.0, 0.125],
|
|
|
|
[100000000.0, 0.15],
|
|
|
|
[200000000.0, 0.25],
|
|
|
|
[300000000.0, 0.5]],
|
|
|
|
"ZEC/USDT": [[0.0, 0.01],
|
|
|
|
[5000.0, 0.025],
|
|
|
|
[25000.0, 0.05],
|
|
|
|
[100000.0, 0.1],
|
|
|
|
[250000.0, 0.125],
|
|
|
|
[1000000.0, 0.5]],
|
2021-09-04 02:30:52 +00:00
|
|
|
|
2021-09-05 01:16:17 +00:00
|
|
|
})
|
2021-09-04 02:30:52 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
|
2021-09-06 04:27:14 +00:00
|
|
|
exchange.fill_leverage_brackets()
|
2021-09-04 02:30:52 +00:00
|
|
|
|
|
|
|
assert exchange._leverage_brackets == {
|
2021-09-06 04:27:14 +00:00
|
|
|
'ADA/BUSD': [[0.0, 0.025],
|
|
|
|
[100000.0, 0.05],
|
|
|
|
[500000.0, 0.1],
|
|
|
|
[1000000.0, 0.15],
|
|
|
|
[2000000.0, 0.25],
|
|
|
|
[5000000.0, 0.5]],
|
|
|
|
'BTC/USDT': [[0.0, 0.004],
|
|
|
|
[50000.0, 0.005],
|
|
|
|
[250000.0, 0.01],
|
|
|
|
[1000000.0, 0.025],
|
|
|
|
[5000000.0, 0.05],
|
|
|
|
[20000000.0, 0.1],
|
|
|
|
[50000000.0, 0.125],
|
|
|
|
[100000000.0, 0.15],
|
|
|
|
[200000000.0, 0.25],
|
|
|
|
[300000000.0, 0.5]],
|
|
|
|
"ZEC/USDT": [[0.0, 0.01],
|
|
|
|
[5000.0, 0.025],
|
|
|
|
[25000.0, 0.05],
|
|
|
|
[100000.0, 0.1],
|
|
|
|
[250000.0, 0.125],
|
|
|
|
[1000000.0, 0.5]],
|
2021-09-04 02:30:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 04:27:14 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.load_leverage_brackets = MagicMock()
|
|
|
|
type(api_mock).has = PropertyMock(return_value={'loadLeverageBrackets': True})
|
|
|
|
|
2021-09-04 02:30:52 +00:00
|
|
|
ccxt_exceptionhandlers(
|
|
|
|
mocker,
|
|
|
|
default_conf,
|
|
|
|
api_mock,
|
|
|
|
"binance",
|
|
|
|
"fill_leverage_brackets",
|
2021-09-06 04:27:14 +00:00
|
|
|
"load_leverage_brackets"
|
2021-09-04 02:30:52 +00:00
|
|
|
)
|