Commit Graph

19 Commits

Author SHA1 Message Date
k b941223c88 fix: complete v5 routing — API exposure, rescore persistence, leverage cap, key backup doc
Three plumbing fixes + one ops doc that close the gaps from the audit.

scripts/rescore_v5.py
  Was overwriting only signal/conf/reasoning/sentiment/relevant/
  prefilter_reason/analysis_version. Now also persists target_asset,
  category, expected_move_pct — without these the bot can't route
  rescored posts correctly (would silently fall back to BTC).

app/schemas.py + app/api/posts.py
  TrumpPost response model didn't expose target_asset/category/
  expected_move_pct, so the frontend had no way to display "this
  signal will trade SOL". Added the three fields + mapping in
  _post_to_schema(). Pre-v5 posts return null. No frontend changes
  yet — display work is a follow-up.

app/services/hyperliquid.py
  HL caps max leverage per asset (BTC/ETH 50×, SOL 20×, memes 3-5×).
  set_leverage() always tried to push self._leverage — if user set
  30× and bot routed to TRUMP, HL rejected the order and the trade
  silently dropped. Added _get_max_leverage() (queries meta()'s
  maxLeverage field) and _clip_leverage() that caps to HL's max.
  set_leverage now returns the effective leverage so callers can
  use it for notional sizing if needed.

deploy/ENCRYPTION_KEY_BACKUP.md
  Documented mandatory backup procedure for the symmetric key that
  encrypts every user's HL API key. Lost key = all users' bots dead
  with no recovery. Includes rotation procedure + quarterly test
  step + things-not-to-do list.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 19:59:30 +08:00
k 5d1bdc1ff2 chore: add $500 capital sweep scripts (conservative + moonshot)
scripts/sweep_500.py:
  Parameter grid for capital-preservation oriented configs.
  Risk filters: max DD <= 30%, max single loss <= 16% of capital.

scripts/sweep_moonshot.py:
  Aggressive grid for one-trade-hits-big strategy.
  Looser DD ceiling (50%), prioritizes biggest single-trade upside.

Both run on the local v4 dataset to inform initial subscription
parameter choices for live trading. Re-run after v5 accumulates
enough signals (~6 weeks) to recalibrate.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 16:07:46 +08:00
k 9872a4cc52 feat(routing): wire AI target_asset end-to-end — bot trades the right perp
Closes the loop on the asset-routing prompt change. Previously the v5
prompt emitted target_asset (e.g. SOL, TRUMP) but bot_engine still
read price_impact_asset and only ever traded BTC/ETH. Now the trade
actually fires on whatever perp the AI picked.

Schema (alembic 006):
  posts.target_asset       (str)   — HL perp ticker, any of the universe
  posts.category           (str)   — 6-class enum (direct_named, etc.)
  posts.expected_move_pct  (float) — AI's 1h move estimate

Wiring:
  truth_social.py persists the three fields when creating Post rows.
  bot_engine.py routing:  asset = target_asset || price_impact_asset || BTC
  Old rows (target_asset=NULL) fall back to legacy BTC/ETH path — no
  retroactive scoring needed; new rows route correctly from now on.

Hyperliquid trader doesn't need changes — `coin` is already a parameter,
and analysis.py validated against HL_PERPS before storing target_asset
so by the time bot_engine reads the field, it's guaranteed tradeable.

Deployment:
  alembic upgrade head    # adds the 3 columns
  Restart api container

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 15:30:41 +08:00
k 8fd9da3c8d feat(ai): asset routing — categorize post → trade the right perp
Why
  Trading BTC on every actionable signal leaves alpha on the table.
  The same Trump post moves different assets very differently:
    • "Strategic Reserve including SOL" → BTC +8%, SOL +33%
    • "Tokenize Treasuries on ETH"      → ETH +5%, BTC +2%
    • "$TRUMP coin is GREATEST"         → TRUMP +50%+, BTC ~0%
  Picking the wrong asset is silent alpha leak.

What changed
  • SYSTEM_PROMPT: new ASSET ROUTING section with 6 named categories
    (direct_named / crypto_policy / macro_risk_on / macro_risk_off /
     defi_thematic / meme_named) + asset-pick rules per category.
  • Few-shot examples now show category + target_asset + expected_move.
  • Output JSON adds: category, target_asset, target_chain,
    expected_move_pct.
  • Python normalization:
    - HL_PERPS whitelist (current Hyperliquid perp universe).
    - CHAIN_FALLBACK map: meme not on HL → trade chain native
      (e.g. $FLOKI on Solana → SOL; ETH-chain meme not on HL → ETH).
    - Safe default → BTC if everything else fails.
    - expected_move_pct < 0.8 → coerce to hold (not worth fees).
    - Legacy `asset` field kept aligned to BTC/ETH for the existing
      price_impact tracker; alts leave it null until tracker is upgraded.

Note
  Bot routing (bot_engine.py) NOT yet updated — it still trades whatever
  `price_impact_asset` is, which is BTC/ETH only. The new fields are
  emitted and stored in DB but consumed downstream in a follow-up.
  This commit is "AI says the right thing" — making the bot ACT on it
  is the next step.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 15:25:27 +08:00
