2020-03-24 19:57:12 +00:00
|
|
|
from random import randint
|
2021-09-17 05:05:13 +00:00
|
|
|
from unittest.mock import MagicMock
|
2020-03-24 19:57:12 +00:00
|
|
|
|
|
|
|
import ccxt
|
|
|
|
import pytest
|
|
|
|
|
2020-06-28 09:56:29 +00:00
|
|
|
from freqtrade.exceptions import DependencyException, InvalidOrderException
|
2020-08-22 15:35:42 +00:00
|
|
|
from freqtrade.exchange.common import API_FETCH_ORDER_RETRY_COUNT
|
2020-03-24 19:57:12 +00:00
|
|
|
from tests.conftest import get_patched_exchange
|
2020-06-28 09:56:29 +00:00
|
|
|
|
2020-03-25 16:01:45 +00:00
|
|
|
from .test_exchange import ccxt_exceptionhandlers
|
2020-03-24 19:57:12 +00:00
|
|
|
|
2020-09-28 17:43:15 +00:00
|
|
|
|
2020-03-24 19:57:12 +00:00
|
|
|
STOPLOSS_ORDERTYPE = 'stop'
|
|
|
|
|
|
|
|
|
2021-09-09 20:24:07 +00:00
|
|
|
@pytest.mark.parametrize('order_price,exchangelimitratio,side', [
|
|
|
|
(217.8, 1.05, "sell"),
|
|
|
|
(222.2, 0.95, "buy"),
|
|
|
|
])
|
|
|
|
def test_stoploss_order_ftx(default_conf, mocker, order_price, exchangelimitratio, side):
|
2020-03-24 19:57:12 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
|
|
|
|
|
|
|
api_mock.create_order = MagicMock(return_value={
|
|
|
|
'id': order_id,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
default_conf['dry_run'] = False
|
|
|
|
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)
|
|
|
|
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx')
|
|
|
|
|
|
|
|
# stoploss_on_exchange_limit_ratio is irrelevant for ftx market orders
|
2021-09-17 05:05:13 +00:00
|
|
|
order = exchange.stoploss(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
amount=1,
|
|
|
|
stop_price=190,
|
|
|
|
side=side,
|
|
|
|
order_types={'stoploss_on_exchange_limit_ratio': exchangelimitratio},
|
|
|
|
leverage=1.0
|
|
|
|
)
|
2020-06-01 09:33:40 +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'] == STOPLOSS_ORDERTYPE
|
2021-09-09 20:24:07 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['side'] == side
|
2020-06-01 09:33:40 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
|
|
|
assert 'orderPrice' not in api_mock.create_order.call_args_list[0][1]['params']
|
2021-04-13 06:26:26 +00:00
|
|
|
assert 'stopPrice' in api_mock.create_order.call_args_list[0][1]['params']
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['params']['stopPrice'] == 190
|
2020-06-01 09:33:40 +00:00
|
|
|
|
2020-03-24 19:57:12 +00:00
|
|
|
assert api_mock.create_order.call_count == 1
|
|
|
|
|
|
|
|
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=side,
|
|
|
|
leverage=1.0
|
|
|
|
)
|
2020-03-24 19:57:12 +00:00
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE
|
2021-09-09 20:24:07 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['side'] == side
|
2020-03-24 19:57:12 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
2020-06-01 09:33:40 +00:00
|
|
|
assert 'orderPrice' not in api_mock.create_order.call_args_list[0][1]['params']
|
2021-04-13 06:26:26 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['params']['stopPrice'] == 220
|
2020-06-01 09:33:40 +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={'stoploss': 'limit'}, side=side,
|
|
|
|
leverage=1.0
|
|
|
|
)
|
2020-06-01 09:33:40 +00:00
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE
|
2021-09-09 20:24:07 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['side'] == side
|
2020-06-01 09:33:40 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
|
|
|
assert 'orderPrice' in api_mock.create_order.call_args_list[0][1]['params']
|
2021-09-09 20:24:07 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['params']['orderPrice'] == order_price
|
2021-04-13 06:26:26 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['params']['stopPrice'] == 220
|
2020-03-24 19:57:12 +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, 'ftx')
|
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
|
|
|
|
)
|
2020-03-24 19:57:12 +00:00
|
|
|
|
|
|
|
with pytest.raises(InvalidOrderException):
|
|
|
|
api_mock.create_order = MagicMock(
|
|
|
|
side_effect=ccxt.InvalidOrder("ftx Order would trigger immediately."))
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx')
|
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
|
|
|
|
)
|
2020-03-24 19:57:12 +00:00
|
|
|
|
2020-06-28 09:56:29 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, "ftx",
|
|
|
|
"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)
|
2020-03-24 19:57:12 +00:00
|
|
|
|
|
|
|
|
2021-09-09 20:24:07 +00:00
|
|
|
@pytest.mark.parametrize('side', [("sell"), ("buy")])
|
|
|
|
def test_stoploss_order_dry_run_ftx(default_conf, mocker, side):
|
2020-03-24 19:57:12 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
default_conf['dry_run'] = True
|
|
|
|
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)
|
|
|
|
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx')
|
|
|
|
|
|
|
|
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=side,
|
|
|
|
leverage=1.0
|
|
|
|
)
|
2020-03-24 19:57:12 +00:00
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert 'type' in order
|
|
|
|
|
|
|
|
assert order['type'] == STOPLOSS_ORDERTYPE
|
|
|
|
assert order['price'] == 220
|
|
|
|
assert order['amount'] == 1
|
|
|
|
|
|
|
|
|
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_ftx(mocker, default_conf, sl1, sl2, sl3, side):
|
2020-03-24 19:57:12 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id='ftx')
|
|
|
|
order = {
|
|
|
|
'type': STOPLOSS_ORDERTYPE,
|
|
|
|
'price': 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-03-24 19:57:12 +00:00
|
|
|
# Test with invalid order case ...
|
|
|
|
order['type'] = 'stop_loss_limit'
|
2021-09-09 20:24:07 +00:00
|
|
|
assert not exchange.stoploss_adjust(sl3, order, side=side)
|
2020-03-25 16:01:45 +00:00
|
|
|
|
|
|
|
|
2022-05-20 04:26:16 +00:00
|
|
|
@pytest.mark.usefixtures("init_persistence")
|
2022-02-23 05:27:56 +00:00
|
|
|
def test_fetch_stoploss_order_ftx(default_conf, mocker, limit_sell_order, limit_buy_order):
|
2020-03-25 16:01:45 +00:00
|
|
|
default_conf['dry_run'] = True
|
|
|
|
order = MagicMock()
|
|
|
|
order.myid = 123
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id='ftx')
|
|
|
|
exchange._dry_run_open_orders['X'] = order
|
2020-06-28 14:30:24 +00:00
|
|
|
assert exchange.fetch_stoploss_order('X', 'TKN/BTC').myid == 123
|
2020-03-25 16:01:45 +00:00
|
|
|
|
|
|
|
with pytest.raises(InvalidOrderException, match=r'Tried to get an invalid dry-run-order.*'):
|
2020-06-28 14:30:24 +00:00
|
|
|
exchange.fetch_stoploss_order('Y', 'TKN/BTC')
|
2020-03-25 16:01:45 +00:00
|
|
|
|
|
|
|
default_conf['dry_run'] = False
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.fetch_orders = MagicMock(return_value=[{'id': 'X', 'status': '456'}])
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx')
|
2020-06-28 14:30:24 +00:00
|
|
|
assert exchange.fetch_stoploss_order('X', 'TKN/BTC')['status'] == '456'
|
2020-03-25 16:01:45 +00:00
|
|
|
|
|
|
|
api_mock.fetch_orders = MagicMock(return_value=[{'id': 'Y', 'status': '456'}])
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx')
|
2020-06-14 04:33:08 +00:00
|
|
|
with pytest.raises(InvalidOrderException, match=r"Could not get stoploss order for id X"):
|
2020-06-28 14:30:24 +00:00
|
|
|
exchange.fetch_stoploss_order('X', 'TKN/BTC')['status']
|
2020-03-25 16:01:45 +00:00
|
|
|
|
2022-02-19 09:07:32 +00:00
|
|
|
# stoploss Limit order
|
|
|
|
api_mock.fetch_orders = MagicMock(return_value=[
|
|
|
|
{'id': 'X', 'status': 'closed',
|
|
|
|
'info': {
|
|
|
|
'orderId': 'mocked_limit_sell',
|
|
|
|
}}])
|
2021-06-13 09:06:34 +00:00
|
|
|
api_mock.fetch_order = MagicMock(return_value=limit_sell_order)
|
|
|
|
|
2022-02-19 09:07:32 +00:00
|
|
|
# No orderId field - no call to fetch_order
|
2021-06-13 09:06:34 +00:00
|
|
|
resp = exchange.fetch_stoploss_order('X', 'TKN/BTC')
|
|
|
|
assert resp
|
|
|
|
assert api_mock.fetch_order.call_count == 1
|
|
|
|
assert resp['id_stop'] == 'mocked_limit_sell'
|
|
|
|
assert resp['id'] == 'X'
|
|
|
|
assert resp['type'] == 'stop'
|
|
|
|
assert resp['status_stop'] == 'triggered'
|
|
|
|
|
2022-02-19 09:07:32 +00:00
|
|
|
# Stoploss market order
|
|
|
|
# Contains no new Order, but "average" instead
|
|
|
|
order = {'id': 'X', 'status': 'closed', 'info': {'orderId': None}, 'average': 0.254}
|
|
|
|
api_mock.fetch_orders = MagicMock(return_value=[order])
|
|
|
|
api_mock.fetch_order.reset_mock()
|
2021-09-09 20:24:07 +00:00
|
|
|
resp = exchange.fetch_stoploss_order('X', 'TKN/BTC')
|
|
|
|
assert resp
|
2022-02-19 09:07:32 +00:00
|
|
|
# fetch_order not called (no regular order ID)
|
|
|
|
assert api_mock.fetch_order.call_count == 0
|
|
|
|
assert order == order
|
2021-09-09 20:24:07 +00:00
|
|
|
|
2020-03-25 16:01:45 +00:00
|
|
|
with pytest.raises(InvalidOrderException):
|
|
|
|
api_mock.fetch_orders = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx')
|
2020-06-28 14:30:24 +00:00
|
|
|
exchange.fetch_stoploss_order(order_id='_', pair='TKN/BTC')
|
2020-03-25 16:01:45 +00:00
|
|
|
assert api_mock.fetch_orders.call_count == 1
|
|
|
|
|
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, 'ftx',
|
2020-06-28 14:30:24 +00:00
|
|
|
'fetch_stoploss_order', 'fetch_orders',
|
2020-08-22 15:35:42 +00:00
|
|
|
retries=API_FETCH_ORDER_RETRY_COUNT + 1,
|
2020-03-25 16:01:45 +00:00
|
|
|
order_id='_', pair='TKN/BTC')
|
2021-05-16 07:08:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_order_id(mocker, default_conf):
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id='ftx')
|
|
|
|
order = {
|
|
|
|
'type': STOPLOSS_ORDERTYPE,
|
|
|
|
'price': 1500,
|
|
|
|
'id': '1111',
|
2021-06-13 09:06:34 +00:00
|
|
|
'id_stop': '1234',
|
2021-05-16 07:08:13 +00:00
|
|
|
'info': {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert exchange.get_order_id_conditional(order) == '1234'
|
|
|
|
|
|
|
|
order = {
|
|
|
|
'type': 'limit',
|
|
|
|
'price': 1500,
|
|
|
|
'id': '1111',
|
2021-06-13 09:06:34 +00:00
|
|
|
'id_stop': '1234',
|
2021-05-16 07:08:13 +00:00
|
|
|
'info': {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert exchange.get_order_id_conditional(order) == '1111'
|