forked from kodorvan/stcs
		
	
		
			
				
	
	
		
			55 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			1.9 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"
 | 
						|
        if str(e).startswith("Open orders exist, so you cannot change position mode"):
 | 
						|
            logger.debug(
 | 
						|
                "Open orders exist, so you cannot change position mode for user: %s",
 | 
						|
                tg_id,
 | 
						|
            )
 | 
						|
            return "Open orders exist, so you cannot change position mode"
 | 
						|
        else:
 | 
						|
            logger.error("Error connecting to Bybit for user %s: %s", tg_id, e)
 | 
						|
            return False
 |