Commit Graph

906 Commits

Author SHA1 Message Date
jgrusewski
041e6f3d9a fix(trading_engine): reject overfills and fills on completed orders
Duplicate execution reports could double fill_quantity and corrupt
average price. Added guards in process_execution to reject fills on
already-Filled orders and to reject fills that would exceed order qty.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 21:52:34 +01:00
jgrusewski
5f47df464b fix(trading_engine): atomic validate-and-add prevents duplicate order race
validate_order and add_order were separate operations. The gap between
them allowed duplicate order IDs to both pass validation because
validate_order only took a read lock. New validate_and_add_order method
holds a single write lock for the entire check-and-insert operation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 21:25:22 +01:00
jgrusewski
330348e84d fix(trading_service): hold write lock across position update to prevent fill race
The update_position method previously acquired a read lock to get the
Arc<AtomicPosition>, dropped it, then called update_with_execution
outside any lock. Two concurrent fills for the same position could both
load the same old_quantity via Acquire, compute independent new quantities,
and the last Release store would silently discard the other fill — causing
the position to show e.g. 10 shares when it should show 20.

Now holds the positions write lock across the entire get-or-create +
update_with_execution sequence, serializing concurrent fills per position.
The validate_position_update call remains outside the lock to minimize
hold time (it does its own async reads and risk checks).

Also removes excessive per-step RDTSC latency tracking from the critical
path (total latency tracking is preserved) and adds a multi-threaded
regression test that spawns 10 concurrent +1 fills and asserts the final
quantity equals 10.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 20:56:49 +01:00
jgrusewski
2f83e57826 fix(trading_engine): deduct execution value from account cash balance
The old code computed `_execution_value = quantity * price` but
discarded it (underscore prefix).  Only commission was subtracted,
so the account cash balance never reflected the cost of buying or
the proceeds from selling.

Now `update_from_execution` matches on the new `execution.side` field:
- Buy  → cash -= execution_value + commission
- Sell → cash += execution_value − commission

Test assertions updated to expect the corrected balances.

Addresses audit item H2 (account never deducting trade value).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 20:47:04 +01:00
jgrusewski
3cbf643b4d fix(trading_engine): add OrderSide to ExecutionResult, fix always-buy direction
ExecutionResult.executed_quantity is always a positive magnitude, so the
old `is_buy = executed_quantity > ZERO` check was always true — every
fill was treated as a buy regardless of order side.

Add an explicit `pub side: OrderSide` field to ExecutionResult and use
`execution.side == OrderSide::Buy` in PositionManager.  All construction
sites (source, tests, benchmarks) updated; sell-side tests now use
positive quantities with `side: OrderSide::Sell` instead of the former
negative-quantity hack.

Addresses audit item H4 (position direction always buy).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 20:45:59 +01:00
jgrusewski
f7b259cb34 fix(trading_service): share atomic counters via Arc in clone_for_async
clone_for_async() was creating independent AtomicU64 instances for
message_count, drop_count, last_heartbeat, and reconnect_attempts
instead of sharing the originals. This meant spawned tasks incremented
their own counters while get_stats() read the original's (always 0),
and the heartbeat monitor watched a counter never updated by the
connection task.

Also fixes last_heartbeat initializing to 0, which caused the first
heartbeat check to compute a huge elapsed time and immediately trigger
a false "connection appears dead" alert.

Changes:
- Change 4 struct fields from AtomicU64 to Arc<AtomicU64>
- Initialize last_heartbeat to HardwareTimestamp::now() instead of 0
- clone_for_async() now clones the Arcs (shared counters)
- Add test verifying counters are shared between original and clone
- Add test verifying heartbeat initialized to current time

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 20:10:00 +01:00
jgrusewski
8bfd010af5 fix(trading_service): reject negative/NaN prices in fixed-point conversion
Casting negative f64 to u64 saturates to 0, corrupting avg_price and all
downstream PnL calculations. Add price_to_fixed_checked() that rejects
non-finite, zero, and negative prices with PositionError::InvalidPrice.
The critical update_with_execution path now uses the checked variant.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 20:01:38 +01:00
jgrusewski
7eb5ae0c03 fix(trading_engine): order status filter was irrefutable pattern binding
The get_orders() status filter used `matches!(order.status, _status)` which
creates a new wildcard binding instead of comparing against the captured
`status` variable. This caused every order to match regardless of filter,
meaning get_orders(Some(Filled)) returned ALL orders — inflating exposure
calculations. Replaced with direct equality comparison `order.status == *status`.

