d6c802ef26
Batch of the pre-launch audit campaign (BUG-01…14 plus three new features): Pricing / TP-SL protection - Add app/services/hl_price_feed.py: supplemental HL allMids poller for HL-native assets (HYPE, PURR) not listed on Binance. Pumps price_store + tp_sl_monitor.on_price_tick so bot trades on these assets keep full stop-loss / take-profit / trailing protection instead of max-hold only. - Wire feed into main.py lifespan (startup task + graceful shutdown cancel). Telegram - Add format_trump_mention + PATH B in _dispatch: crypto-relevant Trump posts with no directional signal (relevant=True, signal=hold) now alert the public channel only (no per-subscriber noise). - Rate limiter (slowapi) on the API; assorted bot/digest fixes. KOL on-chain - seed_kol_wallets.py: KOL_FEEDS coverage cross-check; reversibly deactivate orphaned wallets (handle not in KOL_FEEDS → can never produce divergence) so the scanner stops burning cycles on them. Tests / misc - Fix brittle test_macro_ahr999_uses_same_formula_as_scanner: mock now uses realistic ms timestamps so the in-progress-day drop fires, matching the fetcher's bar count (was 0.3179 vs 0.3178 off-by-one). - Refresh stale notify_signal comment in truth_social.py. Frontend reduce-action type fix lives in the sibling repo. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
88 lines
2.6 KiB
Python
88 lines
2.6 KiB
Python
import logging
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.database import get_db
|
|
from app.models import BotTrade
|
|
from app.schemas import BotPerformance
|
|
from app.services.signed_request import verify_signed_request_any
|
|
|
|
router = APIRouter()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
PERIOD_DAYS = 30
|
|
ACTION_VIEW_PERFORMANCE = "view_performance"
|
|
ACTION_VIEW_USER = "view_user"
|
|
|
|
|
|
@router.get("/performance", response_model=BotPerformance)
|
|
async def get_performance(
|
|
wallet: str = Query(..., description="Wallet address (lower-cased internally)"),
|
|
ts: int = Query(..., description="Signed timestamp (ms)"),
|
|
sig: str = Query(..., description="EIP-191 signature"),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
wallet = wallet.lower().strip()
|
|
verify_signed_request_any(
|
|
actions=[ACTION_VIEW_PERFORMANCE, ACTION_VIEW_USER],
|
|
wallet=wallet,
|
|
timestamp_ms=ts,
|
|
signature=sig,
|
|
body=None,
|
|
allow_replay=True,
|
|
)
|
|
since = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=PERIOD_DAYS)
|
|
|
|
result = await db.execute(
|
|
select(BotTrade)
|
|
.where(BotTrade.wallet_address == wallet)
|
|
.where(BotTrade.closed_at.is_not(None))
|
|
.where(BotTrade.opened_at >= since)
|
|
.order_by(BotTrade.opened_at.asc())
|
|
)
|
|
trades = result.scalars().all()
|
|
|
|
total_trades = len(trades)
|
|
if total_trades == 0:
|
|
return BotPerformance(
|
|
period_days=PERIOD_DAYS,
|
|
total_trades=0,
|
|
win_rate=0.0,
|
|
net_pnl_usd=0.0,
|
|
avg_hold_seconds=0.0,
|
|
max_drawdown_pct=0.0,
|
|
)
|
|
|
|
winning = sum(1 for t in trades if (t.pnl_usd or 0) > 0)
|
|
win_rate = winning / total_trades
|
|
|
|
pnl_values = [(t.pnl_usd or 0.0) for t in trades]
|
|
net_pnl = sum(pnl_values)
|
|
|
|
hold_values = [(t.hold_seconds or 0) for t in trades]
|
|
avg_hold = sum(hold_values) / len(hold_values)
|
|
|
|
# Max drawdown: running peak → trough of cumulative PnL
|
|
cumulative = 0.0
|
|
peak = 0.0
|
|
max_drawdown = 0.0
|
|
for pnl in pnl_values:
|
|
cumulative += pnl
|
|
if cumulative > peak:
|
|
peak = cumulative
|
|
drawdown = (peak - cumulative) / peak * 100 if peak > 0 else 0.0
|
|
if drawdown > max_drawdown:
|
|
max_drawdown = drawdown
|
|
|
|
return BotPerformance(
|
|
period_days=PERIOD_DAYS,
|
|
total_trades=total_trades,
|
|
win_rate=round(win_rate, 4),
|
|
net_pnl_usd=round(net_pnl, 2),
|
|
avg_hold_seconds=round(avg_hold, 1),
|
|
max_drawdown_pct=round(max_drawdown, 4),
|
|
)
|