Files
stcs/app/bybit/set_functions/set_switch_position_mode.py

49 lines
1.6 KiB
Python

import logging.config
from app.bybit import get_bybit_client
from app.bybit.logger_bybit.logger_bybit import LOGGING_CONFIG
logging.config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger("set_switch_position_mode")
async def set_switch_position_mode(tg_id: int, symbol: str, mode: int) -> str | bool:
"""
Set switch position mode
:param tg_id: int - User ID
:param symbol: str - Symbol
:param mode: int - Mode
:return: bool
"""
try:
client = await get_bybit_client(tg_id=tg_id)
response = client.switch_position_mode(
category="linear",
symbol=symbol,
mode=mode,
)
if response["retCode"] == 0:
logger.info("Switch position mode set successfully")
return True
else:
logger.error("Error setting switch position mode for user: %s", tg_id)
return False
except Exception as e:
if str(e).startswith("Position mode is not modified"):
logger.debug(
"Position mode is not modified for user: %s",
tg_id,
)
return True
if str(e).startswith(
"You have an existing position, so position mode cannot be switched"
):
logger.debug(
"You have an existing position, so position mode cannot be switched for user: %s",
tg_id,
)
return "You have an existing position, so position mode cannot be switched"
else:
logger.error("Error connecting to Bybit for user %s: %s", tg_id, e)
return False