Stage 07.3 - auto trading analysis cycle skeleton
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from datetime import datetime
|
||||
|
||||
from src.core.config import load_settings
|
||||
from src.trading.auto.state import AutoTradeState
|
||||
|
||||
@@ -9,12 +12,13 @@ from src.trading.auto.state import AutoTradeState
|
||||
class AutoTradeService:
|
||||
_state = AutoTradeState()
|
||||
|
||||
# получить текущее состояние автоторговли
|
||||
def get_state(self) -> AutoTradeState:
|
||||
if not self._state.symbol:
|
||||
self._state.symbol = load_settings().default_symbol
|
||||
|
||||
return self._state
|
||||
|
||||
# запустить активную торговлю
|
||||
def start(self) -> tuple[AutoTradeState, str]:
|
||||
state = self.get_state()
|
||||
|
||||
@@ -28,6 +32,7 @@ class AutoTradeService:
|
||||
state.status = "RUNNING"
|
||||
return state, "Автоторговля запущена."
|
||||
|
||||
# включить режим наблюдения
|
||||
def observe(self) -> tuple[AutoTradeState, str]:
|
||||
state = self.get_state()
|
||||
previous_status = state.status
|
||||
@@ -42,6 +47,7 @@ class AutoTradeService:
|
||||
|
||||
return state, "Автоторговля переведена в режим наблюдения."
|
||||
|
||||
# полностью выключить автоторговлю
|
||||
def stop(self) -> tuple[AutoTradeState, str]:
|
||||
state = self.get_state()
|
||||
|
||||
@@ -51,17 +57,32 @@ class AutoTradeService:
|
||||
state.status = "OFF"
|
||||
return state, "Автоторговля выключена."
|
||||
|
||||
# установить инструмент
|
||||
def set_symbol(self, symbol: str) -> AutoTradeState:
|
||||
state = self.get_state()
|
||||
state.symbol = symbol
|
||||
return state
|
||||
|
||||
# установить стратегию
|
||||
def set_strategy(self, strategy: str) -> AutoTradeState:
|
||||
state = self.get_state()
|
||||
state.strategy = strategy
|
||||
return state
|
||||
|
||||
# установить риск
|
||||
def set_risk_percent(self, risk_percent: float) -> AutoTradeState:
|
||||
state = self.get_state()
|
||||
state.risk_percent = risk_percent
|
||||
return state
|
||||
|
||||
# выполнить один цикл анализа рынка
|
||||
def run_cycle(self) -> AutoTradeState:
|
||||
state = self.get_state()
|
||||
|
||||
if state.status == "OFF":
|
||||
return state
|
||||
|
||||
state.last_check_at = datetime.now().strftime("%H:%M:%S")
|
||||
state.last_signal = random.choice(["BUY", "SELL", "HOLD"])
|
||||
|
||||
return state
|
||||
@@ -7,8 +7,23 @@ from dataclasses import dataclass
|
||||
|
||||
@dataclass(slots=True)
|
||||
class AutoTradeState:
|
||||
# текущее состояние: OFF / OBSERVING / RUNNING
|
||||
status: str = "OFF"
|
||||
|
||||
# выбранная стратегия: TREND / GRID / SCALP
|
||||
strategy: str | None = None
|
||||
|
||||
# торговый инструмент
|
||||
symbol: str = ""
|
||||
|
||||
# риск на одну сделку в %
|
||||
risk_percent: float | None = None
|
||||
pnl_usd: float = 0.0
|
||||
|
||||
# текущий PnL
|
||||
pnl_usd: float = 0.0
|
||||
|
||||
# время последней проверки
|
||||
last_check_at: str | None = None
|
||||
|
||||
# последний сигнал стратегии
|
||||
last_signal: str | None = None
|
||||
Reference in New Issue
Block a user