k 5bfc07c7e1 chore: one-shot rescore_v5 migration script
After deploying v5 analysis.py, run this once to overwrite v4 scores in
the DB with v5's interpretation. Idempotent — skips rows already at v5.

Has --dry-run mode to preview the change without AI calls or DB writes.
Live mode prompts for confirmation (skipped if stdin is non-tty so it
also works under `docker exec`).

Touches only AI-derived columns (signal, ai_confidence, ai_reasoning,
sentiment, relevant, prefilter_reason, analysis_version). Leaves all
market-derived columns intact (price_at_post, price_impact_*) — those
stay accurate regardless of which prompt version interpreted the post.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 15:03:45 +08:00
k b5e7c82b51 fix(scheduler): trumpstruth poller never fires — APScheduler bug
next_run_time=None means "paused", not "default". The fallback poller was
registered but never triggered, leaving the system on CNN archive only.
Set explicit start time = now + half the poll interval so the two pollers
offset and don't hit upstream simultaneously.

Verified via /api/health/deep: trumpstruth.last_poll was null on the live
server before this fix despite the poller having been deployed.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 15:00:54 +08:00
k 747158b5ed feat(ai): v5 extreme-alpha prompt — checklist gate + drop sell signal
Why
  v4 was firing buy/short on 13% of posts, but only 9% of those had a
  ≥1% move within the hour. Median move on 'actionable' was 0.298% vs
  0.258% on 'hold' — a 1.15× signal-to-noise ratio (random would be 1.0).
  The model was confabulating transmission chains to please the user
  rather than holding when uncertain.

  Separately: 'sell' meant 'close longs / de-risk' in the prompt but
  was traded as 'open short' by bot_engine.py, producing systematically
  negative results on sell signals (27% win rate vs 57% on real shorts).

What changed
  • analysis.py rewritten as v5-extreme-alpha:
    - Asymmetric error costs framing (false positive = -$30, FN = $0)
    - 7-item checklist that MUST all pass before buy/short
    - Only 4 named transmission paths (a/b/c/d); anything else = HOLD
    - 5 positive + 5 negative few-shot examples
    - UTC hour injected with liquidity context (Asia thin → stricter)
    - Adversarial steelman self-check before final output
    - confidence < 80 + checklist failure both force-collapse to HOLD
      in code, regardless of what the model returns (defense-in-depth)
    - 'sell' removed from output schema entirely
  • bot_engine.py: stop trading 'sell' signals (treat as hold)
  • Case-insensitive normalization on checklist values so model
    returning 'None'/'True' (capitalized) doesn't slip through

Expected impact (to validate over next 2-3 weeks of new posts)
  • actionable rate: 13% → 2-4%
  • signal/hold MFE ratio: 1.15× → 3-5×
  • ≥1% hit rate among actionable: 9% → 40-60%

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 15:00:44 +08:00
k 4ffcb442fe feat: add daily budget, active window, trade snapshots, and price impact monitor
- New migrations for daily_budget, active_window, and bottrade snapshot
- Add trumpstruth scraper and price_impact_monitor service
- Expand bot_engine, hyperliquid, recovery, and tp_sl_monitor logic
- Update API/schemas/models for new features

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-25 16:04:49 +08:00
k a2c68e2939 fix: add missing columns migration (signal, ai_reasoning, leverage, etc.)
- posts: signal, ai_reasoning, prefilter_reason, analysis_version
- subscriptions: leverage, position_size_usd, take_profit_pct, stop_loss_pct, min_confidence

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 20:53:01 +08:00
k 6021a3883d feat: auto-migrate SQLite data to Postgres on first deploy
- Add trumpsignal.db to git (one-time migration source)
- entrypoint.sh detects empty Postgres and runs migration automatically
- scripts/migrate_sqlite_to_postgres.py: posts, subscriptions, bot_trades
- Add psycopg2-binary to requirements for migration script

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 20:48:05 +08:00
k 1deb6a3918 correct healthcheck of database 2026-04-21 20:17:50 +08:00
k 1981b73f1f fix: complete requirements.txt and Docker setup for production deployment
- Add missing packages: openai, aiosqlite, msgpack, websocket-client, bitarray, ckzg
- Pin all versions from working venv (including eth-account 0.13.7, hyperliquid-python-sdk 0.23.0)
- Multi-stage Dockerfile: builder with cmake for ckzg compilation, slim runtime image
- docker-compose: postgres healthcheck with start_period, auto alembic migration on startup
- entrypoint.sh: alembic upgrade head before uvicorn

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 20:12:59 +08:00
k 7f7eecc830 fix database url 2026-04-21 19:58:00 +08:00
k b250bdb4d1 fix with coolify magic env var 2026-04-21 19:55:00 +08:00
k 0782aed4e6 fix: failed to solve: process /bin/sh -c pip install --no-cache-dir -r requirements.txt did not complete successfully 2026-04-21 19:49:47 +08:00
k 3f901df82b aiosqlite, cryptography, hyperliquid-python-sdk 2026-04-21 19:45:49 +08:00
k 3056635a5c for deploy 2026-04-21 19:41:11 +08:00
k 3268080401 done 2026-04-21 19:33:24 +08:00
k 9a72566753 first commit 2026-04-20 23:05:59 +08:00