fix: показывать только активные assets + unrealized PnL по позиции
1. Капитал считается только по assets из текущего конфига (не все в БД) 2. Позиция показывает текущий PnL с иконкой и текущей ценой Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -48,11 +48,30 @@ def send_status(cfg: TelegramConfig, event: str, details: str = "") -> bool:
|
||||
return send_telegram(cfg, msg)
|
||||
|
||||
|
||||
def _get_current_price(cfg, ticker):
|
||||
"""Fetch current price for ticker via exchange."""
|
||||
try:
|
||||
# Find symbol from config
|
||||
for a in cfg.assets:
|
||||
if a.ticker == ticker:
|
||||
from .core.signals import fetch_prices_ccxt
|
||||
prices, _ = fetch_prices_ccxt(a.symbol, "4h", "1d", cfg.exchange)
|
||||
if prices is not None and len(prices) > 0:
|
||||
return float(prices[-1])
|
||||
return None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def format_status_report(cfg, conn) -> str:
|
||||
"""Format full status report for Telegram."""
|
||||
from .state import get_all_assets, get_open_position
|
||||
from .state import get_asset, get_open_position
|
||||
|
||||
# Only show assets from current config
|
||||
active_tickers = {a.ticker for a in cfg.assets}
|
||||
assets = [get_asset(conn, t) for t in active_tickers]
|
||||
assets = [a for a in assets if a is not None]
|
||||
|
||||
assets = get_all_assets(conn)
|
||||
total_equity = sum(a["equity"] for a in assets)
|
||||
total_initial = cfg.initial_capital * len(assets) if assets else cfg.initial_capital
|
||||
total_pnl = total_equity - total_initial
|
||||
@@ -67,7 +86,7 @@ def format_status_report(cfg, conn) -> str:
|
||||
lines.append(f" PnL: ${total_pnl:+,.2f}")
|
||||
lines.append("")
|
||||
|
||||
# --- Open positions ---
|
||||
# --- Open positions with unrealized PnL ---
|
||||
open_positions = []
|
||||
for a in assets:
|
||||
pos = get_open_position(conn, a["ticker"])
|
||||
@@ -81,10 +100,22 @@ def format_status_report(cfg, conn) -> str:
|
||||
entry = pos["entry_price"]
|
||||
stop = pos["stop_price"]
|
||||
size = pos["pos_value"]
|
||||
pos_size = pos["pos_size"]
|
||||
risk_pct = abs(entry - stop) / entry * 100
|
||||
|
||||
# Fetch current price for unrealized PnL
|
||||
cur_price = _get_current_price(cfg, a["ticker"])
|
||||
if cur_price:
|
||||
upnl = pos_size * (cur_price - entry) * pos["direction"]
|
||||
upnl_pct = (cur_price / entry - 1) * 100 * pos["direction"]
|
||||
pnl_icon = "\U0001f7e2" if upnl >= 0 else "\U0001f534"
|
||||
pnl_str = f"\n {pnl_icon} PnL: ${upnl:+,.2f} ({upnl_pct:+.1f}%) @ ${cur_price:,.2f}"
|
||||
else:
|
||||
pnl_str = ""
|
||||
|
||||
lines.append(f" {a['ticker']}: {d}")
|
||||
lines.append(f" Вход: ${entry:,.2f} | Стоп: ${stop:,.2f} ({risk_pct:.1f}%)")
|
||||
lines.append(f" Размер: ${size:,.2f}")
|
||||
lines.append(f" Размер: ${size:,.2f}{pnl_str}")
|
||||
else:
|
||||
lines.append(f"\U0001f4c8 <b>Позиции:</b> нет")
|
||||
lines.append("")
|
||||
@@ -112,9 +143,11 @@ def format_status_report(cfg, conn) -> str:
|
||||
def format_startup_details(cfg, conn) -> str:
|
||||
"""Format startup message with key parameters."""
|
||||
import platform
|
||||
from .state import get_all_assets, get_open_position
|
||||
from .state import get_asset, get_open_position
|
||||
|
||||
assets = get_all_assets(conn)
|
||||
active_tickers = {a.ticker for a in cfg.assets}
|
||||
assets = [get_asset(conn, t) for t in active_tickers]
|
||||
assets = [a for a in assets if a is not None]
|
||||
open_count = sum(1 for a in assets if get_open_position(conn, a["ticker"]))
|
||||
total_equity = sum(a["equity"] for a in assets)
|
||||
total_trades = sum(a["trades_count"] for a in assets)
|
||||
|
||||
Reference in New Issue
Block a user