Fixed
This commit is contained in:
@@ -5,7 +5,7 @@ from aiogram.fsm.context import FSMContext
|
||||
from aiogram.types import CallbackQuery
|
||||
|
||||
import app.telegram.keyboards.inline as kbi
|
||||
import database.request as rq
|
||||
|
||||
from app.bybit.get_functions.get_positions import (
|
||||
get_active_orders,
|
||||
get_active_orders_by_symbol,
|
||||
@@ -22,7 +22,7 @@ router_get_positions_handlers = Router(name="get_positions_handlers")
|
||||
|
||||
@router_get_positions_handlers.callback_query(F.data == "my_deals")
|
||||
async def get_positions_handlers(
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
) -> None:
|
||||
"""
|
||||
Gets the user's active positions.
|
||||
@@ -43,7 +43,7 @@ async def get_positions_handlers(
|
||||
|
||||
@router_get_positions_handlers.callback_query(F.data == "change_position")
|
||||
async def get_positions_handler(
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
) -> None:
|
||||
"""
|
||||
Gets the user's active positions.
|
||||
@@ -64,9 +64,17 @@ async def get_positions_handler(
|
||||
await callback_query.answer(text="Нет активных позиций.")
|
||||
return
|
||||
|
||||
active_positions = [pos for pos in res if float(pos.get("size", 0)) > 0]
|
||||
|
||||
if not active_positions:
|
||||
await callback_query.answer(text="Нет активных позиций.")
|
||||
return
|
||||
|
||||
active_symbols_sides = [(pos.get("symbol"), pos.get("side")) for pos in active_positions]
|
||||
|
||||
await callback_query.message.edit_text(
|
||||
text="Ваши активные позиции:",
|
||||
reply_markup=kbi.create_active_positions_keyboard(res),
|
||||
reply_markup=kbi.create_active_positions_keyboard(symbols=active_symbols_sides),
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("Error in get_positions_handler: %s", e)
|
||||
@@ -82,7 +90,10 @@ async def get_positions_handler(
|
||||
)
|
||||
async def get_position_handler(callback_query: CallbackQuery, state: FSMContext):
|
||||
try:
|
||||
symbol = callback_query.data.split("_", 2)[2]
|
||||
data = callback_query.data
|
||||
parts = data.split("_")
|
||||
symbol = parts[2]
|
||||
get_side = parts[3]
|
||||
res = await get_active_positions_by_symbol(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol
|
||||
)
|
||||
@@ -93,26 +104,48 @@ async def get_position_handler(callback_query: CallbackQuery, state: FSMContext)
|
||||
)
|
||||
return
|
||||
|
||||
symbol = res.get("symbol") or "Нет данных"
|
||||
avg_price = res.get("avgPrice") or "Нет данных"
|
||||
size = res.get("size") or "Нет данных"
|
||||
side = res.get("side") or ""
|
||||
position = next((pos for pos in res if pos.get("side") == get_side), None)
|
||||
|
||||
if position:
|
||||
side = position.get("side")
|
||||
symbol = position.get("symbol") or "Нет данных"
|
||||
avg_price = position.get("avgPrice") or "Нет данных"
|
||||
size = position.get("size") or "Нет данных"
|
||||
take_profit = position.get("takeProfit") or "Нет данных"
|
||||
stop_loss = position.get("stopLoss") or "Нет данных"
|
||||
position_idx = position.get("positionIdx")
|
||||
else:
|
||||
side = "Нет данных"
|
||||
symbol = "Нет данных"
|
||||
avg_price = "Нет данных"
|
||||
size = "Нет данных"
|
||||
take_profit = "Нет данных"
|
||||
stop_loss = "Нет данных"
|
||||
position_idx = "Нет данных"
|
||||
|
||||
side_rus = (
|
||||
"Покупка"
|
||||
if side == "Buy"
|
||||
else "Продажа" if side == "Sell" else "Нет данных"
|
||||
)
|
||||
take_profit = res.get("takeProfit") or "Нет данных"
|
||||
stop_loss = res.get("stopLoss") or "Нет данных"
|
||||
|
||||
position_idx_rus = ("Односторонний" if position_idx == 0
|
||||
else "Покупка в режиме хеджирования" if position_idx == 1
|
||||
else "Продажа в режиме хеджирования" if position_idx == 2
|
||||
else "Нет данных")
|
||||
|
||||
await callback_query.message.edit_text(
|
||||
text=f"Торговая пара: {symbol}\n"
|
||||
f"Цена входа: {avg_price}\n"
|
||||
f"Количество: {size}\n"
|
||||
f"Движение: {side_rus}\n"
|
||||
f"Тейк-профит: {take_profit}\n"
|
||||
f"Стоп-лосс: {stop_loss}\n",
|
||||
reply_markup=kbi.make_close_position_keyboard(symbol_pos=symbol),
|
||||
f"Режим позиции: {position_idx_rus}\n"
|
||||
f"Цена входа: {avg_price}\n"
|
||||
f"Количество: {size}\n"
|
||||
f"Движение: {side_rus}\n"
|
||||
f"Тейк-профит: {take_profit}\n"
|
||||
f"Стоп-лосс: {stop_loss}\n",
|
||||
reply_markup=kbi.make_close_position_keyboard(symbol_pos=symbol,
|
||||
side=side,
|
||||
position_idx=position_idx,
|
||||
qty=size),
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
@@ -126,7 +159,7 @@ async def get_position_handler(callback_query: CallbackQuery, state: FSMContext)
|
||||
|
||||
@router_get_positions_handlers.callback_query(F.data == "open_orders")
|
||||
async def get_open_orders_handler(
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
) -> None:
|
||||
"""
|
||||
Gets the user's open orders.
|
||||
@@ -147,9 +180,17 @@ async def get_open_orders_handler(
|
||||
await callback_query.answer(text="Нет активных ордеров.")
|
||||
return
|
||||
|
||||
active_positions = [pos for pos in res if pos.get("orderStatus", 0) == "New"]
|
||||
|
||||
if not active_positions:
|
||||
await callback_query.answer(text="Нет активных ордеров.")
|
||||
return
|
||||
|
||||
active_orders_sides = [(pos.get("symbol"), pos.get("side")) for pos in active_positions]
|
||||
|
||||
await callback_query.message.edit_text(
|
||||
text="Ваши активные ордера:",
|
||||
reply_markup=kbi.create_active_orders_keyboard(res),
|
||||
reply_markup=kbi.create_active_orders_keyboard(orders=active_orders_sides),
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("Error in get_open_orders_handler: %s", e)
|
||||
@@ -163,7 +204,10 @@ async def get_open_orders_handler(
|
||||
@router_get_positions_handlers.callback_query(lambda c: c.data.startswith("get_order_"))
|
||||
async def get_order_handler(callback_query: CallbackQuery, state: FSMContext):
|
||||
try:
|
||||
symbol = callback_query.data.split("_", 2)[2]
|
||||
data = callback_query.data
|
||||
parts = data.split("_")
|
||||
symbol = parts[2]
|
||||
get_side = parts[3]
|
||||
res = await get_active_orders_by_symbol(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol
|
||||
)
|
||||
@@ -174,37 +218,52 @@ async def get_order_handler(callback_query: CallbackQuery, state: FSMContext):
|
||||
)
|
||||
return
|
||||
|
||||
symbol = res.get("symbol") or "Нет данных"
|
||||
price = res.get("price") or "Нет данных"
|
||||
qty = res.get("qty") or "Нет данных"
|
||||
side = res.get("side") or ""
|
||||
orders = next((pos for pos in res if pos.get("side") == get_side), None)
|
||||
|
||||
if orders:
|
||||
side = orders.get("side")
|
||||
symbol = orders.get("symbol") or "Нет данных"
|
||||
price = orders.get("price") or "Нет данных"
|
||||
qty = orders.get("qty") or "Нет данных"
|
||||
order_type = orders.get("orderType") or ""
|
||||
trigger_price = orders.get("triggerPrice") or "Нет данных"
|
||||
take_profit = orders.get("takeProfit") or "Нет данных"
|
||||
stop_loss = orders.get("stopLoss") or "Нет данных"
|
||||
order_id = orders.get("orderId")
|
||||
else:
|
||||
side = "Нет данных"
|
||||
symbol = "Нет данных"
|
||||
price = "Нет данных"
|
||||
qty = "Нет данных"
|
||||
order_type = "Нет данных"
|
||||
trigger_price = "Нет данных"
|
||||
take_profit = "Нет данных"
|
||||
stop_loss = "Нет данных"
|
||||
order_id = "Нет данных"
|
||||
|
||||
side_rus = (
|
||||
"Покупка"
|
||||
if side == "Buy"
|
||||
else "Продажа" if side == "Sell" else "Нет данных"
|
||||
)
|
||||
order_type = res.get("orderType") or ""
|
||||
|
||||
order_type_rus = (
|
||||
"Рыночный"
|
||||
if order_type == "Market"
|
||||
else "Лимитный" if order_type == "Limit" else "Нет данных"
|
||||
)
|
||||
trigger_price = res.get("triggerPrice") or "Нет данных"
|
||||
take_profit = res.get("takeProfit") or "Нет данных"
|
||||
stop_loss = res.get("stopLoss") or "Нет данных"
|
||||
|
||||
await callback_query.message.edit_text(
|
||||
text=f"Торговая пара: {symbol}\n"
|
||||
f"Цена: {price}\n"
|
||||
f"Количество: {qty}\n"
|
||||
f"Движение: {side_rus}\n"
|
||||
f"Тип ордера: {order_type_rus}\n"
|
||||
f"Триггер цена: {trigger_price}\n"
|
||||
f"Тейк-профит: {take_profit}\n"
|
||||
f"Стоп-лосс: {stop_loss}\n",
|
||||
reply_markup=kbi.make_close_orders_keyboard(symbol_order=symbol),
|
||||
f"Цена: {price}\n"
|
||||
f"Количество: {qty}\n"
|
||||
f"Движение: {side_rus}\n"
|
||||
f"Тип ордера: {order_type_rus}\n"
|
||||
f"Триггер цена: {trigger_price}\n"
|
||||
f"Тейк-профит: {take_profit}\n"
|
||||
f"Стоп-лосс: {stop_loss}\n",
|
||||
reply_markup=kbi.make_close_orders_keyboard(symbol_order=symbol, order_id=order_id),
|
||||
)
|
||||
await rq.set_user_symbol(tg_id=callback_query.from_user.id, symbol=symbol)
|
||||
except Exception as e:
|
||||
logger.error("Error in get_order_handler: %s", e)
|
||||
await callback_query.answer(
|
||||
|
@@ -124,6 +124,13 @@ async def trade_mode(callback_query: CallbackQuery, state: FSMContext) -> None:
|
||||
)
|
||||
return
|
||||
|
||||
if response == "Open orders exist, so you cannot change position mode":
|
||||
await callback_query.answer(
|
||||
text="У вас есть открытые ордера, "
|
||||
"поэтому режим позиции не может быть изменен."
|
||||
)
|
||||
return
|
||||
|
||||
if callback_query.data.startswith("Merged_Single"):
|
||||
await callback_query.answer(text="Выбран режим позиции: Односторонний")
|
||||
await set_leverage(
|
||||
|
@@ -5,6 +5,8 @@ from aiogram import F, Router
|
||||
from aiogram.fsm.context import FSMContext
|
||||
from aiogram.types import CallbackQuery
|
||||
|
||||
from app.telegram.tasks.tasks import add_start_task_merged, cancel_start_task_merged, add_start_task_switch, \
|
||||
cancel_start_task_switch
|
||||
import app.telegram.keyboards.inline as kbi
|
||||
import database.request as rq
|
||||
from app.bybit.get_functions.get_positions import get_active_positions_by_symbol
|
||||
@@ -17,8 +19,6 @@ logger = logging.getLogger("start_trading")
|
||||
|
||||
router_start_trading = Router(name="start_trading")
|
||||
|
||||
user_trade_tasks = {}
|
||||
|
||||
|
||||
@router_start_trading.callback_query(F.data == "start_trading")
|
||||
async def start_trading(callback_query: CallbackQuery, state: FSMContext) -> None:
|
||||
@@ -39,36 +39,41 @@ async def start_trading(callback_query: CallbackQuery, state: FSMContext) -> Non
|
||||
deals = await get_active_positions_by_symbol(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol
|
||||
)
|
||||
size = deals.get("size") or 0
|
||||
position_idx = deals.get("positionIdx")
|
||||
position = next((d for d in deals if d.get("symbol") == symbol), None)
|
||||
if position:
|
||||
size = position.get("size", 0)
|
||||
position_idx = position.get("positionIdx")
|
||||
else:
|
||||
size = 0
|
||||
position_idx = None
|
||||
|
||||
if position_idx != 0 and safe_float(size) > 0 and trade_mode == "Merged_Single":
|
||||
await callback_query.answer(
|
||||
text="У вас есть активная позиция в режиме хеджирования. "
|
||||
"Открытие сделки в одностороннем режиме невозможно.",
|
||||
"Открытие сделки в одностороннем режиме невозможно.",
|
||||
)
|
||||
return
|
||||
|
||||
if position_idx == 0 and safe_float(size) > 0 and trade_mode == "Both_Sides":
|
||||
await callback_query.answer(
|
||||
text="У вас есть активная позиция в одностороннем режиме. "
|
||||
"Открытие сделки в режиме хеджирования невозможно.",
|
||||
"Открытие сделки в режиме хеджирования невозможно.",
|
||||
)
|
||||
return
|
||||
|
||||
if trade_mode == "Merged_Single":
|
||||
await callback_query.message.edit_text(
|
||||
text="Выберите режим торговли:\n\n"
|
||||
"Лонг - все сделки серии открываются на покупку.\n"
|
||||
"Шорт - все сделки серии открываются на продажу.\n"
|
||||
"Свитч - направление каждой сделки серии меняется по переменно.\n",
|
||||
"Лонг - все сделки серии открываются на покупку.\n"
|
||||
"Шорт - все сделки серии открываются на продажу.\n"
|
||||
"Свитч - направление каждой сделки серии меняется по переменно.\n",
|
||||
reply_markup=kbi.merged_start_trading,
|
||||
)
|
||||
else: # trade_mode == "Both_Sides":
|
||||
await callback_query.message.edit_text(
|
||||
text="Выберите режим торговли:\n\n"
|
||||
"Лонг - все сделки открываются на покупку.\n"
|
||||
"Шорт - все сделки открываются на продажу.\n",
|
||||
"Лонг - все сделки открываются на покупку.\n"
|
||||
"Шорт - все сделки открываются на продажу.\n",
|
||||
reply_markup=kbi.both_start_trading,
|
||||
)
|
||||
logger.debug(
|
||||
@@ -106,8 +111,13 @@ async def start_trading_long(callback_query: CallbackQuery, state: FSMContext) -
|
||||
deals = await get_active_positions_by_symbol(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol
|
||||
)
|
||||
size = deals.get("size") or 0
|
||||
position_idx = deals.get("positionIdx")
|
||||
position = next((d for d in deals if d.get("symbol") == symbol), None)
|
||||
if position:
|
||||
size = position.get("size", 0)
|
||||
position_idx = position.get("positionIdx")
|
||||
else:
|
||||
size = 0
|
||||
position_idx = None
|
||||
|
||||
if position_idx == 0 and safe_float(size) > 0:
|
||||
await callback_query.answer(
|
||||
@@ -120,19 +130,22 @@ async def start_trading_long(callback_query: CallbackQuery, state: FSMContext) -
|
||||
)
|
||||
timer_start = conditional_data.timer_start
|
||||
|
||||
if callback_query.from_user.id in user_trade_tasks:
|
||||
task = user_trade_tasks[callback_query.from_user.id]
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
del user_trade_tasks[callback_query.from_user.id]
|
||||
cancel_start_task_merged(user_id=callback_query.from_user.id)
|
||||
|
||||
async def delay_start():
|
||||
if timer_start > 0:
|
||||
await callback_query.message.edit_text(
|
||||
text=f"Торговля будет запущена с задержкой {timer_start} мин.",
|
||||
reply_markup=kbi.cancel_timer,
|
||||
reply_markup=kbi.cancel_timer_merged,
|
||||
)
|
||||
await rq.set_start_timer(
|
||||
tg_id=callback_query.from_user.id, timer_start=0
|
||||
)
|
||||
await asyncio.sleep(timer_start * 60)
|
||||
|
||||
await rq.set_auto_trading(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol, auto_trading=True, side=side
|
||||
)
|
||||
res = await start_trading_cycle(
|
||||
tg_id=callback_query.from_user.id,
|
||||
side=side,
|
||||
@@ -146,27 +159,24 @@ async def start_trading_long(callback_query: CallbackQuery, state: FSMContext) -
|
||||
"estimated will trigger liq": "Лимитный ордер может вызвать мгновенную ликвидацию. Проверьте параметры ордера.",
|
||||
"ab not enough for new order": "Недостаточно средств для создания нового ордера",
|
||||
"InvalidRequestError": "Произошла ошибка при запуске торговли.",
|
||||
"Order does not meet minimum order value": "Сумма ордера не sufficientдля запуска торговли",
|
||||
"Order does not meet minimum order value": "Сумма ордера не достаточна для запуска торговли",
|
||||
"position idx not match position mode": "Торговля уже запущена в режиме хеджирования на продажу для данного инструмента",
|
||||
"Qty invalid": "Некорректное значение ордера для данного инструмента",
|
||||
}
|
||||
|
||||
if res == "OK":
|
||||
await rq.set_start_timer(
|
||||
tg_id=callback_query.from_user.id, timer_start=0
|
||||
)
|
||||
await rq.set_auto_trading(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol, auto_trading=True
|
||||
)
|
||||
await callback_query.answer(text="Торговля запущена")
|
||||
await state.clear()
|
||||
else:
|
||||
# Получаем сообщение из таблицы, или дефолтный текст
|
||||
await rq.set_auto_trading(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol, auto_trading=False, side=side
|
||||
)
|
||||
text = error_messages.get(res, "Произошла ошибка при запуске торговли")
|
||||
await callback_query.answer(text=text)
|
||||
|
||||
await callback_query.message.edit_text("Запуск торговли...")
|
||||
task = asyncio.create_task(delay_start())
|
||||
user_trade_tasks[callback_query.from_user.id] = task
|
||||
await add_start_task_merged(user_id=callback_query.from_user.id, task=task)
|
||||
|
||||
except Exception as e:
|
||||
await callback_query.answer(text="Произошла ошибка при запуске торговли")
|
||||
@@ -181,7 +191,7 @@ async def start_trading_long(callback_query: CallbackQuery, state: FSMContext) -
|
||||
|
||||
@router_start_trading.callback_query(lambda c: c.data == "switch")
|
||||
async def start_trading_switch(
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
) -> None:
|
||||
"""
|
||||
Handles the "switch" callback query.
|
||||
@@ -194,10 +204,10 @@ async def start_trading_switch(
|
||||
await state.clear()
|
||||
await callback_query.message.edit_text(
|
||||
text="Выберите направление первой сделки серии:\n\n"
|
||||
"Лонг - открывается первая сделка на покупку.\n"
|
||||
"Шорт - открывается первая сделка на продажу.\n"
|
||||
"По направлению - сделка открывается в направлении последней сделки предыдущей серии.\n"
|
||||
"Противоположно - сделка открывается в противоположном направлении последней сделки предыдущей серии.\n",
|
||||
"Лонг - открывается первая сделка на покупку.\n"
|
||||
"Шорт - открывается первая сделка на продажу.\n"
|
||||
"По направлению - сделка открывается в направлении последней сделки предыдущей серии.\n"
|
||||
"Противоположно - сделка открывается в противоположном направлении последней сделки предыдущей серии.\n",
|
||||
reply_markup=kbi.switch_side,
|
||||
)
|
||||
except Exception as e:
|
||||
@@ -211,7 +221,7 @@ async def start_trading_switch(
|
||||
|
||||
@router_start_trading.callback_query(
|
||||
lambda c: c.data
|
||||
in {"switch_long", "switch_short", "switch_direction", "switch_opposite"}
|
||||
in {"switch_long", "switch_short", "switch_direction", "switch_opposite"}
|
||||
)
|
||||
async def start_switch(callback_query: CallbackQuery, state: FSMContext) -> None:
|
||||
"""
|
||||
@@ -249,8 +259,13 @@ async def start_switch(callback_query: CallbackQuery, state: FSMContext) -> None
|
||||
deals = await get_active_positions_by_symbol(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol
|
||||
)
|
||||
size = deals.get("size") or 0
|
||||
position_idx = deals.get("positionIdx")
|
||||
position = next((d for d in deals if d.get("symbol") == symbol), None)
|
||||
if position:
|
||||
size = position.get("size", 0)
|
||||
position_idx = position.get("positionIdx")
|
||||
else:
|
||||
size = 0
|
||||
position_idx = None
|
||||
|
||||
if position_idx == 1 and safe_float(size) > 0 and side == "Buy":
|
||||
await callback_query.answer(
|
||||
@@ -269,19 +284,21 @@ async def start_switch(callback_query: CallbackQuery, state: FSMContext) -> None
|
||||
)
|
||||
timer_start = conditional_data.timer_start
|
||||
|
||||
if callback_query.from_user.id in user_trade_tasks:
|
||||
task = user_trade_tasks[callback_query.from_user.id]
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
del user_trade_tasks[callback_query.from_user.id]
|
||||
cancel_start_task_switch(user_id=callback_query.from_user.id)
|
||||
|
||||
async def delay_start():
|
||||
if timer_start > 0:
|
||||
await callback_query.message.edit_text(
|
||||
text=f"Торговля будет запущена с задержкой {timer_start} мин.",
|
||||
reply_markup=kbi.cancel_timer,
|
||||
reply_markup=kbi.cancel_timer_switch,
|
||||
)
|
||||
await rq.set_start_timer(
|
||||
tg_id=callback_query.from_user.id, timer_start=0
|
||||
)
|
||||
await asyncio.sleep(timer_start * 60)
|
||||
await rq.set_auto_trading(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol, auto_trading=True, side=side
|
||||
)
|
||||
res = await start_trading_cycle(
|
||||
tg_id=callback_query.from_user.id,
|
||||
side=side,
|
||||
@@ -295,26 +312,24 @@ async def start_switch(callback_query: CallbackQuery, state: FSMContext) -> None
|
||||
"estimated will trigger liq": "Лимитный ордер может вызвать мгновенную ликвидацию. Проверьте параметры ордера.",
|
||||
"ab not enough for new order": "Недостаточно средств для создания нового ордера",
|
||||
"InvalidRequestError": "Произошла ошибка при запуске торговли.",
|
||||
"Order does not meet minimum order value": "Сумма ордера не sufficientдля запуска торговли",
|
||||
"Order does not meet minimum order value": "Сумма ордера не достаточна для запуска торговли",
|
||||
"position idx not match position mode": "Торговля уже запущена в режиме хеджирования на продажу для данного инструмента",
|
||||
"Qty invalid": "Некорректное значение ордера для данного инструмента",
|
||||
}
|
||||
|
||||
if res == "OK":
|
||||
await rq.set_start_timer(
|
||||
tg_id=callback_query.from_user.id, timer_start=0
|
||||
)
|
||||
await rq.set_auto_trading(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol, auto_trading=True
|
||||
)
|
||||
await callback_query.answer(text="Торговля запущена")
|
||||
await state.clear()
|
||||
else:
|
||||
await rq.set_auto_trading(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol, auto_trading=False, side=side
|
||||
)
|
||||
text = error_messages.get(res, "Произошла ошибка при запуске торговли")
|
||||
await callback_query.answer(text=text)
|
||||
|
||||
await callback_query.message.edit_text("Запуск торговли...")
|
||||
task = asyncio.create_task(delay_start())
|
||||
user_trade_tasks[callback_query.from_user.id] = task
|
||||
await add_start_task_switch(user_id=callback_query.from_user.id, task=task)
|
||||
except asyncio.CancelledError:
|
||||
logger.error("Cancelled timer for user %s", callback_query.from_user.id)
|
||||
except Exception as e:
|
||||
@@ -326,9 +341,9 @@ async def start_switch(callback_query: CallbackQuery, state: FSMContext) -> None
|
||||
)
|
||||
|
||||
|
||||
@router_start_trading.callback_query(F.data == "cancel_timer")
|
||||
@router_start_trading.callback_query(lambda c: c.data == "cancel_timer_merged" or c.data == "cancel_timer_switch")
|
||||
async def cancel_start_trading(
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
callback_query: CallbackQuery, state: FSMContext
|
||||
) -> None:
|
||||
"""
|
||||
Handles the "cancel_timer" callback query.
|
||||
@@ -338,21 +353,12 @@ async def cancel_start_trading(
|
||||
:return: None
|
||||
"""
|
||||
try:
|
||||
task = user_trade_tasks.get(callback_query.from_user.id)
|
||||
if task and not task.done():
|
||||
task.cancel()
|
||||
try:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
user_trade_tasks.pop(callback_query.from_user.id, None)
|
||||
await callback_query.message.edit_text(
|
||||
"Таймер отменён.", reply_markup=kbi.profile_bybit
|
||||
)
|
||||
else:
|
||||
await callback_query.message.edit_text(
|
||||
"Таймер не запущен.", reply_markup=kbi.profile_bybit
|
||||
)
|
||||
await state.clear()
|
||||
if callback_query.data == "cancel_timer_merged":
|
||||
cancel_start_task_merged(user_id=callback_query.from_user.id)
|
||||
elif callback_query.data == "cancel_timer_switch":
|
||||
cancel_start_task_switch(user_id=callback_query.from_user.id)
|
||||
await callback_query.message.edit_text(text="Запуск торговли отменен", reply_markup=kbi.profile_bybit)
|
||||
except Exception as e:
|
||||
await callback_query.answer("Произошла ошибка при отмене запуска торговли")
|
||||
logger.error(
|
||||
@@ -360,5 +366,3 @@ async def cancel_start_trading(
|
||||
callback_query.from_user.id,
|
||||
e,
|
||||
)
|
||||
finally:
|
||||
await state.clear()
|
||||
|
@@ -7,7 +7,7 @@ from aiogram.types import CallbackQuery
|
||||
|
||||
import app.telegram.keyboards.inline as kbi
|
||||
import database.request as rq
|
||||
from app.bybit.close_positions import cancel_order, close_position
|
||||
from app.telegram.tasks.tasks import add_stop_task, cancel_stop_task
|
||||
from logger_helper.logger_helper import LOGGING_CONFIG
|
||||
|
||||
logging.config.dictConfig(LOGGING_CONFIG)
|
||||
@@ -15,50 +15,46 @@ logger = logging.getLogger("stop_trading")
|
||||
|
||||
router_stop_trading = Router(name="stop_trading")
|
||||
|
||||
user_trade_tasks = {}
|
||||
|
||||
|
||||
@router_stop_trading.callback_query(F.data == "stop_trading")
|
||||
async def stop_trading(callback_query: CallbackQuery, state: FSMContext):
|
||||
async def stop_all_trading(callback_query: CallbackQuery, state: FSMContext):
|
||||
try:
|
||||
await state.clear()
|
||||
|
||||
if callback_query.from_user.id in user_trade_tasks:
|
||||
task = user_trade_tasks[callback_query.from_user.id]
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
del user_trade_tasks[callback_query.from_user.id]
|
||||
cancel_stop_task(callback_query.from_user.id)
|
||||
|
||||
conditional_data = await rq.get_user_conditional_settings(
|
||||
tg_id=callback_query.from_user.id
|
||||
)
|
||||
timer_end = conditional_data.timer_end
|
||||
symbols = await rq.get_all_symbols(tg_id=callback_query.from_user.id)
|
||||
|
||||
async def delay_start():
|
||||
if timer_end > 0:
|
||||
await callback_query.message.edit_text(
|
||||
text=f"Торговля будет остановлена с задержкой {timer_end} мин.",
|
||||
reply_markup=kbi.cancel_timer,
|
||||
reply_markup=kbi.cancel_timer_stop,
|
||||
)
|
||||
await rq.set_stop_timer(tg_id=callback_query.from_user.id, timer_end=0)
|
||||
await asyncio.sleep(timer_end * 60)
|
||||
|
||||
for symbol in symbols:
|
||||
auto_trading_data = await rq.get_user_auto_trading(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol
|
||||
)
|
||||
if auto_trading_data is not None and auto_trading_data.auto_trading:
|
||||
await close_position(tg_id=callback_query.from_user.id, symbol=symbol)
|
||||
await cancel_order(tg_id=callback_query.from_user.id, symbol=symbol)
|
||||
await rq.set_auto_trading(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol, auto_trading=False
|
||||
)
|
||||
user_auto_trading_list = await rq.get_all_user_auto_trading(tg_id=callback_query.from_user.id)
|
||||
|
||||
await callback_query.answer(text="Торговля остановлена")
|
||||
await rq.set_stop_timer(tg_id=callback_query.from_user.id, timer_end=0)
|
||||
for active_auto_trading in user_auto_trading_list:
|
||||
if active_auto_trading.auto_trading:
|
||||
symbol = active_auto_trading.symbol
|
||||
get_side = active_auto_trading.side
|
||||
req = await rq.set_auto_trading(
|
||||
tg_id=callback_query.from_user.id, symbol=symbol, auto_trading=False, side=get_side
|
||||
)
|
||||
if not req:
|
||||
await callback_query.edit_text(text="Произошла ошибка при остановке торговли",
|
||||
reply_markup=kbi.profile_bybit)
|
||||
return
|
||||
|
||||
await callback_query.message.edit_text(text="Торговля остановлена", reply_markup=kbi.profile_bybit)
|
||||
|
||||
task = asyncio.create_task(delay_start())
|
||||
user_trade_tasks[callback_query.from_user.id] = task
|
||||
await add_stop_task(user_id=callback_query.from_user.id, task=task)
|
||||
|
||||
logger.debug(
|
||||
"Command stop_trading processed successfully for user: %s",
|
||||
@@ -71,3 +67,18 @@ async def stop_trading(callback_query: CallbackQuery, state: FSMContext):
|
||||
callback_query.from_user.id,
|
||||
e,
|
||||
)
|
||||
|
||||
|
||||
@router_stop_trading.callback_query(F.data == "cancel_timer_stop")
|
||||
async def cancel_stop_trading(callback_query: CallbackQuery, state: FSMContext):
|
||||
try:
|
||||
await state.clear()
|
||||
cancel_stop_task(callback_query.from_user.id)
|
||||
await callback_query.message.edit_text(text="Таймер отменён.", reply_markup=kbi.profile_bybit)
|
||||
except Exception as e:
|
||||
await callback_query.answer(text="Произошла ошибка при отмене остановки торговли")
|
||||
logger.error(
|
||||
"Error processing command cancel_timer_stop for user %s: %s",
|
||||
callback_query.from_user.id,
|
||||
e,
|
||||
)
|
@@ -29,9 +29,14 @@ async def set_tp_sl_handler(callback_query: CallbackQuery, state: FSMContext) ->
|
||||
"""
|
||||
try:
|
||||
await state.clear()
|
||||
symbol = callback_query.data.split("_", 3)[3]
|
||||
data = callback_query.data
|
||||
parts = data.split("_")
|
||||
symbol = parts[3]
|
||||
position_idx = int(parts[4])
|
||||
|
||||
await state.set_state(SetTradingStopState.take_profit_state)
|
||||
await state.update_data(symbol=symbol)
|
||||
await state.update_data(position_idx=position_idx)
|
||||
msg = await callback_query.message.answer(
|
||||
text="Введите тейк-профит:", reply_markup=kbi.cancel
|
||||
)
|
||||
@@ -142,11 +147,13 @@ async def set_stop_loss_handler(message: Message, state: FSMContext) -> None:
|
||||
data = await state.get_data()
|
||||
symbol = data["symbol"]
|
||||
take_profit = data["take_profit"]
|
||||
position_idx = data["position_idx"]
|
||||
res = await set_tp_sl_for_position(
|
||||
tg_id=message.from_user.id,
|
||||
symbol=symbol,
|
||||
take_profit_price=float(take_profit),
|
||||
stop_loss_price=float(stop_loss),
|
||||
position_idx=position_idx,
|
||||
)
|
||||
|
||||
if res:
|
||||
|
Reference in New Issue
Block a user