Added test_get_orders_status_filter_only_returns_matching to prevent regression.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 19:43:59 +01:00
jgrusewski
d45692934a docs: production safety audit implementation plan (38 tasks, 5 layers)
Layer 0: Data Integrity (7 fixes) — position race, price validation, counters
Layer 1: Risk Enforcement (10 fixes) — order atomicity, overfill, risk bypass
Layer 2: ML Pipeline (10 fixes) — random weights, epsilon-greedy, NaN validation
Layer 3: Broker Safety (5 fixes) — connection drops, volume overflow, DB sync
Layer 4: Auth & Ops (6 fixes) — auth stub gating, live trading confirmation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 19:00:29 +01:00
jgrusewski
7e9a3841f0 docs: production safety audit design — 38 fixes across 5 layers
4-domain code audit found 13 CRITICAL, 14 HIGH, 10 MEDIUM issues.
Organized as layer-by-layer remediation:
- Layer 0: Data integrity (positions, prices, market data)
- Layer 1: Risk enforcement (make checks actually block)
- Layer 2: ML pipeline (real weights, bounded predictions)
- Layer 3: Broker safety (connection handling, volumes)
- Layer 4: Auth & ops (credentials, rate limiting, monitoring)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 18:51:54 +01:00
jgrusewski
17c49f0788 docs: add real data training pipeline design and implementation plan
Design: Download 730 days Databento OHLCV-1m for 4 CME futures,
train DQN + PPO with hyperopt, walk-forward evaluation.

Implementation: 9 tasks — quarterly download binary, walk-forward
splitter, hyperopt runner, training binary, evaluation binary,
DBN wiring, integration tests, manual execution.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 18:07:03 +01:00
jgrusewski
5af8b0b921 fix(docker): use protoc v25 for ml_training_service CUDA build
Ubuntu 22.04's protobuf-compiler (v3.12) lacks proto3 optional field
support. Install protoc v25.1 from GitHub releases instead, which
matches the protoc version available in Debian bookworm-based images
used by the other 5 services.

Validated: docker build completes successfully, binary starts with
ml_training_service --help.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 17:21:27 +01:00
jgrusewski
d8d51aa2d0 fix(docker): add missing workspace members and SQLX_OFFLINE to all Dockerfiles
All 6 service Dockerfiles were missing newly-added workspace crates
(web-gateway, ctrader-openapi, foxhunt-deploy, broker_gateway_service),
causing cargo workspace resolution failures during Docker builds.

Changes across all Dockerfiles:
- Add COPY directives for web-gateway, ctrader-openapi, foxhunt-deploy,
  broker_gateway_service (new workspace members since Dockerfiles written)
- Add SQLX_OFFLINE=true env and .sqlx cache copy where missing
- Add perl and make system deps (needed for OpenSSL build from source)
- Remove COPY migrations (dir excluded by .dockerignore, not needed)
- Expand broker_gateway_service from 3-crate to full workspace copy

Validated: docker build --check passes all 6, cargo check -p passes all 6.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 16:46:24 +01:00
jgrusewski
fa4c649338 fix(ci): triage workflows — fix 3 essential, delete 6 duplicates
Essential workflow fixes:
- ci.yml: add SQLX_OFFLINE=true, replace fictitious cargo subcommands
  (test-unit, ci-lint, audit-deps, etc.) with real cargo commands,
  remove broken 9-way test matrix, remove instrument-coverage RUSTFLAGS
- test.yml: add SQLX_OFFLINE=true to build-check job, fix conflicting
  clippy flags (-D and -W on clippy::all), update JWT secret to 32+ chars
- compilation-guard.yml: add SQLX_OFFLINE=true, remove references to
  non-existent paths (services/trading-engine, crates/common/types),
  remove dangerous auto-commit-to-main, remove MIRI on missing packages

Deleted duplicates (justification):
- comprehensive_testing.yml: duplicate of comprehensive-testing.yml
  (same purpose, underscore vs hyphen naming)
- production-deploy.yml: duplicate of production-deployment.yml
  (both named "Production Deployment Pipeline", this one has stale paths)
- coverage-fixed.yml: duplicate of coverage.yml
  (uses deprecated actions-rs/toolchain@v1 and actions/cache@v3)
- performance.yml: duplicate of benchmark_regression.yml
  (both "Performance Regression Detection", this one less mature)
- quality-baseline.json: not a workflow, stale fake data (999 warnings)
- quality-metrics.json: not a workflow, stale fake data

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 16:39:00 +01:00
jgrusewski
f81bd3fc2e test(trading_service): add E2E pipeline integration test (10 models)
Proves the critical trading pipeline path works end-to-end:
OHLCV -> features -> ensemble -> prediction. Uses all 10 real candle
inference adapters (DQN, PPO, TFT, Mamba2, Liquid-CfC, TGGN, TLOB,
KAN, xLSTM, Diffusion) with random weights, 60 synthetic OHLCV bars,
and asserts confidence/action/model-count invariants. No external
dependencies (no DB, no Docker).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 16:34:06 +01:00
jgrusewski
c8bc3504f0 feat(trading_service): wire market data feed to ensemble coordinator
Spawn a background tokio task that feeds synthetic OHLCV bars into the
ensemble coordinator's per-symbol feature extractors.  The extractors
require ~51 bars of warmup before they can produce real 51-dim feature
vectors; without this feed they remain cold and return zeros.

