trading-intelligence-platform
暂无简介
- License 许可
- Apache-2.0
- 最近更新
- 2026-10-24
- Stars
- ★ 0
- Forks
- 0
02 资源文件 1
- 参考文档skill.json409 B
03 SKILL.md 正文
trading-intelligence-platform
name: trading-intelligence-platform description: "Build trading intelligence platforms with canonical data."
Trading Intelligence Platform
Quantitative research organization with 12 rooms, 24 agents, SSE dashboard, Telegram signal ingestion, and a supervised 24/7 worker fleet.
Architecture Overview
The CortexQuant Trading Intelligence Platform is a persistent, self-healing system built on:
- FastAPI + uvicorn for the REST API (port 8080)
- systemd for service management (auto-start on boot, restart on failure)
- WorkerSupervisor for independent worker lifecycle (heartbeat, retry, escalation)
- Telethon MTProto for Telegram channel ingestion (CKPscanner)
- BingX VST demo for order execution
- PostgreSQL for all persistent state
24/7 Worker Fleet
The platform runs 19 supervised workers, each with its own asyncio task, heartbeat tracking, and exponential backoff. Workers are defined in src/workers/jobs.py and managed by src/workers/supervisor.py.
Worker Groups
| Group | Workers | Cadence |
|---|---|---|
| core | dashboard_feed, monitor | 5s–60s |
| trading | demo_reconcile | 30s |
| ingestion | tg_ingestion | 120s |
| research | market_data, hypothesis, walkforward, strategy_version, backtest_adapter | 120s–600s |
| gates | red_team, risk_committee | 240s |
| rooms | room_tasks | 45s |
| ml | ml_predict | 300s |
| quantum | quantum_optimize | 600s |
| portfolio | portfolio_optimize | 300s |
Failure Classification
Each worker failure is classified into one of four categories with different retry policies:
| Class | Meaning | Backoff Multiplier |
|---|---|---|
| TRANSIENT | Network, timeout, rate limit | 1.0x |
| RESOURCE | Memory, disk pressure | 4.0x |
| UNKNOWN | Unclassified error | 1.5x |
| PERMANENT | Bad config, missing column | 3.0x (no retry) |
PERMANENT failures do not retry — they escalate to the human.
Escalation
After 3 consecutive failures, a worker enters ESCALATED state. The monitor worker sends alerts (Telegram bot, WorkerEvent). Maximum escalation level is 3.
Heartbeat Staleness
A worker is STALE if no heartbeat for max(60s, interval * 3). The monitor worker tracks stale workers and raises CRITICAL alerts.
Startup Recovery Procedure
On service startup, the system must recover full state before processing any new signals. The recovery sequence is:
- Database schema —
init_db()creates all tables if missing - Experiment state — load last
ExperimentRunfrom DB - Exchange reconciliation —
exchange.fetch_positions()→ sync todemo_trader.positions→ createDemoTradeDB rows for any positions not tracked - Telegram reconnect — Telethon auto-reconnects;
tg_ingestion_workermonitors liveness - Worker fleet start —
supervisor.start_all()launches all 19 workers - Publisher callback —
tg_publisher.set_demo_callback(orchestrator.process_signal)wires signal routing
Critical: Without reconciliation, the internal position state is empty after restart, and the duplicate-position check cannot see existing BTC/AVAX positions — leading to duplicate orders.
Signal Freshness Gate
Every Scout signal must satisfy ALL gates before execution:
| Gate | Check | Rejection Reason |
|---|---|---|
| Freshness | Signal timestamp within 5 minutes | stale_signal |
| Market | Symbol exists on BingX as BASE/USDT:USDT or BASE/USDT |
no_market |
| ML | Model is trained OR ML_SKIP_VALIDATION=true is set |
ml_gate |
| Risk | RiskEngine.check_signal() passes with real risk/size values |
risk |
| Idempotency | No existing DecisionRecord for this signal_id |
duplicate |
| Duplicate | Symbol not already in demo_trader.positions |
duplicate_position |
| Concurrent | Open positions < MAX_CONCURRENT_TRADES |
max_concurrent_trades |
Pipeline Must Pass Real Values to Risk Engine
BUG: The original pipeline hardcoded risk_pct=2.0, position_size_pct=2.0 instead of reading from signal or settings. This caused every signal to be rejected when PER_TRADE_RISK_PCT=5.0.
Fix: In FullPipeline.process_signal(), the risk check must use:
risk_result = self.risk.check_signal(
{"position_size_pct": signal.get("position_size_pct", settings.MAX_POSITION_SIZE_PCT),
"risk_pct": signal.get("risk_pct", settings.PER_TRADE_RISK_PCT)},
portfolio_state,
)
Exposure Management
Emergency Rebalancing
When exchange positions exceed MAX_CONCURRENT_TRADES or exposure exceeds safe thresholds, the system must:
- Identify positions to keep (BTC, AVAX, and other deliberate positions)
- Close all other positions via BingX market orders
- Mark closed positions in DB with
exit_reason='EMERGENCY_REBALANCE' - Verify exposure returns to safe levels (< 25%)
Command pattern for closing positions:
# Fetch all positions from exchange
positions = await exchange.fetch_positions()
for pos in positions:
symbol = pos['symbol'].replace('/USDT', '').replace(':USDT', '')
if symbol not in KEEP:
close_side = 'sell' if pos['side'] == 'long' else 'buy'
await exchange.create_order(symbol=pos['symbol'], type='market',
side=close_side, amount=pos['contracts'],
params={'positionSide': 'LONG', 'leverage': 3})
Meme-Coin Inflation
CKPscanner Scout messages can produce positions in low-value meme coins (BIKETYSON, FLYBRAIN, PIPEDOG, etc.). These positions accumulate rapidly because the pipeline executes every Scout signal without concentration limits beyond MAX_CONCURRENT_TRADES.
Mitigation:
- Regular exposure checks (every reconcile cycle)
- Emergency rebalancing when exposure exceeds threshold
- Do not trust
demo_trader.positionsalone — always reconcile withexchange.fetch_positions()for ground truth
Systemd Service Configuration
Service File
[Unit]
Description=CortexQuant Trading Intelligence Platform
After=network.target postgresql.service redis-server.service
[Service]
Type=simple
WorkingDirectory=/opt/cortexquant
ExecStart=/opt/cortexquant/venv/bin/python /opt/cortexquant/main.py
Restart=on-failure
RestartSec=5
User=root
[Install]
WantedBy=multi-user.target
Environment Drop-in
systemctl edit fails without a TTY. Use the drop-in directory directly:
# Create drop-in to set environment variables:
mkdir -p /etc/systemd/system/cortexquant.service.d
cat > /etc/systemd/system/cortexquant.service.d/ml-skip.conf << 'EOF'
[Service]
Environment=ML_SKIP_VALIDATION=true
EOF
systemctl daemon-reload
systemctl restart cortexquant
Auto-Start on Boot
systemctl enable cortexquant # Enable auto-start
systemctl is-enabled cortexquant # Verify
Restart Throttle Trap
After ~5–8 rapid restart failures, systemd rate-limits further restarts. Reset before retrying:
systemctl reset-failed cortexquant && systemctl restart cortexquant
Health Verification
Health Endpoint
The /api/system/health endpoint returns a comprehensive health snapshot:
- Worker fleet status (healthy/stale/escalated counts)
- Last signal (id, symbol, direction, confidence, timestamp)
- Last trade (id, symbol, direction, status, timestamp)
- Open positions (count and symbols)
- Experiment status (id, day, start_time)
- Model version and approval status
- Recent signals count (last hour)
Verify Without SSH
# From any machine on the network:
curl http://192.168.1.19:8080/api/system/health
curl http://192.168.1.19:8080/api/workers/health
curl http://192.168.1.19:8080/api/demo/status
curl http://192.168.1.19:8080/api/tg/health
Worker Persistence Schema
All worker state is persisted in PostgreSQL. Key tables:
| Table | Purpose |
|---|---|
worker_states |
Live heartbeat, status, counters per worker |
worker_runs |
One row per execution (success or failure) |
worker_events |
Escalations, retries, recoveries |
The WorkerSupervisor.snapshot() method returns live status including staleness computed from heartbeat age.
Post-Trade Learning
After every completed trade, the system:
- Persists full
DecisionRecordwith all pipeline stages - Classifies errors into categories (prediction_error, execution_error, risk_stop, profit_target, funding_cost, slippage_cost)
- Updates the learning dataset
- Generates improvement hypotheses (stored in
research_memory) - Hypotheses must pass walk-forward validation before promotion
- Promotion requires human approval — never auto-promote
Testing Recovery Scenarios
Reboot Recovery
systemctl enable cortexquant— service starts on boot- On startup,
demo_lifecycle.start_demo_experiment()runs reconciliation - All workers restart with fresh heartbeats
Worker Crash Recovery
supervisor._loop()catches the exception- Failure is classified and recorded in
worker_runs - Exponential backoff (TRANSIENT: 1x, RESOURCE: 4x, UNKNOWN: 1.5x)
- After
max_retries, enters DEGRADED or ESCALATED state - Monitor worker sends alert
Telegram Disconnect
- Telethon auto-reconnects (built-in)
tg_ingestion_workerchecks heartbeat age every 120s- If no heartbeat for 5 minutes, reports
connected: false - Monitor worker raises CRITICAL alert
Exchange API Failure
classify_error()catches CCXT exceptions- TRANSIENT errors (timeout, rate limit) trigger retry with backoff
- PERMANENT errors (auth failure, bad symbol) escalate to human
- After reconciliation, positions are restored from exchange
Duplicate Signal Handling
DecisionRecord.signal_idis UNIQUE_check_idempotency()returns existing record before processing- Signals marked
published_to_bus=Trueare skipped on reprocessing
Position Reconciliation
exchange.fetch_positions()returns ground truth- Internal
demo_trader.positionsis rebuilt from exchange data - Missing DB rows are created; stale rows are closed
- Idempotency key:
symbol+signal_id
Silent Bug Patterns
Pipeline Risk Hardcoding
Symptom: Every signal rejected with "Per-trade risk exceeds limit"
Cause: Pipeline submits risk_pct=2.0 but settings.PER_TRADE_RISK_PCT=5.0
Fix: Read from signal or settings dynamically
Missing Freshness Check
Symptom: Stale signals from days ago are executed
Cause: No timestamp validation before execution
Fix: Check (now - signal_timestamp).total_seconds() < 300 before execution
Missing Market Check
Symptom: Signal for symbol not on BingX is executed, causing order failure
Cause: No verification that BASE/USDT:USDT exists in exchange.symbols
Fix: Check market existence before risk check
Meme-Coin Accumulation
Symptom: 19+ positions with >200% exposure Cause: Every Scout signal executes without concentration limits Fix: Emergency rebalancing + exposure monitoring
In-Memory State Loss
Symptom: After restart, demo_trader.positions is empty, duplicate orders placed
Cause: Positions only stored in memory, not persisted
Fix: Reconciliation from exchange.fetch_positions() on startup
Deployment to CT106
See references/cortexquant-ct106-deployment.md for the full deployment procedure.
Production Audit
To audit a running CortexQuant install without modifying it, follow
references/production-audit.md — the read-only method, evidence standard,
and report shape for this system. Use it before any code changes.
References
| File | Purpose |
|---|---|
references/cortexquant-ct106-deployment.md |
CT106 directory layout, env loading, startup lifecycle, restart, verification checklist, agent-host→CT106 access recipe |
references/production-audit.md |
Read-only audit method, evidence standard, and report shape |
references/signal-pipeline.md |
Stage-by-stage pipeline contract and decision-record schema |
references/signal-parser-maintenance.md |
How to tell "parser broken" from "feed empty" when auditing ingestion |
references/quant-hq-orchestration.md |
Monitoring loop: decay → HMM → DSR → permission graph |
references/demo-trading-experiment.md |
Demo trader wiring, VST balance, live-with-demo tradeoffs |
references/lxc-operations.md |
Proxmox LXC file transfer, inspection, and debugging recipes |
User Workflow Preferences (Always-On)
- Do not trade stale signals. Scout signals must pass the 5-minute freshness gate.
- Funding-outlier messages are a separate signal class. Store raw, do NOT parse as Scout.
- Never convert meme-coin positions into "features". Close them via BingX API.
- Reconcile on every startup. Never start the experiment clock before reconciliation.
- Verify from PVE host, not localhost. A CT can serve HTTP 200 on
127.0.0.1but be unreachable from the network. - Use systemd, not pkill+nohup.
systemctl restart cortexquantis the correct restart method. - Use
pct exec <CT> -- bash -c "...", notpct enter.pct enterdoesn't work well withbash -cand long commands. - Use drop-in files for env vars.
systemctl editfails without a TTY. - asyncpg connections don't have
.commit(). Use SQLAlchemy or directexecute()for writes. - Report exact details, not summaries. Signal_id, symbol, direction, risk decision, order params, fill, P&L.
Rooms (12)
| Room | Purpose |
|---|---|
| QUANT_RESEARCH | Hypothesis generation |
| WEIRD_EDGE | Anomalous edge detection |
| MATH_SIGNAL | Mathematical analysis |
| STRATEGY_EG | Strategy engineering |
| REGIME_DETECTION | HMM regime identification |
| BACKTEST | Historical backtesting |
| OPTIMIZATION | Parameter optimization |
| SELECTION | Portfolio selection |
| RED_TEAM | Risk review, veto |
| TRADING_JOURNAL | Performance logging |
| AI_COACH | Agent quality analysis |
| RISK_COMMITTEE | Risk oversight |
Pipeline (Loop, not Linear)
Based on the RuujSs alpha engine article: Discovery → Validation → Deployment → Monitoring → Discovery.
Monitoring feeds decay alerts and regime divergence back to research, closing the loop.
Agents (24)
2 agents per room. Registered in research_agents table via initialize().
Monitoring Layer (NEW)
Decay detection, HMM regime, Deflated Sharpe Ratio, and permission graph.
See references/quant-hq-orchestration.md for details.
Permission Graph
Enforces write/read access per room at the data level. Prevents research from writing APPROVED, etc.
Monitoring API
POST /api/quant-hq/monitoring/check/{id}— full check (decay + regime + DSR)POST /api/quant-hq/monitoring/decay-check— rolling Sharpe decayPOST /api/quant-hq/monitoring/regime— HMM regimePOST /api/quant-hq/monitoring/dsr— Deflated Sharpe RatioGET /api/quant-hq/monitoring/permissions-matrix— full permission matrix
External Signal Ingestion
Telegram Channels (Telethon MTProto)
- Session at
.tgsessions/ckp_scanner.session(gitignored) - Parser extracts SYMBOL, DIRECTION, RSI15/4H, TSI15/4H, funding, divergence
- Signals create ALTCOIN_SIGNAL_RECEIVED events → 10 rooms
POST /api/tg/reparseto re-run parser on all messages
Signal Classes
CKPscanner emits two distinct signal classes that must NOT be mixed:
| Class | Pattern | Action |
|---|---|---|
| Scout Opportunity | 🔴/🟢 Scout Opportunities LONG/SHORT + numbered `SYMBOL-USDT |
RSI...` list |
| Funding Outlier | Funding Alert - Outliers: + SYMBOL: funding=X% |
Separate future signal class — store raw, do NOT auto-convert to Scout |
The Scout parser's header regex (🔴\s*Scout\s+Opportunities) deliberately rejects funding-outlier messages. If you need a funding-outlier parser, build a separate FundingOutlierParser — never shim outlier detection into the Scout path.
Deduplication
Signals are deduplicated by dedup_key = f"{channel_id}:{telegram_message_id}:{symbol}". A parsed signal that fails validation is NOT marked published_to_bus=True — it will be re-attempted on the next publisher tick. Only mark published after successful routing to rooms AND demo execution (or explicit rejection with a recorded reason).
Operations Console
- SSE at
/api/quant-hq/events(heartbeat + audit_log) - State at
/api/quant-hq/state - UI at
/console
12-Step Alpha Pipeline
Before a signal is eligible for execution, it must pass:
- Linear demeaning (remove drift)
- Variance estimation (realized vol)
- Return normalization (z-score)
- Time-series truncation (observation window)
- Cross-sectional demeaning (remove market bias)
- Final trimming (time horizon)
- Expected return estimation (forward-looking)
- Return normalization (vol-scaled)
- Residual extraction (factor orthogonalization)
- Signal weighting (inverse volatility)
- Weight normalization (|w| = 1)
- Combined signal construction
Implemented in src/quant_hq/alpha_pipeline.py.
Full Signal-to-Execution Pipeline
The pipeline wires: Telegram parsing → market snapshot → ML prediction → strategy selection → classical optimization → quantum optimization → risk check → BingX execution → decision record → post-trade analytics.
Architecture
tg_publisher → FullPipeline.process_signal(signal_dict)
├─ idempotency check (DecisionRecord.by signal_id)
├─ market snapshot (OHLCV from Binance/BingX)
├─ ML prediction (MLPredictor)
├─ strategy selection (StrategyEngine)
├─ classical optimization (RiskfolioAdapter)
├─ quantum optimization (QuantumSolver)
├─ risk check (RiskEngine)
├─ duplicate position check
├─ BingX VST execution (demo_trader.process_signal)
└─ DecisionRecord persisted to database
DecisionRecord Schema
The decision_records table captures every signal's complete journey:
signal_id(unique, idempotency key)signal_parsed,market_snapshot,ml_prediction,ml_confidencestrategy_selected,strategy_paramsclassical_optimizer_output,quantum_optimizer_outputrisk_decision(approved/rejected),risk_reasonorder_params,exchange_order_id,fill_price,fill_quantityfees,funding,slippageexit_price,exit_reason,gross_pnl,net_pnlstatus(pending, executed, closed, rejected, error)error_classification,market_regime
Idempotency Pattern
Always check for existing DecisionRecord before processing. A signal that was rejected yesterday must not be re-processed on restart:
existing = await self._check_idempotency(signal_id)
if existing:
return {"status": "duplicate", "decision_id": existing.id}
API Routes
GET /api/pipeline/decisions— list recent decisionsGET /api/pipeline/decisions/{signal_id}— full decision chainGET /api/pipeline/stats— execution statistics
Reconciliation Procedure (On Startup)
When the demo service restarts, internal position state is lost but BingX retains the real positions. Always reconcile before starting the background task:
# Fetch exchange positions
positions = await demo_trader.adapter.get_positions()
# Sync to internal state
for pos in positions:
symbol = pos['symbol'].replace('/USDT', '').replace(':USDT', '')
if symbol not in demo_trader.positions:
demo_trader.positions[symbol] = {
'exchange_order_id': pos['id'],
'direction': pos['side'],
'entry_price': pos['entryPrice'],
'size': pos['contracts'],
'stop_loss': None, # BingX demo may not expose
'take_profit': None,
}
Add this to start_demo() before asyncio.create_task(_run_experiment()).
Risk Config Mismatch Pitfall
Pipeline submits risk_pct=2.0 but settings.PER_TRADE_RISK_PCT=5.0%. Result: every signal is rejected with "Per-trade risk exceeds limit".
Fix: align the pipeline's risk submission with settings.PER_TRADE_RISK_PCT, or override via signal.get('risk_pct', settings.PER_TRADE_RISK_PCT).
Silent bug patterns (parser + risk)
- Loop-variable name collision. A parser that assigns to
parsed_resultsthen loopsfor p in parsed:will NameError on the first successful parse. Grep the loop target against the assignment target whenever you touch a parse-then-iterate block — the two names drifting apart is the bug, not the parse logic. - Scout message leading indentation. CKPscanner list items are indented 2+ spaces (
1. ALCH-USDT | ...). A line pattern anchored at column 0 (^(\d+)) misses every item. Anchor with\s*(\d+)or.lstrip()the line before matching. - Risk position cap must not bind below the target.
PER_TRADE_RISK_PCT=5.0with a 2% stop requires 250% position size to hit — ifMAX_POSITION_SIZE_PCT=5.0, the cap binds first and actual risk is 1%. SetMAX_POSITION_SIZE_PCThigh enough that the cap only triggers on genuine concentration risk, not on the risk-target math. - Demo RiskEngine refuses LIVE_TRADING. The demo
RiskEnginereadsPER_TRADE_RISK_PCT,MAX_POSITION_SIZE_PCT, etc. — limits calibrated for VST. It MUST refuse to operate whensettings.LIVE_TRADING=Trueandsettings.DEMO_TRADING=False(raise in__init__, reject incheck_signal). Never let demo risk limits gate a live trade.
Dashboard Panel Append Pattern
To add panels to fleet.html without breaking the existing layout:
- Append new
<div class="panel" style="flex:X">elements before</main> - Match existing
.ptitle,.body,.panelCSS classes - Do NOT rewrite the file wholesale — patch in place
- SSE panels must reference real API endpoints, not placeholders
CKP Signal Format
Real parsed signals from CKPscanner have this structure:
{
"signal_id": "CKP-676EB7F29132",
"symbol": "ALIGN",
"direction": "LONG",
"confidence": 1.0,
"timestamp": "2026-09-12T21:35:12+00:00",
"parser_version": "2.0.0",
"rsi": None,
"indicators": {
"rsi15": 61.4,
"tsi15": 13.2,
"rsi4h": 42.31,
"tsi4h": -33.99,
"bull_divergence": 0.094
}
}
Note: rsi is top-level; other indicators live in indicators dict. Always use .get("rsi") or 50 for fallback (key may exist with None value).
Pipeline Test Result Interpretation
A "rejected" status is correct behavior, not a bug. The pipeline is working as designed when it rejects:
- Low-cap tokens not on Binance (ALIGN/USDT not found)
- Signals exceeding risk limits
- Signals with stale timestamps
- Signals for symbols with existing positions (duplicate check)
Do not "fix" a rejection — verify the reason is correct. Only investigate if a clearly valid signal is rejected.
Adapter Isolation Pattern
Each optional adapter MUST be loaded lazily with its own try/except:
try:
from src.ml.predictor import MLPredictor
self.ml = MLPredictor()
self.has_ml = True
except Exception as e:
logger.warning(f"ML adapter unavailable: {e}")
One failing adapter must not crash the pipeline. Log and continue with degraded capability.
Database Tables
| Table | Purpose |
|---|---|
research_experiments |
Core experiment data |
research_agents |
Agent registry |
research_messages |
Inter-agent messages |
research_tasks |
Task queue |
research_audit_log |
Immutable audit trail |
research_strategies |
Strategy versions |
research_budget |
Budget tracking |
research_memory |
Shared persistent memory |
research_promotions |
Promotion history + vetos |
tg_raw_messages |
Raw Telegram messages (immutable) |
tg_parsed_signals |
Parsed signals |
tg_backfill_checkpoints |
Backfill progress |
tg_ingestion_metrics |
Channel health |
demo_trades |
Demo trade records |
experiment_runs |
7-day experiment metadata |
signal_validations |
12-step results |
execution_errors |
Error log |
Container Layout (Proxmox ninyi)
| CT | Name | IP | Purpose |
|---|---|---|---|
| 100 | adguard | 192.168.1.10 | DNS ad blocking |
| 101 | nextcloud | 192.168.1.10 | File sync (NOTE: same IP as adguard — conflict) |
| 102 | plex | dhcp | Media server |
| 103 | hermes | 192.168.1.13 | Hermes Agent host |
| 104 | odysseus | dhcp | |
| 105 | cortexcloud | 192.168.1.15 | AI gateway, x402 payments |
| 106 | cortexquant | 192.168.1.19 | Trading Intelligence Platform |
| 107 | n8n | stopped | Workflow automation |
| 108 | network-optimizer | 192.168.1.17 | UniFi Network Optimizer |
Access Pattern (Agent Host → CT106)
The agent host (hermes@192.168.1.13) cannot route directly to CT106 (192.168.1.19) — different subnet. The only path is via the PVE host:
# Execute command inside CT106 from agent host:
ssh -o StrictHostKeyChecking=no root@192.168.1.11 'pct exec 106 -- bash -c "<command>"'
# Pipe a script into CT106:
cat /tmp/script.py | ssh root@192.168.1.11 'pct exec 106 -- bash -s'
# Install Python deps (system pip blocked by PEP 668):
ssh root@192.168.1.11 'pct exec 106 -- bash -c "/opt/cortexquant/.venv/bin/pip install <pkg>"'
Systemd Service Drop-in (bypass ML gate)
systemctl edit fails without a TTY. Use the drop-in directory directly:
# Create drop-in to bypass ML validation gate (demo only):
ssh root@192.168.1.11 'pct exec 106 -- bash -c "
mkdir -p /etc/systemd/system/cortexquant.service.d
echo -e \"[Service]\\nEnvironment=ML_SKIP_VALIDATION=true\" > /etc/systemd/system/cortexquant.service.d/ml-skip.conf
systemctl daemon-reload
systemctl restart cortexquant
"'
The MLPredictor.is_approved property checks os.environ.get("ML_SKIP_VALIDATION") and returns True when set. This lets the strategy engine use momentum_ma instead of ckp_baseline even when the model fails validation gates (R² < 0, etc.).
Static IP Assignment
# Pin static IP (avoid DHCP drift)
ssh pve 'pct set <CTID> -net0 "name=eth0,bridge=vmbr0,hwaddr=<mac>,ip=192.168.1.X/24,gw=192.168.1.1,type=veth"'
ssh pve 'pct restart <CTID>'
DHCP drift is silent — CT keeps old IP until reboot, then gets a new one. Always pin static IPs for externally-reachable services.
Deployment
CT106 directory layout, env loading pattern, restart procedure: see references/cortexquant-ct106-deployment.md
Loop architecture (decay → HMM → DSR → permission graph): see references/quant-hq-orchestration.md
Process management, file transfer, debugging: see references/lxc-operations.md
7-day experiment pre-flight, ML training, signal flow decision tree: see references/demo-experiment-runner.md
LIVE_TRADING=false Always
Research agents isolated from execution credentials. No trades placed.
Risk Configuration (2026-09 update)
| Setting | Old | New | Why |
|---|---|---|---|
PER_TRADE_RISK_PCT |
2.0 | 5.0 | User-requested 5% per-trade risk target |
MAX_POSITION_SIZE_PCT |
5.0 | 50.0 | Old cap bound before risk target reached (2% stop needs 250% position to hit 5% loss); new cap only triggers on genuine concentration risk |
The RiskEngine guards LIVE_TRADING mode: raises in __init__ and rejects in check_signal when LIVE_TRADING=True and DEMO_TRADING=False. Never remove this guard.
Reconciliation Overflow Pitfall
The startup reconciliation script creates DemoTrade DB rows for every open exchange position. If reconciliation runs when more than MAX_CONCURRENT_TRADES positions are open on the exchange (e.g., 5 positions vs 3 limit), the demo trader immediately hits Max concurrent trades reached and rejects every fresh Scout signal — even though some pre-existing positions are desirable (BTC, AVAX) and others are noise (BAS, ARC, EGLD).
Fix: reconciliation must count existing positions against MAX_CONCURRENT_TRADES and only mark the oldest N as open; close (or skip) the rest. Or, explicitly close stale reconciliation artifacts via BingX API before starting the experiment clock.
Training on CT106 (lessons from 2026-09)
- System pip is blocked by PEP 668 — always use
/opt/cortexquant/.venv/bin/pip - A training script that imports
src.*pulls insrc.utils.config→pydantic-settings,src.utils.logger→python-json-logger. These must be in the venv. - Simpler: write a standalone script with no
src.*imports (onlyccxt,sklearn,pandas,numpy,joblib). Pipe it in and run with the venv Python. GradientBoostingRegressoron 365-day 1h OHLCV for BTC/ETH/SOL/AVAX yields R² ≈ -0.04 — no predictive power. The model is saved but fails validation. UseML_SKIP_VALIDATION=trueto bypass the gate for demo purposes, or improve features/model before relying on ML signals.
Dashboard Deployment Safety
- Verify from PVE host, not localhost. A CT can serve HTTP 200 on
127.0.0.1but be unreachable from the PVE host due to stale ARP tables, wrong subnet, or firewall. Alwaysssh pve 'curl http://<CT_IP>:8080/health'after a dashboard change. - Restart via systemd, not pkill+nohup. Use
pct exec <CT> -- systemctl restart cortexquant.pkill -9+nohupbypasses systemd and can leave the service in a zombie state where restart throttling kicks in. - Clear
__pycache__after HTML/template changes. uvicorn with--reloadmay pick up file changes, but a running uvicorn without reload will serve stale cached templates. Restart + clear cache:find /opt/cortexquant -name __pycache__ -type d -exec rm -rf {} +then restart. - Rebuild-and-deploy without preview = broken dashboard. The reference-images workflow (rebuild from screenshots) is high-risk: a subagent can drop 2–5KB of existing functionality, change the file size, and break the live site. Always diff the rebuilt file against HEAD before deploying, and verify the deployed result reaches the PVE host before telling the user it's done.
- A subagent-built full HTML replacement is a delete-by-another-name. Replacing
fleet.htmlwholesale via a subagent (even from reference images) has broken the live dashboard multiple times — the server dies, the user reports 'Nothing is up' or 'unreachable', and the fix isgit checkout -- dashboard/templates/fleet.html+ systemd restart. The safe pattern is to PATCH the existing file (append new panels before</main>, add CSS in the existing<style>block) rather than rewrite it. If a full rebuild is unavoidable, the subagent MUST: (1) preserve every existing CSS class and JS function, (2) keep the existing SSE/auth flow intact, (3) diff against HEAD to verify nothing was dropped, (4) push to a staging path first and verify HTTP 200 from PVE host before swapping in.
Pitfalls
Background Task Startup Ordering (uvicorn await server.serve() blocks forever)
- In FastAPI with
uvicorn.Server.serve(), any code AFTERawait server.serve()never executes - Background tasks (demo experiment, Telegram scheduler) MUST be started BEFORE
await server.serve() - Pattern:
demo_task = asyncio.create_task(supervised_demo_task())beforeawait server.serve() - The task reference MUST be retained for shutdown: cancel it in
shutdown_eventhandler - If demo startup code runs after
serve(), it will silently never start — no error, just no demo
BingX API Secret Required
- BingX requires both API key AND secret for HMAC-SHA256 signing
- Without secret → CCXT
AuthenticationError: bingx requires \"apiKey\" credential(misleading — apiKey IS set) - Always verify both present before
BingXDemoAdapter.initialize():if not api_key or not secret: raise RuntimeError(...)
Demo Orchestrator is_active Bug
DemoExperimentOrchestrator.start()setsself.is_running = Truebutdemo_trader.is_activestays False- The
demo_trader.start_experiment()is called but doesn't setdemo_trader.is_active - Fix: ensure
demo_trader.is_active = Trueafterstart_experiment()
Publisher→Demo Bridge Gap
tg_publisher.publish_pending()routes signals to research rooms, NOT todemo_trader.process_signal()- To wire demo execution: after routing to rooms, also call
await demo_trader.process_signal(signal)for each parsed signal - Without this bridge, demo trader never receives signals
- Register the callback BEFORE
server.serve()blocks:tg_pub.set_demo_callback(demo_trader.process_signal)
CCXT BingX Sandbox Mode
- Must call
exchange.set_sandbox_mode(True)after initialization - Without this, orders go to production (real money)
- CCXT bingx has no
sandboxModeattribute — usegetattr(exchange, 'sandboxMode', 'N/A')to avoid AttributeError - Verify with
exchange.urls['test']vsexchange.urls['api']
BingX Demo Balance is VST (not USDT)
- BingX VST demo accounts use VST (Virtual Stable Token), not USDT
- Demo balance shows in
balance.get("VST", {})— NOTbalance.get("USDT", {}) - A 0 USDT balance does NOT mean the account is unfunded — always check VST first
- Verify with raw CCXT:
fetch_balance()then inspectbal.get("VST", {}).get("total", 0) - The demo API endpoint must explicitly surface VST — if it only queries USDT, it will report 0 for a fully-funded demo account
Position Sizing (DEMO only)
- Risk-driven size:
position_size = (balance * PER_TRADE_RISK_PCT / 100) / |entry - stop_loss| - Hard cap:
position_size <= balance * MAX_POSITION_SIZE_PCT / 100 / entry - Take the MIN of the two. For a 2% stop, the cap binds first (1% actual risk); for a 10%+ stop, the risk target binds.
- Final size is then multiplied by leverage for notional exposure.
- Notional = position_size * price / leverage (actual capital at risk)
- DEMO ONLY: This formula uses
settings.PER_TRADE_RISK_PCT(5%) andsettings.MAX_POSITION_SIZE_PCT(50%) — VST-calibrated.RiskEnginerefuses to operate inLIVE_TRADINGmode. For live trading, a separateLiveRiskEnginewith live-calibrated limits is required. - Never use
LIVE_TRADINGflag for position sizing — it's always False; useDEMO_TRADINGfor demo mode
.get(key, default) with None values
signal.get("rsi", 50)returnsNoneifsignal["rsi"]isNone(key exists, value is null)- Use
signal.get("rsi") or 50to fall back when value is falsy - Common in JSON data from parsers — fields exist but are null
User Workflow Preferences (Always-On)
These are standing rules the user explicitly stated. They apply to every session.
- Do not increase risk limits to force a trade. If
PER_TRADE_RISK_PCT=1.0%and the pipeline submits 2.0%, the rejection is correct behavior. Record the mismatch, add a regression test, and stop. Never silently modify either value to make a trade go through. - Treat rejections as integration tests. A
"rejected"status is a successful pipeline execution, not a bug. Verify the reason is correct. Only investigate if a clearly valid signal (major market, fresh timestamp, no duplicate, risk within limits) is rejected. - Fix blockers in this order: (1) make risk authoritative, (2) replace hardcoded Binance with BingX market resolver, (3) implement
StrategyEngine.select_strategy(), (4) put ML in observation-only mode, (5) add synthetic e2e test mode, (6) process one real signal only when fully validated. - Label momentum_ma as a fallback, not an AI decision. When ML is untrained or confidence < 0.6, the strategy MUST be labeled
"fallback"with"reason": "ml_untrained"or"low_confidence". Never present a default strategy as an AI-selected one. - Never treat default 0.5 confidence as a validated prediction. Untrained ML models MUST return
{"confidence": 0.5, "observation_only": True}. The pipeline MUST checkobservation_onlybefore using any ML output for trading decisions. - Synthetic e2e test mode is mandatory before any real trade. Implement a
test_e2e()method that runs the full pipeline WITHOUT placing orders. Verify: market resolution, ML handling, strategy selection, optimizer handling, risk approval, idempotency, persistence, and dashboard tracing. Run this first. - Never allow an LLM to directly rewrite production trading code, bypass risk controls, or promote itself. Improvement hypotheses must be evaluated through chronological walk-forward validation against the current model before promotion. The LLM proposes; the pipeline disposes.
- Process a real signal ONLY when ALL gates pass: exact market exists on BingX, ML model is trained (or ckp_baseline is explicitly used), risk approves, no duplicate position/order would be created, and the signal is fresh.
- Report exact details, not summaries. For every pipeline test or trade, report: signal_id, symbol, direction, prediction, strategy name + reason, classical/quantum optimizer outputs, risk decision + reason, order params, exchange response, fill details, fees, and final P&L.
- Do not optimize further until one complete real signal has traversed the entire pipeline. No new tools, no new adapters, no performance tuning until a real CKP signal has gone from Telegram → database → BingX order → decision record → dashboard.
BingX VST (Demo) Account Balance
- BingX demo uses
VSTas the balance currency, NOTUSDT fetch_balance()returns{'VST': {'free': 99693.0, 'used': 302.9, 'total': 99996.0}}— NOTUSDT- Access demo balance:
balance['VST']['total'], NOTbalance['USDT']['total'] - Always check raw CCXT response first:
bal = exchange.fetch_balance(); bal.get('VST', {}).get('total', 0) - A 0 USDT balance does NOT mean the account is unfunded — always check VST first
- The demo API endpoint must explicitly surface VST — if it only queries USDT, it will report 0 for a fully-funded demo account
BingX Position Desync on Service Restart
demo_trader.positionsis in-memory only — service restart loses all tracked positions- BingX exchange retains the real positions → desync: exchange shows 2 positions, internal state shows 0
- Fix: On service startup, reconcile internal state from
exchange.fetch_positions() - For each exchange position without a matching internal record, create the internal record
- For each internal position not on exchange, mark as closed
- Add this to
start_demo()before the background task starts
Backtest Engine Validation
- The walkforward engine is NOT broken — it correctly returns +9.47% on synthetic uptrend data and 0% on downtrend
- A 0% win rate on real market data is NOT necessarily a bug — choppy/flat markets produce losing trades
- Always test with synthetic data first to verify engine correctness before trusting real-market results
- Use
run_strategy()with known trending data to validate: uptrend should be positive, downtrend should be flat/negative
Adapter Compatibility (Python 3.13)
- VectorBT 1.1.0 fails with plotly 7.x ('mapbox' template removed) — wrap in try/except, set
available=False - Riskfolio-Lib API changed:
rp_optimizationlkwarg renamed,performanceattribute missing — usegetattrwith fallback - tsfresh 0.21.2 requires Python <3.11 — use 0.21.1 for Python 3.13
- alphalen fails to build on Python 3.13 — use pure pandas fallback for factor analysis
- Isolate each adapter behind try/except with availability flags — one failing adapter must not crash the others
Subagent Long-Run Failure Pattern
- Subagents on complex multi-file builds (adapters + quantum + ML + dashboard) can fail silently with "Delegation owner exited before recording a terminal result" or timeout
- Mitigation: dispatch ONE focused subagent per concern (tools-only, quantum-only, ML-only) rather than a single mega-build
- After dispatch, verify completion by checking
git statusand the expected file count — don't assume success from the dispatch returning - For mission-critical paths, follow up with a direct terminal audit (
ls src/tools/,pytest tests/test_tools.py) rather than trusting the subagent report
Callback verification: in-process vs out-of-process
- A Python script importing
demo_tradercreates a FRESH singleton — not the one the running FastAPI service holds - To audit the live state, use the API endpoints (
/api/demo/status,/api/demo/balance) or query the DB directly - A standalone script will show
demo_trader.is_active = Falseeven when the service has it active
Market Data Must Come From the Execution Exchange
- Pitfall: The pipeline used Binance (
MarketDataFetcher) for market snapshots, but execution is on BingX. Result: signals for coins listed on BingX but not Binance (e.g. PORTAL, KAVA, UMIA) were rejected with "No market data" even though they're perfectly tradeable. - Fix: Always fetch market data from
demo_trader.adapter.exchange(BingX) first, fall back to Binance only if the symbol isn't on the execution exchange. Use the correct swap symbol format:BASE/USDT:USDT. - Mechanism: BingX has ~1,200 perpetual swap markets; Binance has ~1,800 spot. For a VST demo account, only BingX swap markets are tradeable. A signal for PORTAL exists on BingX (
PORTAL/USDT:USDT) but not on Binance — the old code path rejected it. - The fix is in
FullPipeline._fetch_market_data():
async def _fetch_market_data(self, symbol: str) -> Optional[pd.DataFrame]:
# Try execution exchange (BingX) first
if demo_trader.adapter and demo_trader.adapter.exchange:
swap_symbol = f"{symbol}/USDT:USDT"
if swap_symbol in demo_trader.adapter.exchange.symbols:
ohlcv = await asyncio.to_thread(exchange.fetch_ohlcv, swap_symbol, "1h", limit=100)
# ... return df
# Fallback to Binance
return await self.market.fetch_ohlcv(f"{symbol}/USDT", "1h", limit=100)
Subagent File-Mutation Limitation
- Subagents cannot push files into CTs — the file-mutation verifier blocks it with "1 file(s) were NOT modified this turn"
- Workaround: The parent agent must SCP files to the PVE host, then
pct pushinto the CT, then verify with a marker grep - Pattern:
scp /tmp/file.py pve:/tmp/file.py
ssh pve "pct push 106 /tmp/file.py /tmp/file.py"
ssh pve "pct exec 106 -- grep -q 'UNIQUE_MARKER' /tmp/file.py && echo OK"
- Never trust a subagent's claim of "pushed to CT" — verify with a direct terminal audit
venv pip Only (PEP 668)
- System pip on CT106 hits PEP 668 externally-managed guard
- Always use
/opt/cortexquant/venv/bin/python3 -m pipor/opt/cortexquant/venv/bin/pip - Background
nohup pip installprocesses tend to hang — kill stuck PID and retry in foreground
BingX API Credentials Verified (2026-09)
- Key: 2URgZJv3... (redacted in logs)
- VST demo balance confirmed at ~100,000 VST
- BingX perpetual swap symbols: BASE/USDT:USDT format
- set_sandbox_mode(True) required; no sandboxMode attribute — use getattr
Deployment: git stash -u wipes untracked source files
- When deploying NEW files to a remote target via tar/push, never run
git stash -uon the target. The-u(include untracked) flag deletes files that were never committed — which is exactly the code you just pushed. - Use plain
git stash(tracked files only) or commit before stashing on a deployment target.
FastAPI route ordering: literal before path-param
/api/workers/healthregistered AFTER/api/workers/{worker_id}will be captured by the param route, returning{"error": "worker not found"}.- Always register literal routes BEFORE path-param routes with the same prefix.
Pipeline stage ordering: promotions must verify the prior stage passed
- A worker that advances an item to stage N+1 (e.g., RISK_REVIEW) MUST include the previous stage's pass condition (e.g.,
red_team_result["verdict"].astext == "PASS") in its query filter. - Without this, the gate is silently skipped and unvetted items flow through.
pytest-asyncio session-scoped event loop
- Async tests with
pytest-asyncioneed a session-scopedevent_loopfixture - Without it:
RuntimeError: no running event loopon module-scoped async fixtures - Pattern:
@pytest.fixture(scope="session") def event_loop(): loop = asyncio.new_event_loop(); yield loop; loop.close() - Mark async tests with
@pytest.mark.asyncio - Verify with:
python3 -m pytest tests/ -v