fix: pre-launch hardening — HYPE price feed, KOL wallet cleanup, Telegram Trump alert, rate limiting, brittle test

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>
This commit is contained in:
k
2026-05-29 11:57:19 +08:00
parent 6471e44aac
commit d6c802ef26
40 changed files with 1833 additions and 209 deletions
+49 -1
View File
@@ -18,13 +18,14 @@ logger = logging.getLogger(__name__)
async def rehydrate_open_trades() -> None:
# Imported locally to avoid circular imports at module load
from app.services.bot_engine import close_and_finalize
from app.services.bot_engine import close_and_finalize, _time_stop_check, _background_tasks
from app.services.crypto import decrypt_api_key
from app.services.signal_categories import (
get_stop_ladder as _get_stop_ladder,
sys2_derisk_ladder as _sys2_derisk_ladder,
sys2_addon_ladder as _sys2_addon_ladder,
sys2_peak_trail as _sys2_peak_trail,
get_exit_profile as _get_exit_profile,
)
from app.services.tp_sl_monitor import register_trade
@@ -183,5 +184,52 @@ async def rehydrate_open_trades() -> None:
_background_tasks.add(task)
task.add_done_callback(_background_tasks.discard)
# ── System-2 time-stop rehydration ──────────────────────────────
# _time_stop_check is a background task created at open time that
# closes the trade if it's still flat (|unrealised| < 2%) after
# `time_stop_hours`. It is NOT stored in DB — it must be rebuilt
# here on every restart or open sys2 trades silently lose this guard.
# btc_bottom_reversal_long has time_stop_hours=None (no time stop).
if _stop_ladder and t.sys2_mode:
_exit_profile = _get_exit_profile(_cat_for_ladders)
ts_hours = _exit_profile.time_stop_hours
if ts_hours:
ts_elapsed_h = elapsed / 3600
ts_remaining_s = max(0.0, ts_hours - ts_elapsed_h) * 3600
if ts_remaining_s > 0:
ts_task = asyncio.create_task(_time_stop_check(
trade_id=t.id,
api_key=api_key,
leverage=trade_leverage,
asset=t.asset,
wallet=t.wallet_address,
delay_seconds=int(ts_remaining_s),
))
_background_tasks.add(ts_task)
ts_task.add_done_callback(_background_tasks.discard)
logger.info(
"Rehydrated time-stop for trade %d: %.1fh remaining",
t.id, ts_remaining_s / 3600,
)
else:
# Time-stop window already elapsed while backend was down.
# Fire it now — close_and_finalize is idempotent (WHERE
# closed_at IS NULL) so a trade that already closed via
# another path is a safe no-op.
logger.info(
"Trade %d time-stop elapsed during downtime — checking now",
t.id,
)
ts_task = asyncio.create_task(_time_stop_check(
trade_id=t.id,
api_key=api_key,
leverage=trade_leverage,
asset=t.asset,
wallet=t.wallet_address,
delay_seconds=0,
))
_background_tasks.add(ts_task)
ts_task.add_done_callback(_background_tasks.discard)
await db.commit()
logger.info("Rehydrated %d open trades.", len(open_trades))