2021-09-07 17:51:07 +00:00
|
|
|
from datetime import datetime, timezone
|
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
|
|
|
|
|
2022-10-09 08:55:38 +00:00
|
|
|
from freqtrade.enums import CandleType, MarginMode, TradingMode
|
2020-09-28 17:43:15 +00:00
|
|
|
from freqtrade.exceptions import DependencyException, InvalidOrderException, OperationalException
|
2021-09-07 17:51:07 +00:00
|
|
|
from tests.conftest import get_mock_coro, get_patched_exchange, log_has_re
|
2020-06-28 09:56:29 +00:00
|
|
|
from tests.exchange.test_exchange import ccxt_exceptionhandlers
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
|
2022-03-05 12:57:54 +00:00
|
|
|
@pytest.mark.parametrize('trademode', [TradingMode.FUTURES, TradingMode.SPOT])
|
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
|
|
|
])
|
2022-03-05 12:57:54 +00:00
|
|
|
def test_stoploss_order_binance(default_conf, mocker, limitratio, expected, side, trademode):
|
2019-08-25 08:08:06 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
2022-12-25 08:05:53 +00:00
|
|
|
order_type = 'stop_loss_limit' if trademode == TradingMode.SPOT else 'stop'
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
api_mock.create_order = MagicMock(return_value={
|
|
|
|
'id': order_id,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
default_conf['dry_run'] = False
|
2022-03-05 12:57:54 +00:00
|
|
|
default_conf['margin_mode'] = MarginMode.ISOLATED
|
|
|
|
default_conf['trading_mode'] = trademode
|
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,
|
2022-09-17 12:55:00 +00:00
|
|
|
order_types={'stoploss': 'limit', 'stoploss_on_exchange_limit_ratio': 1.05},
|
2021-09-17 05:05:13 +00:00
|
|
|
leverage=1.0
|
2021-09-09 20:24:07 +00:00
|
|
|
)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
api_mock.create_order.reset_mock()
|
2022-09-17 12:55:00 +00:00
|
|
|
order_types = {'stoploss': 'limit'}
|
|
|
|
if limitratio is not None:
|
|
|
|
order_types.update({'stoploss_on_exchange_limit_ratio': limitratio})
|
|
|
|
|
2021-09-17 05:05:13 +00:00
|
|
|
order = exchange.stoploss(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
amount=1,
|
|
|
|
stop_price=220,
|
|
|
|
order_types=order_types,
|
|
|
|
side=side,
|
|
|
|
leverage=1.0
|
|
|
|
)
|
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
|
2022-03-05 12:57:54 +00:00
|
|
|
if trademode == TradingMode.SPOT:
|
|
|
|
params_dict = {'stopPrice': 220}
|
|
|
|
else:
|
|
|
|
params_dict = {'stopPrice': 220, 'reduceOnly': True}
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['params'] == params_dict
|
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-17 05:05:13 +00:00
|
|
|
exchange.stoploss(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
amount=1,
|
|
|
|
stop_price=220,
|
|
|
|
order_types={},
|
|
|
|
side=side,
|
|
|
|
leverage=1.0)
|
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-17 05:05:13 +00:00
|
|
|
exchange.stoploss(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
amount=1,
|
|
|
|
stop_price=220,
|
|
|
|
order_types={},
|
|
|
|
side=side,
|
|
|
|
leverage=1.0
|
|
|
|
)
|
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-17 05:05:13 +00:00
|
|
|
pair='ETH/BTC', amount=1, stop_price=220, order_types={},
|
|
|
|
side=side, leverage=1.0)
|
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-09-17 05:05:13 +00:00
|
|
|
order = exchange.stoploss(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
amount=1,
|
|
|
|
stop_price=190,
|
|
|
|
side="sell",
|
|
|
|
order_types={'stoploss_on_exchange_limit_ratio': 1.05},
|
|
|
|
leverage=1.0
|
|
|
|
)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
api_mock.create_order.reset_mock()
|
|
|
|
|
2021-09-17 05:05:13 +00:00
|
|
|
order = exchange.stoploss(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
amount=1,
|
|
|
|
stop_price=220,
|
|
|
|
order_types={},
|
|
|
|
side="sell",
|
|
|
|
leverage=1.0
|
|
|
|
)
|
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")
|
|
|
|
])
|
2022-02-17 18:37:24 +00:00
|
|
|
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,
|
2022-05-29 08:58:01 +00:00
|
|
|
'stopPrice': 1500,
|
2020-01-19 18:54:30 +00:00
|
|
|
'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)
|
2021-08-20 08:40:22 +00:00
|
|
|
|
|
|
|
|
2022-02-07 08:01:00 +00:00
|
|
|
def test_fill_leverage_tiers_binance(default_conf, mocker):
|
2021-09-04 02:30:52 +00:00
|
|
|
api_mock = MagicMock()
|
2022-02-06 04:36:28 +00:00
|
|
|
api_mock.fetch_leverage_tiers = MagicMock(return_value={
|
2022-02-06 01:32:46 +00:00
|
|
|
'ADA/BUSD': [
|
2022-02-06 04:36:28 +00:00
|
|
|
{
|
|
|
|
"tier": 1,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 0,
|
|
|
|
"maxNotional": 100000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.025,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 20,
|
|
|
|
"info": {
|
|
|
|
"bracket": "1",
|
|
|
|
"initialLeverage": "20",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "100000",
|
|
|
|
"minNotional": "0",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.025",
|
|
|
|
"cum": "0.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 2,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 100000,
|
|
|
|
"maxNotional": 500000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.05,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 10,
|
|
|
|
"info": {
|
|
|
|
"bracket": "2",
|
|
|
|
"initialLeverage": "10",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "500000",
|
|
|
|
"minNotional": "100000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.05",
|
|
|
|
"cum": "2500.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 3,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 500000,
|
|
|
|
"maxNotional": 1000000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.1,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 5,
|
|
|
|
"info": {
|
|
|
|
"bracket": "3",
|
|
|
|
"initialLeverage": "5",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "1000000",
|
|
|
|
"minNotional": "500000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.1",
|
|
|
|
"cum": "27500.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 4,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 1000000,
|
|
|
|
"maxNotional": 2000000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.15,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 3,
|
|
|
|
"info": {
|
|
|
|
"bracket": "4",
|
|
|
|
"initialLeverage": "3",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "2000000",
|
|
|
|
"minNotional": "1000000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.15",
|
|
|
|
"cum": "77500.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 5,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 2000000,
|
|
|
|
"maxNotional": 5000000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.25,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 2,
|
|
|
|
"info": {
|
|
|
|
"bracket": "5",
|
|
|
|
"initialLeverage": "2",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "5000000",
|
|
|
|
"minNotional": "2000000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.25",
|
|
|
|
"cum": "277500.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 6,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 5000000,
|
|
|
|
"maxNotional": 30000000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.5,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 1,
|
|
|
|
"info": {
|
|
|
|
"bracket": "6",
|
|
|
|
"initialLeverage": "1",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "30000000",
|
|
|
|
"minNotional": "5000000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.5",
|
|
|
|
"cum": "1527500.0"
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 01:32:46 +00:00
|
|
|
],
|
|
|
|
"ZEC/USDT": [
|
2022-02-06 04:36:28 +00:00
|
|
|
{
|
|
|
|
"tier": 1,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 0,
|
|
|
|
"maxNotional": 50000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.01,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 50,
|
|
|
|
"info": {
|
|
|
|
"bracket": "1",
|
|
|
|
"initialLeverage": "50",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "50000",
|
|
|
|
"minNotional": "0",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.01",
|
|
|
|
"cum": "0.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 2,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 50000,
|
|
|
|
"maxNotional": 150000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.025,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 20,
|
|
|
|
"info": {
|
|
|
|
"bracket": "2",
|
|
|
|
"initialLeverage": "20",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "150000",
|
|
|
|
"minNotional": "50000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.025",
|
|
|
|
"cum": "750.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 3,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 150000,
|
|
|
|
"maxNotional": 250000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.05,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 10,
|
|
|
|
"info": {
|
|
|
|
"bracket": "3",
|
|
|
|
"initialLeverage": "10",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "250000",
|
|
|
|
"minNotional": "150000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.05",
|
|
|
|
"cum": "4500.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 4,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 250000,
|
|
|
|
"maxNotional": 500000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.1,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 5,
|
|
|
|
"info": {
|
|
|
|
"bracket": "4",
|
|
|
|
"initialLeverage": "5",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "500000",
|
|
|
|
"minNotional": "250000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.1",
|
|
|
|
"cum": "17000.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 5,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 500000,
|
|
|
|
"maxNotional": 1000000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.125,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 4,
|
|
|
|
"info": {
|
|
|
|
"bracket": "5",
|
|
|
|
"initialLeverage": "4",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "1000000",
|
|
|
|
"minNotional": "500000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.125",
|
|
|
|
"cum": "29500.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 6,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 1000000,
|
|
|
|
"maxNotional": 2000000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.25,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 2,
|
|
|
|
"info": {
|
|
|
|
"bracket": "6",
|
|
|
|
"initialLeverage": "2",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "2000000",
|
|
|
|
"minNotional": "1000000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.25",
|
|
|
|
"cum": "154500.0"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tier": 7,
|
2022-04-17 03:47:44 +00:00
|
|
|
"minNotional": 2000000,
|
|
|
|
"maxNotional": 30000000,
|
2022-02-16 15:04:43 +00:00
|
|
|
"maintenanceMarginRate": 0.5,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maxLeverage": 1,
|
|
|
|
"info": {
|
|
|
|
"bracket": "7",
|
|
|
|
"initialLeverage": "1",
|
2022-04-17 03:47:44 +00:00
|
|
|
"maxNotional": "30000000",
|
|
|
|
"minNotional": "2000000",
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintMarginRatio": "0.5",
|
|
|
|
"cum": "654500.0"
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 01:32:46 +00:00
|
|
|
],
|
2021-09-17 06:41:00 +00:00
|
|
|
})
|
|
|
|
default_conf['dry_run'] = False
|
|
|
|
default_conf['trading_mode'] = TradingMode.FUTURES
|
2022-02-01 18:53:38 +00:00
|
|
|
default_conf['margin_mode'] = MarginMode.ISOLATED
|
2021-09-04 02:30:52 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
|
2022-02-07 08:01:00 +00:00
|
|
|
exchange.fill_leverage_tiers()
|
2021-09-04 02:30:52 +00:00
|
|
|
|
2022-02-07 08:01:00 +00:00
|
|
|
assert exchange._leverage_tiers == {
|
2022-02-06 01:32:46 +00:00
|
|
|
'ADA/BUSD': [
|
2022-02-06 04:36:28 +00:00
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
"minNotional": 0,
|
|
|
|
"maxNotional": 100000,
|
|
|
|
"maintenanceMarginRate": 0.025,
|
|
|
|
"maxLeverage": 20,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintAmt": 0.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
"minNotional": 100000,
|
|
|
|
"maxNotional": 500000,
|
|
|
|
"maintenanceMarginRate": 0.05,
|
|
|
|
"maxLeverage": 10,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintAmt": 2500.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
"minNotional": 500000,
|
|
|
|
"maxNotional": 1000000,
|
|
|
|
"maintenanceMarginRate": 0.1,
|
|
|
|
"maxLeverage": 5,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintAmt": 27500.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
"minNotional": 1000000,
|
|
|
|
"maxNotional": 2000000,
|
|
|
|
"maintenanceMarginRate": 0.15,
|
|
|
|
"maxLeverage": 3,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintAmt": 77500.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
"minNotional": 2000000,
|
|
|
|
"maxNotional": 5000000,
|
|
|
|
"maintenanceMarginRate": 0.25,
|
|
|
|
"maxLeverage": 2,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintAmt": 277500.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
"minNotional": 5000000,
|
|
|
|
"maxNotional": 30000000,
|
|
|
|
"maintenanceMarginRate": 0.5,
|
|
|
|
"maxLeverage": 1,
|
2022-02-06 04:36:28 +00:00
|
|
|
"maintAmt": 1527500.0
|
|
|
|
}
|
2022-02-06 01:32:46 +00:00
|
|
|
],
|
|
|
|
"ZEC/USDT": [
|
2022-02-06 04:36:28 +00:00
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
'minNotional': 0,
|
|
|
|
'maxNotional': 50000,
|
|
|
|
'maintenanceMarginRate': 0.01,
|
|
|
|
'maxLeverage': 50,
|
2022-02-06 04:36:28 +00:00
|
|
|
'maintAmt': 0.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
'minNotional': 50000,
|
|
|
|
'maxNotional': 150000,
|
|
|
|
'maintenanceMarginRate': 0.025,
|
|
|
|
'maxLeverage': 20,
|
2022-02-06 04:36:28 +00:00
|
|
|
'maintAmt': 750.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
'minNotional': 150000,
|
|
|
|
'maxNotional': 250000,
|
|
|
|
'maintenanceMarginRate': 0.05,
|
|
|
|
'maxLeverage': 10,
|
2022-02-06 04:36:28 +00:00
|
|
|
'maintAmt': 4500.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
'minNotional': 250000,
|
|
|
|
'maxNotional': 500000,
|
|
|
|
'maintenanceMarginRate': 0.1,
|
|
|
|
'maxLeverage': 5,
|
2022-02-06 04:36:28 +00:00
|
|
|
'maintAmt': 17000.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
'minNotional': 500000,
|
|
|
|
'maxNotional': 1000000,
|
|
|
|
'maintenanceMarginRate': 0.125,
|
|
|
|
'maxLeverage': 4,
|
2022-02-06 04:36:28 +00:00
|
|
|
'maintAmt': 29500.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
'minNotional': 1000000,
|
|
|
|
'maxNotional': 2000000,
|
|
|
|
'maintenanceMarginRate': 0.25,
|
|
|
|
'maxLeverage': 2,
|
2022-02-06 04:36:28 +00:00
|
|
|
'maintAmt': 154500.0
|
|
|
|
},
|
|
|
|
{
|
2022-08-26 17:34:51 +00:00
|
|
|
'minNotional': 2000000,
|
|
|
|
'maxNotional': 30000000,
|
|
|
|
'maintenanceMarginRate': 0.5,
|
|
|
|
'maxLeverage': 1,
|
2022-02-06 04:36:28 +00:00
|
|
|
'maintAmt': 654500.0
|
|
|
|
},
|
2022-02-06 01:32:46 +00:00
|
|
|
]
|
2021-09-17 06:41:00 +00:00
|
|
|
}
|
2021-09-04 02:30:52 +00:00
|
|
|
|
2021-09-06 04:27:14 +00:00
|
|
|
api_mock = MagicMock()
|
2022-02-07 08:01:00 +00:00
|
|
|
api_mock.load_leverage_tiers = MagicMock()
|
2022-02-06 04:36:28 +00:00
|
|
|
type(api_mock).has = PropertyMock(return_value={'fetchLeverageTiers': True})
|
2021-09-06 04:27:14 +00:00
|
|
|
|
2021-09-04 02:30:52 +00:00
|
|
|
ccxt_exceptionhandlers(
|
|
|
|
mocker,
|
|
|
|
default_conf,
|
|
|
|
api_mock,
|
|
|
|
"binance",
|
2022-02-07 08:01:00 +00:00
|
|
|
"fill_leverage_tiers",
|
2022-02-15 06:04:50 +00:00
|
|
|
"fetch_leverage_tiers",
|
2021-09-04 02:30:52 +00:00
|
|
|
)
|
2021-09-12 09:09:51 +00:00
|
|
|
|
|
|
|
|
2022-02-07 10:08:30 +00:00
|
|
|
def test_fill_leverage_tiers_binance_dryrun(default_conf, mocker, leverage_tiers):
|
2021-09-17 06:41:00 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
default_conf['trading_mode'] = TradingMode.FUTURES
|
2022-02-01 18:53:38 +00:00
|
|
|
default_conf['margin_mode'] = MarginMode.ISOLATED
|
2021-09-17 06:41:00 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
|
2022-02-07 08:01:00 +00:00
|
|
|
exchange.fill_leverage_tiers()
|
2022-05-21 14:23:29 +00:00
|
|
|
assert len(exchange._leverage_tiers.keys()) > 100
|
2022-02-07 08:01:00 +00:00
|
|
|
for key, value in leverage_tiers.items():
|
2022-05-22 06:36:28 +00:00
|
|
|
v = exchange._leverage_tiers[key]
|
|
|
|
assert isinstance(v, list)
|
|
|
|
assert len(v) == len(value)
|
2021-09-17 06:41:00 +00:00
|
|
|
|
|
|
|
|
2022-10-01 07:21:40 +00:00
|
|
|
def test_additional_exchange_init_binance(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.fapiPrivateGetPositionsideDual = MagicMock(return_value={"dualSidePosition": True})
|
|
|
|
api_mock.fapiPrivateGetMultiAssetsMargin = MagicMock(return_value={"multiAssetsMargin": True})
|
|
|
|
default_conf['dry_run'] = False
|
|
|
|
default_conf['trading_mode'] = TradingMode.FUTURES
|
|
|
|
default_conf['margin_mode'] = MarginMode.ISOLATED
|
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=r"Hedge Mode is not supported.*\nMulti-Asset Mode is not supported.*"):
|
|
|
|
get_patched_exchange(mocker, default_conf, id="binance", api_mock=api_mock)
|
|
|
|
api_mock.fapiPrivateGetPositionsideDual = MagicMock(return_value={"dualSidePosition": False})
|
|
|
|
api_mock.fapiPrivateGetMultiAssetsMargin = MagicMock(return_value={"multiAssetsMargin": False})
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id="binance", api_mock=api_mock)
|
|
|
|
assert exchange
|
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, 'binance',
|
|
|
|
"additional_exchange_init", "fapiPrivateGetPositionsideDual")
|
|
|
|
|
|
|
|
|
2021-09-12 09:09:51 +00:00
|
|
|
def test__set_leverage_binance(mocker, default_conf):
|
|
|
|
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.set_leverage = MagicMock()
|
|
|
|
type(api_mock).has = PropertyMock(return_value={'setLeverage': True})
|
2021-09-17 06:41:00 +00:00
|
|
|
default_conf['dry_run'] = False
|
2021-09-12 09:09:51 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id="binance")
|
|
|
|
exchange._set_leverage(3.0, trading_mode=TradingMode.MARGIN)
|
|
|
|
|
|
|
|
ccxt_exceptionhandlers(
|
|
|
|
mocker,
|
|
|
|
default_conf,
|
|
|
|
api_mock,
|
|
|
|
"binance",
|
|
|
|
"_set_leverage",
|
|
|
|
"set_leverage",
|
|
|
|
pair="XRP/USDT",
|
|
|
|
leverage=5.0,
|
|
|
|
trading_mode=TradingMode.FUTURES
|
|
|
|
)
|
2021-09-07 17:51:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2022-10-09 08:55:38 +00:00
|
|
|
@pytest.mark.parametrize('candle_type', [CandleType.MARK, ''])
|
2021-11-21 07:43:05 +00:00
|
|
|
async def test__async_get_historic_ohlcv_binance(default_conf, mocker, caplog, candle_type):
|
2021-09-07 17:51:07 +00:00
|
|
|
ohlcv = [
|
|
|
|
[
|
|
|
|
int((datetime.now(timezone.utc).timestamp() - 1000) * 1000),
|
|
|
|
1, # open
|
|
|
|
2, # high
|
|
|
|
3, # low
|
|
|
|
4, # close
|
|
|
|
5, # volume (in quote currency)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id='binance')
|
|
|
|
# Monkey-patch async function
|
|
|
|
exchange._api_async.fetch_ohlcv = get_mock_coro(ohlcv)
|
|
|
|
|
|
|
|
pair = 'ETH/BTC'
|
2023-01-05 21:31:32 +00:00
|
|
|
respair, restf, restype, res, _ = await exchange._async_get_historic_ohlcv(
|
2021-11-21 07:43:05 +00:00
|
|
|
pair, "5m", 1500000000000, is_new_pair=False, candle_type=candle_type)
|
2021-11-04 19:00:02 +00:00
|
|
|
assert respair == pair
|
|
|
|
assert restf == '5m'
|
2021-11-21 07:43:05 +00:00
|
|
|
assert restype == candle_type
|
2021-09-07 17:51:07 +00:00
|
|
|
# Call with very old timestamp - causes tons of requests
|
|
|
|
assert exchange._api_async.fetch_ohlcv.call_count > 400
|
|
|
|
# assert res == ohlcv
|
|
|
|
exchange._api_async.fetch_ohlcv.reset_mock()
|
2023-01-05 21:31:32 +00:00
|
|
|
_, _, _, res, _ = await exchange._async_get_historic_ohlcv(
|
2021-11-21 07:43:05 +00:00
|
|
|
pair, "5m", 1500000000000, is_new_pair=True, candle_type=candle_type)
|
2021-09-07 17:51:07 +00:00
|
|
|
|
|
|
|
# Called twice - one "init" call - and one to get the actual data.
|
|
|
|
assert exchange._api_async.fetch_ohlcv.call_count == 2
|
|
|
|
assert res == ohlcv
|
|
|
|
assert log_has_re(r"Candle-data for ETH/BTC available starting with .*", caplog)
|
2021-09-17 20:16:52 +00:00
|
|
|
|
|
|
|
|
2022-02-01 18:53:38 +00:00
|
|
|
@pytest.mark.parametrize("trading_mode,margin_mode,config", [
|
2021-11-18 19:20:01 +00:00
|
|
|
("spot", "", {}),
|
2021-09-17 20:16:52 +00:00
|
|
|
("margin", "cross", {"options": {"defaultType": "margin"}}),
|
|
|
|
("futures", "isolated", {"options": {"defaultType": "future"}}),
|
|
|
|
])
|
2022-02-01 18:53:38 +00:00
|
|
|
def test__ccxt_config(default_conf, mocker, trading_mode, margin_mode, config):
|
2021-09-17 20:16:52 +00:00
|
|
|
default_conf['trading_mode'] = trading_mode
|
2022-02-01 18:53:38 +00:00
|
|
|
default_conf['margin_mode'] = margin_mode
|
2021-09-17 20:16:52 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id="binance")
|
|
|
|
assert exchange._ccxt_config == config
|
2022-01-10 13:17:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('pair,nominal_value,mm_ratio,amt', [
|
|
|
|
("BNB/BUSD", 0.0, 0.025, 0),
|
|
|
|
("BNB/USDT", 100.0, 0.0065, 0),
|
|
|
|
("BTC/USDT", 170.30, 0.004, 0),
|
2022-01-14 12:58:32 +00:00
|
|
|
("BNB/BUSD", 999999.9, 0.1, 27500.0),
|
2022-01-23 02:24:41 +00:00
|
|
|
("BNB/USDT", 5000000.0, 0.15, 233035.0),
|
2022-02-06 04:36:28 +00:00
|
|
|
("BTC/USDT", 600000000, 0.5, 1.997038E8),
|
2022-01-10 13:17:17 +00:00
|
|
|
])
|
|
|
|
def test_get_maintenance_ratio_and_amt_binance(
|
|
|
|
default_conf,
|
|
|
|
mocker,
|
2022-02-07 10:08:30 +00:00
|
|
|
leverage_tiers,
|
2022-01-10 13:17:17 +00:00
|
|
|
pair,
|
|
|
|
nominal_value,
|
|
|
|
mm_ratio,
|
2022-01-14 12:11:17 +00:00
|
|
|
amt,
|
2022-01-10 13:17:17 +00:00
|
|
|
):
|
2022-02-13 12:00:38 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True)
|
2022-01-10 13:17:17 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id="binance")
|
2022-02-07 10:08:30 +00:00
|
|
|
exchange._leverage_tiers = leverage_tiers
|
2022-01-23 02:24:41 +00:00
|
|
|
(result_ratio, result_amt) = exchange.get_maintenance_ratio_and_amt(pair, nominal_value)
|
|
|
|
assert (round(result_ratio, 8), round(result_amt, 8)) == (mm_ratio, amt)
|