The task uses a random-walk price generator with realistic base prices
for the four baseline futures symbols (ES, NQ, ZN, 6E) and configurable
interval (MARKET_FEED_INTERVAL_MS, default 1s) and symbol list
(MARKET_FEED_SYMBOLS).  In production this will be replaced by a
Databento or exchange data feed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 16:20:02 +01:00
jgrusewski
2942a043ce Merge branch 'worktree-operational-maturity'
# Conflicts:
#	ml/src/ensemble/mod.rs
2026-02-23 16:02:03 +01:00
jgrusewski
1999e5ddbe feat(trading_service): wire all 10 ML models into ensemble coordinator
Register TGGN, TLOB, KAN, xLSTM, and Diffusion inference adapters
alongside the existing DQN, PPO, TFT, Mamba2, and Liquid-CfC.
Rebalance weights to 0.10 each (equal weighting across 10 models).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 15:55:11 +01:00
jgrusewski
6d43f8d8d2 feat(ml): add TLOB inference adapter for ensemble
Sequence-buffered adapter with 3-layer MLP projection
(flat_dim -> hidden -> hidden/2 -> 1). Ring buffer collects
seq_len feature vectors, returns neutral prediction until full,
then flattens and projects through the MLP with sigmoid output
mapping to direction [-1,1] and confidence [0,1].

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 15:51:06 +01:00
jgrusewski
7c4341600d feat(ml): add KAN inference adapter for ensemble
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 15:51:06 +01:00
jgrusewski
3a403669b7 feat(ml): add TGGN inference adapter for ensemble
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 15:51:06 +01:00
jgrusewski
bb5286e910 Merge branch 'feat/trading-universe-data-org' 2026-02-23 15:22:05 +01:00
jgrusewski
236e1665cf fix(ml): address code review findings
- Remove panic!() calls from test_futures_baseline_micro_mapping,
  use map()+Some() pattern consistent with rest of test suite
- Make DatasetSpec::from_universe() accept a name parameter instead
  of hardcoding "futures-baseline"
- Add doc comment to UniverseConfigMeta explaining dead_code fields

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 15:17:58 +01:00
jgrusewski
323b77c820 feat(ml): add manifest generation test — builds cache manifest from on-disk DBN files
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 15:05:10 +01:00
jgrusewski
7e96d6e303 chore: add data/cache/ to .gitignore (downloaded market data)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 15:00:26 +01:00
jgrusewski
7b870726ab docs: add production pipeline wiring implementation plan (10 tasks)
Detailed TDD implementation plan for wiring all 10 ML models into
the ensemble coordinator. Covers 5 new inference adapters (KAN, TGGN,
xLSTM, TLOB, Diffusion), main.rs registration with equal weights,
market data wiring, E2E integration test, Docker/CI validation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 14:59:18 +01:00
jgrusewski
9d8622f410 feat: add futures-baseline universe config (TOML)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 14:57:48 +01:00
jgrusewski
6d844c009a feat(ml): add AssetUniverse::from_config() for TOML config loading
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 14:50:40 +01:00
jgrusewski
d268a9dca8 docs: add production pipeline wiring design (6 tasks)
Wire existing components into end-to-end pipeline: 5 missing inference
adapters, 10-model registration, market data feeding, integration test,
Docker build validation, and CI pipeline triage.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 14:43:22 +01:00
jgrusewski
8ce2d53d21 feat(ml): add trading_symbol field to UniverseAsset for micro contract mapping
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 14:41:22 +01:00
jgrusewski
3a60f303e3 refactor(ml): unify AssetClass — re-export from asset_selection
Remove duplicate AssetClass enum from data_pipeline module and replace
with a re-export from asset_selection, which is now the canonical
location. This ensures data_pipeline::AssetClass and
asset_selection::AssetClass are the same type, enabling direct
comparison and preventing subtle type mismatch bugs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 14:36:36 +01:00
jgrusewski
ae225b0e31 docs: add trading universe & data organization implementation plan
10-task plan: unify AssetClass, add trading_symbol field, futures_baseline()
preset, TOML config loading, DatasetSpec::from_universe() wiring, data
reorganization into cache structure, stale data cleanup, manifest generation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 14:21:48 +01:00
jgrusewski
d4f4f72a50 Merge branch 'feature/operational-maturity'
# Conflicts:
#	ml/src/lib.rs
2026-02-23 14:20:23 +01:00
jgrusewski
66966806c7 docs: add trading universe & data organization design
Define futures baseline universe (ES, NQ, ZN, 6E) with micro
contract mapping for limited capital. Reorganize scattered test
data into cache structure. Download spec for 730 days of OHLCV-1m.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 14:17:22 +01:00
jgrusewski
3f9f6ebe17 fix(trading): fix QuestDB metrics provider for real integration testing
- Use tokio::task::block_in_place for safe sync→async bridging
- Replace count(*) with count(1) in division context (QuestDB parser bug)
- Add coalesce() for stddev NULL handling (single-row edge case)
- Consolidate integration tests into single lifecycle test with DROP+CREATE
- All 4 QuestDB tests pass against real QuestDB 8.2.3

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 14:13:19 +01:00
jgrusewski
69d435cb52 feat(trading): integrate QuestDB metrics provider for feedback loop
Replace stub metrics provider with real QuestDBMetricsProvider that
queries QuestDB via PostgreSQL wire protocol (port 8812) for rolling
model Sharpe, win rates, confidence buckets, and ensemble metrics.

