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
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
import ccxt
|
|
|
|
import pytest
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-05-15 06:09:53 +00:00
|
|
|
@pytest.mark.parametrize('limitratio,expected', [
|
|
|
|
(None, 220 * 0.99),
|
|
|
|
(0.99, 220 * 0.99),
|
|
|
|
(0.98, 220 * 0.98),
|
|
|
|
])
|
|
|
|
def test_stoploss_order_binance(default_conf, mocker, limitratio, expected):
|
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):
|
2020-01-19 12:30:56 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190,
|
|
|
|
order_types={'stoploss_on_exchange_limit_ratio': 1.05})
|
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}
|
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types=order_types)
|
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
|
|
|
|
assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell'
|
|
|
|
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')
|
2020-01-19 12:30:56 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
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')
|
2020-01-19 12:30:56 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
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,
|
|
|
|
pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
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):
|
2020-01-19 12:30:56 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190,
|
|
|
|
order_types={'stoploss_on_exchange_limit_ratio': 1.05})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
api_mock.create_order.reset_mock()
|
|
|
|
|
2020-01-19 12:30:56 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
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-07 17:51:07 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test__async_get_historic_ohlcv_binance(default_conf, mocker, caplog):
|
|
|
|
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'
|
2021-11-04 19:00:02 +00:00
|
|
|
respair, restf, res = await exchange._async_get_historic_ohlcv(
|
|
|
|
pair, "5m", 1500000000000, is_new_pair=False)
|
|
|
|
assert respair == pair
|
|
|
|
assert restf == '5m'
|
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()
|
2021-11-04 19:00:02 +00:00
|
|
|
_, _, res = await exchange._async_get_historic_ohlcv(
|
|
|
|
pair, "5m", 1500000000000, is_new_pair=True)
|
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)
|