freqtrade_origin/tests/exchange/test_htx.py

141 lines
4.7 KiB
Python
Raw Normal View History

2022-01-30 13:15:07 +00:00
from random import randint
from unittest.mock import MagicMock
import ccxt
import pytest
from freqtrade.exceptions import DependencyException, InvalidOrderException
from tests.conftest import EXMS, get_patched_exchange
2022-01-30 13:15:07 +00:00
from tests.exchange.test_exchange import ccxt_exceptionhandlers
2024-05-12 13:50:21 +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"),
],
)
2024-01-20 16:13:09 +00:00
def test_create_stoploss_order_htx(default_conf, mocker, limitratio, expected, side):
2022-01-30 13:15:07 +00:00
api_mock = MagicMock()
2024-05-12 13:50:21 +00:00
order_id = f"test_prod_buy_{randint(0, 10 ** 6)}"
order_type = "stop-limit"
2022-01-30 13:15:07 +00:00
2024-05-12 13:50:21 +00:00
api_mock.create_order = MagicMock(return_value={"id": order_id, "info": {"foo": "bar"}})
default_conf["dry_run"] = False
mocker.patch(f"{EXMS}.amount_to_precision", lambda s, x, y: y)
mocker.patch(f"{EXMS}.price_to_precision", lambda s, x, y, **kwargs: y)
2022-01-30 13:15:07 +00:00
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, api_mock, "htx")
2022-01-30 13:15:07 +00:00
with pytest.raises(InvalidOrderException):
2024-05-12 13:50:21 +00:00
order = exchange.create_stoploss(
pair="ETH/BTC",
amount=1,
stop_price=190,
order_types={"stoploss_on_exchange_limit_ratio": 1.05},
side=side,
leverage=1.0,
)
2022-01-30 13:15:07 +00:00
api_mock.create_order.reset_mock()
2024-05-12 13:50:21 +00:00
order_types = {} if limitratio is None else {"stoploss_on_exchange_limit_ratio": limitratio}
2023-02-14 19:42:08 +00:00
order = exchange.create_stoploss(
2024-05-12 13:50:21 +00:00
pair="ETH/BTC", amount=1, stop_price=220, order_types=order_types, side=side, leverage=1.0
)
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"] == 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
2022-01-30 13:15:07 +00:00
# Price should be 1% below stopprice
2024-05-12 13:50:21 +00:00
assert api_mock.create_order.call_args_list[0][1]["price"] == expected
assert api_mock.create_order.call_args_list[0][1]["params"] == {
"stopPrice": 220,
"operator": "lte",
}
2022-01-30 13:15:07 +00:00
# test exception handling
with pytest.raises(DependencyException):
api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance"))
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, api_mock, "htx")
exchange.create_stoploss(
pair="ETH/BTC", amount=1, stop_price=220, order_types={}, side=side, leverage=1.0
)
2022-01-30 13:15:07 +00:00
with pytest.raises(InvalidOrderException):
api_mock.create_order = MagicMock(
2024-05-12 13:50:21 +00:00
side_effect=ccxt.InvalidOrder("binance Order would trigger immediately.")
)
exchange = get_patched_exchange(mocker, default_conf, api_mock, "binance")
exchange.create_stoploss(
pair="ETH/BTC", amount=1, stop_price=220, order_types={}, side=side, leverage=1.0
)
ccxt_exceptionhandlers(
mocker,
default_conf,
api_mock,
"htx",
"create_stoploss",
"create_order",
retries=1,
pair="ETH/BTC",
amount=1,
stop_price=220,
order_types={},
side=side,
leverage=1.0,
)
2022-01-30 13:15:07 +00:00
2024-01-20 16:13:09 +00:00
def test_create_stoploss_order_dry_run_htx(default_conf, mocker):
2022-01-30 13:15:07 +00:00
api_mock = MagicMock()
2024-05-12 13:50:21 +00:00
order_type = "stop-limit"
default_conf["dry_run"] = True
mocker.patch(f"{EXMS}.amount_to_precision", lambda s, x, y: y)
mocker.patch(f"{EXMS}.price_to_precision", lambda s, x, y, **kwargs: y)
2022-01-30 13:15:07 +00:00
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, api_mock, "htx")
2022-01-30 13:15:07 +00:00
with pytest.raises(InvalidOrderException):
2024-05-12 13:50:21 +00:00
order = exchange.create_stoploss(
pair="ETH/BTC",
amount=1,
stop_price=190,
order_types={"stoploss_on_exchange_limit_ratio": 1.05},
side="sell",
leverage=1.0,
)
2022-01-30 13:15:07 +00:00
api_mock.create_order.reset_mock()
2024-05-12 13:50:21 +00:00
order = exchange.create_stoploss(
pair="ETH/BTC", amount=1, stop_price=220, order_types={}, side="sell", leverage=1.0
)
2022-01-30 13:15:07 +00:00
2024-05-12 13:50:21 +00:00
assert "id" in order
assert "info" in order
assert "type" in order
2022-01-30 13:15:07 +00:00
2024-05-12 13:50:21 +00:00
assert order["type"] == order_type
assert order["price"] == 220
assert order["amount"] == 1
2022-01-30 13:15:07 +00:00
2024-01-20 16:13:09 +00:00
def test_stoploss_adjust_htx(mocker, default_conf):
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, id="htx")
2022-01-30 13:15:07 +00:00
order = {
2024-05-12 13:50:21 +00:00
"type": "stop",
"price": 1500,
"stopPrice": "1500",
2022-01-30 13:15:07 +00:00
}
2024-05-12 13:50:21 +00:00
assert exchange.stoploss_adjust(1501, order, "sell")
assert not exchange.stoploss_adjust(1499, order, "sell")
2022-01-30 13:15:07 +00:00
# Test with invalid order case
2024-05-12 13:50:21 +00:00
assert exchange.stoploss_adjust(1501, order, "sell")