Files
dzentra_bot/app/src/telegram/handlers/trade/main.py

251 lines
7.9 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.
# app/src/telegram/handlers/trade/main.py
from __future__ import annotations
from aiogram import F, Router
from aiogram.fsm.context import FSMContext
from aiogram.types import CallbackQuery, InlineKeyboardMarkup, Message
from aiogram.utils.keyboard import InlineKeyboardBuilder
from src.telegram.ui.common import mode_line
from src.telegram.handlers.trade.new_order import (
show_recent_drafts,
start_new_order_draft,
)
from src.trading.auto.runner import AutoTradeRunner
router = Router(name="trade_main")
def _trade_screen(title: str) -> str:
return (
f"<b>💹 Торговля — {title}</b>\n"
f"{mode_line()}"
"Выбери раздел"
)
# =========================
# KEYBOARDS
# =========================
def _trade_home_keyboard() -> InlineKeyboardMarkup:
builder = InlineKeyboardBuilder()
builder.button(text="📝 Ордер", callback_data="trade:new_order")
builder.button(text="📂 Ордера", callback_data="trade:orders")
builder.button(text="📜 История", callback_data="trade:history")
builder.button(text="🛠️ Настройки", callback_data="settings:trade")
builder.adjust(2, 2)
return builder.as_markup()
def _trade_home_button() -> InlineKeyboardMarkup:
builder = InlineKeyboardBuilder()
builder.button(text="💹 К торговле", callback_data="trade:home")
return builder.as_markup()
def _orders_menu_keyboard() -> InlineKeyboardMarkup:
builder = InlineKeyboardBuilder()
builder.button(text="📂 Черновики", callback_data="trade:orders:drafts")
builder.button(text="💹 К торговле", callback_data="trade:home")
builder.adjust(2)
return builder.as_markup()
def _history_menu_keyboard() -> InlineKeyboardMarkup:
builder = InlineKeyboardBuilder()
builder.button(text="✅ Исполненные", callback_data="trade:history:filled")
builder.button(text="🚫 Отменённые", callback_data="trade:history:canceled")
builder.button(text="💹 К торговле", callback_data="trade:home")
builder.adjust(2, 1)
return builder.as_markup()
def _settings_menu_keyboard() -> InlineKeyboardMarkup:
builder = InlineKeyboardBuilder()
builder.button(text="⚙️ Параметры", callback_data="trade:settings:params")
builder.button(text="🔁 Режим", callback_data="trade:settings:mode")
builder.button(text=" Справка", callback_data="trade:settings:help")
builder.button(text="💹 К торговле", callback_data="trade:home")
builder.adjust(2, 2)
return builder.as_markup()
# =========================
# TEXTS
# =========================
def _trade_home_text() -> str:
return _trade_screen("Основной экран")
def _trade_orders_text() -> str:
return _trade_screen("Ордера")
def _trade_history_text() -> str:
return _trade_screen("История")
def _trade_settings_text() -> str:
return _trade_screen("Настройки")
# =========================
# ENTRY
# =========================
@router.message(F.text.in_({"💹 Торговля"}))
async def open_trade(message: Message) -> None:
AutoTradeRunner.set_current_screen("trade")
await message.answer(
_trade_home_text(),
reply_markup=_trade_home_keyboard(),
)
@router.callback_query(F.data == "trade:home")
async def open_trade_home_callback(
callback: CallbackQuery,
state: FSMContext,
) -> None:
AutoTradeRunner.set_current_screen("trade")
await state.clear()
await callback.answer()
if callback.message is not None:
await callback.message.edit_text(
_trade_home_text(),
reply_markup=_trade_home_keyboard(),
)
# =========================
# NEW ORDER
# =========================
@router.callback_query(F.data == "trade:new_order")
async def open_new_order_from_trade(
callback: CallbackQuery,
state: FSMContext,
) -> None:
await callback.answer()
if callback.message is not None:
await start_new_order_draft(callback.message, state, edit_mode=True)
# =========================
# ORDERS
# =========================
@router.callback_query(F.data == "trade:orders")
async def open_orders_from_trade(callback: CallbackQuery) -> None:
AutoTradeRunner.set_current_screen("trade")
await callback.answer()
if callback.message is not None:
await callback.message.edit_text(
_trade_orders_text(),
reply_markup=_orders_menu_keyboard(),
)
@router.callback_query(F.data == "trade:orders:drafts")
async def open_drafts_from_orders(callback: CallbackQuery) -> None:
await callback.answer()
if callback.message is not None:
await show_recent_drafts(callback.message, edit_mode=True, page=1)
# =========================
# HISTORY
# =========================
@router.callback_query(F.data == "trade:history")
async def open_trade_history(callback: CallbackQuery) -> None:
AutoTradeRunner.set_current_screen("trade")
await callback.answer()
if callback.message is not None:
await callback.message.edit_text(
_trade_history_text(),
reply_markup=_history_menu_keyboard(),
)
@router.callback_query(F.data == "trade:history:filled")
async def open_filled_history(callback: CallbackQuery) -> None:
await callback.answer()
if callback.message is not None:
await callback.message.edit_text(
"<b>💹 Торговля — История</b>\n\n"
"Шаг 1/1: Исполненные\n"
"Раздел в разработке.",
reply_markup=_trade_home_button(),
)
@router.callback_query(F.data == "trade:history:canceled")
async def open_canceled_history(callback: CallbackQuery) -> None:
await callback.answer()
if callback.message is not None:
await callback.message.edit_text(
"<b>💹 Торговля — История</b>\n\n"
"Шаг 1/1: Отменённые\n"
"Раздел в разработке.",
reply_markup=_trade_home_button(),
)
# =========================
# SETTINGS
# =========================
@router.callback_query(F.data == "trade:settings")
async def open_trade_settings(callback: CallbackQuery) -> None:
AutoTradeRunner.set_current_screen("trade")
await callback.answer()
if callback.message is not None:
await callback.message.edit_text(
_trade_settings_text(),
reply_markup=_settings_menu_keyboard(),
)
@router.callback_query(F.data == "trade:settings:params")
async def open_trade_settings_params(callback: CallbackQuery) -> None:
await callback.answer()
if callback.message is not None:
await callback.message.edit_text(
"<b>💹 Торговля — Настройки</b>\n\n"
"Шаг 1/1: Параметры ордера\n"
"Раздел в разработке.",
reply_markup=_trade_home_button(),
)
@router.callback_query(F.data == "trade:settings:mode")
async def open_trade_settings_mode(callback: CallbackQuery) -> None:
await callback.answer()
if callback.message is not None:
await callback.message.edit_text(
"<b>💹 Торговля — Настройки</b>\n\n"
"Шаг 1/1: Режим работы\n"
"Текущий режим: <b>demo</b>",
reply_markup=_trade_home_button(),
)
@router.callback_query(F.data == "trade:settings:help")
async def open_trade_settings_help(callback: CallbackQuery) -> None:
await callback.answer()
if callback.message is not None:
await callback.message.edit_text(
"<b>💹 Торговля — Справка</b>\n\n"
"Шаг 1/1: Информация\n"
"Раздел в разработке.",
reply_markup=_trade_home_button(),
)