2
0
forked from kodorvan/stcs
Files
stcs/app/telegram/handlers/close_orders.py

121 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging.config
from aiogram import F, Router
from aiogram.fsm.context import FSMContext
from aiogram.types import CallbackQuery
from app.bybit.close_positions import cancel_all_orders, cancel_order, close_position
from logger_helper.logger_helper import LOGGING_CONFIG
logging.config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger("close_orders")
router_close_orders = Router(name="close_orders")
@router_close_orders.callback_query(
lambda c: c.data and c.data.startswith("close_position_")
)
async def close_position_handler(
callback_query: CallbackQuery, state: FSMContext
) -> None:
"""
Close a position.
:param callback_query: Incoming callback query from Telegram inline keyboard.
:param state: Finite State Machine context for the current user session.
:return: None
"""
try:
symbol = callback_query.data.split("_", 2)[2]
res = await close_position(tg_id=callback_query.from_user.id, symbol=symbol)
if not res:
await callback_query.answer(text="Произошла ошибка при закрытии позиции.")
return
await callback_query.answer(text="Позиция успешно закрыта.")
logger.debug(
"Command close_position processed successfully for user: %s",
callback_query.from_user.id,
)
except Exception as e:
await callback_query.answer(text="Произошла ошибка при закрытии позиции.")
logger.error(
"Error processing command close_position for user %s: %s",
callback_query.from_user.id,
e,
)
finally:
await state.clear()
@router_close_orders.callback_query(
lambda c: c.data and c.data.startswith("close_order_")
)
async def cancel_order_handler(
callback_query: CallbackQuery, state: FSMContext
) -> None:
"""
Cancel an order.
:param callback_query: Incoming callback query from Telegram inline keyboard.
:param state: Finite State Machine context for the current user session.
:return: None
"""
try:
symbol = callback_query.data.split("_", 2)[2]
res = await cancel_order(tg_id=callback_query.from_user.id, symbol=symbol)
if not res:
await callback_query.answer(text="Произошла ошибка при закрытии ордера.")
return
await callback_query.answer(text="Ордер успешно закрыт.")
logger.debug(
"Command close_order processed successfully for user: %s",
callback_query.from_user.id,
)
except Exception as e:
await callback_query.answer(text="Произошла ошибка при закрытии ордера.")
logger.error(
"Error processing command close_order for user %s: %s",
callback_query.from_user.id,
e,
)
finally:
await state.clear()
@router_close_orders.callback_query(F.data == "cancel_all_orders")
async def cancel_all_orders_handler(
callback_query: CallbackQuery, state: FSMContext
) -> None:
"""
Close all open positions and orders.
:param callback_query: Incoming callback query from Telegram inline keyboard.
:param state: Finite State Machine context for the current user session.
:return: None
"""
try:
res = await cancel_all_orders(tg_id=callback_query.from_user.id)
if not res:
await callback_query.answer(
text="Произошла ошибка при закрытии всех ордеров."
)
return
await callback_query.answer(text="Все ордера успешно закрыты.")
logger.debug(
"Command close_all_orders processed successfully for user: %s",
callback_query.from_user.id,
)
except Exception as e:
await callback_query.answer(text="Произошла ошибка при закрытии всех ордеров.")
logger.error(
"Error processing command close_all_orders for user %s: %s",
callback_query.from_user.id,
e,
)
finally:
await state.clear()