Stage 07.3 - auto trading analysis cycle skeleton

This commit is contained in:
2026-04-28 13:20:59 +03:00
parent 83ab842f6e
commit d639137855
7 changed files with 178 additions and 10 deletions

View File

@@ -15,6 +15,7 @@ from src.trading.auto.service import AutoTradeService
router = Router(name="auto")
# красивое отображение стратегии
def _strategy_label(strategy: str | None) -> str:
mapping = {
"TREND": "📈 Trend Following",
@@ -22,8 +23,9 @@ def _strategy_label(strategy: str | None) -> str:
"SCALP": "⚡ Scalping",
}
return mapping.get(strategy or "", "")
# красивое отображение статуса
def _status_label(status: str) -> str:
mapping = {
"OFF": "⚪ Выключена",
@@ -33,6 +35,17 @@ def _status_label(status: str) -> str:
return mapping.get(status, status)
# красивое отображение сигнала
def _signal_label(signal: str | None) -> str:
mapping = {
"BUY": "🟢 BUY",
"SELL": "🔴 SELL",
"HOLD": "🟡 HOLD",
}
return mapping.get(signal or "", "")
# клавиатура автоторговли
def _auto_keyboard() -> InlineKeyboardMarkup:
builder = InlineKeyboardBuilder()
@@ -48,8 +61,14 @@ def _auto_keyboard() -> InlineKeyboardMarkup:
return builder.as_markup()
# собрать текст экрана
def _build_auto_text() -> str:
state = AutoTradeService().get_state()
service = AutoTradeService()
# выполнить один цикл анализа
service.run_cycle()
state = service.get_state()
strategy = _strategy_label(state.strategy)
risk = f"{state.risk_percent:.1f}%" if state.risk_percent is not None else ""
@@ -61,10 +80,13 @@ def _build_auto_text() -> str:
f"Стратегия: {strategy}\n"
f"Инструмент: {state.symbol}\n"
f"Риск: {risk}\n"
f"PnL: {state.pnl_usd:.2f} USD"
f"PnL: {state.pnl_usd:.2f} USD\n"
f"Последний анализ: {state.last_check_at or ''}\n"
f"Сигнал: {_signal_label(state.last_signal)}"
)
# отрисовать экран
async def _render_auto_screen(
target_message: Message,
*,
@@ -83,12 +105,14 @@ async def _render_auto_screen(
await target_message.answer(text, reply_markup=_auto_keyboard())
# открыть экран из меню
@router.message(F.text.in_({"🤖 Автоторговля", "🤖 Авто"}))
async def open_auto(message: Message, state: FSMContext) -> None:
await state.clear()
await _render_auto_screen(message, edit_mode=False)
# открыть экран callback
@router.callback_query(F.data == "auto:home")
async def open_auto_from_callback(callback: CallbackQuery, state: FSMContext) -> None:
await state.clear()
@@ -101,6 +125,7 @@ async def open_auto_from_callback(callback: CallbackQuery, state: FSMContext) ->
await callback.answer()
# включить активную торговлю
@router.callback_query(F.data == "auto:start")
async def auto_start(callback: CallbackQuery) -> None:
service = AutoTradeService()
@@ -112,6 +137,7 @@ async def auto_start(callback: CallbackQuery) -> None:
await callback.answer(message)
# включить наблюдение
@router.callback_query(F.data == "auto:observe")
async def auto_observe(callback: CallbackQuery) -> None:
service = AutoTradeService()
@@ -123,6 +149,7 @@ async def auto_observe(callback: CallbackQuery) -> None:
await callback.answer(message)
# выключить автоторговлю
@router.callback_query(F.data == "auto:stop")
async def auto_stop(callback: CallbackQuery) -> None:
service = AutoTradeService()