- Add QuestDB service to docker-compose.yml (8.2.3, ports 9009/8812/9003)
- Create questdb_metrics.rs with 5 SQL queries and graceful fallback
- Wire QuestDBMetricsProvider into main.rs (replaces StubMetricsProvider)
- Fix unused Instant import in feedback_loop.rs (#[cfg(test)] scope)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:57:46 +01:00
jgrusewski
dc94c0a757 feat(trading): add feedback loop orchestrator and wire into service init
FeedbackLoop runs as a background tokio task, periodically running
weight and gate optimization cycles with kill switch monitoring and
retraining trigger detection. Wired into main.rs with a stub
MetricsProvider until QuestDB is deployed.

7 new tests covering kill switch, freeze/unfreeze, retraining triggers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:49:47 +01:00
jgrusewski
81ae71c88e feat(ml): export data_pipeline and asset_selection types in prelude
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:40:50 +01:00
jgrusewski
1b7f72a0d2 feat(ml,trading): add gate optimizer, model registry, and P&L attribution
Gate optimizer adjusts conviction gate thresholds based on win-rate per
confidence bucket with cooldown and kill switch safety rails. Model
registry provides lifecycle management (Candidate → Staging → Production
→ Archived) with InMemoryModelRegistry for testing. P&L attribution
decomposes realized trade P&L into per-model contributions using signal
alignment.

24 new tests across 3 modules.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:39:22 +01:00
jgrusewski
1cc5a79365 feat(ml): add ActiveSetSelector for top-N asset selection by composite score
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:34:37 +01:00
jgrusewski
b94caf6d65 feat(ml): add PredictabilityScorer with rolling ML accuracy tracking
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:29:13 +01:00
jgrusewski
d2fe4c4e98 feat(ml): add asset_selection module with AssetUniverse config types
Introduces the asset_selection module with AssetClass enum (Equity/ETF/Future),
UniverseAsset struct, and AssetUniverse with filtering, lookup, and a us_starter()
preset of 7 highly liquid US assets. Includes scorer/selector placeholder stubs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:23:42 +01:00
jgrusewski
d623fb100c feat(ensemble): add autonomous weight optimizer with EMA Sharpe and safety rails
Adjusts model weights based on rolling Sharpe ratios with:
- EMA smoothing (alpha=0.1)
- Bounds: [0.05, 0.40] per model
- Max step: 0.03 per cycle
- 24h cooldown, 7-day grace period for new models
- Kill switch freeze/unfreeze
8 tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:18:53 +01:00
jgrusewski
766e16e724 feat(ml): add PreparedDataset with batch iterators for training
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:17:54 +01:00
jgrusewski
8106f4987b feat(common): add QuestDB client with ring buffer and health monitoring
Non-critical path: if QuestDB is unavailable, metrics buffer locally
(up to 10,000 entries) and flush when connection is restored.
Feature-gated under `questdb` feature. 6 tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:17:52 +01:00
jgrusewski
1b43d80db7 feat(ml): add DatasetManager for orchestrating data preparation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:12:33 +01:00
jgrusewski
aa67929ebc feat(ml): add cache manifest for tracking downloaded training data
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:05:52 +01:00
jgrusewski
ce8fd2b97e feat(ensemble): wire conviction gates into EnsembleCoordinator::predict()
Adds optional ConvictionGateEvaluator field to EnsembleCoordinator.
When configured, predict() evaluates all 7 gates before returning.
If any gate rejects, returns HOLD with rejection metadata.
Includes quorum ratio, trading session, and regime volatility helpers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 13:03:26 +01:00
jgrusewski
5bc56eb9c8 feat(ml): add data_pipeline module with DatasetSpec and DatasetMode types
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 12:54:36 +01:00
jgrusewski
948b2d8992 feat(ensemble): add 7-gate conviction system with evaluator and 15 tests
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 12:41:55 +01:00