Files
stcs/app/bybit/set_functions/set_tp_sl.py
2025-11-02 17:14:22 +05:00

48 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_tp_sl")
async def set_tp_sl_for_position(
tg_id: int,
symbol: str,
take_profit_price: float,
stop_loss_price: float,
position_idx: int,
) -> bool:
"""
Set take profit and stop loss for a symbol.
:param tg_id: Telegram user ID
:param symbol: Symbol to set take profit and stop loss for
:param take_profit_price: Take profit price
:param stop_loss_price: Stop loss price
:param position_idx: Position index
:return: bool
"""
try:
client = await get_bybit_client(tg_id)
take_profit = round(take_profit_price, 6) if take_profit_price is not None else None
stop_loss = round(stop_loss_price, 6) if stop_loss_price is not None else None
resp = client.set_trading_stop(
category="linear",
symbol=symbol,
takeProfit=str(take_profit) if take_profit is not None else None,
stopLoss=str(stop_loss) if stop_loss is not None else None,
positionIdx=position_idx,
tpslMode="Full",
)
if resp.get("retCode") == 0:
logger.info("TP/SL for %s has been set", symbol)
return True
else:
logger.error("Error setting TP/SL for %s: %s", symbol, resp.get("retMsg"))
return False
except Exception as e:
logger.error("Error setting TP/SL for %s: %s", symbol, e)
return False