freqtrade_origin/tests/exchange/test_okx.py

715 lines
27 KiB
Python
Raw Normal View History

from datetime import datetime, timedelta, timezone
from unittest.mock import AsyncMock, MagicMock, PropertyMock
2023-03-19 19:03:34 +00:00
import ccxt
2022-05-07 08:56:13 +00:00
import pytest
2022-09-09 18:31:30 +00:00
from freqtrade.enums import CandleType, MarginMode, TradingMode
from freqtrade.exceptions import RetryableOrderError, TemporaryError
from freqtrade.exchange.exchange import timeframe_to_minutes
from tests.conftest import EXMS, get_patched_exchange, log_has
2022-05-07 08:56:13 +00:00
from tests.exchange.test_exchange import ccxt_exceptionhandlers
def test_okx_ohlcv_candle_limit(default_conf, mocker):
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, id="okx")
timeframes = ("1m", "5m", "1h")
start_time = int(datetime(2021, 1, 1, tzinfo=timezone.utc).timestamp() * 1000)
for timeframe in timeframes:
assert exchange.ohlcv_candle_limit(timeframe, CandleType.SPOT) == 300
assert exchange.ohlcv_candle_limit(timeframe, CandleType.FUTURES) == 300
assert exchange.ohlcv_candle_limit(timeframe, CandleType.MARK) == 100
assert exchange.ohlcv_candle_limit(timeframe, CandleType.FUNDING_RATE) == 100
assert exchange.ohlcv_candle_limit(timeframe, CandleType.SPOT, start_time) == 100
assert exchange.ohlcv_candle_limit(timeframe, CandleType.FUTURES, start_time) == 100
assert exchange.ohlcv_candle_limit(timeframe, CandleType.MARK, start_time) == 100
assert exchange.ohlcv_candle_limit(timeframe, CandleType.FUNDING_RATE, start_time) == 100
2024-05-12 13:50:21 +00:00
one_call = int(
(
datetime.now(timezone.utc)
- timedelta(minutes=290 * timeframe_to_minutes(timeframe))
).timestamp()
* 1000
)
assert exchange.ohlcv_candle_limit(timeframe, CandleType.SPOT, one_call) == 300
assert exchange.ohlcv_candle_limit(timeframe, CandleType.FUTURES, one_call) == 300
2024-05-12 13:50:21 +00:00
one_call = int(
(
datetime.now(timezone.utc)
- timedelta(minutes=320 * timeframe_to_minutes(timeframe))
).timestamp()
* 1000
)
assert exchange.ohlcv_candle_limit(timeframe, CandleType.SPOT, one_call) == 100
assert exchange.ohlcv_candle_limit(timeframe, CandleType.FUTURES, one_call) == 100
2022-02-13 03:59:26 +00:00
def test_get_maintenance_ratio_and_amt_okx(
default_conf,
mocker,
):
api_mock = MagicMock()
2024-05-12 13:50:21 +00:00
default_conf["trading_mode"] = "futures"
default_conf["margin_mode"] = "isolated"
default_conf["dry_run"] = False
2022-02-13 03:59:26 +00:00
mocker.patch.multiple(
2024-05-12 13:50:21 +00:00
"freqtrade.exchange.okx.Okx",
2022-02-13 12:00:38 +00:00
exchange_has=MagicMock(return_value=True),
2024-05-12 13:50:21 +00:00
load_leverage_tiers=MagicMock(
return_value={
"ETH/USDT:USDT": [
{
"tier": 1,
"minNotional": 0,
"maxNotional": 2000,
"maintenanceMarginRate": 0.01,
"maxLeverage": 75,
"info": {
"baseMaxLoan": "",
"imr": "0.013",
"instId": "",
"maxLever": "75",
"maxSz": "2000",
"minSz": "0",
"mmr": "0.01",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "1",
"uly": "ETH-USDT",
},
},
{
"tier": 2,
"minNotional": 2001,
"maxNotional": 4000,
"maintenanceMarginRate": 0.015,
"maxLeverage": 50,
"info": {
"baseMaxLoan": "",
"imr": "0.02",
"instId": "",
"maxLever": "50",
"maxSz": "4000",
"minSz": "2001",
"mmr": "0.015",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "2",
"uly": "ETH-USDT",
},
},
{
"tier": 3,
"minNotional": 4001,
"maxNotional": 8000,
"maintenanceMarginRate": 0.02,
"maxLeverage": 20,
"info": {
"baseMaxLoan": "",
"imr": "0.05",
"instId": "",
"maxLever": "20",
"maxSz": "8000",
"minSz": "4001",
"mmr": "0.02",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "3",
"uly": "ETH-USDT",
},
},
],
"ADA/USDT:USDT": [
{
"tier": 1,
"minNotional": 0,
"maxNotional": 500,
"maintenanceMarginRate": 0.02,
"maxLeverage": 75,
"info": {
"baseMaxLoan": "",
"imr": "0.013",
"instId": "",
"maxLever": "75",
"maxSz": "500",
"minSz": "0",
"mmr": "0.01",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "1",
"uly": "ADA-USDT",
},
},
{
"tier": 2,
"minNotional": 501,
"maxNotional": 1000,
"maintenanceMarginRate": 0.025,
"maxLeverage": 50,
"info": {
"baseMaxLoan": "",
"imr": "0.02",
"instId": "",
"maxLever": "50",
"maxSz": "1000",
"minSz": "501",
"mmr": "0.015",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "2",
"uly": "ADA-USDT",
},
},
{
"tier": 3,
"minNotional": 1001,
"maxNotional": 2000,
"maintenanceMarginRate": 0.03,
"maxLeverage": 20,
"info": {
"baseMaxLoan": "",
"imr": "0.05",
"instId": "",
"maxLever": "20",
"maxSz": "2000",
"minSz": "1001",
"mmr": "0.02",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "3",
"uly": "ADA-USDT",
},
},
],
}
),
2022-02-13 03:59:26 +00:00
)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="okx")
2024-05-12 13:50:21 +00:00
assert exchange.get_maintenance_ratio_and_amt("ETH/USDT:USDT", 2000) == (0.01, None)
assert exchange.get_maintenance_ratio_and_amt("ETH/USDT:USDT", 2001) == (0.015, None)
assert exchange.get_maintenance_ratio_and_amt("ETH/USDT:USDT", 4001) == (0.02, None)
assert exchange.get_maintenance_ratio_and_amt("ETH/USDT:USDT", 8000) == (0.02, None)
2024-05-12 13:50:21 +00:00
assert exchange.get_maintenance_ratio_and_amt("ADA/USDT:USDT", 1) == (0.02, None)
assert exchange.get_maintenance_ratio_and_amt("ADA/USDT:USDT", 2000) == (0.03, None)
2022-02-07 09:44:37 +00:00
2022-02-13 03:59:26 +00:00
def test_get_max_pair_stake_amount_okx(default_conf, mocker, leverage_tiers):
exchange = get_patched_exchange(mocker, default_conf, id="okx")
2024-05-12 13:50:21 +00:00
assert exchange.get_max_pair_stake_amount("BNB/BUSD", 1.0) == float("inf")
2022-02-07 09:44:37 +00:00
2024-05-12 13:50:21 +00:00
default_conf["trading_mode"] = "futures"
default_conf["margin_mode"] = "isolated"
2022-02-13 03:59:26 +00:00
exchange = get_patched_exchange(mocker, default_conf, id="okx")
2022-02-07 10:08:30 +00:00
exchange._leverage_tiers = leverage_tiers
2022-02-07 09:44:37 +00:00
2024-05-12 13:50:21 +00:00
assert exchange.get_max_pair_stake_amount("XRP/USDT:USDT", 1.0) == 30000000
assert exchange.get_max_pair_stake_amount("BNB/USDT:USDT", 1.0) == 50000000
assert exchange.get_max_pair_stake_amount("BTC/USDT:USDT", 1.0) == 1000000000
assert exchange.get_max_pair_stake_amount("BTC/USDT:USDT", 1.0, 10.0) == 100000000
assert exchange.get_max_pair_stake_amount("TTT/USDT:USDT", 1.0) == float("inf") # Not in tiers
@pytest.mark.parametrize(
"mode,side,reduceonly,result",
[
("net", "buy", False, "net"),
("net", "sell", True, "net"),
("net", "sell", False, "net"),
("net", "buy", True, "net"),
("longshort", "buy", False, "long"),
("longshort", "sell", True, "long"),
("longshort", "sell", False, "short"),
("longshort", "buy", True, "short"),
],
)
2022-05-07 08:56:13 +00:00
def test__get_posSide(default_conf, mocker, mode, side, reduceonly, result):
exchange = get_patched_exchange(mocker, default_conf, id="okx")
2024-05-12 13:50:21 +00:00
exchange.net_only = mode == "net"
2022-05-07 08:56:13 +00:00
assert exchange._get_posSide(side, reduceonly) == result
def test_additional_exchange_init_okx(default_conf, mocker):
api_mock = MagicMock()
2024-05-12 13:50:21 +00:00
api_mock.fetch_accounts = MagicMock(
return_value=[
{
"id": "2555",
"type": "2",
"currency": None,
"info": {
"acctLv": "2",
"autoLoan": False,
"ctIsoMode": "automatic",
"greeksType": "PA",
"level": "Lv1",
"levelTmp": "",
"mgnIsoMode": "automatic",
"posMode": "long_short_mode",
"uid": "2555",
},
}
]
)
default_conf["dry_run"] = False
2022-05-07 08:56:13 +00:00
exchange = get_patched_exchange(mocker, default_conf, id="okx", api_mock=api_mock)
assert api_mock.fetch_accounts.call_count == 0
exchange.trading_mode = TradingMode.FUTURES
# Default to netOnly
assert exchange.net_only
exchange.additional_exchange_init()
assert api_mock.fetch_accounts.call_count == 1
assert not exchange.net_only
2024-05-12 13:50:21 +00:00
api_mock.fetch_accounts = MagicMock(
return_value=[
{
"id": "2555",
"type": "2",
"currency": None,
"info": {
"acctLv": "2",
"autoLoan": False,
"ctIsoMode": "automatic",
"greeksType": "PA",
"level": "Lv1",
"levelTmp": "",
"mgnIsoMode": "automatic",
"posMode": "net_mode",
"uid": "2555",
},
}
]
)
2022-05-07 08:56:13 +00:00
exchange.additional_exchange_init()
assert api_mock.fetch_accounts.call_count == 1
assert exchange.net_only
2024-05-12 13:50:21 +00:00
default_conf["trading_mode"] = "futures"
default_conf["margin_mode"] = "isolated"
ccxt_exceptionhandlers(
mocker, default_conf, api_mock, "okx", "additional_exchange_init", "fetch_accounts"
)
2022-05-07 08:56:13 +00:00
2023-11-05 15:23:22 +00:00
def test_load_leverage_tiers_okx(default_conf, mocker, markets, tmp_path, caplog, time_machine):
2024-05-12 13:50:21 +00:00
default_conf["datadir"] = tmp_path
2022-08-20 11:47:34 +00:00
# fd_mock = mocker.patch('freqtrade.exchange.exchange.file_dump_json')
2022-02-14 22:53:29 +00:00
api_mock = MagicMock()
2024-05-12 13:50:21 +00:00
type(api_mock).has = PropertyMock(
return_value={
"fetchLeverageTiers": False,
"fetchMarketLeverageTiers": True,
}
)
api_mock.fetch_market_leverage_tiers = AsyncMock(
side_effect=[
[
{
"tier": 1,
"minNotional": 0,
"maxNotional": 500,
"maintenanceMarginRate": 0.02,
"maxLeverage": 75,
"info": {
"baseMaxLoan": "",
"imr": "0.013",
"instId": "",
"maxLever": "75",
"maxSz": "500",
"minSz": "0",
"mmr": "0.01",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "1",
"uly": "ADA-USDT",
},
},
{
"tier": 2,
"minNotional": 501,
"maxNotional": 1000,
"maintenanceMarginRate": 0.025,
"maxLeverage": 50,
"info": {
"baseMaxLoan": "",
"imr": "0.02",
"instId": "",
"maxLever": "50",
"maxSz": "1000",
"minSz": "501",
"mmr": "0.015",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "2",
"uly": "ADA-USDT",
},
},
{
"tier": 3,
"minNotional": 1001,
"maxNotional": 2000,
"maintenanceMarginRate": 0.03,
"maxLeverage": 20,
"info": {
"baseMaxLoan": "",
"imr": "0.05",
"instId": "",
"maxLever": "20",
"maxSz": "2000",
"minSz": "1001",
"mmr": "0.02",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "3",
"uly": "ADA-USDT",
},
},
],
TemporaryError("this Failed"),
[
{
"tier": 1,
"minNotional": 0,
"maxNotional": 2000,
"maintenanceMarginRate": 0.01,
"maxLeverage": 75,
"info": {
"baseMaxLoan": "",
"imr": "0.013",
"instId": "",
"maxLever": "75",
"maxSz": "2000",
"minSz": "0",
"mmr": "0.01",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "1",
"uly": "ETH-USDT",
},
},
{
"tier": 2,
"minNotional": 2001,
"maxNotional": 4000,
"maintenanceMarginRate": 0.015,
"maxLeverage": 50,
"info": {
"baseMaxLoan": "",
"imr": "0.02",
"instId": "",
"maxLever": "50",
"maxSz": "4000",
"minSz": "2001",
"mmr": "0.015",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "2",
"uly": "ETH-USDT",
},
},
{
"tier": 3,
"minNotional": 4001,
"maxNotional": 8000,
"maintenanceMarginRate": 0.02,
"maxLeverage": 20,
"info": {
"baseMaxLoan": "",
"imr": "0.05",
"instId": "",
"maxLever": "20",
"maxSz": "8000",
"minSz": "4001",
"mmr": "0.02",
"optMgnFactor": "0",
"quoteMaxLoan": "",
"tier": "3",
"uly": "ETH-USDT",
},
},
],
2022-02-25 18:58:47 +00:00
]
2024-05-12 13:50:21 +00:00
)
default_conf["trading_mode"] = "futures"
default_conf["margin_mode"] = "isolated"
default_conf["stake_currency"] = "USDT"
2022-02-14 22:53:29 +00:00
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="okx")
exchange.trading_mode = TradingMode.FUTURES
exchange.margin_mode = MarginMode.ISOLATED
exchange.markets = markets
# Initialization of load_leverage_tiers happens as part of exchange init.
2022-02-17 18:37:24 +00:00
assert exchange._leverage_tiers == {
2024-05-12 13:50:21 +00:00
"ADA/USDT:USDT": [
2022-02-14 22:53:29 +00:00
{
2024-05-12 13:50:21 +00:00
"minNotional": 0,
"maxNotional": 500,
"maintenanceMarginRate": 0.02,
"maxLeverage": 75,
"maintAmt": None,
2022-02-14 22:53:29 +00:00
},
{
2024-05-12 13:50:21 +00:00
"minNotional": 501,
"maxNotional": 1000,
"maintenanceMarginRate": 0.025,
"maxLeverage": 50,
"maintAmt": None,
2022-02-14 22:53:29 +00:00
},
{
2024-05-12 13:50:21 +00:00
"minNotional": 1001,
"maxNotional": 2000,
"maintenanceMarginRate": 0.03,
"maxLeverage": 20,
"maintAmt": None,
2022-02-14 22:53:29 +00:00
},
],
2024-05-12 13:50:21 +00:00
"ETH/USDT:USDT": [
2022-02-14 22:53:29 +00:00
{
2024-05-12 13:50:21 +00:00
"minNotional": 0,
"maxNotional": 2000,
"maintenanceMarginRate": 0.01,
"maxLeverage": 75,
"maintAmt": None,
2022-02-14 22:53:29 +00:00
},
{
2024-05-12 13:50:21 +00:00
"minNotional": 2001,
"maxNotional": 4000,
"maintenanceMarginRate": 0.015,
"maxLeverage": 50,
"maintAmt": None,
2022-02-14 22:53:29 +00:00
},
{
2024-05-12 13:50:21 +00:00
"minNotional": 4001,
"maxNotional": 8000,
"maintenanceMarginRate": 0.02,
"maxLeverage": 20,
"maintAmt": None,
2022-02-14 22:53:29 +00:00
},
],
}
2024-05-12 13:50:21 +00:00
filename = (
default_conf["datadir"] / f"futures/leverage_tiers_{default_conf['stake_currency']}.json"
)
2022-08-20 11:47:34 +00:00
assert filename.is_file()
2024-05-12 13:50:21 +00:00
logmsg = "Cached leverage tiers are outdated. Will update."
2022-08-20 11:47:34 +00:00
assert not log_has(logmsg, caplog)
api_mock.fetch_market_leverage_tiers.reset_mock()
exchange.load_leverage_tiers()
assert not log_has(logmsg, caplog)
2024-04-20 07:39:11 +00:00
assert api_mock.fetch_market_leverage_tiers.call_count == 0
2022-08-20 11:47:34 +00:00
# 2 day passes ...
time_machine.move_to(datetime.now() + timedelta(weeks=5))
2022-08-20 11:47:34 +00:00
exchange.load_leverage_tiers()
assert log_has(logmsg, caplog)
2023-03-19 19:03:34 +00:00
2023-03-20 18:27:48 +00:00
def test__set_leverage_okx(mocker, default_conf):
api_mock = MagicMock()
api_mock.set_leverage = MagicMock()
2024-05-12 13:50:21 +00:00
type(api_mock).has = PropertyMock(return_value={"setLeverage": True})
default_conf["dry_run"] = False
default_conf["trading_mode"] = TradingMode.FUTURES
default_conf["margin_mode"] = MarginMode.ISOLATED
2023-03-20 18:27:48 +00:00
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="okx")
2024-05-12 13:50:21 +00:00
exchange._lev_prep("BTC/USDT:USDT", 3.2, "buy")
2023-03-20 18:27:48 +00:00
assert api_mock.set_leverage.call_count == 1
# Leverage is rounded to 3.
2024-05-12 13:50:21 +00:00
assert api_mock.set_leverage.call_args_list[0][1]["leverage"] == 3.2
assert api_mock.set_leverage.call_args_list[0][1]["symbol"] == "BTC/USDT:USDT"
assert api_mock.set_leverage.call_args_list[0][1]["params"] == {
"mgnMode": "isolated",
"posSide": "net",
}
api_mock.set_leverage = MagicMock(side_effect=ccxt.NetworkError())
2024-05-12 13:50:21 +00:00
exchange._lev_prep("BTC/USDT:USDT", 3.2, "buy")
2024-04-20 07:39:11 +00:00
assert api_mock.fetch_leverage.call_count == 1
2023-03-20 18:27:48 +00:00
api_mock.fetch_leverage = MagicMock(side_effect=ccxt.NetworkError())
2023-03-20 18:27:48 +00:00
ccxt_exceptionhandlers(
mocker,
default_conf,
api_mock,
"okx",
"_lev_prep",
"set_leverage",
pair="XRP/USDT:USDT",
leverage=5.0,
2024-05-12 13:50:21 +00:00
side="buy",
2023-03-20 18:27:48 +00:00
)
2023-03-19 19:03:34 +00:00
@pytest.mark.usefixtures("init_persistence")
def test_fetch_stoploss_order_okx(default_conf, mocker):
2024-05-12 13:50:21 +00:00
default_conf["dry_run"] = False
2023-03-19 19:03:34 +00:00
api_mock = MagicMock()
api_mock.fetch_order = MagicMock()
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="okx")
2023-03-19 19:03:34 +00:00
2024-05-12 13:50:21 +00:00
exchange.fetch_stoploss_order("1234", "ETH/BTC")
2023-03-19 19:03:34 +00:00
assert api_mock.fetch_order.call_count == 1
2024-05-12 13:50:21 +00:00
assert api_mock.fetch_order.call_args_list[0][0][0] == "1234"
assert api_mock.fetch_order.call_args_list[0][0][1] == "ETH/BTC"
assert api_mock.fetch_order.call_args_list[0][1]["params"] == {"stop": True}
2023-03-19 19:03:34 +00:00
api_mock.fetch_order = MagicMock(side_effect=ccxt.OrderNotFound)
api_mock.fetch_open_orders = MagicMock(return_value=[])
api_mock.fetch_closed_orders = MagicMock(return_value=[])
api_mock.fetch_canceled_orders = MagicMock(creturn_value=[])
with pytest.raises(RetryableOrderError):
2024-05-12 13:50:21 +00:00
exchange.fetch_stoploss_order("1234", "ETH/BTC")
2023-03-19 19:03:34 +00:00
assert api_mock.fetch_order.call_count == 1
assert api_mock.fetch_open_orders.call_count == 1
assert api_mock.fetch_closed_orders.call_count == 1
assert api_mock.fetch_canceled_orders.call_count == 1
api_mock.fetch_order.reset_mock()
api_mock.fetch_open_orders.reset_mock()
api_mock.fetch_closed_orders.reset_mock()
api_mock.fetch_canceled_orders.reset_mock()
2024-05-12 13:50:21 +00:00
api_mock.fetch_closed_orders = MagicMock(
return_value=[{"id": "1234", "status": "closed", "info": {"ordId": "123455"}}]
)
mocker.patch(f"{EXMS}.fetch_order", MagicMock(return_value={"id": "123455"}))
resp = exchange.fetch_stoploss_order("1234", "ETH/BTC")
2023-03-19 19:03:34 +00:00
assert api_mock.fetch_order.call_count == 1
assert api_mock.fetch_open_orders.call_count == 1
assert api_mock.fetch_closed_orders.call_count == 1
assert api_mock.fetch_canceled_orders.call_count == 0
2024-05-12 13:50:21 +00:00
assert resp["id"] == "1234"
assert resp["id_stop"] == "123455"
assert resp["type"] == "stoploss"
2023-03-20 05:38:42 +00:00
2024-05-12 13:50:21 +00:00
default_conf["dry_run"] = True
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="okx")
dro_mock = mocker.patch(f"{EXMS}.fetch_dry_run_order", MagicMock(return_value={"id": "123455"}))
2023-03-20 05:38:42 +00:00
api_mock.fetch_order.reset_mock()
api_mock.fetch_open_orders.reset_mock()
api_mock.fetch_closed_orders.reset_mock()
api_mock.fetch_canceled_orders.reset_mock()
2024-05-12 13:50:21 +00:00
resp = exchange.fetch_stoploss_order("1234", "ETH/BTC")
2023-03-20 05:38:42 +00:00
assert api_mock.fetch_order.call_count == 0
assert api_mock.fetch_open_orders.call_count == 0
assert api_mock.fetch_closed_orders.call_count == 0
assert api_mock.fetch_canceled_orders.call_count == 0
assert dro_mock.call_count == 1
2023-03-20 05:46:00 +00:00
2024-05-12 13:50:21 +00:00
@pytest.mark.parametrize(
"sl1,sl2,sl3,side", [(1501, 1499, 1501, "sell"), (1499, 1501, 1499, "buy")]
)
2023-03-20 05:46:00 +00:00
def test_stoploss_adjust_okx(mocker, default_conf, sl1, sl2, sl3, side):
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, id="okx")
2023-03-20 05:46:00 +00:00
order = {
2024-05-12 13:50:21 +00:00
"type": "stoploss",
"price": 1500,
"stopLossPrice": 1500,
2023-03-20 05:46:00 +00:00
}
assert exchange.stoploss_adjust(sl1, order, side=side)
assert not exchange.stoploss_adjust(sl2, order, side=side)
2023-06-10 14:41:37 +00:00
def test_stoploss_cancel_okx(mocker, default_conf):
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, id="okx")
2023-06-10 14:41:37 +00:00
exchange.cancel_order = MagicMock()
2024-05-12 13:50:21 +00:00
exchange.cancel_stoploss_order("1234", "ETH/USDT")
2023-06-10 14:41:37 +00:00
assert exchange.cancel_order.call_count == 1
2024-05-12 13:50:21 +00:00
assert exchange.cancel_order.call_args_list[0][1]["order_id"] == "1234"
assert exchange.cancel_order.call_args_list[0][1]["pair"] == "ETH/USDT"
assert exchange.cancel_order.call_args_list[0][1]["params"] == {"stop": True}
def test__get_stop_params_okx(mocker, default_conf):
2024-05-12 13:50:21 +00:00
default_conf["trading_mode"] = "futures"
default_conf["margin_mode"] = "isolated"
exchange = get_patched_exchange(mocker, default_conf, id="okx")
params = exchange._get_stop_params("ETH/USDT:USDT", 1500, "sell")
2024-05-12 13:50:21 +00:00
assert params["tdMode"] == "isolated"
assert params["posSide"] == "net"
def test_fetch_orders_okx(default_conf, mocker, limit_order):
api_mock = MagicMock()
2024-05-12 13:50:21 +00:00
api_mock.fetch_orders = MagicMock(
return_value=[
limit_order["buy"],
limit_order["sell"],
]
)
api_mock.fetch_open_orders = MagicMock(return_value=[limit_order["buy"]])
api_mock.fetch_closed_orders = MagicMock(return_value=[limit_order["buy"]])
mocker.patch(f"{EXMS}.exchange_has", return_value=True)
start_time = datetime.now(timezone.utc) - timedelta(days=20)
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="okx")
# Not available in dry-run
2024-05-12 13:50:21 +00:00
assert exchange.fetch_orders("mocked", start_time) == []
assert api_mock.fetch_orders.call_count == 0
2024-05-12 13:50:21 +00:00
default_conf["dry_run"] = False
2024-05-12 13:50:21 +00:00
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="okx")
def has_resp(_, endpoint):
2024-05-12 13:50:21 +00:00
if endpoint == "fetchOrders":
return False
2024-05-12 13:50:21 +00:00
if endpoint == "fetchClosedOrders":
return True
2024-05-12 13:50:21 +00:00
if endpoint == "fetchOpenOrders":
return True
2024-05-12 13:50:21 +00:00
mocker.patch(f"{EXMS}.exchange_has", has_resp)
2024-05-12 13:50:21 +00:00
history_params = {"method": "privateGetTradeOrdersHistoryArchive"}
# happy path without fetchOrders
2024-05-12 13:50:21 +00:00
exchange.fetch_orders("mocked", start_time)
assert api_mock.fetch_orders.call_count == 0
assert api_mock.fetch_open_orders.call_count == 1
assert api_mock.fetch_closed_orders.call_count == 2
2024-05-12 13:50:21 +00:00
assert "params" not in api_mock.fetch_closed_orders.call_args_list[0][1]
assert api_mock.fetch_closed_orders.call_args_list[1][1]["params"] == history_params
api_mock.fetch_open_orders.reset_mock()
api_mock.fetch_closed_orders.reset_mock()
# regular closed_orders endpoint only has history for 7 days.
2024-05-12 13:50:21 +00:00
exchange.fetch_orders("mocked", datetime.now(timezone.utc) - timedelta(days=6))
assert api_mock.fetch_orders.call_count == 0
assert api_mock.fetch_open_orders.call_count == 1
assert api_mock.fetch_closed_orders.call_count == 1
2024-05-12 13:50:21 +00:00
assert "params" not in api_mock.fetch_closed_orders.call_args_list[0][1]
2024-05-12 13:50:21 +00:00
mocker.patch(f"{EXMS}.exchange_has", return_value=True)
# Unhappy path - first fetch-orders call fails.
api_mock.fetch_orders = MagicMock(side_effect=ccxt.NotSupported())
api_mock.fetch_open_orders.reset_mock()
api_mock.fetch_closed_orders.reset_mock()
2024-05-12 13:50:21 +00:00
exchange.fetch_orders("mocked", start_time)
assert api_mock.fetch_orders.call_count == 1
assert api_mock.fetch_open_orders.call_count == 1
assert api_mock.fetch_closed_orders.call_count == 2
2024-05-12 13:50:21 +00:00
assert "params" not in api_mock.fetch_closed_orders.call_args_list[0][1]
assert api_mock.fetch_closed_orders.call_args_list[1][1]["params"] == history_params