2019-03-11 19:22:51 +00:00
|
|
|
from random import randint
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
2020-01-19 13:08:47 +00:00
|
|
|
import ccxt
|
|
|
|
import pytest
|
|
|
|
|
2020-06-28 09:56:29 +00:00
|
|
|
from freqtrade.exceptions import DependencyException, InvalidOrderException
|
2019-09-08 07:54:15 +00:00
|
|
|
from tests.conftest import get_patched_exchange
|
2019-09-11 17:43:29 +00:00
|
|
|
from tests.exchange.test_exchange import ccxt_exceptionhandlers
|
2019-03-11 19:22:51 +00:00
|
|
|
|
2020-09-28 17:43:15 +00:00
|
|
|
|
2020-03-24 19:57:12 +00:00
|
|
|
STOPLOSS_ORDERTYPE = 'stop-loss'
|
2020-11-25 15:27:27 +00:00
|
|
|
STOPLOSS_LIMIT_ORDERTYPE = 'stop-loss-limit'
|
2020-03-24 19:57:12 +00:00
|
|
|
|
2019-03-11 19:22:51 +00:00
|
|
|
|
|
|
|
def test_buy_kraken_trading_agreement(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
|
|
|
order_type = 'limit'
|
|
|
|
time_in_force = 'ioc'
|
2019-07-14 12:18:30 +00:00
|
|
|
api_mock.options = {}
|
2019-03-11 19:22:51 +00:00
|
|
|
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-03-11 19:22:51 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken")
|
|
|
|
|
2021-07-25 01:30:34 +00:00
|
|
|
order = exchange.create_order(pair='ETH/BTC', ordertype=order_type, side="buy",
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
2019-03-11 19:22:51 +00:00
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
|
|
|
assert api_mock.create_order.call_args[0][2] == 'buy'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] == 200
|
|
|
|
assert api_mock.create_order.call_args[0][5] == {'timeInForce': 'ioc',
|
|
|
|
'trading_agreement': 'agree'}
|
|
|
|
|
|
|
|
|
|
|
|
def test_sell_kraken_trading_agreement(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
order_id = 'test_prod_sell_{}'.format(randint(0, 10 ** 6))
|
|
|
|
order_type = 'market'
|
2019-07-14 12:18:30 +00:00
|
|
|
api_mock.options = {}
|
2019-03-11 19:22:51 +00:00
|
|
|
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-03-11 19:22:51 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken")
|
|
|
|
|
2021-07-25 01:30:34 +00:00
|
|
|
order = exchange.create_order(pair='ETH/BTC', ordertype=order_type,
|
|
|
|
side="sell", amount=1, rate=200)
|
2019-03-11 19:22:51 +00:00
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
|
|
|
assert api_mock.create_order.call_args[0][2] == 'sell'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] is None
|
|
|
|
assert api_mock.create_order.call_args[0][5] == {'trading_agreement': 'agree'}
|
2019-09-11 17:43:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_balances_prod(default_conf, mocker):
|
|
|
|
balance_item = {
|
|
|
|
'free': None,
|
|
|
|
'total': 10.0,
|
|
|
|
'used': 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.fetch_balance = MagicMock(return_value={
|
|
|
|
'1ST': balance_item.copy(),
|
|
|
|
'2ST': balance_item.copy(),
|
|
|
|
'3ST': balance_item.copy(),
|
|
|
|
'4ST': balance_item.copy(),
|
2020-12-29 19:05:07 +00:00
|
|
|
'EUR': balance_item.copy(),
|
2021-05-05 04:47:26 +00:00
|
|
|
'timestamp': 123123
|
2019-09-11 17:43:29 +00:00
|
|
|
})
|
|
|
|
kraken_open_orders = [{'symbol': '1ST/EUR',
|
|
|
|
'type': 'limit',
|
|
|
|
'side': 'sell',
|
|
|
|
'price': 20,
|
|
|
|
'cost': 0.0,
|
|
|
|
'amount': 1.0,
|
|
|
|
'filled': 0.0,
|
|
|
|
'average': 0.0,
|
|
|
|
'remaining': 1.0,
|
|
|
|
},
|
|
|
|
{'status': 'open',
|
|
|
|
'symbol': '2ST/EUR',
|
|
|
|
'type': 'limit',
|
|
|
|
'side': 'sell',
|
|
|
|
'price': 20.0,
|
|
|
|
'cost': 0.0,
|
|
|
|
'amount': 2.0,
|
|
|
|
'filled': 0.0,
|
|
|
|
'average': 0.0,
|
|
|
|
'remaining': 2.0,
|
|
|
|
},
|
|
|
|
{'status': 'open',
|
|
|
|
'symbol': '2ST/USD',
|
|
|
|
'type': 'limit',
|
|
|
|
'side': 'sell',
|
|
|
|
'price': 20.0,
|
|
|
|
'cost': 0.0,
|
|
|
|
'amount': 2.0,
|
|
|
|
'filled': 0.0,
|
|
|
|
'average': 0.0,
|
|
|
|
'remaining': 2.0,
|
|
|
|
},
|
|
|
|
{'status': 'open',
|
2020-12-29 19:05:07 +00:00
|
|
|
'symbol': '3ST/EUR',
|
2019-09-11 17:43:29 +00:00
|
|
|
'type': 'limit',
|
|
|
|
'side': 'buy',
|
2020-12-29 19:05:07 +00:00
|
|
|
'price': 0.02,
|
2019-09-11 17:43:29 +00:00
|
|
|
'cost': 0.0,
|
2020-12-29 19:05:07 +00:00
|
|
|
'amount': 100.0,
|
2019-09-11 17:43:29 +00:00
|
|
|
'filled': 0.0,
|
|
|
|
'average': 0.0,
|
2020-12-29 19:05:07 +00:00
|
|
|
'remaining': 100.0,
|
2019-09-11 17:43:29 +00:00
|
|
|
}]
|
|
|
|
api_mock.fetch_open_orders = MagicMock(return_value=kraken_open_orders)
|
|
|
|
default_conf['dry_run'] = False
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken")
|
|
|
|
balances = exchange.get_balances()
|
2021-05-05 04:47:26 +00:00
|
|
|
assert len(balances) == 6
|
2020-12-29 19:05:07 +00:00
|
|
|
|
2019-09-11 17:43:29 +00:00
|
|
|
assert balances['1ST']['free'] == 9.0
|
|
|
|
assert balances['1ST']['total'] == 10.0
|
|
|
|
assert balances['1ST']['used'] == 1.0
|
|
|
|
|
|
|
|
assert balances['2ST']['free'] == 6.0
|
|
|
|
assert balances['2ST']['total'] == 10.0
|
|
|
|
assert balances['2ST']['used'] == 4.0
|
|
|
|
|
2020-12-29 19:05:07 +00:00
|
|
|
assert balances['3ST']['free'] == 10.0
|
2019-09-11 17:43:29 +00:00
|
|
|
assert balances['3ST']['total'] == 10.0
|
2020-12-29 19:05:07 +00:00
|
|
|
assert balances['3ST']['used'] == 0.0
|
2019-09-11 17:43:29 +00:00
|
|
|
|
|
|
|
assert balances['4ST']['free'] == 10.0
|
|
|
|
assert balances['4ST']['total'] == 10.0
|
|
|
|
assert balances['4ST']['used'] == 0.0
|
2020-12-29 19:05:07 +00:00
|
|
|
|
|
|
|
assert balances['EUR']['free'] == 8.0
|
|
|
|
assert balances['EUR']['total'] == 10.0
|
|
|
|
assert balances['EUR']['used'] == 2.0
|
2019-09-11 17:43:29 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, "kraken",
|
|
|
|
"get_balances", "fetch_balance")
|
2020-01-19 13:08:47 +00:00
|
|
|
|
2021-08-09 05:13:35 +00:00
|
|
|
# TODO-lev: All these stoploss tests with shorts
|
|
|
|
|
2020-01-19 13:08:47 +00:00
|
|
|
|
2020-11-25 15:27:27 +00:00
|
|
|
@pytest.mark.parametrize('ordertype', ['market', 'limit'])
|
|
|
|
def test_stoploss_order_kraken(default_conf, mocker, ordertype):
|
2020-01-19 13:08:47 +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, 'kraken')
|
|
|
|
|
2021-07-26 06:01:57 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, side="sell",
|
2020-11-25 15:27:27 +00:00
|
|
|
order_types={'stoploss': ordertype,
|
|
|
|
'stoploss_on_exchange_limit_ratio': 0.99
|
|
|
|
})
|
2020-01-19 13:08:47 +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'
|
2020-11-25 15:27:27 +00:00
|
|
|
if ordertype == 'limit':
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_LIMIT_ORDERTYPE
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['params'] == {
|
|
|
|
'trading_agreement': 'agree', 'price2': 217.8}
|
|
|
|
else:
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['params'] == {
|
|
|
|
'trading_agreement': 'agree'}
|
2020-01-19 13:08:47 +00:00
|
|
|
assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell'
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['amount'] == 1
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['price'] == 220
|
|
|
|
|
|
|
|
# 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, 'kraken')
|
2021-07-26 06:01:57 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side="sell")
|
2020-01-19 13:08:47 +00:00
|
|
|
|
|
|
|
with pytest.raises(InvalidOrderException):
|
|
|
|
api_mock.create_order = MagicMock(
|
|
|
|
side_effect=ccxt.InvalidOrder("kraken Order would trigger immediately."))
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken')
|
2021-07-26 06:01:57 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side="sell")
|
2020-01-19 13:08:47 +00:00
|
|
|
|
2020-06-28 09:56:29 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, "kraken",
|
|
|
|
"stoploss", "create_order", retries=1,
|
2021-07-26 06:01:57 +00:00
|
|
|
pair='ETH/BTC', amount=1, stop_price=220, order_types={}, side="sell")
|
2020-01-19 13:08:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_stoploss_order_dry_run_kraken(default_conf, mocker):
|
|
|
|
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, 'kraken')
|
|
|
|
|
|
|
|
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")
|
2020-01-19 13:08:47 +00:00
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert 'type' in order
|
|
|
|
|
2020-03-24 19:57:12 +00:00
|
|
|
assert order['type'] == STOPLOSS_ORDERTYPE
|
2020-01-19 13:08:47 +00:00
|
|
|
assert order['price'] == 220
|
|
|
|
assert order['amount'] == 1
|
2020-01-19 18:54:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_stoploss_adjust_kraken(mocker, default_conf):
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id='kraken')
|
|
|
|
order = {
|
2020-03-24 19:57:12 +00:00
|
|
|
'type': STOPLOSS_ORDERTYPE,
|
2020-01-19 18:54:30 +00:00
|
|
|
'price': 1500,
|
|
|
|
}
|
2021-07-26 06:01:57 +00:00
|
|
|
assert exchange.stoploss_adjust(1501, order, side="sell")
|
|
|
|
assert not exchange.stoploss_adjust(1499, order, side="sell")
|
2020-01-19 18:54:30 +00:00
|
|
|
# Test with invalid order case ...
|
|
|
|
order['type'] = 'stop_loss_limit'
|
2021-07-26 06:01:57 +00:00
|
|
|
assert not exchange.stoploss_adjust(1501, order, side="sell")
|
2021-09-04 01:25:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('pair,nominal_value,max_lev', [
|
|
|
|
("ADA/BTC", 0.0, 3.0),
|
|
|
|
("BTC/EUR", 100.0, 5.0),
|
|
|
|
("ZEC/USD", 173.31, 2.0),
|
|
|
|
])
|
|
|
|
def test_get_max_leverage_kraken(default_conf, mocker, pair, nominal_value, max_lev):
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id="kraken")
|
|
|
|
exchange._leverage_brackets = {
|
|
|
|
'ADA/BTC': ['2', '3'],
|
|
|
|
'BTC/EUR': ['2', '3', '4', '5'],
|
|
|
|
'ZEC/USD': ['2']
|
|
|
|
}
|
|
|
|
assert exchange.get_max_leverage(pair, nominal_value) == max_lev
|
2021-09-04 02:20:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_fill_leverage_brackets_kraken(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken")
|
2021-09-06 04:27:14 +00:00
|
|
|
exchange.fill_leverage_brackets()
|
2021-09-04 02:20:42 +00:00
|
|
|
|
|
|
|
assert exchange._leverage_brackets == {
|
2021-09-06 04:27:14 +00:00
|
|
|
'BLK/BTC': ['2', '3'],
|
|
|
|
'TKN/BTC': ['2', '3', '4', '5'],
|
|
|
|
'ETH/BTC': ['2']
|
2021-09-04 02:20:42 +00:00
|
|
|
}
|