aadb6c13d40868cb51375cff84d23f62041b5adb
Found the second val measurement bug while investigating the residual WinRate anomaly (1.5-4.7% val vs 15-23% train) after the pure-P&L fix: backtest_metrics_kernel was bounding "trades" by `exp_idx` (direction × magnitude composite), so every magnitude change (Long-Half → Long-Full while still long) counted as starting a NEW trade. Each new trade absorbed the bar-of-change tx_cost as its first step_return, biasing win-rate downward asymmetrically — and producing absurdly high trade counts (300-450 over 4k bars) that didn't match training's "position cycle" semantics (experience_kernels.cu:1592, win_count++ on reversing_trade or exiting_trade). Fix: collapse the trade-boundary key to a 3-state `signed_dir`: -1 = Short, 0 = Hold/Flat (no exposure), +1 = Long This matches training's "position sign change" definition exactly. A new trade fires only when the model crosses through the no-exposure state or reverses sign — i.e., on real position cycles. Sentinel for "no data in this CUDA chunk" moved from -1 → -2 since -1 is now a legitimate direction value. Boundary stitching at the cross- block reduction was updated accordingly (`if (fa < -1)` instead of `< 0`). Action-distribution counters (local_buys/sells/holds, used for action diversity logging) also updated: previously used a legacy 9-action threshold (num_actions/2) that didn't match the 4-branch encoding. Now classifies by signed_dir > 0 / < 0 / == 0 directly. Smoke verification (TD-prop, RTX 3050 Ti, 20 epochs, after fix): metric before WinRate fix after WinRate fix val_WinRate 1.5-4.7% 22-65% (mean 43.7%) val_Trades 300-450 17-31 val_Sharpe -1.24 to +2.34 -1.37 to +2.76 epochs val_S > 0 10 / 20 11 / 20 The first three commits this session removed/reduced the "physical" asymmetries (tau bug, CUSUM, exploration_scale/shaping_scale wiring). The fourth (pure P&L) and this one are MEASUREMENT bugs in the val metrics layer — both made the model look catastrophic when the underlying behavior was merely mediocre. The remaining Sharpe variance (min -1.37, max +2.76) is genuine signal: epochs with higher WinRate correlate with positive Sharpe, as expected from a working measurement. Files touched: crates/ml/src/cuda_pipeline/backtest_metrics_kernel.cu (+30 / -7) Verified: SQLX_OFFLINE=true cargo check -p ml --lib --tests passes. TD-propagation smoke test: one run passed (Best Sharpe 21.31, sharpe_ema trajectory 3.31 → 12.26 — clear upward trend), one run failed by 0.0024 on q_gap (test variance, not regression — same flakiness existed before this commit). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Foxhunt
Production HFT trading system in Rust.
Architecture
The workspace contains 32 crates organized as follows:
Core Libraries (16)
| Crate | Purpose |
|---|---|
trading_engine |
Order processing, FIX 4.4, IB TWS, SIMD, RDTSC timing |
risk |
VaR, Kelly, circuit breakers, kill switches, compliance |
risk-data |
Risk data types and shared structures |
trading-data |
Trading data types |
ml |
DQN Rainbow, PPO, TFT, Mamba2, ensemble inference |
ml-data |
ML data types and feature definitions |
data |
Market data ingestion and storage |
backtesting |
Replay engine, strategy tester |
adaptive-strategy |
Ensemble execution, microstructure analysis |
common |
Shared types, resilience, error handling |
storage |
S3 and local model storage |
model_loader |
Model serialization and loading |
market-data |
Market data feed handlers |
database |
PostgreSQL access layer (SQLx) |
config |
Configuration management |
tli |
CLI commands and tooling |
Services (8)
| Service | Purpose |
|---|---|
backtesting_service |
gRPC backtesting service |
broker_gateway_service |
FIX routing, broker connectivity |
trading_service |
Core trading operations |
ml_training_service |
Model training orchestration |
data_acquisition_service |
Market data acquisition |
trading_agent_service |
Autonomous trading agents |
api_gateway |
gRPC API gateway with auth |
web-gateway |
Axum REST + WebSocket gateway |
Frontend
web-dashboard/ -- React 19 + TypeScript + Vite + TradingView charts.
Building
# Check compilation (no PostgreSQL required)
SQLX_OFFLINE=true cargo check --workspace
# Run tests for a specific crate
SQLX_OFFLINE=true cargo test -p <crate> --lib
# Clippy
SQLX_OFFLINE=true cargo clippy --workspace
ML Models
Four production model architectures on Candle v0.9.1 with CUDA:
- DQN Rainbow -- Deep Q-Network with prioritized replay, dueling heads, noisy nets
- PPO -- Proximal Policy Optimization with GAE and LSTM policies
- TFT -- Temporal Fusion Transformer for multi-horizon forecasting
- Mamba2 -- State space model for sequence prediction
Each model has a standalone trainer and a UnifiedTrainable adapter for the hyperopt pipeline.
Infrastructure
- Git: Gitea at
git.fxhnt.ai(Tailscale-only), Scaleway DEV1-S - Observability: OpenTelemetry OTLP (env
OTEL_EXPORTER_OTLP_ENDPOINT) - Database: PostgreSQL with SQLx offline mode for CI
License
Proprietary. All rights reserved.
Description
Languages
Rust
88.2%
Cuda
7.7%
Python
1.3%
Shell
1.1%
PLpgSQL
0.8%
Other
0.8%