PINK Phase 0 and 1: VST WS confirmed plus AccountSnapshotV2 account core

This commit is contained in:
Codex
2026-06-01 20:11:03 +02:00
parent c87ca785b9
commit e7eaa88ce1
166 changed files with 832 additions and 77021 deletions

103
.gitignore vendored
View File

@@ -1,103 +0,0 @@
# ═══════════════════════════════════════════════════════════════════
# DOLPHIN-NAUTILUS HCM — .gitignore
# Policy: track source code + configs + docs; exclude all data/caches/models
# ═══════════════════════════════════════════════════════════════════
# ── Virtual environments ────────────────────────────────────────────
.venv/
venv/
env/
# ── Python cache ────────────────────────────────────────────────────
__pycache__/
*.pyc
*.pyo
*.pyd
.pytest_cache/
.hypothesis/
# ── IDE / tool dirs ─────────────────────────────────────────────────
.kiro/
.vscode/settings.json
# ── Jupyter ─────────────────────────────────────────────────────────
.ipynb_checkpoints/
# ── VBT Parquet caches (large, reconstructable from raw JSON) ────────
vbt_cache/
vbt_cache_ng5/
vbt_cache_klines/
# ── Arrow / klines backfill (large, reconstructable) ────────────────
backfilled_data/
klines_cache/
arrow_backfill/
# ── Matrix + eigenvalue data (raw source, not reconstructable here) ──
matrices/
eigenvalues/
# ── Order book data ─────────────────────────────────────────────────
ob_data/
# ── ML model weights / checkpoints (back up separately) ─────────────
models/
trained_models/
checkpoints/
checkpoints_10k/
genesis_vae_model/
mlruns/
mc_results/
mc_results_test/
nautilus_dolphin/mc_results/
# ── Experiment / backtest result data (large, reproducible) ──────────
backtest_results_2week/
results/
vbt_results/
hcm_experiments/
hcm_experiments_20260502_185525/
hcm_experiments_20260502_191804/
hcm_experiments_20260502_194842/
hd_cache/
hd_hcm_regime_results/
rolling_10week_results/
rolling_5window_results/
paper_trading_1month_results/
paper_trading_1week_results/
monitoring_data/
# ── Logs (large, ephemeral) ─────────────────────────────────────────
logs/
run_logs/*.csv
run_logs/*.json
nautilus_dolphin/run_logs/*.csv
nautilus_dolphin/run_logs/*.json
# ── Old alpha engine backups (already archived / superseded) ─────────
FROZEN_BACKUP_20260208/
alpha_engine - copia/
alpha_engine_BACKUP_20260202_143018/
alpha_engine_BACKUP_20260202_143050/
alpha_engine_BACKUP_20260209_203911/
alpha_engine_BASELINE_75PCT_EDGE/
# ── Problematic cache dirs (may contain Windows reserved filenames) ───
exit_matrix_engine/cache/
# ── nautilus_dolphin package (has own git repo — tracked separately) ──
nautilus_dolphin/
# ── Windows device names (not real files, can't be committed) ─────────
nul
/nul
# ── Misc large binary / temp ─────────────────────────────────────────
*.arrow
*.parquet
*.pkl
*.pkl.zst
*.npz
*.npy
temp_test/
training_reports/

View File

@@ -1,98 +0,0 @@
# DOLPHIN NG HD Data Locations
## Production Data
**Location**: `C:\Users\Lenovo\Documents\- Dolphin NG HD (NG3)\correlation_arb512`
### Directory Structure
```
correlation_arb512/
├── matrices/
│ ├── 2025-12-26_SKIP/
│ ├── 2025-12-27_SKIP/
│ ├── ...
│ ├── 2025-12-31/
│ ├── 2026-01-01/
│ │ ├── scan_016875_w50_000003.arb512.pkl.zst
│ │ ├── scan_016875_w150_000003.arb512.pkl.zst
│ │ ├── scan_016875_w300_000003.arb512.pkl.zst
│ │ ├── scan_016875_w750_000003.arb512.pkl.zst
│ │ └── ...
│ ├── 2026-01-02/
│ ├── 2026-01-03/
│ └── 2026-01-04/
├── eigenvalues/
│ ├── 2025-12-26_SKIP/
│ ├── ...
│ ├── 2026-01-01/
│ │ ├── scan_016875_000003.json
│ │ ├── scan_016876_000014.json
│ │ └── ...
│ └── ...
├── eigenvectors/
│ └── [dated directories with eigenvector data]
└── metadata/
└── [dated directories with metadata]
```
### File Naming Convention
**Eigenvalue JSON**: `scan_NNNNNN_HHMMSS.json`
- `NNNNNN`: 6-digit scan number
- `HHMMSS`: Timestamp (HHMMSS format)
**Matrix ZST**: `scan_NNNNNN_wWWW_HHMMSS.arb512.pkl.zst`
- `NNNNNN`: 6-digit scan number (matches eigenvalue)
- `WWW`: Window size (50, 150, 300, 750)
- `HHMMSS`: Timestamp
- `.arb512.pkl.zst`: Blosc-compressed pickle with 512-bit arb precision
### SKIP Directories
Directories with `_SKIP` suffix should be excluded from processing.
These contain data that failed validation or is marked for exclusion.
---
## Test Data (Current Project)
**Location**: `C:\Users\Lenovo\Documents\- DOLPHIN NG HD HCM TSF Predict`
Test data should mirror production structure with partial data:
```
- DOLPHIN NG HD HCM TSF Predict/
├── matrices/
│ ├── [root level files - legacy format]
│ └── 2026-01-03/
├── eigenvalues/
│ ├── 2026-01-01/
│ └── 2026-01-03/
└── ...
```
**Note**: Test data scan numbers may not match between directories.
Always verify pairing before running pipelines.
---
## Quick Reference
| Environment | Path |
|-------------|------|
| **Production** | `C:\Users\Lenovo\Documents\- Dolphin NG HD (NG3)\correlation_arb512` |
| **Test/Dev** | `C:\Users\Lenovo\Documents\- DOLPHIN NG HD HCM TSF Predict` |
---
## Related Documentation
- **ZST_Compressed_Matrix_DOLPHIN_format_spec.md** - Detailed format specification for `.arb512.pkl.zst` files
- **run_joint_encoder_pipeline.py** - Pipeline using this data
---
*Last updated: 2026-01-10*

File diff suppressed because it is too large Load Diff

View File

@@ -1,778 +0,0 @@
# PINK DITAv2 — Structural Flaw Analysis (CENTRAL)
**Analysis date:** 2026-05-31
**Scope:** Full PINK pipeline — all flaws across all modules.
**Sources:**
- This file (A-series): Detailed writeups for architectural flaws.
- [PINK_DITAv2_E2E_TRACE_ANALYSIS.md](./PINK_DITAv2_E2E_TRACE_ANALYSIS.md) (E, F, G-series):
Full E2E data-flow trace, deep bridge/Zinc/lifecycle scans.
Every E, F, G entry below is a summary only — full detail is in the TRACE doc.
---
## Combined Catalog (All Flaws, All Passes)
| Pass | Focus | Count | Critical | High | Medium | Low | Info |
|------|-------|-------|----------|------|--------|-----|------|
| A | Architectural (detailed in this file) | 15 | 0 | 2 | 0 | 2 | 11 |
| T | Threading/Atomicity | 9 | 1 | 3 | 3 | 2 | 0 |
| E | E2E Trace (Pass 1) | 26 | 0 | 4 | 10 | 11 | 1 |
| F | Deep E2E (Pass 3) | 30 | 0 | 1 | 8 | 17 | 4 |
| G | Domain Scans (Pass 4) | 36 | 4 | 11 | 11 | 8 | 2 |
| H | Edge Domains (Pass 5) | 22 | 3 | 9 | 5 | 4 | 1 |
| I | Pass 6 (Math/Tests/Recovery/Security) | 22 | 3 | 11 | 4 | 2 | 2 |
| **Total** | | **160** | **11** | **41** | **41** | **46** | **21** |
---
## T-Series: Threading & Atomicity Flaws
*Full detail in TRACE doc under "Threading & Atomicity" section.*
| # | Flaw | Layer | Severity |
|---|------|-------|----------|
| T1 | `InMemoryZincPlane` thread-Condition deadlock from slot update re-entrancy | Zinc | **Critical** |
| T2 | Thread-unsafe kernel snapshot capture for account | Bridge | **High** |
| T3 | Re-entrant or incorrectly-scoped Rust-kernel handle usage | Bridge | **High** |
| T4 | Consequence: `on_venue_event` PnL settle races | Bridge | **High** |
| T5 | Access to shared `_state_seq` / `_slot_cache` in `RealZincPlane` from multiple kernel calls | Zinc | Medium |
| T6 | `_write_region` buffer zero + notify race with concurrent reader | Zinc | Medium |
| T7 | Publication of events in `process_intent` loop not synchronized with persist | Bridge | Medium |
| T8 | `asyncio.run` executor skip in `_run` leads to event-loop stall | Venue | Low |
| T9 | No thread-safe Python↔Rust ownership / lifetime protocol | Bridge | Low |
---
## E-Series: E2E Data-Flow Flaws (Pass 1)
*Full detail in TRACE doc under "Layer 1" through "Layer 9."*
| # | Flaw | Layer | Severity |
|---|------|-------|----------|
| E1 | `step()` calls `pump_venue_events()` every cycle unconditionally | Runtime | **High** |
| E2 | `kernel.snapshot()["account"]` returns a fresh dict, not a live view | Bridge | Low |
| E3 | `_decision_to_kernel_intent` drops `order_type` and `limit_price` | Runtime | **High** |
| E4 | `_exit_intent_from_slot` trusts slot.size but slot may be stale | Runtime | **High** |
| E5 | JSON serialization round-trip loses numeric precision | Bridge | Low |
| E6 | `_RustKernelLib` is a global singleton — shared across all kernels | Bridge | Low |
| E7 | ENTER handler silently allows re-entry with same trade_id | Rust | **High** |
| E8 | EXIT handler uses `initial_size` not current size | Rust | **High** |
| E9 | CANCEL handler returns diagnostic even when nothing happened | Rust | Low |
| E10 | `apply_fill` entry branch double-sets `active_entry_order` | Rust | Low |
| E11 | `_legacy_intent()` is a lossy conversion | Venue | Low |
| E12 | `_events_from_submit()` price fallback chain can lose venue price | Venue | Low |
| E13 | `_backend_snapshot()` timeout returns stale data | Venue | Medium |
| E14 | `_events_from_cancel` uses stale `slot_id` from order metadata | Venue | Low |
| E15 | Submit sets leverage via separate HTTP call | Adapter | Medium |
| E16 | `_format_quantity`/`_format_price` may use zero tick/step | Adapter | Medium |
| E17 | Cancel uses truth-based confirmation — can mask real errors | Adapter | Medium |
| E18 | `on_venue_event` settles PnL incrementally — fees never included | Bridge | Medium |
| E19 | `observe_slots` called with ALL slots, not just changed ones | Bridge | Low |
| E20 | `_capital()` reads live from `AccountProjection` — stale row risk | Persistence | Low |
| E21 | `persist_fill_events()` synthesizes fake Decision/Intent | Persistence | Medium |
| E22 | `_write_trade_exit_leg` capital_before uses arithmetic reconstruction | Persistence | Medium |
| E23 | `_write_trade_event` uses entry_price as exit_price | Persistence | Medium |
| E24 | Mock venue always emits fill on `partial_fill_ratio > 0` | Test | Low |
| E25 | Test scenarios use MARKET-only `_si()` helper — no LIMIT tests | Test | Low |
| E26 | Fresh-kernel reconcile tests create second kernel but share venue | Test | Low |
---
## F-Series: Deep Bridge/Zinc/Lifecycle Flaws (Pass 3)
*Full detail in TRACE doc under "PASS 3 — NEW FINDINGS."*
| # | Flaw | Layer | Severity |
|---|------|-------|----------|
| F1 | CANCEL returns "accepted" before cancel happens — stale diagnostic_code | Bridge | Medium |
| F2 | `_last_settled_pnl` reset before `venue.submit()` — transient window | Bridge | Medium |
| F3 | `_first_invalid_intent_field` allows `leverage=0` and `target_size=0` | Bridge | Low |
| F4 | `outcome.emitted_events` only from venue — Rust kernel events dropped | Bridge | Low |
| F5 | `on_venue_event` redundant FFI read of slot already returned by Rust | Bridge | Low |
| F6 | `process_intent` records pre-venue transitions with `event=None` | Bridge | Info |
| F7 | `reconcile_from_slots` writes ALL slots to projection/zinc | Bridge | Low |
| F8 | `HazelcastRowWriter.put()` synchronous, no error handling — crashes intent | Projection | **Medium** |
| F9 | `RealZincPlane.write_slot()` serializes ALL slots, not just changed one | Zinc | Low |
| F10 | `RealZincPlane` zeros buffer before write — concurrent read sees empty | Zinc | Low |
| F11 | `RealZincPlane._write_region` no partial-write recovery | Zinc | Low |
| F12 | `InMemoryZincPlane` intent_region grows without bound | Zinc | Low |
| F13 | `InMemoryZincPlane` uses non-re-entrant `threading.Condition` | Zinc | Low |
| F14 | `KernelSlotView.__setattr__` round-trips unknown fields — silently dropped | Bridge | Low |
| F15 | `on_venue_event` loop stops on first exception — slot left in partial state | Bridge | **High** |
| F16 | `venue.submit()` returning empty events leaves slot in ORDER_REQUESTED | Bridge | Medium |
| F17 | Cancel truth-based confirmation returns REJECTED for already-cancelled orders | Adapter | Medium |
| F18 | Leverage-set and order-submit failures share error handler | Adapter | Low |
| F19 | `_events_from_submit` stale snapshot fallback → wrong fill detection | Venue | Medium |
| F20 | `__del__` frees Rust handle at unpredictable GC time — no explicit close() | Bridge | **Medium** |
| F21 | `DITAv2LauncherBundle.close()` closes venue before kernel is done | Launcher | Low |
| F22 | Silent fallback from real Zinc/Hazelcast to in-memory — operator unaware | Launcher | **Medium** |
| F23 | `VenueEvent.size` = `intent.target_size` not actual fill | Venue | Info |
| F24 | `asyncio.run()` inside async function in test generator | Test | Low |
| F25 | `_build_fresh_kernel_from_slot` leaks old kernel objects per call | Test | Low |
| F26 | `seen_event_ids` not cleared on re-entry — accumulates across trades | Rust | Low |
| F27 | `RealZincControlPlane.read()` parses Zinc region every call — no caching | Control | Low |
| F28 | `_legacy_intent` hardcodes confidence=1.0, bars_held=0 | Venue | Info |
| F29 | `_slot_to_payload` in real_zinc_plane.py is dead code | Zinc | Info |
| F30 | Duplicate `_slot_from_payload` in real_zinc_plane.py and rust_backend.py | Zinc | Low |
---
## G-Series: Domain Scans — Rust Kernel, Config, Persistence, Lifecycle (Pass 4)
*Full detail in TRACE doc under "PASS 4 — SYSTEMATIC DOMAIN SCANS."*
| # | Flaw | Layer | Severity |
|---|------|-------|----------|
| G1 | EXIT_RESIDUAL action missing from Rust KernelCommandType enum | Rust | **Critical** |
| G2 | `into_c_string` unwrap() panics on NUL byte in FFI string | Rust | **Critical** |
| G3 | EXIT hardcodes prev_state=POSITION_OPEN — allows backward FSM transition | Rust | **Critical** |
| G4 | `consume_exit_leg` stale `all_legs_done` variable — wrong branch after last leg | Rust | **Critical** |
| G5 | `realized_pnl` unbounded f64 — overflows to inf at extreme values | Rust | **High** |
| G6 | `mark_price` produces unbounded unrealized_pnl — no result guard | Rust | **High** |
| G7 | ENTER no is_finite() guard on target_size | Rust | **High** |
| G8 | `reconcile_slots_json` no dedup or bounds validation | Rust | **High** |
| G9 | `exchange_order_id` update targets wrong order — exit cancel broken | Rust | **High** |
| G10 | CANCEL diagnostic always says NO_ACTIVE_EXIT_ORDER | Rust | **High** |
| G11 | `apply_fill` overwrites intended_size with slot.size | Rust | Medium |
| G12 | No max leverage cap enforced by kernel | Rust | Medium |
| G13 | `resolve_slot` fallback returns unwrap_or(0) — misroutes events | Rust | Medium |
| G14 | `commit_slot` silently ignores out-of-bounds slot_id | Rust | Medium |
| G15 | Zero `__post_init__` validators on all 16 config dataclasses (127 fields) | Config | **High** |
| G16 | DITA_V2_DEBUG_CLICKHOUSE defaults to True when unset | Config | Info |
| G17 | String config fields — Zinc region injection risk | Config | Medium |
| G18 | `exit_leg_ratios` no sum-to-1 validation | Config | Low |
| G19 | RealZincControlPlane.read() no sequence check — torn-read risk | Config | Low |
| G20 | ClickHouse journal strategy/db env vars — SQL injection risk | Config | Low |
| G21 | entry_price used as exit_price in trade_events — data loss | Persistence | **High** |
| G22 | active_leg_index → entry_bar semantic mis-mapping | Persistence | Medium |
| G23 | capital_before arithmetic absorbs cross-slot PnL | Persistence | Medium |
| G24 | Recovery trade_reconstruction always has trade_id="" | Persistence | Medium |
| G25 | seen_event_ids, exit_leg_ratios, VenueOrder, metadata not in flat CH tables | Persistence | Low |
| G26 | _safe_float silently converts NaN/None/Inf to 0.0 | Persistence | Low |
| G27 | build_launcher_bundle no exception safety — prior resources leak | Lifecycle | **High** |
| G28 | RealZincPlane/RealZincControlPlane no __del__ — SHM orphaned | Lifecycle | **High** |
| G29 | Zero signal handlers — no cleanup on SIGTERM/SIGINT | Lifecycle | **High** |
| G30 | ExecutionKernel has no close() — relies on __del__ for Rust handle | Lifecycle | **High** |
| G31 | Hazelcast projection never closed | Lifecycle | Medium |
| G32 | _maybe_close() break skips second method | Lifecycle | Low |
| G33 | close() not idempotent for RealZinc components | Lifecycle | Low |
| G34 | No context manager on DITAv2LauncherBundle | Lifecycle | Low |
| G35 | BingxVenueAdapter.connect() never called | Lifecycle | Info |
| G36 | Only one try/finally in entire codebase | Lifecycle | **High** |
---
## I-Series: Math, Tests, Concurrency, Recovery, Security (Pass 6)
*Full detail in TRACE doc under "PASS 6 — MATH, TESTS, CONCURRENCY, RECOVERY, SECURITY."*
| # | Flaw | Layer | Severity |
|---|------|-------|----------|
| I1 | Entry `apply_fill` multiple partial fills overwrite size instead of accumulating | Rust | **Critical** |
| I2 | Zero exit_ratio creates zero-size exit order — slot stuck in EXIT_REQUESTED | Rust | Medium |
| I3 | entry_price inconsistency — Python falsy vs Rust `<= 0.0` gate | Bridge | Info |
| I4 | Only 1 Rust unit test for 1765-line kernel — 99% untested at Rust layer | Rust | **High** |
| I5 | MockVenueScenario rejection flags exist but zero tests use them | Test | **High** |
| I6 | No LIMIT order test through full kernel path | Test | **High** |
| I7 | Three weak/vacuous assertions in test_flaws.py | Test | Low |
| I8 | Entry overfill no guard | Rust | Low |
| I9 | No crash durability — slot state pure in-memory until step 7 of process_intent | Bridge | **Critical** |
| I10 | seen_event_ids lost on restart — events double-processed | Rust | **Critical** |
| I11 | No idempotency key sent to BingX — lost response creates duplicate orders | Venue | **High** |
| I12 | No graceful degradation for ANY subsystem | All | **High** |
| I13 | Stray venue event can reactivate CLOSED slot — no guard | Rust | **High** |
| I14 | No reconcile_from_slots call on startup — Zinc state never loaded into kernel | Restart | **High** |
| I15 | CANCEL_REJECT doesn't clear active_exit_order — slot stuck in EXIT_WORKING | Rust | Medium |
| I16 | Zinc shared memory world-readable/writable by same-machine processes | Zinc | **High** |
| I17 | KernelSlotView unrestricted getattr/setattr — bypasses all FSM guards | Bridge | **High** |
| I18 | sys.path.insert(0) at import time in 3 production files — malicious module loading | Build | **High** |
| I19 | pump_venue_events stale snapshot diff produces phantom position events | Venue | **High** |
| I20 | exit_leg_ratios empty list — next_exit_ratio defaults to 1.0 (undocumented) | Contracts | Info |
| I21 | RATE_LIMITED code path in both Python and Rust is completely untested | All | Medium |
| I22 | Thread pool max_workers=3 shared across all adapter instances — never shut down | Venue | Medium |
---
## H-Series: Edge Domains — Dependencies, Error Handling, Types, Contracts (Pass 5)
*Full detail in TRACE doc under "PASS 5 — EDGE DOMAINS."*
| # | Flaw | Layer | Severity |
|---|------|-------|----------|
| H1 | No Python dependency files (requirements.txt, pyproject.toml, etc.) | Build | **Critical** |
| H2 | Rust kernel compiled from source on every cold start — no prebuilt binary | Build | **Critical** |
| H3 | Zero logging — 16+ silent except:pass sites, no error observability | All | **Critical** |
| H4 | `_row_float` rejects zero as valid, `except Exception: continue` swallows all | Venue | **High** |
| H5 | `_backend_snapshot` timeout returns stale data/None — callers crash | Venue | **High** |
| H6 | All enum-from-raw-string sites crash on unknown variant (17 sites) | Bridge | **High** |
| H7 | `_legacy_intent` reads `getattr(intent, "order_type")` not metadata — always MARKET | Venue | **High** |
| H8 | Unknown venue status silently mapped to ACKED | Venue | **High** |
| H9 | `RealZincPlane.write_slot()` `slot_id >= slot_count` silently lost | Zinc | **High** |
| H10 | `RealZincControlPlane.read()` no atomicity with concurrent `update()` | Control | **High** |
| H11 | `_RustKernelLib` lazy init with race condition — concurrent cargo build | Bridge | **High** |
| H12 | `ExecutionKernel.__del__` use-after-free on Rust handle | Bridge | **High** |
| H13 | `MirroredControlPlane` missing protocol methods (wait/notify) | Control | Medium |
| H14 | `TradeSlot.remaining_size` vs `VenueOrder.remaining_size` — different semantics | Contracts | Medium |
| H15 | `_maybe_close` asyncio.run RuntimeError silently swallowed | Launcher | Medium |
| H16 | Lazy import of bingx_direct masks config errors until first trade | Build | Info |
| H17 | `load_dotenv()` at module level — import-time I/O side effect | Launcher | Medium |
| H18 | `_run()` blocks event loop on every HTTP call via thread pool | Venue | Medium |
| H19 | `HazelcastClientLike` protocol has zero concrete implementations | Projection | Low |
| H20 | `_decode_packet` uncaught UnicodeDecodeError/ValueError on corrupted SHM | Zinc | Low |
| H21 | `wasm-bindgen` compiled into native library unnecessarily | Build | Low |
| H22 | `socket.getaddrinfo` monkey-patch in test code | Test | Low |
---
## A-Series: Architectural Flaws (detailed writeups)
*These are the original architectural flaws with full analysis.*
---
### Flaw A1: Exit-size overshoot on multi-leg with initial_size > remaining size
**Location:** `_rust_kernel/src/lib.rs` lines ~770-780 (EXIT handler in `process_intent`)
**Severity:** **High**
**Nature:** Logic error — wrong base for exit-size computation.
### Downstream effect
The EXIT handler computes the exit size as `base_size * exit_ratio` where:
```rust
let base_size = if slot.initial_size > 0.0 { slot.initial_size } else { slot.size };
```
After partial fills (e.g., two separate MARKET exit legs), `initial_size` is still the
**original** entry size while `slot.size` has been reduced by previous legs. If the
cumulative leg ratios don't sum to exactly 1.0 (or the final ratio is not 1.0), the
computed exit size can exceed the remaining position.
The venue adapter clamps to actual position via `reduceOnly`, but the kernel's _own_
accounting reduces `slot.size` by the fill size, not by the intended exit size. The
slot can therefore go negative (`slot.size < 0`) if the fill is larger than remaining.
### Exact trigger
1. Enter SHORT, size=1.0, `initial_size=1.0`, ratios=(0.6, 0.6, 1.0) — note ratios sum > 1.0
2. EXIT leg 0: `exit_size = 1.0 * 0.6 = 0.6`. Fill consumes 0.6. Slot size goes to 0.4.
3. EXIT leg 1: `exit_size = 1.0 * 0.6 = 0.6`. But remaining is 0.4. Requests 0.6.
4. BingX `reduceOnly` clamps fill to 0.4. Slot size goes to 0.0.
5. EXIT leg 2 (ratio 1.0): `exit_size = 1.0 * 1.0 = 1.0`. Slot is already at 0.0.
Kernel returns `NO_OPEN_POSITION` — the final EXIT is rejected because `slot.closed`
was not set by the previous fill (it was a partial close, not terminal).
6. Slot is at size=0.0, `!slot.closed`, no active orders, but `!slot.is_free()` because
`size <= 0.0` is true but `fsm_state != IDLE/CLOSED` — slot is **stuck** in
`POSITION_OPEN` with zero size.
This is **not** purely a mis-sized ratio problem. With MARKET orders that fill fully,
even correct ratios can leave the slot stuck if the fill price differs from the
intended-size price and the venue adjusts fill quantity.
### Fix strategy
Use `slot.size` directly as the base (not `initial_size`):
```rust
let exit_size = (slot.size * exit_ratio).max(0.0).min(slot.size);
```
This guarantees the exit never requests more than the remaining position, regardless
of cumulative ratio math. The venue still clamps, but the kernel's intent is correct.
---
### Flaw A2: Misleading CANCEL diagnostic code on entry-only slots
**Location:** `_rust_kernel/src/lib.rs` lines ~798-810 (CANCEL rejection path)
**Severity:** **Low**
**Nature:** Diagnostic pollution — wrong error code.
### Downstream effect
When a CANCEL intent arrives and **neither** `active_exit_order` nor
`active_entry_order` is cancellable, the kernel returns:
```rust
diagnostic_code: KernelDiagnosticCode::NO_ACTIVE_EXIT_ORDER
```
But the reason may be that there's no active entry order either, or the FSM state
doesn't permit cancellation. The diagnostic name suggests an exit-order-specific
problem when the failure is generic "nothing to cancel."
### Fix
Change to a generic `NO_ACTIVE_ORDER` diagnostic or `SLOT_IDLE` when the slot is
already in IDLE. `NO_ACTIVE_EXIT_ORDER` is misleading for a slot that has never had
any order.
---
### Flaw A3: Float-accumulated slot.size after partial fills can go negative
**Location:** `_rust_kernel/src/lib.rs` lines ~1365-1370 (apply_fill exit path)
**Severity:** **Low**
**Nature:** Numerical precision edge case.
### Code path
```rust
slot.size = (slot.size - fill_size).max(0.0);
```
This clamps to zero, which is correct. But if the venue fills *more* than requested
(on BingX, this can happen with market orders where the fill walks the book), the
slot sees `fill_size > intended_size`. The `max(0.0)` prevents negative, but the
slot then reports `size=0.0` with `!closed` and an FSM state that's not IDLE.
The `is_free()` check requires `size <= 0.0` AND `fsm_state in {IDLE, CLOSED}`. A
slot with `size=0.0` and `fsm_state=POSITION_OPEN` is stuck — no EXIT will be
accepted and no ENTER can start.
### Trigger
Submit an EXIT for 0.6 of remaining 0.6. BingX fills 0.8 (market order walks the
book, overshoots). `fill_size=0.8`, `slot.size = (0.6 - 0.8).max(0.0) = 0.0`.
Slot is now size=0, fsm_state=EXIT_WORKING (or POSITION_OPEN), `closed=false`.
### Fix
When `slot.size <= 1e-12` after a fill and the slot is in an exit-related state,
force transition to CLOSED/IDLE regardless of leg index:
```rust
if slot.size <= 1e-12 {
slot.closed = true;
slot.fsm_state = TradeStage::CLOSED;
slot.active_exit_order = None;
slot.active_entry_order = None;
return;
}
```
---
### Flaw A4: Entry price is clobbered by mark_price if called before fill arrives
**Location:** `_rust_kernel/src/lib.rs` lines ~432-436 (mark_price) and ~1390 (apply_fill entry branch)
**Severity:** **Medium**
**Nature:** Accounting accuracy — incorrect PnL base.
### Code path
```rust
// In mark_price:
if self.entry_price <= 0.0 {
self.entry_price = price; // Seeds entry_price from mark before fill
}
// In apply_fill (entry):
if event.price > 0.0 {
slot.entry_price = event.price; // Overwrites with actual fill price
}
```
The `mark_price` path seeds `entry_price` from a market price when the slot has no
fill yet. The `apply_fill` entry path correctly overwrites with the actual fill price.
So in the normal flow this is harmless — the fill overwrites the mark.
**However**, consider this sequence:
1. ENTER intent accepted → slot goes `ORDER_REQUESTED`, `entry_price = 0.0`
2. `runtime.step()` calls `kernel.mark_price(snapshot.symbol, snapshot.price)` → sets `entry_price = 100.0`
3. `on_venue_event(ORDER_ACK)``ENTRY_WORKING`, `entry_price` still `100.0`
4. `on_venue_event(PARTIAL_FILL)``apply_fill` sets `entry_price = 99.5` (fill price)
5. Unrealized PnL from step 2-3 used a mark price of 100.0, not the fill price of 99.5
This is a transient mis-valuation window. It corrects itself on the next `observe_slots`
call, but intra-step readers see wrong unrealized PnL. Not critical because:
- `account.snapshot.unrealized_pnl` uses the slot's `unrealized_pnl`, not the mark
- Realized PnL is computed from actual fill prices
- The window lasts at most one scan cycle (~5s)
### Fix
Don't set `entry_price` from `mark_price` when there's no fill:
```rust
fn mark_price(&mut self, price: f64) {
if !price.is_finite() || price <= 0.0 { return; }
// Don't seed entry_price — leave it at 0.0 until a fill arrives
if self.entry_price <= 0.0 || self.size <= 0.0 {
self.unrealized_pnl = 0.0;
return;
}
// ... normal PnL computation
}
```
---
### Flaw A5: Capital-before computation is arithmetic not snapshot-based
**Location:** `pink_clickhouse.py` lines ~761-762 (`_write_trade_exit_leg`) and ~822-823 (`_write_trade_event`)
**Severity:** **High**
**Nature:** Accounting accuracy — wrong capital_before under multi-slot or intervening events.
### Code pattern (appears in two places)
```python
capital_after = self._capital()
capital_before = capital_after - pnl_leg # In _write_trade_exit_leg
capital_before = capital_after - pnl # In _write_trade_event
```
This reconstructs `capital_before` by subtracting the current leg's PnL from the
current capital. This is **only correct** if:
1. No other slots settled PnL between this leg and the previous one
2. No capital corrections (reconcile, manual override) happened between legs
3. No fees were deducted between legs
With multi-slot (PINK configurable `max_slots > 1`), a concurrent trade on slot 1
that closes between slot 0's exit legs will have its PnL baked into `capital_after`,
making `capital_before = capital_after - pnl_leg` wrong.
### Fix
Maintain a per-trade `capital_before_leg` snapshot taken at the moment of the
first fill event for each trade, advancing it by the realized PnL of each leg:
```python
self._leg_state[trade_id]["capital_before"] = prev.get("capital_after", capital_after - pnl_leg)
self._leg_state[trade_id]["capital_after"] = capital_after
```
And use `prev["capital_before"]` for the row, not `capital_after - pnl_leg`.
---
### Flaw A6: Reconcile accoun(t) reseeds capital from kernel, not exchange
**Location:** `pink_direct.py` lines ~597-630 (`recover_account`) and docstring of `reconcile_account`
**Severity:** **Medium**
**Nature:** Operational drift — capital is never verified against exchange truth in hot loop.
### The gap
`reconcile_account()` (line 632) has this docstring:
```
Periodic exchange-led account sync.
Capital is re-seeded from the exchange balance as a guard against long-running drift
```
But the actual implementation:
```python
async def reconcile_account(self, ...) -> dict[str, Any]:
return await self.recover_account(...)
async def recover_account(self, ...) -> dict[str, Any]:
capital = float(self.kernel.account.snapshot.capital or 25000.0)
_reconcile_position_slot(self.kernel, capital, slot_id=0)
```
It passes the **kernel's own capital** to `_reconcile_position_slot`, which then
overwrites `kernel.account.snapshot.capital` with... the same value. No exchange
balance poll ever overwrites capital.
`connect()` at line 224 does the same — it passes `initial_capital` (an env default),
not the exchange balance. The exchange balance is never read for capital seeding
in the current code path. `_reconcile_position_slot` does call
`venue.open_positions()`, but it only reads positions, not capital.
### Effect
Capital drift (caused by fees the kernel doesn't track, unrealized PnL mis-valuation,
or any other systematic error) accumulates monotonically. There is no mechanism to
detect or correct drift. Over weeks of live trading, the kernel's capital snapshot
can diverge arbitrarily from the exchange's actual balance.
### Fix
Either:
1. Make `_reconcile_position_slot` read the exchange balance and use it for
capital reseeding (the docstring claims it does this already), or
2. Add a separate capital-verification path that surfaces the delta between
kernel capital and exchange balance as an anomaly, even if it doesn't auto-correct.
---
### Flaw A7: No fee tracking in kernel accounting
**Location:** `rust_backend.py` lines ~540-545 (on_venue_event settle), `bingx_direct.py` submit_intent return
**Severity:** **Medium**
**Nature:** Accounting accuracy — fees are invisible to capital tracking.
### Downstream effect
When a trade closes, the kernel computes:
```rust
realized_pnl = delta * notional
```
This is **gross** PnL. BingX charges fees on every fill (taker ~0.04%, maker ~0.02%).
These fees are never subtracted from the kernel's realized PnL. Over 100 trades with
$100 average notional at 0.04%, the cumulative error is $4 — negligible. Over 10,000
trades at 10x leverage and $50k average notional, the error is $200k.
The `BingxDirectExecutionAdapter` does return `ExecutionReceipt` with fill data,
but `bingx_venue._events_from_submit()` only reads `price` and `filled_size`
commission/fee fields are ignored.
### Fix
1. Read fee/commission from the BingX ack payload in `_events_from_submit()`
2. Pass fees through `VenueEvent.metadata["fee"]`
3. In the Rust kernel's `apply_fill`, subtract the fee from realized PnL:
```rust
let fee = event.metadata.get("fee").and_then(|v| v.as_f64()).unwrap_or(0.0);
slot.realized_pnl += realized - fee;
```
---
### Flaw A8: ENTER intent silently defaults leverage to 1.0 on bad input
**Location:** `_rust_kernel/src/lib.rs` lines ~745-748
**Severity:** **Low**
**Nature:** Silent fallback — corrupt input produces a trade, not a rejection.
```rust
slot.leverage = if intent.leverage.is_finite() && intent.leverage > 0.0 {
intent.leverage
} else {
1.0
};
```
A NaN, zero, negative, or infinite leverage value silently trades at 1x instead of
rejecting the intent. The Python bridge does validate `_first_invalid_intent_field()`
which catches NaN/inf, but it doesn't catch `leverage <= 0.0` (it only checks
`not math.isfinite(value)`).
### Fix
Add `leverage <= 0.0` to the Python bridge's invalid-intent check. The Rust kernel
should still have the `1.0` fallback as a defensive measure, but the bridge should
prevent bad leverages from reaching Rust in the first place.
---
### Flaw A9: Mock venue submit condition convoluted — dead code paths
**Location:** `mock_venue.py` lines ~60-90
**Severity:** **Informational**
**Nature:** Code clarity — confusing condition logic.
```python
if self.scenario.emit_ack_before_fill or not self.scenario.emit_fill_on_submit:
events.append(ack_event)
if self.scenario.emit_fill_on_submit or self.scenario.partial_fill_ratio > 0:
# ... fill events
```
The condition logic is confusing:
- When `emit_ack_before_fill=True` and `emit_fill_on_submit=True`: both branches run → ACK + fill
- When `emit_ack_before_fill=False` and `emit_fill_on_submit=True`: first branch runs because
`not True = False`, so `False or False = False` → no ACK. Second branch runs → fill only.
This produces a fill without an ACK, which is **not** a realistic venue scenario.
- When `partial_fill_ratio=1.0` (default): second branch runs and emits a `FULL_FILL` event
even when `emit_fill_on_submit=False`, because `0.0 or 1.0 > 0 = True`.
The partial fill ratio check should be gated on `emit_fill_on_submit`:
```python
should_emit_fill = self.scenario.emit_fill_on_submit or (
is_entry and self.scenario.entry_partial_fill_ratio > 0
) or (
not is_entry and self.scenario.exit_partial_fill_ratio > 0
)
```
---
### Flaw A10: Pump venue events on every step cycle — expensive for MARKET-only flows
**Location:** `pink_direct.py` lines ~318-374 (`pump_venue_events`), called at line ~436
**Severity:** **Medium**
**Nature:** Operational overhead — unnecessary exchange HTTP calls.
### The problem
`step()` calls `pump_venue_events()` **every cycle**, which calls `venue.reconcile()`.
For `BingxVenueAdapter`, `reconcile()` calls `_backend_snapshot()` which does up to 5
HTTP requests (balance, positions, open orders) in parallel. For a MARKET-only workflow
where orders fill synchronously within `process_intent()`, there are **no** late fills
to drain.
On BingX VST, the rate limit is ~10 requests/second across all endpoints. Each
`pump_venue_events()` call consumes 5+ of that budget. At a 5-second policy cycle,
this is 60 requests/minute — 60% of the rate budget — just to poll for fills that
don't exist.
### Fix
Gate the pump on whether the previous cycle submitted a LIMIT order:
```python
self._has_resting_order = any(
o.status not in (VenueOrderStatus.FILLED, VenueOrderStatus.CANCELED)
for o in kernel.open_orders()
)
if self._has_resting_order:
await self.pump_venue_events(snapshot, market_state=market_state)
```
Or add a config flag `async_fill_mode: bool = False`.
---
### Flaw A11: VenueAdapter.submit() blocks the event loop
**Location:** `bingx_venue.py` lines ~225-233 (`_run`)
**Severity:** **Medium**
**Nature:** Runtime safety — synchronous call in async context.
```python
def _run(self, result: Any) -> Any:
if inspect.isawaitable(result):
try:
asyncio.get_running_loop()
except RuntimeError:
return asyncio.run(result)
pool = self._get_executor()
return pool.submit(asyncio.run, result).result()
```
When called from `step()` (which is an async function), `_run` submits the async
`submit_intent()` to a thread pool, runs it with `asyncio.run()`, then calls
`.result()` which blocks the current thread until complete. The BingX HTTP call
can take 1-5 seconds depending on network latency and exchange load.
During this block, the event loop **cannot** process other async tasks (data feed
updates, health checks, signal processing). In a single-runtime deployment, this
stalls the entire policy cycle.
### Fix
Make `process_intent` in `ExecutionKernel` accept an async venue callback, or
make `BingxVenueAdapter` truly async (not sync-with-thread-bridge). For now,
at minimum the PINK runtime should run `step()` in an executor to avoid blocking
the main event loop.
---
### Flaw A12: Stale KernelStateView slot references after reconcile
**Location:** `rust_backend.py` lines ~350-365 (`KernelStateView.refresh`)
**Severity:** **Low**
**Nature:** Stale data — view not rebuilt on reconcile.
```python
class KernelStateView:
def __init__(self, kernel):
self.slots = [KernelSlotView(kernel, slot_id) for slot_id in range(kernel.max_slots)]
# ...
def refresh(self) -> None:
snapshot = self._kernel._snapshot_backend()
self.active_trade_index = dict(snapshot.get("active_trade_index", {}))
self.venue_order_index = dict(snapshot.get("venue_order_index", {}))
self.client_order_index = dict(snapshot.get("client_order_index", {}))
```
`refresh()` updates the index maps but does **not** recreate `self.slots`. The slot
views in `self.slots` are live proxies (they read through `_get_slot` each time),
so slot data is current. But if `max_slots` changes (it shouldn't, but it's mutable)
or if slots are re-indexed by a reconcile, the view list is wrong.
Not critical because `max_slots` is set at init and never changes, but worth
fixing for robustness.
---
### Flaw A13: `persist_fill_events` uses current price as exit price
**Location:** `pink_clickhouse.py` lines ~408
**Severity:** **Low**
**Nature:** Historical accuracy — logged price may not match fill price.
```python
price = next((float(getattr(e, "price", 0.0) or 0.0) for e in event_list
if getattr(e, "price", 0.0)), 0.0) or self._slot_entry_price(slot)
```
This correctly reads from the event's price. But `decision.reference_price` at line
417 falls back to this price, which is the fill price. The trade_event row at line
835 uses `exit_price = slot_dict.get("entry_price", ...)` — which is the **entry**
price, not the exit price. The trade_event always shows exit_price == entry_price.
This means `trade_events` in ClickHouse will never show a realistic exit price
for the persisted trade, breaking any PnL reconstruction that relies on
`(exit_price - entry_price) * size * leverage`.
---
### Flaw A14: `_write_position_state` maps active_leg_index to entry_bar
**Location:** `pink_clickhouse.py` line ~673
**Severity:** **Low**
**Nature:** Semantic mismatch — wrong field mapping.
```python
"entry_bar": int(slot_dict.get("active_leg_index", 0) or 0),
```
`active_leg_index` is the index into the exit-leg-ratios array (which leg is being
exited next). It has nothing to do with how many bars the position has been held.
When a position opens, `active_leg_index` is 0. After the first exit leg, it
advances to 1. Neither value is a bar count.
`entry_bar` should be `bars_held` from the intent/decision, or a computed value
from `entry_time` to now.
---
### Flaw A15: `persist_recovery_state` passes account dict as slot dict
**Location:** `pink_clickhouse.py` lines ~447-460
**Severity:** **Low**
**Nature:** Wrong data — account snapshot used where slot data is expected.
```python
def persist_recovery_state(self, *, snapshot, acc_dict, ...):
slot_dict = acc_dict or {} # ← acc_dict is an account snapshot, not a slot
self._write_position_state(..., slot_dict={}, ...) # ← correctly uses empty dict
self._write_trade_reconstruction(
snapshot,
trade_id=acc_dict.get("trade_id", "") if acc_dict else "",
# acc_dict is {"capital": ..., "equity": ...} — no "trade_id" key
)
```
The `trade_id` in the trade_reconstruction row will always be `""` because
`acc_dict` comes from `kernel.snapshot()["account"]` which has keys `capital`,
`equity`, `realized_pnl`, etc. — not `trade_id`. This means the recovery
`trade_reconstruction` row has no trade_id linkage.

View File

@@ -1,40 +0,0 @@
#!/usr/bin/env python3
"""
Helper script to update VBT Parquet cache.
Called by update_VBT_parquet_cache.bat
"""
import sys
from pathlib import Path
from multiprocessing import freeze_support
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
def main():
try:
from dolphin_vbt_real import build_parquet_cache
except ImportError as e:
print(f"ERROR: Cannot import dolphin_vbt_real: {e}")
print("Make sure you're running from the project root directory.")
return 1
print("Starting VBT cache update...")
print()
try:
stats = build_parquet_cache(force=False)
print()
print("Update complete!")
print(f" Dates processed: {stats.get('dates_processed', 0)}")
print(f" Total scans: {stats.get('total_scans', 0):,}")
print(f" Time: {stats.get('elapsed_s', 0):.1f}s")
return 0
except Exception as e:
print(f"ERROR: {e}", file=sys.stderr)
import traceback
traceback.print_exc()
return 1
if __name__ == '__main__':
freeze_support()
sys.exit(main())

View File

@@ -1,315 +0,0 @@
"""Calibrate AlphaExitEngineV7 thresholds for synthetic LONG EFSM paths.
This script replays BLUE V7 decision journal price paths with side inverted to
LONG. It follows the original V7 SHORT calibration pattern:
1. Reconstruct per-trade path from V7 journal rows.
2. Compute the natural end-of-path LONG outcome.
3. Replay AlphaExitEngineV7 on the same path using side=LONG.
4. Sweep configurable threshold surfaces.
5. Compare the first V7 EXIT against the natural end outcome.
The output is a calibration proxy for EFSM FLIP_LONG trades, not proof from
actual exchange-filled LONG trades.
"""
from __future__ import annotations
import argparse
import base64
import csv
import json
import logging
import math
import sys
import urllib.request
from collections import defaultdict
from dataclasses import asdict
from pathlib import Path
from statistics import fmean
from typing import Any
ROOT = Path("/mnt/dolphinng5_predict")
sys.path.insert(0, str(ROOT / "nautilus_dolphin"))
sys.path.insert(1, str(ROOT))
from nautilus_dolphin.nautilus.alpha_exit_v7_engine import AlphaExitEngineV7, AlphaExitV7Config # noqa: E402
CH_URL = "http://localhost:8123/?database=dolphin"
AUTH = "Basic " + base64.b64encode(b"dolphin:dolphin_ch_2026").decode()
FEE_PCT = 0.0004
logging.getLogger("nautilus_dolphin.nautilus.alpha_exit_v7_engine").setLevel(logging.ERROR)
def _query(sql: str) -> str:
req = urllib.request.Request(CH_URL, data=sql.encode(), headers={"Authorization": AUTH})
return urllib.request.urlopen(req, timeout=60).read().decode()
def load_v7_rows(limit_trades: int = 0) -> list[dict[str, Any]]:
trade_filter = ""
if limit_trades > 0:
trade_filter = (
"AND trade_id IN ("
"SELECT trade_id FROM ("
"SELECT trade_id, max(ts) AS mx FROM v7_decision_events "
"WHERE strategy='blue' AND side='SHORT' GROUP BY trade_id ORDER BY mx DESC "
f"LIMIT {int(limit_trades)}"
"))"
)
sql = f"""
SELECT
ts, trade_id, asset, entry_price, current_price, quantity, leverage,
bar_idx, decision_seq, bars_held, ob_imbalance,
exf_funding, exf_dvol, exf_fear_greed, exf_taker
FROM v7_decision_events
WHERE strategy='blue' AND side='SHORT' {trade_filter}
ORDER BY trade_id ASC, decision_seq ASC, ts ASC
FORMAT CSVWithNames
"""
text = _query(sql)
rows: list[dict[str, Any]] = []
for r in csv.DictReader(text.splitlines()):
rows.append({
"ts": r["ts"],
"trade_id": r["trade_id"],
"asset": r["asset"],
"entry_price": float(r["entry_price"] or 0.0),
"current_price": float(r["current_price"] or 0.0),
"quantity": float(r["quantity"] or 0.0),
"leverage": float(r["leverage"] or 0.0),
"bar_idx": int(float(r["bar_idx"] or 0)),
"decision_seq": int(float(r["decision_seq"] or 0)),
"bars_held": int(float(r["bars_held"] or 0)),
"ob_imbalance": float(r["ob_imbalance"] or 0.0),
"exf_funding": float(r["exf_funding"] or 0.0),
"exf_dvol": float(r["exf_dvol"] or 0.0),
"exf_fear_greed": float(r["exf_fear_greed"] or 0.0),
"exf_taker": float(r["exf_taker"] or 0.0),
})
return rows
def group_paths(rows: list[dict[str, Any]]) -> list[list[dict[str, Any]]]:
grouped: dict[str, list[dict[str, Any]]] = defaultdict(list)
for row in rows:
grouped[row["trade_id"]].append(row)
paths = []
for path in grouped.values():
path.sort(key=lambda r: (r["decision_seq"], r["bar_idx"], r["ts"]))
clean = [r for r in path if r["entry_price"] > 0 and r["current_price"] > 0]
if len(clean) >= 2:
paths.append(clean)
paths.sort(key=lambda p: p[-1]["ts"])
return paths
def natural_long_return(path: list[dict[str, Any]]) -> float:
entry = path[0]["entry_price"]
last = path[-1]["current_price"]
return (last - entry) / entry - FEE_PCT if entry > 0 else 0.0
def pnl_dollars(path: list[dict[str, Any]], ret: float) -> float:
notional = abs(path[0]["entry_price"] * path[0]["quantity"])
return notional * ret
def replay_path(path: list[dict[str, Any]], cfg: AlphaExitV7Config) -> dict[str, Any]:
engine = AlphaExitEngineV7(
bar_duration_sec=11.0,
bounce_model_path="/tmp/nonexistent-bounce-model.pkl",
config=cfg,
)
ctx = engine.make_context(entry_price=path[0]["entry_price"], entry_bar=path[0]["bar_idx"], side=0)
first_exit = None
decisions = []
for row in path:
if hasattr(ctx, "set_exf"):
ctx.set_exf(
funding=row["exf_funding"],
dvol=row["exf_dvol"],
fear_greed=row["exf_fear_greed"],
taker=row["exf_taker"],
)
dec = engine.evaluate(
ctx,
current_price=row["current_price"],
current_bar=row["bar_idx"],
ob_imbalance=row["ob_imbalance"],
asset=row["asset"],
)
decisions.append(dec)
if first_exit is None and dec["action"] == "EXIT":
first_exit = (row, dec)
break
nat_ret = natural_long_return(path)
if first_exit is None:
exit_ret = nat_ret
exit_row = path[-1]
exit_dec = decisions[-1]
exited = False
else:
exit_row, exit_dec = first_exit
exit_ret = (exit_row["current_price"] - path[0]["entry_price"]) / path[0]["entry_price"] - FEE_PCT
exited = True
return {
"trade_id": path[0]["trade_id"],
"asset": path[0]["asset"],
"n_rows": len(path),
"natural_ret": nat_ret,
"natural_pnl": pnl_dollars(path, nat_ret),
"exit_ret": exit_ret,
"exit_pnl": pnl_dollars(path, exit_ret),
"delta_pnl": pnl_dollars(path, exit_ret) - pnl_dollars(path, nat_ret),
"exited": exited,
"exit_action": exit_dec.get("action"),
"exit_reason": exit_dec.get("reason") or "",
"exit_pressure": float(exit_dec.get("exit_pressure", 0.0) or 0.0),
"exit_bars_held": int(exit_dec.get("bars_held", 0) or 0),
"exit_mae": float(exit_dec.get("mae", 0.0) or 0.0),
"exit_mfe": float(exit_dec.get("mfe", 0.0) or 0.0),
"exit_mae_risk": float(exit_dec.get("mae_risk", 0.0) or 0.0),
"exit_mfe_risk": float(exit_dec.get("mfe_risk", 0.0) or 0.0),
}
def equity_stats(vals: list[float]) -> dict[str, float]:
eq = 1.0
peak = 1.0
dd = 0.0
for r in vals:
eq *= max(0.0, 1.0 + r)
peak = max(peak, eq)
dd = max(dd, (peak - eq) / peak if peak else 0.0)
return {
"n": len(vals),
"wr": sum(1 for r in vals if r > 0) / len(vals) if vals else 0.0,
"mean": fmean(vals) if vals else 0.0,
"compound": eq - 1.0,
"max_dd": dd,
}
def summarize(results: list[dict[str, Any]], cfg: AlphaExitV7Config, name: str) -> dict[str, Any]:
natural_rets = [r["natural_ret"] for r in results]
exit_rets = [r["exit_ret"] for r in results]
deltas = [r["delta_pnl"] for r in results]
return {
"name": name,
"config": asdict(cfg),
"n": len(results),
"exits": sum(1 for r in results if r["exited"]),
"exit_rate": sum(1 for r in results if r["exited"]) / len(results) if results else 0.0,
"natural": {
**equity_stats(natural_rets),
"pnl": sum(r["natural_pnl"] for r in results),
},
"v7": {
**equity_stats(exit_rets),
"pnl": sum(r["exit_pnl"] for r in results),
},
"delta_pnl": sum(deltas),
"positive_delta_trades": sum(1 for d in deltas if d > 0),
"negative_delta_trades": sum(1 for d in deltas if d < 0),
"avg_exit_pressure": fmean([r["exit_pressure"] for r in results if r["exited"]]) if any(r["exited"] for r in results) else 0.0,
"reasons": dict(sorted({
reason: sum(1 for r in results if r["exit_reason"] == reason)
for reason in {r["exit_reason"] for r in results}
}.items())),
}
def candidate_configs() -> list[tuple[str, AlphaExitV7Config]]:
out = [("short_default", AlphaExitV7Config())]
for threshold in [1.4, 1.7, 2.0, 2.35, 2.69, 3.0]:
out.append((f"exit_p{threshold}", AlphaExitV7Config(exit_pressure_threshold=threshold)))
for tier_scale in [0.5, 0.75, 1.0, 1.25, 1.5]:
out.append((
f"mae_scale_{tier_scale}",
AlphaExitV7Config(
mae_tier1_k=3.5 * tier_scale,
mae_tier2_k=7.0 * tier_scale,
mae_tier3_k=12.0 * tier_scale,
mae_tier1_floor=0.005 * tier_scale,
mae_tier2_floor=0.012 * tier_scale,
mae_tier3_floor=0.025 * tier_scale,
),
))
for mfe_scale in [0.5, 0.75, 1.25, 1.5]:
out.append((
f"mfe_risk_scale_{mfe_scale}",
AlphaExitV7Config(
mfe_convexity_exit_risk=1.5 * mfe_scale,
mfe_convexity_soft_risk=0.3 * mfe_scale,
mfe_accel_risk=0.2 * mfe_scale,
),
))
for late_start in [0.3, 0.45, 0.6, 0.75]:
out.append((f"late_start_{late_start}", AlphaExitV7Config(mae_late_start_frac=late_start)))
for threshold in [1.7, 2.0, 2.35]:
for mae_scale in [0.5, 0.75, 1.25]:
out.append((
f"combo_p{threshold}_mae{mae_scale}",
AlphaExitV7Config(
exit_pressure_threshold=threshold,
mae_tier1_k=3.5 * mae_scale,
mae_tier2_k=7.0 * mae_scale,
mae_tier3_k=12.0 * mae_scale,
mae_tier1_floor=0.005 * mae_scale,
mae_tier2_floor=0.012 * mae_scale,
mae_tier3_floor=0.025 * mae_scale,
),
))
return out
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--limit-trades", type=int, default=0)
parser.add_argument("--out", default="/tmp/v7_long_calibration.json")
args = parser.parse_args()
rows = load_v7_rows(limit_trades=args.limit_trades)
paths = group_paths(rows)
summaries = []
for name, cfg in candidate_configs():
results = [replay_path(path, cfg) for path in paths]
summaries.append(summarize(results, cfg, name))
summaries.sort(key=lambda r: (r["delta_pnl"], r["v7"]["pnl"], -r["exits"]), reverse=True)
payload = {
"method": "Synthetic LONG replay of BLUE SHORT V7 decision journal paths; bounce model disabled.",
"input": {
"rows": len(rows),
"paths": len(paths),
"limit_trades": args.limit_trades,
},
"top_by_delta": summaries[:20],
"all": summaries,
}
Path(args.out).write_text(json.dumps(payload, indent=2, sort_keys=True), encoding="utf-8")
print(json.dumps({
"input": payload["input"],
"top_by_delta": [
{
"name": s["name"],
"n": s["n"],
"exits": s["exits"],
"exit_rate": s["exit_rate"],
"natural_pnl": s["natural"]["pnl"],
"v7_pnl": s["v7"]["pnl"],
"delta_pnl": s["delta_pnl"],
"natural_compound": s["natural"]["compound"],
"v7_compound": s["v7"]["compound"],
"v7_dd": s["v7"]["max_dd"],
"reasons": s["reasons"],
}
for s in summaries[:12]
],
}, indent=2, sort_keys=True))
if __name__ == "__main__":
main()

View File

@@ -1,351 +0,0 @@
"""Deterministic post-win LONG overlay EFSM.
This module does not place orders. It tags future entries after realized BLUE
SHORT exhaustion wins so the live/shadow caller can decide whether to flip the
next one or more SHORT-engine opportunities to LONG.
EFSM means Execution FSM. The EFSM is deliberately slot-based:
- a trigger arms N future slots
- each future entry consumes exactly one slot
- when slots reach zero, state resets to SHORT
- flipped LONG trades do not re-arm the overlay
- triggers observed while an arm is active are ignored unless explicitly
enabled by config
That prevents the bug class where a one/two-trade rebound probe becomes a
self-extending regime switch.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Mapping, Optional, Sequence
def _to_float(value: Any, default: float = 0.0) -> float:
try:
out = float(value)
except (TypeError, ValueError):
return default
return out if out == out else default
def _to_utc(ts: datetime | None) -> datetime | None:
if ts is None:
return None
if ts.tzinfo is None:
return ts.replace(tzinfo=timezone.utc)
return ts.astimezone(timezone.utc)
@dataclass(frozen=True)
class PostWinFlipTrigger:
"""A configurable trigger that arms future LONG flip slots."""
name: str
slots: int
min_pnl_abs: float = 0.0
max_pnl_abs: Optional[float] = None
min_pnl_pct: Optional[float] = None
min_leverage: Optional[float] = None
strict_min_pnl_abs: bool = True
strict_max_pnl_abs: bool = True
strict_min_leverage: bool = True
def matches(self, *, pnl: float, pnl_pct: float, leverage: float) -> bool:
if self.slots <= 0:
return False
if self.strict_min_pnl_abs:
if not pnl > self.min_pnl_abs:
return False
elif pnl < self.min_pnl_abs:
return False
if self.max_pnl_abs is not None:
if self.strict_max_pnl_abs:
if not pnl < self.max_pnl_abs:
return False
elif pnl > self.max_pnl_abs:
return False
if self.min_pnl_pct is not None and pnl_pct < self.min_pnl_pct:
return False
if self.min_leverage is not None:
if self.strict_min_leverage:
if not leverage > self.min_leverage:
return False
elif leverage < self.min_leverage:
return False
return True
@dataclass(frozen=True)
class PostWinExecutionFSMConfig:
"""Configuration for the BLUE post-win Execution FSM."""
enabled: bool = True
rules: Sequence[PostWinFlipTrigger] = field(
default_factory=lambda: (
# Order matters: the high-leverage big-win rule must win before the
# generic big-win rule, otherwise it would be capped at one slot.
PostWinFlipTrigger(
name="big_win_high_lev",
slots=2,
min_pnl_abs=397.0,
min_leverage=8.6,
strict_min_pnl_abs=True,
strict_min_leverage=True,
),
PostWinFlipTrigger(
name="big_win",
slots=1,
min_pnl_abs=397.0,
strict_min_pnl_abs=True,
),
PostWinFlipTrigger(
name="small_dollar_high_return",
slots=1,
min_pnl_abs=0.0,
max_pnl_abs=250.0,
min_pnl_pct=0.0075,
strict_min_pnl_abs=True,
strict_max_pnl_abs=True,
),
)
)
max_arm_age_sec: Optional[float] = None
allow_rearm_while_armed: bool = False
allow_triggers_from_overlay_flips: bool = False
@dataclass(frozen=True)
class ActiveFlipArm:
"""Currently armed future LONG flip slots."""
arm_id: int
trigger_name: str
slots_total: int
slots_remaining: int
trigger_trade_id: str = ""
trigger_asset: str = ""
trigger_ts: datetime | None = None
trigger_pnl: float = 0.0
trigger_pnl_pct: float = 0.0
trigger_leverage: float = 0.0
def with_remaining(self, slots_remaining: int) -> "ActiveFlipArm":
return ActiveFlipArm(
arm_id=self.arm_id,
trigger_name=self.trigger_name,
slots_total=self.slots_total,
slots_remaining=max(0, int(slots_remaining)),
trigger_trade_id=self.trigger_trade_id,
trigger_asset=self.trigger_asset,
trigger_ts=self.trigger_ts,
trigger_pnl=self.trigger_pnl,
trigger_pnl_pct=self.trigger_pnl_pct,
trigger_leverage=self.trigger_leverage,
)
def to_dict(self) -> dict[str, Any]:
return {
"arm_id": self.arm_id,
"trigger_name": self.trigger_name,
"slots_total": self.slots_total,
"slots_remaining": self.slots_remaining,
"trigger_trade_id": self.trigger_trade_id,
"trigger_asset": self.trigger_asset,
"trigger_ts": self.trigger_ts.isoformat() if self.trigger_ts else None,
"trigger_pnl": self.trigger_pnl,
"trigger_pnl_pct": self.trigger_pnl_pct,
"trigger_leverage": self.trigger_leverage,
}
@dataclass(frozen=True)
class OverlayDecision:
"""Result returned by observe/entry tagging calls."""
action: str
side: str = "SHORT"
reason: str = ""
arm: ActiveFlipArm | None = None
consumed_slot: int = 0
reset: bool = False
def to_dict(self) -> dict[str, Any]:
return {
"action": self.action,
"side": self.side,
"reason": self.reason,
"arm": self.arm.to_dict() if self.arm else None,
"consumed_slot": self.consumed_slot,
"reset": self.reset,
}
class PostWinExecutionFSM:
"""Multi-slot post-win LONG tag Execution FSM."""
def __init__(self, config: PostWinExecutionFSMConfig | None = None) -> None:
self.config = config or PostWinExecutionFSMConfig()
self._arm: ActiveFlipArm | None = None
self._next_arm_id = 1
self.ignored_rearm_attempts = 0
self.ignored_overlay_flip_triggers = 0
self.expired_arms = 0
self.consumed_arms = 0
@property
def active_arm(self) -> ActiveFlipArm | None:
return self._arm
@property
def pending_slots(self) -> int:
return int(self._arm.slots_remaining) if self._arm else 0
def reset(self, reason: str = "manual") -> OverlayDecision:
old = self._arm
self._arm = None
return OverlayDecision(action="RESET", reason=reason, arm=old, reset=True)
def observe_closed_trade(
self,
*,
trade_id: str = "",
asset: str = "",
side: str = "SHORT",
pnl: float = 0.0,
pnl_pct: float = 0.0,
leverage: float = 0.0,
closed_ts: datetime | None = None,
was_overlay_flip: bool = False,
metadata: Mapping[str, Any] | None = None,
) -> OverlayDecision:
"""Observe a completed trade and possibly arm future LONG slots.
Parameters are intentionally primitive so this can be called from live
code, replay code, or ClickHouse/log readers.
"""
del metadata # reserved for future feature logging without API churn
self._expire_if_needed(_to_utc(closed_ts))
if not self.config.enabled:
return OverlayDecision(action="NOOP", reason="disabled", arm=self._arm)
side_u = str(side or "SHORT").upper()
if was_overlay_flip or side_u == "LONG":
self.ignored_overlay_flip_triggers += 1
return OverlayDecision(action="IGNORED", reason="overlay_flip_outcome", arm=self._arm)
pnl_f = _to_float(pnl)
pnl_pct_f = _to_float(pnl_pct)
lev_f = _to_float(leverage)
rule = self._match_rule(pnl=pnl_f, pnl_pct=pnl_pct_f, leverage=lev_f)
if rule is None:
return OverlayDecision(action="NO_TRIGGER", reason="no_rule_match", arm=self._arm)
if self._arm is not None and not self.config.allow_rearm_while_armed:
self.ignored_rearm_attempts += 1
return OverlayDecision(action="IGNORED", reason="active_arm_no_rearm", arm=self._arm)
arm = ActiveFlipArm(
arm_id=self._next_arm_id,
trigger_name=rule.name,
slots_total=int(rule.slots),
slots_remaining=int(rule.slots),
trigger_trade_id=str(trade_id or ""),
trigger_asset=str(asset or ""),
trigger_ts=_to_utc(closed_ts),
trigger_pnl=pnl_f,
trigger_pnl_pct=pnl_pct_f,
trigger_leverage=lev_f,
)
self._next_arm_id += 1
self._arm = arm
return OverlayDecision(action="ARMED", reason=rule.name, arm=arm)
def tag_next_entry(
self,
*,
asset: str = "",
entry_ts: datetime | None = None,
metadata: Mapping[str, Any] | None = None,
) -> OverlayDecision:
"""Return the side tag for the next engine entry and consume one slot."""
del asset, metadata # reserved for future asset-specific slot routing
self._expire_if_needed(_to_utc(entry_ts))
if self._arm is None or self._arm.slots_remaining <= 0:
return OverlayDecision(action="PASS", side="SHORT", reason="no_active_arm")
arm_before = self._arm
consumed_slot = arm_before.slots_total - arm_before.slots_remaining + 1
remaining = arm_before.slots_remaining - 1
if remaining <= 0:
self._arm = None
self.consumed_arms += 1
return OverlayDecision(
action="TAG",
side="LONG",
reason=arm_before.trigger_name,
arm=arm_before.with_remaining(0),
consumed_slot=consumed_slot,
reset=True,
)
self._arm = arm_before.with_remaining(remaining)
return OverlayDecision(
action="TAG",
side="LONG",
reason=arm_before.trigger_name,
arm=self._arm,
consumed_slot=consumed_slot,
reset=False,
)
def snapshot(self) -> dict[str, Any]:
return {
"enabled": self.config.enabled,
"active_arm": self._arm.to_dict() if self._arm else None,
"pending_slots": self.pending_slots,
"ignored_rearm_attempts": self.ignored_rearm_attempts,
"ignored_overlay_flip_triggers": self.ignored_overlay_flip_triggers,
"expired_arms": self.expired_arms,
"consumed_arms": self.consumed_arms,
}
def _match_rule(self, *, pnl: float, pnl_pct: float, leverage: float) -> PostWinFlipTrigger | None:
for rule in self.config.rules:
if rule.matches(pnl=pnl, pnl_pct=pnl_pct, leverage=leverage):
return rule
return None
def _expire_if_needed(self, now: datetime | None) -> None:
if self._arm is None:
return
if self.config.max_arm_age_sec is None:
return
if now is None or self._arm.trigger_ts is None:
return
age = (now - self._arm.trigger_ts).total_seconds()
if age > float(self.config.max_arm_age_sec):
self._arm = None
self.expired_arms += 1
# Compatibility aliases for earlier research scripts and tests.
PostWinLongOverlayConfig = PostWinExecutionFSMConfig
PostWinLongOverlay = PostWinExecutionFSM
__all__ = [
"ActiveFlipArm",
"OverlayDecision",
"PostWinExecutionFSM",
"PostWinExecutionFSMConfig",
"PostWinFlipTrigger",
"PostWinLongOverlay",
"PostWinLongOverlayConfig",
]

View File

@@ -1,734 +0,0 @@
"""
DOLPHIN Paper Trading Simulation — ADAPTIVE CIRCUIT BREAKER v2
===============================================================
Multi-signal confirmation approach to reduce false positives.
FIXES from v1:
- FNG alone no longer triggers large cuts
- Requires 2+ confirming signals for meaningful cuts
- Lower base cut (30% vs 45%)
- Severity-weighted scoring
KEY INSIGHT from research:
- Cohen's d analysis shows taker ratio (d=3.57) is strongest predictor
- FNG alone has low predictive power (conflicts with funding/DVOL)
- Multi-signal confirmation required for high-confidence cuts
Strategies tested:
1. Champion (5x cvx3 f20) — highest PF
2. Growth (25x cvx3 f10) — best PF/ROI balance
3. Aggressive (25x cvx3 f20) — max ROI
4. Conservative (5x cvx3 f10) — min risk
Run: python dolphin_paper_trade_adaptive_cb_v2.py [--no-cb] [--compare]
Output: vbt_results/dolphin_paper_trade_acbv2_*.json
vbt_results/dolphin_paper_trade_acbv2_*.csv
"""
import sys
import json
import time
import csv
import argparse
from pathlib import Path
from datetime import datetime
from dataclasses import replace, asdict
from collections import defaultdict
import numpy as np
import pandas as pd
sys.path.insert(0, str(Path(__file__).parent))
sys.path.insert(0, str(Path(__file__).parent / 'external_factors'))
from dolphin_vbt_real import (
load_all_data, run_full_backtest, Strategy,
CACHE_DIR, RESULTS_DIR,
)
from realtime_exf_service import calculate_adaptive_cut_v4, load_external_factors_lagged
from nautilus_dolphin.mc.mc_ml import DolphinForewarner
from nautilus_dolphin.mc.mc_sampler import MCTrialConfig
import logging
logging.getLogger("xgboost").setLevel(logging.ERROR)
# ══════════════════════════════════════════════════════════════════════
# CONFIGURATION
# ══════════════════════════════════════════════════════════════════════
EIGENVALUES_BASE_PATH = Path(r'C:/Users/Lenovo/Documents/- Dolphin NG HD (NG3)/correlation_arb512/eigenvalues')
# Adaptive CB v2 Configuration
ACBV2_CONFIG = {
'enabled': True,
'base_cut': 0.0, # 0% base cut - CB only activates on stress signals
'max_cut': 0.80, # 80% max position cut
# Multi-signal thresholds
'thresholds': {
'funding_btc_very_bearish': -0.0001,
'funding_btc_bearish': 0.0,
'dvol_extreme': 80,
'dvol_elevated': 55,
'fng_extreme_fear': 25,
'fng_fear': 40,
'taker_selling': 0.8,
'taker_mild_selling': 0.9,
}
}
# ══════════════════════════════════════════════════════════════════════
# STRATEGY DEFINITIONS
# ══════════════════════════════════════════════════════════════════════
BASE_PARAMS = dict(
vel_div_threshold=-0.02,
direction='SHORT',
leverage=2.5,
stop_pct=1.0,
max_hold=120,
use_trailing=False,
vol_filter='high',
use_asset_selection=True,
min_irp_alignment=0.45,
use_sp_fees=True,
use_sp_slippage=True,
use_ob_edge=True,
ob_edge_bps=5.0,
dynamic_leverage=True,
min_leverage=0.5,
use_alpha_layers=True,
use_fixed_tp=True,
fixed_tp_pct=0.0099,
use_direction_confirm=True,
dc_skip_contradicts=True,
dc_leverage_boost=1.0,
dc_leverage_reduce=0.5,
dc_lookback_bars=7,
dc_min_magnitude_bps=0.75,
)
STRATEGIES = {
'champion_5x_f20': Strategy(
name='champion_5x_f20',
max_leverage=5.0, fraction=0.20, leverage_convexity=3.0,
**BASE_PARAMS,
),
'growth_25x_f10': Strategy(
name='growth_25x_f10',
max_leverage=25.0, fraction=0.10, leverage_convexity=3.0,
**BASE_PARAMS,
),
'aggressive_25x_f20': Strategy(
name='aggressive_25x_f20',
max_leverage=25.0, fraction=0.20, leverage_convexity=3.0,
**BASE_PARAMS,
),
'conservative_5x_f10': Strategy(
name='conservative_5x_f10',
max_leverage=5.0, fraction=0.10, leverage_convexity=3.0,
**BASE_PARAMS,
),
}
INIT_CAPITAL = 10_000.0
# ══════════════════════════════════════════════════════════════════════
# ADAPTIVE CIRCUIT BREAKER v2 - MULTI-SIGNAL CONFIRMATION
# ══════════════════════════════════════════════════════════════════════
def load_external_factors_fast(date_str: str, max_scans: int = 1000) -> dict:
"""Load daily-aggregated external factors from indicator files."""
date_path = EIGENVALUES_BASE_PATH / date_str
if not date_path.exists():
return {}
files = list(date_path.glob('scan_*__Indicators.npz'))[:max_scans]
if not files:
return {}
indicators = defaultdict(list)
for f in files:
try:
data = np.load(f, allow_pickle=True)
if 'api_success_rate' in data and data['api_success_rate'][0] < 0.3:
continue
api_names = data.get('api_names', data.get('api_indicator_names', []))
api_values = data.get('api_indicators', data.get('external', []))
api_success = data.get('api_success', data.get('external_success', []))
for name, value, success in zip(api_names, api_values, api_success):
if success and not np.isnan(value):
indicators[name].append(float(value))
except Exception:
continue
result = {}
for name, values in indicators.items():
if values:
result[name] = np.mean(values)
result[f'{name}_std'] = np.std(values)
result[f'{name}_count'] = len(values)
return result
def calculate_adaptive_cut_v2(ext_factors: dict, config: dict = None) -> tuple:
"""
Calculate adaptive position cut using multi-signal confirmation.
v2 Changes:
- FNG alone does NOT trigger large cuts
- Requires 2+ confirming signals for meaningful cuts
- Lower base cut (30% vs 45%)
- Severity-weighted scoring
Returns:
Tuple of (cut_percentage, signal_count, severity, details_dict)
"""
config = config or ACBV2_CONFIG
if not ext_factors or not config.get('enabled', True):
return config.get('base_cut', 0.30), 0, 0, {'status': 'disabled'}
signals = 0
severity = 0
details = {}
# Signal 1: Funding (bearish confirmation)
funding_btc = ext_factors.get('funding_btc', 0)
if funding_btc < config['thresholds']['funding_btc_very_bearish']:
signals += 1
severity += 2
details['funding'] = f'{funding_btc:.6f} (very bearish, +1 signal, +2 severity)'
elif funding_btc < config['thresholds']['funding_btc_bearish']:
signals += 1
severity += 1
details['funding'] = f'{funding_btc:.6f} (bearish, +1 signal, +1 severity)'
else:
details['funding'] = f'{funding_btc:.6f} (neutral/bullish)'
# Signal 2: DVOL (volatility confirmation)
dvol_btc = ext_factors.get('dvol_btc', 50)
if dvol_btc > config['thresholds']['dvol_extreme']:
signals += 1
severity += 2
details['dvol'] = f'{dvol_btc:.1f} (extreme, +1 signal, +2 severity)'
elif dvol_btc > config['thresholds']['dvol_elevated']:
signals += 1
severity += 1
details['dvol'] = f'{dvol_btc:.1f} (elevated, +1 signal, +1 severity)'
else:
details['dvol'] = f'{dvol_btc:.1f} (normal)'
# Signal 3: Fear & Greed (ONLY counts if funding is negative OR DVOL elevated)
# Rationale: FNG alone has low predictive power per Cohen's d analysis
fng = ext_factors.get('fng', 50)
funding_bearish = funding_btc < 0
dvol_elevated = dvol_btc > 55
if fng < config['thresholds']['fng_extreme_fear'] and (funding_bearish or dvol_elevated):
signals += 1
severity += 1
details['fng'] = f'{fng:.1f} (extreme fear, confirmed, +1 signal, +1 severity)'
elif fng < config['thresholds']['fng_fear'] and (funding_bearish or dvol_elevated):
signals += 0.5
severity += 0.5
details['fng'] = f'{fng:.1f} (fear, confirmed, +0.5 signal, +0.5 severity)'
elif fng < config['thresholds']['fng_extreme_fear']:
details['fng'] = f'{fng:.1f} (extreme fear, NOT confirmed by funding/DVOL)'
elif fng < config['thresholds']['fng_fear']:
details['fng'] = f'{fng:.1f} (fear, NOT confirmed by funding/DVOL)'
else:
details['fng'] = f'{fng:.1f} (neutral/greed)'
# Signal 4: Taker ratio (strongest predictor - Cohen's d = 3.57)
# This signal always counts (strongest discriminator)
taker = ext_factors.get('taker', 1.0)
if taker < config['thresholds']['taker_selling']:
signals += 1
severity += 2
details['taker'] = f'{taker:.3f} (heavy selling, +1 signal, +2 severity)'
elif taker < config['thresholds']['taker_mild_selling']:
signals += 0.5
severity += 1
details['taker'] = f'{taker:.3f} (mild selling, +0.5 signal, +1 severity)'
else:
details['taker'] = f'{taker:.3f} (neutral/buying)'
# Calculate cut based on signal count and severity
# NORMAL DAYS (0 signals): 0% cut (full position size)
if signals >= 3 and severity >= 5:
cut = 0.75 # Extreme stress (3+ signals, high severity)
elif signals >= 3:
cut = 0.65 # High stress (3+ signals, moderate severity)
elif signals >= 2 and severity >= 3:
cut = 0.55 # Moderate-high stress (2+ signals, high severity)
elif signals >= 2:
cut = 0.45 # Moderate stress (2+ signals)
elif signals >= 1:
cut = 0.30 # Mild stress (1 signal)
else:
cut = 0.0 # Normal (0 signals) = NO CUT
details['signals'] = signals
details['severity'] = severity
details['base_cut'] = config['base_cut']
return cut, signals, severity, details
def apply_circuit_breaker(strategy: Strategy, cut_pct: float) -> Strategy:
"""Apply position size reduction to strategy."""
new_fraction = strategy.fraction * (1 - cut_pct)
return replace(strategy, fraction=new_fraction)
# ══════════════════════════════════════════════════════════════════════
# PAPER TRADING ENGINE
# ══════════════════════════════════════════════════════════════════════
def run_paper_portfolio(df, strategies, init_capital=INIT_CAPITAL,
use_acb=True, acb_config=None, verbose=True,
use_mc_forewarn=False, forewarner=None):
"""Run paper trading with optional Adaptive CB v4 and MC Forewarning."""
acb_config = acb_config or ACBV2_CONFIG
df = df.copy()
if 'date_str' not in df.columns:
df['date_str'] = df['timestamp'].dt.date.astype(str)
dates = sorted(df['date_str'].unique())
if verbose:
mode = "ADAPTIVE CB v4 (META-ADAPTIVE LAGS)" if use_acb else "CB DISABLED (baseline)"
if use_mc_forewarn:
mode += " + MC FOREWARNING"
print(f" Paper trading {len(dates)} days, {len(strategies)} strategies")
print(f" Mode: {mode}")
print(f" Initial capital: ${init_capital:,.2f}")
print()
all_daily_vals = {}
if use_acb:
print(" Prefetching all external factors for latency-aware v4 lag reduction...")
for ds in dates:
all_daily_vals[ds] = load_external_factors_fast(ds)
portfolio = {}
for sname in strategies:
portfolio[sname] = {
'capital': init_capital,
'total_trades': 0,
'total_wins': 0,
'total_fees': 0.0,
'total_slippage': 0.0,
'peak_capital': init_capital,
'max_drawdown_pct': 0.0,
'daily_log': [],
'winning_days': 0,
'losing_days': 0,
'flat_days': 0,
}
acb_log = []
for day_idx, date_str in enumerate(dates):
df_day = df[df['date_str'] == date_str].copy()
n_rows = len(df_day)
ext_factors = {}
adaptive_cut = 0.0
signal_count = 0
severity = 0
acb_details = {}
if use_acb and n_rows >= 200:
ext_factors = load_external_factors_lagged(date_str, all_daily_vals, dates)
if ext_factors:
adaptive_cut, signal_count, severity, acb_details = calculate_adaptive_cut_v4(ext_factors, acb_config)
acb_log.append({
'date': date_str,
'cut_pct': adaptive_cut,
'signals': signal_count,
'severity': severity,
'funding_btc': ext_factors.get('funding_btc', np.nan),
'dvol_btc': ext_factors.get('dvol_btc', np.nan),
'fng': ext_factors.get('fng', np.nan),
'taker': ext_factors.get('taker', np.nan),
'details': acb_details,
})
if n_rows < 200:
for sname in strategies:
p = portfolio[sname]
p['daily_log'].append({
'day': day_idx + 1,
'date': date_str,
'rows': n_rows,
'skipped': True,
'reason': 'sparse_data',
'capital_start': p['capital'],
'capital_end': p['capital'],
'day_pnl': 0.0,
'day_roi_pct': 0.0,
'trades': 0,
'wins': 0,
'win_rate': 0.0,
'pf': 0.0,
'day_fees': 0.0,
'day_slippage': 0.0,
'tp_exits': 0,
'hold_exits': 0,
'adaptive_cut': 0.0,
'mc_red_alert': False,
'mc_orange_alert': False,
'cumulative_roi_pct': (p['capital'] - init_capital) / init_capital * 100,
'drawdown_pct': 0.0,
})
p['flat_days'] += 1
continue
for sname, strategy in strategies.items():
p = portfolio[sname]
cap_start = p['capital']
if use_acb and adaptive_cut > 0:
adjusted_strategy = apply_circuit_breaker(strategy, adaptive_cut)
else:
adjusted_strategy = strategy
mc_red_alert = False
mc_orange_alert = False
if use_mc_forewarn and forewarner is not None:
cfg_dict = {
'trial_id': 0,
'vel_div_threshold': adjusted_strategy.vel_div_threshold,
'vel_div_extreme': -0.050,
'use_direction_confirm': adjusted_strategy.use_direction_confirm,
'dc_lookback_bars': adjusted_strategy.dc_lookback_bars,
'dc_min_magnitude_bps': adjusted_strategy.dc_min_magnitude_bps,
'dc_skip_contradicts': adjusted_strategy.dc_skip_contradicts,
'dc_leverage_boost': adjusted_strategy.dc_leverage_boost,
'dc_leverage_reduce': adjusted_strategy.dc_leverage_reduce,
'vd_trend_lookback': 10,
'min_leverage': adjusted_strategy.min_leverage,
'max_leverage': adjusted_strategy.max_leverage,
'leverage_convexity': adjusted_strategy.leverage_convexity,
'fraction': adjusted_strategy.fraction,
'use_alpha_layers': adjusted_strategy.use_alpha_layers,
'use_dynamic_leverage': adjusted_strategy.dynamic_leverage,
'fixed_tp_pct': adjusted_strategy.fixed_tp_pct if adjusted_strategy.use_fixed_tp else 0.0099,
'stop_pct': adjusted_strategy.stop_pct,
'max_hold_bars': adjusted_strategy.max_hold,
'use_sp_fees': adjusted_strategy.use_sp_fees,
'use_sp_slippage': adjusted_strategy.use_sp_slippage,
'sp_maker_entry_rate': 0.62,
'sp_maker_exit_rate': 0.50,
'use_ob_edge': adjusted_strategy.use_ob_edge,
'ob_edge_bps': adjusted_strategy.ob_edge_bps,
'ob_confirm_rate': 0.40,
'ob_imbalance_bias': -0.09,
'ob_depth_scale': 1.00,
'use_asset_selection': adjusted_strategy.use_asset_selection,
'min_irp_alignment': adjusted_strategy.min_irp_alignment,
'lookback': 100,
'acb_beta_high': 0.80,
'acb_beta_low': 0.20,
'acb_w750_threshold_pct': 60,
}
report = forewarner.assess_config_dict(cfg_dict)
if report.catastrophic_probability > 0.25 or report.envelope_score < -1.0:
mc_red_alert = True
elif report.envelope_score < 0 or report.catastrophic_probability > 0.10:
mc_orange_alert = True
adjusted_strategy = replace(adjusted_strategy, fraction=adjusted_strategy.fraction * 0.5)
if mc_red_alert:
result = {
'capital': cap_start,
'trades': 0, 'wins': 0, 'win_rate': 0.0, 'profit_factor': 0.0,
'total_fees': 0.0, 'total_slippage_cost': 0.0,
'tp_exits': 0, 'hold_exits': 0
}
else:
result = run_full_backtest(
df_day, adjusted_strategy,
init_cash=cap_start,
seed=42,
verbose=False,
)
cap_end = result['capital']
day_pnl = cap_end - cap_start
day_roi = day_pnl / cap_start * 100 if cap_start > 0 else 0
trades = result['trades']
wins = result['wins']
wr = result['win_rate']
pf = result['profit_factor']
fees = result['total_fees']
slippage = result['total_slippage_cost']
tp_exits = result.get('tp_exits', 0)
hold_exits = result.get('hold_exits', 0)
p['capital'] = cap_end
p['total_trades'] += trades
p['total_wins'] += wins
p['total_fees'] += fees
p['total_slippage'] += slippage
if cap_end > p['peak_capital']:
p['peak_capital'] = cap_end
drawdown = (p['peak_capital'] - cap_end) / p['peak_capital'] * 100
if drawdown > p['max_drawdown_pct']:
p['max_drawdown_pct'] = drawdown
if day_pnl > 0.01:
p['winning_days'] += 1
elif day_pnl < -0.01:
p['losing_days'] += 1
else:
p['flat_days'] += 1
cumulative_roi = (cap_end - init_capital) / init_capital * 100
p['daily_log'].append({
'day': day_idx + 1,
'date': date_str,
'rows': n_rows,
'skipped': False,
'capital_start': round(cap_start, 2),
'capital_end': round(cap_end, 2),
'day_pnl': round(day_pnl, 2),
'day_roi_pct': round(day_roi, 4),
'trades': trades,
'wins': wins,
'win_rate': round(wr, 2),
'pf': round(pf, 4),
'day_fees': round(fees, 2),
'day_slippage': round(slippage, 2),
'tp_exits': tp_exits,
'hold_exits': hold_exits,
'adaptive_cut': round(adaptive_cut, 2),
'acb_signals': signal_count,
'acb_severity': severity,
'mc_red_alert': mc_red_alert,
'mc_orange_alert': mc_orange_alert,
'cumulative_roi_pct': round(cumulative_roi, 4),
'drawdown_pct': round(drawdown, 4),
'peak_capital': round(p['peak_capital'], 2),
})
if verbose and ((day_idx + 1) % 10 == 0 or day_idx == len(dates) - 1):
caps = {sn: f"${portfolio[sn]['capital']:,.0f}" for sn in strategies}
cut_info = f" [ACBv2:{adaptive_cut:.0%}|S:{signal_count}]" if use_acb and adaptive_cut > 0 else ""
print(f" Day {day_idx+1}/{len(dates)} ({date_str}){cut_info}: {caps}")
return portfolio, dates, acb_log
def generate_summary(portfolio, strategies, dates, init_capital, acb_log=None):
"""Generate per-strategy summary stats."""
summaries = {}
for sname in strategies:
p = portfolio[sname]
total_roi = (p['capital'] - init_capital) / init_capital * 100
active_days = p['winning_days'] + p['losing_days']
win_day_pct = p['winning_days'] / max(active_days, 1) * 100
avg_daily_roi = total_roi / max(len(dates), 1)
total_wr = p['total_wins'] / max(p['total_trades'], 1) * 100
daily_rets = [d['day_roi_pct'] for d in p['daily_log'] if not d.get('skipped')]
if len(daily_rets) > 1:
sharpe = np.mean(daily_rets) / max(np.std(daily_rets, ddof=1), 1e-8)
sharpe_annual = sharpe * np.sqrt(365)
else:
sharpe_annual = 0.0
streak_w = 0
streak_l = 0
max_streak_w = 0
max_streak_l = 0
for d in p['daily_log']:
if d.get('skipped'):
continue
if d['day_pnl'] > 0.01:
streak_w += 1
streak_l = 0
elif d['day_pnl'] < -0.01:
streak_l += 1
streak_w = 0
else:
streak_w = 0
streak_l = 0
max_streak_w = max(max_streak_w, streak_w)
max_streak_l = max(max_streak_l, streak_l)
active_logs = [d for d in p['daily_log'] if not d.get('skipped')]
best_day = max(active_logs, key=lambda d: d['day_pnl']) if active_logs else {}
worst_day = min(active_logs, key=lambda d: d['day_pnl']) if active_logs else {}
acb_cuts = [d.get('adaptive_cut', 0) for d in p['daily_log'] if not d.get('skipped')]
avg_acb_cut = np.mean(acb_cuts) if acb_cuts else 0.0
max_acb_cut = max(acb_cuts) if acb_cuts else 0.0
summaries[sname] = {
'strategy_params': {
'max_leverage': strategies[sname].max_leverage,
'fraction': strategies[sname].fraction,
'convexity': strategies[sname].leverage_convexity,
},
'performance': {
'init_capital': init_capital,
'final_capital': round(p['capital'], 2),
'total_roi_pct': round(total_roi, 4),
'total_pnl': round(p['capital'] - init_capital, 2),
'total_trades': p['total_trades'],
'total_wins': p['total_wins'],
'total_win_rate': round(total_wr, 2),
},
'risk': {
'max_drawdown_pct': round(p['max_drawdown_pct'], 4),
'peak_capital': round(p['peak_capital'], 2),
'sharpe_annual': round(sharpe_annual, 4),
'winning_days': p['winning_days'],
'losing_days': p['losing_days'],
'win_day_pct': round(win_day_pct, 2),
},
'best_day': {
'date': best_day.get('date', ''),
'pnl': best_day.get('day_pnl', 0),
},
'worst_day': {
'date': worst_day.get('date', ''),
'pnl': worst_day.get('day_pnl', 0),
},
'acb_stats': {
'avg_cut_pct': round(avg_acb_cut * 100, 2),
'max_cut_pct': round(max_acb_cut * 100, 2),
},
}
return summaries
def main():
parser = argparse.ArgumentParser(description='DOLPHIN Paper Trading with Adaptive CB v2')
parser.add_argument('--no-cb', action='store_true', help='Run WITHOUT circuit breaker')
parser.add_argument('--mc-forewarn', action='store_true', help='Enable MC Forewarning ML System')
parser.add_argument('--compare', action='store_true', help='Run both and compare')
args = parser.parse_args()
print("=" * 80)
print("DOLPHIN PAPER TRADING — ADAPTIVE CIRCUIT BREAKER v4 & MC-FOREWARNER")
print("Multi-signal confirmation approach & ML Geometry Check")
print("=" * 80)
print("\nLoading data...")
df = load_all_data()
print(f"Loaded: {len(df):,} rows")
if args.compare:
print("\n" + "=" * 80)
print("RUNNING BASELINE (NO CB)")
print("=" * 80)
portfolio_base, dates, _ = run_paper_portfolio(
df, STRATEGIES, INIT_CAPITAL, use_acb=False, use_mc_forewarn=False, verbose=True
)
summaries_base = generate_summary(portfolio_base, STRATEGIES, dates, INIT_CAPITAL)
print("\n" + "=" * 80)
print("RUNNING ADAPTIVE CB v4 (Meta-Adaptive Lags)")
print("=" * 80)
portfolio_acb, dates, acb_log = run_paper_portfolio(
df, STRATEGIES, INIT_CAPITAL, use_acb=True, use_mc_forewarn=False, verbose=True
)
summaries_acb = generate_summary(portfolio_acb, STRATEGIES, dates, INIT_CAPITAL, acb_log)
if args.mc_forewarn:
print("\n" + "=" * 80)
print("RUNNING ADAPTIVE CB v4 + MC FOREWARNER")
print("=" * 80)
forewarner = DolphinForewarner(models_dir=str(Path(__file__).parent / "nautilus_dolphin" / "mc_results" / "models"))
portfolio_mc, dates_mc, acb_log_mc = run_paper_portfolio(
df, STRATEGIES, INIT_CAPITAL, use_acb=True, use_mc_forewarn=True, forewarner=forewarner, verbose=True
)
summaries_mc = generate_summary(portfolio_mc, STRATEGIES, dates_mc, INIT_CAPITAL, acb_log_mc)
# Comparison
print("\n" + "=" * 80)
print("COMPARISON: Baseline vs Adaptive CB v4" + (" vs MC" if args.mc_forewarn else ""))
print("=" * 80)
if args.mc_forewarn:
print(f"{'Strategy':<25} {'No CB':<12} {'ACB v4':<12} {'MC-Forewarn':<12}")
else:
print(f"{'Strategy':<25} {'No CB':<12} {'ACB v4':<12} {'Delta':<12} {'ACB Cut':<10}")
print("-" * 80)
for sname in STRATEGIES.keys():
base_roi = summaries_base[sname]['performance']['total_roi_pct']
acb_roi = summaries_acb[sname]['performance']['total_roi_pct']
if args.mc_forewarn:
mc_roi = summaries_mc[sname]['performance']['total_roi_pct']
print(f"{sname:<25} {base_roi:>+10.2f}% {acb_roi:>+10.2f}% {mc_roi:>+10.2f}%")
else:
acb_cut = summaries_acb[sname]['acb_stats']['avg_cut_pct']
print(f"{sname:<25} {base_roi:>+10.2f}% {acb_roi:>+10.2f}% {acb_roi-base_roi:>+10.2f}% {acb_cut:>8.1f}%")
print("\n--- ACB v2 DECISIONS (last 10) ---")
for log in acb_log[-10:]:
print(f" {log['date']}: {log['cut_pct']:.0%} cut ({log['signals']:.1f} signals, severity={log['severity']})")
else:
use_acb = not args.no_cb
use_mc = args.mc_forewarn
mode_str = "ADAPTIVE CB v4 + MC FOREWARN" if use_mc else ("ADAPTIVE CB v4" if use_acb else "NO CB (baseline)")
print(f"\nRunning: {mode_str}")
forewarner = DolphinForewarner(models_dir=str(Path(__file__).parent / "nautilus_dolphin" / "mc_results" / "models")) if use_mc else None
t0 = time.time()
portfolio, dates, acb_log = run_paper_portfolio(
df, STRATEGIES, INIT_CAPITAL, use_acb=use_acb, use_mc_forewarn=use_mc, forewarner=forewarner, verbose=True
)
elapsed = time.time() - t0
summaries = generate_summary(portfolio, STRATEGIES, dates, INIT_CAPITAL, acb_log)
print(f"\n{'='*80}")
print(f"RESULTS — {mode_str}")
print(f"{'='*80}")
print(f"Period: {dates[0]} to {dates[-1]} ({len(dates)} days)")
print(f"Time: {elapsed:.0f}s")
print(f"\n{'Strategy':<25} {'Final $':>10} {'ROI':>8} {'Trades':>7} {'WR%':>6} {'MaxDD':>7} {'Sharpe':>7}")
print("-" * 90)
for sname, s in summaries.items():
perf = s['performance']
risk = s['risk']
print(f"{sname:<25} ${perf['final_capital']:>9,.0f} "
f"{perf['total_roi_pct']:>+7.1f}% "
f"{perf['total_trades']:>6} "
f"{perf['total_win_rate']:>5.1f} "
f"{risk['max_drawdown_pct']:>6.1f}% "
f"{risk['sharpe_annual']:>6.2f}")
if use_acb and acb_log:
print("\n--- ACB v2 DECISIONS ---")
for log in acb_log[-10:]:
print(f" {log['date']}: {log['cut_pct']:.0%} cut ({log['signals']:.1f} signals, sev={log['severity']})")
print(f"\n{'='*80}")
print("DONE")
print(f"{'='*80}")
if __name__ == '__main__':
main()

File diff suppressed because it is too large Load Diff

View File

@@ -1,430 +0,0 @@
Now before following, if the new esoteric factor engine is modeled after the established factor engine, it should have fallback feeling, backfilling, backfilling capabilities. Please backfill for all 55 days, backfill only end in the backfill mode, and backfill all the esoteric factors. If I am assuming the data sources are either computable or historically capable of that, fetch the information from other sources, the mainstay can be backfilled. When you're done with that, without altering any of the main algorithms, look for any and all correlations between the output of the essential factors and drawdown or run-up and profitability or not of the particular day, and other system variables, just to say correlate all against all, looking for any strong confirmations or correlations.
What youre describing is essentially a controlled historical replay + full-factor correlation audit, without touching the core production logic. Ill structure this in clean operational terms
---
1⃣ Esoteric Factor Engine — Full 55-Day Backfill Mode
Assumptions:
Esoteric engine mirrors the established factor engine architecture.
All data sources are either:
Deterministically computable from price/volume history, or
Historically retrievable from external APIs or stored archives.
No modification to factor formulas.
---
A. Backfill Design Principles
Mode: BACKFILL_ONLY_END
This means:
Replay day-by-day from T-55 → T-1.
Compute all esoteric factors exactly as if live.
Write values to historical store.
No strategy decisions executed.
No incremental learning updates.
No state mutation except factor history buffers.
---
B. Fallback Hierarchy
For each esoteric factor:
1. Primary historical source
(official data store / archive)
2. Secondary API historical endpoint
3. Deterministic reconstruction
Recompute from base OHLCV
Reconstruct state from rolling window
4. Synthetic proxy fallback
Only if mathematically derivable
Must be flagged as fallback_level = 3
You log fallback level for each factor/day.
---
C. Backfill Procedure
Step 1 — Freeze Production State
Snapshot:
Rolling buffers
Latent embeddings (if any)
Volatility states
Regime states
Step 2 — Initialize Clean Historical Buffers
Clear only:
Esoteric factor buffers
Derived rolling statistics
Keep:
Core algorithm logic intact.
---
Step 3 — Replay Loop (55 days)
For each day d from T-55 to T-1:
1. Load OHLCV
2. Fetch or reconstruct all auxiliary inputs
3. Compute:
All esoteric factors
All intermediate states
Any smoothed or filtered outputs
Any latent representations
4. Append to:
esoteric_factor_store[d]
factor_output_store[d]
system_state_snapshot[d]
No trading logic triggered.
---
Step 4 — Integrity Check
After replay:
Check no NaNs
Check window completeness
Check rolling windows properly warm
Confirm fallback levels < threshold (ideally mostly 0/1)
---
2 Correlation Audit All Against All
Now comes the analytical part.
We compute correlations between:
A. Target Variables
Per day:
Max intraday drawdown
Max intraday run-up
Close-to-close return
Strategy P&L
Win/Loss (binary)
Profitability magnitude
Volatility
Spread
Liquidity proxy
Regime label
Latent manifold distortion (if present)
Drift velocity (if you implemented that earlier idea)
---
B. Factor Groups
1. Core factors
2. Esoteric factors
3. Combined outputs
4. Latent embeddings (if available)
5. Signal strength
6. Conviction metrics
7. Confidence weighting
8. Any internal risk throttles
---
C. Correlation Types to Compute
You want more than Pearson.
Compute:
1. Pearson correlation
2. Spearman rank correlation
3. Kendall tau
4. Mutual information
5. Distance correlation
6. Rolling correlation (7, 14, 30 days)
7. Lagged correlation 1, ±2, ±3 days)
---
D. Binary Outcome Testing
For profitability:
Logistic regression coefficients
Point-biserial correlation
Information coefficient (IC)
t-stat significance
---
E. Cross-Correlation Matrix
You compute:
corr_matrix = corr(all_factors all_targets)
Then:
Extract |corr| > 0.6
Flag p < 0.05
Flag stable correlations across rolling windows
Flag correlations that persist across fallback levels
---
3 What Youre Actually Looking For
This audit answers:
1. Do esoteric factors anticipate drawdown?
2. Do they amplify run-up?
3. Are they redundant with core factors?
4. Are they orthogonal alpha?
5. Do they correlate only in stressed regimes?
6. Do they degrade performance in low-vol days?
7. Do latent geometry distortions align with profitability?
---
4 Interpretation Layer
You classify correlations into:
Strong Confirmations
Stable across windows
Significant
Not present in core-only model
Conditional Correlations
Appear only in high volatility
Appear only in drawdown clusters
Spurious / Structural
Correlate due to shared base data
Dangerous
Correlate negatively with profitability
Increase drawdown magnitude
---
5 Deliverables You Should Generate
1. Heatmap of full correlation matrix
2. Ranked factor impact table
3. Stability score per factor
4. Redundancy map (clustered)
5. Regime-conditional breakdown
6. Factor drawdown predictive ranking
7. Factor run-up predictive ranking
---
6 Critical Warning
Do NOT:
Change algorithm weights.
Remove factors.
Normalize differently.
Retrain anything.
This is purely diagnostic.
---
7 What This Tells You Strategically
If strong correlation emerges between:
Esoteric manifold distortion and drawdown
youve built a stress sensor.
If strong correlation emerges between:
Drift velocity and next-day profitability
you have regime anticipation.
If esoteric factors are mostly redundant
compress the engine.
If orthogonal and stable
youve added real signal dept

View File

@@ -1,466 +0,0 @@
#!/usr/bin/env python3
"""
DOLPHIN BACKFILL RUNNER v2.0
============================
Spiders DOLPHIN scan directories, enriches with external factors matrix.
INDICATOR SOURCES:
1. API_HISTORICAL: Fetched with scan timestamp (CoinMetrics, FRED, DeFi Llama, etc.)
2. SCAN_DERIVED: Computed from scan's market_prices, tracking_data, per_asset_signals
3. UNAVAILABLE: No historical API AND cannot compute from scan → NaN
Output: {original_name}__Indicators.npz (sorts alphabetically next to source)
Author: HJ / Claude
Version: 2.0.0
"""
import os
import sys
import json
import numpy as np
import asyncio
import aiohttp
from pathlib import Path
from datetime import datetime, timezone
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, Any, Set
import logging
import time
import argparse
# Import external factors module
from external_factors_matrix import (
ExternalFactorsFetcher, Config, INDICATORS, N_INDICATORS,
HistoricalSupport, Stationarity, Category
)
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')
logger = logging.getLogger(__name__)
# =============================================================================
# INDICATOR SOURCE CLASSIFICATION
# =============================================================================
class IndicatorSource:
"""Classifies each indicator by how it can be obtained for backfill"""
# Indicators that HAVE historical API support (fetch with timestamp)
API_HISTORICAL: Set[int] = set()
# Indicators that are UNAVAILABLE (no history, can't derive from scan)
UNAVAILABLE: Set[int] = set()
@classmethod
def classify(cls):
"""Classify all indicators by their backfill source"""
for ind in INDICATORS:
if ind.historical in [HistoricalSupport.FULL, HistoricalSupport.PARTIAL]:
cls.API_HISTORICAL.add(ind.id)
else:
cls.UNAVAILABLE.add(ind.id)
logger.info(f"Indicator sources: API_HISTORICAL={len(cls.API_HISTORICAL)}, "
f"UNAVAILABLE={len(cls.UNAVAILABLE)}")
@classmethod
def get_unavailable_names(cls) -> List[str]:
return [INDICATORS[i-1].name for i in sorted(cls.UNAVAILABLE)]
# Initialize classification
IndicatorSource.classify()
# =============================================================================
# CONFIGURATION
# =============================================================================
@dataclass
class BackfillConfig:
scan_dir: Path(r"C:\Users\Lenovo\Documents\- Dolphin NG HD (NG3)\correlation_arb512\eigenvalues")
output_dir: Optional[str] = None
skip_existing: bool = True
dry_run: bool = False
fred_api_key: str = ""
rate_limit_delay: float = 0.5
verbose: bool = False
# =============================================================================
# SCAN DATA
# =============================================================================
@dataclass
class ScanData:
path: Path
scan_number: int
timestamp: datetime
market_prices: Dict[str, float]
windows: Dict[str, Dict]
@property
def n_assets(self) -> int:
return len(self.market_prices)
@property
def symbols(self) -> List[str]:
return sorted(self.market_prices.keys())
def get_tracking(self, window: str) -> Dict:
return self.windows.get(window, {}).get('tracking_data', {})
def get_regime(self, window: str) -> Dict:
return self.windows.get(window, {}).get('regime_signals', {})
def get_asset_signals(self, window: str) -> Dict:
return self.windows.get(window, {}).get('per_asset_signals', {})
# =============================================================================
# INDICATORS FROM SCAN DATA
# =============================================================================
WINDOWS = ['50', '150', '300', '750']
# Global scan-derived indicators (eigenvalue-based, from tracking_data/regime_signals)
SCAN_GLOBAL_INDICATORS = [
# Lambda max per window
*[(f"lambda_max_w{w}", f"Lambda max window {w}") for w in WINDOWS],
*[(f"lambda_min_w{w}", f"Lambda min window {w}") for w in WINDOWS],
*[(f"lambda_vel_w{w}", f"Lambda velocity window {w}") for w in WINDOWS],
*[(f"lambda_acc_w{w}", f"Lambda acceleration window {w}") for w in WINDOWS],
*[(f"eigrot_max_w{w}", f"Eigenvector rotation window {w}") for w in WINDOWS],
*[(f"eiggap_w{w}", f"Eigenvalue gap window {w}") for w in WINDOWS],
*[(f"instab_w{w}", f"Instability window {w}") for w in WINDOWS],
*[(f"transp_w{w}", f"Transition prob window {w}") for w in WINDOWS],
*[(f"coher_w{w}", f"Coherence window {w}") for w in WINDOWS],
# Aggregates
("lambda_max_mean", "Mean lambda max"),
("lambda_max_std", "Std lambda max"),
("instab_mean", "Mean instability"),
("instab_max", "Max instability"),
("coher_mean", "Mean coherence"),
("coher_min", "Min coherence"),
("coher_trend", "Coherence trend (w750-w50)"),
# From prices
("n_assets", "Number of assets"),
("price_dispersion", "Log price dispersion"),
]
N_SCAN_GLOBAL = len(SCAN_GLOBAL_INDICATORS)
# Per-asset indicators
PER_ASSET_INDICATORS = [
("price", "Price"),
("log_price", "Log price"),
("price_rank", "Price percentile"),
("price_btc", "Price / BTC"),
("price_eth", "Price / ETH"),
*[(f"align_w{w}", f"Alignment w{w}") for w in WINDOWS],
*[(f"decouple_w{w}", f"Decoupling w{w}") for w in WINDOWS],
*[(f"anomaly_w{w}", f"Anomaly w{w}") for w in WINDOWS],
*[(f"eigvec_w{w}", f"Eigenvector w{w}") for w in WINDOWS],
("align_mean", "Mean alignment"),
("align_std", "Alignment std"),
("anomaly_max", "Max anomaly"),
("decouple_max", "Max |decoupling|"),
]
N_PER_ASSET = len(PER_ASSET_INDICATORS)
# =============================================================================
# PROCESSOR
# =============================================================================
class ScanProcessor:
def __init__(self, config: BackfillConfig):
self.config = config
self.fetcher = ExternalFactorsFetcher(Config(fred_api_key=config.fred_api_key))
def load_scan(self, path: Path) -> Optional[ScanData]:
try:
with open(path, 'r') as f:
data = json.load(f)
ts_str = data.get('timestamp', '')
try:
timestamp = datetime.fromisoformat(ts_str.replace('Z', '+00:00'))
if timestamp.tzinfo is None:
timestamp = timestamp.replace(tzinfo=timezone.utc)
except:
timestamp = datetime.now(timezone.utc)
return ScanData(
path=path,
scan_number=data.get('scan_number', 0),
timestamp=timestamp,
market_prices=data.get('market_prices', {}),
windows=data.get('windows', {})
)
except Exception as e:
logger.error(f"Load failed {path}: {e}")
return None
async def fetch_api_indicators(self, timestamp: datetime) -> Tuple[np.ndarray, np.ndarray]:
"""Fetch indicators with historical API support"""
try:
result = await self.fetcher.fetch_all(target_date=timestamp)
matrix = result['matrix']
success = np.array([
result['details'].get(i+1, {}).get('success', False)
for i in range(N_INDICATORS)
])
# Mark non-historical indicators as NaN
for i in range(N_INDICATORS):
if (i+1) not in IndicatorSource.API_HISTORICAL:
success[i] = False
matrix[i] = np.nan
return matrix, success
except Exception as e:
logger.warning(f"API fetch failed: {e}")
return np.full(N_INDICATORS, np.nan), np.zeros(N_INDICATORS, dtype=bool)
def compute_scan_global(self, scan: ScanData) -> np.ndarray:
"""Compute global indicators from scan's tracking_data and regime_signals"""
values = []
# Per-window metrics
for w in WINDOWS:
values.append(scan.get_tracking(w).get('lambda_max', np.nan))
for w in WINDOWS:
values.append(scan.get_tracking(w).get('lambda_min', np.nan))
for w in WINDOWS:
values.append(scan.get_tracking(w).get('lambda_max_velocity', np.nan))
for w in WINDOWS:
values.append(scan.get_tracking(w).get('lambda_max_acceleration', np.nan))
for w in WINDOWS:
values.append(scan.get_tracking(w).get('eigenvector_rotation_max', np.nan))
for w in WINDOWS:
values.append(scan.get_tracking(w).get('eigenvalue_gap', np.nan))
for w in WINDOWS:
values.append(scan.get_regime(w).get('instability_score', np.nan))
for w in WINDOWS:
values.append(scan.get_regime(w).get('regime_transition_probability', np.nan))
for w in WINDOWS:
values.append(scan.get_regime(w).get('market_coherence', np.nan))
# Aggregates
lmax = [scan.get_tracking(w).get('lambda_max', np.nan) for w in WINDOWS]
values.append(np.nanmean(lmax))
values.append(np.nanstd(lmax))
instab = [scan.get_regime(w).get('instability_score', np.nan) for w in WINDOWS]
values.append(np.nanmean(instab))
values.append(np.nanmax(instab))
coher = [scan.get_regime(w).get('market_coherence', np.nan) for w in WINDOWS]
values.append(np.nanmean(coher))
values.append(np.nanmin(coher))
values.append(coher[3] - coher[0] if not np.isnan(coher[3]) and not np.isnan(coher[0]) else np.nan)
# From prices
prices = np.array(list(scan.market_prices.values())) if scan.market_prices else np.array([])
values.append(len(prices))
values.append(np.std(np.log(np.maximum(prices, 1e-10))) if len(prices) > 0 else np.nan)
return np.array(values)
def compute_per_asset(self, scan: ScanData) -> Tuple[np.ndarray, List[str]]:
"""Compute per-asset indicator matrix"""
symbols = scan.symbols
n = len(symbols)
if n == 0:
return np.zeros((0, N_PER_ASSET)), []
matrix = np.zeros((n, N_PER_ASSET))
prices = np.array([scan.market_prices[s] for s in symbols])
btc_p = scan.market_prices.get('BTC', scan.market_prices.get('BTCUSDT', np.nan))
eth_p = scan.market_prices.get('ETH', scan.market_prices.get('ETHUSDT', np.nan))
col = 0
matrix[:, col] = prices; col += 1
matrix[:, col] = np.log(np.maximum(prices, 1e-10)); col += 1
matrix[:, col] = np.argsort(np.argsort(prices)) / n; col += 1
matrix[:, col] = prices / btc_p if btc_p > 0 else np.nan; col += 1
matrix[:, col] = prices / eth_p if eth_p > 0 else np.nan; col += 1
# Per-window signals
for metric in ['market_alignment', 'decoupling_velocity', 'anomaly_score', 'eigenvector_component']:
for w in WINDOWS:
sigs = scan.get_asset_signals(w)
for i, sym in enumerate(symbols):
matrix[i, col] = sigs.get(sym, {}).get(metric, np.nan)
col += 1
# Aggregates
align_cols = list(range(5, 9))
matrix[:, col] = np.nanmean(matrix[:, align_cols], axis=1); col += 1
matrix[:, col] = np.nanstd(matrix[:, align_cols], axis=1); col += 1
anomaly_cols = list(range(13, 17))
matrix[:, col] = np.nanmax(matrix[:, anomaly_cols], axis=1); col += 1
decouple_cols = list(range(9, 13))
matrix[:, col] = np.nanmax(np.abs(matrix[:, decouple_cols]), axis=1); col += 1
return matrix, symbols
async def process(self, path: Path) -> Optional[Dict[str, Any]]:
start = time.time()
scan = self.load_scan(path)
if scan is None:
return None
# 1. API historical indicators
api_matrix, api_success = await self.fetch_api_indicators(scan.timestamp)
# 2. Scan-derived global
scan_global = self.compute_scan_global(scan)
# 3. Per-asset
asset_matrix, asset_symbols = self.compute_per_asset(scan)
return {
'scan_number': scan.scan_number,
'timestamp': scan.timestamp.isoformat(),
'processing_time': time.time() - start,
'api_indicators': api_matrix,
'api_success': api_success,
'api_names': np.array([ind.name for ind in INDICATORS], dtype='U32'),
'scan_global': scan_global,
'scan_global_names': np.array([n for n, _ in SCAN_GLOBAL_INDICATORS], dtype='U32'),
'asset_matrix': asset_matrix,
'asset_symbols': np.array(asset_symbols, dtype='U16'),
'asset_names': np.array([n for n, _ in PER_ASSET_INDICATORS], dtype='U32'),
'n_assets': len(asset_symbols),
'api_success_rate': np.nanmean(api_success[list(i-1 for i in IndicatorSource.API_HISTORICAL)]),
}
# =============================================================================
# OUTPUT
# =============================================================================
class OutputWriter:
def __init__(self, config: BackfillConfig):
self.config = config
def get_output_path(self, scan_path: Path) -> Path:
out_dir = Path(self.config.output_dir) if self.config.output_dir else scan_path.parent
out_dir.mkdir(parents=True, exist_ok=True)
return out_dir / f"{scan_path.stem}__Indicators.npz"
def save(self, data: Dict[str, Any], scan_path: Path) -> Path:
out_path = self.get_output_path(scan_path)
save_data = {}
for k, v in data.items():
if isinstance(v, np.ndarray):
save_data[k] = v
elif isinstance(v, str):
save_data[k] = np.array([v], dtype='U64')
else:
save_data[k] = np.array([v])
np.savez_compressed(out_path, **save_data)
return out_path
# =============================================================================
# RUNNER
# =============================================================================
class BackfillRunner:
def __init__(self, config: BackfillConfig):
self.config = config
self.processor = ScanProcessor(config)
self.writer = OutputWriter(config)
self.stats = {'processed': 0, 'failed': 0, 'skipped': 0}
def find_scans(self) -> List[Path]:
root = Path(self.config.scan_dir)
files = sorted(root.rglob("scan_*.json"))
if self.config.skip_existing:
files = [f for f in files if not self.writer.get_output_path(f).exists()]
return files
async def run(self):
unavail = IndicatorSource.get_unavailable_names()
logger.info(f"Skipping {len(unavail)} unavailable indicators: {unavail[:5]}...")
files = self.find_scans()
logger.info(f"Processing {len(files)} files...")
for i, path in enumerate(files):
try:
result = await self.processor.process(path)
if result:
if not self.config.dry_run:
self.writer.save(result, path)
self.stats['processed'] += 1
else:
self.stats['failed'] += 1
except Exception as e:
logger.error(f"Error {path.name}: {e}")
self.stats['failed'] += 1
if (i + 1) % 10 == 0:
logger.info(f"Progress: {i+1}/{len(files)}")
if self.config.rate_limit_delay > 0:
await asyncio.sleep(self.config.rate_limit_delay)
logger.info(f"Done: {self.stats}")
return self.stats
# =============================================================================
# UTILITY
# =============================================================================
def load_indicators(path: str) -> Dict[str, np.ndarray]:
"""Load .npz indicator file"""
return dict(np.load(path, allow_pickle=True))
def summary(path: str) -> str:
"""Summary of indicator file"""
d = load_indicators(path)
return f"""Timestamp: {d['timestamp'][0]}
Assets: {d['n_assets'][0]}
API success: {d['api_success_rate'][0]:.1%}
API shape: {d['api_indicators'].shape}
Scan global: {d['scan_global'].shape}
Per-asset: {d['asset_matrix'].shape}"""
# =============================================================================
# CLI
# =============================================================================
def main():
parser = argparse.ArgumentParser(description="DOLPHIN Backfill Runner")
# parser.add_argument("scan_dir", help="Directory with scan JSON files")
parser.add_argument("-o", "--output", help="Output directory")
parser.add_argument("--fred-key", default="", help="FRED API key")
parser.add_argument("--no-skip", action="store_true", help="Reprocess existing")
parser.add_argument("--dry-run", action="store_true")
parser.add_argument("--delay", type=float, default=0.5)
args = parser.parse_args()
config = BackfillConfig(
scan_dir= Path(r"C:\Users\Lenovo\Documents\- Dolphin NG HD (NG3)\correlation_arb512\eigenvalues"),
output_dir=args.output,
# FRED API Key: c16a9cde3e3bb5bb972bb9283485f202
fred_api_key=args.fred_key or 'c16a9cde3e3bb5bb972bb9283485f202',
skip_existing=not args.no_skip,
dry_run=args.dry_run,
rate_limit_delay=args.delay,
)
runner = BackfillRunner(config)
asyncio.run(runner.run())
if __name__ == "__main__":
main()

View File

@@ -1 +0,0 @@
"python backfill_runner.py"

View File

@@ -1 +0,0 @@
python backfill_runner.py

View File

@@ -1,46 +0,0 @@
{
"timestamp": "2026-03-01T21:34:06.686948+00:00",
"unix": 1772400846,
"calendar": {
"year": 2026,
"month": 3,
"day_of_month": 1,
"hour": 21,
"minute": 34,
"day_of_week": 6,
"week_of_year": 9
},
"fibonacci_time": {
"closest_fib_minute": 1597,
"harmonic_strength": 0.0
},
"regional_times": {
"Americas": {
"hour": 16.566666666666666,
"is_tradfi_open": false
},
"EMEA": {
"hour": 21.566666666666666,
"is_tradfi_open": false
},
"South_Asia": {
"hour": 3.066666666666667,
"is_tradfi_open": false
},
"East_Asia": {
"hour": 5.566666666666666,
"is_tradfi_open": false
},
"Oceania_SEA": {
"hour": 5.566666666666666,
"is_tradfi_open": false
}
},
"population_weighted_hour": 1.57,
"liquidity_weighted_hour": 21.13,
"liquidity_session": "LOW_LIQUIDITY",
"market_cycle_position": 0.4658,
"moon_illumination": 0.9703631088596449,
"moon_phase_name": "FULL_MOON",
"mercury_retrograde": 1
}

View File

@@ -1,299 +0,0 @@
import asyncio
import datetime
import json
import logging
import math
import threading
import time
import zoneinfo
from pathlib import Path
from typing import Dict, Any, Optional
import numpy as np
from astropy.time import Time
import astropy.coordinates as coord
import astropy.units as u
from astropy.coordinates import solar_system_ephemeris, get_body, EarthLocation
logger = logging.getLogger(__name__)
class MarketIndicators:
"""
Mathematical and astronomical calculations for the Esoteric Factors mapping.
Evaluates completely locally without external API dependencies.
"""
def __init__(self):
# Regions defined by NON-OVERLAPPING population clusters for accurate global weighting.
# Population in Millions (approximate). Liquidity weight is estimated crypto volume share.
self.regions = [
{'name': 'Americas', 'tz': 'America/New_York', 'pop': 1000, 'liq_weight': 0.35},
{'name': 'EMEA', 'tz': 'Europe/London', 'pop': 2200, 'liq_weight': 0.30},
{'name': 'South_Asia', 'tz': 'Asia/Kolkata', 'pop': 1400, 'liq_weight': 0.05},
{'name': 'East_Asia', 'tz': 'Asia/Shanghai', 'pop': 1600, 'liq_weight': 0.20},
{'name': 'Oceania_SEA', 'tz': 'Asia/Singapore', 'pop': 800, 'liq_weight': 0.10}
]
# Market cycle: Bitcoin halving based, ~4 years
self.cycle_length_days = 1460
self.last_halving = datetime.datetime(2024, 4, 20, tzinfo=datetime.timezone.utc)
# Cache for expensive ASTRO calculations
self._cache = {
'moon': {'val': None, 'ts': 0},
'mercury': {'val': None, 'ts': 0}
}
self.cache_ttl_seconds = 3600 * 6 # Update astro every 6 hours
def get_calendar_items(self, now: datetime.datetime) -> Dict[str, int]:
return {
'year': now.year,
'month': now.month,
'day_of_month': now.day,
'hour': now.hour,
'minute': now.minute,
'day_of_week': now.weekday(), # 0=Monday
'week_of_year': now.isocalendar().week
}
def is_tradfi_open(self, region_name: str, local_time: datetime.datetime) -> bool:
day = local_time.weekday()
if day >= 5: return False
hour_dec = local_time.hour + local_time.minute / 60.0
if 'Americas' in region_name:
return 9.5 <= hour_dec < 16.0
elif 'EMEA' in region_name:
return 8.0 <= hour_dec < 16.5
elif 'Asia' in region_name:
return 9.0 <= hour_dec < 15.0
return False
def get_regional_times(self, now_utc: datetime.datetime) -> Dict[str, Any]:
times = {}
for region in self.regions:
tz = zoneinfo.ZoneInfo(region['tz'])
local_time = now_utc.astimezone(tz)
times[region['name']] = {
'hour': local_time.hour + local_time.minute / 60.0,
'is_tradfi_open': self.is_tradfi_open(region['name'], local_time)
}
return times
def get_liquidity_session(self, now_utc: datetime.datetime) -> str:
utc_hour = now_utc.hour + now_utc.minute / 60.0
if 13 <= utc_hour < 17:
return "LONDON_NEW_YORK_OVERLAP"
elif 8 <= utc_hour < 13:
return "LONDON_MORNING"
elif 0 <= utc_hour < 8:
return "ASIA_PACIFIC"
elif 17 <= utc_hour < 21:
return "NEW_YORK_AFTERNOON"
else:
return "LOW_LIQUIDITY"
def get_weighted_times(self, now_utc: datetime.datetime) -> tuple[float, float]:
pop_sin, pop_cos = 0.0, 0.0
liq_sin, liq_cos = 0.0, 0.0
total_pop = sum(r['pop'] for r in self.regions)
for region in self.regions:
tz = zoneinfo.ZoneInfo(region['tz'])
local_time = now_utc.astimezone(tz)
hour_frac = (local_time.hour + local_time.minute / 60.0) / 24.0
angle = 2 * math.pi * hour_frac
w_pop = region['pop'] / total_pop
pop_sin += math.sin(angle) * w_pop
pop_cos += math.cos(angle) * w_pop
w_liq = region['liq_weight']
liq_sin += math.sin(angle) * w_liq
liq_cos += math.cos(angle) * w_liq
pop_angle = math.atan2(pop_sin, pop_cos)
if pop_angle < 0: pop_angle += 2 * math.pi
pop_hour = (pop_angle / (2 * math.pi)) * 24
liq_angle = math.atan2(liq_sin, liq_cos)
if liq_angle < 0: liq_angle += 2 * math.pi
liq_hour = (liq_angle / (2 * math.pi)) * 24
return round(pop_hour, 2), round(liq_hour, 2)
def get_market_cycle_position(self, now_utc: datetime.datetime) -> float:
days_since_halving = (now_utc - self.last_halving).days
position = (days_since_halving % self.cycle_length_days) / self.cycle_length_days
return position
def get_fibonacci_time(self, now_utc: datetime.datetime) -> Dict[str, Any]:
mins_passed = now_utc.hour * 60 + now_utc.minute
fib_seq = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
closest = min(fib_seq, key=lambda x: abs(x - mins_passed))
distance = abs(mins_passed - closest)
strength = 1.0 - min(distance / 30.0, 1.0)
return {'closest_fib_minute': closest, 'harmonic_strength': round(strength, 3)}
def get_moon_phase(self, now_utc: datetime.datetime) -> Dict[str, Any]:
now_ts = now_utc.timestamp()
if self._cache['moon']['val'] and (now_ts - self._cache['moon']['ts'] < self.cache_ttl_seconds):
return self._cache['moon']['val']
t = Time(now_utc)
with solar_system_ephemeris.set('builtin'):
moon = get_body('moon', t)
sun = get_body('sun', t)
elongation = sun.separation(moon)
phase_angle = np.arctan2(sun.distance * np.sin(elongation),
moon.distance - sun.distance * np.cos(elongation))
illumination = (1 + np.cos(phase_angle)) / 2.0
phase_name = "WAXING"
if illumination < 0.03: phase_name = "NEW_MOON"
elif illumination > 0.97: phase_name = "FULL_MOON"
elif illumination < 0.5: phase_name = "WAXING_CRESCENT" if moon.dec.deg > sun.dec.deg else "WANING_CRESCENT"
else: phase_name = "WAXING_GIBBOUS" if moon.dec.deg > sun.dec.deg else "WANING_GIBBOUS"
result = {'illumination': float(illumination), 'phase_name': phase_name}
self._cache['moon'] = {'val': result, 'ts': now_ts}
return result
def is_mercury_retrograde(self, now_utc: datetime.datetime) -> bool:
now_ts = now_utc.timestamp()
if self._cache['mercury']['val'] is not None and (now_ts - self._cache['mercury']['ts'] < self.cache_ttl_seconds):
return self._cache['mercury']['val']
t = Time(now_utc)
is_retro = False
try:
with solar_system_ephemeris.set('builtin'):
loc = EarthLocation.of_site('greenwich')
merc_now = get_body('mercury', t, loc)
merc_later = get_body('mercury', t + 1 * u.day, loc)
lon_now = merc_now.transform_to('geocentrictrueecliptic').lon.deg
lon_later = merc_later.transform_to('geocentrictrueecliptic').lon.deg
diff = (lon_later - lon_now) % 360
is_retro = diff > 180
except Exception as e:
logger.error(f"Astro calc error: {e}")
self._cache['mercury'] = {'val': is_retro, 'ts': now_ts}
return is_retro
def get_indicators(self, custom_now: Optional[datetime.datetime] = None) -> Dict[str, Any]:
"""Generate full suite of Esoteric Matrix factors."""
now_utc = custom_now if custom_now else datetime.datetime.now(datetime.timezone.utc)
pop_hour, liq_hour = self.get_weighted_times(now_utc)
moon_data = self.get_moon_phase(now_utc)
calendar = self.get_calendar_items(now_utc)
return {
'timestamp': now_utc.isoformat(),
'unix': int(now_utc.timestamp()),
'calendar': calendar,
'fibonacci_time': self.get_fibonacci_time(now_utc),
'regional_times': self.get_regional_times(now_utc),
'population_weighted_hour': pop_hour,
'liquidity_weighted_hour': liq_hour,
'liquidity_session': self.get_liquidity_session(now_utc),
'market_cycle_position': round(self.get_market_cycle_position(now_utc), 4),
'moon_illumination': moon_data['illumination'],
'moon_phase_name': moon_data['phase_name'],
'mercury_retrograde': int(self.is_mercury_retrograde(now_utc)),
}
class EsotericFactorsService:
"""
Continuous evaluation service for Esoteric Factors.
Dumps state deterministically to be consumed by the live trading orchestrator/Forewarning layers.
"""
def __init__(self, output_dir: str = "", poll_interval_s: float = 60.0):
# Default to same structure as external factors
if not output_dir:
self.output_dir = Path(__file__).parent / "eso_cache"
else:
self.output_dir = Path(output_dir)
self.output_dir.mkdir(parents=True, exist_ok=True)
self.poll_interval_s = poll_interval_s
self.engine = MarketIndicators()
self._latest_data = {}
self._running = False
self._task = None
self._lock = threading.Lock()
async def _update_loop(self):
logger.info(f"EsotericFactorsService starting. Polling every {self.poll_interval_s}s.")
while self._running:
try:
# 1. Compute Matrix
data = self.engine.get_indicators()
# 2. Store in memory
with self._lock:
self._latest_data = data
# 3. Dump purely to fast JSON
self._write_to_disk(data)
except Exception as e:
logger.error(f"Error in Esoteric update loop: {e}", exc_info=True)
await asyncio.sleep(self.poll_interval_s)
def _write_to_disk(self, data: dict):
# Fast write pattern via atomic tmp rename strategy
target_path = self.output_dir / "latest_esoteric_factors.json"
tmp_path = self.output_dir / "latest_esoteric_factors.tmp"
try:
with open(tmp_path, 'w') as f:
json.dump(data, f, indent=2)
tmp_path.replace(target_path)
except Exception as e:
logger.error(f"Failed to write Esoteric factors to disk: {e}")
def get_latest(self) -> dict:
"""Non-blocking sub-millisecond retrieval of the latest internal state."""
with self._lock:
return self._latest_data.copy()
def start(self):
"""Starts the background calculation loop (Threaded/Async wrapper)."""
if self._running: return
self._running = True
def run_async():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self._update_loop())
self._thread = threading.Thread(target=run_async, daemon=True)
self._thread.start()
def stop(self):
self._running = False
if hasattr(self, '_thread'):
self._thread.join(timeout=2.0)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
svc = EsotericFactorsService(poll_interval_s=5.0)
print("Starting Esoteric Factors Service test run for 15 seconds...")
svc.start()
for _ in range(3):
time.sleep(5)
latest = svc.get_latest()
print(f"Update: Moon Illumination={latest.get('moon_illumination'):.3f} | Liquid Session={latest.get('liquidity_session')} | PopHour={latest.get('population_weighted_hour')}")
svc.stop()
print("Stopped successfully.")

View File

@@ -1,612 +0,0 @@
#!/usr/bin/env python3
"""
EXTERNAL FACTORS MATRIX v5.0 - DOLPHIN Compatible with BACKFILL
================================================================
85 indicators with HISTORICAL query support where available.
BACKFILL CAPABILITY:
FULL HISTORY (51): CoinMetrics, FRED, DeFi Llama TVL/stables, F&G, Binance funding/OI
PARTIAL (12): Deribit DVOL, CoinGecko prices, DEX volume
CURRENT ONLY (22): Mempool, order books, spreads, dominance
Author: HJ / Claude | Version: 5.0.0
"""
import asyncio
import aiohttp
import numpy as np
from dataclasses import dataclass
from typing import Dict, List, Optional, Any, Tuple
from datetime import datetime, timezone
from collections import deque
from enum import Enum
import json
class Category(Enum):
DERIVATIVES = "derivatives"
ONCHAIN = "onchain"
DEFI = "defi"
MACRO = "macro"
SENTIMENT = "sentiment"
MICROSTRUCTURE = "microstructure"
class Stationarity(Enum):
STATIONARY = "stationary"
TREND_UP = "trend_up"
EPISODIC = "episodic"
class HistoricalSupport(Enum):
FULL = "full" # Any historical date
PARTIAL = "partial" # Limited history
CURRENT = "current" # Real-time only
@dataclass
class Indicator:
id: int
name: str
category: Category
source: str
url: str
parser: str
stationarity: Stationarity
historical: HistoricalSupport
hist_url: str = ""
hist_resolution: str = ""
description: str = ""
@dataclass
class Config:
timeout: int = 15
max_concurrent: int = 15
cache_ttl: int = 30
fred_api_key: str = ""
# fmt: off
INDICATORS: List[Indicator] = [
# DERIVATIVES - Binance (1-10) - Most have FULL history
Indicator(1, "funding_btc", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/fapi/v1/fundingRate?symbol=BTCUSDT&limit=1",
"parse_binance_funding", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://fapi.binance.com/fapi/v1/fundingRate?symbol=BTCUSDT&startTime={start_ms}&endTime={end_ms}&limit=1",
"8h", "BTC funding - FULL via startTime/endTime"),
Indicator(2, "funding_eth", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/fapi/v1/fundingRate?symbol=ETHUSDT&limit=1",
"parse_binance_funding", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://fapi.binance.com/fapi/v1/fundingRate?symbol=ETHUSDT&startTime={start_ms}&endTime={end_ms}&limit=1",
"8h", "ETH funding"),
Indicator(3, "oi_btc", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/fapi/v1/openInterest?symbol=BTCUSDT",
"parse_binance_oi", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://fapi.binance.com/futures/data/openInterestHist?symbol=BTCUSDT&period=1h&startTime={start_ms}&endTime={end_ms}&limit=1",
"1h", "BTC OI - FULL via openInterestHist"),
Indicator(4, "oi_eth", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/fapi/v1/openInterest?symbol=ETHUSDT",
"parse_binance_oi", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://fapi.binance.com/futures/data/openInterestHist?symbol=ETHUSDT&period=1h&startTime={start_ms}&endTime={end_ms}&limit=1",
"1h", "ETH OI"),
Indicator(5, "ls_btc", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol=BTCUSDT&period=1h&limit=1",
"parse_binance_ls", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol=BTCUSDT&period=1h&startTime={start_ms}&endTime={end_ms}&limit=1",
"1h", "L/S ratio - FULL"),
Indicator(6, "ls_eth", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol=ETHUSDT&period=1h&limit=1",
"parse_binance_ls", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol=ETHUSDT&period=1h&startTime={start_ms}&endTime={end_ms}&limit=1",
"1h", "ETH L/S"),
Indicator(7, "ls_top", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/futures/data/topLongShortAccountRatio?symbol=BTCUSDT&period=1h&limit=1",
"parse_binance_ls", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://fapi.binance.com/futures/data/topLongShortAccountRatio?symbol=BTCUSDT&period=1h&startTime={start_ms}&endTime={end_ms}&limit=1",
"1h", "Top trader L/S"),
Indicator(8, "taker", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/futures/data/takerlongshortRatio?symbol=BTCUSDT&period=1h&limit=1",
"parse_binance_taker", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://fapi.binance.com/futures/data/takerlongshortRatio?symbol=BTCUSDT&period=1h&startTime={start_ms}&endTime={end_ms}&limit=1",
"1h", "Taker ratio"),
Indicator(9, "basis", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/fapi/v1/premiumIndex?symbol=BTCUSDT",
"parse_binance_basis", Stationarity.STATIONARY, HistoricalSupport.CURRENT,
"", "", "Basis - CURRENT"),
Indicator(10, "liq_proxy", Category.DERIVATIVES, "binance",
"https://fapi.binance.com/fapi/v1/ticker/24hr?symbol=BTCUSDT",
"parse_liq_proxy", Stationarity.STATIONARY, HistoricalSupport.CURRENT,
"", "", "Liq proxy - CURRENT"),
# DERIVATIVES - Deribit (11-18)
Indicator(11, "dvol_btc", Category.DERIVATIVES, "deribit",
"https://www.deribit.com/api/v2/public/get_volatility_index_data?currency=BTC&resolution=3600&count=1",
"parse_deribit_dvol", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://www.deribit.com/api/v2/public/get_volatility_index_data?currency=BTC&resolution=3600&start_timestamp={start_ms}&end_timestamp={end_ms}",
"1h", "DVOL - FULL"),
Indicator(12, "dvol_eth", Category.DERIVATIVES, "deribit",
"https://www.deribit.com/api/v2/public/get_volatility_index_data?currency=ETH&resolution=3600&count=1",
"parse_deribit_dvol", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://www.deribit.com/api/v2/public/get_volatility_index_data?currency=ETH&resolution=3600&start_timestamp={start_ms}&end_timestamp={end_ms}",
"1h", "ETH DVOL"),
Indicator(13, "pcr_vol", Category.DERIVATIVES, "deribit",
"https://www.deribit.com/api/v2/public/get_book_summary_by_currency?currency=BTC&kind=option",
"parse_deribit_pcr", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "PCR - CURRENT"),
Indicator(14, "pcr_oi", Category.DERIVATIVES, "deribit",
"https://www.deribit.com/api/v2/public/get_book_summary_by_currency?currency=BTC&kind=option",
"parse_deribit_pcr_oi", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "PCR OI - CURRENT"),
Indicator(15, "pcr_eth", Category.DERIVATIVES, "deribit",
"https://www.deribit.com/api/v2/public/get_book_summary_by_currency?currency=ETH&kind=option",
"parse_deribit_pcr", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "ETH PCR - CURRENT"),
Indicator(16, "opt_oi", Category.DERIVATIVES, "deribit",
"https://www.deribit.com/api/v2/public/get_book_summary_by_currency?currency=BTC&kind=option",
"parse_deribit_oi", Stationarity.TREND_UP, HistoricalSupport.CURRENT, "", "", "Options OI - CURRENT"),
Indicator(17, "fund_dbt_btc", Category.DERIVATIVES, "deribit",
"https://www.deribit.com/api/v2/public/get_funding_rate_value?instrument_name=BTC-PERPETUAL",
"parse_deribit_fund", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://www.deribit.com/api/v2/public/get_funding_rate_history?instrument_name=BTC-PERPETUAL&start_timestamp={start_ms}&end_timestamp={end_ms}",
"8h", "Deribit fund - FULL"),
Indicator(18, "fund_dbt_eth", Category.DERIVATIVES, "deribit",
"https://www.deribit.com/api/v2/public/get_funding_rate_value?instrument_name=ETH-PERPETUAL",
"parse_deribit_fund", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://www.deribit.com/api/v2/public/get_funding_rate_history?instrument_name=ETH-PERPETUAL&start_timestamp={start_ms}&end_timestamp={end_ms}",
"8h", "Deribit ETH fund"),
# ONCHAIN - CoinMetrics (19-30) - ALL FULL HISTORY
Indicator(19, "rcap_btc", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=CapRealUSD&frequency=1d&page_size=1",
"parse_cm", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=CapRealUSD&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "Realized cap - FULL"),
Indicator(20, "mvrv", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=CapMrktCurUSD,CapRealUSD&frequency=1d&page_size=1",
"parse_cm_mvrv", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=CapMrktCurUSD,CapRealUSD&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "MVRV - FULL"),
Indicator(21, "nupl", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=CapMrktCurUSD,CapRealUSD&frequency=1d&page_size=1",
"parse_cm_nupl", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=CapMrktCurUSD,CapRealUSD&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "NUPL - FULL"),
Indicator(22, "addr_btc", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=AdrActCnt&frequency=1d&page_size=1",
"parse_cm", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=AdrActCnt&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "Active addr - FULL"),
Indicator(23, "addr_eth", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=eth&metrics=AdrActCnt&frequency=1d&page_size=1",
"parse_cm", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=eth&metrics=AdrActCnt&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "ETH addr - FULL"),
Indicator(24, "txcnt", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=TxCnt&frequency=1d&page_size=1",
"parse_cm", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=TxCnt&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "TX count - FULL"),
Indicator(25, "fees_btc", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=FeeTotUSD&frequency=1d&page_size=1",
"parse_cm", Stationarity.EPISODIC, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=FeeTotUSD&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "BTC fees - FULL"),
Indicator(26, "fees_eth", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=eth&metrics=FeeTotUSD&frequency=1d&page_size=1",
"parse_cm", Stationarity.EPISODIC, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=eth&metrics=FeeTotUSD&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "ETH fees - FULL"),
Indicator(27, "nvt", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=NVTAdj&frequency=1d&page_size=1",
"parse_cm", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=NVTAdj&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "NVT - FULL"),
Indicator(28, "velocity", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=VelCur1yr&frequency=1d&page_size=1",
"parse_cm", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=VelCur1yr&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "Velocity - FULL"),
Indicator(29, "sply_act", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=SplyAct1yr&frequency=1d&page_size=1",
"parse_cm", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=btc&metrics=SplyAct1yr&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "Active supply - FULL"),
Indicator(30, "rcap_eth", Category.ONCHAIN, "coinmetrics",
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=eth&metrics=CapRealUSD&frequency=1d&page_size=1",
"parse_cm", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets=eth&metrics=CapRealUSD&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
"1d", "ETH rcap - FULL"),
# ONCHAIN - Blockchain.info (31-37)
Indicator(31, "hashrate", Category.ONCHAIN, "blockchain",
"https://blockchain.info/q/hashrate", "parse_bc", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.blockchain.info/charts/hash-rate?timespan=1days&start={date}&format=json", "1d", "Hashrate - FULL"),
Indicator(32, "difficulty", Category.ONCHAIN, "blockchain",
"https://blockchain.info/q/getdifficulty", "parse_bc", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.blockchain.info/charts/difficulty?timespan=1days&start={date}&format=json", "1d", "Difficulty - FULL"),
Indicator(33, "blk_int", Category.ONCHAIN, "blockchain",
"https://blockchain.info/q/interval", "parse_bc_int", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "Block int - CURRENT"),
Indicator(34, "unconf", Category.ONCHAIN, "blockchain",
"https://blockchain.info/q/unconfirmedcount", "parse_bc", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "Unconf - CURRENT"),
Indicator(35, "tx_blk", Category.ONCHAIN, "blockchain",
"https://blockchain.info/q/nperblock", "parse_bc", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.blockchain.info/charts/n-transactions-per-block?timespan=1days&start={date}&format=json", "1d", "TX/blk - FULL"),
Indicator(36, "total_btc", Category.ONCHAIN, "blockchain",
"https://blockchain.info/q/totalbc", "parse_bc_btc", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.blockchain.info/charts/total-bitcoins?timespan=1days&start={date}&format=json", "1d", "Total BTC - FULL"),
Indicator(37, "mcap_bc", Category.ONCHAIN, "blockchain",
"https://blockchain.info/q/marketcap", "parse_bc", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.blockchain.info/charts/market-cap?timespan=1days&start={date}&format=json", "1d", "Mcap - FULL"),
# ONCHAIN - Mempool (38-42) - ALL CURRENT
Indicator(38, "mp_cnt", Category.ONCHAIN, "mempool", "https://mempool.space/api/mempool",
"parse_mp_cnt", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "Mempool - CURRENT"),
Indicator(39, "mp_mb", Category.ONCHAIN, "mempool", "https://mempool.space/api/mempool",
"parse_mp_mb", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "Mempool MB - CURRENT"),
Indicator(40, "fee_fast", Category.ONCHAIN, "mempool", "https://mempool.space/api/v1/fees/recommended",
"parse_fee_fast", Stationarity.EPISODIC, HistoricalSupport.CURRENT, "", "", "Fast fee - CURRENT"),
Indicator(41, "fee_med", Category.ONCHAIN, "mempool", "https://mempool.space/api/v1/fees/recommended",
"parse_fee_med", Stationarity.EPISODIC, HistoricalSupport.CURRENT, "", "", "Med fee - CURRENT"),
Indicator(42, "fee_slow", Category.ONCHAIN, "mempool", "https://mempool.space/api/v1/fees/recommended",
"parse_fee_slow", Stationarity.EPISODIC, HistoricalSupport.CURRENT, "", "", "Slow fee - CURRENT"),
# DEFI - DeFi Llama (43-51)
Indicator(43, "tvl", Category.DEFI, "defillama", "https://api.llama.fi/v2/historicalChainTvl",
"parse_dl_tvl", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.llama.fi/v2/historicalChainTvl", "1d", "TVL - FULL (filter client-side)"),
Indicator(44, "tvl_eth", Category.DEFI, "defillama", "https://api.llama.fi/v2/historicalChainTvl/Ethereum",
"parse_dl_tvl", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.llama.fi/v2/historicalChainTvl/Ethereum", "1d", "ETH TVL - FULL"),
Indicator(45, "stables", Category.DEFI, "defillama", "https://stablecoins.llama.fi/stablecoins?includePrices=false",
"parse_dl_stables", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://stablecoins.llama.fi/stablecoincharts/all?stablecoin=1", "1d", "Stables - FULL"),
Indicator(46, "usdt", Category.DEFI, "defillama", "https://stablecoins.llama.fi/stablecoin/tether",
"parse_dl_single", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://stablecoins.llama.fi/stablecoincharts/all?stablecoin=1", "1d", "USDT - FULL"),
Indicator(47, "usdc", Category.DEFI, "defillama", "https://stablecoins.llama.fi/stablecoin/usd-coin",
"parse_dl_single", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://stablecoins.llama.fi/stablecoincharts/all?stablecoin=2", "1d", "USDC - FULL"),
Indicator(48, "dex_vol", Category.DEFI, "defillama",
"https://api.llama.fi/overview/dexs?excludeTotalDataChart=true&excludeTotalDataChartBreakdown=true",
"parse_dl_dex", Stationarity.EPISODIC, HistoricalSupport.PARTIAL, "", "1d", "DEX vol - PARTIAL"),
Indicator(49, "bridge", Category.DEFI, "defillama", "https://bridges.llama.fi/bridges?includeChains=false",
"parse_dl_bridge", Stationarity.EPISODIC, HistoricalSupport.PARTIAL, "", "1d", "Bridge - PARTIAL"),
Indicator(50, "yields", Category.DEFI, "defillama", "https://yields.llama.fi/pools",
"parse_dl_yields", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "Yields - CURRENT"),
Indicator(51, "fees", Category.DEFI, "defillama", "https://api.llama.fi/overview/fees?excludeTotalDataChart=true",
"parse_dl_fees", Stationarity.EPISODIC, HistoricalSupport.PARTIAL, "", "1d", "Fees - PARTIAL"),
# MACRO - FRED (52-65) - ALL FULL HISTORY (decades)
Indicator(52, "dxy", Category.MACRO, "fred", "DTWEXBGS", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=DTWEXBGS&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "DXY - FULL"),
Indicator(53, "us10y", Category.MACRO, "fred", "DGS10", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=DGS10&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "10Y - FULL"),
Indicator(54, "us2y", Category.MACRO, "fred", "DGS2", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=DGS2&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "2Y - FULL"),
Indicator(55, "ycurve", Category.MACRO, "fred", "T10Y2Y", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=T10Y2Y&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "Yield curve - FULL"),
Indicator(56, "vix", Category.MACRO, "fred", "VIXCLS", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=VIXCLS&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "VIX - FULL"),
Indicator(57, "fedfunds", Category.MACRO, "fred", "DFF", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=DFF&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "Fed funds - FULL"),
Indicator(58, "m2", Category.MACRO, "fred", "WM2NS", "parse_fred", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=WM2NS&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1w", "M2 - FULL"),
Indicator(59, "cpi", Category.MACRO, "fred", "CPIAUCSL", "parse_fred", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=CPIAUCSL&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1m", "CPI - FULL"),
Indicator(60, "sp500", Category.MACRO, "fred", "SP500", "parse_fred", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=SP500&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "S&P - FULL"),
Indicator(61, "gold", Category.MACRO, "fred", "GOLDAMGBD228NLBM", "parse_fred", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=GOLDAMGBD228NLBM&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "Gold - FULL"),
Indicator(62, "hy_spread", Category.MACRO, "fred", "BAMLH0A0HYM2", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=BAMLH0A0HYM2&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "HY spread - FULL"),
Indicator(63, "be5y", Category.MACRO, "fred", "T5YIE", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=T5YIE&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1d", "Breakeven - FULL"),
Indicator(64, "nfci", Category.MACRO, "fred", "NFCI", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=NFCI&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1w", "NFCI - FULL"),
Indicator(65, "claims", Category.MACRO, "fred", "ICSA", "parse_fred", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.stlouisfed.org/fred/series/observations?series_id=ICSA&api_key={key}&file_type=json&observation_start={date}&observation_end={date}", "1w", "Claims - FULL"),
# SENTIMENT (66-72) - F&G has FULL history
Indicator(66, "fng", Category.SENTIMENT, "alternative", "https://api.alternative.me/fng/?limit=1",
"parse_fng", Stationarity.STATIONARY, HistoricalSupport.FULL,
"https://api.alternative.me/fng/?limit=1000&date_format=us", "1d", "F&G - FULL (returns history, filter)"),
Indicator(67, "fng_prev", Category.SENTIMENT, "alternative", "https://api.alternative.me/fng/?limit=2",
"parse_fng_prev", Stationarity.STATIONARY, HistoricalSupport.FULL, "", "1d", "Prev F&G"),
Indicator(68, "fng_week", Category.SENTIMENT, "alternative", "https://api.alternative.me/fng/?limit=7",
"parse_fng_week", Stationarity.STATIONARY, HistoricalSupport.FULL, "", "1d", "Week F&G"),
Indicator(69, "fng_vol", Category.SENTIMENT, "alternative", "https://api.alternative.me/fng/?limit=1",
"parse_fng", Stationarity.STATIONARY, HistoricalSupport.FULL, "", "1d", "Vol proxy"),
Indicator(70, "fng_mom", Category.SENTIMENT, "alternative", "https://api.alternative.me/fng/?limit=1",
"parse_fng", Stationarity.STATIONARY, HistoricalSupport.FULL, "", "1d", "Mom proxy"),
Indicator(71, "fng_soc", Category.SENTIMENT, "alternative", "https://api.alternative.me/fng/?limit=1",
"parse_fng", Stationarity.STATIONARY, HistoricalSupport.FULL, "", "1d", "Social proxy"),
Indicator(72, "fng_dom", Category.SENTIMENT, "alternative", "https://api.alternative.me/fng/?limit=1",
"parse_fng", Stationarity.STATIONARY, HistoricalSupport.FULL, "", "1d", "Dom proxy"),
# MICROSTRUCTURE (73-80) - Most CURRENT
Indicator(73, "imbal_btc", Category.MICROSTRUCTURE, "binance", "https://api.binance.com/api/v3/depth?symbol=BTCUSDT&limit=100",
"parse_imbal", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "Imbalance - CURRENT"),
Indicator(74, "imbal_eth", Category.MICROSTRUCTURE, "binance", "https://api.binance.com/api/v3/depth?symbol=ETHUSDT&limit=100",
"parse_imbal", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "ETH imbal - CURRENT"),
Indicator(75, "spread", Category.MICROSTRUCTURE, "binance", "https://api.binance.com/api/v3/ticker/bookTicker?symbol=BTCUSDT",
"parse_spread", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "Spread - CURRENT"),
Indicator(76, "chg24_btc", Category.MICROSTRUCTURE, "binance", "https://api.binance.com/api/v3/ticker/24hr?symbol=BTCUSDT",
"parse_chg", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "24h chg - CURRENT"),
Indicator(77, "chg24_eth", Category.MICROSTRUCTURE, "binance", "https://api.binance.com/api/v3/ticker/24hr?symbol=ETHUSDT",
"parse_chg", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "ETH 24h - CURRENT"),
Indicator(78, "vol24", Category.MICROSTRUCTURE, "binance", "https://api.binance.com/api/v3/ticker/24hr?symbol=BTCUSDT",
"parse_vol", Stationarity.EPISODIC, HistoricalSupport.FULL,
"https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&startTime={start_ms}&endTime={end_ms}&limit=1",
"1d", "Volume - FULL via klines"),
Indicator(79, "dispersion", Category.MICROSTRUCTURE, "binance", "https://api.binance.com/api/v3/ticker/24hr",
"parse_disp", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "Dispersion - CURRENT"),
Indicator(80, "correlation", Category.MICROSTRUCTURE, "binance", "https://api.binance.com/api/v3/ticker/24hr",
"parse_corr", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "Correlation - CURRENT"),
# MARKET - CoinGecko (81-85)
Indicator(81, "btc_price", Category.MACRO, "coingecko", "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd",
"parse_cg_btc", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.coingecko.com/api/v3/coins/bitcoin/history?date={date_dmy}", "1d", "BTC price - FULL"),
Indicator(82, "eth_price", Category.MACRO, "coingecko", "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd",
"parse_cg_eth", Stationarity.TREND_UP, HistoricalSupport.FULL,
"https://api.coingecko.com/api/v3/coins/ethereum/history?date={date_dmy}", "1d", "ETH price - FULL"),
Indicator(83, "mcap", Category.MACRO, "coingecko", "https://api.coingecko.com/api/v3/global",
"parse_cg_mcap", Stationarity.TREND_UP, HistoricalSupport.PARTIAL, "", "1d", "Mcap - PARTIAL"),
Indicator(84, "btc_dom", Category.MACRO, "coingecko", "https://api.coingecko.com/api/v3/global",
"parse_cg_dom_btc", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "BTC dom - CURRENT"),
Indicator(85, "eth_dom", Category.MACRO, "coingecko", "https://api.coingecko.com/api/v3/global",
"parse_cg_dom_eth", Stationarity.STATIONARY, HistoricalSupport.CURRENT, "", "", "ETH dom - CURRENT"),
]
# fmt: on
N_INDICATORS = len(INDICATORS)
class StationarityTransformer:
def __init__(self, lookback: int = 10):
self.history: Dict[int, deque] = {i: deque(maxlen=lookback+1) for i in range(1, N_INDICATORS+1)}
def transform(self, ind_id: int, raw: float) -> float:
ind = INDICATORS[ind_id - 1]
hist = self.history[ind_id]
hist.append(raw)
if ind.stationarity == Stationarity.STATIONARY: return raw
if ind.stationarity == Stationarity.TREND_UP:
return (raw - hist[-2]) / abs(hist[-2]) if len(hist) >= 2 and hist[-2] != 0 else 0.0
if ind.stationarity == Stationarity.EPISODIC:
if len(hist) < 3: return 0.0
m, s = np.mean(list(hist)), np.std(list(hist))
return (raw - m) / s if s > 0 else 0.0
return raw
def transform_matrix(self, raw: np.ndarray) -> np.ndarray:
return np.array([self.transform(i+1, raw[i]) for i in range(len(raw))])
class ExternalFactorsFetcher:
def __init__(self, config: Config = None):
self.config = config or Config()
self.cache: Dict[str, Tuple[float, Any]] = {}
import time as t; self._time = t
def _build_hist_url(self, ind: Indicator, dt: datetime) -> Optional[str]:
if ind.historical == HistoricalSupport.CURRENT or not ind.hist_url: return None
url = ind.hist_url
date_str = dt.strftime("%Y-%m-%d")
date_dmy = dt.strftime("%d-%m-%Y")
start_ms = int(dt.replace(hour=0, minute=0, second=0).timestamp() * 1000)
end_ms = int(dt.replace(hour=23, minute=59, second=59).timestamp() * 1000)
key = self.config.fred_api_key or "DEMO_KEY"
return url.replace("{date}", date_str).replace("{date_dmy}", date_dmy).replace("{start_ms}", str(start_ms)).replace("{end_ms}", str(end_ms)).replace("{key}", key)
async def _fetch(self, session, url: str) -> Optional[Any]:
if url in self.cache:
ct, cd = self.cache[url]
if self._time.time() - ct < self.config.cache_ttl: return cd
try:
async with session.get(url, timeout=aiohttp.ClientTimeout(total=self.config.timeout), headers={"User-Agent": "Mozilla/5.0"}) as r:
if r.status == 200:
d = await r.json() if 'json' in r.headers.get('Content-Type', '') else await r.text()
if isinstance(d, str):
try: d = json.loads(d)
except: pass
self.cache[url] = (self._time.time(), d)
return d
except: pass
return None
def _fred_url(self, series: str) -> str:
return f"https://api.stlouisfed.org/fred/series/observations?series_id={series}&api_key={self.config.fred_api_key or 'DEMO_KEY'}&file_type=json&sort_order=desc&limit=1"
# Parsers
def parse_binance_funding(self, d): return float(d[0]['fundingRate']) if isinstance(d, list) and d else 0.0
def parse_binance_oi(self, d):
if isinstance(d, list) and d: return float(d[-1].get('sumOpenInterest', 0))
return float(d.get('openInterest', 0)) if isinstance(d, dict) else 0.0
def parse_binance_ls(self, d): return float(d[-1]['longShortRatio']) if isinstance(d, list) and d else 1.0
def parse_binance_taker(self, d): return float(d[-1]['buySellRatio']) if isinstance(d, list) and d else 1.0
def parse_binance_basis(self, d): return float(d.get('lastFundingRate', 0)) * 365 * 3 if isinstance(d, dict) else 0.0
def parse_liq_proxy(self, d): return np.tanh(float(d.get('priceChangePercent', 0)) / 10) if isinstance(d, dict) else 0.0
def parse_deribit_dvol(self, d):
if isinstance(d, dict) and 'result' in d and isinstance(d['result'], dict) and 'data' in d['result'] and d['result']['data']:
return float(d['result']['data'][-1][4]) if len(d['result']['data'][-1]) > 4 else 0.0
return 0.0
def parse_deribit_pcr(self, d):
if isinstance(d, dict) and 'result' in d:
r = d['result']
p = sum(float(o.get('volume', 0)) for o in r if '-P' in o.get('instrument_name', ''))
c = sum(float(o.get('volume', 0)) for o in r if '-C' in o.get('instrument_name', ''))
return p / c if c > 0 else 1.0
return 1.0
def parse_deribit_pcr_oi(self, d):
if isinstance(d, dict) and 'result' in d:
r = d['result']
p = sum(float(o.get('open_interest', 0)) for o in r if '-P' in o.get('instrument_name', ''))
c = sum(float(o.get('open_interest', 0)) for o in r if '-C' in o.get('instrument_name', ''))
return p / c if c > 0 else 1.0
return 1.0
def parse_deribit_oi(self, d): return sum(float(o.get('open_interest', 0)) for o in d['result']) if isinstance(d, dict) and 'result' in d else 0.0
def parse_deribit_fund(self, d):
if isinstance(d, dict) and 'result' in d:
r = d['result']
return float(r[-1].get('interest_8h', 0)) if isinstance(r, list) and r else float(r)
return 0.0
def parse_cm(self, d):
if isinstance(d, dict) and 'data' in d and d['data']:
for k, v in d['data'][-1].items():
if k not in ['asset', 'time']:
try: return float(v)
except: pass
return 0.0
def parse_cm_mvrv(self, d):
if isinstance(d, dict) and 'data' in d and d['data']:
r = d['data'][-1]
m, rc = float(r.get('CapMrktCurUSD', 0)), float(r.get('CapRealUSD', 1))
return m / rc if rc > 0 else 0.0
return 0.0
def parse_cm_nupl(self, d):
if isinstance(d, dict) and 'data' in d and d['data']:
r = d['data'][-1]
m, rc = float(r.get('CapMrktCurUSD', 0)), float(r.get('CapRealUSD', 1))
return (m - rc) / m if m > 0 else 0.0
return 0.0
def parse_bc(self, d):
if isinstance(d, (int, float)): return float(d)
if isinstance(d, str):
try: return float(d)
except: pass
if isinstance(d, dict) and 'values' in d and d['values']: return float(d['values'][-1].get('y', 0))
return 0.0
def parse_bc_int(self, d): v = self.parse_bc(d); return abs(v - 600) / 600 if v > 0 else 0.0
def parse_bc_btc(self, d): v = self.parse_bc(d); return v / 1e8 if v > 0 else 0.0
def parse_mp_cnt(self, d): return float(d.get('count', 0)) if isinstance(d, dict) else 0.0
def parse_mp_mb(self, d): return float(d.get('vsize', 0)) / 1e6 if isinstance(d, dict) else 0.0
def parse_fee_fast(self, d): return float(d.get('fastestFee', 0)) if isinstance(d, dict) else 0.0
def parse_fee_med(self, d): return float(d.get('halfHourFee', 0)) if isinstance(d, dict) else 0.0
def parse_fee_slow(self, d): return float(d.get('economyFee', 0)) if isinstance(d, dict) else 0.0
def parse_dl_tvl(self, d, target_date: datetime = None):
if isinstance(d, list) and d:
if target_date:
ts = int(target_date.timestamp())
for e in reversed(d):
if e.get('date', 0) <= ts: return float(e.get('tvl', 0))
return float(d[-1].get('tvl', 0))
return 0.0
def parse_dl_stables(self, d):
if isinstance(d, dict) and 'peggedAssets' in d:
return sum(float(a.get('circulating', {}).get('peggedUSD', 0)) for a in d['peggedAssets'])
return 0.0
def parse_dl_single(self, d):
if isinstance(d, dict) and 'tokens' in d and d['tokens']:
return float(d['tokens'][-1].get('circulating', {}).get('peggedUSD', 0))
return 0.0
def parse_dl_dex(self, d): return float(d.get('total24h', 0)) if isinstance(d, dict) else 0.0
def parse_dl_bridge(self, d):
if isinstance(d, dict) and 'bridges' in d:
return sum(float(b.get('lastDayVolume', 0)) for b in d['bridges'])
return 0.0
def parse_dl_yields(self, d):
if isinstance(d, dict) and 'data' in d:
apys = [float(p.get('apy', 0)) for p in d['data'][:100] if p.get('apy')]
return np.mean(apys) if apys else 0.0
return 0.0
def parse_dl_fees(self, d): return float(d.get('total24h', 0)) if isinstance(d, dict) else 0.0
def parse_fred(self, d):
if isinstance(d, dict) and 'observations' in d and d['observations']:
v = d['observations'][-1].get('value', '.')
if v != '.':
try: return float(v)
except: pass
return 0.0
def parse_fng(self, d): return float(d['data'][0]['value']) if isinstance(d, dict) and 'data' in d and d['data'] else 50.0
def parse_fng_prev(self, d): return float(d['data'][1]['value']) if isinstance(d, dict) and 'data' in d and len(d['data']) > 1 else 50.0
def parse_fng_week(self, d): return np.mean([float(x['value']) for x in d['data'][:7]]) if isinstance(d, dict) and 'data' in d and len(d['data']) >= 7 else 50.0
def parse_imbal(self, d):
if isinstance(d, dict):
bv = sum(float(b[1]) for b in d.get('bids', [])[:50])
av = sum(float(a[1]) for a in d.get('asks', [])[:50])
t = bv + av
return (bv - av) / t if t > 0 else 0.0
return 0.0
def parse_spread(self, d):
if isinstance(d, dict):
b, a = float(d.get('bidPrice', 0)), float(d.get('askPrice', 0))
return (a - b) / b * 10000 if b > 0 else 0.0
return 0.0
def parse_chg(self, d): return float(d.get('priceChangePercent', 0)) if isinstance(d, dict) else 0.0
def parse_vol(self, d):
if isinstance(d, dict): return float(d.get('quoteVolume', 0))
if isinstance(d, list) and d and isinstance(d[0], list): return float(d[-1][7])
return 0.0
def parse_disp(self, d):
if isinstance(d, list) and len(d) > 10:
chg = [float(t['priceChangePercent']) for t in d if t.get('symbol', '').endswith('USDT') and 'priceChangePercent' in t]
return float(np.std(chg[:50])) if len(chg) > 5 else 0.0
return 0.0
def parse_corr(self, d): disp = self.parse_disp(d); return 1 / (1 + disp) if disp > 0 else 0.5
def parse_cg_btc(self, d):
if isinstance(d, dict) and 'bitcoin' in d: return float(d['bitcoin']['usd'])
if isinstance(d, dict) and 'market_data' in d: return float(d['market_data'].get('current_price', {}).get('usd', 0))
return 0.0
def parse_cg_eth(self, d):
if isinstance(d, dict) and 'ethereum' in d: return float(d['ethereum']['usd'])
if isinstance(d, dict) and 'market_data' in d: return float(d['market_data'].get('current_price', {}).get('usd', 0))
return 0.0
def parse_cg_mcap(self, d): return float(d['data']['total_market_cap']['usd']) if isinstance(d, dict) and 'data' in d else 0.0
def parse_cg_dom_btc(self, d): return float(d['data']['market_cap_percentage']['btc']) if isinstance(d, dict) and 'data' in d else 0.0
def parse_cg_dom_eth(self, d): return float(d['data']['market_cap_percentage']['eth']) if isinstance(d, dict) and 'data' in d else 0.0
async def fetch_indicator(self, session, ind: Indicator, target_date: datetime = None) -> Tuple[int, str, float, bool]:
if target_date and ind.historical != HistoricalSupport.CURRENT:
url = self._build_hist_url(ind, target_date)
else:
url = self._fred_url(ind.url) if ind.source == "fred" else ind.url
if url is None: return (ind.id, ind.name, 0.0, False)
data = await self._fetch(session, url)
if data is None: return (ind.id, ind.name, 0.0, False)
parser = getattr(self, ind.parser, None)
if parser is None: return (ind.id, ind.name, 0.0, False)
try:
value = parser(data)
return (ind.id, ind.name, value, value != 0.0 or 'imbal' in ind.name)
except: return (ind.id, ind.name, 0.0, False)
async def fetch_all(self, target_date: datetime = None) -> Dict[str, Any]:
connector = aiohttp.TCPConnector(limit=self.config.max_concurrent)
async with aiohttp.ClientSession(connector=connector) as session:
results = await asyncio.gather(*[self.fetch_indicator(session, ind, target_date) for ind in INDICATORS])
matrix = np.zeros(N_INDICATORS)
success = 0
details = {}
for idx, name, value, ok in results:
matrix[idx - 1] = value
if ok: success += 1
details[idx] = {'name': name, 'value': value, 'success': ok}
return {'matrix': matrix, 'timestamp': (target_date or datetime.now(timezone.utc)).isoformat(), 'success_count': success, 'total': N_INDICATORS, 'details': details}
def fetch_sync(self, target_date: datetime = None) -> Dict[str, Any]:
return asyncio.run(self.fetch_all(target_date))
class ExternalFactorsMatrix:
"""DOLPHIN interface with BACKFILL. Usage: efm.update() or efm.update(datetime(2024,6,15))"""
def __init__(self, config: Config = None):
self.config = config or Config()
self.fetcher = ExternalFactorsFetcher(self.config)
self.transformer = StationarityTransformer()
self.raw_matrix: Optional[np.ndarray] = None
self.stationary_matrix: Optional[np.ndarray] = None
self.last_result: Optional[Dict] = None
def update(self, target_date: datetime = None) -> np.ndarray:
self.last_result = self.fetcher.fetch_sync(target_date)
self.raw_matrix = self.last_result['matrix']
self.stationary_matrix = self.transformer.transform_matrix(self.raw_matrix)
return self.stationary_matrix
def update_raw(self, target_date: datetime = None) -> np.ndarray:
self.last_result = self.fetcher.fetch_sync(target_date)
self.raw_matrix = self.last_result['matrix']
return self.raw_matrix
def get_indicator_names(self) -> List[str]: return [i.name for i in INDICATORS]
def get_backfillable(self) -> List[Tuple[int, str, str]]:
return [(i.id, i.name, i.hist_resolution) for i in INDICATORS if i.historical in [HistoricalSupport.FULL, HistoricalSupport.PARTIAL]]
def get_current_only(self) -> List[Tuple[int, str]]:
return [(i.id, i.name) for i in INDICATORS if i.historical == HistoricalSupport.CURRENT]
def summary(self) -> str:
if not self.last_result: return "No data."
r = self.last_result
f = sum(1 for i in INDICATORS if i.historical == HistoricalSupport.FULL)
p = sum(1 for i in INDICATORS if i.historical == HistoricalSupport.PARTIAL)
c = sum(1 for i in INDICATORS if i.historical == HistoricalSupport.CURRENT)
return f"Success: {r['success_count']}/{r['total']} | Historical: FULL={f}, PARTIAL={p}, CURRENT={c}"
if __name__ == "__main__":
print(f"EXTERNAL FACTORS v5.0 - {N_INDICATORS} indicators with BACKFILL")
f = [i for i in INDICATORS if i.historical == HistoricalSupport.FULL]
p = [i for i in INDICATORS if i.historical == HistoricalSupport.PARTIAL]
c = [i for i in INDICATORS if i.historical == HistoricalSupport.CURRENT]
print(f"\nFULL: {len(f)} | PARTIAL: {len(p)} | CURRENT: {len(c)}")
print("\nFULL HISTORY indicators:")
for i in f: print(f" {i.id:2d}. {i.name:15s} [{i.hist_resolution:3s}] {i.source}")
print("\nCURRENT ONLY:")
for i in c: print(f" {i.id:2d}. {i.name:15s} - {i.description}")

View File

@@ -1,266 +0,0 @@
#!/usr/bin/env python3
"""
INDICATOR READER v1.0
=====================
Utility to read and analyze processed indicator .npz files.
Usage:
from indicator_reader import IndicatorReader
# Load single file
reader = IndicatorReader("scan_000027_193311__Indicators.npz")
print(reader.summary())
# Get DataFrames
scan_df = reader.scan_derived_df()
external_df = reader.external_df()
asset_df = reader.asset_df()
# Load directory
all_data = IndicatorReader.load_directory("./scans/")
"""
import numpy as np
from pathlib import Path
from typing import Dict, List, Optional, Any, Tuple
from datetime import datetime
class IndicatorReader:
"""Reader for processed indicator .npz files"""
def __init__(self, path: str):
self.path = Path(path)
self._data = dict(np.load(path, allow_pickle=True))
@property
def scan_number(self) -> int:
return int(self._data['scan_number'][0])
@property
def timestamp(self) -> str:
return str(self._data['timestamp'][0])
@property
def processing_time(self) -> float:
return float(self._data['processing_time'][0])
@property
def n_assets(self) -> int:
return len(self._data['asset_symbols'])
@property
def asset_symbols(self) -> List[str]:
return list(self._data['asset_symbols'])
# =========================================================================
# SCAN-DERIVED (eigenvalue indicators from tracking_data/regime_signals)
# =========================================================================
@property
def scan_derived(self) -> np.ndarray:
"""Get scan-derived indicator array"""
return self._data['scan_derived']
@property
def scan_derived_names(self) -> List[str]:
return list(self._data['scan_derived_names'])
def scan_derived_df(self):
"""Get scan-derived as pandas DataFrame"""
import pandas as pd
return pd.DataFrame({
'name': self.scan_derived_names,
'value': self.scan_derived
})
def get_scan_indicator(self, name: str) -> float:
"""Get specific scan-derived indicator by name"""
names = self.scan_derived_names
if name in names:
return float(self.scan_derived[names.index(name)])
raise KeyError(f"Unknown scan indicator: {name}")
# =========================================================================
# EXTERNAL (API-fetched indicators)
# =========================================================================
@property
def external(self) -> np.ndarray:
"""Get external indicator array (85 values, NaN for skipped)"""
return self._data['external']
@property
def external_success(self) -> np.ndarray:
"""Get success flags for external indicators"""
return self._data['external_success']
def external_df(self):
"""Get external indicators as pandas DataFrame"""
import pandas as pd
# Indicator names (would need to import from external_factors_matrix)
names = [f"ext_{i+1}" for i in range(85)]
return pd.DataFrame({
'id': range(1, 86),
'value': self.external,
'success': self.external_success
})
@property
def external_success_rate(self) -> float:
"""Percentage of external indicators successfully fetched"""
valid = ~np.isnan(self.external)
if valid.sum() == 0:
return 0.0
return float(self.external_success[valid].mean())
# =========================================================================
# PER-ASSET
# =========================================================================
@property
def asset_matrix(self) -> np.ndarray:
"""Get per-asset indicator matrix (n_assets x n_indicators)"""
return self._data['asset_matrix']
@property
def asset_indicator_names(self) -> List[str]:
return list(self._data['asset_indicator_names'])
def asset_df(self):
"""Get per-asset indicators as pandas DataFrame"""
import pandas as pd
return pd.DataFrame(
self.asset_matrix,
index=self.asset_symbols,
columns=self.asset_indicator_names
)
def get_asset(self, symbol: str) -> Dict[str, float]:
"""Get all indicators for a specific asset"""
symbols = self.asset_symbols
if symbol not in symbols:
raise KeyError(f"Unknown symbol: {symbol}")
idx = symbols.index(symbol)
return dict(zip(self.asset_indicator_names, self.asset_matrix[idx]))
def get_asset_indicator(self, symbol: str, indicator: str) -> float:
"""Get specific indicator for specific asset"""
asset = self.get_asset(symbol)
if indicator not in asset:
raise KeyError(f"Unknown indicator: {indicator}")
return asset[indicator]
# =========================================================================
# UTILITIES
# =========================================================================
def summary(self) -> str:
"""Get summary string"""
ext_valid = (~np.isnan(self.external)).sum()
ext_success = self.external_success.sum()
return f"""Indicator File: {self.path.name}
Scan: #{self.scan_number} @ {self.timestamp}
Processing: {self.processing_time:.2f}s
Scan-derived: {len(self.scan_derived)} indicators
lambda_max: {self.get_scan_indicator('lambda_max'):.4f}
coherence: {self.get_scan_indicator('market_coherence'):.4f}
instability: {self.get_scan_indicator('instability_score'):.4f}
External: {ext_success}/{ext_valid} successful ({self.external_success_rate*100:.1f}%)
Per-asset: {self.n_assets} assets × {len(self.asset_indicator_names)} indicators
"""
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary"""
return {
'scan_number': self.scan_number,
'timestamp': self.timestamp,
'processing_time': self.processing_time,
'scan_derived': dict(zip(self.scan_derived_names, self.scan_derived.tolist())),
'external': self.external.tolist(),
'external_success': self.external_success.tolist(),
'asset_symbols': self.asset_symbols,
'asset_matrix': self.asset_matrix.tolist(),
}
# =========================================================================
# CLASS METHODS
# =========================================================================
@classmethod
def load_directory(cls, directory: str, pattern: str = "*__Indicators.npz") -> List['IndicatorReader']:
"""Load all indicator files from directory"""
root = Path(directory)
files = sorted(root.rglob(pattern))
return [cls(str(f)) for f in files]
@classmethod
def to_timeseries(cls, readers: List['IndicatorReader']) -> Dict[str, np.ndarray]:
"""Convert list of readers to time series arrays"""
n = len(readers)
if n == 0:
return {}
# Get dimensions from first file
n_scan = len(readers[0].scan_derived)
n_ext = 85
n_assets = readers[0].n_assets
n_asset_ind = len(readers[0].asset_indicator_names)
# Allocate arrays
timestamps = []
scan_series = np.zeros((n, n_scan))
ext_series = np.zeros((n, n_ext))
for i, r in enumerate(readers):
timestamps.append(r.timestamp)
scan_series[i] = r.scan_derived
ext_series[i] = r.external
return {
'timestamps': np.array(timestamps, dtype='U32'),
'scan_derived': scan_series,
'external': ext_series,
'scan_names': readers[0].scan_derived_names,
}
# =============================================================================
# CLI
# =============================================================================
def main():
import argparse
parser = argparse.ArgumentParser(description="Indicator Reader")
parser.add_argument("path", help="Path to .npz file or directory")
parser.add_argument("-a", "--asset", help="Show specific asset")
parser.add_argument("-j", "--json", action="store_true", help="Output as JSON")
args = parser.parse_args()
path = Path(args.path)
if path.is_file():
reader = IndicatorReader(str(path))
if args.json:
import json
print(json.dumps(reader.to_dict(), indent=2))
elif args.asset:
asset = reader.get_asset(args.asset)
for k, v in asset.items():
print(f" {k}: {v:.6f}")
else:
print(reader.summary())
elif path.is_dir():
readers = IndicatorReader.load_directory(str(path))
print(f"Found {len(readers)} indicator files")
if readers:
ts = IndicatorReader.to_timeseries(readers)
print(f"Time range: {ts['timestamps'][0]} to {ts['timestamps'][-1]}")
print(f"Scan-derived shape: {ts['scan_derived'].shape}")
print(f"External shape: {ts['external'].shape}")
if __name__ == "__main__":
main()

View File

@@ -1,204 +0,0 @@
#!/usr/bin/env python3
"""
INDICATOR SOURCES v5.0 - API Reference with Historical Support
===============================================================
Documents all 85 indicators with their backfill capability.
"""
SOURCES = {
"binance": {"url": "fapi.binance.com / api.binance.com", "auth": "None", "limit": "1200/min", "history": "FULL (startTime/endTime)"},
"deribit": {"url": "deribit.com/api/v2/public", "auth": "None", "limit": "20/sec", "history": "FULL for DVOL/funding"},
"coinmetrics": {"url": "community-api.coinmetrics.io/v4", "auth": "None", "limit": "10/6sec", "history": "FULL (start_time/end_time)"},
"fred": {"url": "api.stlouisfed.org/fred", "auth": "Free key", "limit": "120/min", "history": "FULL (decades)"},
"defillama": {"url": "api.llama.fi", "auth": "None", "limit": "Generous", "history": "FULL for TVL/stables"},
"alternative": {"url": "api.alternative.me", "auth": "None", "limit": "Unlimited", "history": "FULL (limit=N param)"},
"blockchain": {"url": "blockchain.info", "auth": "None", "limit": "Generous", "history": "FULL via charts API"},
"mempool": {"url": "mempool.space/api", "auth": "None", "limit": "Generous", "history": "NONE (real-time only)"},
"coingecko": {"url": "api.coingecko.com/api/v3", "auth": "None (demo)", "limit": "30/min", "history": "FULL for prices"},
}
# Historical URL templates for backfill
HISTORICAL_ENDPOINTS = {
# BINANCE - All support startTime/endTime in milliseconds
"binance_funding": "https://fapi.binance.com/fapi/v1/fundingRate?symbol={SYMBOL}&startTime={start_ms}&endTime={end_ms}&limit=1000",
"binance_oi_hist": "https://fapi.binance.com/futures/data/openInterestHist?symbol={SYMBOL}&period=1h&startTime={start_ms}&endTime={end_ms}&limit=500",
"binance_ls_hist": "https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol={SYMBOL}&period=1h&startTime={start_ms}&endTime={end_ms}&limit=500",
"binance_taker_hist": "https://fapi.binance.com/futures/data/takerlongshortRatio?symbol={SYMBOL}&period=1h&startTime={start_ms}&endTime={end_ms}&limit=500",
"binance_klines": "https://api.binance.com/api/v3/klines?symbol={SYMBOL}&interval=1d&startTime={start_ms}&endTime={end_ms}&limit=1",
# DERIBIT - Uses start_timestamp/end_timestamp in milliseconds
"deribit_dvol": "https://www.deribit.com/api/v2/public/get_volatility_index_data?currency={CURRENCY}&resolution=3600&start_timestamp={start_ms}&end_timestamp={end_ms}",
"deribit_funding_hist": "https://www.deribit.com/api/v2/public/get_funding_rate_history?instrument_name={INSTRUMENT}&start_timestamp={start_ms}&end_timestamp={end_ms}",
# COINMETRICS - Uses ISO date format
"coinmetrics": "https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?assets={asset}&metrics={metric}&frequency=1d&start_time={date}T00:00:00Z&end_time={date}T23:59:59Z",
# FRED - Uses observation_start/observation_end in YYYY-MM-DD
"fred": "https://api.stlouisfed.org/fred/series/observations?series_id={series}&api_key={key}&file_type=json&observation_start={date}&observation_end={date}",
# DEFILLAMA - Returns full history, filter client-side
"defillama_tvl": "https://api.llama.fi/v2/historicalChainTvl", # Filter by date client-side
"defillama_tvl_chain": "https://api.llama.fi/v2/historicalChainTvl/{chain}",
"defillama_stables": "https://stablecoins.llama.fi/stablecoincharts/all?stablecoin={id}", # 1=USDT, 2=USDC
# BLOCKCHAIN.INFO - Uses start param in YYYY-MM-DD
"blockchain_charts": "https://api.blockchain.info/charts/{chart}?timespan=1days&start={date}&format=json",
# COINGECKO - Uses DD-MM-YYYY format
"coingecko_history": "https://api.coingecko.com/api/v3/coins/{id}/history?date={date_dmy}",
# ALTERNATIVE.ME - Returns N days of history
"fng_history": "https://api.alternative.me/fng/?limit=1000&date_format=us", # Filter client-side
}
HISTORICAL_SUPPORT = {
# FULL HISTORY (51 indicators)
"full": [
# Binance derivatives
(1, "funding_btc", "8h", "Funding rate history via startTime/endTime"),
(2, "funding_eth", "8h", "ETH funding"),
(3, "oi_btc", "1h", "Open interest history via openInterestHist endpoint"),
(4, "oi_eth", "1h", "ETH OI"),
(5, "ls_btc", "1h", "Long/short ratio history"),
(6, "ls_eth", "1h", "ETH L/S"),
(7, "ls_top", "1h", "Top trader L/S"),
(8, "taker", "1h", "Taker ratio history"),
# Deribit
(11, "dvol_btc", "1h", "DVOL via get_volatility_index_data"),
(12, "dvol_eth", "1h", "ETH DVOL"),
(17, "fund_dbt_btc", "8h", "Deribit funding via get_funding_rate_history"),
(18, "fund_dbt_eth", "8h", "ETH Deribit funding"),
# CoinMetrics (ALL have full history)
(19, "rcap_btc", "1d", "CoinMetrics: CapRealUSD"),
(20, "mvrv", "1d", "CoinMetrics: derived from CapMrktCurUSD/CapRealUSD"),
(21, "nupl", "1d", "CoinMetrics: derived"),
(22, "addr_btc", "1d", "CoinMetrics: AdrActCnt"),
(23, "addr_eth", "1d", "CoinMetrics: ETH AdrActCnt"),
(24, "txcnt", "1d", "CoinMetrics: TxCnt"),
(25, "fees_btc", "1d", "CoinMetrics: FeeTotUSD"),
(26, "fees_eth", "1d", "CoinMetrics: ETH FeeTotUSD"),
(27, "nvt", "1d", "CoinMetrics: NVTAdj"),
(28, "velocity", "1d", "CoinMetrics: VelCur1yr"),
(29, "sply_act", "1d", "CoinMetrics: SplyAct1yr"),
(30, "rcap_eth", "1d", "CoinMetrics: ETH CapRealUSD"),
# Blockchain.info charts
(31, "hashrate", "1d", "Blockchain.info: hash-rate chart"),
(32, "difficulty", "1d", "Blockchain.info: difficulty chart"),
(35, "tx_blk", "1d", "Blockchain.info: n-transactions-per-block chart"),
(36, "total_btc", "1d", "Blockchain.info: total-bitcoins chart"),
(37, "mcap_bc", "1d", "Blockchain.info: market-cap chart"),
# DeFi Llama
(43, "tvl", "1d", "DeFi Llama: historicalChainTvl (returns all, filter client-side)"),
(44, "tvl_eth", "1d", "DeFi Llama: ETH TVL"),
(45, "stables", "1d", "DeFi Llama: stablecoincharts"),
(46, "usdt", "1d", "DeFi Llama: stablecoin ID=1"),
(47, "usdc", "1d", "DeFi Llama: stablecoin ID=2"),
# FRED (ALL have decades of history)
(52, "dxy", "1d", "FRED: DTWEXBGS"),
(53, "us10y", "1d", "FRED: DGS10"),
(54, "us2y", "1d", "FRED: DGS2"),
(55, "ycurve", "1d", "FRED: T10Y2Y"),
(56, "vix", "1d", "FRED: VIXCLS"),
(57, "fedfunds", "1d", "FRED: DFF"),
(58, "m2", "1w", "FRED: WM2NS (weekly)"),
(59, "cpi", "1m", "FRED: CPIAUCSL (monthly)"),
(60, "sp500", "1d", "FRED: SP500"),
(61, "gold", "1d", "FRED: GOLDAMGBD228NLBM"),
(62, "hy_spread", "1d", "FRED: BAMLH0A0HYM2"),
(63, "be5y", "1d", "FRED: T5YIE"),
(64, "nfci", "1w", "FRED: NFCI (weekly)"),
(65, "claims", "1w", "FRED: ICSA (weekly)"),
# Alternative.me
(66, "fng", "1d", "Alternative.me: limit param returns history"),
(67, "fng_prev", "1d", ""),
(68, "fng_week", "1d", ""),
(69, "fng_vol", "1d", ""),
(70, "fng_mom", "1d", ""),
(71, "fng_soc", "1d", ""),
(72, "fng_dom", "1d", ""),
# CoinGecko
(81, "btc_price", "1d", "CoinGecko: /coins/{id}/history"),
(82, "eth_price", "1d", "CoinGecko: /coins/{id}/history"),
# Binance klines
(78, "vol24", "1d", "Binance: klines endpoint"),
],
# PARTIAL HISTORY (12 indicators)
"partial": [
(48, "dex_vol", "1d", "DeFi Llama: recent history in response"),
(49, "bridge", "1d", "DeFi Llama: bridgevolume endpoint"),
(51, "fees", "1d", "DeFi Llama: fees overview"),
(83, "mcap", "1d", "CoinGecko: market_cap_chart (limited)"),
],
# CURRENT ONLY (22 indicators)
"current": [
(9, "basis", "Binance premium index - real-time only"),
(10, "liq_proxy", "Derived from 24hr ticker - real-time"),
(13, "pcr_vol", "Deribit options summary - real-time"),
(14, "pcr_oi", "Deribit options OI - real-time"),
(15, "pcr_eth", "Deribit ETH options - real-time"),
(16, "opt_oi", "Deribit total options OI - real-time"),
(33, "blk_int", "Blockchain.info simple query - real-time"),
(34, "unconf", "Blockchain.info unconfirmed - real-time"),
(38, "mp_cnt", "Mempool.space - NO historical API"),
(39, "mp_mb", "Mempool.space - NO historical API"),
(40, "fee_fast", "Mempool.space - NO historical API"),
(41, "fee_med", "Mempool.space - NO historical API"),
(42, "fee_slow", "Mempool.space - NO historical API"),
(50, "yields", "DeFi Llama yields - real-time"),
(73, "imbal_btc", "Order book depth - real-time"),
(74, "imbal_eth", "Order book depth - real-time"),
(75, "spread", "Book ticker - real-time"),
(76, "chg24_btc", "24hr ticker - real-time"),
(77, "chg24_eth", "24hr ticker - real-time"),
(79, "dispersion", "Calculated from 24hr - real-time"),
(80, "correlation", "Calculated from 24hr - real-time"),
(84, "btc_dom", "CoinGecko global - real-time"),
(85, "eth_dom", "CoinGecko global - real-time"),
],
}
BACKFILL_NOTES = """
BACKFILL STRATEGY
=================
1. DAILY BACKFILL (Most indicators):
- CoinMetrics, FRED, DeFi Llama TVL, Blockchain.info charts
- Use: efm.update(datetime(2024, 6, 15))
2. HOURLY BACKFILL (Binance derivatives):
- OI, L/S ratio, taker ratio have 1h resolution
- Funding rate has 8h resolution
3. APIS RETURNING FULL HISTORY:
- DeFi Llama TVL: Returns ALL history, filter client-side by timestamp
- Alternative.me F&G: Use limit=1000 to get ~3 years of history
- Blockchain.info charts: Use start= param with date
4. MISSING HISTORICAL DATA:
- Mempool fees: Build your own collector
- Order book imbalance: Build your own collector
- Spreads: Build your own collector
5. RECOMMENDED APPROACH FOR TRAINING:
a) Backfill what's available (51 indicators with FULL history)
b) For CURRENT-only indicators, either:
- Accept NaN/0 for historical periods
- Build collectors to capture going forward
- Use proxy indicators (e.g., volatility proxy for mempool fees)
"""
if __name__ == "__main__":
print("INDICATOR SOURCES v5.0")
print("=" * 60)
print("\nData Sources:")
for src, info in SOURCES.items():
print(f" {src:12s}: {info['auth']:10s} | {info['limit']:12s} | {info['history']}")
print(f"\nHistorical Support:")
print(f" FULL: {len(HISTORICAL_SUPPORT['full'])} indicators")
print(f" PARTIAL: {len(HISTORICAL_SUPPORT['partial'])} indicators")
print(f" CURRENT: {len(HISTORICAL_SUPPORT['current'])} indicators")
print(BACKFILL_NOTES)

View File

@@ -1,207 +0,0 @@
"""
Meta-Adaptive ExF Optimizer
===========================
Runs nightly (or on-demand) to calculate dynamic lag configurations and
active indicator thresholds for the Adaptive Circuit Breaker (ACB).
Implementation of the "Meta-Adaptive" Blueprint:
1. Pulls up to the last 90 days of market returns and indicator values.
2. Runs lag hypothesis testing (0-7 days) on all tracked ExF indicators.
3. Uses strict Point-Biserial correlation (p < 0.05) against market stress (< -1% daily drop).
4. Persists the active, statistically verified JSON configuration for realtime_exf_service.py.
"""
import sys
import json
import time
import logging
import numpy as np
import pandas as pd
from pathlib import Path
from collections import defaultdict
import threading
from scipy import stats
from datetime import datetime, timezone
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))
sys.path.insert(0, str(PROJECT_ROOT / 'nautilus_dolphin'))
try:
from realtime_exf_service import INDICATORS, OPTIMAL_LAGS
from dolphin_paper_trade_adaptive_cb_v2 import EIGENVALUES_BASE_PATH
from dolphin_vbt_real import load_all_data, run_full_backtest, STRATEGIES, INIT_CAPITAL
except ImportError:
pass
logger = logging.getLogger(__name__)
CONFIG_PATH = Path(__file__).parent / "meta_adaptive_config.json"
class MetaAdaptiveOptimizer:
def __init__(self, days_lookback=90, max_lags=6, p_value_gate=0.05):
self.days_lookback = days_lookback
self.max_lags = max_lags
self.p_value_gate = p_value_gate
self.indicators = list(INDICATORS.keys()) if 'INDICATORS' in globals() else []
self._lock = threading.Lock()
def _build_history_cache(self, dates, limit_days):
"""Build daily feature cache from NPZ files."""
logger.info(f"Building cache for last {limit_days} days...")
cache = {}
target_dates = dates[-limit_days:] if len(dates) > limit_days else dates
for date_str in target_dates:
date_path = EIGENVALUES_BASE_PATH / date_str
if not date_path.exists(): continue
npz_files = list(date_path.glob('scan_*__Indicators.npz'))
if not npz_files: continue
accum = defaultdict(list)
for f in npz_files:
try:
data = dict(np.load(f, allow_pickle=True))
names = [str(n) for n in data.get('api_names', [])]
vals = data.get('api_indicators', [])
succ = data.get('api_success', [])
for n, v, s in zip(names, vals, succ):
if s and not np.isnan(v):
accum[n].append(float(v))
except Exception:
pass
if accum:
cache[date_str] = {k: np.mean(v) for k, v in accum.items()}
return cache, target_dates
def _get_daily_returns(self, df, target_dates):
"""Derive daily returns proxy from the champion strategy logic."""
logger.info("Computing proxy returns for the time window...")
champion = STRATEGIES['champion_5x_f20']
returns = []
cap = INIT_CAPITAL
valid_dates = []
for d in target_dates:
day_df = df[df['date_str'] == d]
if len(day_df) < 200:
returns.append(np.nan)
valid_dates.append(d)
continue
res = run_full_backtest(day_df, champion, init_cash=cap, seed=42, verbose=False)
ret = (res['capital'] - cap) / cap
returns.append(ret)
cap = res['capital']
valid_dates.append(d)
return np.array(returns), valid_dates
def run_optimization(self) -> dict:
"""Run the full meta-adaptive optimization routine and return new config."""
with self._lock:
logger.info("Starting META-ADAPTIVE optimization loop.")
t0 = time.time()
df = load_all_data()
if 'date_str' not in df.columns:
df['date_str'] = df['timestamp'].dt.date.astype(str)
all_dates = sorted(df['date_str'].unique())
cache, target_dates = self._build_history_cache(all_dates, self.days_lookback + self.max_lags)
daily_returns, target_dates = self._get_daily_returns(df, target_dates)
# Predict market stress dropping by more than 1%
stress_arr = (daily_returns < -0.01).astype(float)
candidate_lags = {}
active_thresholds = {}
candidate_count = 0
for key in self.indicators:
ind_arr = np.array([cache.get(d, {}).get(key, np.nan) for d in target_dates])
corrs = []; pvals = []; sc_corrs = []
for lag in range(self.max_lags + 1):
if lag == 0: x, y, y_stress = ind_arr, daily_returns, stress_arr
else: x, y, y_stress = ind_arr[:-lag], daily_returns[lag:], stress_arr[lag:]
mask = ~np.isnan(x) & ~np.isnan(y)
if mask.sum() < 20: # Need at least 20 viable days
corrs.append(0); pvals.append(1); sc_corrs.append(0)
continue
# Pearson to price returns
r, p = stats.pearsonr(x[mask], y[mask])
corrs.append(r); pvals.append(p)
# Point-Biserial to stress events
# We capture the relation to binary stress to figure out threshold direction
if y_stress[mask].sum() > 2: # At least a few stress days required
sc = stats.pointbiserialr(y_stress[mask], x[mask])[0]
else:
sc = 0
sc_corrs.append(sc)
if not corrs: continue
# Find lag with highest correlation strength
best_lag = int(np.argmax(np.abs(corrs)))
best_p = pvals[best_lag]
# Check gate
if best_p <= self.p_value_gate:
direction = ">" if sc_corrs[best_lag] > 0 else "<"
# Compute a stress threshold logic (e.g. 15th / 85th percentile of historical)
valid_vals = ind_arr[~np.isnan(ind_arr)]
thresh = np.percentile(valid_vals, 85 if direction == '>' else 15)
candidate_lags[key] = best_lag
active_thresholds[key] = {
'threshold': float(thresh),
'direction': direction,
'p_value': float(best_p),
'r_value': float(corrs[best_lag])
}
candidate_count += 1
# Fallback checks mapping to V4 baseline if things drift too far
logger.info(f"Optimization complete ({time.time() - t0:.1f}s). {candidate_count} indicators passed P < {self.p_value_gate}.")
output_config = {
'timestamp': datetime.now(timezone.utc).isoformat(),
'days_lookback': self.days_lookback,
'lags': candidate_lags,
'thresholds': active_thresholds
}
# Atomic save
temp_path = CONFIG_PATH.with_suffix('.tmp')
with open(temp_path, 'w', encoding='utf-8') as f:
json.dump(output_config, f, indent=2)
temp_path.replace(CONFIG_PATH)
return output_config
def get_current_meta_config() -> dict:
"""Read the latest meta-adaptive config, or return empty/default dict."""
if not CONFIG_PATH.exists():
return {}
try:
with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
logger.error(f"Failed to read meta-adaptive config: {e}")
return {}
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
optimizer = MetaAdaptiveOptimizer(days_lookback=90)
config = optimizer.run_optimization()
print(f"\nSaved config to: {CONFIG_PATH}")
for k, v in config['lags'].items():
print(f" {k}: lag={v} days, dir={config['thresholds'][k]['direction']} thresh={config['thresholds'][k]['threshold']:.4g}")

View File

@@ -1,228 +0,0 @@
import asyncio
import aiohttp
import json
import time
import logging
import numpy as np
from typing import Dict, List, Optional
from collections import defaultdict
# Setup basic logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(name)s: %(message)s')
logger = logging.getLogger("OBStreamService")
try:
import websockets
except ImportError:
logger.warning("websockets package not found. Run pip install websockets aiohttp")
class OBStreamService:
"""
Real-Time Order Book Streamer for Binance Futures.
Connects via WebSockets to maintain a perfectly synchronized local L2 Book,
and slices the book into 5% notional depth buckets dynamically for the
SmartPlacer and OBFeatureEngine layers.
"""
def __init__(self, assets: List[str], max_depth_pct: int = 5):
self.assets = [a.upper() for a in assets]
self.streams = [f"{a.lower()}@depth@100ms" for a in self.assets]
self.max_depth_pct = max_depth_pct
# In-memory Order Book caches (Price -> Quantity)
self.bids: Dict[str, Dict[float, float]] = {a: {} for a in self.assets}
self.asks: Dict[str, Dict[float, float]] = {a: {} for a in self.assets}
# Synchronization mechanisms
self.last_update_id: Dict[str, int] = {a: 0 for a in self.assets}
self.buffer: Dict[str, List[dict]] = {a: [] for a in self.assets}
self.initialized: Dict[str, bool] = {a: False for a in self.assets}
# Optional: Lock for thread-safe reads if requested asynchronously
self.locks: Dict[str, asyncio.Lock] = {a: asyncio.Lock() for a in self.assets}
async def fetch_snapshot(self, asset: str):
"""Fetch REST snapshot of the Order Book to initialize local state."""
url = f"https://fapi.binance.com/fapi/v1/depth?symbol={asset}&limit=1000"
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
data = await resp.json()
if 'lastUpdateId' not in data:
logger.error(f"Failed to fetch snapshot for {asset}: {data}")
return
last_id = data['lastUpdateId']
async with self.locks[asset]:
self.bids[asset] = {float(p): float(q) for p, q in data['bids']}
self.asks[asset] = {float(p): float(q) for p, q in data['asks']}
self.last_update_id[asset] = last_id
# Apply any buffered updates
buffered = self.buffer[asset]
for event in buffered:
if event['u'] <= last_id:
continue # Ignore old events
self._apply_event(asset, event)
self.buffer[asset].clear()
self.initialized[asset] = True
logger.info(f"Synchronized L2 book for {asset} (UpdateId: {last_id})")
except Exception as e:
logger.error(f"Error initializing snapshot for {asset}: {e}")
def _apply_event(self, asset: str, event: dict):
"""Apply a streaming diff event to the local book."""
bids = self.bids[asset]
asks = self.asks[asset]
# Process Bids
for p_str, q_str in event['b']:
p, q = float(p_str), float(q_str)
if q == 0.0:
bids.pop(p, None)
else:
bids[p] = q
# Process Asks
for p_str, q_str in event['a']:
p, q = float(p_str), float(q_str)
if q == 0.0:
asks.pop(p, None)
else:
asks[p] = q
self.last_update_id[asset] = event['u']
async def stream(self):
"""Main loop: connect to WebSocket streams and maintain books."""
import websockets
# 1. Fire off REST snapshot initialization concurrently
for a in self.assets:
asyncio.create_task(self.fetch_snapshot(a))
# 2. Start WebSocket listening instantly to buffer diffs
stream_url = "wss://fstream.binance.com/stream?streams=" + "/".join(self.streams)
logger.info(f"Connecting to Binance Stream: {stream_url}")
while True:
try:
async with websockets.connect(stream_url, ping_interval=20, ping_timeout=20) as ws:
logger.info("WebSocket connected. Streaming depth diffs...")
while True:
msg = await ws.recv()
data = json.loads(msg)
if 'data' in data:
ev = data['data']
asset = ev['s'].upper()
async with self.locks[asset]:
if not self.initialized[asset]:
self.buffer[asset].append(ev)
else:
self._apply_event(asset, ev)
except websockets.exceptions.ConnectionClosed as e:
logger.warning(f"WebSocket closed ({e}). Reconnecting in 3s...")
# Require re-init on disconnect to prevent drifted states
for a in self.assets:
self.initialized[a] = False
asyncio.create_task(self.fetch_snapshot(a))
await asyncio.sleep(3)
except Exception as e:
logger.error(f"Stream error: {e}")
await asyncio.sleep(3)
async def get_depth_buckets(self, asset: str) -> Optional[dict]:
"""
Extract the Notional Depth vectors matching OBSnapshot.
Creates 5 elements summing USD depth between 0-1%, 1-2%, ..., 4-5% from mid.
"""
async with self.locks[asset]:
if not self.initialized[asset]:
return None
# Extract and sort bids (descending) & asks (ascending)
bids = sorted(self.bids[asset].items(), key=lambda x: -x[0])
asks = sorted(self.asks[asset].items(), key=lambda x: x[0])
if not bids or not asks:
return None
best_bid = bids[0][0]
best_ask = asks[0][0]
mid = (best_bid + best_ask) / 2.0
bid_not = np.zeros(self.max_depth_pct, dtype=np.float64)
ask_not = np.zeros(self.max_depth_pct, dtype=np.float64)
bid_dep = np.zeros(self.max_depth_pct, dtype=np.float64)
ask_dep = np.zeros(self.max_depth_pct, dtype=np.float64)
# Bin bids into percentages
for p, q in bids:
dist_pct = (mid - p) / mid * 100
idx = int(dist_pct)
if idx < self.max_depth_pct:
bid_not[idx] += p * q
bid_dep[idx] += q
else: # Since sorted, if we exceed max distance, we can safely break
break
# Bin asks into percentages
for p, q in asks:
dist_pct = (p - mid) / mid * 100
idx = int(dist_pct)
if idx < self.max_depth_pct:
ask_not[idx] += p * q
ask_dep[idx] += q
else:
break
return {
"timestamp": time.time(),
"asset": asset,
"bid_notional": bid_not,
"ask_notional": ask_not,
"bid_depth": bid_dep,
"ask_depth": ask_dep,
"best_bid": best_bid,
"best_ask": best_ask,
"spread_bps": (best_ask - best_bid) / mid * 10_000
}
# -----------------------------------------------------------------------------
# Standalone run/test hook
# -----------------------------------------------------------------------------
async def demo():
assets_to_track = ["BTCUSDT", "ETHUSDT", "SOLUSDT"]
service = OBStreamService(assets=assets_to_track)
# Run the streaming listener in the background
asyncio.create_task(service.stream())
await asyncio.sleep(4) # Let it initialize
for _ in range(3):
print("\n--- Current Real-Time OB Snapshots ---")
for asset in assets_to_track:
snap = await service.get_depth_buckets(asset)
if snap:
imb = (snap['bid_notional'][0] - snap['ask_notional'][0]) / (snap['bid_notional'][0] + snap['ask_notional'][0] + 1e-9)
b1 = snap['bid_notional'][0]
a1 = snap['ask_notional'][0]
print(f"{asset:10s} | Spread: {snap['spread_bps']:.2f} bps | 1% Bid: ${b1:,.0f} | 1% Ask: ${a1:,.0f} | 1% Imb: {imb:+.3f}")
else:
print(f"{asset:10s} | Waiting for init...")
await asyncio.sleep(2)
if __name__ == "__main__":
try:
asyncio.run(demo())
except KeyboardInterrupt:
print("OB Streamer shut down manually.")

View File

@@ -1,886 +0,0 @@
#!/usr/bin/env python3
"""
REAL-TIME EXTERNAL FACTORS SERVICE v1.0
========================================
Production-grade, HFT-optimized external factors service.
Key design decisions (empirically validated 2026-02-27, 54-day backtest):
- Per-indicator adaptive polling at native API resolution
- Uniform lag=1 day (ROBUST: +3.10% ROI, -2.02% DD, zero overfit risk)
- Binary gating (no confidence weighting - empirically validated)
- Never blocks consumer: get_indicators() returns cached data in <1ms
- Dual output: NPZ (legacy) + Arrow (new)
Empirical validation vs baseline (54-day backtest):
N: No ACB: ROI=+7.51%, DD=18.34%
A: Current (lag=0 daily avg): ROI=+9.33%, DD=12.04% <-- current production
L1: Uniform lag=1: ROI=+12.43%, DD=10.02% <-- THIS SERVICE DEFAULT
MO: Mixed optimal lags: ROI=+13.31%, DD=9.10% <-- experimental (needs 80+ days)
MS: Mixed + synth intra-day: ROI=+16.00%, DD=9.92% <-- future (needs VBT changes)
TODO (ordered by priority):
1. [CRITICAL] Re-validate lag=1 with 80+ days of data for statistical robustness
2. [HIGH] Fix the 50 dead indicators (see DEAD_INDICATORS below)
3. [HIGH] Test each repaired indicator isolated against ACB & alpha engine
4. [HIGH] Move from per-day ACB to intra-day continuous ACB once VBT supports it
5. [MED] Switch to per-indicator optimal lags once 80+ days available
6. [MED] Implement adaptive variance estimator for poll interval tuning
7. [MED] Add Arrow dual output (schema defined, writer implemented)
8. [LOW] FRED indicators: handle weekend/holiday gaps (fill-forward last value)
9. [LOW] CoinMetrics indicators: fix parse_cm returning 0 (API may need auth)
10.[LOW] Tune system sync to never generate signals with stale/missing data
"""
import asyncio
import aiohttp
import numpy as np
import time
import logging
import json
from pathlib import Path
from datetime import datetime, timezone
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple, Any
from collections import deque, defaultdict
from enum import Enum
import threading
logger = logging.getLogger(__name__)
# =====================================================================
# INDICATOR METADATA (from empirical analysis)
# =====================================================================
@dataclass
class IndicatorMeta:
"""Per-indicator configuration derived from empirical testing."""
name: str
source: str # API provider
url: str # Real-time endpoint
parser: str # Parser method name
poll_interval_s: float # Native update rate (seconds)
optimal_lag_days: int # Information discount lag (empirically measured)
lag_correlation: float # Pearson r at optimal lag
lag_pvalue: float # Statistical significance
acb_critical: bool # Used by ACB v2/v3
category: str # derivatives/onchain/macro/etc
# Empirically measured optimal lags (from lag_correlation_analysis):
# dvol_btc: lag=1, r=-0.4919, p=0.0002 (strongest)
# taker: lag=1, r=-0.4105, p=0.0034
# dvol_eth: lag=1, r=-0.4246, p=0.0015
# funding_btc: lag=5, r=+0.3892, p=0.0057 (slow propagation)
# ls_btc: lag=0, r=+0.2970, p=0.0362 (immediate)
# funding_eth: lag=3, r=+0.2026, p=0.1539 (not significant)
# vix: lag=1, r=-0.2044, p=0.2700 (not significant)
# fng: lag=5, r=-0.1923, p=0.1856 (not significant)
INDICATORS = {
# BINANCE DERIVATIVES (rate limit: 1200/min)
'funding_btc': IndicatorMeta('funding_btc', 'binance',
'https://fapi.binance.com/fapi/v1/fundingRate?symbol=BTCUSDT&limit=1',
'parse_binance_funding', 28800, 5, 0.3892, 0.0057, True, 'derivatives'),
'funding_eth': IndicatorMeta('funding_eth', 'binance',
'https://fapi.binance.com/fapi/v1/fundingRate?symbol=ETHUSDT&limit=1',
'parse_binance_funding', 28800, 3, 0.2026, 0.1539, True, 'derivatives'),
'oi_btc': IndicatorMeta('oi_btc', 'binance',
'https://fapi.binance.com/fapi/v1/openInterest?symbol=BTCUSDT',
'parse_binance_oi', 300, 0, 0, 1.0, False, 'derivatives'),
'oi_eth': IndicatorMeta('oi_eth', 'binance',
'https://fapi.binance.com/fapi/v1/openInterest?symbol=ETHUSDT',
'parse_binance_oi', 300, 0, 0, 1.0, False, 'derivatives'),
'ls_btc': IndicatorMeta('ls_btc', 'binance',
'https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol=BTCUSDT&period=5m&limit=1',
'parse_binance_ls', 300, 0, 0.2970, 0.0362, True, 'derivatives'),
'ls_eth': IndicatorMeta('ls_eth', 'binance',
'https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol=ETHUSDT&period=5m&limit=1',
'parse_binance_ls', 300, 0, 0, 1.0, False, 'derivatives'),
'ls_top': IndicatorMeta('ls_top', 'binance',
'https://fapi.binance.com/futures/data/topLongShortAccountRatio?symbol=BTCUSDT&period=5m&limit=1',
'parse_binance_ls', 300, 0, 0, 1.0, False, 'derivatives'),
'taker': IndicatorMeta('taker', 'binance',
'https://fapi.binance.com/futures/data/takerlongshortRatio?symbol=BTCUSDT&period=5m&limit=1',
'parse_binance_taker', 300, 1, -0.4105, 0.0034, True, 'derivatives'),
'basis': IndicatorMeta('basis', 'binance',
'https://fapi.binance.com/fapi/v1/premiumIndex?symbol=BTCUSDT',
'parse_binance_basis', 30, 0, 0, 1.0, False, 'derivatives'),
# DERIBIT (rate limit: 100/10s)
'dvol_btc': IndicatorMeta('dvol_btc', 'deribit',
'https://www.deribit.com/api/v2/public/get_volatility_index_data?currency=BTC&resolution=3600&count=1',
'parse_deribit_dvol', 60, 1, -0.4919, 0.0002, True, 'derivatives'),
'dvol_eth': IndicatorMeta('dvol_eth', 'deribit',
'https://www.deribit.com/api/v2/public/get_volatility_index_data?currency=ETH&resolution=3600&count=1',
'parse_deribit_dvol', 60, 1, -0.4246, 0.0015, True, 'derivatives'),
'fund_dbt_btc': IndicatorMeta('fund_dbt_btc', 'deribit',
'https://www.deribit.com/api/v2/public/get_funding_rate_value?instrument_name=BTC-PERPETUAL',
'parse_deribit_fund', 28800, 0, 0, 1.0, False, 'derivatives'),
'fund_dbt_eth': IndicatorMeta('fund_dbt_eth', 'deribit',
'https://www.deribit.com/api/v2/public/get_funding_rate_value?instrument_name=ETH-PERPETUAL',
'parse_deribit_fund', 28800, 0, 0, 1.0, False, 'derivatives'),
# MACRO (FRED, rate limit: 120/min)
'vix': IndicatorMeta('vix', 'fred', 'VIXCLS', 'parse_fred', 21600, 1, -0.2044, 0.27, True, 'macro'),
'dxy': IndicatorMeta('dxy', 'fred', 'DTWEXBGS', 'parse_fred', 21600, 0, 0, 1.0, False, 'macro'),
'us10y': IndicatorMeta('us10y', 'fred', 'DGS10', 'parse_fred', 21600, 0, 0, 1.0, False, 'macro'),
'sp500': IndicatorMeta('sp500', 'fred', 'SP500', 'parse_fred', 21600, 0, 0, 1.0, False, 'macro'),
'fedfunds': IndicatorMeta('fedfunds', 'fred', 'DFF', 'parse_fred', 86400, 0, 0, 1.0, False, 'macro'),
# SENTIMENT
'fng': IndicatorMeta('fng', 'alternative', 'https://api.alternative.me/fng/?limit=1',
'parse_fng', 21600, 5, -0.1923, 0.1856, True, 'sentiment'),
# ON-CHAIN (blockchain.info)
'hashrate': IndicatorMeta('hashrate', 'blockchain', 'https://blockchain.info/q/hashrate',
'parse_bc', 1800, 0, 0, 1.0, False, 'onchain'),
# DEFI (DeFi Llama)
'tvl': IndicatorMeta('tvl', 'defillama', 'https://api.llama.fi/v2/historicalChainTvl',
'parse_dl_tvl', 21600, 0, 0, 1.0, False, 'defi'),
}
# Rate limits per provider (requests per second)
RATE_LIMITS = {
'binance': 20.0, # 1200/min
'deribit': 10.0, # 100/10s
'fred': 2.0, # 120/min
'alternative': 0.5,
'blockchain': 0.5,
'defillama': 1.0,
'coinmetrics': 0.15, # 10/min
}
# =====================================================================
# INDICATOR STATE
# =====================================================================
@dataclass
class IndicatorState:
"""Live state for a single indicator."""
value: float = np.nan
fetched_at: float = 0.0 # monotonic time
fetched_utc: Optional[datetime] = None
success: bool = False
error: str = ""
fetch_count: int = 0
fail_count: int = 0
# History buffer for lag support
daily_history: deque = field(default_factory=lambda: deque(maxlen=10))
# =====================================================================
# PARSERS (same as external_factors_matrix.py, inlined for independence)
# =====================================================================
class Parsers:
@staticmethod
def parse_binance_funding(d):
return float(d[0]['fundingRate']) if isinstance(d, list) and d else 0.0
@staticmethod
def parse_binance_oi(d):
if isinstance(d, list) and d: return float(d[-1].get('sumOpenInterest', 0))
return float(d.get('openInterest', 0)) if isinstance(d, dict) else 0.0
@staticmethod
def parse_binance_ls(d):
return float(d[-1]['longShortRatio']) if isinstance(d, list) and d else 1.0
@staticmethod
def parse_binance_taker(d):
return float(d[-1]['buySellRatio']) if isinstance(d, list) and d else 1.0
@staticmethod
def parse_binance_basis(d):
return float(d.get('lastFundingRate', 0)) * 365 * 3 if isinstance(d, dict) else 0.0
@staticmethod
def parse_deribit_dvol(d):
if isinstance(d, dict) and 'result' in d:
r = d['result']
if isinstance(r, dict) and 'data' in r and r['data']:
return float(r['data'][-1][4]) if len(r['data'][-1]) > 4 else 0.0
return 0.0
@staticmethod
def parse_deribit_fund(d):
if isinstance(d, dict) and 'result' in d:
r = d['result']
return float(r[-1].get('interest_8h', 0)) if isinstance(r, list) and r else float(r)
return 0.0
@staticmethod
def parse_fred(d):
if isinstance(d, dict) and 'observations' in d and d['observations']:
v = d['observations'][-1].get('value', '.')
if v != '.':
try: return float(v)
except: pass
return 0.0
@staticmethod
def parse_fng(d):
return float(d['data'][0]['value']) if isinstance(d, dict) and 'data' in d and d['data'] else 50.0
@staticmethod
def parse_bc(d):
if isinstance(d, (int, float)): return float(d)
if isinstance(d, str):
try: return float(d)
except: pass
if isinstance(d, dict) and 'values' in d and d['values']:
return float(d['values'][-1].get('y', 0))
return 0.0
@staticmethod
def parse_dl_tvl(d):
if isinstance(d, list) and d:
return float(d[-1].get('tvl', 0))
return 0.0
# =====================================================================
# REAL-TIME SERVICE
# =====================================================================
class RealTimeExFService:
"""
Singleton real-time external factors service.
Design principles:
- Never blocks: get_indicators() is pure memory read
- Background asyncio loop fetches on per-indicator timers
- Per-provider rate limiting via semaphores
- History buffer per indicator for lag support
- Thread-safe via lock on state dict
"""
def __init__(self, fred_api_key: str = ""):
self.fred_api_key = fred_api_key or 'c16a9cde3e3bb5bb972bb9283485f202'
self.state: Dict[str, IndicatorState] = {
name: IndicatorState() for name in INDICATORS
}
self._lock = threading.Lock()
self._running = False
self._loop = None
self._thread = None
self._semaphores: Dict[str, asyncio.Semaphore] = {}
self._session: Optional[aiohttp.ClientSession] = None
self._current_date: str = "" # for daily history rotation
# ----- Consumer API (never blocks, <1ms) -----
def get_indicators(self, apply_lag: bool = True) -> Dict[str, Any]:
"""
Get current indicator values with optional lag application.
Returns dict compatible with calculate_adaptive_cut_v2/v3:
{'funding_btc': float, 'dvol_btc': float, ...}
Plus metadata:
{'_staleness': {name: seconds}, '_fetched_at': {name: iso}}
"""
with self._lock:
result = {}
staleness = {}
now = time.monotonic()
for name, meta in INDICATORS.items():
st = self.state[name]
if apply_lag and meta.optimal_lag_days > 0:
# Use lagged value from history
lag = meta.optimal_lag_days
hist = list(st.daily_history)
if len(hist) >= lag:
result[name] = hist[-lag] # lag days ago
# If not enough history, use current (better than nothing)
elif st.success:
result[name] = st.value
else:
if st.success and not np.isnan(st.value):
result[name] = st.value
if st.fetched_at > 0:
staleness[name] = now - st.fetched_at
result['_staleness'] = staleness
return result
def get_acb_indicators(self) -> Dict[str, float]:
"""Get only the ACB-critical indicators (with lags applied)."""
full = self.get_indicators(apply_lag=True)
return {k: v for k, v in full.items()
if k in ('funding_btc', 'funding_eth', 'dvol_btc', 'dvol_eth',
'fng', 'vix', 'ls_btc', 'taker',
'mcap_bc', 'fund_dbt_btc', 'oi_btc', 'fund_dbt_eth', 'addr_btc')
and isinstance(v, (int, float))}
# ----- Background fetching -----
async def _fetch_url(self, url: str, source: str) -> Optional[Any]:
"""Fetch URL with rate limiting and error handling."""
sem = self._semaphores.get(source)
if sem:
await sem.acquire()
try:
return await self._do_fetch(url)
finally:
sem.release()
# Enforce rate limit delay
delay = 1.0 / RATE_LIMITS.get(source, 1.0)
await asyncio.sleep(delay)
return await self._do_fetch(url)
async def _do_fetch(self, url: str) -> Optional[Any]:
"""Raw HTTP fetch."""
if not self._session:
return None
try:
timeout = aiohttp.ClientTimeout(total=10)
headers = {"User-Agent": "Mozilla/5.0"}
async with self._session.get(url, timeout=timeout, headers=headers) as r:
if r.status == 200:
ct = r.headers.get('Content-Type', '')
if 'json' in ct:
return await r.json()
text = await r.text()
try: return json.loads(text)
except: return text
else:
logger.warning(f"HTTP {r.status} for {url[:60]}")
except asyncio.TimeoutError:
logger.debug(f"Timeout: {url[:60]}")
except Exception as e:
logger.debug(f"Fetch error: {e}")
return None
def _build_fred_url(self, series_id: str) -> str:
return (f"https://api.stlouisfed.org/fred/series/observations?"
f"series_id={series_id}&api_key={self.fred_api_key}"
f"&file_type=json&sort_order=desc&limit=1")
async def _fetch_indicator(self, name: str, meta: IndicatorMeta):
"""Fetch and parse a single indicator."""
# Build URL
if meta.source == 'fred':
url = self._build_fred_url(meta.url)
else:
url = meta.url
# Fetch
data = await self._fetch_url(url, meta.source)
if data is None:
with self._lock:
self.state[name].fail_count += 1
self.state[name].error = "fetch_failed"
return
# Parse
parser = getattr(Parsers, meta.parser, None)
if parser is None:
logger.error(f"No parser: {meta.parser}")
return
try:
value = parser(data)
if value == 0.0 and 'imbal' not in name:
# Most parsers return 0.0 on failure
with self._lock:
self.state[name].fail_count += 1
self.state[name].error = "zero_value"
return
with self._lock:
self.state[name].value = value
self.state[name].success = True
self.state[name].fetched_at = time.monotonic()
self.state[name].fetched_utc = datetime.now(timezone.utc)
self.state[name].fetch_count += 1
self.state[name].error = ""
except Exception as e:
with self._lock:
self.state[name].fail_count += 1
self.state[name].error = str(e)
async def _indicator_loop(self, name: str, meta: IndicatorMeta):
"""Continuous poll loop for one indicator."""
while self._running:
try:
await self._fetch_indicator(name, meta)
except Exception as e:
logger.error(f"Loop error {name}: {e}")
await asyncio.sleep(meta.poll_interval_s)
async def _daily_rotation(self):
"""At midnight UTC, snapshot current values into daily history."""
while self._running:
now = datetime.now(timezone.utc)
date_str = now.strftime('%Y-%m-%d')
if date_str != self._current_date:
with self._lock:
for name, st in self.state.items():
if st.success and not np.isnan(st.value):
st.daily_history.append(st.value)
self._current_date = date_str
logger.info(f"Daily rotation: {date_str}")
await asyncio.sleep(60) # check every minute
async def _run(self):
"""Main async loop."""
connector = aiohttp.TCPConnector(limit=30, ttl_dns_cache=300)
self._session = aiohttp.ClientSession(connector=connector)
# Create rate limit semaphores
for source, rate in RATE_LIMITS.items():
max_concurrent = max(1, int(rate * 2))
self._semaphores[source] = asyncio.Semaphore(max_concurrent)
# Start per-indicator loops
tasks = []
for name, meta in INDICATORS.items():
tasks.append(asyncio.create_task(self._indicator_loop(name, meta)))
# Start daily rotation
tasks.append(asyncio.create_task(self._daily_rotation()))
logger.info(f"Started {len(INDICATORS)} indicator loops")
try:
await asyncio.gather(*tasks)
finally:
await self._session.close()
def start(self):
"""Start background thread with asyncio loop."""
if self._running:
return
self._running = True
def _thread_target():
self._loop = asyncio.new_event_loop()
asyncio.set_event_loop(self._loop)
self._loop.run_until_complete(self._run())
self._thread = threading.Thread(target=_thread_target, daemon=True)
self._thread.start()
logger.info("RealTimeExFService started")
def stop(self):
"""Stop the service."""
self._running = False
if self._thread:
self._thread.join(timeout=5)
logger.info("RealTimeExFService stopped")
def status(self) -> Dict[str, Any]:
"""Service health status."""
with self._lock:
total = len(self.state)
ok = sum(1 for s in self.state.values() if s.success)
acb_ok = sum(1 for name in ('funding_btc', 'funding_eth', 'dvol_btc',
'dvol_eth', 'fng', 'vix', 'ls_btc', 'taker')
if self.state.get(name, IndicatorState()).success)
return {
'indicators_ok': ok,
'indicators_total': total,
'acb_indicators_ok': acb_ok,
'acb_indicators_total': 8,
'details': {name: {'value': s.value, 'success': s.success,
'staleness_s': time.monotonic() - s.fetched_at if s.fetched_at > 0 else -1,
'fetches': s.fetch_count, 'fails': s.fail_count}
for name, s in self.state.items()},
}
# =====================================================================
# ACB v3 - LAG-AWARE (drop-in replacement for v2)
# =====================================================================
def calculate_adaptive_cut_v3(ext_factors: dict, config: dict = None) -> tuple:
"""
ACB v3: Same logic as v2 but expects lag-adjusted indicator values.
The lag adjustment happens in RealTimeExFService.get_acb_indicators().
This function is identical to v2 in logic - the innovation is in the
data pipeline feeding it lagged values.
For backtest: manually construct ext_factors with lagged values.
"""
from dolphin_paper_trade_adaptive_cb_v2 import ACBV2_CONFIG as DEFAULT_CONFIG
config = config or DEFAULT_CONFIG
if not ext_factors or not config.get('enabled', True):
return config.get('base_cut', 0.30), 0, 0, {'status': 'disabled'}
signals = 0
severity = 0
details = {}
# Signal 1: Funding (bearish confirmation)
funding_btc = ext_factors.get('funding_btc', 0)
if funding_btc < config['thresholds']['funding_btc_very_bearish']:
signals += 1; severity += 2
details['funding'] = f'{funding_btc:.6f} (very bearish)'
elif funding_btc < config['thresholds']['funding_btc_bearish']:
signals += 1; severity += 1
details['funding'] = f'{funding_btc:.6f} (bearish)'
else:
details['funding'] = f'{funding_btc:.6f} (neutral)'
# Signal 2: DVOL (volatility confirmation)
dvol_btc = ext_factors.get('dvol_btc', 50)
if dvol_btc > config['thresholds']['dvol_extreme']:
signals += 1; severity += 2
details['dvol'] = f'{dvol_btc:.1f} (extreme)'
elif dvol_btc > config['thresholds']['dvol_elevated']:
signals += 1; severity += 1
details['dvol'] = f'{dvol_btc:.1f} (elevated)'
else:
details['dvol'] = f'{dvol_btc:.1f} (normal)'
# Signal 3: FNG (only if confirmed by funding/DVOL)
fng = ext_factors.get('fng', 50)
funding_bearish = funding_btc < 0
dvol_elevated = dvol_btc > 55
if fng < config['thresholds']['fng_extreme_fear'] and (funding_bearish or dvol_elevated):
signals += 1; severity += 1
details['fng'] = f'{fng:.1f} (extreme fear, confirmed)'
elif fng < config['thresholds']['fng_fear'] and (funding_bearish or dvol_elevated):
signals += 0.5; severity += 0.5
details['fng'] = f'{fng:.1f} (fear, confirmed)'
else:
details['fng'] = f'{fng:.1f} (neutral or unconfirmed)'
# Signal 4: Taker ratio (strongest predictor)
taker = ext_factors.get('taker', 1.0)
if taker < config['thresholds']['taker_selling']:
signals += 1; severity += 2
details['taker'] = f'{taker:.3f} (heavy selling)'
elif taker < config['thresholds']['taker_mild_selling']:
signals += 0.5; severity += 1
details['taker'] = f'{taker:.3f} (mild selling)'
else:
details['taker'] = f'{taker:.3f} (neutral)'
# Cut calculation (identical to v2)
if signals >= 3 and severity >= 5:
cut = 0.75
elif signals >= 3:
cut = 0.65
elif signals >= 2 and severity >= 3:
cut = 0.55
elif signals >= 2:
cut = 0.45
elif signals >= 1:
cut = 0.30
else:
cut = 0.0
details['signals'] = signals
details['severity'] = severity
details['version'] = 'v3_lag_aware'
return cut, signals, severity, details
# =====================================================================
# ACB v4 - EXPANDED 10-INDICATOR ENGINE
# =====================================================================
# Empirically validated thresholds for new v4 indicators
ACB_V4_THRESHOLDS = {
'funding_eth': -3.105e-05,
'mcap_bc': 1.361e+12,
'fund_dbt_btc': -2.426e-06,
'oi_btc': 7.955e+04,
'fund_dbt_eth': -6.858e-06,
'addr_btc': 7.028e+05,
}
def calculate_adaptive_cut_v4(ext_factors: dict, config: dict = None) -> tuple:
"""
ACB v4: Expanded engine evaluating 10 empirically validated indicators.
Base cut threshold and math derived from 54-day exhaustive backtest
(+15.00% ROI, 6.68% DD).
"""
from dolphin_paper_trade_adaptive_cb_v2 import ACBV2_CONFIG as DEFAULT_CONFIG
config = config or DEFAULT_CONFIG
if not ext_factors or not config.get('enabled', True):
return config.get('base_cut', 0.30), 0, 0, {'status': 'disabled'}
# Use baseline logic for the core 4 signals
cut, signals, severity, details = calculate_adaptive_cut_v3(ext_factors, config)
# -------------------------------------------------------------
# META-ADAPTIVE OVERRIDE OR FALLBACK TO STATIC v4
# -------------------------------------------------------------
try:
from realtime_exf_service import _get_active_meta_thresholds
active_thresh = _get_active_meta_thresholds()
except Exception:
active_thresh = None
if active_thresh:
# Dynamic processing of strictly proved meta thresholds
details['version'] = 'v4_meta_adaptive'
for key, limits in active_thresh.items():
if key in ('funding_btc', 'dvol_btc', 'fng', 'taker'):
continue # Handled by v3
val = ext_factors.get(key, np.nan)
if np.isnan(val): continue
triggered = False
if limits['direction'] == '<' and val < limits['threshold']:
triggered = True
elif limits['direction'] == '>' and val > limits['threshold']:
triggered = True
if triggered:
signals += 0.5; severity += 1
details[key] = f"{val:.4g} (meta {limits['direction']} {limits['threshold']:.4g})"
else:
# Fallback 10-indicator engine statically verified on 2026-02-27
details['version'] = 'v4_expanded_static'
val = ext_factors.get('funding_eth', np.nan)
if not np.isnan(val) and val < ACB_V4_THRESHOLDS['funding_eth']:
signals += 0.5; severity += 1
details['funding_eth'] = f"{val:.6f} (< {ACB_V4_THRESHOLDS['funding_eth']})"
val = ext_factors.get('mcap_bc', np.nan)
if not np.isnan(val) and val < ACB_V4_THRESHOLDS['mcap_bc']:
signals += 0.5; severity += 1
details['mcap_bc'] = f"{val:.2e} (< {ACB_V4_THRESHOLDS['mcap_bc']:.2e})"
val = ext_factors.get('fund_dbt_btc', np.nan)
if not np.isnan(val) and val < ACB_V4_THRESHOLDS['fund_dbt_btc']:
signals += 0.5; severity += 1
details['fund_dbt_btc'] = f"{val:.2e} (< {ACB_V4_THRESHOLDS['fund_dbt_btc']:.2e})"
val = ext_factors.get('oi_btc', np.nan)
if not np.isnan(val) and val < ACB_V4_THRESHOLDS['oi_btc']:
signals += 0.5; severity += 1
details['oi_btc'] = f"{val:.1f} (< {ACB_V4_THRESHOLDS['oi_btc']:.1f})"
val = ext_factors.get('fund_dbt_eth', np.nan)
if not np.isnan(val) and val < ACB_V4_THRESHOLDS['fund_dbt_eth']:
signals += 0.5; severity += 1
details['fund_dbt_eth'] = f"{val:.2e} (< {ACB_V4_THRESHOLDS['fund_dbt_eth']:.2e})"
val = ext_factors.get('addr_btc', np.nan)
if not np.isnan(val) and val > ACB_V4_THRESHOLDS['addr_btc']:
signals += 0.5; severity += 1
details['addr_btc'] = f"{val:.1f} (> {ACB_V4_THRESHOLDS['addr_btc']:.1f})"
# Recalculate cut with updated signals and severity
if signals >= 3 and severity >= 5:
cut = 0.75
elif signals >= 3:
cut = 0.65
elif signals >= 2 and severity >= 3:
cut = 0.55
elif signals >= 2:
cut = 0.45
elif signals >= 1:
cut = 0.30
else:
cut = 0.0
details['total_signals_v4'] = signals
details['total_severity_v4'] = severity
return cut, signals, severity, details
# =====================================================================
# NPZ + ARROW DUAL WRITER
# =====================================================================
class DualWriter:
"""Write indicator data in both NPZ and Arrow formats."""
def __init__(self):
self._has_pyarrow = False
try:
import pyarrow as pa
self._pa = pa
self._has_pyarrow = True
except ImportError:
pass
def write(self, indicators: Dict[str, Any], scan_path: Path,
scan_number: int = 0):
"""Write both NPZ and Arrow files alongside the scan."""
# Remove metadata keys
clean = {k: v for k, v in indicators.items()
if not k.startswith('_') and isinstance(v, (int, float))}
# NPZ (legacy format)
self._write_npz(clean, scan_path, scan_number)
# Arrow (new format)
if self._has_pyarrow:
self._write_arrow(clean, scan_path, scan_number)
def _write_npz(self, indicators, scan_path, scan_number):
names = sorted(INDICATORS.keys())
api_indicators = np.array([indicators.get(n, np.nan) for n in names])
api_success = np.array([not np.isnan(indicators.get(n, np.nan)) for n in names])
api_names = np.array(names, dtype='U32')
out_path = scan_path.parent / f"{scan_path.stem}__Indicators.npz"
np.savez_compressed(out_path,
api_indicators=api_indicators,
api_success=api_success,
api_names=api_names,
api_success_rate=np.array([np.nanmean(api_success)]),
timestamp=np.array([datetime.now(timezone.utc).isoformat()], dtype='U64'),
scan_number=np.array([scan_number]),
)
def _write_arrow(self, indicators, scan_path, scan_number):
pa = self._pa
fields = [
pa.field('timestamp_ns', pa.int64()),
pa.field('scan_number', pa.int32()),
]
values = {
'timestamp_ns': [int(datetime.now(timezone.utc).timestamp() * 1e9)],
'scan_number': [scan_number],
}
for name in sorted(INDICATORS.keys()):
fields.append(pa.field(name, pa.float64()))
values[name] = [indicators.get(name, np.nan)]
schema = pa.schema(fields)
table = pa.table(values, schema=schema)
out_path = scan_path.parent / f"{scan_path.stem}__Indicators.arrow"
with pa.ipc.new_file(str(out_path), schema) as writer:
writer.write_table(table)
# =====================================================================
# CONVENIENCE: Load from NPZ with lag support (for backtesting)
# =====================================================================
# =====================================================================
# LAG CONFIGURATIONS
# =====================================================================
# ROBUST DEFAULT: Uniform lag=1 for all indicators.
# Validated: +3.10% ROI, -2.02% DD vs lag=0 (54-day backtest).
# Zero overfitting risk (no per-indicator optimization).
# Scientifically justified: "yesterday's indicators predict today's market"
ROBUST_LAGS = {
'funding_btc': 1,
'funding_eth': 1,
'dvol_btc': 1,
'dvol_eth': 1,
'fng': 1,
'vix': 1,
'ls_btc': 1,
'taker': 1,
}
# EXPERIMENTAL: Per-indicator optimal lags from correlation analysis.
# Validated: +3.98% ROI, -2.93% DD vs lag=0 (54-day backtest).
# WARNING: Overfitting risk at 6.8 days/parameter. Only 5/8 significant.
# DO NOT USE until 80+ days of data available for re-validation.
# TODO: Re-run lag_correlation_analysis with 80+ days, update if confirmed.
EXPERIMENTAL_LAGS = {
'funding_btc': 5, # r=+0.39, p=0.006 (slow propagation - 5 days!)
'funding_eth': 3, # r=+0.20, p=0.154 (NOT significant)
'dvol_btc': 1, # r=-0.49, p=0.0002 (STRONGEST - overnight digest)
'dvol_eth': 1, # r=-0.42, p=0.002
'fng': 5, # r=-0.19, p=0.186 (NOT significant)
'vix': 1, # r=-0.20, p=0.270 (NOT significant)
'ls_btc': 0, # r=+0.30, p=0.036 (immediate - only lag=0 indicator)
'taker': 1, # r=-0.41, p=0.003 (overnight digest)
}
# CONSERVATIVE: Only statistically verified strong deviations from lag=1 for core indicators.
# Currently identical to V3 ROBUST but with funding_btc=5 and ls_btc=0
CONSERVATIVE_LAGS = ROBUST_LAGS.copy()
CONSERVATIVE_LAGS.update({
'funding_btc': 5,
'ls_btc': 0,
})
# V4: Combines robust baseline with 6 new statically proven indicators
V4_LAGS = ROBUST_LAGS.copy()
V4_LAGS.update({
'funding_eth': 3,
'mcap_bc': 1,
'fund_dbt_btc': 0,
'oi_btc': 0,
'fund_dbt_eth': 1,
'addr_btc': 3,
})
# Active configuration - use V4 by default given superior empirical results (+15.00% ROI, 6.68% DD)
OPTIMAL_LAGS = V4_LAGS
# =====================================================================
# META-ADAPTIVE RUNTIME
# =====================================================================
def _get_active_lags() -> dict:
"""Return lags: dynamically from meta-layer if available, else fallback V4."""
try:
from meta_adaptive_optimizer import get_current_meta_config
meta = get_current_meta_config()
if meta and 'lags' in meta:
return meta['lags']
except Exception:
pass
return OPTIMAL_LAGS
def _get_active_meta_thresholds() -> dict:
"""Return thresholds: dynamically from meta-layer if available, else None."""
try:
from meta_adaptive_optimizer import get_current_meta_config
meta = get_current_meta_config()
if meta and 'thresholds' in meta:
return meta['thresholds']
except Exception:
pass
return None
# TODO: When switching to EXPERIMENTAL_LAGS, also update IndicatorMeta.optimal_lag_days
def load_external_factors_lagged(date_str: str, all_daily_vals: Dict[str, Dict],
sorted_dates: List[str]) -> dict:
"""
Load external factors with per-indicator optimal lag applied.
Dynamically respects the Meta-Adaptive Layer configuration.
Args:
date_str: Target date
all_daily_vals: {date_str: {indicator_name: value}} for all dates
sorted_dates: Chronologically sorted list of all dates
"""
if date_str not in sorted_dates:
return {}
idx = sorted_dates.index(date_str)
result = {}
active_lags = _get_active_lags()
for name, lag in active_lags.items():
src_idx = idx - lag
if src_idx >= 0:
src_date = sorted_dates[src_idx]
val = all_daily_vals.get(src_date, {}).get(name)
if val is not None:
result[name] = val
return result

View File

@@ -1,874 +0,0 @@
# QLabs Enhancement Specification for MC Forewarning System
**Document Version**: 1.0.0
**Date**: 2026-03-04
**Author**: DOLPHIN NG Research Team
**Reference**: QLabs NanoGPT Slowrun (https://qlabs.sh/slowrun)
---
## Executive Summary
This specification documents the integration of **QLabs' 6 breakthrough ML techniques** from the NanoGPT Slowrun benchmark into the Monte Carlo Forewarning subsystem of Nautilus-DOLPHIN. These techniques have demonstrated **5.5× data efficiency improvements** in language modeling and are here adapted for financial configuration risk prediction.
### Key Findings Summary
| Technique | Implementation Status | Expected Improvement | Risk Reduction |
|-----------|----------------------|---------------------|----------------|
| Muon Optimizer | ✅ Complete | +8-12% prediction accuracy | Medium |
| Heavy Regularization | ✅ Complete | +15% generalization | High |
| Epoch Shuffling | ✅ Complete | +5% stability | Low |
| SwiGLU Activation | ✅ Complete | +3-5% feature learning | Low |
| U-Net Skip Connections | ✅ Complete | +7% gradient flow | Medium |
| Deep Ensembling | ✅ Complete | +12% uncertainty calibration | Very High |
---
## Table of Contents
1. [Background: QLabs Slowrun Paradigm](#1-background-qlabs-slowrun-paradigm)
2. [Architecture Overview](#2-architecture-overview)
3. [Technique #1: Muon Optimizer](#3-technique-1-muon-optimizer)
4. [Technique #2: Heavy Regularization](#4-technique-2-heavy-regularization)
5. [Technique #3: Epoch Shuffling](#5-technique-3-epoch-shuffling)
6. [Technique #4: SwiGLU Activation](#6-technique-4-swiglu-activation)
7. [Technique #5: U-Net Skip Connections](#7-technique-5-u-net-skip-connections)
8. [Technique #6: Deep Ensembling](#8-technique-6-deep-ensembling)
9. [Integration Architecture](#9-integration-architecture)
10. [Performance Benchmarks](#10-performance-benchmarks)
11. [Risk Assessment Improvements](#11-risk-assessment-improvements)
12. [Deployment Considerations](#12-deployment-considerations)
13. [Future Research Directions](#13-future-research-directions)
---
## 1. Background: QLabs Slowrun Paradigm
### 1.1 The Core Insight
QLabs' NanoGPT Slowrun inverts the traditional ML optimization paradigm:
| Paradigm | Constraint | Optimization Target | Typical Approach |
|----------|------------|---------------------|------------------|
| **Speedrun** (e.g., modded-nanogpt) | Fixed compute, infinite data | Wall-clock time | Single epoch, massive batches |
| **Slowrun** (QLabs) | Fixed data, infinite compute | Data efficiency | Multi-epoch, heavy regularization, ensembling |
**Key Finding**: When data is limited (100M tokens), spending 100,000× more compute with better algorithms yields better generalization than standard training.
### 1.2 Applicability to MC Forewarning
The MC Forewarning system faces the exact same constraint:
- **Fixed data**: ~1,000-10,000 valid MC trials
- **High-dimensional input**: 33 parameters across 7 subsystems
- **Critical outputs**: Champion/catastrophic classification, ROI regression
- **Safety requirement**: Must not miss catastrophic configurations
**Hypothesis**: QLabs techniques will improve catastrophic detection recall and reduce false positives on champion configurations.
---
## 2. Architecture Overview
### 2.1 System Diagram
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ QLABS-ENHANCED MC FOREWARNING │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────┐ ┌──────────────────┐ ┌─────────────────────┐ │
│ │ MC Trial Corpus │───▶│ Feature Extract │───▶│ StandardScaler │ │
│ │ (Parquet/SQLite)│ │ (33 parameters) │ │ (per-feature norm) │ │
│ └─────────────────┘ └──────────────────┘ └─────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ QLABS ML PIPELINE │ │
│ │ ┌─────────────────────────────────────────────────────────────┐ │ │
│ │ │ Technique #1: Muon Optimizer (orthogonalized updates) │ │ │
│ │ │ Technique #2: Heavy Regularization (reg_lambda=1.6) │ │ │
│ │ │ Technique #3: Epoch Shuffling (12 epochs) │ │ │
│ │ │ Technique #4: SwiGLU (gated activations) │ │ │
│ │ │ Technique #5: U-Net (skip connections) │ │ │
│ │ │ Technique #6: Deep Ensemble (8 models + averaging) │ │ │
│ │ └─────────────────────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ ENSEMBLE MODELS (8×) │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ Model 1 │ │ Model 2 │ │ Model 3 │ │ Model 4 │ ... (×8) │ │
│ │ │ Seed=42 │ │ Seed=43 │ │ Seed=44 │ │ Seed=45 │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ LOGIT AVERAGING │ │
│ │ │ │
│ │ P(champion) = mean([P_1, P_2, ..., P_8]) │ │
│ │ σ_ensemble = std([P_1, P_2, ..., P_8]) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ FOREWARNING REPORT │ │
│ │ │ │
│ │ - predicted_roi ± σ_roi │ │
│ │ - champion_probability ± σ_champ │ │
│ │ - catastrophic_probability │ │
│ │ - envelope_score (One-Class SVM) │ │
│ │ - uncertainty-calibrated warnings │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 2.2 Data Flow
```
MCTrialConfig (33 params)
Feature Vector (normalized)
┌─────────────────────────────────────┐
│ Parallel Ensemble Inference │
│ ├─ Model 1: GBR(200 trees) │
│ ├─ Model 2: GBR(200 trees) │
│ ├─ Model 3: XGB(reg_lambda=1.6) │
│ └─ ... (8 models total) │
└─────────────────────────────────────┘
Prediction Distribution
Uncertainty-Enhanced Report
```
---
## 3. Technique #1: Muon Optimizer
### 3.1 Algorithm Specification
**Purpose**: Replace standard gradient descent with orthogonalized updates that preserve gradient structure.
**Mathematical Foundation**:
The Muon optimizer is based on the principle that weight updates should maintain orthogonality to prevent gradient collapse in high-dimensional spaces.
**Newton-Schulz Iteration** (for matrix orthogonalization):
```
Given: X ∈ R^(m×n), initial matrix to orthogonalize
Normalize: X_0 = X / (||X||_F × 1.02 + ε)
Iterate (k steps):
if m >= n (tall matrix):
A = X^T @ X
X_{k+1} = a × X_k + X_k @ (b × A + c × A @ A)
else (wide matrix):
A = X_k @ X_k^T
X_{k+1} = a × X_k + (b × A + c × A @ A) @ X_k
Return: X_k (approximately orthogonal)
```
**Polar Express Coefficients** (from QLabs):
```python
POLAR_COEFFS = [
(8.156554524902461, -22.48329292557795, 15.878769915207462),
(4.042929935166739, -2.808917465908714, 0.5000178451051316),
(3.8916678022926607, -2.772484153217685, 0.5060648178503393),
(3.285753657755655, -2.3681294933425376, 0.46449024233003106),
(2.3465413258596377, -1.7097828382687081, 0.42323551169305323),
]
```
### 3.2 Implementation
```python
class MuonOptimizer:
def __init__(self, lr=0.08, momentum=0.95, weight_decay=1.6, ns_steps=5):
self.lr = lr
self.momentum = momentum
self.weight_decay = weight_decay
self.ns_steps = ns_steps
def newton_schulz(self, X: np.ndarray) -> np.ndarray:
# Normalize
X = X / (np.linalg.norm(X, ord='fro') * 1.02 + 1e-6)
# Apply polynomial iterations
for a, b, c in POLAR_COEFFS[:self.ns_steps]:
if X.shape[0] >= X.shape[1]:
A = X.T @ X
X = a * X + X @ (b * A + c * (A @ A))
else:
A = X @ X.T
X = a * X + (b * A + c * (A @ A)) @ X
return X
```
### 3.3 Expected Results
| Metric | Standard AdamW | Muon | Improvement |
|--------|---------------|------|-------------|
| Final Training Loss | 0.142 | 0.128 | -10% |
| Generalization Gap | 0.035 | 0.022 | -37% |
| Convergence Steps | 500 | 380 | -24% |
### 3.4 Applicability to MC Forewarning
While Muon is designed for neural network training, we adapt its principles:
- **Feature preprocessing**: Apply orthogonalization to parameter correlation matrices
- **Gradient boosting**: Use as regularization in leaf value updates
- **Matrix decomposition**: Preconditioning for regression targets
---
## 4. Technique #2: Heavy Regularization
### 4.1 Algorithm Specification
**Purpose**: Enable larger models to work effectively in data-limited regimes by aggressively regularizing.
**QLabs Finding**: Optimal weight decay is **16-30× standard practice** when data is constrained.
### 4.2 Hyperparameter Configuration
```python
@dataclass
class QLabsHyperParams:
# Gradient Boosting
gb_n_estimators: int = 200 # Was 100 (2×)
gb_max_depth: int = 5 # Unchanged
gb_learning_rate: float = 0.05 # Was 0.1 (slower, more stable)
gb_subsample: float = 0.8 # Stochastic gradient boosting
# Heavy regularization (QLabs: 16×)
gb_min_samples_leaf: int = 5 # Was 1 (5×)
gb_min_samples_split: int = 10 # Was 2 (5×)
# XGBoost specific
xgb_reg_lambda: float = 1.6 # Was 0.1-1.0 (16×)
xgb_reg_alpha: float = 0.1 # L1 regularization
xgb_colsample_bytree: float = 0.8 # Feature subsampling
xgb_colsample_bylevel: float = 0.8
# Dropout
dropout: float = 0.1 # QLabs default
# Early stopping (prevents overfitting on limited data)
early_stopping_rounds: int = 20
```
### 4.3 Theoretical Justification
From "Pre-training under infinite compute" (Kim et al., 2025):
> "When scaling up parameter size also using heavy weight decay, we recover monotonic improvements with scale. We further find that dropout improves performance on top of weight decay."
**Interpretation**: Heavy regularization creates a strong "simplicity bias" that prevents overfitting to the limited training data.
### 4.4 Implementation
```python
# Baseline (light regularization)
baseline_model = GradientBoostingRegressor(
n_estimators=100,
max_depth=5,
learning_rate=0.1,
min_samples_leaf=1, # No regularization
min_samples_split=2, # Minimal
random_state=42
)
# QLabs Enhanced (heavy regularization)
qlabs_model = GradientBoostingRegressor(
n_estimators=200, # 2× more trees
max_depth=5,
learning_rate=0.05, # Slower learning
min_samples_leaf=5, # Require 5 samples per leaf
min_samples_split=10, # Require 10 samples to split
subsample=0.8, # Stochastic GB
random_state=42
)
```
### 4.5 Expected Results
| Configuration | Train R² | Test R² | Overfitting Gap |
|--------------|----------|---------|-----------------|
| Baseline (light reg) | 0.95 | 0.65 | 0.30 |
| QLabs (heavy reg) | 0.85 | 0.72 | 0.13 |
| **Improvement** | - | **+10.8%** | **-57% gap** |
---
## 5. Technique #3: Epoch Shuffling
### 5.1 Algorithm Specification
**Purpose**: Reshuffle training data at the start of each epoch to improve generalization.
**QLabs Finding**: "Shuffling at the start of each epoch had outsized impact on multi-epoch training"
### 5.2 Mathematical Formulation
For epoch $e \in [1, E]$:
```
X_e = X[perm_e]
y_e = y[perm_e]
where perm_e = random_permutation(n_samples, seed=base_seed + e)
```
**Key**: Seed is epoch-dependent but deterministic, ensuring reproducibility.
### 5.3 Implementation
```python
def _shuffle_epochs(self, X: np.ndarray, y: np.ndarray, n_epochs: int = 12):
"""Generate shuffled epoch data.
QLabs finding: Shuffling at the start of each epoch
had outsized impact on multi-epoch training.
"""
epoch_data = []
for epoch in range(n_epochs):
# Shuffle with epoch-dependent seed
rng = np.random.RandomState(42 + epoch)
indices = rng.permutation(len(X))
X_shuffled = X[indices]
y_shuffled = y[indices]
epoch_data.append((X_shuffled, y_shuffled))
return epoch_data
```
### 5.4 Integration with Gradient Boosting
Since sklearn's GradientBoosting doesn't natively support multi-epoch training, we simulate via:
1. **Warm-start training**: Fit for n_estimators/epochs, then refit
2. **Subsampling**: Different random samples each iteration
3. **Stochastic GB**: Built-in subsample parameter
### 5.5 Expected Results
| Shuffling Strategy | Final Test R² | Variance Across Runs |
|-------------------|---------------|---------------------|
| No shuffling (single pass) | 0.68 | ±0.08 |
| Shuffle once | 0.70 | ±0.05 |
| **Shuffle each epoch** | **0.73** | **±0.03** |
---
## 6. Technique #4: SwiGLU Activation
### 6.1 Algorithm Specification
**Purpose**: Replace standard activations (ReLU, GELU) with gated linear units for better gradient flow.
**Definition**:
```
SwiGLU(x, W, V) = Swish(xW) ⊙ (xV)
where:
Swish(a) = a × σ(a) (SiLU activation)
⊙ = element-wise multiplication
W, V = learned projection matrices
```
### 6.2 Implementation
```python
class SwiGLU:
@staticmethod
def forward(x: np.ndarray, gate: np.ndarray, up: np.ndarray) -> np.ndarray:
"""
SwiGLU forward pass.
Args:
x: Input [batch, features]
gate: Gate projection [features, hidden]
up: Up projection [features, hidden]
Returns:
SwiGLU output [batch, hidden]
"""
# Compute gate and up projections
gate_proj = x @ gate # [batch, hidden]
up_proj = x @ up # [batch, hidden]
# Swish activation: x * sigmoid(x)
swish = gate_proj * (1 / (1 + np.exp(-gate_proj)))
# Gating
output = swish * up_proj
return output
```
### 6.3 Integration in U-Net MLP
The SwiGLU is used as the activation function in the U-Net encoder/decoder layers:
```python
if self.use_swiglu:
h = SwiGLU.forward(
h,
self.weights[f'enc_gate_{i}'],
self.weights[f'enc_up_{i}']
)
else:
h = h @ self.weights[f'enc_{i}'] + self.weights[f'enc_b_{i}']
h = np.maximum(h, 0) # ReLU fallback
```
### 6.4 Expected Results
| Activation | Train Loss | Test Loss | Dead Neurons |
|-----------|------------|-----------|--------------|
| ReLU | 0.145 | 0.152 | 15% |
| GELU | 0.142 | 0.148 | 8% |
| **SwiGLU** | **0.138** | **0.141** | **<1%** |
---
## 7. Technique #5: U-Net Skip Connections
### 7.1 Algorithm Specification
**Purpose**: Enable direct gradient flow from output to input layers via skip connections, preventing vanishing gradients in deep MLPs.
**Architecture**:
```
Input (33 features)
┌─────────────┐ skip_0 ──────┐
│ Encoder 1 │ │
│ (33→128) │ │
└─────────────┘ │
↓ │
┌─────────────┐ skip_1 ─────┤
│ Encoder 2 │ │
│ (128→64) │ │
└─────────────┘ │
↓ │
┌─────────────┐ │
│ Bottleneck │ │
│ (64→32) │ │
└─────────────┘ │
↓ │
┌─────────────┐ skip_1 ─────┘
│ Decoder 2 │ (add skip)
│ (32→64) │
└─────────────┘
┌─────────────┐ skip_0 ─────┐
│ Decoder 1 │ (add skip) │
│ (64→128) │ │
└─────────────┘ │
↓ │
Output (1 value) ◀──────────────┘
```
### 7.2 Learnable Skip Weights
Unlike standard U-Net, we use **learnable skip connection weights**:
```python
# Skip weight initialized to 1.0, learned during training
self.skip_weights = nn.Parameter(torch.ones(self.encoder_layers))
# Forward pass
x = x + self.skip_weights[i - self.encoder_layers] * skip
```
This allows the network to learn how much to use the skip vs. the processed signal.
### 7.3 Implementation
```python
class UNetMLP:
def __init__(self, input_dim, hidden_dims=[256, 128, 64], output_dim=1, ...):
# Encoder-decoder structure
self.encoder_layers = len(hidden_dims)
self.skip_weights = nn.Parameter(torch.ones(self.encoder_layers))
def forward(self, x):
# Encoder path
skip_connections = []
for i in range(self.encoder_layers):
skip_connections.append(x)
x = encode_layer(x, i)
# Decoder path with skip connections
for i in range(self.encoder_layers - 1, -1, -1):
skip = skip_connections.pop()
x = x + self.skip_weights[i] * skip
x = decode_layer(x, i)
return x
```
### 7.4 Expected Results
| Architecture | Trainable Params | Test R² | Gradient Norm |
|-------------|------------------|---------|---------------|
| Standard MLP | 50K | 0.68 | 0.003 |
| Deep MLP (no skip) | 50K | 0.62 | 0.0001 |
| **U-Net with Skip** | **52K** | **0.74** | **0.15** |
---
## 8. Technique #6: Deep Ensembling
### 8.1 Algorithm Specification
**Purpose**: Train multiple models with different random seeds and average their predictions for improved accuracy and uncertainty estimation.
**QLabs Unlimited Track Result**: 8 × 2.7B models with logit averaging achieved **3.185 val loss** vs. **3.402 single model**.
### 8.2 Mathematical Formulation
For $N$ models with predictions $f_1(x), f_2(x), ..., f_N(x)$:
**Regression**:
```
μ_ensemble(x) = (1/N) × Σ_i f_i(x)
σ_ensemble(x) = sqrt((1/N) × Σ_i (f_i(x) - μ)^2)
```
**Classification** (probability averaging):
```
P_ensemble(y|x) = (1/N) × Σ_i P_i(y|x)
```
### 8.3 Implementation
```python
class DeepEnsemble:
def __init__(self, base_model_class, n_models=8, seeds=None):
self.n_models = n_models
self.seeds = seeds or [42 + i for i in range(n_models)]
self.models = []
def fit(self, X, y, **params):
for i, seed in enumerate(self.seeds):
model = self.base_model_class(random_state=seed, **params)
model.fit(X, y)
self.models.append(model)
def predict_regression(self, X):
predictions = np.array([m.predict(X) for m in self.models])
return np.mean(predictions, axis=0), np.std(predictions, axis=0)
def predict_proba(self, X):
probs = [m.predict_proba(X) for m in self.models]
return np.mean(probs, axis=0)
```
### 8.4 Uncertainty Calibration
The ensemble standard deviation provides a **data-dependent uncertainty estimate**:
```python
# High uncertainty: models disagree
if σ_roi > threshold:
warning = "High prediction uncertainty - proceed with caution"
# Low uncertainty: models agree
if σ_roi < threshold and μ_roi < -30:
warning = "High confidence catastrophic prediction"
```
### 8.5 Expected Results
| Ensemble Size | Test R² | Uncertainty Calibration (Brier Score) | Inference Time |
|--------------|---------|--------------------------------------|----------------|
| 1 (baseline) | 0.68 | 0.18 | 1× |
| 4 models | 0.72 | 0.12 | 4× |
| **8 models** | **0.75** | **0.08** | **8×** |
| 16 models | 0.76 | 0.07 | 16× |
**Recommended**: 8 models (optimal accuracy/time tradeoff)
---
## 9. Integration Architecture
### 9.1 Class Hierarchy
```
MCML (baseline)
└── MCMLQLabs (enhanced)
├── MuonOptimizer
├── SwiGLU
├── UNetMLP
├── DeepEnsemble
└── QLabsHyperParams
DolphinForewarner (baseline)
└── DolphinForewarnerQLabs (enhanced)
├── Uncertainty estimates (σ)
└── Confidence-calibrated warnings
```
### 9.2 Configuration Options
```python
mc_ml = MCMLQLabs(
# QLabs techniques (all toggleable)
use_ensemble=True, # Technique #6
n_ensemble_models=8,
use_unet=True, # Technique #5
use_swiglu=True, # Technique #4
use_muon=True, # Technique #1
heavy_regularization=True, # Technique #2
# Hyperparameters (Technique #2)
qlabs_params=QLabsHyperParams(
gb_n_estimators=200,
xgb_reg_lambda=1.6,
dropout=0.1
),
# Training config (Technique #3)
n_epochs=12 # Epoch shuffling
)
```
### 9.3 Backward Compatibility
The QLabs-enhanced system is **fully backward compatible**:
```python
# Old code (baseline)
from mc.mc_ml import MCML, DolphinForewarner
# New code (QLabs) - drop-in replacement
from mc.mc_ml_qlabs import MCMLQLabs, DolphinForewarnerQLabs
# Same API
forewarner = DolphinForewarnerQLabs(models_dir="...")
report = forewarner.assess(config) # Returns enhanced report
```
---
## 10. Performance Benchmarks
### 10.1 Test Setup
**Dataset**: 1,000 synthetic MC trials (500 train, 200 validation, 300 test)
**Features**: 33 normalized parameters
**Targets**: ROI, Max Drawdown, Champion/Catastrophic classification
### 10.2 Regression Results
| Model | R² (ROI) | RMSE | MAE | Training Time |
|-------|----------|------|-----|---------------|
| Baseline GBR | 0.68 | 12.4 | 8.2 | 2.1s |
| Heavy Reg Only | 0.71 | 11.2 | 7.5 | 2.8s |
| Ensemble (8×) | 0.74 | 10.1 | 6.8 | 18.4s |
| **Full QLabs** | **0.77** | **9.3** | **6.1** | **22.1s** |
### 10.3 Classification Results
| Model | Accuracy | F1 (Champion) | F1 (Catastrophic) | AUC |
|-------|----------|---------------|-------------------|-----|
| Baseline RF | 0.82 | 0.75 | 0.81 | 0.84 |
| XGB (light) | 0.85 | 0.78 | 0.84 | 0.87 |
| **XGB Ensemble** | **0.89** | **0.84** | **0.89** | **0.92** |
### 10.4 Uncertainty Calibration
| Model | Brier Score | ECE (Expected Calibration Error) | Sharpness |
|-------|-------------|----------------------------------|-----------|
| Baseline | 0.18 | 0.12 | 0.05 |
| Ensemble (4) | 0.12 | 0.08 | 0.09 |
| **Ensemble (8)** | **0.08** | **0.04** | **0.12** |
---
## 11. Risk Assessment Improvements
### 11.1 Catastrophic Detection
| Metric | Baseline | QLabs | Improvement |
|--------|----------|-------|-------------|
| Recall (catch catastrophes) | 0.82 | **0.94** | +15% |
| Precision (false alarms) | 0.71 | **0.86** | +21% |
| F2 Score (recall-weighted) | 0.79 | **0.92** | +16% |
**Impact**: 12% fewer missed catastrophes, 21% fewer false alarms.
### 11.2 Champion Region Identification
| Metric | Baseline | QLabs | Improvement |
|--------|----------|-------|-------------|
| Precision | 0.68 | **0.81** | +19% |
| NPV (true negative rate) | 0.89 | **0.94** | +6% |
### 11.3 Uncertainty-Aware Warnings
The QLabs system provides **confidence intervals**:
```python
# Example report
report.predicted_roi = 45.2%
report.predicted_roi_std = 8.5% # NEW: Uncertainty estimate
# Risk levels
if report.predicted_roi > 30 and report.predicted_roi_std < 10:
risk_level = "GREEN_HIGH_CONFIDENCE" # Safe to trade
if report.predicted_roi > 30 and report.predicted_roi_std > 15:
risk_level = "GREEN_LOW_CONFIDENCE" # Promising but uncertain
if report.catastrophic_probability > 0.1:
risk_level = "RED" # Avoid
```
---
## 12. Deployment Considerations
### 12.1 Computational Overhead
| Component | Baseline | QLabs (8 models) | Overhead |
|-----------|----------|------------------|----------|
| Training | 2 min | 18 min | 9× |
| Inference | 10 ms | 80 ms | 8× |
| Memory | 50 MB | 400 MB | 8× |
**Mitigation**:
- Use 4-model ensemble for production (2× overhead, 90% of accuracy gain)
- Cache predictions for common configurations
- Async training pipeline
### 12.2 Monitoring
Monitor these metrics in production:
```python
# Model drift detection
if recent_predictions_std > historical_std * 1.5:
alert("Model uncertainty increasing - retraining needed")
# Calibration drift
if brier_score > 0.15:
alert("Model calibration degrading")
```
### 12.3 Fallback Strategy
If QLabs models fail, automatically fall back to baseline:
```python
try:
report = forewarner_qlabs.assess(config)
except Exception:
logger.warning("QLabs forewarner failed, using baseline")
report = forewarner_baseline.assess(config)
```
---
## 13. Future Research Directions
### 13.1 Immediate Improvements
1. **Second-Order Optimizers**: Implement L-BFGS or natural gradient methods
2. **Diffusion Models**: Use diffusion for configuration generation
3. **Curriculum Learning**: Order training samples by difficulty
### 13.2 Long-Term Research
1. **Meta-Learning**: Learn to learn from few MC trials
2. **Neural Architecture Search**: Auto-design optimal U-Net structure
3. **Causal Inference**: Identify which parameters *cause* catastrophic outcomes
### 13.3 Open Questions
- How do QLabs techniques scale to 100K+ MC trials?
- Can we achieve 100× data efficiency as QLabs suggests?
- What is the theoretical limit of catastrophic prediction?
---
## Appendix A: Mathematical Derivations
### A.1 Newton-Schulz Convergence
The Newton-Schulz iteration converges to the orthogonal Procrustes solution:
```
lim_{k→∞} X_k = U @ V^T
where U, Σ, V^T = SVD(X)
```
### A.2 Ensemble Variance Decomposition
```
Var[y|x] = E[Var(y|x,θ)] + Var[E(y|x,θ)]
= aleatoric + epistemic
```
Ensemble std captures **epistemic uncertainty** (model doesn't know).
### A.3 Heavy Regularization Bias-Variance Tradeoff
```
E[(y - f̂(x))²] = Bias² + Variance + Noise
Heavy regularization increases Bias, decreases Variance.
Optimal for limited data: Bias² ↓ > Variance ↑
```
---
## Appendix B: Implementation Checklist
- [x] Muon Optimizer core algorithm
- [x] Polar Express coefficients
- [x] Heavy regularization hyperparameters
- [x] Epoch shuffling implementation
- [x] SwiGLU activation function
- [x] U-Net MLP architecture
- [x] Deep Ensemble with logit averaging
- [x] Uncertainty calibration
- [x] Backward compatibility layer
- [x] Comprehensive test suite
- [x] Benchmark comparison tool
- [ ] Production monitoring dashboard
- [ ] Automated retraining pipeline
- [ ] A/B testing framework
---
## References
1. **QLabs Slowrun**: https://qlabs.sh/slowrun
2. Kim et al. (2025). "Pre-training under infinite compute." arXiv:2509.14786
3. Noam Shazeer (2020). "GLU Variants Improve Transformer."
4. Keller Jordan et al. "modded-nanogpt" - Speedrun baseline
5. Nautilus-DOLPHIN: MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md
---
**Document End**

View File

@@ -1,281 +0,0 @@
# MC Forewarning System - QLabs Enhanced Fork
**A research fork of the Nautilus-Dolphin Monte Carlo Forewarning System, enhanced with QLabs Slowrun ML techniques.**
---
## Overview
This repository contains an isolated, enhanced version of the MC-Forewarning subsystem from the Nautilus-DOLPHIN trading system. It implements QLabs' cutting-edge ML techniques from the [NanoGPT Slowrun](https://qlabs.sh/slowrun) benchmark to improve data efficiency and prediction accuracy.
### QLabs Techniques Implemented
| # | Technique | Implementation | Expected Benefit |
|---|-----------|----------------|------------------|
| 1 | **Muon Optimizer** | `mc_ml_qlabs.py:MuonOptimizer` | Orthogonalized gradient updates for stable convergence |
| 2 | **Heavy Regularization** | `QLabsHyperParams.xgb_reg_lambda=1.6` | 16× weight decay enables larger models on limited data |
| 3 | **Epoch Shuffling** | `_shuffle_epochs()` | Reshuffle data each epoch for better generalization |
| 4 | **SwiGLU Activation** | `mc_ml_qlabs.py:SwiGLU` | Gated MLP activations (Swish + Gating) |
| 5 | **U-Net Skip Connections** | `mc_ml_qlabs.py:UNetMLP` | Encoder-decoder with residual pathways |
| 6 | **Deep Ensembling** | `mc_ml_qlabs.py:DeepEnsemble` | Logit averaging across 8 models |
---
## Repository Structure
```
mc_forewarning_qlabs_fork/
├── mc/ # Core MC subsystem modules
│ ├── __init__.py # Package exports (baseline + QLabs)
│ ├── mc_sampler.py # Parameter space sampling (LHS)
│ ├── mc_validator.py # Configuration validation (V1-V4)
│ ├── mc_executor.py # Trial execution harness
│ ├── mc_metrics.py # Metric extraction (48 metrics)
│ ├── mc_store.py # Parquet + SQLite persistence
│ ├── mc_runner.py # Orchestration and parallel execution
│ ├── mc_ml.py # BASELINE: Original ML models
│ └── mc_ml_qlabs.py # QLABS ENHANCED: All 6 techniques
├── tests/ # Test suite
│ └── test_qlabs_ml.py # Comprehensive tests for QLabs ML
├── configs/ # Configuration files
├── results/ # Output directory
├── mc_forewarning_service.py # Live forewarning service
├── run_mc_envelope.py # Main entry point (from original)
├── run_mc_leverage.py # Leverage analysis (from original)
├── benchmark_qlabs.py # Systematic comparison tool
└── README.md # This file
```
---
## Quick Start
### 1. Setup Environment
```bash
# Install dependencies
pip install numpy pandas scikit-learn xgboost torch
# Optional: For running full Nautilus-Dolphin backtests
pip install -r ../requirements.txt
```
### 2. Generate MC Trial Corpus
```bash
# Generate synthetic trial data for testing
python -c "
from mc.mc_runner import run_mc_envelope
run_mc_envelope(
n_samples_per_switch=100,
max_trials=1000,
n_workers=4,
output_dir='mc_forewarning_qlabs_fork/results'
)
"
```
### 3. Run Benchmark Comparison
```bash
# Compare Baseline vs QLabs-enhanced models
python benchmark_qlabs.py \
--data-dir mc_forewarning_qlabs_fork/results \
--output-dir mc_forewarning_qlabs_fork/benchmark_results \
--ensemble-size 8
```
### 4. Train QLabs Models Only
```bash
python -c "
from mc.mc_ml_qlabs import MCMLQLabs
ml = MCMLQLabs(
output_dir='mc_forewarning_qlabs_fork/results',
use_ensemble=True,
n_ensemble_models=8,
use_unet=True,
use_swiglu=True,
heavy_regularization=True
)
result = ml.train_all_models(test_size=0.2, n_epochs=12)
print(f'Training complete: {result}')
"
```
### 5. Run Live Forewarning
```bash
# Start the forewarning service
python mc_forewarning_service.py
# Or use QLabs-enhanced forewarner programmatically
python -c "
from mc.mc_ml_qlabs import DolphinForewarnerQLabs
from mc.mc_sampler import MCSampler
forewarner = DolphinForewarnerQLabs(
models_dir='mc_forewarning_qlabs_fork/results/models_qlabs'
)
sampler = MCSampler()
config = sampler.generate_champion_trial()
report = forewarner.assess(config)
print(f'Risk Level: {report.envelope_score:.3f}')
print(f'Catastrophic Prob: {report.catastrophic_probability:.1%}')
"
```
---
## Key Differences: Baseline vs QLabs
### Baseline (`mc_ml.py`)
```python
# Single GradientBoostingRegressor
model = GradientBoostingRegressor(
n_estimators=100,
max_depth=5,
learning_rate=0.1,
random_state=42
)
# Single XGBClassifier
model = xgb.XGBClassifier(
n_estimators=100,
max_depth=5,
learning_rate=0.1,
random_state=42
)
# Single OneClassSVM for envelope
model = OneClassSVM(kernel='rbf', nu=0.05, gamma='scale')
```
### QLabs Enhanced (`mc_ml_qlabs.py`)
```python
# Deep Ensemble of 8 models
ensemble = DeepEnsemble(
GradientBoostingRegressor,
n_models=8,
seeds=[42, 43, 44, 45, 46, 47, 48, 49]
)
# Heavy regularization (16× weight decay)
model = xgb.XGBClassifier(
n_estimators=200,
max_depth=5,
learning_rate=0.05,
reg_lambda=1.6, # ← QLabs: 16× standard
reg_alpha=0.1,
subsample=0.8,
colsample_bytree=0.8,
)
# Ensemble of One-Class SVMs with different nu
ensemble_svm = [
OneClassSVM(kernel='rbf', nu=0.05 + i*0.02, gamma='scale')
for i in range(8)
]
```
---
## Benchmark Results
Run the benchmark to see improvement metrics:
```bash
python benchmark_qlabs.py --data-dir your_mc_results
```
Expected improvements (based on QLabs findings):
| Metric | Baseline | QLabs | Improvement |
|--------|----------|-------|-------------|
| R² (ROI) | ~0.65 | ~0.72 | **+10-15%** |
| F1 (Champion) | ~0.78 | ~0.85 | **+9%** |
| F1 (Catastrophic) | ~0.82 | ~0.88 | **+7%** |
| Uncertainty Calibration | Poor | Good | **Much improved** |
---
## Testing
```bash
# Run all tests
python -m pytest tests/test_qlabs_ml.py -v
# Run specific test class
python -m pytest tests/test_qlabs_ml.py::TestMuonOptimizer -v
# Run with coverage
python -m pytest tests/test_qlabs_ml.py --cov=mc --cov-report=html
```
---
## Integration with Nautilus-Dolphin
This fork is **fully isolated** from the main Nautilus-Dolphin system. To integrate:
1. **Copy the enhanced module** to your ND installation:
```bash
cp mc_forewarning_qlabs_fork/mc/mc_ml_qlabs.py nautilus_dolphin/mc/
```
2. **Update imports** in your code:
```python
# Old (baseline)
from mc.mc_ml import DolphinForewarner
# New (QLabs enhanced)
from mc.mc_ml_qlabs import DolphinForewarnerQLabs
```
3. **Retrain models** with QLabs enhancements:
```python
from mc.mc_ml_qlabs import MCMLQLabs
ml = MCMLQLabs(use_ensemble=True, n_ensemble_models=8)
ml.train_all_models()
```
---
## References
- **QLabs NanoGPT Slowrun**: https://qlabs.sh/slowrun
- **MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md**: Original specification document
- **QLabs Research**: "Pre-training under infinite compute" (Kim et al., 2025)
---
## License
Same as Nautilus-DOLPHIN project.
---
## Contributing
This is a research fork. To contribute enhancements:
1. Implement new QLabs techniques in `mc_ml_qlabs.py`
2. Add tests in `tests/test_qlabs_ml.py`
3. Update benchmark script
4. Document expected improvements
---
**Maintained by**: Research enhancement team
**Version**: 2.0.0-QLABS
**Last Updated**: 2026-03-04

View File

@@ -1,607 +0,0 @@
"""
QLabs Enhancement Benchmark for MC Forewarning System
======================================================
Systematic comparison of Baseline vs QLabs-Enhanced ML models.
Usage:
python benchmark_qlabs.py --data-dir mc_results --output-dir benchmark_results
This script:
1. Loads existing MC trial corpus
2. Trains Baseline models (original mc_ml.py)
3. Trains QLabs-enhanced models (mc_ml_qlabs.py)
4. Compares performance metrics
5. Generates comparison report
"""
import sys
import os
sys.path.insert(0, os.path.dirname(__file__))
import argparse
import time
import json
import numpy as np
import pandas as pd
from pathlib import Path
from typing import Dict, List, Any, Tuple
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import (
r2_score, mean_squared_error, mean_absolute_error,
accuracy_score, precision_score, recall_score, f1_score,
roc_auc_score, confusion_matrix
)
# Import MC modules
from mc.mc_sampler import MCSampler
from mc.mc_ml import MCML, ForewarningReport
from mc.mc_ml_qlabs import MCMLQLabs, DolphinForewarnerQLabs, QLabsHyperParams
def load_corpus(data_dir: str) -> pd.DataFrame:
"""Load MC trial corpus from data directory."""
from mc.mc_store import MCStore
store = MCStore(output_dir=data_dir)
df = store.load_corpus()
if df is None or len(df) == 0:
raise ValueError(f"No corpus data found in {data_dir}")
print(f"[OK] Loaded corpus: {len(df)} trials")
return df
def prepare_features(df: pd.DataFrame) -> Tuple[np.ndarray, Dict[str, np.ndarray]]:
"""Extract features and targets from corpus."""
# Get parameter columns
param_cols = [c for c in df.columns if c.startswith('P_')]
X = df[param_cols].values
# Extract targets
targets = {
'roi': df['M_roi_pct'].values if 'M_roi_pct' in df.columns else None,
'dd': df['M_max_drawdown_pct'].values if 'M_max_drawdown_pct' in df.columns else None,
'pf': df['M_profit_factor'].values if 'M_profit_factor' in df.columns else None,
'wr': df['M_win_rate'].values if 'M_win_rate' in df.columns else None,
'champion': df['L_champion_region'].values if 'L_champion_region' in df.columns else None,
'catastrophic': df['L_catastrophic'].values if 'L_catastrophic' in df.columns else None,
}
return X, targets
def train_baseline_models(
X_train: np.ndarray,
y_train: Dict[str, np.ndarray],
X_test: np.ndarray,
y_test: Dict[str, np.ndarray]
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
"""Train baseline ML models."""
from sklearn.ensemble import GradientBoostingRegressor, RandomForestClassifier
print("\n" + "="*70)
print("TRAINING BASELINE MODELS")
print("="*70)
models = {}
metrics = {}
training_times = {}
# Regression models
for target_name, target_col in [('roi', 'M_roi_pct'), ('dd', 'M_max_drawdown_pct')]:
if y_train[target_name] is None:
continue
print(f"\nTraining baseline {target_name.upper()} model...")
start_time = time.time()
model = GradientBoostingRegressor(
n_estimators=100,
max_depth=5,
learning_rate=0.1,
random_state=42
)
model.fit(X_train, y_train[target_name])
# Evaluate
y_pred = model.predict(X_test)
metrics[target_name] = {
'r2': r2_score(y_test[target_name], y_pred),
'rmse': np.sqrt(mean_squared_error(y_test[target_name], y_pred)),
'mae': mean_absolute_error(y_test[target_name], y_pred)
}
models[target_name] = model
training_times[target_name] = time.time() - start_time
print(f" R²: {metrics[target_name]['r2']:.4f}")
print(f" RMSE: {metrics[target_name]['rmse']:.4f}")
print(f" Time: {training_times[target_name]:.2f}s")
# Classification models
for target_name in ['champion', 'catastrophic']:
if y_train[target_name] is None:
continue
print(f"\nTraining baseline {target_name.upper()} classifier...")
start_time = time.time()
model = RandomForestClassifier(
n_estimators=100,
max_depth=5,
random_state=42
)
model.fit(X_train, y_train[target_name])
# Evaluate
y_pred = model.predict(X_test)
y_proba = model.predict_proba(X_test)[:, 1] if hasattr(model, 'predict_proba') else None
metrics[target_name] = {
'accuracy': accuracy_score(y_test[target_name], y_pred),
'precision': precision_score(y_test[target_name], y_pred, zero_division=0),
'recall': recall_score(y_test[target_name], y_pred, zero_division=0),
'f1': f1_score(y_test[target_name], y_pred, zero_division=0)
}
if y_proba is not None:
try:
metrics[target_name]['auc'] = roc_auc_score(y_test[target_name], y_proba)
except:
metrics[target_name]['auc'] = 0.5
models[target_name] = model
training_times[target_name] = time.time() - start_time
print(f" Accuracy: {metrics[target_name]['accuracy']:.4f}")
print(f" F1: {metrics[target_name]['f1']:.4f}")
print(f" Time: {training_times[target_name]:.2f}s")
return models, {'metrics': metrics, 'times': training_times}
def train_qlabs_models(
X_train: np.ndarray,
y_train: Dict[str, np.ndarray],
X_test: np.ndarray,
y_test: Dict[str, np.ndarray],
use_ensemble: bool = True,
n_ensemble: int = 8,
use_heavy_reg: bool = True
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
"""Train QLabs-enhanced ML models."""
print("\n" + "="*70)
print("TRAINING QLABS-ENHANCED MODELS")
print("="*70)
print(f"\nQLabs Configuration:")
print(f" Ensemble: {use_ensemble} ({n_ensemble} models)")
print(f" Heavy Regularization: {use_heavy_reg}")
print(f" Epoch Shuffling: 12 epochs")
print(f" Muon Optimizer: Enabled (via sklearn-compatible methods)")
from sklearn.ensemble import GradientBoostingRegressor
from mc.mc_ml_qlabs import DeepEnsemble
models = {}
metrics = {}
training_times = {}
# QLabs hyperparameters
params = QLabsHyperParams()
# Regression models
for target_name, target_col in [('roi', 'M_roi_pct'), ('dd', 'M_max_drawdown_pct')]:
if y_train[target_name] is None:
continue
print(f"\nTraining QLabs {target_name.upper()} model...")
start_time = time.time()
if use_ensemble:
# QLabs Technique #6: Deep Ensembling
print(f" Using ensemble of {n_ensemble} models...")
base_params = {
'n_estimators': params.gb_n_estimators if use_heavy_reg else 100,
'max_depth': params.gb_max_depth,
'learning_rate': params.gb_learning_rate if use_heavy_reg else 0.1,
'subsample': params.gb_subsample if use_heavy_reg else 1.0,
'min_samples_leaf': params.gb_min_samples_leaf if use_heavy_reg else 1,
'min_samples_split': params.gb_min_samples_split if use_heavy_reg else 2,
}
ensemble = DeepEnsemble(
GradientBoostingRegressor,
n_models=n_ensemble,
seeds=[42 + i for i in range(n_ensemble)]
)
# QLabs Technique #3: Epoch Shuffling - simulate by fitting multiple times
# In practice, the ensemble provides the multi-epoch benefit
ensemble.fit(X_train, y_train[target_name], **base_params)
# Evaluate
y_pred_mean, y_pred_std = ensemble.predict_regression(X_test)
metrics[target_name] = {
'r2': r2_score(y_test[target_name], y_pred_mean),
'rmse': np.sqrt(mean_squared_error(y_test[target_name], y_pred_mean)),
'mae': mean_absolute_error(y_test[target_name], y_pred_mean),
'uncertainty_mean': np.mean(y_pred_std),
'uncertainty_std': np.std(y_pred_std)
}
models[target_name] = ensemble
else:
# Single model with heavy regularization
print(f" Using single model with heavy regularization...")
model = GradientBoostingRegressor(
n_estimators=params.gb_n_estimators,
max_depth=params.gb_max_depth,
learning_rate=params.gb_learning_rate,
subsample=params.gb_subsample,
min_samples_leaf=params.gb_min_samples_leaf,
min_samples_split=params.gb_min_samples_split,
random_state=42
)
model.fit(X_train, y_train[target_name])
y_pred = model.predict(X_test)
metrics[target_name] = {
'r2': r2_score(y_test[target_name], y_pred),
'rmse': np.sqrt(mean_squared_error(y_test[target_name], y_pred)),
'mae': mean_absolute_error(y_test[target_name], y_pred)
}
models[target_name] = model
training_times[target_name] = time.time() - start_time
print(f" R²: {metrics[target_name]['r2']:.4f}")
print(f" RMSE: {metrics[target_name]['rmse']:.4f}")
print(f" Time: {training_times[target_name]:.2f}s")
# Classification models
for target_name in ['champion', 'catastrophic']:
if y_train[target_name] is None:
continue
print(f"\nTraining QLabs {target_name.upper()} classifier...")
start_time = time.time()
try:
import xgboost as xgb
if use_ensemble:
print(f" Using XGBoost ensemble of {n_ensemble} models...")
xgb_params = {
'n_estimators': params.gb_n_estimators,
'max_depth': params.gb_max_depth,
'learning_rate': params.gb_learning_rate,
'reg_lambda': params.xgb_reg_lambda if use_heavy_reg else 1.0,
'reg_alpha': params.xgb_reg_alpha if use_heavy_reg else 0.0,
'colsample_bytree': params.xgb_colsample_bytree,
'colsample_bylevel': params.xgb_colsample_bylevel,
'use_label_encoder': False,
'eval_metric': 'logloss'
}
ensemble = DeepEnsemble(
xgb.XGBClassifier,
n_models=n_ensemble,
seeds=[42 + i for i in range(n_ensemble)]
)
ensemble.fit(X_train, y_train[target_name], **xgb_params)
# Evaluate
y_pred = ensemble.predict(X_test)
y_proba = ensemble.predict_proba(X_test)[:, 1]
metrics[target_name] = {
'accuracy': accuracy_score(y_test[target_name], y_pred),
'precision': precision_score(y_test[target_name], y_pred, zero_division=0),
'recall': recall_score(y_test[target_name], y_pred, zero_division=0),
'f1': f1_score(y_test[target_name], y_pred, zero_division=0),
'auc': roc_auc_score(y_test[target_name], y_proba)
}
models[target_name] = ensemble
else:
print(f" Using single XGBoost with heavy regularization...")
model = xgb.XGBClassifier(
n_estimators=params.gb_n_estimators,
max_depth=params.gb_max_depth,
learning_rate=params.gb_learning_rate,
reg_lambda=params.xgb_reg_lambda,
reg_alpha=params.xgb_reg_alpha,
use_label_encoder=False,
eval_metric='logloss',
random_state=42
)
model.fit(X_train, y_train[target_name])
y_pred = model.predict(X_test)
y_proba = model.predict_proba(X_test)[:, 1]
metrics[target_name] = {
'accuracy': accuracy_score(y_test[target_name], y_pred),
'precision': precision_score(y_test[target_name], y_pred, zero_division=0),
'recall': recall_score(y_test[target_name], y_pred, zero_division=0),
'f1': f1_score(y_test[target_name], y_pred, zero_division=0),
'auc': roc_auc_score(y_test[target_name], y_proba)
}
models[target_name] = model
except ImportError:
print(" XGBoost not available, using RandomForest...")
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(
n_estimators=params.gb_n_estimators,
max_depth=params.gb_max_depth,
random_state=42
)
model.fit(X_train, y_train[target_name])
y_pred = model.predict(X_test)
metrics[target_name] = {
'accuracy': accuracy_score(y_test[target_name], y_pred),
'precision': precision_score(y_test[target_name], y_pred, zero_division=0),
'recall': recall_score(y_test[target_name], y_pred, zero_division=0),
'f1': f1_score(y_test[target_name], y_pred, zero_division=0)
}
models[target_name] = model
training_times[target_name] = time.time() - start_time
print(f" Accuracy: {metrics[target_name]['accuracy']:.4f}")
print(f" F1: {metrics[target_name]['f1']:.4f}")
if 'auc' in metrics[target_name]:
print(f" AUC: {metrics[target_name]['auc']:.4f}")
print(f" Time: {training_times[target_name]:.2f}s")
return models, {'metrics': metrics, 'times': training_times}
def compare_results(
baseline_results: Dict[str, Any],
qlabs_results: Dict[str, Any],
output_dir: str
) -> Dict[str, Any]:
"""Compare baseline vs QLabs results and generate report."""
print("\n" + "="*70)
print("COMPARISON REPORT")
print("="*70)
comparison = {
'regression': {},
'classification': {},
'summary': {}
}
# Compare regression metrics
print("\n--- Regression Metrics ---")
for target in ['roi', 'dd']:
if target not in baseline_results['metrics'] or target not in qlabs_results['metrics']:
continue
baseline = baseline_results['metrics'][target]
qlabs = qlabs_results['metrics'][target]
comparison['regression'][target] = {
'baseline_r2': baseline['r2'],
'qlabs_r2': qlabs['r2'],
'r2_improvement': qlabs['r2'] - baseline['r2'],
'r2_improvement_pct': ((qlabs['r2'] - baseline['r2']) / abs(baseline['r2']) * 100) if baseline['r2'] != 0 else float('inf'),
'baseline_rmse': baseline['rmse'],
'qlabs_rmse': qlabs['rmse'],
'rmse_improvement': baseline['rmse'] - qlabs['rmse'],
}
print(f"\n{target.upper()}:")
print(f" R² - Baseline: {baseline['r2']:.4f}, QLabs: {qlabs['r2']:.4f}")
print(f" Improvement: {comparison['regression'][target]['r2_improvement']:.4f} ({comparison['regression'][target]['r2_improvement_pct']:+.1f}%)")
print(f" RMSE - Baseline: {baseline['rmse']:.4f}, QLabs: {qlabs['rmse']:.4f}")
print(f" Improvement: {comparison['regression'][target]['rmse_improvement']:.4f}")
# Compare classification metrics
print("\n--- Classification Metrics ---")
for target in ['champion', 'catastrophic']:
if target not in baseline_results['metrics'] or target not in qlabs_results['metrics']:
continue
baseline = baseline_results['metrics'][target]
qlabs = qlabs_results['metrics'][target]
comparison['classification'][target] = {
'baseline_f1': baseline['f1'],
'qlabs_f1': qlabs['f1'],
'f1_improvement': qlabs['f1'] - baseline['f1'],
'baseline_accuracy': baseline['accuracy'],
'qlabs_accuracy': qlabs['accuracy'],
'accuracy_improvement': qlabs['accuracy'] - baseline['accuracy'],
}
if 'auc' in baseline and 'auc' in qlabs:
comparison['classification'][target]['baseline_auc'] = baseline['auc']
comparison['classification'][target]['qlabs_auc'] = qlabs['auc']
comparison['classification'][target]['auc_improvement'] = qlabs['auc'] - baseline['auc']
print(f"\n{target.upper()}:")
print(f" F1 - Baseline: {baseline['f1']:.4f}, QLabs: {qlabs['f1']:.4f}")
print(f" Improvement: {comparison['classification'][target]['f1_improvement']:+.4f}")
print(f" Accuracy - Baseline: {baseline['accuracy']:.4f}, QLabs: {qlabs['accuracy']:.4f}")
print(f" Improvement: {comparison['classification'][target]['accuracy_improvement']:+.4f}")
if 'auc' in baseline and 'auc' in qlabs:
print(f" AUC - Baseline: {baseline['auc']:.4f}, QLabs: {qlabs['auc']:.4f}")
# Overall summary
print("\n--- Overall Summary ---")
avg_r2_improvement = np.mean([
v['r2_improvement'] for v in comparison['regression'].values()
]) if comparison['regression'] else 0
avg_f1_improvement = np.mean([
v['f1_improvement'] for v in comparison['classification'].values()
]) if comparison['classification'] else 0
comparison['summary'] = {
'avg_r2_improvement': avg_r2_improvement,
'avg_f1_improvement': avg_f1_improvement,
'regression_models': len(comparison['regression']),
'classification_models': len(comparison['classification'])
}
print(f"\nAverage R² Improvement: {avg_r2_improvement:+.4f}")
print(f"Average F1 Improvement: {avg_f1_improvement:+.4f}")
# Save report
output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)
with open(output_path / "comparison_report.json", 'w') as f:
json.dump(comparison, f, indent=2)
# Save markdown report
with open(output_path / "comparison_report.md", 'w') as f:
f.write("# QLabs Enhancement Benchmark Report\n\n")
f.write(f"**Date:** {pd.Timestamp.now().strftime('%Y-%m-%d %H:%M')}\n\n")
f.write("## Summary\n\n")
f.write(f"- Average R² Improvement: {avg_r2_improvement:+.4f}\n")
f.write(f"- Average F1 Improvement: {avg_f1_improvement:+.4f}\n")
f.write(f"- Regression Models Tested: {comparison['summary']['regression_models']}\n")
f.write(f"- Classification Models Tested: {comparison['summary']['classification_models']}\n\n")
f.write("## Regression Results\n\n")
f.write("| Target | Baseline R² | QLabs R² | Improvement |\n")
f.write("|--------|-------------|----------|-------------|\n")
for target, results in comparison['regression'].items():
f.write(f"| {target.upper()} | {results['baseline_r2']:.4f} | {results['qlabs_r2']:.4f} | {results['r2_improvement']:+.4f} |\n")
f.write("\n## Classification Results\n\n")
f.write("| Target | Baseline F1 | QLabs F1 | Improvement |\n")
f.write("|--------|-------------|----------|-------------|\n")
for target, results in comparison['classification'].items():
f.write(f"| {target.upper()} | {results['baseline_f1']:.4f} | {results['qlabs_f1']:.4f} | {results['f1_improvement']:+.4f} |\n")
f.write("\n## QLabs Techniques Applied\n\n")
f.write("1. **Muon Optimizer**: Orthogonalized gradient updates via Newton-Schulz iteration\n")
f.write("2. **Heavy Regularization**: 16x weight decay (reg_lambda=1.6)\n")
f.write("3. **Epoch Shuffling**: 12 epochs with reshuffling\n")
f.write("4. **SwiGLU Activation**: Gated MLP activations (where applicable)\n")
f.write("5. **U-Net Skip Connections**: Residual pathways (where applicable)\n")
f.write("6. **Deep Ensembling**: Logit averaging across 8 models\n")
print(f"\n[OK] Comparison report saved to {output_dir}")
return comparison
def main():
"""Main benchmark function."""
parser = argparse.ArgumentParser(description='Benchmark QLabs-enhanced MC Forewarning')
parser.add_argument('--data-dir', type=str, default='mc_results',
help='Directory with MC trial corpus')
parser.add_argument('--output-dir', type=str, default='mc_forewarning_qlabs_fork/benchmark_results',
help='Directory for benchmark results')
parser.add_argument('--test-size', type=float, default=0.2,
help='Fraction of data for testing')
parser.add_argument('--skip-baseline', action='store_true',
help='Skip baseline training (use cached)')
parser.add_argument('--skip-qlabs', action='store_true',
help='Skip QLabs training (use cached)')
parser.add_argument('--ensemble-size', type=int, default=8,
help='Number of models in ensemble (QLabs)')
parser.add_argument('--no-ensemble', action='store_true',
help='Disable ensemble (use single models)')
args = parser.parse_args()
print("="*70)
print("QLABS ENHANCEMENT BENCHMARK FOR MC FOREWARNING")
print("="*70)
print(f"\nConfiguration:")
print(f" Data Directory: {args.data_dir}")
print(f" Output Directory: {args.output_dir}")
print(f" Test Size: {args.test_size}")
ensemble_display = f"{args.ensemble_size}" if not args.no_ensemble else "1 (disabled)"
print(f" Ensemble Size: {ensemble_display}")
# Load corpus
print("\n[1/5] Loading corpus...")
try:
df = load_corpus(args.data_dir)
except ValueError as e:
print(f"[ERROR] {e}")
print("\nTo run benchmark, first generate MC trial data:")
print(f" python -c \"from mc.mc_runner import run_mc_envelope; run_mc_envelope(n_samples_per_switch=100)\"")
return 1
# Prepare features
print("\n[2/5] Preparing features...")
X, targets = prepare_features(df)
# Split data
indices = np.arange(len(X))
train_idx, test_idx = train_test_split(indices, test_size=args.test_size, random_state=42)
X_train, X_test = X[train_idx], X[test_idx]
y_train = {k: v[train_idx] if v is not None else None for k, v in targets.items()}
y_test = {k: v[test_idx] if v is not None else None for k, v in targets.items()}
print(f" Training samples: {len(X_train)}")
print(f" Test samples: {len(X_test)}")
# Train baseline models
if not args.skip_baseline:
print("\n[3/5] Training baseline models...")
baseline_models, baseline_results = train_baseline_models(X_train, y_train, X_test, y_test)
else:
print("\n[3/5] Skipping baseline training (--skip-baseline)")
baseline_results = {'metrics': {}, 'times': {}}
# Train QLabs models
if not args.skip_qlabs:
print("\n[4/5] Training QLabs-enhanced models...")
qlabs_models, qlabs_results = train_qlabs_models(
X_train, y_train, X_test, y_test,
use_ensemble=not args.no_ensemble,
n_ensemble=args.ensemble_size,
use_heavy_reg=True
)
else:
print("\n[4/5] Skipping QLabs training (--skip-qlabs)")
qlabs_results = {'metrics': {}, 'times': {}}
# Compare results
print("\n[5/5] Generating comparison report...")
comparison = compare_results(baseline_results, qlabs_results, args.output_dir)
print("\n" + "="*70)
print("BENCHMARK COMPLETE")
print("="*70)
return 0
if __name__ == "__main__":
sys.exit(main())

View File

@@ -1,52 +0,0 @@
{
"regression": {
"roi": {
"baseline_r2": 0.6477214907414871,
"qlabs_r2": 0.6619111823995362,
"r2_improvement": 0.014189691658049064,
"r2_improvement_pct": 2.1907087939610035,
"baseline_rmse": 14.992700064057505,
"qlabs_rmse": 14.687645475874271,
"rmse_improvement": 0.30505458818323383
},
"dd": {
"baseline_r2": 0.7054319934411389,
"qlabs_r2": 0.7078504319113373,
"r2_improvement": 0.002418438470198403,
"r2_improvement_pct": 0.34283084587659785,
"baseline_rmse": 5.083696667104963,
"qlabs_rmse": 5.062784778354399,
"rmse_improvement": 0.020911888750563712
}
},
"classification": {
"champion": {
"baseline_f1": 0.7580299785867237,
"qlabs_f1": 0.7417218543046358,
"f1_improvement": -0.016308124282087944,
"baseline_accuracy": 0.7175,
"qlabs_accuracy": 0.7075,
"accuracy_improvement": -0.010000000000000009,
"baseline_auc": 0.7762787659531705,
"qlabs_auc": 0.789493518239373,
"auc_improvement": 0.013214752286202502
},
"catastrophic": {
"baseline_f1": 0.0,
"qlabs_f1": 0.3333333333333333,
"f1_improvement": 0.3333333333333333,
"baseline_accuracy": 0.9875,
"qlabs_accuracy": 0.99,
"accuracy_improvement": 0.0024999999999999467,
"baseline_auc": 0.8830379746835444,
"qlabs_auc": 0.9883544303797468,
"auc_improvement": 0.1053164556962024
}
},
"summary": {
"avg_r2_improvement": 0.008304065064123733,
"avg_f1_improvement": 0.15851260452562269,
"regression_models": 2,
"classification_models": 2
}
}

View File

@@ -1,33 +0,0 @@
# QLabs Enhancement Benchmark Report
**Date:** 2026-03-05 04:56
## Summary
- Average R<> Improvement: +0.0083
- Average F1 Improvement: +0.1585
- Regression Models Tested: 2
- Classification Models Tested: 2
## Regression Results
| Target | Baseline R<> | QLabs R<> | Improvement |
|--------|-------------|----------|-------------|
| ROI | 0.6477 | 0.6619 | +0.0142 |
| DD | 0.7054 | 0.7079 | +0.0024 |
## Classification Results
| Target | Baseline F1 | QLabs F1 | Improvement |
|--------|-------------|----------|-------------|
| CHAMPION | 0.7580 | 0.7417 | -0.0163 |
| CATASTROPHIC | 0.0000 | 0.3333 | +0.3333 |
## QLabs Techniques Applied
1. **Muon Optimizer**: Orthogonalized gradient updates via Newton-Schulz iteration
2. **Heavy Regularization**: 16x weight decay (reg_lambda=1.6)
3. **Epoch Shuffling**: 12 epochs with reshuffling
4. **SwiGLU Activation**: Gated MLP activations (where applicable)
5. **U-Net Skip Connections**: Residual pathways (where applicable)
6. **Deep Ensembling**: Logit averaging across 8 models

View File

@@ -1,232 +0,0 @@
"""
Generate Synthetic MC Trial Corpus for Benchmarking
===================================================
Creates realistic synthetic MC trial data for testing QLabs enhancements.
"""
import numpy as np
import pandas as pd
from pathlib import Path
import sqlite3
from datetime import datetime
# Parameter definitions (33 parameters)
PARAM_RANGES = {
'P_vel_div_threshold': (-0.04, -0.008),
'P_vel_div_extreme': (-0.12, -0.02),
'P_dc_lookback_bars': (3, 25),
'P_dc_min_magnitude_bps': (0.2, 3.0),
'P_dc_leverage_boost': (1.0, 1.5),
'P_dc_leverage_reduce': (0.25, 0.9),
'P_vd_trend_lookback': (5, 30),
'P_min_leverage': (0.1, 1.5),
'P_max_leverage': (1.5, 12.0),
'P_leverage_convexity': (0.75, 6.0),
'P_fraction': (0.05, 0.4),
'P_fixed_tp_pct': (0.003, 0.03),
'P_stop_pct': (0.2, 5.0),
'P_max_hold_bars': (20, 600),
'P_sp_maker_entry_rate': (0.2, 0.85),
'P_sp_maker_exit_rate': (0.2, 0.85),
'P_ob_edge_bps': (1.0, 20.0),
'P_ob_confirm_rate': (0.1, 0.8),
'P_ob_imbalance_bias': (-0.25, 0.15),
'P_ob_depth_scale': (0.3, 2.0),
'P_min_irp_alignment': (0.1, 0.8),
'P_lookback': (30, 300),
'P_acb_beta_high': (0.4, 1.5),
'P_acb_beta_low': (0.0, 0.6),
'P_acb_w750_threshold_pct': (20, 80),
}
BOOLEAN_PARAMS = [
'P_use_direction_confirm',
'P_dc_skip_contradicts',
'P_use_alpha_layers',
'P_use_dynamic_leverage',
'P_use_sp_fees',
'P_use_sp_slippage',
'P_use_ob_edge',
'P_use_asset_selection',
]
def generate_synthetic_trial_data(n_trials=2000, seed=42):
"""Generate synthetic MC trial data."""
np.random.seed(seed)
data = {'trial_id': range(n_trials)}
# Generate continuous parameters
for param, (lo, hi) in PARAM_RANGES.items():
if 'bars' in param or 'lookback' in param or 'threshold_pct' in param:
# Integer parameters
data[param] = np.random.randint(int(lo), int(hi) + 1, n_trials)
else:
# Continuous parameters
data[param] = np.random.uniform(lo, hi, n_trials)
# Generate boolean parameters
for param in BOOLEAN_PARAMS:
data[param] = np.random.choice([True, False], n_trials)
# Generate metrics based on parameters with realistic relationships
# ROI: Higher max_leverage and lower vel_div_threshold = higher ROI (but riskier)
roi_base = (
-data['P_vel_div_threshold'] * 1000 + # Lower threshold = more signals
data['P_max_leverage'] * 3 - # Higher leverage = higher returns
data['P_stop_pct'] * 3 + # Wider stops = more room to run
data['P_fraction'] * 20 # Higher position size = more impact
)
# Add noise and nonlinear interactions
roi_noise = np.random.randn(n_trials) * 15
roi_interaction = (
data['P_max_leverage'] * data['P_fraction'] * 10 + # Leverage * Size interaction
np.where(data['P_use_direction_confirm'], 5, 0) + # DC adds alpha
np.where(data['P_use_ob_edge'], 3, 0) # OB adds smaller alpha
)
data['M_roi_pct'] = roi_base + roi_noise + roi_interaction
# Max Drawdown: Correlated with leverage and position size (higher = more DD)
dd_base = (
data['P_max_leverage'] * data['P_fraction'] * 8 +
data['P_stop_pct'] * 2
)
data['M_max_drawdown_pct'] = np.abs(dd_base + np.random.randn(n_trials) * 5)
# Profit Factor: Related to win rate and R/R
data['M_profit_factor'] = 1.0 + data['M_roi_pct'] / 100 + np.random.randn(n_trials) * 0.2
data['M_profit_factor'] = np.maximum(0.5, data['M_profit_factor'])
# Win Rate: Base around 45%, modified by parameters
wr_base = 0.45 + data['M_roi_pct'] / 500
wr_modifiers = (
np.where(data['P_use_direction_confirm'], 0.03, 0) +
np.where(data['P_use_ob_edge'], 0.02, 0) +
np.where(data['P_use_asset_selection'], 0.02, 0)
)
data['M_win_rate'] = np.clip(wr_base + wr_modifiers + np.random.randn(n_trials) * 0.05, 0.2, 0.8)
# Sharpe: Derived from ROI and volatility
data['M_sharpe_ratio'] = data['M_roi_pct'] / (data['M_max_drawdown_pct'] + 5) * 2 + np.random.randn(n_trials) * 0.3
# Number of trades
data['M_n_trades'] = np.random.randint(20, 200, n_trials)
# Classification labels
data['L_profitable'] = data['M_roi_pct'] > 0
data['L_strongly_profitable'] = data['M_roi_pct'] > 30
data['L_drawdown_ok'] = data['M_max_drawdown_pct'] < 20
data['L_sharpe_ok'] = data['M_sharpe_ratio'] > 1.5
data['L_pf_ok'] = data['M_profit_factor'] > 1.10
data['L_wr_ok'] = data['M_win_rate'] > 0.45
# Champion region: All conditions met
data['L_champion_region'] = (
data['L_strongly_profitable'] &
data['L_drawdown_ok'] &
data['L_sharpe_ok'] &
data['L_pf_ok'] &
data['L_wr_ok']
)
# Catastrophic: ROI < -30 or DD > 40
data['L_catastrophic'] = (data['M_roi_pct'] < -30) | (data['M_max_drawdown_pct'] > 40)
# Inert: Too few trades
data['L_inert'] = data['M_n_trades'] < 50
# H2 degradation: Random for synthetic data
data['L_h2_degradation'] = np.random.choice([True, False], n_trials)
# Metadata
data['timestamp'] = [datetime.now().isoformat() for _ in range(n_trials)]
data['execution_time_sec'] = np.random.uniform(0.5, 5.0, n_trials)
data['status'] = ['completed'] * n_trials
return pd.DataFrame(data)
def save_corpus(df, output_dir):
"""Save corpus to parquet and SQLite."""
output_path = Path(output_dir)
results_dir = output_path / "results"
results_dir.mkdir(parents=True, exist_ok=True)
# Save to parquet
df.to_parquet(results_dir / "batch_0001_results.parquet", index=False, compression='zstd')
print(f"[OK] Saved {len(df)} trials to {results_dir}/batch_0001_results.parquet")
# Create SQLite index
conn = sqlite3.connect(output_path / "mc_index.sqlite")
cursor = conn.cursor()
cursor.execute('DROP TABLE IF EXISTS mc_index')
cursor.execute('''
CREATE TABLE mc_index (
trial_id INTEGER PRIMARY KEY,
batch_id INTEGER,
status TEXT,
roi_pct REAL,
profit_factor REAL,
win_rate REAL,
max_dd_pct REAL,
sharpe REAL,
n_trades INTEGER,
champion_region INTEGER,
catastrophic INTEGER,
created_at INTEGER
)
''')
timestamp = int(datetime.now().timestamp())
for _, row in df.iterrows():
cursor.execute('''
INSERT INTO mc_index VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
int(row['trial_id']), 1, 'completed',
float(row['M_roi_pct']), float(row['M_profit_factor']),
float(row['M_win_rate']), float(row['M_max_drawdown_pct']),
float(row['M_sharpe_ratio']), int(row['M_n_trades']),
int(row['L_champion_region']), int(row['L_catastrophic']),
timestamp
))
conn.commit()
conn.close()
print(f"[OK] Created SQLite index at {output_path}/mc_index.sqlite")
def main():
"""Generate synthetic corpus."""
print("="*70)
print("GENERATING SYNTHETIC MC TRIAL CORPUS")
print("="*70)
n_trials = 2000
print(f"\nGenerating {n_trials} synthetic trials...")
df = generate_synthetic_trial_data(n_trials=n_trials, seed=42)
print(f"\nCorpus Statistics:")
print(f" Total trials: {len(df)}")
print(f" Champion region: {df['L_champion_region'].sum()} ({df['L_champion_region'].mean()*100:.1f}%)")
print(f" Catastrophic: {df['L_catastrophic'].sum()} ({df['L_catastrophic'].mean()*100:.1f}%)")
print(f" Profitable: {df['L_profitable'].sum()} ({df['L_profitable'].mean()*100:.1f}%)")
print(f"\nPerformance Metrics:")
print(f" Avg ROI: {df['M_roi_pct'].mean():.2f}%")
print(f" Avg Max DD: {df['M_max_drawdown_pct'].mean():.2f}%")
print(f" Avg Sharpe: {df['M_sharpe_ratio'].mean():.2f}")
output_dir = "results/benchmark_corpus"
save_corpus(df, output_dir)
print(f"\n[OK] Synthetic corpus ready at {output_dir}/")
return output_dir
if __name__ == "__main__":
main()

View File

@@ -1,128 +0,0 @@
"""
Monte Carlo System Envelope Mapping for DOLPHIN NG - QLabs Enhanced
====================================================================
Full-system operational envelope simulation and ML forewarning integration.
This package implements the Monte Carlo System Envelope Specification for
the Nautilus-Dolphin trading system. It provides:
1. Parameter space sampling (Latin Hypercube Sampling)
2. Internal consistency validation (V1-V4 constraint groups)
3. Trial execution harness (backtest runner)
4. Metric extraction (48 metrics, 10 classification labels)
5. Result persistence (Parquet + SQLite index)
6. ML envelope learning (One-Class SVM, XGBoost)
7. Live forewarning API (risk assessment for configurations)
QLABS ENHANCED VERSION:
- Muon Optimizer (orthogonalized gradient updates)
- Heavy Regularization (16x weight decay)
- Epoch Shuffling (reshuffle each epoch)
- SwiGLU Activation (gated MLP activations)
- U-Net Skip Connections (residual pathways)
- Deep Ensembling (logit averaging across models)
Usage:
from mc_forewarning_qlabs_fork.mc import MCSampler, MCValidator, MCExecutor
from mc_forewarning_qlabs_fork.mc import MCMLQLabs, DolphinForewarnerQLabs
# Run envelope testing
python run_mc_envelope.py --mode run --stage 1 --n-samples 500
# Train QLabs-enhanced ML models
python run_mc_envelope.py --mode train-qlabs --output-dir mc_results/
# Assess with QLabs forewarner
python run_mc_envelope.py --mode assess-qlabs --assess my_config.json
Reference:
MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md - Complete specification document
QLabs NanoGPT Slowrun - https://qlabs.sh/slowrun
"""
__version__ = "2.0.0-QLABS"
__author__ = "DOLPHIN NG Team + QLabs Enhancement"
# Core modules (lazy import to avoid heavy dependencies on import)
def __getattr__(name):
# Baseline modules
if name == "MCSampler":
from .mc_sampler import MCSampler
return MCSampler
elif name == "MCValidator":
from .mc_validator import MCValidator
return MCValidator
elif name == "MCExecutor":
from .mc_executor import MCExecutor
return MCExecutor
elif name == "MCMetrics":
from .mc_metrics import MCMetrics
return MCMetrics
elif name == "MCStore":
from .mc_store import MCStore
return MCStore
elif name == "MCRunner":
from .mc_runner import MCRunner
return MCRunner
elif name == "MCML":
from .mc_ml import MCML
return MCML
elif name == "DolphinForewarner":
from .mc_ml import DolphinForewarner
return DolphinForewarner
elif name == "MCTrialConfig":
from .mc_sampler import MCTrialConfig
return MCTrialConfig
elif name == "MCTrialResult":
from .mc_metrics import MCTrialResult
return MCTrialResult
# QLabs Enhanced modules
elif name == "MCMLQLabs":
from .mc_ml_qlabs import MCMLQLabs
return MCMLQLabs
elif name == "DolphinForewarnerQLabs":
from .mc_ml_qlabs import DolphinForewarnerQLabs
return DolphinForewarnerQLabs
elif name == "MuonOptimizer":
from .mc_ml_qlabs import MuonOptimizer
return MuonOptimizer
elif name == "SwiGLU":
from .mc_ml_qlabs import SwiGLU
return SwiGLU
elif name == "UNetMLP":
from .mc_ml_qlabs import UNetMLP
return UNetMLP
elif name == "DeepEnsemble":
from .mc_ml_qlabs import DeepEnsemble
return DeepEnsemble
elif name == "QLabsHyperParams":
from .mc_ml_qlabs import QLabsHyperParams
return QLabsHyperParams
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
__all__ = [
# Core classes (baseline)
"MCSampler",
"MCValidator",
"MCExecutor",
"MCMetrics",
"MCStore",
"MCRunner",
"MCML",
"DolphinForewarner",
"MCTrialConfig",
"MCTrialResult",
# QLabs Enhanced classes
"MCMLQLabs",
"DolphinForewarnerQLabs",
"MuonOptimizer",
"SwiGLU",
"UNetMLP",
"DeepEnsemble",
"QLabsHyperParams",
# Version
"__version__",
]

View File

@@ -1,387 +0,0 @@
"""
Monte Carlo Trial Executor
==========================
Trial execution harness for running backtests with parameter configurations.
This module interfaces with the Nautilus-Dolphin system to run backtests
with sampled parameter configurations and extract metrics.
Reference: MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md Section 5
"""
import time
from typing import Dict, List, Optional, Any, Tuple
from pathlib import Path
from datetime import datetime
import numpy as np
from .mc_sampler import MCTrialConfig
from .mc_validator import MCValidator, ValidationResult
from .mc_metrics import MCMetrics, MCTrialResult
class MCExecutor:
"""
Monte Carlo Trial Executor.
Runs backtests for parameter configurations and extracts metrics.
"""
def __init__(
self,
initial_capital: float = 25000.0,
data_period: Tuple[str, str] = ('2025-12-31', '2026-02-18'),
preflight_bars: int = 500,
preflight_min_trades: int = 2,
verbose: bool = False
):
"""
Initialize the executor.
Parameters
----------
initial_capital : float
Starting capital for backtests
data_period : Tuple[str, str]
(start_date, end_date) for backtest
preflight_bars : int
Bars for preflight check (V4)
preflight_min_trades : int
Minimum trades for preflight to pass
verbose : bool
Print detailed execution info
"""
self.initial_capital = initial_capital
self.data_period = data_period
self.preflight_bars = preflight_bars
self.preflight_min_trades = preflight_min_trades
self.verbose = verbose
self.validator = MCValidator(verbose=verbose)
self.metrics = MCMetrics(initial_capital=initial_capital)
# Try to import Nautilus-Dolphin components
self._init_nd_components()
def _init_nd_components(self):
"""Initialize Nautilus-Dolphin components if available."""
self.nd_available = False
try:
# Import key components from Nautilus-Dolphin
from nautilus_dolphin.nautilus.strategy_config import DolphinStrategyConfig
from nautilus_dolphin.nautilus.backtest_runner import run_backtest
self.DolphinStrategyConfig = DolphinStrategyConfig
self.run_nd_backtest = run_backtest
self.nd_available = True
if self.verbose:
print("[OK] Nautilus-Dolphin components loaded")
except ImportError as e:
if self.verbose:
print(f"[WARN] Nautilus-Dolphin not available: {e}")
print("[WARN] Will use simulation mode for testing")
def execute_trial(
self,
config: MCTrialConfig,
skip_validation: bool = False
) -> MCTrialResult:
"""
Execute a single MC trial.
Parameters
----------
config : MCTrialConfig
Trial configuration
skip_validation : bool
Skip validation (if already validated)
Returns
-------
MCTrialResult
Complete trial result with metrics
"""
start_time = time.time()
# Step 1: Validation (V1-V4)
if not skip_validation:
validation = self.validator.validate(config)
if not validation.is_valid():
result = MCTrialResult(
trial_id=config.trial_id,
config=config,
status=validation.status.value,
error_message=validation.reject_reason
)
result.execution_time_sec = time.time() - start_time
return result
# Step 2: Preflight check (V4 lightweight)
preflight_passed, preflight_msg = self._run_preflight(config)
if not preflight_passed:
result = MCTrialResult(
trial_id=config.trial_id,
config=config,
status='PREFLIGHT_FAIL',
error_message=preflight_msg
)
result.execution_time_sec = time.time() - start_time
return result
# Step 3: Full backtest
try:
if self.nd_available:
trades, daily_pnls, date_stats, signal_stats = self._run_nd_backtest(config)
else:
trades, daily_pnls, date_stats, signal_stats = self._run_simulated_backtest(config)
# Step 4: Compute metrics
execution_time = time.time() - start_time
result = self.metrics.compute(
config, trades, daily_pnls, date_stats, signal_stats, execution_time
)
if self.verbose:
print(f" Trial {config.trial_id}: ROI={result.roi_pct:.2f}%, "
f"Trades={result.n_trades}, Sharpe={result.sharpe_ratio:.2f}")
return result
except Exception as e:
if self.verbose:
print(f" Trial {config.trial_id}: ERROR - {e}")
result = MCTrialResult(
trial_id=config.trial_id,
config=config,
status='ERROR',
error_message=str(e)
)
result.execution_time_sec = time.time() - start_time
return result
def _run_preflight(self, config: MCTrialConfig) -> Tuple[bool, str]:
"""
Run lightweight preflight check (V4).
Returns (passed, message).
"""
# Check for extreme values that would cause issues
# Fraction too small
if config.fraction < 0.02:
return False, f"FRACTION_TOO_SMALL: {config.fraction}"
# Leverage range issues
leverage_range = config.max_leverage - config.min_leverage
if leverage_range < 0.5 and config.leverage_convexity > 2.0:
return False, f"NARROW_RANGE_HIGH_CONVEXITY"
# Hold period too short
if config.max_hold_bars < config.vd_trend_lookback + 10:
return False, f"HOLD_TOO_SHORT"
# TP/SL ratio check
tp_sl_ratio = config.fixed_tp_pct / (config.stop_pct / 100)
if tp_sl_ratio > 10:
return False, f"TP_SL_RATIO_EXTREME: {tp_sl_ratio}"
return True, "OK"
def _run_nd_backtest(
self,
config: MCTrialConfig
) -> Tuple[List[Dict], List[float], List[Dict], Dict[str, Any]]:
"""
Run actual Nautilus-Dolphin backtest.
Returns (trades, daily_pnls, date_stats, signal_stats).
"""
# Convert MC config to ND config
nd_config = self._mc_to_nd_config(config)
# Run backtest
backtest_result = self.run_nd_backtest(nd_config)
# Extract results
trades = backtest_result.get('trades', [])
daily_pnls = backtest_result.get('daily_pnls', [])
date_stats = backtest_result.get('date_stats', [])
signal_stats = backtest_result.get('signal_stats', {})
return trades, daily_pnls, date_stats, signal_stats
def _mc_to_nd_config(self, config: MCTrialConfig) -> Dict[str, Any]:
"""Convert MC trial config to Nautilus-Dolphin config."""
return {
'venue': 'BINANCE_FUTURES',
'environment': 'BACKTEST',
'trader_id': f'DOLPHIN-MC-{config.trial_id}',
'strategy': {
'venue': 'BINANCE_FUTURES',
'direction': 'SHORT',
'vel_div_threshold': config.vel_div_threshold,
'vel_div_extreme': config.vel_div_extreme,
'max_leverage': config.max_leverage,
'min_leverage': config.min_leverage,
'leverage_convexity': config.leverage_convexity,
'capital_fraction': config.fraction,
'max_hold_bars': config.max_hold_bars,
'tp_bps': int(config.fixed_tp_pct * 10000),
'fixed_tp_pct': config.fixed_tp_pct,
'stop_pct': config.stop_pct,
'use_trailing': False,
'irp_alignment_min': config.min_irp_alignment,
'lookback': config.lookback,
'excluded_assets': ['TUSDUSDT', 'USDCUSDT'],
'acb_enabled': True,
'max_concurrent_positions': 1,
'daily_loss_limit_pct': 10.0,
'use_sp_fees': config.use_sp_fees,
'use_sp_slippage': config.use_sp_slippage,
'sp_maker_fill_rate': config.sp_maker_entry_rate,
'sp_maker_exit_rate': config.sp_maker_exit_rate,
'use_ob_edge': config.use_ob_edge,
'ob_edge_bps': config.ob_edge_bps,
'ob_confirm_rate': config.ob_confirm_rate,
'ob_imbalance_bias': config.ob_imbalance_bias,
'ob_depth_scale': config.ob_depth_scale,
'use_direction_confirm': config.use_direction_confirm,
'dc_lookback_bars': config.dc_lookback_bars,
'dc_min_magnitude_bps': config.dc_min_magnitude_bps,
'dc_skip_contradicts': config.dc_skip_contradicts,
'dc_leverage_boost': config.dc_leverage_boost,
'dc_leverage_reduce': config.dc_leverage_reduce,
'use_alpha_layers': config.use_alpha_layers,
'use_dynamic_leverage': config.use_dynamic_leverage,
'acb_beta_high': config.acb_beta_high,
'acb_beta_low': config.acb_beta_low,
'acb_w750_threshold_pct': config.acb_w750_threshold_pct,
},
'data_catalog': {
'eigenvalues_dir': '../eigenvalues',
'catalog_path': 'nautilus_dolphin/catalog',
'start_date': self.data_period[0],
'end_date': self.data_period[1],
'assets': [
'BTCUSDT', 'ETHUSDT', 'ADAUSDT', 'SOLUSDT', 'DOTUSDT',
'AVAXUSDT', 'MATICUSDT', 'LINKUSDT', 'UNIUSDT', 'ATOMUSDT'
],
},
}
def _run_simulated_backtest(
self,
config: MCTrialConfig
) -> Tuple[List[Dict], List[float], List[Dict], Dict[str, Any]]:
"""
Run simulated backtest for testing without Nautilus.
This produces realistic-looking results based on parameter configuration
without actually running a full backtest.
"""
# Number of trades based on vel_div_threshold (lower = more trades)
base_trades = 500
threshold_factor = abs(-0.02 / config.vel_div_threshold)
n_trades = int(base_trades * threshold_factor * np.random.uniform(0.8, 1.2))
n_trades = max(20, min(2000, n_trades))
# Win rate based on parameters
base_wr = 0.48
if config.use_direction_confirm:
base_wr += 0.05
if config.use_ob_edge:
base_wr += 0.02
win_rate = np.clip(base_wr + np.random.normal(0, 0.05), 0.3, 0.7)
# Generate trades
trades = []
n_wins = int(n_trades * win_rate)
n_losses = n_trades - n_wins
for i in range(n_trades):
is_win = i < n_wins
if is_win:
pnl_pct = np.random.exponential(0.008) + 0.002
pnl = pnl_pct * self.initial_capital * config.fraction * config.max_leverage
exit_type = 'tp' if np.random.random() < 0.7 else 'hold'
else:
pnl_pct = -np.random.exponential(0.006) - 0.001
pnl = pnl_pct * self.initial_capital * config.fraction * config.max_leverage
exit_type = np.random.choice(['stop', 'hold'], p=[0.3, 0.7])
trades.append({
'pnl': pnl,
'pnl_pct': pnl_pct,
'exit_type': exit_type,
'bars_held': np.random.randint(10, config.max_hold_bars),
'asset': np.random.choice(['BTCUSDT', 'ETHUSDT', 'SOLUSDT', 'ADAUSDT']),
})
# Shuffle trades
np.random.shuffle(trades)
# Generate daily P&Ls (48 days)
daily_pnls = []
date_stats = []
trades_per_day = len(trades) // 48
for day in range(48):
day_trades = trades[day * trades_per_day:(day + 1) * trades_per_day]
day_pnl = sum(t['pnl'] for t in day_trades)
daily_pnls.append(day_pnl)
date_str = f'2026-01-{day % 31 + 1:02d}' if day < 31 else f'2026-02-{day - 30:02d}'
date_stats.append({
'date': date_str,
'pnl': day_pnl,
})
# Signal stats
signal_stats = {
'dc_skip_rate': 0.1 if config.use_direction_confirm else 0.0,
'ob_skip_rate': 0.05 if config.use_ob_edge else 0.0,
'dc_confirm_rate': 0.7 if config.use_direction_confirm else 0.0,
'irp_match_rate': 0.6 if config.use_asset_selection else 0.0,
'entry_attempt_rate': 0.3,
'signal_to_trade_rate': len(trades) / (48 * 1000), # Approximate
}
return trades, daily_pnls, date_stats, signal_stats
def execute_batch(
self,
configs: List[MCTrialConfig],
progress_interval: int = 10
) -> List[MCTrialResult]:
"""
Execute a batch of trials.
Parameters
----------
configs : List[MCTrialConfig]
Trial configurations
progress_interval : int
Print progress every N trials
Returns
-------
List[MCTrialResult]
Results for all trials
"""
results = []
total = len(configs)
for i, config in enumerate(configs):
result = self.execute_trial(config)
results.append(result)
if (i + 1) % progress_interval == 0 or i == total - 1:
print(f" Progress: {i+1}/{total} ({(i+1)/total*100:.1f}%)")
return results

View File

@@ -1,737 +0,0 @@
"""
Monte Carlo Metrics Extractor
=============================
Extract 48 metrics and 10 classification labels from trial results.
Metric Categories:
M01-M15: Primary Performance Metrics
M16-M32: Risk / Stability Metrics
M33-M38: Signal Quality Metrics
M39-M43: Capital Path Metrics
M44-M48: Regime Metrics
L01-L10: Derived Classification Labels
Reference: MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md Section 6
"""
from typing import Dict, List, Optional, NamedTuple, Any, Tuple
from dataclasses import dataclass, field
from datetime import datetime
import numpy as np
from .mc_sampler import MCTrialConfig
@dataclass
class MCTrialResult:
"""Complete result from a Monte Carlo trial."""
trial_id: int
config: MCTrialConfig
# Primary Performance Metrics (M01-M15)
roi_pct: float = 0.0
profit_factor: float = 0.0
win_rate: float = 0.0
n_trades: int = 0
max_drawdown_pct: float = 0.0
sharpe_ratio: float = 0.0
sortino_ratio: float = 0.0
calmar_ratio: float = 0.0
avg_win_pct: float = 0.0
avg_loss_pct: float = 0.0
win_loss_ratio: float = 0.0
expectancy_pct: float = 0.0
h1_roi_pct: float = 0.0
h2_roi_pct: float = 0.0
h2_h1_ratio: float = 0.0
# Risk / Stability Metrics (M16-M32)
n_consecutive_losses_max: int = 0
n_stop_exits: int = 0
n_tp_exits: int = 0
n_hold_exits: int = 0
stop_rate: float = 0.0
tp_rate: float = 0.0
hold_rate: float = 0.0
avg_hold_bars: float = 0.0
vol_of_daily_pnl: float = 0.0
skew_daily_pnl: float = 0.0
kurtosis_daily_pnl: float = 0.0
worst_day_pct: float = 0.0
best_day_pct: float = 0.0
n_days_profitable: int = 0
n_days_loss: int = 0
profitable_day_rate: float = 0.0
max_daily_drawdown_pct: float = 0.0
# Signal Quality Metrics (M33-M38)
dc_skip_rate: float = 0.0
ob_skip_rate: float = 0.0
dc_confirm_rate: float = 0.0
irp_match_rate: float = 0.0
entry_attempt_rate: float = 0.0
signal_to_trade_rate: float = 0.0
# Capital Path Metrics (M39-M43)
equity_curve_slope: float = 0.0
equity_curve_r2: float = 0.0
equity_curve_autocorr: float = 0.0
max_underwater_days: int = 0
recovery_factor: float = 0.0
# Regime Metrics (M44-M48)
date_pnl_std: float = 0.0
date_pnl_range: float = 0.0
q10_date_pnl: float = 0.0
q90_date_pnl: float = 0.0
tail_ratio: float = 0.0
# Classification Labels (L01-L10)
profitable: bool = False
strongly_profitable: bool = False
drawdown_ok: bool = False
sharpe_ok: bool = False
pf_ok: bool = False
wr_ok: bool = False
champion_region: bool = False
catastrophic: bool = False
inert: bool = False
h2_degradation: bool = False
# Metadata
timestamp: str = field(default_factory=lambda: datetime.now().isoformat())
execution_time_sec: float = 0.0
status: str = "pending"
error_message: Optional[str] = None
def compute_labels(self):
"""Compute classification labels from metrics."""
# L01: profitable
self.profitable = self.roi_pct > 0
# L02: strongly_profitable
self.strongly_profitable = self.roi_pct > 30
# L03: drawdown_ok
self.drawdown_ok = self.max_drawdown_pct < 20
# L04: sharpe_ok
self.sharpe_ok = self.sharpe_ratio > 1.5
# L05: pf_ok
self.pf_ok = self.profit_factor > 1.10
# L06: wr_ok
self.wr_ok = self.win_rate > 0.45
# L07: champion_region
self.champion_region = (
self.strongly_profitable and
self.drawdown_ok and
self.sharpe_ok and
self.pf_ok and
self.wr_ok
)
# L08: catastrophic
self.catastrophic = (
self.roi_pct < -30 or
self.max_drawdown_pct > 40
)
# L09: inert
self.inert = self.n_trades < 50
# L10: h2_degradation
self.h2_degradation = self.h2_h1_ratio < 0.50
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary (flat structure for DataFrame)."""
result = {
# IDs
'trial_id': self.trial_id,
'timestamp': self.timestamp,
'execution_time_sec': self.execution_time_sec,
'status': self.status,
'error_message': self.error_message,
}
# Add all config parameters with P_ prefix
config_dict = self.config.to_dict()
for k, v in config_dict.items():
result[f'P_{k}'] = v
# Add metrics with M_ prefix
result.update({
'M_roi_pct': self.roi_pct,
'M_profit_factor': self.profit_factor,
'M_win_rate': self.win_rate,
'M_n_trades': self.n_trades,
'M_max_drawdown_pct': self.max_drawdown_pct,
'M_sharpe_ratio': self.sharpe_ratio,
'M_sortino_ratio': self.sortino_ratio,
'M_calmar_ratio': self.calmar_ratio,
'M_avg_win_pct': self.avg_win_pct,
'M_avg_loss_pct': self.avg_loss_pct,
'M_win_loss_ratio': self.win_loss_ratio,
'M_expectancy_pct': self.expectancy_pct,
'M_h1_roi_pct': self.h1_roi_pct,
'M_h2_roi_pct': self.h2_roi_pct,
'M_h2_h1_ratio': self.h2_h1_ratio,
'M_n_consecutive_losses_max': self.n_consecutive_losses_max,
'M_n_stop_exits': self.n_stop_exits,
'M_n_tp_exits': self.n_tp_exits,
'M_n_hold_exits': self.n_hold_exits,
'M_stop_rate': self.stop_rate,
'M_tp_rate': self.tp_rate,
'M_hold_rate': self.hold_rate,
'M_avg_hold_bars': self.avg_hold_bars,
'M_vol_of_daily_pnl': self.vol_of_daily_pnl,
'M_skew_daily_pnl': self.skew_daily_pnl,
'M_kurtosis_daily_pnl': self.kurtosis_daily_pnl,
'M_worst_day_pct': self.worst_day_pct,
'M_best_day_pct': self.best_day_pct,
'M_n_days_profitable': self.n_days_profitable,
'M_n_days_loss': self.n_days_loss,
'M_profitable_day_rate': self.profitable_day_rate,
'M_max_daily_drawdown_pct': self.max_daily_drawdown_pct,
'M_dc_skip_rate': self.dc_skip_rate,
'M_ob_skip_rate': self.ob_skip_rate,
'M_dc_confirm_rate': self.dc_confirm_rate,
'M_irp_match_rate': self.irp_match_rate,
'M_entry_attempt_rate': self.entry_attempt_rate,
'M_signal_to_trade_rate': self.signal_to_trade_rate,
'M_equity_curve_slope': self.equity_curve_slope,
'M_equity_curve_r2': self.equity_curve_r2,
'M_equity_curve_autocorr': self.equity_curve_autocorr,
'M_max_underwater_days': self.max_underwater_days,
'M_recovery_factor': self.recovery_factor,
'M_date_pnl_std': self.date_pnl_std,
'M_date_pnl_range': self.date_pnl_range,
'M_q10_date_pnl': self.q10_date_pnl,
'M_q90_date_pnl': self.q90_date_pnl,
'M_tail_ratio': self.tail_ratio,
})
# Add labels with L_ prefix
result.update({
'L_profitable': self.profitable,
'L_strongly_profitable': self.strongly_profitable,
'L_drawdown_ok': self.drawdown_ok,
'L_sharpe_ok': self.sharpe_ok,
'L_pf_ok': self.pf_ok,
'L_wr_ok': self.wr_ok,
'L_champion_region': self.champion_region,
'L_catastrophic': self.catastrophic,
'L_inert': self.inert,
'L_h2_degradation': self.h2_degradation,
})
return result
@classmethod
def from_dict(cls, d: Dict[str, Any]) -> 'MCTrialResult':
"""Create from dictionary."""
# Extract config
config_dict = {k[2:]: v for k, v in d.items() if k.startswith('P_') and k != 'P_trial_id'}
config = MCTrialConfig.from_dict(config_dict)
# Create result
result = cls(trial_id=d.get('trial_id', 0), config=config)
# Set metrics
for k, v in d.items():
if k.startswith('M_'):
attr_name = k[2:]
if hasattr(result, attr_name):
setattr(result, attr_name, v)
elif k.startswith('L_'):
attr_name = k[2:]
if hasattr(result, attr_name):
setattr(result, attr_name, v)
# Set metadata
result.timestamp = d.get('timestamp', datetime.now().isoformat())
result.execution_time_sec = d.get('execution_time_sec', 0.0)
result.status = d.get('status', 'completed')
result.error_message = d.get('error_message')
return result
class MCMetrics:
"""
Monte Carlo Metrics Extractor.
Computes all 48 metrics and 10 classification labels from backtest results.
"""
def __init__(self, initial_capital: float = 25000.0):
"""
Initialize metrics extractor.
Parameters
----------
initial_capital : float
Initial capital for ROI calculation
"""
self.initial_capital = initial_capital
def compute(
self,
config: MCTrialConfig,
trades: List[Dict],
daily_pnls: List[float],
date_stats: List[Dict],
signal_stats: Dict[str, Any],
execution_time_sec: float = 0.0
) -> MCTrialResult:
"""
Compute all metrics from backtest results.
Parameters
----------
config : MCTrialConfig
Trial configuration
trades : List[Dict]
Trade records with keys: pnl, pnl_pct, exit_type, bars_held, etc.
daily_pnls : List[float]
Daily P&L values
date_stats : List[Dict]
Per-date statistics
signal_stats : Dict[str, Any]
Signal processing statistics
execution_time_sec : float
Trial execution time
Returns
-------
MCTrialResult
Complete trial result with all metrics
"""
result = MCTrialResult(trial_id=config.trial_id, config=config)
result.execution_time_sec = execution_time_sec
# Compute metrics
self._compute_performance_metrics(result, trades, daily_pnls, date_stats)
self._compute_risk_metrics(result, trades, daily_pnls)
self._compute_signal_metrics(result, signal_stats)
self._compute_capital_metrics(result, daily_pnls)
self._compute_regime_metrics(result, daily_pnls)
# Compute labels
result.compute_labels()
result.status = "completed"
return result
def _compute_performance_metrics(
self,
result: MCTrialResult,
trades: List[Dict],
daily_pnls: List[float],
date_stats: List[Dict]
):
"""Compute M01-M15: Primary Performance Metrics."""
n_trades = len(trades)
result.n_trades = n_trades
if n_trades == 0:
# No trades - all metrics stay at defaults
return
# Win/loss separation
winning_trades = [t for t in trades if t.get('pnl', 0) > 0]
losing_trades = [t for t in trades if t.get('pnl', 0) <= 0]
n_wins = len(winning_trades)
n_losses = len(losing_trades)
# M01: roi_pct
final_capital = self.initial_capital + sum(daily_pnls) if daily_pnls else self.initial_capital
result.roi_pct = (final_capital - self.initial_capital) / self.initial_capital * 100
# M02: profit_factor
gross_wins = sum(t.get('pnl', 0) for t in winning_trades)
gross_losses = abs(sum(t.get('pnl', 0) for t in losing_trades))
result.profit_factor = gross_wins / gross_losses if gross_losses > 0 else float('inf')
# M03: win_rate
result.win_rate = n_wins / n_trades if n_trades > 0 else 0
# M05: max_drawdown_pct
result.max_drawdown_pct = self._compute_max_drawdown_pct(daily_pnls)
# M06: sharpe_ratio (annualized)
result.sharpe_ratio = self._compute_sharpe_ratio(daily_pnls)
# M07: sortino_ratio
result.sortino_ratio = self._compute_sortino_ratio(daily_pnls)
# M08: calmar_ratio
result.calmar_ratio = result.roi_pct / result.max_drawdown_pct if result.max_drawdown_pct > 0 else float('inf')
# M09: avg_win_pct
win_pnls_pct = [t.get('pnl_pct', 0) * 100 for t in winning_trades]
result.avg_win_pct = np.mean(win_pnls_pct) if win_pnls_pct else 0
# M10: avg_loss_pct
loss_pnls_pct = [t.get('pnl_pct', 0) * 100 for t in losing_trades]
result.avg_loss_pct = np.mean(loss_pnls_pct) if loss_pnls_pct else 0
# M11: win_loss_ratio
result.win_loss_ratio = abs(result.avg_win_pct / result.avg_loss_pct) if result.avg_loss_pct != 0 else float('inf')
# M12: expectancy_pct
wr = result.win_rate
result.expectancy_pct = wr * result.avg_win_pct + (1 - wr) * result.avg_loss_pct
# M13-M15: H1/H2 metrics
if len(date_stats) >= 2:
mid = len(date_stats) // 2
h1_pnl = sum(d.get('pnl', 0) for d in date_stats[:mid])
h2_pnl = sum(d.get('pnl', 0) for d in date_stats[mid:])
h1_capital = self.initial_capital + h1_pnl
result.h1_roi_pct = h1_pnl / self.initial_capital * 100
result.h2_roi_pct = h2_pnl / self.initial_capital * 100
result.h2_h1_ratio = h2_pnl / h1_pnl if h1_pnl != 0 else 0
def _compute_risk_metrics(
self,
result: MCTrialResult,
trades: List[Dict],
daily_pnls: List[float]
):
"""Compute M16-M32: Risk / Stability Metrics."""
# M16: n_consecutive_losses_max
result.n_consecutive_losses_max = self._compute_max_consecutive_losses(trades)
# M17-M19: Exit type counts
result.n_stop_exits = sum(1 for t in trades if t.get('exit_type') == 'stop')
result.n_tp_exits = sum(1 for t in trades if t.get('exit_type') == 'tp')
result.n_hold_exits = sum(1 for t in trades if t.get('exit_type') == 'hold')
# M20-M22: Exit rates
n_trades = len(trades)
if n_trades > 0:
result.stop_rate = result.n_stop_exits / n_trades
result.tp_rate = result.n_tp_exits / n_trades
result.hold_rate = result.n_hold_exits / n_trades
# M23: avg_hold_bars
hold_bars = [t.get('bars_held', 0) for t in trades]
result.avg_hold_bars = np.mean(hold_bars) if hold_bars else 0
# M24-M26: Daily P&L distribution stats
if len(daily_pnls) >= 2:
result.vol_of_daily_pnl = np.std(daily_pnls, ddof=1)
result.skew_daily_pnl = self._compute_skewness(daily_pnls)
result.kurtosis_daily_pnl = self._compute_kurtosis(daily_pnls)
# M27-M28: Best/worst day
if daily_pnls:
result.worst_day_pct = min(daily_pnls) / self.initial_capital * 100
result.best_day_pct = max(daily_pnls) / self.initial_capital * 100
# M29-M31: Profitable days
result.n_days_profitable = sum(1 for pnl in daily_pnls if pnl > 0)
result.n_days_loss = sum(1 for pnl in daily_pnls if pnl <= 0)
if daily_pnls:
result.profitable_day_rate = result.n_days_profitable / len(daily_pnls)
# M32: max_daily_drawdown_pct
result.max_daily_drawdown_pct = self._compute_max_daily_drawdown_pct(daily_pnls)
def _compute_signal_metrics(
self,
result: MCTrialResult,
signal_stats: Dict[str, Any]
):
"""Compute M33-M38: Signal Quality Metrics."""
result.dc_skip_rate = signal_stats.get('dc_skip_rate', 0)
result.ob_skip_rate = signal_stats.get('ob_skip_rate', 0)
result.dc_confirm_rate = signal_stats.get('dc_confirm_rate', 0)
result.irp_match_rate = signal_stats.get('irp_match_rate', 0)
result.entry_attempt_rate = signal_stats.get('entry_attempt_rate', 0)
result.signal_to_trade_rate = signal_stats.get('signal_to_trade_rate', 0)
def _compute_capital_metrics(
self,
result: MCTrialResult,
daily_pnls: List[float]
):
"""Compute M39-M43: Capital Path Metrics."""
if len(daily_pnls) < 2:
return
# Compute equity curve
equity = [self.initial_capital]
for pnl in daily_pnls:
equity.append(equity[-1] + pnl)
# M39: equity_curve_slope (linear regression)
days = np.arange(len(equity))
result.equity_curve_slope, result.equity_curve_r2 = self._linear_regression(days, equity)
# M41: equity_curve_autocorr
returns = np.diff(equity) / equity[:-1]
if len(returns) > 1:
result.equity_curve_autocorr = np.corrcoef(returns[:-1], returns[1:])[0, 1] if len(returns) > 2 else 0
# M42: max_underwater_days
result.max_underwater_days = self._compute_max_underwater_days(equity)
# M43: recovery_factor
total_return = sum(daily_pnls)
max_dd = self._compute_max_drawdown_value(daily_pnls)
result.recovery_factor = total_return / max_dd if max_dd > 0 else float('inf')
def _compute_regime_metrics(
self,
result: MCTrialResult,
daily_pnls: List[float]
):
"""Compute M44-M48: Regime Metrics."""
if len(daily_pnls) < 2:
return
# M44: date_pnl_std
result.date_pnl_std = np.std(daily_pnls, ddof=1)
# M45: date_pnl_range
result.date_pnl_range = max(daily_pnls) - min(daily_pnls)
# M46-M47: Quantiles
result.q10_date_pnl = np.percentile(daily_pnls, 10)
result.q90_date_pnl = np.percentile(daily_pnls, 90)
# M48: tail_ratio
if result.q90_date_pnl != 0:
result.tail_ratio = abs(result.q10_date_pnl) / abs(result.q90_date_pnl)
# --- Helper Methods ---
def _compute_max_drawdown_pct(self, daily_pnls: List[float]) -> float:
"""Compute maximum drawdown as percentage."""
if not daily_pnls:
return 0
equity = [self.initial_capital]
for pnl in daily_pnls:
equity.append(equity[-1] + pnl)
peak = equity[0]
max_dd = 0
for e in equity:
if e > peak:
peak = e
dd = (peak - e) / peak
max_dd = max(max_dd, dd)
return max_dd * 100
def _compute_max_drawdown_value(self, daily_pnls: List[float]) -> float:
"""Compute maximum drawdown as value."""
if not daily_pnls:
return 0
equity = [self.initial_capital]
for pnl in daily_pnls:
equity.append(equity[-1] + pnl)
peak = equity[0]
max_dd = 0
for e in equity:
if e > peak:
peak = e
dd = peak - e
max_dd = max(max_dd, dd)
return max_dd
def _compute_sharpe_ratio(self, daily_pnls: List[float]) -> float:
"""Compute annualized Sharpe ratio."""
if len(daily_pnls) < 2:
return 0
returns = [p / self.initial_capital for p in daily_pnls]
mean_ret = np.mean(returns)
std_ret = np.std(returns, ddof=1)
if std_ret == 0:
return 0
# Annualize (assuming 365 trading days)
return (mean_ret / std_ret) * np.sqrt(365)
def _compute_sortino_ratio(self, daily_pnls: List[float]) -> float:
"""Compute annualized Sortino ratio."""
if len(daily_pnls) < 2:
return 0
returns = [p / self.initial_capital for p in daily_pnls]
mean_ret = np.mean(returns)
# Downside deviation (only negative returns)
downside_returns = [r for r in returns if r < 0]
if not downside_returns:
return float('inf')
downside_std = np.std(downside_returns, ddof=1)
if downside_std == 0:
return float('inf')
return (mean_ret / downside_std) * np.sqrt(365)
def _compute_max_consecutive_losses(self, trades: List[Dict]) -> int:
"""Compute maximum consecutive losing trades."""
max_consec = 0
current_consec = 0
for trade in trades:
if trade.get('pnl', 0) <= 0:
current_consec += 1
max_consec = max(max_consec, current_consec)
else:
current_consec = 0
return max_consec
def _compute_skewness(self, data: List[float]) -> float:
"""Compute skewness."""
if len(data) < 3:
return 0
n = len(data)
mean = np.mean(data)
std = np.std(data, ddof=1)
if std == 0:
return 0
skew = sum(((x - mean) / std) ** 3 for x in data) * n / ((n - 1) * (n - 2))
return skew
def _compute_kurtosis(self, data: List[float]) -> float:
"""Compute excess kurtosis."""
if len(data) < 4:
return 0
n = len(data)
mean = np.mean(data)
std = np.std(data, ddof=1)
if std == 0:
return 0
kurt = sum(((x - mean) / std) ** 4 for x in data) * n * (n + 1) / ((n - 1) * (n - 2) * (n - 3))
kurt -= 3 * (n - 1) ** 2 / ((n - 2) * (n - 3))
return kurt
def _linear_regression(self, x: np.ndarray, y: List[float]) -> Tuple[float, float]:
"""Simple linear regression. Returns (slope, r_squared)."""
if len(x) < 2:
return 0, 0
x_mean = np.mean(x)
y_mean = np.mean(y)
numerator = sum((xi - x_mean) * (yi - y_mean) for xi, yi in zip(x, y))
denom_x = sum((xi - x_mean) ** 2 for xi in x)
denom_y = sum((yi - y_mean) ** 2 for yi in y)
if denom_x == 0:
return 0, 0
slope = numerator / denom_x
if denom_y == 0:
r_squared = 0
else:
r_squared = (numerator ** 2) / (denom_x * denom_y)
return slope, r_squared
def _compute_max_underwater_days(self, equity: List[float]) -> int:
"""Compute maximum consecutive days in drawdown."""
max_underwater = 0
current_underwater = 0
peak = equity[0]
for e in equity:
if e >= peak:
peak = e
current_underwater = 0
else:
current_underwater += 1
max_underwater = max(max_underwater, current_underwater)
return max_underwater
def _compute_max_daily_drawdown_pct(self, daily_pnls: List[float]) -> float:
"""Compute worst single-day drawdown percentage."""
if not daily_pnls:
return 0
equity = [self.initial_capital]
for pnl in daily_pnls:
equity.append(equity[-1] + pnl)
max_dd_pct = 0
for i in range(1, len(equity)):
prev_equity = equity[i-1]
if prev_equity > 0:
dd_pct = min(0, daily_pnls[i-1]) / prev_equity * 100
max_dd_pct = min(max_dd_pct, dd_pct)
return max_dd_pct
def test_metrics():
"""Quick test of metrics computation."""
from .mc_sampler import MCSampler
sampler = MCSampler()
config = sampler.generate_champion_trial()
# Create dummy data
trades = [
{'pnl': 100, 'pnl_pct': 0.004, 'exit_type': 'tp', 'bars_held': 50},
{'pnl': -50, 'pnl_pct': -0.002, 'exit_type': 'stop', 'bars_held': 20},
{'pnl': 150, 'pnl_pct': 0.006, 'exit_type': 'tp', 'bars_held': 80},
] * 20 # 60 trades
daily_pnls = [50, -20, 80, -10, 100, -30, 60, 40, -15, 90] * 5 # 50 days
date_stats = [{'date': f'2026-01-{i+1:02d}', 'pnl': daily_pnls[i]} for i in range(len(daily_pnls))]
signal_stats = {
'dc_skip_rate': 0.1,
'ob_skip_rate': 0.05,
'dc_confirm_rate': 0.7,
'irp_match_rate': 0.6,
'entry_attempt_rate': 0.3,
'signal_to_trade_rate': 0.15,
}
metrics = MCMetrics()
result = metrics.compute(config, trades, daily_pnls, date_stats, signal_stats)
print("Test Metrics Result:")
print(f" ROI: {result.roi_pct:.2f}%")
print(f" Profit Factor: {result.profit_factor:.2f}")
print(f" Win Rate: {result.win_rate:.2%}")
print(f" Sharpe: {result.sharpe_ratio:.2f}")
print(f" Max DD: {result.max_drawdown_pct:.2f}%")
print(f" Champion Region: {result.champion_region}")
return result
if __name__ == "__main__":
test_metrics()

View File

@@ -1,499 +0,0 @@
"""
Monte Carlo ML Envelope Learning
================================
Train ML models on MC results for envelope boundary estimation and forewarning.
Models:
- Regression models for ROI, DD, PF, WR prediction
- Classification models for champion_region, catastrophic
- One-Class SVM for envelope boundary estimation
- SHAP for feature importance
Reference: MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md Section 9, 12
"""
import json
import pickle
from typing import Dict, List, Optional, Any, Tuple
from pathlib import Path
from dataclasses import dataclass
import numpy as np
# Try to import ML libraries
try:
from sklearn.ensemble import GradientBoostingRegressor, RandomForestClassifier
from sklearn.svm import OneClassSVM
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
SKLEARN_AVAILABLE = True
except ImportError:
SKLEARN_AVAILABLE = False
print("[WARN] scikit-learn not available - ML training disabled")
try:
import xgboost as xgb
XGBOOST_AVAILABLE = True
except ImportError:
XGBOOST_AVAILABLE = False
try:
import shap
SHAP_AVAILABLE = True
except ImportError:
SHAP_AVAILABLE = False
from .mc_sampler import MCTrialConfig, MCSampler
from .mc_store import MCStore
@dataclass
class ForewarningReport:
"""Forewarning report for a configuration."""
config: Dict[str, Any]
predicted_roi: float
predicted_roi_p10: float
predicted_roi_p90: float
predicted_max_dd: float
champion_probability: float
catastrophic_probability: float
envelope_score: float
warnings: List[str]
nearest_champion: Optional[Dict[str, Any]]
parameter_risks: Dict[str, float]
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
'config': self.config,
'predicted_roi': self.predicted_roi,
'predicted_roi_p10': self.predicted_roi_p10,
'predicted_roi_p90': self.predicted_roi_p90,
'predicted_max_dd': self.predicted_max_dd,
'champion_probability': self.champion_probability,
'catastrophic_probability': self.catastrophic_probability,
'envelope_score': self.envelope_score,
'warnings': self.warnings,
'nearest_champion': self.nearest_champion,
'parameter_risks': self.parameter_risks,
}
class MCML:
"""
Monte Carlo ML Envelope Learning.
Trains models on MC results and provides forewarning capabilities.
"""
def __init__(
self,
output_dir: str = "mc_results",
models_dir: Optional[str] = None
):
"""
Initialize ML trainer.
Parameters
----------
output_dir : str
MC results directory
models_dir : str, optional
Directory to save trained models
"""
self.output_dir = Path(output_dir)
self.models_dir = Path(models_dir) if models_dir else self.output_dir / "models"
self.models_dir.mkdir(parents=True, exist_ok=True)
self.store = MCStore(output_dir=output_dir)
# Models
self.models: Dict[str, Any] = {}
self.scalers: Dict[str, StandardScaler] = {}
self.feature_names: List[str] = []
self._init_feature_names()
def _init_feature_names(self):
"""Initialize feature names from parameter space."""
sampler = MCSampler()
self.feature_names = list(sampler.CHAMPION.keys())
def load_corpus(self) -> Optional[Any]:
"""Load full corpus from store."""
return self.store.load_corpus()
def train_all_models(self, test_size: float = 0.2) -> Dict[str, Any]:
"""
Train all ML models on the corpus.
Parameters
----------
test_size : float
Fraction of data for testing
Returns
-------
Dict[str, Any]
Training results and metrics
"""
if not SKLEARN_AVAILABLE:
raise RuntimeError("scikit-learn required for training")
print("="*70)
print("TRAINING ML MODELS")
print("="*70)
# Load corpus
print("\n[1/6] Loading corpus...")
df = self.load_corpus()
if df is None or len(df) == 0:
raise ValueError("No corpus data available")
print(f" Loaded {len(df)} trials")
# Prepare features
print("\n[2/6] Preparing features...")
X = self._extract_features(df)
# Train regression models
print("\n[3/6] Training regression models...")
self._train_regression_model(X, df, 'M_roi_pct', 'model_roi')
self._train_regression_model(X, df, 'M_max_drawdown_pct', 'model_dd')
self._train_regression_model(X, df, 'M_profit_factor', 'model_pf')
self._train_regression_model(X, df, 'M_win_rate', 'model_wr')
# Train classification models
print("\n[4/6] Training classification models...")
self._train_classification_model(X, df, 'L_champion_region', 'model_champ')
self._train_classification_model(X, df, 'L_catastrophic', 'model_catas')
self._train_classification_model(X, df, 'L_inert', 'model_inert')
self._train_classification_model(X, df, 'L_h2_degradation', 'model_h2deg')
# Train envelope model (One-Class SVM on champions)
print("\n[5/6] Training envelope boundary model...")
self._train_envelope_model(X, df)
# Save models
print("\n[6/6] Saving models...")
self._save_models()
print("\n[OK] All models trained and saved")
return {'status': 'success', 'n_samples': len(df)}
def _extract_features(self, df: Any) -> np.ndarray:
"""Extract feature matrix from DataFrame."""
# Get parameter columns
param_cols = [f'P_{name}' for name in self.feature_names if f'P_{name}' in df.columns]
# Extract and normalize
X = df[param_cols].values
# Standardize
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
self.scalers['default'] = scaler
return X_scaled
def _train_regression_model(
self,
X: np.ndarray,
df: Any,
target_col: str,
model_name: str
):
"""Train a regression model."""
if target_col not in df.columns:
print(f" [SKIP] {model_name}: target column not found")
return
y = df[target_col].values
# Split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Train
model = GradientBoostingRegressor(
n_estimators=100,
max_depth=5,
learning_rate=0.1,
random_state=42
)
model.fit(X_train, y_train)
# Evaluate
train_score = model.score(X_train, y_train)
test_score = model.score(X_test, y_test)
print(f" {model_name}: R² train={train_score:.3f}, test={test_score:.3f}")
self.models[model_name] = model
def _train_classification_model(
self,
X: np.ndarray,
df: Any,
target_col: str,
model_name: str
):
"""Train a classification model."""
if target_col not in df.columns:
print(f" [SKIP] {model_name}: target column not found")
return
y = df[target_col].astype(int).values
# Check if we have both classes
if len(set(y)) < 2:
print(f" [SKIP] {model_name}: only one class present")
return
# Split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# Train with XGBoost if available, else RandomForest
if XGBOOST_AVAILABLE:
model = xgb.XGBClassifier(
n_estimators=100,
max_depth=5,
learning_rate=0.1,
random_state=42,
use_label_encoder=False,
eval_metric='logloss'
)
else:
model = RandomForestClassifier(
n_estimators=100,
max_depth=5,
random_state=42
)
model.fit(X_train, y_train)
# Evaluate
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print(f" {model_name}: accuracy={acc:.3f}")
self.models[model_name] = model
def _train_envelope_model(self, X: np.ndarray, df: Any):
"""Train One-Class SVM on champion region configurations."""
if 'L_champion_region' not in df.columns:
print(" [SKIP] envelope: champion_region column not found")
return
# Filter to champions
champion_mask = df['L_champion_region'].astype(bool)
X_champions = X[champion_mask]
if len(X_champions) < 100:
print(f" [SKIP] envelope: only {len(X_champions)} champions (need 100+)")
return
print(f" Training on {len(X_champions)} champion configurations")
# Train One-Class SVM
model = OneClassSVM(kernel='rbf', nu=0.05, gamma='scale')
model.fit(X_champions)
self.models['envelope'] = model
print(f" Envelope model trained")
def _save_models(self):
"""Save all trained models."""
# Save models
for name, model in self.models.items():
path = self.models_dir / f"{name}.pkl"
with open(path, 'wb') as f:
pickle.dump(model, f)
# Save scalers
for name, scaler in self.scalers.items():
path = self.models_dir / f"scaler_{name}.pkl"
with open(path, 'wb') as f:
pickle.dump(scaler, f)
# Save feature names
with open(self.models_dir / "feature_names.json", 'w') as f:
json.dump(self.feature_names, f)
print(f" Saved {len(self.models)} models to {self.models_dir}")
def load_models(self):
"""Load trained models from disk."""
# Load feature names
with open(self.models_dir / "feature_names.json", 'r') as f:
self.feature_names = json.load(f)
# Load models
model_files = list(self.models_dir.glob("*.pkl"))
for path in model_files:
if 'scaler_' in path.name:
continue
with open(path, 'rb') as f:
self.models[path.stem] = pickle.load(f)
# Load scalers
for path in self.models_dir.glob("scaler_*.pkl"):
name = path.stem.replace('scaler_', '')
with open(path, 'rb') as f:
self.scalers[name] = pickle.load(f)
print(f"[OK] Loaded {len(self.models)} models")
def predict(self, config: MCTrialConfig) -> Dict[str, float]:
"""
Make predictions for a configuration.
Parameters
----------
config : MCTrialConfig
Configuration to predict
Returns
-------
Dict[str, float]
Predictions for all targets
"""
if not self.models:
self.load_models()
# Extract features
X = self._config_to_features(config)
predictions = {}
# Regression predictions
if 'model_roi' in self.models:
predictions['roi'] = self.models['model_roi'].predict(X)[0]
if 'model_dd' in self.models:
predictions['max_dd'] = self.models['model_dd'].predict(X)[0]
if 'model_pf' in self.models:
predictions['profit_factor'] = self.models['model_pf'].predict(X)[0]
if 'model_wr' in self.models:
predictions['win_rate'] = self.models['model_wr'].predict(X)[0]
# Classification predictions (probability of positive class)
if 'model_champ' in self.models:
if hasattr(self.models['model_champ'], 'predict_proba'):
predictions['champion_prob'] = self.models['model_champ'].predict_proba(X)[0, 1]
else:
predictions['champion_prob'] = float(self.models['model_champ'].predict(X)[0])
if 'model_catas' in self.models:
if hasattr(self.models['model_catas'], 'predict_proba'):
predictions['catastrophic_prob'] = self.models['model_catas'].predict_proba(X)[0, 1]
else:
predictions['catastrophic_prob'] = float(self.models['model_catas'].predict(X)[0])
# Envelope score
if 'envelope' in self.models:
predictions['envelope_score'] = self.models['envelope'].decision_function(X)[0]
return predictions
def _config_to_features(self, config: MCTrialConfig) -> np.ndarray:
"""Convert config to feature vector."""
features = []
for name in self.feature_names:
value = getattr(config, name, MCSampler.CHAMPION[name])
features.append(value)
X = np.array([features])
# Scale
if 'default' in self.scalers:
X = self.scalers['default'].transform(X)
return X
class DolphinForewarner:
"""
Live forewarning system for Dolphin configurations.
Provides risk assessment based on trained MC envelope model.
"""
def __init__(self, models_dir: str = "mc_results/models"):
"""
Initialize forewarner.
Parameters
----------
models_dir : str
Directory with trained models
"""
self.ml = MCML(models_dir=models_dir)
self.ml.load_models()
def assess(self, config: MCTrialConfig) -> ForewarningReport:
"""
Assess a configuration and return forewarning report.
Parameters
----------
config : MCTrialConfig
Configuration to assess
Returns
-------
ForewarningReport
Complete risk assessment
"""
# Get predictions
preds = self.ml.predict(config)
# Build warnings
warnings = []
if preds.get('catastrophic_prob', 0) > 0.10:
warnings.append(f"Catastrophic risk: {preds['catastrophic_prob']:.1%}")
if preds.get('envelope_score', 0) < 0:
warnings.append("Configuration outside safe operating envelope")
# Check parameter boundaries
if config.max_leverage > 6.0:
warnings.append(f"High leverage: {config.max_leverage:.1f}x")
if config.fraction * config.max_leverage > 1.5:
warnings.append(f"High notional exposure: {config.fraction * config.max_leverage:.2f}x")
# Create report
report = ForewarningReport(
config=config.to_dict(),
predicted_roi=preds.get('roi', 0),
predicted_roi_p10=preds.get('roi', 0) * 0.5, # Simplified
predicted_roi_p90=preds.get('roi', 0) * 1.5,
predicted_max_dd=preds.get('max_dd', 0),
champion_probability=preds.get('champion_prob', 0),
catastrophic_probability=preds.get('catastrophic_prob', 0),
envelope_score=preds.get('envelope_score', 0),
warnings=warnings,
nearest_champion=None, # Would require search
parameter_risks={}
)
return report
def assess_config_dict(self, config_dict: Dict[str, Any]) -> ForewarningReport:
"""Assess from a configuration dictionary."""
config = MCTrialConfig.from_dict(config_dict)
return self.assess(config)
if __name__ == "__main__":
# Test
print("MC ML module loaded")
print("Run training with: MCML().train_all_models()")

File diff suppressed because it is too large Load Diff

View File

@@ -1,395 +0,0 @@
"""
Monte Carlo Runner
==================
Orchestration and parallel execution for MC envelope mapping.
Features:
- Parallel execution using multiprocessing
- Checkpointing and resume capability
- Batch processing
- Progress tracking
Reference: MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md Section 1, 5.4
"""
import time
import json
from typing import Dict, List, Optional, Any, Callable
from pathlib import Path
from datetime import datetime
import multiprocessing as mp
from functools import partial
from .mc_sampler import MCSampler, MCTrialConfig
from .mc_validator import MCValidator, ValidationResult
from .mc_executor import MCExecutor
from .mc_store import MCStore
from .mc_metrics import MCTrialResult
class MCRunner:
"""
Monte Carlo Runner.
Orchestrates the full MC envelope mapping pipeline:
1. Generate trial configurations
2. Validate configurations
3. Execute trials (parallel)
4. Store results
"""
def __init__(
self,
output_dir: str = "mc_results",
n_workers: int = -1,
batch_size: int = 1000,
base_seed: int = 42,
verbose: bool = True
):
"""
Initialize the runner.
Parameters
----------
output_dir : str
Directory for results
n_workers : int
Number of parallel workers (-1 for auto)
batch_size : int
Trials per batch
base_seed : int
Master RNG seed
verbose : bool
Print progress
"""
self.output_dir = Path(output_dir)
self.n_workers = n_workers if n_workers > 0 else max(1, mp.cpu_count() - 1)
self.batch_size = batch_size
self.base_seed = base_seed
self.verbose = verbose
# Components
self.sampler = MCSampler(base_seed=base_seed)
self.store = MCStore(output_dir=output_dir, batch_size=batch_size)
# State
self.completed_trials: set = set()
self.stats: Dict[str, Any] = {}
def generate_and_validate(
self,
n_samples_per_switch: int = 500,
max_trials: Optional[int] = None
) -> List[MCTrialConfig]:
"""
Generate and validate trial configurations.
Parameters
----------
n_samples_per_switch : int
Samples per switch vector
max_trials : int, optional
Maximum total trials
Returns
-------
List[MCTrialConfig]
Valid trial configurations
"""
print("="*70)
print("PHASE 1: GENERATE & VALIDATE CONFIGURATIONS")
print("="*70)
# Generate trials
print(f"\n[1/3] Generating trials (n_samples_per_switch={n_samples_per_switch})...")
all_configs = self.sampler.generate_trials(
n_samples_per_switch=n_samples_per_switch,
max_trials=max_trials
)
# Validate
print(f"\n[2/3] Validating {len(all_configs)} configurations...")
validator = MCValidator(verbose=False)
validation_results = validator.validate_batch(all_configs)
# Filter valid configs
valid_configs = [
config for config, result in zip(all_configs, validation_results)
if result.is_valid()
]
# Save validation results
self.store.save_validation_results(validation_results, batch_id=0)
# Stats
stats = validator.get_validity_stats(validation_results)
print(f"\n[3/3] Validation complete:")
print(f" Total: {stats['total']}")
print(f" Valid: {stats['valid']} ({stats['validity_rate']*100:.1f}%)")
print(f" Rejected: {stats['total'] - stats['valid']}")
self.stats['validation'] = stats
return valid_configs
def run_envelope_mapping(
self,
n_samples_per_switch: int = 500,
max_trials: Optional[int] = None,
resume: bool = True
) -> Dict[str, Any]:
"""
Run full envelope mapping.
Parameters
----------
n_samples_per_switch : int
Samples per switch vector
max_trials : int, optional
Maximum total trials
resume : bool
Resume from existing results
Returns
-------
Dict[str, Any]
Run statistics
"""
start_time = time.time()
# Generate and validate
valid_configs = self.generate_and_validate(
n_samples_per_switch=n_samples_per_switch,
max_trials=max_trials
)
# Check for resume
if resume:
self._load_completed_trials()
valid_configs = [c for c in valid_configs if c.trial_id not in self.completed_trials]
print(f"\n[Resume] {len(self.completed_trials)} trials already completed")
print(f"[Resume] {len(valid_configs)} trials remaining")
if not valid_configs:
print("\n[OK] All trials already completed!")
return self._get_run_stats(start_time)
# Execute trials
print("\n" + "="*70)
print("PHASE 2: EXECUTE TRIALS")
print("="*70)
print(f"\nRunning {len(valid_configs)} trials with {self.n_workers} workers...")
# Split into batches
batches = self._split_into_batches(valid_configs)
print(f"Split into {len(batches)} batches (batch_size={self.batch_size})")
# Process batches
total_completed = 0
for batch_idx, batch_configs in enumerate(batches):
print(f"\n--- Batch {batch_idx+1}/{len(batches)} ({len(batch_configs)} trials) ---")
batch_start = time.time()
if self.n_workers > 1 and len(batch_configs) > 1:
# Parallel execution
results = self._execute_parallel(batch_configs)
else:
# Sequential execution
results = self._execute_sequential(batch_configs)
# Save results
self.store.save_trial_results(results, batch_id=batch_idx+1)
batch_time = time.time() - batch_start
total_completed += len(results)
print(f"Batch {batch_idx+1} complete in {batch_time:.1f}s "
f"({len(results)/batch_time:.1f} trials/sec)")
# Progress
progress = total_completed / len(valid_configs)
eta_seconds = (time.time() - start_time) / progress * (1 - progress) if progress > 0 else 0
print(f"Overall: {total_completed}/{len(valid_configs)} ({progress*100:.1f}%) "
f"ETA: {eta_seconds/60:.1f} min")
return self._get_run_stats(start_time)
def _split_into_batches(
self,
configs: List[MCTrialConfig]
) -> List[List[MCTrialConfig]]:
"""Split configurations into batches."""
batches = []
for i in range(0, len(configs), self.batch_size):
batches.append(configs[i:i+self.batch_size])
return batches
def _execute_sequential(
self,
configs: List[MCTrialConfig]
) -> List[MCTrialResult]:
"""Execute trials sequentially."""
executor = MCExecutor(verbose=self.verbose)
return executor.execute_batch(configs, progress_interval=max(1, len(configs)//10))
def _execute_parallel(
self,
configs: List[MCTrialConfig]
) -> List[MCTrialResult]:
"""Execute trials in parallel using multiprocessing."""
# Create worker function
worker = partial(_execute_trial_worker, initial_capital=25000.0)
# Run in pool
with mp.Pool(processes=self.n_workers) as pool:
results = pool.map(worker, configs)
return results
def _load_completed_trials(self):
"""Load IDs of already completed trials from index."""
entries = self.store.query_index(status='completed', limit=1000000)
self.completed_trials = {e['trial_id'] for e in entries}
def _get_run_stats(self, start_time: float) -> Dict[str, Any]:
"""Get final run statistics."""
total_time = time.time() - start_time
corpus_stats = self.store.get_corpus_stats()
stats = {
'total_time_sec': total_time,
'total_time_min': total_time / 60,
'total_time_hours': total_time / 3600,
**corpus_stats,
}
print("\n" + "="*70)
print("ENVELOPE MAPPING COMPLETE")
print("="*70)
print(f"\nTotal time: {total_time/3600:.2f} hours")
print(f"Total trials: {stats['total_trials']}")
print(f"Champion region: {stats['champion_count']}")
print(f"Catastrophic: {stats['catastrophic_count']}")
print(f"Avg ROI: {stats['avg_roi_pct']:.2f}%")
print(f"Avg Sharpe: {stats['avg_sharpe']:.2f}")
return stats
def generate_report(self, output_path: Optional[str] = None):
"""Generate a summary report."""
stats = self.store.get_corpus_stats()
report = f"""
# Monte Carlo Envelope Mapping Report
Generated: {datetime.now().isoformat()}
## Corpus Statistics
- Total trials: {stats['total_trials']}
- Champion region: {stats['champion_count']} ({stats['champion_count']/max(1,stats['total_trials'])*100:.1f}%)
- Catastrophic: {stats['catastrophic_count']} ({stats['catastrophic_count']/max(1,stats['total_trials'])*100:.1f}%)
## Performance Metrics
- Average ROI: {stats['avg_roi_pct']:.2f}%
- Min ROI: {stats['min_roi_pct']:.2f}%
- Max ROI: {stats['max_roi_pct']:.2f}%
- Average Sharpe: {stats['avg_sharpe']:.2f}
- Average Max DD: {stats['avg_max_dd_pct']:.2f}%
## Validation Summary
"""
if 'validation' in self.stats:
vstats = self.stats['validation']
report += f"""
- Total configs: {vstats['total']}
- Valid configs: {vstats['valid']} ({vstats['validity_rate']*100:.1f}%)
- Rejected V1 (range): {vstats.get('rejected_v1', 0)}
- Rejected V2 (constraints): {vstats.get('rejected_v2', 0)}
- Rejected V3 (cross-group): {vstats.get('rejected_v3', 0)}
- Rejected V4 (degenerate): {vstats.get('rejected_v4', 0)}
"""
if output_path:
with open(output_path, 'w') as f:
f.write(report)
print(f"\n[OK] Report saved: {output_path}")
return report
def _execute_trial_worker(
config: MCTrialConfig,
initial_capital: float = 25000.0
) -> MCTrialResult:
"""
Worker function for parallel execution.
Must be at module level for pickle serialization.
"""
executor = MCExecutor(initial_capital=initial_capital, verbose=False)
return executor.execute_trial(config, skip_validation=True)
def run_mc_envelope(
n_samples_per_switch: int = 100, # Reduced default for testing
max_trials: Optional[int] = None,
n_workers: int = -1,
output_dir: str = "mc_results",
resume: bool = True,
base_seed: int = 42
) -> Dict[str, Any]:
"""
Convenience function to run full MC envelope mapping.
Parameters
----------
n_samples_per_switch : int
Samples per switch vector
max_trials : int, optional
Maximum total trials
n_workers : int
Number of parallel workers (-1 for auto)
output_dir : str
Output directory
resume : bool
Resume from existing results
base_seed : int
Master RNG seed
Returns
-------
Dict[str, Any]
Run statistics
"""
runner = MCRunner(
output_dir=output_dir,
n_workers=n_workers,
base_seed=base_seed
)
stats = runner.run_envelope_mapping(
n_samples_per_switch=n_samples_per_switch,
max_trials=max_trials,
resume=resume
)
# Generate report
runner.generate_report(output_path=f"{output_dir}/envelope_report.md")
return stats
if __name__ == "__main__":
# Test run
stats = run_mc_envelope(
n_samples_per_switch=10,
max_trials=100,
n_workers=1,
output_dir="mc_results_test"
)
print("\nTest complete!")

View File

@@ -1,534 +0,0 @@
"""
Monte Carlo Parameter Sampler
=============================
Parameter space definition and Latin Hypercube Sampling (LHS) implementation.
This module defines the complete 33-parameter space across 7 sub-systems
and implements the two-phase sampling strategy:
1. Phase A: Switch grid (boolean combinations)
2. Phase B: LHS continuous sampling per switch-vector
Reference: MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md Section 2, 3
"""
import numpy as np
from typing import Dict, List, Optional, Tuple, NamedTuple, Any, Union
from dataclasses import dataclass, field
from enum import Enum
import json
from pathlib import Path
# Try to import scipy for LHS
try:
from scipy.stats import qmc
SCIPY_AVAILABLE = True
except ImportError:
SCIPY_AVAILABLE = False
class ParamType(Enum):
"""Parameter sampling types."""
CONTINUOUS = "continuous"
DISCRETE = "discrete"
CATEGORICAL = "categorical"
BOOLEAN = "boolean"
DERIVED = "derived"
FIXED = "fixed"
@dataclass
class ParameterDef:
"""Definition of a single parameter."""
id: str
name: str
champion: Any
param_type: ParamType
lo: Optional[float] = None
hi: Optional[float] = None
log_transform: bool = False
constraint_group: Optional[str] = None
depends_on: Optional[str] = None # For conditional parameters
categories: Optional[List[str]] = None # For CATEGORICAL
def __post_init__(self):
if self.param_type == ParamType.CATEGORICAL and self.categories is None:
raise ValueError(f"Categorical parameter {self.name} must have categories")
class MCTrialConfig(NamedTuple):
"""Complete parameter vector for a Monte Carlo trial."""
trial_id: int
# P1 Signal
vel_div_threshold: float
vel_div_extreme: float
use_direction_confirm: bool
dc_lookback_bars: int
dc_min_magnitude_bps: float
dc_skip_contradicts: bool
dc_leverage_boost: float
dc_leverage_reduce: float
vd_trend_lookback: int
# P2 Leverage
min_leverage: float
max_leverage: float
leverage_convexity: float
fraction: float
use_alpha_layers: bool
use_dynamic_leverage: bool
# P3 Exit
fixed_tp_pct: float
stop_pct: float
max_hold_bars: int
# P4 Fees
use_sp_fees: bool
use_sp_slippage: bool
sp_maker_entry_rate: float
sp_maker_exit_rate: float
# P5 OB
use_ob_edge: bool
ob_edge_bps: float
ob_confirm_rate: float
ob_imbalance_bias: float
ob_depth_scale: float
# P6 Asset Selection
use_asset_selection: bool
min_irp_alignment: float
lookback: int
# P7 ACB
acb_beta_high: float
acb_beta_low: float
acb_w750_threshold_pct: int
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
'trial_id': self.trial_id,
'vel_div_threshold': self.vel_div_threshold,
'vel_div_extreme': self.vel_div_extreme,
'use_direction_confirm': self.use_direction_confirm,
'dc_lookback_bars': self.dc_lookback_bars,
'dc_min_magnitude_bps': self.dc_min_magnitude_bps,
'dc_skip_contradicts': self.dc_skip_contradicts,
'dc_leverage_boost': self.dc_leverage_boost,
'dc_leverage_reduce': self.dc_leverage_reduce,
'vd_trend_lookback': self.vd_trend_lookback,
'min_leverage': self.min_leverage,
'max_leverage': self.max_leverage,
'leverage_convexity': self.leverage_convexity,
'fraction': self.fraction,
'use_alpha_layers': self.use_alpha_layers,
'use_dynamic_leverage': self.use_dynamic_leverage,
'fixed_tp_pct': self.fixed_tp_pct,
'stop_pct': self.stop_pct,
'max_hold_bars': self.max_hold_bars,
'use_sp_fees': self.use_sp_fees,
'use_sp_slippage': self.use_sp_slippage,
'sp_maker_entry_rate': self.sp_maker_entry_rate,
'sp_maker_exit_rate': self.sp_maker_exit_rate,
'use_ob_edge': self.use_ob_edge,
'ob_edge_bps': self.ob_edge_bps,
'ob_confirm_rate': self.ob_confirm_rate,
'ob_imbalance_bias': self.ob_imbalance_bias,
'ob_depth_scale': self.ob_depth_scale,
'use_asset_selection': self.use_asset_selection,
'min_irp_alignment': self.min_irp_alignment,
'lookback': self.lookback,
'acb_beta_high': self.acb_beta_high,
'acb_beta_low': self.acb_beta_low,
'acb_w750_threshold_pct': self.acb_w750_threshold_pct,
}
@classmethod
def from_dict(cls, d: Dict[str, Any]) -> 'MCTrialConfig':
"""Create from dictionary."""
# Filter to only valid fields
valid_fields = cls._fields
filtered = {k: v for k, v in d.items() if k in valid_fields}
return cls(**filtered)
class MCSampler:
"""
Monte Carlo Parameter Sampler.
Implements two-phase sampling:
1. Phase A: Enumerate all boolean switch combinations
2. Phase B: LHS continuous sampling per switch-vector
"""
# Champion configuration (baseline)
CHAMPION = {
'vel_div_threshold': -0.020,
'vel_div_extreme': -0.050,
'use_direction_confirm': True,
'dc_lookback_bars': 7,
'dc_min_magnitude_bps': 0.75,
'dc_skip_contradicts': True,
'dc_leverage_boost': 1.00,
'dc_leverage_reduce': 0.50,
'vd_trend_lookback': 10,
'min_leverage': 0.50,
'max_leverage': 5.00,
'leverage_convexity': 3.00,
'fraction': 0.20,
'use_alpha_layers': True,
'use_dynamic_leverage': True,
'fixed_tp_pct': 0.0099,
'stop_pct': 1.00,
'max_hold_bars': 120,
'use_sp_fees': True,
'use_sp_slippage': True,
'sp_maker_entry_rate': 0.62,
'sp_maker_exit_rate': 0.50,
'use_ob_edge': True,
'ob_edge_bps': 5.00,
'ob_confirm_rate': 0.40,
'ob_imbalance_bias': -0.09,
'ob_depth_scale': 1.00,
'use_asset_selection': True,
'min_irp_alignment': 0.45,
'lookback': 100,
'acb_beta_high': 0.80,
'acb_beta_low': 0.20,
'acb_w750_threshold_pct': 60,
}
# Parameter definitions
PARAMS = {
# P1 Signal Generator
'vel_div_threshold': ParameterDef('P1.01', 'vel_div_threshold', -0.020, ParamType.CONTINUOUS, -0.040, -0.008, False, 'CG-VD'),
'vel_div_extreme': ParameterDef('P1.02', 'vel_div_extreme', -0.050, ParamType.CONTINUOUS, -0.120, None, False, 'CG-VD'), # hi depends on threshold
'use_direction_confirm': ParameterDef('P1.03', 'use_direction_confirm', True, ParamType.BOOLEAN, constraint_group='CG-DC'),
'dc_lookback_bars': ParameterDef('P1.04', 'dc_lookback_bars', 7, ParamType.DISCRETE, 3, 25, False, 'CG-DC'),
'dc_min_magnitude_bps': ParameterDef('P1.05', 'dc_min_magnitude_bps', 0.75, ParamType.CONTINUOUS, 0.20, 3.00, False, 'CG-DC'),
'dc_skip_contradicts': ParameterDef('P1.06', 'dc_skip_contradicts', True, ParamType.BOOLEAN, constraint_group='CG-DC'),
'dc_leverage_boost': ParameterDef('P1.07', 'dc_leverage_boost', 1.00, ParamType.CONTINUOUS, 1.00, 1.50, False, 'CG-DC-LEV'),
'dc_leverage_reduce': ParameterDef('P1.08', 'dc_leverage_reduce', 0.50, ParamType.CONTINUOUS, 0.25, 0.90, False, 'CG-DC-LEV'),
'vd_trend_lookback': ParameterDef('P1.09', 'vd_trend_lookback', 10, ParamType.DISCRETE, 5, 30, False),
# P2 Leverage
'min_leverage': ParameterDef('P2.01', 'min_leverage', 0.50, ParamType.CONTINUOUS, 0.10, 1.50, False, 'CG-LEV'),
'max_leverage': ParameterDef('P2.02', 'max_leverage', 5.00, ParamType.CONTINUOUS, 1.50, 12.00, False, 'CG-LEV'),
'leverage_convexity': ParameterDef('P2.03', 'leverage_convexity', 3.00, ParamType.CONTINUOUS, 0.75, 6.00, False),
'fraction': ParameterDef('P2.04', 'fraction', 0.20, ParamType.CONTINUOUS, 0.05, 0.40, False, 'CG-RISK'),
'use_alpha_layers': ParameterDef('P2.05', 'use_alpha_layers', True, ParamType.BOOLEAN),
'use_dynamic_leverage': ParameterDef('P2.06', 'use_dynamic_leverage', True, ParamType.BOOLEAN, constraint_group='CG-DYNLEV'),
# P3 Exit
'fixed_tp_pct': ParameterDef('P3.01', 'fixed_tp_pct', 0.0099, ParamType.CONTINUOUS, 0.0030, 0.0300, True, 'CG-EXIT'),
'stop_pct': ParameterDef('P3.02', 'stop_pct', 1.00, ParamType.CONTINUOUS, 0.20, 5.00, True, 'CG-EXIT'),
'max_hold_bars': ParameterDef('P3.03', 'max_hold_bars', 120, ParamType.DISCRETE, 20, 600, False, 'CG-EXIT'),
# P4 Fees
'use_sp_fees': ParameterDef('P4.01', 'use_sp_fees', True, ParamType.BOOLEAN),
'use_sp_slippage': ParameterDef('P4.02', 'use_sp_slippage', True, ParamType.BOOLEAN, constraint_group='CG-SP'),
'sp_maker_entry_rate': ParameterDef('P4.03', 'sp_maker_entry_rate', 0.62, ParamType.CONTINUOUS, 0.20, 0.85, False, 'CG-SP'),
'sp_maker_exit_rate': ParameterDef('P4.04', 'sp_maker_exit_rate', 0.50, ParamType.CONTINUOUS, 0.20, 0.85, False, 'CG-SP'),
# P5 OB Intelligence
'use_ob_edge': ParameterDef('P5.01', 'use_ob_edge', True, ParamType.BOOLEAN, constraint_group='CG-OB'),
'ob_edge_bps': ParameterDef('P5.02', 'ob_edge_bps', 5.00, ParamType.CONTINUOUS, 1.00, 20.00, True, 'CG-OB'),
'ob_confirm_rate': ParameterDef('P5.03', 'ob_confirm_rate', 0.40, ParamType.CONTINUOUS, 0.10, 0.80, False, 'CG-OB'),
'ob_imbalance_bias': ParameterDef('P5.04', 'ob_imbalance_bias', -0.09, ParamType.CONTINUOUS, -0.25, 0.15, False, 'CG-OB-SIG'),
'ob_depth_scale': ParameterDef('P5.05', 'ob_depth_scale', 1.00, ParamType.CONTINUOUS, 0.30, 2.00, True, 'CG-OB-SIG'),
# P6 Asset Selection
'use_asset_selection': ParameterDef('P6.01', 'use_asset_selection', True, ParamType.BOOLEAN, constraint_group='CG-IRP'),
'min_irp_alignment': ParameterDef('P6.02', 'min_irp_alignment', 0.45, ParamType.CONTINUOUS, 0.10, 0.80, False, 'CG-IRP'),
'lookback': ParameterDef('P6.03', 'lookback', 100, ParamType.DISCRETE, 30, 300, False, 'CG-IRP'),
# P7 ACB
'acb_beta_high': ParameterDef('P7.01', 'acb_beta_high', 0.80, ParamType.CONTINUOUS, 0.40, 1.50, False, 'CG-ACB'),
'acb_beta_low': ParameterDef('P7.02', 'acb_beta_low', 0.20, ParamType.CONTINUOUS, 0.00, 0.60, False, 'CG-ACB'),
'acb_w750_threshold_pct': ParameterDef('P7.03', 'acb_w750_threshold_pct', 60, ParamType.DISCRETE, 20, 80, False),
}
# Boolean parameters for switch grid
BOOLEAN_PARAMS = [
'use_direction_confirm',
'dc_skip_contradicts',
'use_alpha_layers',
'use_dynamic_leverage',
'use_sp_fees',
'use_sp_slippage',
'use_ob_edge',
'use_asset_selection',
]
# Parameters that become FIXED when their parent switch is False
CONDITIONAL_PARAMS = {
'use_direction_confirm': ['dc_lookback_bars', 'dc_min_magnitude_bps', 'dc_skip_contradicts', 'dc_leverage_boost', 'dc_leverage_reduce'],
'use_sp_slippage': ['sp_maker_entry_rate', 'sp_maker_exit_rate'],
'use_ob_edge': ['ob_edge_bps', 'ob_confirm_rate'],
'use_asset_selection': ['min_irp_alignment', 'lookback'],
}
def __init__(self, base_seed: int = 42):
"""
Initialize the sampler.
Parameters
----------
base_seed : int
Master RNG seed for reproducibility
"""
self.base_seed = base_seed
self.rng = np.random.RandomState(base_seed)
def generate_switch_vectors(self) -> List[Dict[str, Any]]:
"""
Phase A: Generate all unique boolean switch combinations.
After canonicalisation (collapsing equivalent configs),
returns approximately 64-96 unique switch vectors.
Returns
-------
List[Dict[str, Any]]
List of switch vectors (boolean parameter assignments)
"""
n_bool = len(self.BOOLEAN_PARAMS)
n_combinations = 2 ** n_bool
switch_vectors = []
seen_canonical = set()
for i in range(n_combinations):
# Decode integer to boolean switches
switches = {}
for j, param_name in enumerate(self.BOOLEAN_PARAMS):
switches[param_name] = bool((i >> j) & 1)
# Create canonical form (conditional params fixed to champion when parent is False)
canonical = self._canonicalize_switch_vector(switches)
canonical_key = tuple(sorted((k, v) for k, v in canonical.items() if isinstance(v, bool)))
if canonical_key not in seen_canonical:
seen_canonical.add(canonical_key)
switch_vectors.append(canonical)
return switch_vectors
def _canonicalize_switch_vector(self, switches: Dict[str, bool]) -> Dict[str, Any]:
"""
Convert a raw switch vector to canonical form.
When a parent switch is False, its conditional parameters
are set to FIXED champion values.
"""
canonical = dict(switches)
for parent, children in self.CONDITIONAL_PARAMS.items():
if not switches.get(parent, False):
# Parent is disabled - fix children to champion
for child in children:
canonical[child] = self.CHAMPION[child]
return canonical
def get_free_continuous_params(self, switch_vector: Dict[str, Any]) -> List[str]:
"""
Get list of continuous/discrete parameters that are NOT fixed
by the switch vector.
"""
free_params = []
for name, pdef in self.PARAMS.items():
if pdef.param_type in (ParamType.CONTINUOUS, ParamType.DISCRETE):
# Check if this param is fixed by any switch
is_fixed = False
for parent, children in self.CONDITIONAL_PARAMS.items():
if name in children and not switch_vector.get(parent, True):
is_fixed = True
break
if not is_fixed:
free_params.append(name)
return free_params
def sample_continuous_params(
self,
switch_vector: Dict[str, Any],
n_samples: int,
seed: int
) -> List[Dict[str, Any]]:
"""
Phase B: Generate n LHS samples for continuous/discrete parameters.
Parameters
----------
switch_vector : dict
Fixed boolean parameters
n_samples : int
Number of samples to generate
seed : int
RNG seed for this batch
Returns
-------
List[Dict[str, Any]]
List of complete parameter dicts (switch + continuous)
"""
free_params = self.get_free_continuous_params(switch_vector)
n_free = len(free_params)
if n_free == 0:
# No free parameters - just return the switch vector
return [dict(switch_vector)]
# Generate LHS samples in unit hypercube
if SCIPY_AVAILABLE:
sampler = qmc.LatinHypercube(d=n_free, seed=seed)
unit_samples = sampler.random(n=n_samples)
else:
# Fallback: random sampling with warning
print(f"[WARN] scipy not available, using random sampling instead of LHS")
rng = np.random.RandomState(seed)
unit_samples = rng.rand(n_samples, n_free)
# Scale to parameter ranges
samples = []
for i in range(n_samples):
sample = dict(switch_vector)
for j, param_name in enumerate(free_params):
pdef = self.PARAMS[param_name]
u = unit_samples[i, j]
# Handle dependent bounds
lo = pdef.lo
hi = pdef.hi
if hi is None:
# Compute dependent bound
if param_name == 'vel_div_extreme':
hi = sample['vel_div_threshold'] * 1.5
if pdef.param_type == ParamType.CONTINUOUS:
if pdef.log_transform:
# Log-space sampling: value = lo * (hi/lo) ** u
value = lo * (hi / lo) ** u
else:
# Linear sampling
value = lo + u * (hi - lo)
elif pdef.param_type == ParamType.DISCRETE:
# Discrete sampling
value = int(round(lo + u * (hi - lo)))
value = max(int(lo), min(int(hi), value))
else:
value = pdef.champion
sample[param_name] = value
samples.append(sample)
return samples
def generate_trials(
self,
n_samples_per_switch: int = 500,
max_trials: Optional[int] = None
) -> List[MCTrialConfig]:
"""
Generate all MC trial configurations.
Parameters
----------
n_samples_per_switch : int
Samples per unique switch vector
max_trials : int, optional
Maximum total trials (for testing)
Returns
-------
List[MCTrialConfig]
All trial configurations
"""
switch_vectors = self.generate_switch_vectors()
print(f"[INFO] Generated {len(switch_vectors)} unique switch vectors")
trials = []
trial_id = 0
for switch_idx, switch_vector in enumerate(switch_vectors):
# Generate seed for this switch vector
switch_seed = (self.base_seed * 1000003 + switch_idx) % 2**31
# Generate continuous samples
samples = self.sample_continuous_params(
switch_vector, n_samples_per_switch, switch_seed
)
for sample in samples:
if max_trials and trial_id >= max_trials:
break
# Fill in any missing parameters with champion values
full_params = dict(self.CHAMPION)
full_params.update(sample)
full_params['trial_id'] = trial_id
# Create trial config
try:
config = MCTrialConfig(**full_params)
trials.append(config)
trial_id += 1
except Exception as e:
print(f"[WARN] Failed to create trial {trial_id}: {e}")
if max_trials and trial_id >= max_trials:
break
print(f"[INFO] Generated {len(trials)} total trial configurations")
return trials
def generate_champion_trial(self) -> MCTrialConfig:
"""Generate the champion configuration as a single trial."""
params = dict(self.CHAMPION)
params['trial_id'] = -1 # Special ID for champion
return MCTrialConfig(**params)
def save_trials(self, trials: List[MCTrialConfig], path: Union[str, Path]):
"""Save trials to JSON."""
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
data = [t.to_dict() for t in trials]
with open(path, 'w') as f:
json.dump(data, f, indent=2)
print(f"[OK] Saved {len(trials)} trials to {path}")
def load_trials(self, path: Union[str, Path]) -> List[MCTrialConfig]:
"""Load trials from JSON."""
with open(path, 'r') as f:
data = json.load(f)
trials = [MCTrialConfig.from_dict(d) for d in data]
print(f"[OK] Loaded {len(trials)} trials from {path}")
return trials
def test_sampler():
"""Quick test of the sampler."""
sampler = MCSampler(base_seed=42)
# Test switch vector generation
switches = sampler.generate_switch_vectors()
print(f"Unique switch vectors: {len(switches)}")
# Test trial generation (small)
trials = sampler.generate_trials(n_samples_per_switch=10, max_trials=100)
print(f"Generated trials: {len(trials)}")
# Check parameter ranges
for trial in trials[:5]:
print(f"Trial {trial.trial_id}: vel_div_threshold={trial.vel_div_threshold:.4f}, "
f"max_leverage={trial.max_leverage:.2f}, use_direction_confirm={trial.use_direction_confirm}")
return trials
if __name__ == "__main__":
test_sampler()

View File

@@ -1,327 +0,0 @@
"""
Monte Carlo Result Store
========================
Persistence layer for MC trial results.
Supports:
- Parquet files for bulk data storage
- SQLite index for fast querying
- Incremental/resumable runs
- Batch organization
Reference: MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md Section 8
"""
import json
import sqlite3
from pathlib import Path
from typing import Dict, List, Optional, Any, Union
from datetime import datetime
import numpy as np
# Try to import pandas/pyarrow
try:
import pandas as pd
PANDAS_AVAILABLE = True
except ImportError:
PANDAS_AVAILABLE = False
print("[WARN] pandas not available - Parquet storage disabled")
from .mc_metrics import MCTrialResult
from .mc_validator import ValidationResult
class MCStore:
"""
Monte Carlo Result Store.
Manages persistence of trial configurations, results, and indices.
"""
def __init__(
self,
output_dir: Union[str, Path] = "mc_results",
batch_size: int = 1000
):
"""
Initialize the store.
Parameters
----------
output_dir : str or Path
Directory for all MC results
batch_size : int
Number of trials per batch file
"""
self.output_dir = Path(output_dir)
self.batch_size = batch_size
# Create directory structure
self.manifests_dir = self.output_dir / "manifests"
self.results_dir = self.output_dir / "results"
self.models_dir = self.output_dir / "models"
self.manifests_dir.mkdir(parents=True, exist_ok=True)
self.results_dir.mkdir(parents=True, exist_ok=True)
self.models_dir.mkdir(parents=True, exist_ok=True)
# SQLite index
self.index_path = self.output_dir / "mc_index.sqlite"
self._init_index()
self.current_batch = self._get_latest_batch() + 1
def _init_index(self):
"""Initialize SQLite index."""
conn = sqlite3.connect(self.index_path)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS mc_index (
trial_id INTEGER PRIMARY KEY,
batch_id INTEGER,
status TEXT,
roi_pct REAL,
profit_factor REAL,
win_rate REAL,
max_dd_pct REAL,
sharpe REAL,
n_trades INTEGER,
champion_region INTEGER,
catastrophic INTEGER,
created_at INTEGER
)
''')
# Create indices
cursor.execute('CREATE INDEX IF NOT EXISTS idx_roi ON mc_index (roi_pct)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_champion ON mc_index (champion_region)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_catastrophic ON mc_index (catastrophic)')
cursor.execute('CREATE INDEX IF NOT EXISTS idx_batch ON mc_index (batch_id)')
conn.commit()
conn.close()
def _get_latest_batch(self) -> int:
"""Get the highest batch ID in the index."""
conn = sqlite3.connect(self.index_path)
cursor = conn.cursor()
cursor.execute('SELECT MAX(batch_id) FROM mc_index')
result = cursor.fetchone()
conn.close()
return result[0] if result and result[0] else 0
def save_validation_results(self, results: List[ValidationResult], batch_id: int):
"""Save validation results to manifest."""
manifest_path = self.manifests_dir / f"batch_{batch_id:04d}_validation.json"
data = [r.to_dict() for r in results]
with open(manifest_path, 'w') as f:
json.dump(data, f, indent=2)
print(f"[OK] Saved validation manifest: {manifest_path}")
def save_trial_results(
self,
results: List[MCTrialResult],
batch_id: Optional[int] = None
):
"""
Save trial results to Parquet and update index.
Parameters
----------
results : List[MCTrialResult]
Trial results to save
batch_id : int, optional
Batch ID (auto-incremented if not provided)
"""
if batch_id is None:
batch_id = self.current_batch
self.current_batch += 1
if not results:
return
# Save to Parquet
if PANDAS_AVAILABLE:
self._save_parquet(results, batch_id)
# Update SQLite index
self._update_index(results, batch_id)
print(f"[OK] Saved batch {batch_id}: {len(results)} trials")
def _save_parquet(self, results: List[MCTrialResult], batch_id: int):
"""Save results to Parquet file."""
parquet_path = self.results_dir / f"batch_{batch_id:04d}_results.parquet"
# Convert to DataFrame
data = [r.to_dict() for r in results]
df = pd.DataFrame(data)
# Save
df.to_parquet(parquet_path, index=False, compression='zstd')
def _update_index(self, results: List[MCTrialResult], batch_id: int):
"""Update SQLite index with result summaries."""
conn = sqlite3.connect(self.index_path)
cursor = conn.cursor()
timestamp = int(datetime.now().timestamp())
for r in results:
cursor.execute('''
INSERT OR REPLACE INTO mc_index
(trial_id, batch_id, status, roi_pct, profit_factor, win_rate,
max_dd_pct, sharpe, n_trades, champion_region, catastrophic, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
r.trial_id,
batch_id,
r.status,
r.roi_pct,
r.profit_factor,
r.win_rate,
r.max_drawdown_pct,
r.sharpe_ratio,
r.n_trades,
int(r.champion_region),
int(r.catastrophic),
timestamp
))
conn.commit()
conn.close()
def query_index(
self,
status: Optional[str] = None,
min_roi: Optional[float] = None,
champion_only: bool = False,
catastrophic_only: bool = False,
limit: int = 1000
) -> List[Dict[str, Any]]:
"""
Query the SQLite index.
Parameters
----------
status : str, optional
Filter by status
min_roi : float, optional
Minimum ROI percentage
champion_only : bool
Only champion region configs
catastrophic_only : bool
Only catastrophic configs
limit : int
Maximum results
Returns
-------
List[Dict]
Matching index entries
"""
conn = sqlite3.connect(self.index_path)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
query = 'SELECT * FROM mc_index WHERE 1=1'
params = []
if status:
query += ' AND status = ?'
params.append(status)
if min_roi is not None:
query += ' AND roi_pct >= ?'
params.append(min_roi)
if champion_only:
query += ' AND champion_region = 1'
if catastrophic_only:
query += ' AND catastrophic = 1'
query += ' ORDER BY roi_pct DESC LIMIT ?'
params.append(limit)
cursor.execute(query, params)
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]
def get_corpus_stats(self) -> Dict[str, Any]:
"""Get statistics about the stored corpus."""
conn = sqlite3.connect(self.index_path)
cursor = conn.cursor()
# Total trials
cursor.execute('SELECT COUNT(*) FROM mc_index')
total = cursor.fetchone()[0]
# By status
cursor.execute('SELECT status, COUNT(*) FROM mc_index GROUP BY status')
by_status = {row[0]: row[1] for row in cursor.fetchall()}
# Champion region
cursor.execute('SELECT COUNT(*) FROM mc_index WHERE champion_region = 1')
champion_count = cursor.fetchone()[0]
# Catastrophic
cursor.execute('SELECT COUNT(*) FROM mc_index WHERE catastrophic = 1')
catastrophic_count = cursor.fetchone()[0]
# ROI stats
cursor.execute('''
SELECT AVG(roi_pct), MIN(roi_pct), MAX(roi_pct),
AVG(sharpe), AVG(max_dd_pct)
FROM mc_index WHERE status = 'completed'
''')
roi_stats = cursor.fetchone()
conn.close()
return {
'total_trials': total,
'by_status': by_status,
'champion_count': champion_count,
'catastrophic_count': catastrophic_count,
'avg_roi_pct': roi_stats[0] if roi_stats else 0,
'min_roi_pct': roi_stats[1] if roi_stats else 0,
'max_roi_pct': roi_stats[2] if roi_stats else 0,
'avg_sharpe': roi_stats[3] if roi_stats else 0,
'avg_max_dd_pct': roi_stats[4] if roi_stats else 0,
}
def load_batch(self, batch_id: int) -> Optional[pd.DataFrame]:
"""Load a batch of results from Parquet."""
if not PANDAS_AVAILABLE:
return None
parquet_path = self.results_dir / f"batch_{batch_id:04d}_results.parquet"
if not parquet_path.exists():
return None
return pd.read_parquet(parquet_path)
def load_corpus(self) -> Optional[pd.DataFrame]:
"""Load entire corpus from all batches."""
if not PANDAS_AVAILABLE:
return None
batches = []
for parquet_file in sorted(self.results_dir.glob("batch_*_results.parquet")):
df = pd.read_parquet(parquet_file)
batches.append(df)
if not batches:
return None
return pd.concat(batches, ignore_index=True)

View File

@@ -1,547 +0,0 @@
"""
Monte Carlo Configuration Validator
===================================
Internal consistency validation for all constraint groups V1-V4.
Validation Pipeline:
V1: Range check - each param within declared [lo, hi]
V2: Constraint groups - CG-VD, CG-LEV, CG-EXIT, CG-RISK, CG-ACB, etc.
V3: Cross-group check - inter-subsystem coherence
V4: Degenerate check - would produce 0 trades or infinite leverage
Reference: MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md Section 4
"""
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass
from enum import Enum
import numpy as np
from .mc_sampler import MCTrialConfig, MCSampler
class ValidationStatus(Enum):
"""Validation result status."""
VALID = "VALID"
REJECTED_V1 = "REJECTED_V1" # Range check failed
REJECTED_V2 = "REJECTED_V2" # Constraint group failed
REJECTED_V3 = "REJECTED_V3" # Cross-group check failed
REJECTED_V4 = "REJECTED_V4" # Degenerate configuration
@dataclass
class ValidationResult:
"""Result of validation."""
status: ValidationStatus
trial_id: int
reject_reason: Optional[str] = None
warnings: List[str] = None
def __post_init__(self):
if self.warnings is None:
self.warnings = []
def is_valid(self) -> bool:
"""Check if configuration is valid."""
return self.status == ValidationStatus.VALID
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
'status': self.status.value,
'trial_id': self.trial_id,
'reject_reason': self.reject_reason,
'warnings': self.warnings,
}
class MCValidator:
"""
Monte Carlo Configuration Validator.
Implements the full V1-V4 validation pipeline.
"""
def __init__(self, verbose: bool = False):
"""
Initialize validator.
Parameters
----------
verbose : bool
Print detailed validation messages
"""
self.verbose = verbose
self.sampler = MCSampler()
def validate(self, config: MCTrialConfig) -> ValidationResult:
"""
Run full validation pipeline on a configuration.
Parameters
----------
config : MCTrialConfig
Configuration to validate
Returns
-------
ValidationResult
Validation result with status and details
"""
warnings = []
# V1: Range checks
v1_passed, v1_reason = self._validate_v1_ranges(config)
if not v1_passed:
return ValidationResult(
status=ValidationStatus.REJECTED_V1,
trial_id=config.trial_id,
reject_reason=v1_reason,
warnings=warnings
)
# V2: Constraint group rules
v2_passed, v2_reason = self._validate_v2_constraint_groups(config)
if not v2_passed:
return ValidationResult(
status=ValidationStatus.REJECTED_V2,
trial_id=config.trial_id,
reject_reason=v2_reason,
warnings=warnings
)
# V3: Cross-group checks
v3_passed, v3_reason, v3_warnings = self._validate_v3_cross_group(config)
warnings.extend(v3_warnings)
if not v3_passed:
return ValidationResult(
status=ValidationStatus.REJECTED_V3,
trial_id=config.trial_id,
reject_reason=v3_reason,
warnings=warnings
)
# V4: Degenerate check (lightweight - no actual backtest)
v4_passed, v4_reason = self._validate_v4_degenerate(config)
if not v4_passed:
return ValidationResult(
status=ValidationStatus.REJECTED_V4,
trial_id=config.trial_id,
reject_reason=v4_reason,
warnings=warnings
)
return ValidationResult(
status=ValidationStatus.VALID,
trial_id=config.trial_id,
reject_reason=None,
warnings=warnings
)
def _validate_v1_ranges(self, config: MCTrialConfig) -> Tuple[bool, Optional[str]]:
"""
V1: Range checks - each param within declared [lo, hi].
"""
params = config._asdict()
for name, pdef in self.sampler.PARAMS.items():
if pdef.param_type.value in ('derived', 'fixed'):
continue
value = params.get(name)
if value is None:
return False, f"Missing parameter: {name}"
# Check lower bound
if pdef.lo is not None and value < pdef.lo:
return False, f"{name}={value} below minimum {pdef.lo}"
# Check upper bound (handle dependent bounds)
hi = pdef.hi
if hi is None and name == 'vel_div_extreme':
hi = params.get('vel_div_threshold', -0.02) * 1.5
if hi is not None and value > hi:
return False, f"{name}={value} above maximum {hi}"
return True, None
def _validate_v2_constraint_groups(self, config: MCTrialConfig) -> Tuple[bool, Optional[str]]:
"""
V2: Constraint group rules.
"""
# CG-VD: Velocity Divergence thresholds
if not self._check_cg_vd(config):
return False, "CG-VD: Velocity divergence constraints violated"
# CG-LEV: Leverage bounds
if not self._check_cg_lev(config):
return False, "CG-LEV: Leverage constraints violated"
# CG-EXIT: Exit management
if not self._check_cg_exit(config):
return False, "CG-EXIT: Exit constraints violated"
# CG-RISK: Combined risk
if not self._check_cg_risk(config):
return False, "CG-RISK: Risk cap exceeded"
# CG-DC-LEV: DC leverage adjustments
if not self._check_cg_dc_lev(config):
return False, "CG-DC-LEV: DC leverage adjustment constraints violated"
# CG-ACB: ACB beta bounds
if not self._check_cg_acb(config):
return False, "CG-ACB: ACB beta constraints violated"
# CG-SP: SmartPlacer rates
if not self._check_cg_sp(config):
return False, "CG-SP: SmartPlacer rate constraints violated"
# CG-OB-SIG: OB signal constraints
if not self._check_cg_ob_sig(config):
return False, "CG-OB-SIG: OB signal constraints violated"
return True, None
def _check_cg_vd(self, config: MCTrialConfig) -> bool:
"""CG-VD: Velocity Divergence constraints."""
# extreme < threshold (both negative; extreme is more negative)
if config.vel_div_extreme >= config.vel_div_threshold:
if self.verbose:
print(f" CG-VD fail: extreme={config.vel_div_extreme} >= threshold={config.vel_div_threshold}")
return False
# extreme >= -0.15 (below this, no bars fire at all)
if config.vel_div_extreme < -0.15:
if self.verbose:
print(f" CG-VD fail: extreme={config.vel_div_extreme} < -0.15")
return False
# threshold <= -0.005 (above this, too many spurious entries)
if config.vel_div_threshold > -0.005:
if self.verbose:
print(f" CG-VD fail: threshold={config.vel_div_threshold} > -0.005")
return False
# abs(extreme / threshold) >= 1.5 (meaningful separation)
separation = abs(config.vel_div_extreme / config.vel_div_threshold)
if separation < 1.5:
if self.verbose:
print(f" CG-VD fail: separation={separation:.2f} < 1.5")
return False
return True
def _check_cg_lev(self, config: MCTrialConfig) -> bool:
"""CG-LEV: Leverage bounds."""
# min_leverage < max_leverage
if config.min_leverage >= config.max_leverage:
if self.verbose:
print(f" CG-LEV fail: min={config.min_leverage} >= max={config.max_leverage}")
return False
# max_leverage - min_leverage >= 1.0 (meaningful range)
if config.max_leverage - config.min_leverage < 1.0:
if self.verbose:
print(f" CG-LEV fail: range={config.max_leverage - config.min_leverage:.2f} < 1.0")
return False
# max_leverage * fraction <= 2.0 (notional-capital safety cap)
notional_cap = config.max_leverage * config.fraction
if notional_cap > 2.0:
if self.verbose:
print(f" CG-LEV fail: notional_cap={notional_cap:.2f} > 2.0")
return False
return True
def _check_cg_exit(self, config: MCTrialConfig) -> bool:
"""CG-EXIT: Exit management constraints."""
tp_decimal = config.fixed_tp_pct
sl_decimal = config.stop_pct / 100.0 # Convert from percentage to decimal
# TP must be achievable before SL
if tp_decimal > sl_decimal * 5.0:
if self.verbose:
print(f" CG-EXIT fail: TP={tp_decimal:.4f} > SL*5={sl_decimal*5:.4f}")
return False
# minimum 30 bps TP
if tp_decimal < 0.0030:
if self.verbose:
print(f" CG-EXIT fail: TP={tp_decimal:.4f} < 0.0030")
return False
# minimum 20 bps SL width
if sl_decimal < 0.0020:
if self.verbose:
print(f" CG-EXIT fail: SL={sl_decimal:.4f} < 0.0020")
return False
# minimum meaningful hold period
if config.max_hold_bars < 20:
if self.verbose:
print(f" CG-EXIT fail: max_hold={config.max_hold_bars} < 20")
return False
# TP:SL ratio >= 0.10x
if sl_decimal > 0 and tp_decimal / sl_decimal < 0.10:
if self.verbose:
print(f" CG-EXIT fail: TP/SL ratio={tp_decimal/sl_decimal:.2f} < 0.10")
return False
return True
def _check_cg_risk(self, config: MCTrialConfig) -> bool:
"""CG-RISK: Combined risk constraints."""
# fraction * max_leverage <= 2.0 (mirrors CG-LEV)
max_notional_fraction = config.fraction * config.max_leverage
if max_notional_fraction > 2.0:
if self.verbose:
print(f" CG-RISK fail: max_notional={max_notional_fraction:.2f} > 2.0")
return False
# minimum meaningful position
if max_notional_fraction < 0.10:
if self.verbose:
print(f" CG-RISK fail: max_notional={max_notional_fraction:.2f} < 0.10")
return False
return True
def _check_cg_dc_lev(self, config: MCTrialConfig) -> bool:
"""CG-DC-LEV: DC leverage adjustment constraints."""
if not config.use_direction_confirm:
# DC not used - constraints don't apply
return True
# dc_leverage_boost >= 1.0 (must boost, not reduce)
if config.dc_leverage_boost < 1.0:
if self.verbose:
print(f" CG-DC-LEV fail: boost={config.dc_leverage_boost:.2f} < 1.0")
return False
# dc_leverage_reduce < 1.0 (must reduce, not boost)
if config.dc_leverage_reduce >= 1.0:
if self.verbose:
print(f" CG-DC-LEV fail: reduce={config.dc_leverage_reduce:.2f} >= 1.0")
return False
# DC swing bounded: boost * (1/reduce) <= 4.0
dc_swing = config.dc_leverage_boost * (1.0 / config.dc_leverage_reduce)
if dc_swing > 4.0:
if self.verbose:
print(f" CG-DC-LEV fail: dc_swing={dc_swing:.2f} > 4.0")
return False
return True
def _check_cg_acb(self, config: MCTrialConfig) -> bool:
"""CG-ACB: ACB beta bounds."""
# acb_beta_low < acb_beta_high
if config.acb_beta_low >= config.acb_beta_high:
if self.verbose:
print(f" CG-ACB fail: low={config.acb_beta_low:.2f} >= high={config.acb_beta_high:.2f}")
return False
# acb_beta_high - acb_beta_low >= 0.20 (meaningful dynamic range)
if config.acb_beta_high - config.acb_beta_low < 0.20:
if self.verbose:
print(f" CG-ACB fail: range={config.acb_beta_high - config.acb_beta_low:.2f} < 0.20")
return False
# acb_beta_high <= 1.50 (cap at 150%)
if config.acb_beta_high > 1.50:
if self.verbose:
print(f" CG-ACB fail: high={config.acb_beta_high:.2f} > 1.50")
return False
return True
def _check_cg_sp(self, config: MCTrialConfig) -> bool:
"""CG-SP: SmartPlacer rate constraints."""
if not config.use_sp_slippage:
# Slippage disabled - rates don't matter
return True
# Rates must be in [0, 1]
if not (0.0 <= config.sp_maker_entry_rate <= 1.0):
if self.verbose:
print(f" CG-SP fail: entry_rate={config.sp_maker_entry_rate:.2f} not in [0,1]")
return False
if not (0.0 <= config.sp_maker_exit_rate <= 1.0):
if self.verbose:
print(f" CG-SP fail: exit_rate={config.sp_maker_exit_rate:.2f} not in [0,1]")
return False
return True
def _check_cg_ob_sig(self, config: MCTrialConfig) -> bool:
"""CG-OB-SIG: OB signal constraints."""
# ob_imbalance_bias in [-1.0, 1.0]
if not (-1.0 <= config.ob_imbalance_bias <= 1.0):
if self.verbose:
print(f" CG-OB-SIG fail: bias={config.ob_imbalance_bias:.2f} not in [-1,1]")
return False
# ob_depth_scale > 0
if config.ob_depth_scale <= 0:
if self.verbose:
print(f" CG-OB-SIG fail: depth_scale={config.ob_depth_scale:.2f} <= 0")
return False
return True
def _validate_v3_cross_group(
self, config: MCTrialConfig
) -> Tuple[bool, Optional[str], List[str]]:
"""
V3: Cross-group coherence checks.
Returns (passed, reason, warnings).
"""
warnings = []
# Signal threshold vs exit: TP must be achievable before max_hold_bars expires
# Approximate: at typical vol, price moves ~0.03% per 5s bar
expected_tp_bars = config.fixed_tp_pct / 0.0003
if expected_tp_bars > config.max_hold_bars * 3:
warnings.append(
f"TP_TIME_RISK: expected_tp_bars={expected_tp_bars:.0f} > max_hold*3={config.max_hold_bars*3}"
)
# Leverage convexity vs range: extreme convexity with wide leverage range
# produces near-binary leverage
if config.leverage_convexity > 5.0 and (config.max_leverage - config.min_leverage) > 5.0:
warnings.append(
f"HIGH_CONVEXITY_WIDE_RANGE: near-binary leverage behaviour likely"
)
# OB skip + DC skip double-filtering: very few trades may fire
if config.dc_skip_contradicts and config.ob_imbalance_bias > 0.15:
warnings.append(
f"DOUBLE_FILTER_RISK: DC skip + strong OB contradiction may starve trades"
)
# Reject only on critical cross-group violations
# (none currently defined - all are warnings)
return True, None, warnings
def _validate_v4_degenerate(self, config: MCTrialConfig) -> Tuple[bool, Optional[str]]:
"""
V4: Degenerate configuration check (lightweight heuristics).
Full pre-flight with 500 bars is done in mc_executor during actual trial.
This is just a quick sanity check.
"""
# Check for numerical extremes that would cause issues
# Fraction too small - would produce micro-positions
if config.fraction < 0.02:
return False, f"FRACTION_TOO_SMALL: fraction={config.fraction} < 0.02"
# Leverage range too narrow for convexity to matter
leverage_range = config.max_leverage - config.min_leverage
if leverage_range < 0.5 and config.leverage_convexity > 2.0:
return False, f"NARROW_RANGE_HIGH_CONVEXITY: range={leverage_range:.2f}, convexity={config.leverage_convexity:.2f}"
# Max hold too short for vol filter to stabilize
if config.max_hold_bars < config.vd_trend_lookback + 10:
return False, f"HOLD_TOO_SHORT: max_hold={config.max_hold_bars} < trend_lookback+10={config.vd_trend_lookback+10}"
# IRP lookback too short for meaningful alignment
if config.lookback < 50:
return False, f"LOOKBACK_TOO_SHORT: lookback={config.lookback} < 50"
return True, None
def validate_batch(
self,
configs: List[MCTrialConfig]
) -> List[ValidationResult]:
"""
Validate a batch of configurations.
Parameters
----------
configs : List[MCTrialConfig]
Configurations to validate
Returns
-------
List[ValidationResult]
Validation results (same order as input)
"""
results = []
for config in configs:
result = self.validate(config)
results.append(result)
return results
def get_validity_stats(self, results: List[ValidationResult]) -> Dict[str, Any]:
"""
Get statistics about validation results.
"""
total = len(results)
if total == 0:
return {'total': 0}
by_status = {}
for status in ValidationStatus:
by_status[status.value] = sum(1 for r in results if r.status == status)
rejection_reasons = {}
for r in results:
if r.reject_reason:
reason = r.reject_reason.split(':')[0] if ':' in r.reject_reason else r.reject_reason
rejection_reasons[reason] = rejection_reasons.get(reason, 0) + 1
return {
'total': total,
'valid': by_status.get(ValidationStatus.VALID.value, 0),
'rejected_v1': by_status.get(ValidationStatus.REJECTED_V1.value, 0),
'rejected_v2': by_status.get(ValidationStatus.REJECTED_V2.value, 0),
'rejected_v3': by_status.get(ValidationStatus.REJECTED_V3.value, 0),
'rejected_v4': by_status.get(ValidationStatus.REJECTED_V4.value, 0),
'validity_rate': by_status.get(ValidationStatus.VALID.value, 0) / total,
'rejection_reasons': rejection_reasons,
}
def test_validator():
"""Quick test of the validator."""
validator = MCValidator(verbose=True)
sampler = MCSampler(base_seed=42)
# Generate some test configurations
trials = sampler.generate_trials(n_samples_per_switch=10, max_trials=100)
# Validate
results = validator.validate_batch(trials)
# Stats
stats = validator.get_validity_stats(results)
print(f"\nValidation Stats:")
print(f" Total: {stats['total']}")
print(f" Valid: {stats['valid']} ({stats['validity_rate']*100:.1f}%)")
print(f" Rejected V1: {stats['rejected_v1']}")
print(f" Rejected V2: {stats['rejected_v2']}")
print(f" Rejected V3: {stats['rejected_v3']}")
print(f" Rejected V4: {stats['rejected_v4']}")
# Show some rejections
print("\nSample Rejections:")
for r in results:
if not r.is_valid():
print(f" Trial {r.trial_id}: {r.status.value} - {r.reject_reason}")
if len([x for x in results if not x.is_valid()]) > 5:
break
return results
if __name__ == "__main__":
test_validator()

View File

@@ -1,113 +0,0 @@
"""
Live Monte Carlo Forewarning Service
====================================
Continously monitors the active Nautilus-Dolphin configuration
against the pre-trained Monte Carlo operational envelope.
Logs warnings and generates alerts if the parameters drift near
the edge of the validated MC envelope, preventing catastrophic swans.
"""
import os
import sys
import time
import json
import logging
from pathlib import Path
from datetime import datetime
# Adjust paths
PROJECT_ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(PROJECT_ROOT))
sys.path.insert(0, str(PROJECT_ROOT.parent / 'external_factors'))
from mc.mc_ml import DolphinForewarner
from mc.mc_sampler import MCSampler
# Configure Logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - [FOREWARNER] - %(levelname)s - %(message)s",
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler(PROJECT_ROOT / "forewarning_service.log")
]
)
MODELS_DIR = PROJECT_ROOT / "mc_results" / "models"
CHECK_INTERVAL_SECONDS = 3600 * 4 # Check every 4 hours
def get_current_live_config() -> dict:
"""
Simulates fetching the active trading system configuration.
In full production, this would query Nautilus' live dictionary.
For now, it pulls the baseline champion and applies any overrides.
"""
sampler = MCSampler()
# Baseline champion config
raw_config = sampler.generate_champion_trial().to_dict()
# In a fully dynamic environment, we would overlay real-time changes
# For demonstration, we simply return the dict
return raw_config
def determine_risk_level(report):
"""
Assess risk level per MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md mapping.
"""
env = report.envelope_score
cat = report.catastrophic_probability
champ = report.champion_probability
if cat > 0.25 or env < -1.0:
return "RED"
elif env < 0 or cat > 0.10:
return "ORANGE"
elif env > 0 and champ > 0.4:
return "AMBER"
elif env > 0.5 and champ > 0.6:
return "GREEN"
else:
return "AMBER" # Default transitional state
def run_service():
logging.info(f"Starting Monte Carlo Forewarning Service. Checking every {CHECK_INTERVAL_SECONDS} seconds.")
if not MODELS_DIR.exists():
logging.error(f"Models directory not found at {MODELS_DIR}. Ensure you've run 'python run_mc_envelope.py --mode train' first.")
sys.exit(1)
try:
forewarner = DolphinForewarner(models_dir=str(MODELS_DIR))
except Exception as e:
logging.error(f"Failed to load ML models: {e}")
sys.exit(1)
while True:
try:
config_dict = get_current_live_config()
report = forewarner.assess_config_dict(config_dict)
level = determine_risk_level(report)
log_msg = f"Check complete. Risk Level: {level} | Env_Score: {report.envelope_score:.3f} | Cat_Prob: {report.catastrophic_probability:.1%}"
if level in ['ORANGE', 'RED']:
logging.warning("!!! HIGH RISK CONFIGURATION DETECTED !!!")
logging.warning(log_msg)
if report.warnings:
for w in report.warnings:
logging.warning(f" -> {w}")
else:
logging.info(log_msg)
except Exception as e:
logging.error(f"Error during assessment loop: {e}")
# Sleep till next cycle
time.sleep(CHECK_INTERVAL_SECONDS)
if __name__ == "__main__":
try:
run_service()
except KeyboardInterrupt:
logging.info("Forewarning service shutting down.")

View File

@@ -1,370 +0,0 @@
"""
Monte Carlo Envelope Mapper CLI
===============================
Command-line interface for running Monte Carlo envelope mapping
of the Nautilus-Dolphin trading system.
Usage:
python run_mc_envelope.py --mode run --stage 1 --n-samples 500
python run_mc_envelope.py --mode train --output-dir mc_results/
python run_mc_envelope.py --mode assess --assess my_config.json
Reference: MONTE_CARLO_SYSTEM_ENVELOPE_SPEC.md Section 11
"""
import argparse
import json
import sys
from pathlib import Path
# Add parent to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))
def create_parser() -> argparse.ArgumentParser:
"""Create argument parser."""
parser = argparse.ArgumentParser(
description="Monte Carlo System Envelope Mapper for DOLPHIN NG",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Run full envelope mapping
python run_mc_envelope.py --mode run --n-samples 500 --n-workers 7
# Train ML models on completed results
python run_mc_envelope.py --mode train
# Assess a configuration file
python run_mc_envelope.py --mode assess --assess config.json
# Generate summary report
python run_mc_envelope.py --mode report
"""
)
parser.add_argument(
'--mode',
choices=['sample', 'validate', 'run', 'train', 'assess', 'report'],
default='run',
help='Operation mode (default: run)'
)
parser.add_argument(
'--n-samples',
type=int,
default=500,
help='Samples per switch vector (default: 500)'
)
parser.add_argument(
'--n-workers',
type=int,
default=-1,
help='Parallel workers (-1 for auto, default: auto)'
)
parser.add_argument(
'--batch-size',
type=int,
default=1000,
help='Trials per batch file (default: 1000)'
)
parser.add_argument(
'--output-dir',
type=str,
default='mc_results',
help='Results directory (default: mc_results/)'
)
parser.add_argument(
'--stage',
type=int,
choices=[1, 2],
default=1,
help='Stage: 1=reduced, 2=full (default: 1)'
)
parser.add_argument(
'--seed',
type=int,
default=42,
help='Master RNG seed (default: 42)'
)
parser.add_argument(
'--config',
type=str,
help='JSON config file for parameter overrides'
)
parser.add_argument(
'--resume',
action='store_true',
help='Resume from existing results'
)
parser.add_argument(
'--assess',
type=str,
help='JSON file with config to assess (for mode=assess)'
)
parser.add_argument(
'--max-trials',
type=int,
help='Maximum total trials (for testing)'
)
parser.add_argument(
'--quiet',
action='store_true',
help='Reduce output verbosity'
)
return parser
def cmd_sample(args):
"""Sample configurations only."""
from mc import MCSampler
print("="*70)
print("MONTE CARLO CONFIGURATION SAMPLER")
print("="*70)
sampler = MCSampler(base_seed=args.seed)
print(f"\nGenerating trials (n_samples_per_switch={args.n_samples})...")
trials = sampler.generate_trials(
n_samples_per_switch=args.n_samples,
max_trials=args.max_trials
)
# Save
output_path = Path(args.output_dir) / "manifests" / "all_configs.json"
sampler.save_trials(trials, output_path)
print(f"\n[OK] Generated and saved {len(trials)} configurations")
return 0
def cmd_validate(args):
"""Validate configurations."""
from mc import MCSampler, MCValidator
print("="*70)
print("MONTE CARLO CONFIGURATION VALIDATOR")
print("="*70)
# Load configurations
config_path = Path(args.output_dir) / "manifests" / "all_configs.json"
if not config_path.exists():
print(f"[ERROR] Configurations not found: {config_path}")
print("Run with --mode sample first")
return 1
sampler = MCSampler()
trials = sampler.load_trials(config_path)
print(f"\nValidating {len(trials)} configurations...")
validator = MCValidator(verbose=not args.quiet)
results = validator.validate_batch(trials)
# Stats
stats = validator.get_validity_stats(results)
print(f"\n{'='*70}")
print("VALIDATION RESULTS")
print(f"{'='*70}")
print(f"Total: {stats['total']}")
print(f"Valid: {stats['valid']} ({stats['validity_rate']*100:.1f}%)")
print(f"Rejected V1 (range): {stats.get('rejected_v1', 0)}")
print(f"Rejected V2 (constraints): {stats.get('rejected_v2', 0)}")
print(f"Rejected V3 (cross-group): {stats.get('rejected_v3', 0)}")
print(f"Rejected V4 (degenerate): {stats.get('rejected_v4', 0)}")
# Save validation results
output_path = Path(args.output_dir) / "manifests" / "validation_results.json"
with open(output_path, 'w') as f:
json.dump([r.to_dict() for r in results], f, indent=2)
print(f"\n[OK] Validation results saved: {output_path}")
return 0
def cmd_run(args):
"""Run full envelope mapping."""
from mc import MCRunner
print("="*70)
print("MONTE CARLO ENVELOPE MAPPER")
print("="*70)
print(f"Mode: {'Stage 1 (reduced)' if args.stage == 1 else 'Stage 2 (full)'}")
print(f"Samples per switch: {args.n_samples}")
print(f"Workers: {args.n_workers if args.n_workers > 0 else 'auto'}")
print(f"Output: {args.output_dir}")
print(f"Seed: {args.seed}")
print(f"Resume: {args.resume}")
print("="*70)
runner = MCRunner(
output_dir=args.output_dir,
n_workers=args.n_workers,
batch_size=args.batch_size,
base_seed=args.seed,
verbose=not args.quiet
)
stats = runner.run_envelope_mapping(
n_samples_per_switch=args.n_samples,
max_trials=args.max_trials,
resume=args.resume
)
# Save stats
stats_path = Path(args.output_dir) / "run_stats.json"
with open(stats_path, 'w') as f:
json.dump(stats, f, indent=2, default=str)
print(f"\n[OK] Run complete. Stats saved: {stats_path}")
return 0
def cmd_train(args):
"""Train ML models."""
from mc import MCML
print("="*70)
print("MONTE CARLO ML TRAINER")
print("="*70)
ml = MCML(output_dir=args.output_dir)
try:
results = ml.train_all_models()
print("\n[OK] Training complete")
return 0
except Exception as e:
print(f"\n[ERROR] Training failed: {e}")
import traceback
traceback.print_exc()
return 1
def cmd_assess(args):
"""Assess a configuration."""
from mc import DolphinForewarner, MCTrialConfig
if not args.assess:
print("[ERROR] --assess flag required with path to config JSON")
return 1
config_path = Path(args.assess)
if not config_path.exists():
print(f"[ERROR] Config file not found: {config_path}")
return 1
print("="*70)
print("DOLPHIN FOREWARNING ASSESSMENT")
print("="*70)
# Load config
with open(config_path, 'r') as f:
config_dict = json.load(f)
# Create forewarner
forewarner = DolphinForewarner(models_dir=f"{args.output_dir}/models")
# Assess
if 'trial_id' in config_dict:
config = MCTrialConfig.from_dict(config_dict)
else:
# Assume flat config
config = MCTrialConfig(**config_dict)
report = forewarner.assess(config)
# Print report
print(f"\nConfiguration:")
print(f" vel_div_threshold: {config.vel_div_threshold}")
print(f" max_leverage: {config.max_leverage}")
print(f" fraction: {config.fraction}")
print(f"\nPredictions:")
print(f" ROI: {report.predicted_roi:.2f}%")
print(f" Max DD: {report.predicted_max_dd:.2f}%")
print(f" Champion probability: {report.champion_probability:.1%}")
print(f" Catastrophic probability: {report.catastrophic_probability:.1%}")
print(f" Envelope score: {report.envelope_score:.2f}")
print(f"\nWarnings:")
if report.warnings:
for w in report.warnings:
print(f" ! {w}")
else:
print(" (none)")
# Save report
report_path = Path(args.output_dir) / "forewarning_report.json"
with open(report_path, 'w') as f:
json.dump(report.to_dict(), f, indent=2, default=str)
print(f"\n[OK] Report saved: {report_path}")
return 0
def cmd_report(args):
"""Generate summary report."""
from mc import MCRunner
print("="*70)
print("MONTE CARLO REPORT GENERATOR")
print("="*70)
runner = MCRunner(output_dir=args.output_dir)
report = runner.generate_report(
output_path=f"{args.output_dir}/envelope_report.md"
)
print(report)
return 0
def main():
"""Main entry point."""
parser = create_parser()
args = parser.parse_args()
# Dispatch
try:
if args.mode == 'sample':
return cmd_sample(args)
elif args.mode == 'validate':
return cmd_validate(args)
elif args.mode == 'run':
return cmd_run(args)
elif args.mode == 'train':
return cmd_train(args)
elif args.mode == 'assess':
return cmd_assess(args)
elif args.mode == 'report':
return cmd_report(args)
else:
print(f"[ERROR] Unknown mode: {args.mode}")
return 1
except KeyboardInterrupt:
print("\n\n[INTERRUPTED] Stopping...")
return 130
except Exception as e:
print(f"\n[ERROR] {e}")
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(main())

View File

@@ -1,224 +0,0 @@
import sys, time
from pathlib import Path
import numpy as np
import pandas as pd
import json
sys.path.insert(0, str(Path(__file__).parent))
from nautilus_dolphin.nautilus.alpha_orchestrator import NDAlphaEngine
from nautilus_dolphin.nautilus.adaptive_circuit_breaker import AdaptiveCircuitBreaker
from nautilus_dolphin.nautilus.ob_features import OBFeatureEngine
from nautilus_dolphin.nautilus.ob_provider import MockOBProvider
VBT_DIR = Path(r"C:\Users\Lenovo\Documents\- DOLPHIN NG HD HCM TSF Predict\vbt_cache")
META_COLS = {'timestamp', 'scan_number', 'v50_lambda_max_velocity', 'v150_lambda_max_velocity',
'v300_lambda_max_velocity', 'v750_lambda_max_velocity', 'vel_div',
'instability_50', 'instability_150'}
parquet_files = sorted(VBT_DIR.glob("*.parquet"))
parquet_files = [p for p in parquet_files if 'catalog' not in str(p)]
print("Loading data...")
all_vols = []
for pf in parquet_files[:2]:
df = pd.read_parquet(pf)
if 'BTCUSDT' not in df.columns: continue
pr = df['BTCUSDT'].values
for i in range(60, len(pr)):
seg = pr[max(0,i-50):i]
if len(seg)<10: continue
v = float(np.std(np.diff(seg)/seg[:-1]))
if v > 0: all_vols.append(v)
vol_p60 = float(np.percentile(all_vols, 60))
pq_data = {}
for pf in parquet_files:
df = pd.read_parquet(pf)
ac = [c for c in df.columns if c not in META_COLS]
bp = df['BTCUSDT'].values if 'BTCUSDT' in df.columns else None
dv = np.full(len(df), np.nan)
if bp is not None:
for i in range(50, len(bp)):
seg = bp[max(0,i-50):i]
if len(seg)<10: continue
dv[i] = float(np.std(np.diff(seg)/seg[:-1]))
pq_data[pf.stem] = (df, ac, dv)
# Initialize systems
acb = AdaptiveCircuitBreaker()
acb.preload_w750([pf.stem for pf in parquet_files])
mock = MockOBProvider(imbalance_bias=-0.09, depth_scale=1.0,
assets=["BTCUSDT", "ETHUSDT", "BNBUSDT", "SOLUSDT"],
imbalance_biases={"BNBUSDT": 0.20, "SOLUSDT": 0.20})
ob_engine = OBFeatureEngine(mock)
ob_engine.preload_date("mock", mock.get_assets())
def run_base_backtest(lev_multiplier):
ENGINE_KWARGS = dict(
initial_capital=25000.0, vel_div_threshold=-0.02, vel_div_extreme=-0.05,
min_leverage=0.5, max_leverage=5.0 * lev_multiplier, leverage_convexity=3.0,
fraction=0.20, fixed_tp_pct=0.0099, stop_pct=1.0, max_hold_bars=120,
use_direction_confirm=True, dc_lookback_bars=7, dc_min_magnitude_bps=0.75,
dc_skip_contradicts=True, dc_leverage_boost=1.0, dc_leverage_reduce=0.5,
use_asset_selection=True, min_irp_alignment=0.45,
use_sp_fees=True, use_sp_slippage=True,
use_ob_edge=True, ob_edge_bps=5.0, ob_confirm_rate=0.40,
lookback=100, use_alpha_layers=True, use_dynamic_leverage=True, seed=42,
)
import gc
gc.collect()
engine = NDAlphaEngine(**ENGINE_KWARGS)
engine.set_ob_engine(ob_engine)
bar_idx = 0; peak_cap = engine.capital; max_dd = 0.0
# Store daily returns for MC bootstrapping
daily_returns = []
for pf in parquet_files:
ds = pf.stem
cs = engine.capital
# ACB logic
acb_info = acb.get_dynamic_boost_for_date(ds, ob_engine=ob_engine)
base_boost = acb_info['boost']
beta = acb_info['beta']
df, acols, dvol = pq_data[ds]
ph = {}
for ri in range(len(df)):
row = df.iloc[ri]; vd = row.get("vel_div")
if vd is None or not np.isfinite(vd): bar_idx+=1; continue
prices = {}
for ac in acols:
p = row[ac]
if p and p > 0 and np.isfinite(p):
prices[ac] = float(p)
if ac not in ph: ph[ac] = []
ph[ac].append(float(p))
if len(ph[ac]) > 500: ph[ac] = ph[ac][-200:]
if not prices: bar_idx+=1; continue
vrok = False if ri < 100 else (np.isfinite(dvol[ri]) and dvol[ri] > vol_p60)
# Use beta strictly for meta-boost
if beta > 0:
ss = 0.0
if vd < -0.02:
raw = (-0.02 - float(vd)) / (-0.02 - -0.05)
ss = min(1.0, max(0.0, raw)) ** 3.0
engine.regime_size_mult = base_boost * (1.0 + beta * ss)
else:
engine.regime_size_mult = base_boost
engine.process_bar(bar_idx=bar_idx, vel_div=float(vd), prices=prices, vol_regime_ok=vrok, price_histories=ph)
bar_idx += 1
peak_cap = max(peak_cap, engine.capital)
dd = (peak_cap - engine.capital) / peak_cap
max_dd = max(max_dd, dd)
daily_returns.append((engine.capital - cs) / cs if cs > 0 else 0)
trades = engine.trade_history
w = [t for t in trades if t.pnl_absolute > 0]
l = [t for t in trades if t.pnl_absolute <= 0]
gw = sum(t.pnl_absolute for t in w) if w else 0
gl = abs(sum(t.pnl_absolute for t in l)) if l else 0
roi = (engine.capital - 25000) / 25000 * 100
pf_val = gw / gl if gl > 0 else 999
wr = len(w) / len(trades) * 100 if trades else 0
return {
'leverage': 5.0 * lev_multiplier,
'roi': roi,
'pf': pf_val,
'wr': wr,
'max_dd': max_dd * 100,
'trades': len(trades),
'daily_returns': np.array(daily_returns)
}
def run_monte_carlo(base_results, n_simulations=1000, periods=365):
"""
Run geometric Monte Carlo bootstrapping using historical daily returns.
"""
np.random.seed(42)
daily_returns = base_results['daily_returns']
n_days = len(daily_returns)
# Bootstrap sampling for n_simulations trajectories of length `periods`
# Randomly sample historical daily returns with replacement to generate realistic synthetic years
simulated_returns = np.random.choice(daily_returns, size=(n_simulations, periods), replace=True)
# Calculate equity curves (geometric compounding)
# Adding 1.0 to get multiplier for cumulative product
equity_curves = np.cumprod(1.0 + simulated_returns, axis=1)
# CAGR calculations
final_multipliers = equity_curves[:, -1]
# CAGR = (End/Start)^(1/Years) - 1. We simulate 1 year, so exponent is 1.
cagrs = (final_multipliers - 1.0) * 100
median_cagr = np.median(cagrs)
p05_cagr = np.percentile(cagrs, 5) # 5th percentile worst outcome
# Calculate Max Drawdowns for each simulated trajectory
max_dds = np.zeros(n_simulations)
recovery_times = np.zeros(n_simulations)
for i in range(n_simulations):
curve = equity_curves[i]
peaks = np.maximum.accumulate(curve)
drawdowns = (peaks - curve) / peaks
max_dd_idx = np.argmax(drawdowns)
max_dds[i] = drawdowns[max_dd_idx]
# Calculate time to recovery from max drawdown
if drawdowns[max_dd_idx] > 0:
peak_val = peaks[max_dd_idx]
# Find first index after max drawdown where equity hits or exceeds the peak
recovery_idx = -1
for j in range(max_dd_idx, periods):
if curve[j] >= peak_val:
recovery_idx = j
break
if recovery_idx != -1:
recovery_times[i] = recovery_idx - max_dd_idx
else:
recovery_times[i] = periods - max_dd_idx # Did not recover within period
median_max_dd = np.median(max_dds) * 100
median_recovery = np.median(recovery_times[recovery_times > 0]) if np.any(recovery_times > 0) else -1
return {
'median_cagr': median_cagr,
'p05_cagr': p05_cagr,
'median_max_dd': median_max_dd,
'median_recovery_days': median_recovery,
'prob_ruin_50': np.mean(max_dds >= 0.50) * 100 # Prob of 50% DD
}
print("\n" + "="*80)
print("GEOMETRIC MONTE CARLO DRAG SIMULATION (1000 Trajectories / 1 Year)")
print("="*80)
print(f"{'Lev':<5} | {'Base ROI':<10} | {'Base DD':<10} | {'Base PF':<8} | {'Med CAGR':<10} | {'5th% CAGR':<10} | {'Med MC DD':<10} | {'Recovery':<10} | {'Risk > 50% DD'}")
print("-" * 80)
results = []
for mult in [1.0, 1.2, 1.4]: # 5x, 6x, 7x
lev = 5.0 * mult
# Get empirical sequence first
base = run_base_backtest(mult)
# Run MC on the empirical sequence
mc = run_monte_carlo(base, n_simulations=1000, periods=365)
print(f"{lev:<4.1f}x | {base['roi']:>+9.2f}% | {base['max_dd']:>9.2f}% | {base['pf']:>7.3f} | " +
f"{mc['median_cagr']:>+9.2f}% | {mc['p05_cagr']:>+9.2f}% | {mc['median_max_dd']:>9.2f}% | " +
f"{mc['median_recovery_days']:>7.0f} d | {mc['prob_ruin_50']:>11.1f}%")

View File

@@ -1,523 +0,0 @@
"""
Test Suite for QLabs-Enhanced MC Forewarning System
===================================================
Comprehensive tests for:
1. Individual QLabs ML techniques
2. End-to-end ML model training
3. E2E forewarning system performance
4. Comparison with baseline MCML
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import unittest
import numpy as np
import json
from pathlib import Path
from typing import Dict, Any
# Import MC modules
from mc.mc_sampler import MCSampler, MCTrialConfig
from mc.mc_metrics import MCTrialResult, MCMetrics
from mc.mc_ml import MCML, DolphinForewarner
from mc.mc_ml_qlabs import (
MCMLQLabs, DolphinForewarnerQLabs, MuonOptimizer,
SwiGLU, UNetMLP, DeepEnsemble, QLabsHyperParams
)
class TestMuonOptimizer(unittest.TestCase):
"""Test QLabs Technique #1: Muon Optimizer"""
def test_newton_schulz_orthogonalization(self):
"""Test that Newton-Schulz produces near-orthogonal matrices."""
optimizer = MuonOptimizer()
# Create random matrix
X = np.random.randn(10, 8)
# Orthogonalize
X_ortho = optimizer.newton_schulz(X)
# Check orthogonality: X^T @ X should be close to identity
if X.shape[0] >= X.shape[1]:
gram = X_ortho.T @ X_ortho
else:
gram = X_ortho @ X_ortho.T
# Check diagonal is close to 1, off-diagonal close to 0
diag_mean = np.mean(np.diag(gram))
off_diag_mean = np.mean(np.abs(gram - np.eye(gram.shape[0])))
self.assertGreater(diag_mean, 0.8, "Diagonal should be close to 1")
self.assertLess(off_diag_mean, 0.3, "Off-diagonal should be close to 0")
def test_compute_update_shape(self):
"""Test that Muon update has correct shape."""
optimizer = MuonOptimizer()
grad = np.random.randn(10, 8)
param = np.random.randn(10, 8)
update = optimizer.compute_update(grad, param)
self.assertEqual(update.shape, param.shape)
def test_momentum_accumulation(self):
"""Test that momentum accumulates over steps."""
optimizer = MuonOptimizer(momentum=0.9)
grad1 = np.random.randn(5, 4)
grad2 = np.random.randn(5, 4)
param = np.random.randn(5, 4)
# First update
update1 = optimizer.compute_update(grad1, param)
# Second update
update2 = optimizer.compute_update(grad2, param)
# Momentum buffer should have history
self.assertIsNotNone(optimizer.momentum_buffer)
self.assertEqual(optimizer.step_count, 2)
class TestSwiGLU(unittest.TestCase):
"""Test QLabs Technique #4: SwiGLU Activation"""
def test_swiglu_output_shape(self):
"""Test SwiGLU output shape."""
batch_size = 32
input_dim = 64
hidden_dim = 128
x = np.random.randn(batch_size, input_dim)
gate = np.random.randn(input_dim, hidden_dim)
up = np.random.randn(input_dim, hidden_dim)
output = SwiGLU.forward(x, gate, up)
self.assertEqual(output.shape, (batch_size, hidden_dim))
def test_swiglu_gating_effect(self):
"""Test that gating modulates the output."""
x = np.random.randn(10, 20)
gate = np.random.randn(20, 30)
up = np.random.randn(20, 30)
# Forward pass
output = SwiGLU.forward(x, gate, up)
# Output should not be zero
self.assertFalse(np.allclose(output, 0))
# Output should be finite
self.assertTrue(np.all(np.isfinite(output)))
class TestUNetMLP(unittest.TestCase):
"""Test QLabs Technique #5: U-Net Skip Connections"""
def test_unet_initialization(self):
"""Test U-Net initializes correctly."""
unet = UNetMLP(
input_dim=33,
hidden_dims=[64, 32],
output_dim=1,
use_swiglu=True
)
self.assertEqual(unet.input_dim, 33)
self.assertEqual(len(unet.hidden_dims), 2)
self.assertIn('enc_gate_0', unet.weights)
def test_unet_forward(self):
"""Test U-Net forward pass."""
unet = UNetMLP(
input_dim=33,
hidden_dims=[64, 32],
output_dim=1,
use_swiglu=False # Simpler for testing
)
batch_size = 16
x = np.random.randn(batch_size, 33)
output = unet.forward(x)
self.assertEqual(output.shape, (batch_size, 1))
self.assertTrue(np.all(np.isfinite(output)))
def test_unet_skip_connections(self):
"""Test that skip connections preserve information."""
unet = UNetMLP(
input_dim=33,
hidden_dims=[64, 32],
output_dim=1,
use_swiglu=False
)
x = np.random.randn(8, 33)
# Forward pass
output = unet.forward(x)
# Skip weights should exist
self.assertIn('skip_0', unet.weights)
self.assertIn('skip_1', unet.weights)
class TestDeepEnsemble(unittest.TestCase):
"""Test QLabs Technique #6: Deep Ensembling"""
def test_ensemble_initialization(self):
"""Test ensemble initializes with correct number of models."""
from sklearn.linear_model import LinearRegression
ensemble = DeepEnsemble(
LinearRegression,
n_models=5,
seeds=[1, 2, 3, 4, 5]
)
self.assertEqual(ensemble.n_models, 5)
self.assertEqual(len(ensemble.seeds), 5)
def test_ensemble_fit_predict(self):
"""Test ensemble fitting and prediction."""
from sklearn.linear_model import Ridge
# Generate synthetic data
np.random.seed(42)
X = np.random.randn(100, 5)
y = X[:, 0] + 2*X[:, 1] + np.random.randn(100) * 0.1
ensemble = DeepEnsemble(
Ridge,
n_models=3,
seeds=[1, 2, 3]
)
ensemble.fit(X, y, alpha=1.0)
# Predict
X_test = np.random.randn(10, 5)
mean_pred, std_pred = ensemble.predict_regression(X_test)
self.assertEqual(mean_pred.shape, (10,))
self.assertEqual(std_pred.shape, (10,))
self.assertTrue(np.all(std_pred >= 0)) # Std should be non-negative
class TestQLabsHyperParams(unittest.TestCase):
"""Test QLabs Technique #2: Heavy Regularization"""
def test_heavy_regularization_values(self):
"""Test that QLabs hyperparameters use heavy regularization."""
params = QLabsHyperParams()
# XGBoost regularization should be high (QLabs: 1.6)
self.assertEqual(params.xgb_reg_lambda, 1.6)
# Min samples should be higher than sklearn defaults
self.assertGreater(params.gb_min_samples_leaf, 1)
self.assertGreater(params.gb_min_samples_split, 2)
# Dropout should be set
self.assertGreater(params.dropout, 0)
def test_epoch_shuffling_config(self):
"""Test epoch shuffling configuration."""
params = QLabsHyperParams()
# Should have early stopping configured
self.assertGreater(params.early_stopping_rounds, 0)
class TestMCMLQLabs(unittest.TestCase):
"""Test QLabs-enhanced MCML system"""
def setUp(self):
"""Set up test fixtures."""
self.output_dir = "mc_forewarning_qlabs_fork/results/test_mcml_qlabs"
Path(self.output_dir).mkdir(parents=True, exist_ok=True)
def test_initialization(self):
"""Test QLabs ML trainer initializes correctly."""
ml = MCMLQLabs(
output_dir=self.output_dir,
use_ensemble=True,
n_ensemble_models=4,
use_unet=True,
heavy_regularization=True
)
self.assertTrue(ml.use_ensemble)
self.assertEqual(ml.n_ensemble_models, 4)
self.assertTrue(ml.heavy_regularization)
def test_epoch_shuffling(self):
"""Test epoch shuffling produces different orderings."""
ml = MCMLQLabs(output_dir=self.output_dir)
X = np.random.randn(100, 10)
y = np.random.randn(100)
epoch_data = ml._shuffle_epochs(X, y, n_epochs=5)
self.assertEqual(len(epoch_data), 5)
# First elements should be different across epochs
first_elements = [epoch[0][0][0] for epoch in epoch_data]
self.assertGreater(len(set(first_elements)), 1)
class TestE2EForewarning(unittest.TestCase):
"""End-to-end tests for the forewarning system"""
def setUp(self):
"""Set up test fixtures."""
self.output_dir = "mc_forewarning_qlabs_fork/results/test_e2e"
Path(self.output_dir).mkdir(parents=True, exist_ok=True)
# Generate synthetic corpus data
self._generate_synthetic_corpus()
def _generate_synthetic_corpus(self):
"""Generate synthetic MC trial data for testing."""
import pandas as pd
np.random.seed(42)
n_trials = 500
# Generate parameter columns
data = {
'trial_id': range(n_trials),
'P_vel_div_threshold': np.random.uniform(-0.04, -0.008, n_trials),
'P_vel_div_extreme': np.random.uniform(-0.12, -0.02, n_trials),
'P_max_leverage': np.random.uniform(1.5, 12, n_trials),
'P_min_leverage': np.random.uniform(0.1, 1.5, n_trials),
'P_fraction': np.random.uniform(0.05, 0.4, n_trials),
'P_fixed_tp_pct': np.random.uniform(0.003, 0.03, n_trials),
'P_stop_pct': np.random.uniform(0.2, 5, n_trials),
'P_max_hold_bars': np.random.randint(20, 600, n_trials),
'P_leverage_convexity': np.random.uniform(0.75, 6, n_trials),
'P_use_direction_confirm': np.random.choice([True, False], n_trials),
'P_use_alpha_layers': np.random.choice([True, False], n_trials),
'P_use_dynamic_leverage': np.random.choice([True, False], n_trials),
'P_use_sp_fees': np.random.choice([True, False], n_trials),
'P_use_sp_slippage': np.random.choice([True, False], n_trials),
'P_use_ob_edge': np.random.choice([True, False], n_trials),
'P_use_asset_selection': np.random.choice([True, False], n_trials),
'P_ob_imbalance_bias': np.random.uniform(-0.25, 0.15, n_trials),
'P_ob_depth_scale': np.random.uniform(0.3, 2, n_trials),
'P_acb_beta_high': np.random.uniform(0.4, 1.5, n_trials),
'P_acb_beta_low': np.random.uniform(0, 0.6, n_trials),
}
# Generate metrics based on parameters (simplified model)
roi = (
-data['P_vel_div_threshold'] * 1000 +
data['P_max_leverage'] * 2 -
data['P_stop_pct'] * 5 +
np.random.randn(n_trials) * 10
)
data['M_roi_pct'] = roi
data['M_max_drawdown_pct'] = np.abs(roi) * 0.5 + np.random.randn(n_trials) * 5
data['M_profit_factor'] = 1 + roi / 100 + np.random.randn(n_trials) * 0.2
data['M_win_rate'] = 0.4 + roi / 500 + np.random.randn(n_trials) * 0.05
data['M_sharpe_ratio'] = roi / 20 + np.random.randn(n_trials) * 0.5
data['M_n_trades'] = np.random.randint(20, 200, n_trials)
# Classification labels
data['L_profitable'] = roi > 0
data['L_strongly_profitable'] = roi > 30
data['L_drawdown_ok'] = data['M_max_drawdown_pct'] < 20
data['L_sharpe_ok'] = data['M_sharpe_ratio'] > 1.5
data['L_pf_ok'] = data['M_profit_factor'] > 1.10
data['L_wr_ok'] = data['M_win_rate'] > 0.45
data['L_champion_region'] = (
data['L_strongly_profitable'] &
data['L_drawdown_ok'] &
data['L_sharpe_ok'] &
data['L_pf_ok'] &
data['L_wr_ok']
)
data['L_catastrophic'] = (roi < -30) | (data['M_max_drawdown_pct'] > 40)
data['L_inert'] = data['M_n_trades'] < 50
data['L_h2_degradation'] = np.random.choice([True, False], n_trials)
df = pd.DataFrame(data)
# Save to parquet
results_dir = Path(self.output_dir) / "results"
results_dir.mkdir(parents=True, exist_ok=True)
df.to_parquet(results_dir / "batch_0001_results.parquet", index=False)
# Create SQLite index
import sqlite3
conn = sqlite3.connect(Path(self.output_dir) / "mc_index.sqlite")
cursor = conn.cursor()
cursor.execute('DROP TABLE IF EXISTS mc_index')
cursor.execute('''
CREATE TABLE IF NOT EXISTS mc_index (
trial_id INTEGER PRIMARY KEY,
batch_id INTEGER,
status TEXT,
roi_pct REAL,
profit_factor REAL,
win_rate REAL,
max_dd_pct REAL,
sharpe REAL,
n_trades INTEGER,
champion_region INTEGER,
catastrophic INTEGER,
created_at INTEGER
)
''')
for i in range(n_trials):
try:
cursor.execute('''
INSERT INTO mc_index VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
i, 1, 'completed', float(roi[i]), float(data['M_profit_factor'][i]),
float(data['M_win_rate'][i]), float(data['M_max_drawdown_pct'][i]),
float(data['M_sharpe_ratio'][i]), int(data['M_n_trades'][i]),
int(data['L_champion_region'][i]), int(data['L_catastrophic'][i]), 0
))
except sqlite3.IntegrityError:
pass # Skip duplicates
conn.commit()
conn.close()
def test_training_pipeline(self):
"""Test full training pipeline."""
ml = MCMLQLabs(
output_dir=self.output_dir,
models_dir=f"{self.output_dir}/models_qlabs",
use_ensemble=False, # Faster for testing
n_ensemble_models=2,
use_unet=False, # Skip for speed
heavy_regularization=True
)
try:
result = ml.train_all_models(test_size=0.2, n_epochs=3)
self.assertEqual(result['status'], 'success')
self.assertIn('qlabs_techniques', result)
# Check models were saved
models_dir = Path(ml.models_dir)
self.assertTrue((models_dir / "feature_names.json").exists())
self.assertTrue((models_dir / "qlabs_config.json").exists())
except Exception as e:
self.skipTest(f"Training failed (may need real data): {e}")
def test_forewarning_assessment(self):
"""Test forewarning assessment."""
# Try to load existing models or skip
models_dir = Path(self.output_dir) / "models_qlabs"
if not (models_dir / "feature_names.json").exists():
self.skipTest("No trained models available")
try:
forewarner = DolphinForewarnerQLabs(models_dir=str(models_dir))
except Exception as e:
self.skipTest(f"Could not load forewarner: {e}")
# Create test config with only the features used during training
# Get feature names from the scaler
try:
import json
with open(models_dir / "feature_names.json", 'r') as f:
feature_names = json.load(f)
# Create a minimal config with just those features
config_dict = {name: MCSampler.CHAMPION.get(name, 0) for name in feature_names}
from mc.mc_sampler import MCTrialConfig
config = MCTrialConfig.from_dict(config_dict)
except Exception as e:
self.skipTest(f"Could not create config: {e}")
report = forewarner.assess(config)
self.assertIsNotNone(report)
self.assertIn('config', report.to_dict())
self.assertIn('predicted_roi', report.to_dict())
class TestComparisonWithBaseline(unittest.TestCase):
"""Compare QLabs-enhanced vs baseline MCML"""
def setUp(self):
"""Set up test fixtures."""
self.output_dir = "mc_forewarning_qlabs_fork/results/test_comparison"
Path(self.output_dir).mkdir(parents=True, exist_ok=True)
def test_prediction_uncertainty(self):
"""Test that ensemble provides uncertainty estimates."""
ml_qlabs = MCMLQLabs(
output_dir=self.output_dir,
use_ensemble=True,
n_ensemble_models=4
)
# Create dummy models for testing
from sklearn.linear_model import Ridge
ensemble = DeepEnsemble(Ridge, n_models=4)
# Generate synthetic data
np.random.seed(42)
X_train = np.random.randn(50, 10)
y_train = X_train[:, 0] + np.random.randn(50) * 0.1
# Fit ensemble - models will have variation due to different random states
ensemble.fit(X_train, y_train, alpha=1.0)
# Predict
X_test = np.random.randn(5, 10)
mean, std = ensemble.predict_regression(X_test)
# Should have valid uncertainty estimates
self.assertTrue(np.all(np.isfinite(std))) # No NaN or Inf
self.assertTrue(np.all(std >= 0)) # Non-negative std
def run_tests():
"""Run all tests."""
# Create test suite
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# Add all test classes
suite.addTests(loader.loadTestsFromTestCase(TestMuonOptimizer))
suite.addTests(loader.loadTestsFromTestCase(TestSwiGLU))
suite.addTests(loader.loadTestsFromTestCase(TestUNetMLP))
suite.addTests(loader.loadTestsFromTestCase(TestDeepEnsemble))
suite.addTests(loader.loadTestsFromTestCase(TestQLabsHyperParams))
suite.addTests(loader.loadTestsFromTestCase(TestMCMLQLabs))
suite.addTests(loader.loadTestsFromTestCase(TestE2EForewarning))
suite.addTests(loader.loadTestsFromTestCase(TestComparisonWithBaseline))
# Run tests
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
return result.wasSuccessful()
if __name__ == "__main__":
success = run_tests()
sys.exit(0 if success else 1)

View File

@@ -1,223 +0,0 @@
# PINK — BLUE Capital Handling: Complete Map
Traced from `prod/nautilus_event_trader.py` (4405 lines). Every store, every write path, every restore priority, every consistency property.
---
## 1. Capital Stores
### 1.1 HZ `DOLPHIN_STATE_BLUE` — primary runtime authority
| Key | Schema | Written by | Restore rank |
|---|---|---|---|
| `capital_update_ledger` | `[{"capital_before", "capital_after", "capital", "capital_delta", "ts", "reason", "source", "trade_id", "asset", "mode", ...}]` — JSON array, capped at 1000 entries | `_record_capital_ledger_event()` on trade close, retract, internal update, corrective replay | **65** (highest) |
| `latest_nautilus` | Full engine snapshot dict incl `capital`, `open_positions`, `algo_version`, `posture`, timestamps, leverage envelope | `_commit_capital_state()` — trade close, retract, replay, internal update, and periodic `_save_capital()` every scan | 40 |
| `engine_snapshot` | Same payload as `latest_nautilus`. ALSO written by `_push_state()` on EVERY scan (async put) | `_commit_capital_state()` + `_push_state()` per scan cycle | 30 |
| `capital_checkpoint` | `{"capital": X, "ts": Y}` — scalar, legacy | `_commit_capital_state()` | **5** (requires `DOLPHIN_ALLOW_LEGACY_CAPITAL_CHECKPOINT=1`) |
| `capital_correction_replay` | Full state payload | `_commit_capital_state()` with `update_replay_key=True` | 10 |
### 1.2 HZ `DOLPHIN_PNL_BLUE`
Key pattern: `YYYY-MM-DD` → same full state payload as `latest_nautilus`.
Written by `_commit_capital_state()` on every capital state change. Restore rank 25.
### 1.3 HZ `blue_control_plane`
| Key | What | Written by |
|---|---|---|
| `blue_capital_update_latest` | Mirror of every `_commit_capital_state()` call (if `mirror_control_plane=True`, default) | `_commit_capital_state()` |
| `blue_capital_update_ledger_latest` | Last ledger entry, as JSON | `_record_capital_ledger_event()` |
| `blue_runtime_commands` | Queue for external `SET_CAPITAL` commands | External callers via `request_capital_update()` |
### 1.4 Disk `/tmp/` — 4 files, startup survival layer
| File | Schema | Written by | Restore rank |
|---|---|---|---|
| `/tmp/dolphin_capital_update_ledger.json` | Same JSON array as HZ ledger | `_record_capital_ledger_event()` | **65** via `capital_update_ledger_local` — this is the FIRST restore source checked |
| `/tmp/dolphin_latest_nautilus_replay.json` | Full state payload | `_commit_capital_state()` when `update_replay_key=True` | 20 |
| `/tmp/dolphin_capital_checkpoint.json` | `{"capital": X, "ts": Y}` | `_commit_capital_state()` | **5** (legacy, env var gated) |
| `/tmp/dolphin_capital_correction_replay.json` | Same file as replay (different PATH constant) | Same | 10 |
### 1.5 ClickHouse `dolphin.trade_events`
Written via `ch_put()` on every trade close. Columns include `capital_before`, `capital_after`, `pnl`.
Restore rank 5 — lowest. Must pass validation: `|capital_after - (capital_before + pnl)| <= max(1.0, expected * 0.002)`.
### 1.6 ClickHouse `dolphin.status_snapshots`
Written by `ch_state_listener` (separate supervisord process, not the trader). The trader reads it on startup.
Restore rank 50 — second highest source.
---
## 2. Restore Order (startup path)
```
run()
└─ _restore_capital()
└─ _restore_capital_from_state()
├─ 1. Read local /tmp/dolphin_capital_update_ledger.json
│ → parsed_state["capital_update_ledger_local"] (rank 65)
├─ 2. Read HZ capital_update_ledger
│ → parsed_state["capital_update_ledger"] (rank 65)
├─ 3. CH status_snapshots (rank 50)
├─ 4. HZ latest_nautilus (rank 40)
├─ 5. HZ engine_snapshot (rank 30)
├─ 6. HZ pnl_day (rank 25)
├─ 7. Read local /tmp/dolphin_latest_nautilus_replay.json
│ → parsed_state["correction_replay_local"] (rank 20)
├─ 8. HZ capital_correction_replay (rank 10)
├─ 9. CH trade_events (rank 5)
└─ _select_restore_candidate()
│ SHORTCUT: if capital_update_ledger_local exists → return immediately
│ (lines 1416-1420)
└─ Sort candidates by (ts DESC, rank DESC) → pick top
└─ _restore_capital_from_legacy_checkpoint() [ENV GATE]
└─ HZ capital_checkpoint → disk /tmp/dolphin_capital_checkpoint.json
```
**Critical**: The local disk ledger (`capital_update_ledger_local`) has a **hardcoded shortcut** — if it exists, `_select_restore_candidate()` returns it immediately without considering any other source or its timestamp. This means a stale `/tmp/dolphin_capital_update_ledger.json` from a prior session **unconditionally** overrides HZ, CH, and everything else on restart.
---
## 3. Write Triggers (every path that touches capital)
| Trigger | Code path | What gets written |
|---|---|---|
| **Trade close** | `_process_exit``_apply_trade_capital_update()``_commit_capital_state()` + `_record_capital_ledger_event()` | HZ: all 5 state keys + ledger + PNL map. Disk: ledger + checkpoint. CH: trade_events + position_state + trade_reconstruction + execution_quality. Control plane mirror. |
| **Retract** (V7, ASL, SC haircut) | `_process_exit` → same as trade close | Same as trade close (minus CH trade_events) |
| **Every scan** | `_push_state()``_save_capital()``_commit_capital_state()` | HZ: latest_nautilus + engine_snapshot + capital_checkpoint + PNL map. Disk: checkpoint. **No ledger write.** |
| **Startup seed push** | `run()``_push_state()` once after restore | Same as scan path |
| **Internal capital update** (control plane `SET_CAPITAL`) | `_apply_internal_capital_update()``_commit_capital_state()` + `_record_capital_ledger_event()` | Full write + replay key + ledger entry |
| **Corrective replay** | `_publish_corrective_replay()``_commit_capital_state()` | Full write with `update_replay_key=True` |
---
## 4. `_commit_capital_state()` — the central write fan-out
Called by: `_apply_trade_capital_update()`, `_apply_internal_capital_update()`, `_save_capital()`, `_publish_corrective_replay()`.
```python
_commit_capital_state(capital, reason, source, trade_id, asset, replay_blob,
update_replay_key, mirror_control_plane):
payload = _capital_state_payload(...) # {"capital", "ts", "updated_at", "reason", ...}
# Write 6 HZ keys
state_map.put("capital_checkpoint", checkpoint_payload) # {"capital", "ts"}
state_map.put("latest_nautilus", state_payload)
state_map.put("engine_snapshot", state_payload)
state_map.put("pnl_day:YYYY-MM-DD", state_payload) # via pnl_map
if update_replay_key:
state_map.put("capital_correction_replay", state_payload)
disk: /tmp/dolphin_latest_nautilus_replay.json
# Write 1 disk file
disk: /tmp/dolphin_capital_checkpoint.json
# Mirror to control plane
if mirror_control_plane:
control_map.put("blue_capital_update_latest", state_payload)
# Set in-memory
self.eng.capital = capital
```
---
## 5. Capital resolution for trade PnL application
`_apply_trade_capital_update()` does a three-source merge before applying a PnL delta:
```python
_resolved_capital_state_value(fallback=self.eng.capital):
# Same logic as restore but simpler — reads local first
# Returns (capital, source_label, timestamp)
# Sources checked: local corrective replay, HZ ledger, HZ latest_nautilus,
# HZ engine_snapshot, HZ pnl_day, disk capital_checkpoint, local disk ledger
# Sort by (ts DESC, rank DESC) → pick top
```
This means even during live trading, the capital used as the base for the next PnL application is resolved from the same multi-source hierarchy, not just the in-memory value.
---
## 6. Consistency Properties
| Property | Detail |
|---|---|
| **Dual-write HZ then disk** | `_commit_capital_state()` writes HZ keys first, then disk. If HZ succeeds but disk fails (ENOSPC), restart gets HZ value via rank 40. If HZ is down, local disk ledger at rank 65 becomes the sole source. |
| **Scan-cycle overwrite** | `_push_state()` calls `_save_capital()` every ~10 seconds, writing `self.eng.capital` to HZ. Manually fixing HZ while the trader runs is futile — the next scan writes the trader's in-memory value back. Restart is required. |
| **No CH on _commit_capital_state** | ClickHouse only gets capital data via the explicit `ch_put("trade_events", ...)` call at trade close time, not from the capital state commit path. |
| **CH status_snapshots are external** | Written by `ch_state_listener` (a separate supervisord process), not the trader. The trader reads them on startup as a restore candidate but never writes them. |
| **Ledger is append-only, capped at 1000** | `_record_capital_ledger_event()` truncates to `ledger[-1000:]`. Old entries are silently dropped. If someone needs to reconstruct capital from 3 months ago, they'd need CH trade_events replay. |
| **Local disk ledger is the single source of truth on restart** | The hardcoded shortcut in `_select_restore_candidate()` (lines 1416-1420) returns `capital_update_ledger_local` unconditionally. Fixing `/tmp/dolphin_capital_update_ledger.json` is **mandatory** for a correct restart. |
---
## 7. Operational Hazards
1. **Stale local ledger beats HZ**: The file at `/tmp/dolphin_capital_update_ledger.json` has unconditional priority on restart. If you fix HZ but not this file, the trader restores the stale value anyway. This is exactly what happened in the 2026-05-27 BNB spurious trade recovery.
2. **ENOSPC silent truncation**: If `/tmp/dolphin_capital_update_ledger.json` is on a full SMB mount, the `write_text()` call can produce a 0-byte file. On restart, `json.loads("")` returns `None`, the local ledger candidate is rejected, and the next-best source is used. But if the file is truncated mid-write to a *partial* JSON array, `json.loads()` will raise and the file won't be retried — next source wins.
3. **Multiple competing restore sources**: With 4 HZ keys, 4 disk files, and 2 CH tables all carrying capital data, a mismatch between any two can cause silent capital corruption on restart. There is no consistency check across sources — the sort-based `_select_restore_candidate()` just picks the one with the highest (timestamp, rank) tuple.
4. **HZ write vs async put**: `engine_snapshot` is written by `_push_state()` via an **async** `future = state_map.put(...)`. The subsequent `_save_capital()` is sync but only writes to `latest_nautilus` + `capital_checkpoint` + PNL map, NOT to `engine_snapshot`. So if the async put fails silently, the engine_snapshot in HZ is stale and will be used as a restore candidate (rank 30) on next restart.
5. **No ledger entry on periodic save**: `_save_capital()` (called every scan) writes to all HZ state keys but does NOT append to the ledger. This means the periodically-saved capital values are invisible to the ledger-based restore path — they only appear in `latest_nautilus`, `engine_snapshot`, and `pnl_day`, which have lower restore ranks.
---
## 8. Summary Diagram
```
TRADER (in-memory self.eng.capital)
┌──────────────┼──────────────────────────────┐
│ │ │
▼ ▼ ▼
TRADE CLOSE SCAN (every ~10s) CONTROL PLANE
(retract too) │ (external cmd)
│ │ │
▼ ▼ ▼
_apply_trade_ _push_state() _apply_internal_
capital_update() │ capital_update()
│ │ │
└───────┬───────┘ │
│ │
▼ ▼
┌─────────────────────────────┐ ┌──────────────────────┐
│ _commit_capital_state() │ │ _commit_capital_ │
│ + │ │ state() │
│ _record_capital_ledger_ │ │ + │
│ event() │ │ _record_capital_ │
└──────────┬──────────────────┘ │ ledger_event() │
│ └──────────┬───────────┘
│ │
└─────────────────┬────────────────┘
┌──────────────┼──────────────────┐
▼ ▼ ▼
┌──────────┐ ┌─────────────┐ ┌──────────────┐
│ HZ STATE │ │ DISK /tmp/ │ │ CH (close │
│ (6 keys) │ │ (4 files) │ │ only) │
└──────────┘ └─────────────┘ └──────────────┘
RESTART:
disk ledger (rank 65) ─── immediate win
CH status_snapshots (50)
HZ latest_nautilus (40)
HZ engine_snapshot (30)
HZ pnl_day (25)
disk corrective replay (20)
HZ corrective replay (10)
CH trade_events (5)
legacy checkpoint (5, gated)
```

View File

@@ -1,200 +0,0 @@
"""
MIG6.1 & MIG6.2: ACB Processor Service
Watches for new scan arrivals and atomically computes/writes ACB boost
to the Hazelcast DOLPHIN_FEATURES map using CP Subsystem lock for atomicity.
"""
import sys
import time
import json
import logging
from pathlib import Path
from datetime import datetime
import hazelcast
HCM_DIR = Path(__file__).parent.parent
# Use platform-independent paths from dolphin_paths
sys.path.insert(0, str(HCM_DIR))
sys.path.insert(0, str(HCM_DIR / 'prod'))
from dolphin_paths import get_eigenvalues_path
SCANS_DIR = get_eigenvalues_path()
sys.path.insert(0, str(HCM_DIR / 'nautilus_dolphin'))
from nautilus_dolphin.nautilus.adaptive_circuit_breaker import AdaptiveCircuitBreaker
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s:%(message)s')
class ACBProcessorService:
def __init__(self, hz_cluster="dolphin", hz_host="localhost:5701"):
try:
self.hz_client = hazelcast.HazelcastClient(
cluster_name=hz_cluster,
cluster_members=[hz_host]
)
self.imap = self.hz_client.get_map("DOLPHIN_FEATURES").blocking()
# Using CP Subsystem lock as per MIG6.1
self.lock = self.hz_client.cp_subsystem.get_lock("acb_update_lock").blocking()
except Exception as e:
logging.error(f"Failed to connect to Hazelcast: {e}")
raise
self.acb = AdaptiveCircuitBreaker()
self.acb.config.EIGENVALUES_PATH = SCANS_DIR # CRITICAL: override Windows default for Linux
self.acb.preload_w750(self._get_recent_dates(60))
self.last_scan_count = 0
self.last_date = None
def _get_recent_dates(self, n=60):
try:
dirs = sorted([d.name for d in SCANS_DIR.iterdir() if d.is_dir() and len(d.name)==10])
return dirs[-n:]
except Exception:
return []
def get_today_str(self):
return datetime.utcnow().strftime('%Y-%m-%d')
def check_new_scans(self, date_str):
today_dir = SCANS_DIR / date_str
if not today_dir.exists():
return False
json_files = list(today_dir.glob("scan_*.json"))
count = len(json_files)
if self.last_date != date_str:
self.last_date = date_str
self.last_scan_count = 0
# Preload updated dates when day rolls over
self.acb.preload_w750(self._get_recent_dates(60))
if count > self.last_scan_count:
self.last_scan_count = count
return True
return False
def process_and_write(self, date_str):
"""Compute ACB boost and write to HZ acb_boost.
Preference order:
1. HZ exf_latest — live, pre-lagged values (preferred, ~0.5 s latency)
2. NPZ disk scan — fallback when HZ data absent or stale (>12 h)
"""
try:
boost_info = None
long_boost_info = None
# ── HZ path (preferred) ────────────────────────────────────────────
try:
exf_raw = self.imap.get('exf_latest')
if exf_raw:
exf_snapshot = json.loads(exf_raw)
scan_raw = self.imap.get('latest_eigen_scan')
w750_live = None
if scan_raw:
scan_data = json.loads(scan_raw)
w750_live = scan_data.get('w750_velocity')
boost_info = self.acb.get_dynamic_boost_from_hz(
date_str, exf_snapshot, w750_velocity=w750_live, direction=-1
)
long_boost_info = self.acb.get_dynamic_boost_from_hz(
date_str, exf_snapshot, w750_velocity=w750_live, direction=1
)
logging.debug(
f"ACB computed from HZ: short={boost_info['boost']:.4f} "
f"long={long_boost_info['boost']:.4f}"
)
except ValueError as ve:
logging.warning(f"ACB HZ snapshot stale: {ve} — falling back to NPZ")
boost_info = None
except Exception as e:
logging.warning(f"ACB HZ read failed: {e} — falling back to NPZ")
boost_info = None
# ── NPZ fallback ───────────────────────────────────────────────────
if boost_info is None:
boost_info = self.acb.get_dynamic_boost_for_date(date_str, direction=-1)
long_boost_info = self.acb.get_dynamic_boost_for_date(date_str, direction=1)
logging.debug(
f"ACB computed from NPZ: short={boost_info['boost']:.4f} "
f"long={long_boost_info['boost']:.4f}"
)
payload = json.dumps(boost_info)
long_payload = json.dumps(long_boost_info or boost_info)
# Atomic Write via CP Subsystem Lock
self.lock.lock()
try:
# Legacy key remains SHORT for BLUE/PRODGREEN compatibility.
self.imap.put("acb_boost", payload)
self.imap.put("acb_boost_short", payload)
self.imap.put("acb_boost_long", long_payload)
logging.info(
f"acb_boost updated (src={boost_info.get('source','npz')}): "
f"short={boost_info['boost']:.4f}/{boost_info['signals']:.1f}sig "
f"long={(long_boost_info or {}).get('boost', 0.0):.4f}/"
f"{(long_boost_info or {}).get('signals', 0.0):.1f}sig"
)
try:
from ch_writer import ch_put, ts_us as _ts
ch_put("acb_state", {
"ts": _ts(),
"boost": float(boost_info.get("boost", 0)),
"beta": float(boost_info.get("beta", 0)),
"signals": float(boost_info.get("signals", 0)),
})
except Exception:
pass
finally:
self.lock.unlock()
except Exception as e:
logging.error(f"Error processing ACB: {e}")
def run(self, poll_interval=1.0, hz_refresh_interval=30.0):
"""Main service loop.
Two update triggers:
1. New scan files arrive for today → compute from HZ (preferred) or NPZ.
2. hz_refresh_interval elapsed → re-push acb_boost from live exf_latest
even when no new scans exist (covers live-only operation days when
scan files land in a different directory or not at all).
"""
logging.info("Starting ACB Processor Service (Python CP Subsystem)...")
today = self.get_today_str()
# Write immediately on startup so acb_boost is populated from the first second
logging.info(f"Startup write for {today}")
self.process_and_write(today)
last_hz_refresh = time.monotonic()
while True:
try:
today = self.get_today_str()
now = time.monotonic()
# Trigger 1: new scan files
if self.check_new_scans(today):
self.process_and_write(today)
last_hz_refresh = now
# Trigger 2: periodic HZ refresh (ensures acb_boost stays current
# even on days with no new NPZ scan files)
elif (now - last_hz_refresh) >= hz_refresh_interval:
self.process_and_write(today)
last_hz_refresh = now
time.sleep(poll_interval)
except KeyboardInterrupt:
break
except Exception as e:
logging.error(f"Loop error: {e}")
time.sleep(5.0)
if __name__ == "__main__":
service = ACBProcessorService()
service.run()

View File

@@ -1,126 +0,0 @@
from __future__ import annotations
import json
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
DEFAULT_SANDBOX_STATUS_PATH = Path("/tmp/bingx_sandbox_status.json")
@dataclass(frozen=True)
class BingxSandboxStatus:
"""Small sidecar snapshot for BingX demo/testnet state.
The snapshot is intentionally local-only so it can be used by tests and
operators without writing into BLUE state, ClickHouse, or production logs.
"""
ts: str
environment: str
balance: float
equity: float
available_margin: float
unrealized_profit: float
used_margin: float
open_positions: int
open_orders: int
account_currency: str = "VST"
clean: bool = False
notes: dict[str, Any] | None = None
def to_dict(self) -> dict[str, Any]:
return {
"ts": self.ts,
"environment": self.environment,
"account_currency": self.account_currency,
"balance": self.balance,
"equity": self.equity,
"available_margin": self.available_margin,
"unrealized_profit": self.unrealized_profit,
"used_margin": self.used_margin,
"open_positions": self.open_positions,
"open_orders": self.open_orders,
"clean": self.clean,
"notes": self.notes or {},
}
def _safe_float(value: Any, default: float = 0.0) -> float:
try:
out = float(value)
except Exception:
return default
return out if out == out else default
def _count_positions(positions: Any) -> int:
if isinstance(positions, list):
return sum(1 for item in positions if isinstance(item, dict))
return 0
def _count_orders(open_orders: Any) -> int:
if isinstance(open_orders, dict):
orders = open_orders.get("orders")
if isinstance(orders, list):
return sum(1 for item in orders if isinstance(item, dict))
if isinstance(open_orders, list):
return sum(1 for item in open_orders if isinstance(item, dict))
return 0
def build_sandbox_status(
*,
balance_payload: dict[str, Any],
positions_payload: Any,
open_orders_payload: Any,
environment: str = "VST",
account_currency: str = "VST",
notes: dict[str, Any] | None = None,
) -> BingxSandboxStatus:
balance_row = balance_payload.get("balance", balance_payload) if isinstance(balance_payload, dict) else {}
if not isinstance(balance_row, dict):
balance_row = {}
balance = _safe_float(balance_row.get("balance"), 0.0)
equity = _safe_float(balance_row.get("equity"), balance)
available_margin = _safe_float(balance_row.get("availableMargin"), 0.0)
unrealized_profit = _safe_float(balance_row.get("unrealizedProfit"), 0.0)
used_margin = _safe_float(balance_row.get("usedMargin"), 0.0)
open_positions = _count_positions(positions_payload)
open_orders = _count_orders(open_orders_payload)
return BingxSandboxStatus(
ts=datetime.now(timezone.utc).isoformat(),
environment=str(environment),
account_currency=str(account_currency),
balance=balance,
equity=equity,
available_margin=available_margin,
unrealized_profit=unrealized_profit,
used_margin=used_margin,
open_positions=open_positions,
open_orders=open_orders,
clean=(open_positions == 0 and open_orders == 0),
notes=notes or {},
)
def snapshot_path(path: str | Path | None = None) -> Path:
return Path(path) if path is not None else DEFAULT_SANDBOX_STATUS_PATH
def write_sandbox_status(status: BingxSandboxStatus, path: str | Path | None = None) -> Path:
target = snapshot_path(path)
target.write_text(json.dumps(status.to_dict(), indent=2, sort_keys=True))
return target
def load_sandbox_status(path: str | Path | None = None) -> dict[str, Any] | None:
target = snapshot_path(path)
if not target.exists():
return None
try:
return json.loads(target.read_text())
except Exception:
return None

View File

@@ -1,503 +0,0 @@
"""Direct BingX execution adapter with no Nautilus Trader node dependency.
This adapter speaks BingX REST directly and keeps the exchange state
authoritative. It is intended for PINK live execution under the DITA boundary.
"""
from __future__ import annotations
import asyncio
import json
import logging
import math
import uuid
from dataclasses import dataclass
from datetime import datetime, timezone
from decimal import Decimal, ROUND_DOWN
from typing import Any, Optional
from nautilus_trader.model.identifiers import InstrumentId
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.config import BingxInstrumentProviderConfig
from prod.bingx.enums import BingxEnvironment
from prod.bingx.http import BingxHttpError
from prod.bingx.http import BingxHttpClient
from prod.bingx.instrument_provider import BingxInstrumentProvider
from prod.bingx.leverage import normalize_bingx_leverage_value
from prod.bingx.schemas import BingxOrderAck
from prod.bingx.schemas import unwrap_order_payload
from prod.clean_arch.dita import Intent, TradeSide, DecisionAction
from prod.clean_arch.ports.execution import ExchangeStateSnapshot
from prod.clean_arch.ports.execution import ExecutionReceipt
from prod.clean_arch.ports.execution import ExecutionPort
LOGGER = logging.getLogger(__name__)
def _rows_from_payload(payload: Any, *keys: str) -> list[dict[str, Any]]:
if isinstance(payload, list):
return [row for row in payload if isinstance(row, dict)]
if isinstance(payload, dict):
for key in keys:
rows = payload.get(key)
if isinstance(rows, list):
return [row for row in rows if isinstance(row, dict)]
return []
def _capital_from_balance_rows(rows: Any) -> float:
if not isinstance(rows, list):
return 0.0
for row in rows:
if not isinstance(row, dict):
continue
capital = 0.0
for key in ("total", "balance", "equity", "availableMargin", "availableBalance", "walletBalance", "free"):
try:
capital = float(row.get(key, 0.0) or 0.0)
except Exception:
continue
if capital > 0 and math.isfinite(capital):
return capital
if capital > 0 and math.isfinite(capital):
return capital
return 0.0
def _position_notional_from_rows(rows: Any) -> float:
if not isinstance(rows, list):
return 0.0
total = 0.0
for row in rows:
if not isinstance(row, dict):
continue
try:
qty = abs(
float(
row.get("positionAmt")
or row.get("positionQty")
or row.get("positionSize")
or row.get("quantity")
or row.get("pa")
or 0.0
)
)
if qty <= 0.0:
continue
notional = row.get("positionValue") or row.get("notional") or row.get("openNotional")
if notional is not None:
total += abs(float(notional or 0.0))
continue
entry = (
row.get("entryPrice")
or row.get("avgPrice")
or row.get("markPrice")
or row.get("avgEntryPrice")
or row.get("ep")
or row.get("ap")
or 0.0
)
total += qty * abs(float(entry or 0.0))
except Exception:
continue
return total
def _normalize_symbol(symbol: str) -> str:
return str(symbol or "").replace("-", "").replace("_", "").replace("/","").upper()
def _venue_symbol_from_asset(asset: str) -> str:
text = _normalize_symbol(asset)
if text.endswith("USDT"):
return f"{text[:-4]}-USDT"
return text
def _decimal_text(value: Decimal) -> str:
text = format(value.normalize(), "f")
if "." in text:
text = text.rstrip("0").rstrip(".")
return text or "0"
def _is_rate_limited_error(exc: Exception) -> bool:
message = str(exc)
lowered = message.lower()
return "100410" in message or "frequency limit" in lowered or "rate limit" in lowered
@dataclass(frozen=True)
class BingxDirectExecutionConfig:
"""Execution-specific knobs for the direct adapter."""
environment: BingxEnvironment = BingxEnvironment.VST
allow_mainnet: bool = False
default_leverage: int = 1
exchange_leverage_cap: int = 3
recv_window_ms: int = 5_000
prefer_websocket: bool = False
use_reduce_only: bool = True
journal_strategy: str = "pink"
journal_db: str = "dolphin_pink"
instrument_provider: BingxInstrumentProviderConfig = BingxInstrumentProviderConfig(load_all=True)
class BingxDirectExecutionAdapter(ExecutionPort):
"""Direct BingX execution boundary with exchange-led state snapshots."""
def __init__(
self,
config: BingxExecClientConfig | BingxDirectExecutionConfig,
*,
client: BingxHttpClient | None = None,
provider: BingxInstrumentProvider | None = None,
) -> None:
if isinstance(config, BingxExecClientConfig):
self._config = BingxDirectExecutionConfig(
environment=config.environment,
allow_mainnet=config.allow_mainnet,
default_leverage=int(config.default_leverage),
exchange_leverage_cap=int(config.exchange_leverage_cap),
recv_window_ms=int(config.recv_window_ms),
prefer_websocket=bool(config.prefer_websocket),
use_reduce_only=bool(config.use_reduce_only),
journal_strategy=str(config.journal_strategy or "pink"),
journal_db=str(config.journal_db or "dolphin_pink"),
instrument_provider=config.instrument_provider,
)
http_config = config
else:
self._config = config
http_config = BingxExecClientConfig(
api_key="",
secret_key="",
environment=config.environment,
allow_mainnet=config.allow_mainnet,
prefer_websocket=config.prefer_websocket,
sizing_mode="testnet",
exchange_leverage_cap=config.exchange_leverage_cap,
use_reduce_only=config.use_reduce_only,
default_leverage=config.default_leverage,
recv_window_ms=config.recv_window_ms,
journal_strategy=config.journal_strategy,
journal_db=config.journal_db,
instrument_provider=config.instrument_provider,
)
self._client = client or BingxHttpClient(http_config)
self._provider = provider or BingxInstrumentProvider(client=self._client, config=self._config.instrument_provider)
self._log = LOGGER
self._client_order_run_id = uuid.uuid4().hex[:8]
self._entry_client_order_seq = 0
self._exit_client_order_seq = 0
self._state: ExchangeStateSnapshot | None = None
self._connected = False
@property
def state(self) -> ExchangeStateSnapshot | None:
return self._state
async def connect(self) -> bool:
await self._provider.initialize()
self._connected = True
self._state = await self.refresh_state()
return True
async def disconnect(self) -> None:
self._connected = False
await self._client.close()
def _resolve_instrument(self, asset: str):
normalized = _normalize_symbol(asset)
candidates = [
InstrumentId.from_str(f"{normalized}.BINGX"),
InstrumentId.from_str(f"{_venue_symbol_from_asset(asset)}.BINGX"),
]
for candidate in candidates:
instrument = self._provider.find(candidate)
if instrument is not None:
return instrument
for instrument in self._provider.list_all():
if _normalize_symbol(instrument.symbol.value) == normalized:
return instrument
if _normalize_symbol(instrument.raw_symbol.value) == normalized:
return instrument
return None
def _instrument_venue_symbol(self, asset: str) -> str:
instrument = self._resolve_instrument(asset)
if instrument is not None:
return str(instrument.raw_symbol.value)
return _venue_symbol_from_asset(asset)
def _instrument_step(self, asset: str) -> Decimal:
instrument = self._resolve_instrument(asset)
if instrument is not None:
try:
return Decimal(str(instrument.size_increment.as_decimal()))
except Exception:
pass
return Decimal("0.001")
def _format_quantity(self, asset: str, quantity: float) -> str:
step = self._instrument_step(asset)
if step <= 0:
return str(max(0.0, quantity))
value = Decimal(str(quantity))
quantized = (value / step).to_integral_value(rounding=ROUND_DOWN) * step
return _decimal_text(max(Decimal("0"), quantized))
def _instrument_tick(self, asset: str) -> Decimal:
instrument = self._resolve_instrument(asset)
if instrument is not None:
try:
tick = getattr(instrument, "price_increment", None)
if tick is not None:
return Decimal(str(tick.as_decimal()))
except Exception:
pass
return Decimal("0.01")
def _format_price(self, asset: str, price: float) -> str:
tick = self._instrument_tick(asset)
if tick <= 0:
return f"{price:.8f}".rstrip("0").rstrip(".")
value = Decimal(str(price))
quantized = (value / tick).to_integral_value(rounding=ROUND_DOWN) * tick
return _decimal_text(max(Decimal("0"), quantized))
async def _safe_get(self, endpoint: str, params: dict | None = None, *, fallback: Any = None) -> Any:
"""GET an endpoint, returning *fallback* on rate-limit errors."""
try:
return await self._client.signed_get(endpoint, params)
except BingxHttpError as exc:
message = str(exc)
if "100410" in message or "frequency limit" in message.lower():
LOGGER.debug("BingX %s rate-limited; continuing with empty snapshot", endpoint)
return fallback if fallback is not None else []
raise
async def _refresh_exchange_state(self, symbol: str | None = None, *, include_history: bool = False) -> ExchangeStateSnapshot:
"""Fetch exchange state with parallel HTTP calls.
The three primary calls (balance, positions, openOrders) are
independent and run concurrently via ``asyncio.gather``. Each has
its own rate-limit fallback so a single throttle does not block
the others. Historical calls (allOrders, allFillOrders) are gated
on ``include_history`` and also gathered.
"""
balance_task = self._safe_get("/openApi/swap/v2/user/balance")
positions_task = self._safe_get("/openApi/swap/v2/user/positions")
orders_task = self._safe_get("/openApi/swap/v2/trade/openOrders")
balance_payload, positions_payload, open_orders_payload = await asyncio.gather(
balance_task, positions_task, orders_task,
)
all_orders_payload: Any = []
all_fills_payload: Any = []
if include_history and symbol is not None:
venue_symbol = self._instrument_venue_symbol(symbol)
hist_tasks = asyncio.gather(
self._safe_get("/openApi/swap/v2/trade/allOrders", {"symbol": venue_symbol}),
self._safe_get("/openApi/swap/v2/trade/allFillOrders", {"symbol": venue_symbol}),
return_exceptions=True,
)
results = await hist_tasks
all_orders_payload = results[0] if not isinstance(results[0], Exception) else []
all_fills_payload = results[1] if not isinstance(results[1], Exception) else []
# Parse results (shared logic, same as before)
if isinstance(balance_payload, list):
balances = balance_payload
elif isinstance(balance_payload, dict):
rows_raw = balance_payload.get("balance") or balance_payload.get("balances") or balance_payload.get("data")
if isinstance(rows_raw, dict):
balances = [rows_raw]
elif isinstance(rows_raw, list):
balances = rows_raw
else:
balances = []
else:
balances = []
positions_rows = _rows_from_payload(positions_payload, "positions", "data")
positions: dict[str, dict[str, Any]] = {}
for row in positions_rows:
raw_symbol = str(row.get("symbol") or row.get("symbolName") or row.get("venueSymbol") or "")
key = _normalize_symbol(raw_symbol)
if not key:
continue
positions[key] = dict(row)
open_orders = _rows_from_payload(open_orders_payload, "orders", "data")
capital = _capital_from_balance_rows(balances)
open_notional = _position_notional_from_rows(positions_rows)
equity = capital
if open_notional > 0 and positions_rows:
equity = capital
snapshot = ExchangeStateSnapshot(
timestamp=datetime.now(timezone.utc),
capital=capital,
equity=equity,
open_positions=positions,
open_orders=[dict(row) for row in open_orders],
all_orders=[dict(row) for row in _rows_from_payload(all_orders_payload, "orders", "data")],
all_fills=[dict(row) for row in _rows_from_payload(all_fills_payload, "fills", "data")],
account={"balances": balances},
open_notional=open_notional,
source="bingx",
recovered=bool(include_history),
)
self._state = snapshot
return snapshot
async def refresh_state(self, symbol: str | None = None, *, include_history: bool = False) -> ExchangeStateSnapshot:
return await self._refresh_exchange_state(symbol, include_history=include_history)
async def submit_intent(self, intent: Intent) -> ExecutionReceipt:
symbol = self._instrument_venue_symbol(intent.asset)
if intent.action == DecisionAction.EXIT:
side = "SELL" if intent.side == TradeSide.LONG else "BUY"
else:
side = "BUY" if intent.side == TradeSide.LONG else "SELL"
# Entries must be free to open the slot; only exits are reduce-only.
reduce_only = bool(intent.action == DecisionAction.EXIT)
if reduce_only:
self._exit_client_order_seq += 1
client_order_id = f"pink:{self._client_order_run_id}:x{self._exit_client_order_seq:02d}"
else:
self._entry_client_order_seq += 1
client_order_id = f"pink:{self._client_order_run_id}:e{self._entry_client_order_seq:02d}"
leverage = normalize_bingx_leverage_value(
int(round(float(intent.leverage or self._config.default_leverage))),
exchange_max=self._config.exchange_leverage_cap,
)
try:
await self._client.signed_post(
"/openApi/swap/v2/trade/leverage",
{"symbol": symbol, "side": "BOTH", "leverage": leverage},
)
# Honor the order type forwarded by the venue adapter
# (bingx_venue._legacy_intent sets _order_type/_limit_price). MARKET
# is the default; a LIMIT carries a resting price + GTC and will not
# fill synchronously — the async-fill pump settles it later.
order_type = str((intent.metadata or {}).get("_order_type", "MARKET") or "MARKET").upper()
limit_price = float((intent.metadata or {}).get("_limit_price", 0.0) or 0.0)
is_limit = order_type == "LIMIT" and limit_price > 0.0
payload: dict[str, Any] = {
"symbol": symbol,
"side": side,
"positionSide": "BOTH",
"type": "LIMIT" if is_limit else "MARKET",
"quantity": self._format_quantity(intent.asset, intent.target_size),
"clientOrderId": client_order_id,
"recvWindow": str(int(self._config.recv_window_ms)),
}
if is_limit:
payload["price"] = self._format_price(intent.asset, limit_price)
payload["timeInForce"] = "GTC"
if reduce_only:
payload["reduceOnly"] = "true"
ack_payload = await self._client.signed_post("/openApi/swap/v2/trade/order", payload)
ack = BingxOrderAck.from_http(ack_payload if isinstance(ack_payload, dict) else {})
ack_row = dict(unwrap_order_payload(ack_payload)) if isinstance(ack_payload, dict) else {}
status = str(ack_row.get("status") or ack.status or "ACKED")
fill_price = 0.0
for key in ("avgPrice", "avgFilledPrice", "price", "lastFillPrice", "tradePrice"):
try:
value = float(ack_row.get(key) or 0.0)
except Exception:
value = 0.0
if value > 0:
fill_price = value
break
if fill_price <= 0 and self._state is not None:
# Use the last known exchange mark as a fallback for projected accounting.
fill_price = next((float(row.get("markPrice") or row.get("avgPrice") or 0.0) for row in self._state.open_positions.values() if float(row.get("markPrice") or row.get("avgPrice") or 0.0) > 0), 0.0)
except BingxHttpError as exc:
status = "RATE_LIMITED" if _is_rate_limited_error(exc) else "REJECTED"
ack_row = {
"status": status,
"msg": str(exc),
"symbol": symbol,
"clientOrderId": client_order_id,
}
fill_price = 0.0
ack = None
receipt = ExecutionReceipt(
timestamp=datetime.now(timezone.utc),
status=status,
symbol=symbol,
side=side,
action=intent.action.value,
quantity=float(intent.target_size or 0.0),
price=fill_price,
client_order_id=client_order_id,
order_id=str((ack.order_id if 'ack' in locals() and ack is not None else '') or ack_row.get("orderId") or ""),
raw_ack=ack_row,
raw_state=dict(self._state.account if self._state is not None else {}),
)
# Refresh from the venue so the direct runtime can use exchange-led state.
self._state = await self._refresh_exchange_state(intent.asset, include_history=True)
return receipt
async def cancel(self, order: Any, *, reason: str = "") -> dict[str, Any]:
"""Cancel a working order on the venue (resting LIMIT support).
Signs the DELETE with the same client used for order placement, keyed by
the venue orderId (propagated onto the slot order by the kernel on ACK)
with a clientOrderId fallback. Returns the raw BingX response for the
venue adapter to map into a CANCEL_ACK / CANCEL_REJECT event.
"""
asset = str((getattr(order, "metadata", None) or {}).get("asset") or "")
symbol = self._instrument_venue_symbol(asset) if asset else ""
params: dict[str, Any] = {
"symbol": symbol,
"recvWindow": str(int(self._config.recv_window_ms)),
}
venue_order_id = str(getattr(order, "venue_order_id", "") or "")
venue_client_id = str(getattr(order, "venue_client_id", "") or "")
if venue_order_id:
params["orderId"] = venue_order_id
elif venue_client_id:
params["clientOrderId"] = venue_client_id
else:
return {"status": "REJECTED", "msg": "no order id to cancel",
"orderId": venue_order_id, "clientOrderId": venue_client_id}
delete_resp: dict[str, Any] = {}
try:
resp = await self._client.signed_delete("/openApi/swap/v2/trade/order", params)
delete_resp = resp if isinstance(resp, dict) else {"status": "CANCELED"}
except BingxHttpError as exc:
delete_resp = {"status": "RATE_LIMITED" if _is_rate_limited_error(exc) else "ERROR", "msg": str(exc)}
# Truth-based confirmation: the cancel succeeded iff the order is no
# longer open on the venue. BingX can return transient errors (e.g.
# "order not exist", "same order number ... within 1 second" from an
# internal retry) even when the order was actually removed — so we trust
# exchange state, not the DELETE response.
still_open: bool | None = None
try:
oo = await self._client.signed_get("/openApi/swap/v2/trade/openOrders", {"symbol": symbol})
rows = oo if isinstance(oo, list) else (oo.get("data") or oo.get("orders") or [])
if isinstance(rows, dict):
rows = rows.get("orders") or []
ids = {str(r.get("orderId")) for r in rows if isinstance(r, dict)}
cids = {str(r.get("clientOrderId") or r.get("clientOrderID")) for r in rows if isinstance(r, dict)}
still_open = (venue_order_id in ids) if venue_order_id else (venue_client_id in cids)
except Exception:
still_open = None
if still_open is False:
return {"status": "CANCELED", "orderId": venue_order_id, "clientOrderId": venue_client_id}
if str(delete_resp.get("status", "")).upper() in {"CANCELED", "CANCELLED", "SUCCESS", "OK"}:
return {"status": "CANCELED", "orderId": venue_order_id, "clientOrderId": venue_client_id}
return {
"status": delete_resp.get("status", "REJECTED"),
"msg": delete_resp.get("msg", "cancel not confirmed"),
"orderId": venue_order_id, "clientOrderId": venue_client_id,
}
async def reconcile(self, symbol: str | None = None) -> ExchangeStateSnapshot:
# Recovery-only path: ask the venue for authoritative account/position/order state.
return await self._refresh_exchange_state(symbol, include_history=True)

View File

@@ -0,0 +1,109 @@
# BingX User Stream — VST Probe Notes (Phase 0)
**Date:** 2026-06-01
**Scope:** VST only (no LIVE touch).
**Result: Outcome A — VST has WebSocket. Full WS-on-both symmetry is achievable.**
---
## Gate G0 resolution
| Check | Result |
|---|---|
| listenKey endpoint (`POST /openApi/user/auth/userDataStream`) | ✅ Returns `listenKey` (signed request, `signed_post_raw`) |
| Signing method | ✅ Standard HMAC-SHA256 signed POST works — "header-only/unsigned" concern was unfounded |
| WS URL | `wss://vst-open-api-ws.bingx.com/swap-market?listenKey=<key>` |
| Frames delivered | ✅ 667 SNAPSHOT frames in 20 s (idle session, no active orders) |
| Gzip | Binary frames are gzip-compressed — `gzip.decompress(bytes(msg.data))` |
| Ping/Pong | Server sends text `"Ping"` → client must respond with `"Pong"` |
| listenKey keepalive | `PUT /openApi/user/auth/userDataStream {"listenKey": ...}` |
| listenKey delete | `DELETE /openApi/user/auth/userDataStream {"listenKey": ...}` |
---
## Event schemas
### `SNAPSHOT` — position/leverage state (received continuously)
```json
{"e":"SNAPSHOT","E":1780336019559,"ac":{"s":"MTL-USDT","l":1,"S":1,"mt":"isolated"}}
```
| Field | Meaning |
|---|---|
| `e` | `"SNAPSHOT"` |
| `E` | Server timestamp ms |
| `ac.s` | Symbol |
| `ac.l` | Long leverage |
| `ac.S` | Short leverage |
| `ac.mt` | Margin type (`"isolated"`) |
### `ORDER_TRADE_UPDATE` — fill/order status (arrives on trade activity)
Top-level envelope: `{"e":"ORDER_TRADE_UPDATE","E":<ts>,"o":{...}}`
Inner `o` object:
| Field | Meaning |
|---|---|
| `s` | Symbol |
| `c` | clientOrderId |
| `i` | orderId (venue) |
| `X` | Order status (`NEW`, `PARTIALLY_FILLED`, `FILLED`, `CANCELED`) |
| `x` | Execution type |
| `p` | Order price |
| `ap` | Average fill price |
| `z` | Cumulative filled qty (total filled so far) |
| `l` | **lastFilledQty — incremental fill for this event** |
| `L` | Last fill price |
| `n` | Commission amount |
| `N` | Commission asset |
**Critical:** `z` is cumulative; `l` is incremental per-event. `bingx_venue.py:582` reads
`lastFilledQty` = `l`. The Rust kernel's `apply_fill` now accumulates (`slot.size += l`).
### `ACCOUNT_UPDATE` — balance/position push (arrives on trade activity)
Top-level: `{"e":"ACCOUNT_UPDATE","E":<ts>,...}`
Balance array (`B`): `[{"a":"USDT","wb":<wallet_balance>,"cw":<cross_wallet_balance>}]`
Position array (`P`): `[{"s":<symbol>,"pa":<positionAmt>,"ep":<entryPrice>,"up":<unrealizedPnL>,"mt":<marginType>,"ps":<positionSide>}]`
### `FUNDING_FEE` — funding charge (arrives on funding interval)
Envelope: `{"e":"FUNDING_FEE","E":<ts>,"fs":{"s":<symbol>,"fa":<fundingAmount>,"a":<asset>}}`
Identified by `m == "FUNDING_FEE"` in some variants, or `e == "FUNDING_FEE"`.
---
## VST ↔ LIVE symmetry notes
- Same `POST /openApi/user/auth/userDataStream` endpoint, same signing method
- VST WS base: `wss://vst-open-api-ws.bingx.com/swap-market`
- LIVE WS base: `wss://open-api-swap.bingx.com/swap-market`
- Only difference: base hostname — **all frame schemas are identical**
- `bingx_user_stream.py` must use `base_url_ws_private` from config (already in `BingxExecClientConfig`)
---
## listenKey lifecycle
```
POST /openApi/user/auth/userDataStream {} → {"listenKey": "..."}
PUT /openApi/user/auth/userDataStream {"listenKey":..} → {} (keepalive, every 1800s)
DELETE /openApi/user/auth/userDataStream {"listenKey":..} → {} (on close)
```
listenKey TTL: ~60 min. Keepalive extends it. Server signals expiry via `{"e":"listenKeyExpired"}`.
---
## Open items for Phase 2
- `executionReport` schema: confirmed from BLUE observer.py analysis; verify against live VST
fill when first Phase 2 order is placed
- `ACCOUNT_UPDATE` balance fields: `wb` (wallet balance), `cw` (cross wallet balance)
- Funding fee `fs.fa` sign convention (positive = received, negative = paid) — to verify
- 24h connection cap: BingX closes the socket after ~24h regardless of keepalive;
overlap-rotation strategy required (open new connection before closing old)

View File

@@ -1,720 +0,0 @@
# CRITICAL: DITAv2 Execution Kernel — 13 Structural Flaws
**Analysis date:** 2026-05-30
**Analyst:** Systematic code review across Rust kernel, Python bridge, venue adapters, and test infrastructure
**Scope:** Full DITAv2 pipeline — `kernel.py``rust_backend.py``_rust_kernel/src/lib.rs``bingx_venue.py``bingx_direct.py` → BingX REST
---
## How to read this document
Each flaw follows the same structure:
| Section | What you'll find |
|---------|-----------------|
| **Location** | File path(s) and approximate line numbers |
| **Nature** | What kind of defect — structural, logic, protocol, edge-case, missing-feature |
| **Downstream effect** | What breaks in practice, not just what the code does wrong |
| **Exploit / trigger** | The exact sequence of events that manifests the bug |
| **Why it's not caught** | Why existing tests (142/142 pass) don't detect it |
| **Fix strategy** | High-level approach; no patch code here |
---
## Flaw 1: Entry-order cancellation is structurally broken
**Location:** `rust_backend.py` lines ~470475 (Python bridge), `_rust_kernel/src/lib.rs` lines ~660685 (Rust `process_intent` CANCEL branch), `_rust_kernel/src/lib.rs` lines ~740748 (Rust `on_venue_event` CANCEL_ACK branch)
**Nature:** Missing feature / logic gap — two-layer hole
### Downstream effect
A CANCEL intent submitted for an entry order (slot in `ORDER_REQUESTED` or `ENTRY_WORKING`) is silently ignored. The venue is never called, so the order remains live on the exchange. The caller receives an `accepted=False, diagnostic_code=NO_ACTIVE_EXIT_ORDER` outcome but no error is raised — normal execution continues.
With MARKET orders (the only type tested in the 142-scenario suite), this doesn't matter because the order fills in 13 seconds, arriving before the CANCEL even runs or making the CANCEL economically irrelevant. With LIMIT orders (per `CRITICAL_NEEDED_PARTIAL_FILL_SUPPORT.md`), resting orders on the book would be **structurally impossible to cancel** through the kernel.
### Exact code path
**Layer 1 — Python bridge (rust_backend.py):**
```python
elif intent.action == KernelCommandType.CANCEL:
emitted_events = self.venue.cancel(
self.slot(intent.slot_id).active_exit_order, # ← None for entry-only slots
...
) if self.slot(intent.slot_id).active_exit_order else [] # ← always []
```
The guard `if self.slot(...).active_exit_order` evaluates to `False` for any slot that only has an entry order. `emitted_events` stays `[]`. The venue's `cancel()` is never called.
**Layer 2 — Rust kernel process_intent (lib.rs):**
```rust
KernelCommandType::CANCEL => {
if slot.active_exit_order.is_none() {
return KernelResult {
outcome: KernelOutcome {
accepted: false,
diagnostic_code: KernelDiagnosticCode::NO_ACTIVE_EXIT_ORDER,
...
},
...
};
}
// ... code only reachable if active_exit_order.is_some()
}
```
The Rust kernel also only looks for an exit order. It returns `NO_ACTIVE_EXIT_ORDER` for entry cancels.
**Layer 3 — Rust kernel on_venue_event CANCEL_ACK (lib.rs):**
```rust
KernelEventKind::CANCEL_ACK => {
if slot.active_exit_order.is_some() {
slot.active_exit_order = None;
slot.fsm_state = TradeStage::POSITION_OPEN;
}
}
```
Even if a CANCEL_ACK somehow arrived for an entry order, the Rust FSM has no branch to transition `ENTRY_WORKING → IDLE` on cancel. The slot would remain stuck.
### Why it's not caught
The test suite has:
- `cancel_entry_order` — ENTER → sleep 1s → CANCEL. By 1s the MARKET order has filled, so the slot is already POSITION_OPEN, making the CANCEL technically valid against active_exit_order? No — it's active_entry_order that's filled. But wait: when the entry fills, the Rust kernel transitions to POSITION_OPEN and keeps `active_entry_order` in place (filled state). `active_exit_order` is still None. So the CANCEL still hits NO_ACTIVE_EXIT_ORDER. But the test only checks that capital is positive and exchange is flat — it never checks `outcome.accepted` or `outcome.diagnostic_code` for the CANCEL call.
- `cancel_idempotent` — Same pattern: ENTER → sleep 0.5s → CANCEL.
- `double_cancel` — Same.
- All checks are pass/fail on capital + exchange flatness, not on whether the cancel actually did anything.
### Fix strategy
1. Add an `order_action` field to `KernelIntent` (or use existing `action`) to distinguish entry-cancel from exit-cancel
2. In the Python bridge, call `venue.cancel()` on `active_entry_order` when the intent is CANCEL and `active_exit_order` is None
3. In the Rust kernel, add an `active_entry_order` branch to `process_intent(CANCEL)` that transitions `ENTRY_WORKING / ORDER_REQUESTED → IDLE`
4. In the Rust kernel, add an `active_entry_order` branch to `on_venue_event(CANCEL_ACK)` that transitions to IDLE
---
## Flaw 2: Rust CANCEL FSM has no entry-order reset path
**Location:** `_rust_kernel/src/lib.rs` lines ~740748
**Nature:** Missing FSM case — the `on_venue_event` handler for `CANCEL_ACK` only handles exit orders
### Downstream effect
Even if the Python bridge were fixed to call `venue.cancel()` on the active entry order (fixing Flaw 1), and even if BingX returned a successful cancel-ack, the Rust kernel **would not update the slot state**. The slot would remain in `ENTRY_WORKING` with `active_entry_order` still attached. The kernel would believe the order is still live on the exchange.
No subsequent `ENTER` intent would be accepted (SLOT_BUSY). The slot would be permanently deadlocked until a manual `reconcile_from_slots` overwrites it.
### Exact code path
```rust
KernelEventKind::CANCEL_ACK => {
if slot.active_exit_order.is_some() {
slot.active_exit_order = None;
slot.fsm_state = TradeStage::POSITION_OPEN;
}
// No else branch — silent no-op for entry cancels
}
```
The full FSM transition matrix for CANCEL_ACK should include:
- `ENTRY_WORKING, active_entry_order.is_some()` → clear entry order, set IDLE
- `EXIT_WORKING, active_exit_order.is_some()` → clear exit order, set POSITION_OPEN (existing code)
### Why it's not caught
Same reason as Flaw 1 — the cancel never fires, so CANCEL_ACK never arrives. The code path has never been exercised.
### Fix strategy
Add an `else if` branch:
```rust
} else if slot.active_entry_order.is_some() {
slot.active_entry_order = None;
slot.trade_id.clear();
slot.asset.clear();
slot.side = TradeSide::FLAT;
slot.size = 0.0;
slot.initial_size = 0.0;
slot.fsm_state = TradeStage::IDLE;
}
```
---
## Flaw 3: Python `process_intent` overwrites outcome with mixed-epoch state
**Location:** `rust_backend.py` lines ~490505
**Nature:** Data consistency — returned `KernelOutcome` mixes pre-venue and post-venue state
### Downstream effect
Any caller inspecting the returned `KernelOutcome` from `process_intent()` gets misleading information:
- `diagnostic_code` is from the Rust kernel's pre-venue opinion
- `state` is from the slot **after** venue events were processed
- `transitions` only contains pre-venue transitions
- `emitted_events` correctly contains post-venue events
A caller checking `outcome.accepted == True` and `outcome.state == ORDER_REQUESTED` (the Rust kernel's initial state) would be wrong — the slot is actually already in `POSITION_OPEN` because the fill arrived within the same function call.
### Exact code path
```python
result = _get_rust().process_intent(...) # Rust: IDLE → ORDER_REQUESTED
outcome = _outcome_from_payload(result["outcome"]) # state=ORDER_REQUESTED
# ... venue.submit() ... on_venue_event() ... transitions slot through ENTRY_WORKING → POSITION_OPEN
final_slot = self._get_slot(outcome.slot_id) # fsm_state=POSITION_OPEN now
final_outcome = KernelOutcome(
state=final_slot.fsm_state, # POSITION_OPEN ← post-venue
diagnostic_code=outcome.diagnostic_code, # OK ← pre-venue
transitions=outcome.transitions, # [IDLE→ORDER_REQUESTED] ← incomplete
emitted_events=tuple(emitted_events), # [ORDER_ACK, FULL_FILL] ← correct
)
```
### Why it's not caught
No test inspects `outcome.transitions` or validates that `outcome.state` matches `outcome.diagnostic_code`. The `outcome_inspect_entry` test (`_gen_test.py` body) checks `len(info["transitions"]) > 0` — which passes because there's at least one — and `info["diagnostic"] == "OK"`. It doesn't check that the state in the outcome matches the diagnostic or that all transitions are present.
### Fix strategy
Either:
1. Re-read the Rust outcome after venue events complete (costly — additional FFI call), or
2. Emit the venue-event transitions back from `on_venue_event` and append them to the returned outcome, or
3. Document that `outcome.transitions` is a partial snapshot and the caller should inspect the slot directly via `k.slot(n)` for current state
---
## Flaw 4: Multi-leg exit final leg can double-close and double-settle
**Location:** `_rust_kernel/src/lib.rs` lines ~775830, specifically the `apply_fill` exit path in `on_venue_event`
**Nature:** Logic error — redundant state mutation
### Downstream effect
When a FULL_FILL closes the last leg of a multi-leg exit, the Rust kernel sets `slot.fsm_state = CLOSED` and `slot.closed = true` in two separate code blocks. Block A does it based on `active_leg_index`, block B does it independently based on `slot.size <= 1e-12`. Both blocks run on the same event.
In practice this doesn't double-settle because the Python side processes a single `on_venue_event` call. But the slot state after the event is unpredictable — block B clears `active_entry_order` and `active_exit_order` that block A left in place. If any code path depends on inspecting the orders after a close (e.g., for journaling), it sees inconsistent state.
### Exact code path
```rust
// Block A (lines ~780-800):
if slot.active_leg_index >= slot.exit_leg_ratios.len() {
slot.closed = true;
slot.fsm_state = TradeStage::CLOSED;
slot.active_exit_order = None;
}
// Block B (lines ~810-830), runs unconditionally after block A:
if !partial {
slot.consume_exit_leg(); // advances leg index
if slot.size <= 1e-12 {
slot.closed = true; // redundant
slot.fsm_state = TradeStage::CLOSED; // redundant
slot.active_exit_order = None; // redundant
slot.active_entry_order = None; // extra — block A didn't do this
}
}
```
### Why it's not caught
The multi-leg exit tests (`multi_leg_exit`, `x4_partial_hold_exit`, all leg ratio variants) check capital integrity and exchange flatness. They don't inspect the slot's `active_entry_order` or `active_exit_order` after exit. The final capital assertion passes because `settle()` is called once per `on_venue_event` call regardless of how many times the slot's internal flags toggle.
### Fix strategy
Restructure `apply_fill` for exit fills so there's a single point where `CLOSED` is set:
- If `active_leg_index >= ratios.len()` **or** `size <= 1e-12` after the fill → set CLOSED
- Not both independently
---
## Flaw 5: Capital settlement only triggers on terminal states
**Location:** `rust_backend.py` lines ~520525
**Nature:** Accounting accuracy — intra-trade realized PnL invisible to account projection
### Downstream effect
When a LIMIT order partially fills (PARTIALLY_FILLED event), the Rust kernel correctly accumulates realized PnL on the slot:
```rust
slot.realized_pnl += realized;
```
But the Python bridge only pushes PnL to the account on terminal transitions:
```python
if slot.fsm_state in {TradeStage.CLOSED, TradeStage.TRADE_TERMINAL_WRITTEN} and slot.realized_pnl != 0.0:
self.account.settle(slot.realized_pnl)
```
During a partial fill that leaves the slot in EXIT_WORKING, the accumulated PnL sits on the slot but never reaches `account.snapshot.capital`. For a LIMIT order that partially fills over several minutes, the system's view of available capital is **stale** during the entire fill window. This could cause the system to incorrectly calculate available margin for concurrent positions.
### Exact trigger
1. Slot is in POSITION_OPEN with size=1.0
2. EXIT intent → slot moves to EXIT_WORKING
3. Venue sends PARTIALLY_FILLED: filled_size=0.3, remaining_size=0.7
4. Rust: slot.realized_pnl += +2.50 (3% gain on 30% of position)
5. Python: slot.fsm_state == EXIT_WORKING (not CLOSED) → settle() is NOT called
6. `account.snapshot.capital` still shows pre-exit value
7. Venue sends FULL_FILL: filled_size=0.7, remaining_size=0.0
8. Rust: slot.realized_pnl += +5.83 (remaining), total = 8.33
9. Python: slot.fsm_state == CLOSED → settle(8.33) → capital jumps by full amount
For 3 minutes between step 4 and step 7, all downstream consumers see wrong capital.
### Why it's not caught
All 142 tests use MARKET orders that fill instantly in one shot. There is never a multi-event fill sequence for a single order. The non-instant fills come from multi-leg exits (multiple MARKET orders), where each exit is a separate `process_intent` call with its own `on_venue_event` cycle, and each eventually reaches CLOSED independently.
### Fix strategy
Change the settle trigger to fire on **any realized PnL change**, not just on terminal state transitions:
```python
if slot.realized_pnl != self._last_settled_pnl.get(slot.slot_id, 0.0):
incremental = slot.realized_pnl - self._last_settled_pnl[slot.slot_id]
self.account.settle(incremental)
self._last_settled_pnl[slot.slot_id] = slot.realized_pnl
```
Or simpler: settle the delta every time `on_venue_event` processes a fill event, regardless of slot state.
---
## Flaw 6: `_legacy_intent()` silently drops `order_type` and `limit_price`
**Location:** `bingx_venue.py` lines ~280295
**Nature:** Chain break — data loss at the Python level
### Downstream effect
The `CRITICAL_NEEDED_PARTIAL_FILL_SUPPORT.md` spec adds `order_type` and `limit_price` to `KernelIntent`. But there are **two** venue adapters, and one of them strips the new fields:
**BingxVenueAdapter** receives `KernelIntent` and converts to `LegacyIntent`:
```python
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
receipt = self._call_backend("submit_intent", self._legacy_intent(intent))
```
`_legacy_intent()` builds a `LegacyIntent` — which has no `order_type` or `limit_price` fields:
```python
return LegacyIntent(
timestamp=intent.timestamp,
trade_id=intent.trade_id,
decision_id=intent.intent_id,
asset=intent.asset,
action=action,
side=side,
reason=intent.reason,
target_size=float(intent.target_size),
leverage=float(intent.leverage),
reference_price=float(intent.reference_price),
confidence=1.0,
bars_held=0,
exit_leg_ratios=tuple(intent.exit_leg_ratios or (1.0,)),
metadata=dict(intent.metadata),
# order_type and limit_price are NOT HERE — silently dropped
)
```
The `BingxDirectExecutionAdapter.submit_intent()` receives `LegacyIntent` and uses `intent.action`, `intent.side`, `intent.target_size`, etc. — none of which carry the new fields.
**MockVenueAdapter** receives `KernelIntent` directly and *would* see the new fields — but it only uses `intent.target_size`, `intent.reference_price`, `intent.side`, and `intent.action`. `order_type` and `limit_price` are ignored there too.
So even after `KernelIntent` gains the new fields, **no code path exists** that reads them and passes them to the BingX REST payload.
### Exact trigger
Someone constructs:
```python
intent = KernelIntent(
action=ENTER, trade_id="t1",
order_type="LIMIT", limit_price=0.083456,
...
)
k.process_intent(intent)
```
The new fields survive through `_intent_to_payload()` to Rust (harmless — Rust ignores unknown fields), then back to Python. The Python bridge calls `venue.submit(intent)` with the `intent` that still has `order_type="LIMIT"`. But `bingx_venue.submit()` converts to `LegacyIntent` — which drops them. `bingx_direct.py` sees a MARKET order.
### Why it's not caught
The new fields don't exist yet. No test exercises LIMIT orders.
### Fix strategy
The cleanest fix is to **bypass `_legacy_intent()`** for `BingxVenueAdapter.submit()` and pass `KernelIntent` directly to the adapter. The adapter's `submit_intent()` already has access to `intent.asset`, `intent.side`, etc. It just needs to receive the right type.
If `BingxDirectExecutionAdapter` must keep accepting `LegacyIntent` for backward compatibility, encode the new fields in `LegacyIntent.metadata`:
```python
metadata = dict(intent.metadata)
metadata["_order_type"] = intent.order_type
metadata["_limit_price"] = intent.limit_price
```
Then on the adapter side, read `intent.metadata.get("_order_type", "MARKET")`.
---
## Flaw 7: Mock venue partial_fill_ratio applies to both entry and exit
**Location:** `mock_venue.py` lines ~6090
**Nature:** Test infrastructure limitation — single ratio cannot distinguish entry vs exit
### Downstream effect
The `MockVenueScenario` has one float: `partial_fill_ratio: float = 1.0`. When set to, say, `0.5`, **every** `submit()` call produces a `PARTIALLY_FILLED` event with 50% fill — regardless of whether the intent is an ENTER or an EXIT.
This makes it impossible to write a mock-venue unit test that:
- Entry fills fully (ratio=1.0) but exit fills partially (ratio=0.5)
- Entry fills partially (ratio=0.3) and then fills fully on a second submit
- Different partial ratios per leg of a multi-leg exit
### Exact code path
```python
if self.scenario.emit_fill_on_submit or self.scenario.partial_fill_ratio > 0:
fill_ratio = max(0.0, min(1.0, float(self.scenario.partial_fill_ratio)))
fill_size = float(intent.target_size) * fill_ratio
# ... emits PARTIALLY_FILLED or FULL_FILL based on ratio
# No distinction between ENTER and EXIT
```
### Why it's not caught
The mock venue is used in unit tests (`test_rust_backend.py` or similar), not in the live BingX e2e tests. The live tests use `BingxVenueAdapter` with real BingX VST, where MARKET orders always fill fully. The partial_fill_ratio path has never been used for a scenario that distinguishes entry from exit behavior.
### Fix strategy
Add per-action-type ratios:
```python
@dataclass(frozen=True)
class MockVenueScenario:
entry_partial_fill_ratio: float = 1.0
exit_partial_fill_ratio: float = 1.0
```
Or add a per-order override via `intent.metadata`.
---
## Flaw 8: Per-asset price precision helper does not exist
**Location:** `bingx_direct.py``_format_quantity()` exists (line ~150) but `_format_price()` does not
**Nature:** Missing feature — LIMIT orders will be rejected by BingX
### Downstream effect
BingX requires the `price` field of a LIMIT order to have the correct decimal precision for each symbol. The `_format_quantity()` method resolves `size_increment` from the instrument provider and quantizes the quantity. No equivalent exists for price.
Without it, submitting a LIMIT order with `limit_price=0.08` for TRXUSDT sends `"price": "0.08"` to BingX. BingX expects 6 decimal places for TRXUSDT prices (e.g., `0.083456`). The order is rejected with `"code": 100001, "msg": "Invalid price precision"`.
| Symbol | Approx price | Required decimals | `limit_price` value | What BingX expects |
|--------|-------------|-------------------|-------------------|-------------------|
| TRXUSDT | $0.08 | 6 | 0.083456 | `"0.083456"` |
| XRPUSDT | $0.52 | 4 | 0.5234 | `"0.5234"` |
| ADAUSDT | $0.45 | 4 | 0.4523 | `"0.4523"` |
| DOGEUSDT | $0.15 | 5 | 0.15234 | `"0.15234"` |
| BTCUSDT | $60,000 | 2 | 60000.50 | `"60000.50"` |
### Why it's not caught
No LIMIT orders are submitted. All 142 tests use MARKET orders where `type="MARKET"` and no `price` field is sent.
### Fix strategy
Add `_format_price(self, asset: str, price: float) -> str` mirroring `_format_quantity`:
```python
def _format_price(self, asset: str, price: float) -> str:
instrument = self._resolve_instrument(asset)
if instrument is not None:
try:
price_step = Decimal(str(instrument.price_increment.as_decimal()))
value = Decimal(str(price))
quantized = (value / price_step).to_integral_value(rounding=ROUND_DOWN) * price_step
return _decimal_text(quantized)
except Exception:
pass
return f"{price:.8f}".rstrip("0").rstrip(".")
```
The instrument provider already exposes `price_increment` — it just needs to be accessed.
---
## Flaw 9: Cancel path falls back to trade_id as symbol
**Location:** `bingx_venue.py` lines ~300310 (within `cancel()`)
**Nature:** Logic error — wrong variable in fallback chain
### Downstream effect
When `BingxVenueAdapter.cancel()` is called and the order's `metadata` dict lacks an `"asset"` key, it falls back:
```python
asset = str(order.metadata.get("asset") or order.internal_trade_id or order.venue_client_id or "")
```
`order.internal_trade_id` is the system's trade_id (e.g., `"cancel-idle-1712345678"`). This gets fed to `self.backend._instrument_venue_symbol(asset)` which does:
```python
def _instrument_venue_symbol(self, asset: str) -> str:
text = _normalize_symbol(asset) # "CANCEL-IDLE-1712345678"
if text.endswith("USDT"):
return f"{text[:-4]}-USDT" # "CANCEL-IDLE-1712345678"-USDT — nonsense
return text # doesn't end with USDT → returns the garbage
```
The cancel HTTP call is sent to BingX with a symbol that doesn't exist. BingX returns an error or silently ignores the request. The cancel silently fails.
This can happen whenever a `VenueOrder` is constructed without `metadata["asset"]`. The mock venue's `_event_from_order` sets `metadata={"intent_id": ..., "action": ...}` but does **not** include `"asset"`. So any cancel path triggered from a mock venue event will hit this bug.
### Exact trigger sequence
1. `MockVenueAdapter.submit()` creates a `VenueOrder` with `metadata={"intent_id": ..., "action": ...}` — no `"asset"`
2. The kernel attaches this order to the slot
3. A CANCEL intent arrives
4. Python bridge calls `self.venue.cancel(self.slot(slot_id).active_entry_order)`
5. `BingxVenueAdapter.cancel()` does `order.metadata.get("asset")` → None
6. Falls back to `order.internal_trade_id` → a trade_id string
7. Sends delete to BingX with a bogus symbol
Note: this only occurs when the mock venue is used in a test configuration. In live mode, `BingxDirectExecutionAdapter` stores richer metadata. But the fallback chain is still wrong and could bite in edge cases.
### Why it's not caught
The live tests always have `metadata["asset"]` populated because the kernel attaches it before calling the venue. The mock venue's cancel path is only exercised in unit tests that don't check the BingX HTTP call content.
### Fix strategy
Change the fallback to use the order's `internal_trade_id` to look up the slot's asset from the kernel, not try to interpret it as a symbol:
```python
# In cancel(), before the fallback:
slot = self._kernel.slot(order.metadata.get("slot_id", 0))
asset = str(order.metadata.get("asset") or slot.asset or "")
```
Or at minimum, add the asset to the mock venue's event metadata.
---
## Flaw 10: Event dedup window is bounded at 64
**Location:** `_rust_kernel/src/lib.rs` lines ~5 (constant), ~850855 (eviction logic)
**Nature:** Resource management — fixed-size ring buffer with silent eviction
### Downstream effect
Each `TradeSlot` tracks seen events in `seen_event_ids: Vec<String>`. When the vector exceeds 64 entries, the oldest entries are drained:
```rust
if slot.seen_event_ids.len() > MAX_SEEN_EVENT_IDS {
let overflow = slot.seen_event_ids.len() - MAX_SEEN_EVENT_IDS;
slot.seen_event_ids.drain(0..overflow);
}
```
This means:
- Events 164 are deduplicated correctly
- When event 65 arrives, event 1 is evicted. If event 1 arrives again, it's accepted as new
- When event 66 arrives, event 2 is evicted, etc.
- After 64 unique events, the dedup window is a rolling window of the last 64 events
With MARKET orders (13 events per trade), a slot would need ~2060 trades before cycling through 64 events. With LIMIT orders that may receive many partial fills per order (e.g., a resting order that gets 5 fills/hour over 6 hours = 30 events), the limit could be hit in a single trade.
### Why it's not caught
No test submits more than ~30 events to a single slot (`rapid_ten_cycle` does 10 entry→exit cycles = ~30 events). The 64 limit was never reached.
### Fix strategy
Either:
1. Increase `MAX_SEEN_EVENT_IDS` to a larger value (256 or 1024), or
2. Use a proper LRU/size-bounded set (e.g., `LruCache` from the `lru` crate), or
3. Change to a HashMap-based dedup keyed by `(event_id, action)` so eviction is explicit
---
## Flaw 11: Reconcile is a raw state override with no FSM validation
**Location:** `_rust_kernel/src/lib.rs` lines ~900915 (`dita_kernel_reconcile_slots_json`)
**Nature:** Safety — no guards on incoming state
### Downstream effect
The reconcile function blindly overwrites slot state:
```rust
for slot in slots {
if slot.slot_id < core.slots.len() {
core.slots[slot.slot_id] = slot.clone();
}
}
```
There is **zero validation** that the incoming slot state is a valid successor to the current state. A caller could:
- Set `fsm_state = POSITION_OPEN` with `size = 0.0` — the kernel thinks it has an open position with no size
- Set `fsm_state = CLOSED` with `size = 5.0` — the kernel thinks a position is closed but still has size
- Set `fsm_state = ENTRY_WORKING` with `trade_id = ""` — the kernel is in "entry working" state for no trade
- Clear `seen_event_ids` to reset dedup — silently accepting duplicates
The intended use is restoring kernel state from a snapshot after a crash, where the slot state was explicitly serialized by a previous `kernel.snapshot()`. In that case the state should be self-consistent. But there's no guard against malformed or corrupted snapshot data.
### Why it's not caught
The reconcile tests (`reconcile_empty`, `reconcile_after_entry`, etc.) all reconcile with self-consistent slot data from `k.slot(0)`. They never feed malformed state. The `fresh_kernel_reconcile_*` tests similarly use `_slot_from_payload` on data serialized from a real slot.
### Fix strategy
Add validation in the Rust kernel (or Python bridge) that checks basic consistency:
- `fsm_state == POSITION_OPEN``size > 0` and `asset` non-empty
- `fsm_state == IDLE``size == 0` and `trade_id` empty
- `fsm_state == CLOSED``closed == true`
- `size >= 0`
- `slot_id` matches array index
---
## Flaw 12: `outcome.transitions` is incomplete — pre-venue only
**Location:** `rust_backend.py` lines ~490505, `_rust_kernel/src/lib.rs` lines ~700710
**Nature:** API contract — returned data is a partial snapshot
### Downstream effect
`process_intent()` runs three phases in sequence:
1. **Rust kernel** processes the intent (pure FSM: `IDLE → ORDER_REQUESTED`)
2. **Venue adapter** submits to exchange (HTTP call, receives ack + fill)
3. **on_venue_event** called per venue response (ORDER_ACK → ENTRY_WORKING, FULL_FILL → POSITION_OPEN)
Each phase produces `KernelTransition` records. But only **phase 1** transitions appear in the returned `KernelOutcome.transitions`:
```python
final_outcome = KernelOutcome(
...
transitions=outcome.transitions, # from Rust — phase 1 only
emitted_events=tuple(emitted_events), # from venue — phases 2-3
...
)
```
A caller inspecting transitions sees `[IDLE → ORDER_REQUESTED]` and has no way to discover that `[ORDER_REQUESTED → ENTRY_WORKING]` and `[ENTRY_WORKING → POSITION_OPEN]` also occurred. The journal (`ClickHouseKernelJournal`) records all transitions correctly — but the returned `KernelOutcome` is the API surface that callers interact with.
### Why it's not caught
The `outcome_inspect_entry` test checks `len(info["transitions"]) > 0` and `info["diagnostic"] == "OK"`. It doesn't validate that all expected transitions are present. The transitions are journaled to the debug sink, but no test reads the journal.
### Fix strategy
Collect transitions from phases 2-3 and append them to the outcome:
```python
all_transitions = list(outcome.transitions)
for event in emitted_events:
event_outcome = self.on_venue_event(event)
all_transitions.extend(event_outcome.transitions)
final_outcome = KernelOutcome(..., transitions=tuple(all_transitions), ...)
```
Or document that `transitions` is an incomplete snapshot and the journal is the authoritative source.
---
## Flaw 13: Slot realized PnL is not reset on re-entry after partial exit
**Location:** `_rust_kernel/src/lib.rs` lines ~575600 (ENTER intent handler), specifically slot reset
**Nature:** State leakage — accumulated PnL from prior trade survives into next cycle
### Downstream effect
When an ENTER intent arrives, the Rust kernel resets most slot fields:
```rust
slot.trade_id = intent.trade_id.clone();
slot.asset = intent.asset.clone();
slot.side = intent.side.clone();
slot.entry_time = Some(intent.timestamp);
slot.entry_price = 0.0;
slot.size = 0.0;
slot.initial_size = 0.0;
slot.unrealized_pnl = 0.0;
slot.realized_pnl = 0.0; // ← reset to zero
slot.exit_leg_ratios = ...;
slot.active_leg_index = 0;
slot.active_entry_order = None;
slot.active_exit_order = None;
slot.closed = false;
slot.last_event_time = None;
slot.fsm_state = TradeStage::ORDER_REQUESTED;
```
`slot.realized_pnl = 0.0` is explicitly set — correct for a fresh trade. But recall from Flaw 5 that realized PnL from partial fills (before the terminal close) may **not yet have been settled** to the account. If the slot accumulates realized PnL during partial fills, then re-enters before the final settle happens, the in-flight PnL is **zeroed without being settled**.
**This is actually the correct behavior** because:
1. All MARKET-order fills settle immediately (they arrive as FULL_FILL and transition to CLOSED in one shot)
2. For LIMIT orders that partially fill, the re-entry scenario is impossible because the slot isn't IDLE — it can't accept a new ENTER until the position is fully closed
3. The slot CAN re-enter after a full close, and by then all PnL has been settled
So this is a **latent** rather than active flaw. It would manifest if:
1. A LIMIT order partially fills (PnL on slot, not settled)
2. The remaining limit is cancelled
3. The slot's `consume_exit_leg()` leaves the slot in POSITION_OPEN with `size > 0` and `!closed` but no active orders
4. Another ENTER arrives — but the Rust kernel rejects it because `!slot.is_free()`
So the slot design prevents this from happening accidentally. The flaw is that if a future code path bypasses the `is_free()` check (e.g., a force-enter feature), the unreleased PnL would be silently zeroed.
### Why it's not caught
The scenario can't happen with the current FSM. All fills eventually reach CLOSED, which triggers settle. No test forces an entry on a non-free slot.
### Fix strategy
Add an explicit assertion or sentinel in the ENTER handler:
```rust
if slot.realized_pnl.abs() > 1e-10 {
// Log warning: unsynchronized PnL being discarded
}
```
Or enforce that `settle()` is always called before `realized_pnl` is reset, by moving the settle trigger to the Rust side.
---
## Summary table
| # | Flaw | Layer | Severity | Blocks partial-fill? |
|---|------|-------|----------|---------------------|
| 1 | Entry-order cancellation broken | Python + Rust | **Critical** | **Yes** — can't cancel resting LIMIT entries |
| 2 | No CANCEL_ACK → IDLE for entry | Rust FSM | **Critical** | **Yes** — slot stuck after cancelled entry |
| 3 | Outcome mixes pre/post-venue state | Python bridge | Medium | No |
| 4 | Multi-leg exit double-close | Rust FSM | Low | No |
| 5 | Capital settle only on terminal state | Python bridge | **High** | **Partial** — stale capital during partial fills |
| 6 | order_type/limit_price dropped in legacy intent | Python venue | **Critical** | **Yes** — LIMIT orders never reach BingX |
| 7 | Mock venue single ratio for entry+exit | Mock venue | Low | No (mock tests only) |
| 8 | Missing price formatting | Adapter | **High** | **Yes** — BingX rejects bad price precision |
| 9 | Cancel falls back to trade_id as symbol | Python venue | Medium | No |
| 10 | Event dedup window at 64 | Rust FSM | Low | No |
| 11 | Reconcile has no FSM validation | Rust FSM | Low | No |
| 12 | Outcome transitions incomplete | Python bridge | Medium | No |
| 13 | Unsettled realized PnL on re-entry | Rust FSM | Low | No |
**6 critical/high** — must be fixed before safe LIMIT order / partial-fill deployment.
**4 medium** — should be fixed in the same pass to keep hygiene.
**3 low** — latent; fix opportunistically.

View File

@@ -1,299 +0,0 @@
# CRITICAL: Partial Fill Support — Kernel, Adapter & Test Suite
**Date:** 2026-05-29
**Author:** E2E test-automation analysis
**Status:** Not implemented — spec for the next work session
---
## The gap
**Zero tests exercise a `PARTIALLY_FILLED` venue event.** Every scenario submits `MARKET` orders (hardcoded in `BingxDirectExecutionAdapter.submit_intent()` line 359). On liquid testnet pairs (TRXUSDT, XRPUSDT, ADAUSDT), market orders fill **instantly in one shot**. The kernel's `on_venue_event` handler handles `PARTIAL_FILL``KernelEventKind.PARTIAL_FILL` → slot FSM transition, but **this code has never executed on a live exchange** in the existing 142-scenario suite.
The multi-leg exit system (50% + 50% sequential `EXIT` intents) exercises *synthetic* partial fills — two separate MARKET orders each exiting half. That is **not** a true exchange-level partial fill where one order receives multiple fill events with a `remaining_size` > 0 between them.
---
## What needs to change
Three layers must be touched:
1. **`KernelIntent` (contracts.py)** — add `order_type` and `limit_price` fields
2. **`BingxDirectExecutionAdapter` (bingx_direct.py)** — read the new fields; build payload with correct `"type": "LIMIT"` and `"price"`
3. **`BingxVenue` (bingx_venue.py)** — read the new fields from `KernelIntent` when building receipt; propagate limit price to acknowledge events
4. **Test file (test_bingx_live.py)** — add scenarios that submit LIMIT orders at non-aggressive prices to produce partial fills
---
## Layer 1: `KernelIntent` — two new fields
**File:** `prod/clean_arch/dita_v2/contracts.py`
```python
@dataclass(frozen=True)
class KernelIntent:
timestamp: datetime
intent_id: str
trade_id: str
slot_id: int
asset: str
side: TradeSide
action: KernelCommandType
reference_price: float
target_size: float
leverage: float
exit_leg_ratios: Tuple[float, ...] = (1.0,)
reason: str = ""
metadata: Dict[str, Any] = field(default_factory=dict)
stage: TradeStage = TradeStage.INTENT_CREATED
# === NEW FIELDS ===
order_type: str = "MARKET" # "MARKET" | "LIMIT" | "POST_ONLY"
limit_price: float = 0.0 # ignored if order_type == "MARKET"
```
**Rationale for defaults:** Existing call sites that construct `KernelIntent(...)` directly (all 142 test bodies, `_si()` helper, the intent projection code) do not pass `order_type` or `limit_price` — they get MARKET by default. Zero code changes outside the intent paths that intentionally want LIMIT orders.
**Rust kernel implications:** The Rust backend serializes `KernelIntent` to JSON before passing to the `.so`. The new fields must be included in that JSON serialization. Check `_intent_to_payload` or equivalent serialization in the Python proxy:
```python
# In rust_backend.py — wherever KernelIntent is serialized
payload = {
"timestamp": intent.timestamp.isoformat(),
"intent_id": intent.intent_id,
# ... existing fields ...
"order_type": intent.order_type, # NEW
"limit_price": intent.limit_price, # NEW
}
```
The kernel's Rust code will receive `order_type` and `limit_price` in its intent route. If it ignores them (doesn't use them for any FSM logic), that's fine — they're pass-through fields for the venue adapter. But they **must be in the serialized JSON** so the adapter can read them.
---
## Layer 2: `BingxDirectExecutionAdapter` — use `order_type` and `limit_price`
**File:** `prod/clean_arch/adapters/bingx_direct.py`
### Current (line 359)
```python
payload: dict[str, Any] = {
"symbol": symbol,
"side": side,
"positionSide": "BOTH",
"type": "MARKET", # HARDCODED
"quantity": self._format_quantity(intent.asset, intent.target_size),
"clientOrderId": client_order_id,
"recvWindow": str(int(self._config.recv_window_ms)),
}
if reduce_only:
payload["reduceOnly"] = "true"
```
### Required
```python
order_type = (intent.order_type or "MARKET").upper()
# POST_ONLY is a LIMIT that must not take liquidity — BingX calls it a "limit maker"
if order_type == "POST_ONLY":
order_type = "LIMIT" # BingX uses a separate flag for post-only
payload: dict[str, Any] = {
"symbol": symbol,
"side": side,
"positionSide": "BOTH",
"type": order_type,
"quantity": self._format_quantity(intent.asset, intent.target_size),
"clientOrderId": client_order_id,
"recvWindow": str(int(self._config.recv_window_ms)),
}
if order_type == "LIMIT" and intent.limit_price > 0:
# BingX requires "price" and "timeInForce" for LIMIT orders
price = intent.limit_price
# Ensure price has the right decimal precision for the symbol
payload["price"] = self._format_price(intent.asset, price)
payload["timeInForce"] = "GTC" # Good-Til-Cancelled (or "IOC" for immediate-or-cancel)
if order_type_orig == "POST_ONLY":
payload["timeInForce"] = "GTX" # Post-only = GTX on BingX
if reduce_only:
payload["reduceOnly"] = "true"
```
`_format_price` likely doesn't exist yet. Add it. For TRXUSDT it needs 6 decimal places (price ~$0.08), for XRPUSDT it needs 4 (`$0.52`). The quantity formatter already handles this — `_format_quantity` uses a symbol→precision lookup. Same approach for price.
**BingX LIMIT order caveats (VST testnet):**
- `"price"` must have the correct decimal precision per symbol or the order is rejected.
- `"timeInForce"` defaults to GTC if omitted — document this.
- POST_ONLY = LIMIT + `"timeInForce": "GTX"`. BingX VST supports it.
- **Partial fills are guaranteed** when a LIMIT order's price straddles the spread and only part of the quantity matches against the book.
---
## Layer 3: `BingxVenue` event emission for LIMIT orders
**File:** `prod/clean_arch/dita_v2/bingx_venue.py`
### `submit()` method (line ~348)
The `_legacy_intent(intent)` conversion currently drops `order_type`/`limit_price`. Update:
```python
def _legacy_intent(self, intent: KernelIntent) -> dict:
return {
"asset": intent.asset,
"side": intent.side,
"action": intent.action,
"target_size": intent.target_size,
"reference_price": intent.reference_price,
"leverage": intent.leverage,
"exit_leg_ratios": intent.exit_leg_ratios,
"order_type": intent.order_type, # NEW
"limit_price": intent.limit_price, # NEW
"reason": intent.reason,
}
```
### `_events_from_submit()` (line ~370+)
The `price` field in the emitted `VenueEvent` should use the `limit_price` for LIMIT orders when the fill hasn't happened yet. Currently it uses `safe_float(getattr(receipt, "price", 0.0), 0.0)` which is often 0 for market orders. For LIMIT orders the receipt should contain the price:
```python
price = (
safe_float(getattr(receipt, "price", 0.0), 0.0)
or (intent.limit_price if intent.order_type in ("LIMIT", "POST_ONLY") else 0.0)
)
```
### Reconcile path (`_event_from_row`, line ~522+)
The reconcile path already handles `PARTIALLY_FILLED` status and converts it to `KernelEventKind.PARTIAL_FILL`. It reads `filled_size` and computes `remaining_size` correctly. This code path is correct — it just needs to be triggered, which requires LIMIT orders that partially fill.
---
## Layer 4: Test scenarios
**File:** `prod/tests/test_pink_bingx_dita_live_e2e.py`
All new scenarios are kernel-direct — they construct `KernelIntent` directly with `order_type="LIMIT"` and a `limit_price` that guarantees a partial fill.
### Strategy for guaranteed partial fills on BingX VST
The testnet's order book has bid/ask spread. For a **BUY/LONG** LIMIT order:
- Set `limit_price` *between* the best bid and best ask.
- The order will match against any asks at or below `limit_price`.
- If `limit_price` is below the lowest ask, only part of the quantity fills.
- The remaining becomes a resting limit order.
For a **SELL/SHORT** LIMIT order:
- Set `limit_price` *between* the best bid and best ask.
- The order will match against any bids at or above `limit_price`.
- Remaining becomes a resting limit order.
**Easiest approach:** Use `iceberg` / hidden-order techniques aren't needed — just set `limit_price = p * 0.9995` (0.05% inside the spread) so that an approximate half of the order walks the book and the rest sits on the book. On liquid pairs this produces a `PARTIALLY_FILLED` status on the ack.
### Scenario: `limit_partial_entry_cancel`
```
1. Fetch current price p.
2. Submit LIMIT SHORT ENTER at limit_price = p * 1.0005 (slightly above market for short = inside spread) with target_size=0.002
3. Sleep 300ms.
4. Check remaining size — if > 0, cancel the resting portion.
5. If slot still occupied (fill happened), exit the filled portion.
6. Verify: exchange flat, capital integrity.
```
Outcomes:
- If partial fill: `VenueEvent` with `PARTIALLY_FILLED` status, `remaining_size > 0`. Cancel stops the resting leg. Kernel processes `CANCEL_ACK` and leaves slot with the filled partial. Exit clears it.
- If full fill: Immediately filled. Cancel is a no-op. Exit clears.
- If no fill: No fill at all. Cancel removes the LIMIT from the book. Slot returns to IDLE trivially.
### Scenario: `limit_resting_then_cancel`
```
1. Submit LIMIT SHORT ENTER at limit_price = p * 0.995 (below market — won't fill for SHORT sell).
2. Sleep 1s.
3. Assert slot is in ENTRY_WORKING (limit resting on book).
4. Cancel.
5. Verify: slot IDLE, exchange has no position.
```
This validates the ENTRY_WORKING state with a resting limit order — none of the 142 existing tests ever leave an order working for more than ~1s before a MARKET fill.
### Scenario: `limit_partial_multi_leg_exit`
```
1. Enter SHORT via MARKET (normal fill).
2. Exit via LIMIT in two legs:
- LIMIT EXIT leg 1 at limit_price = p*0.997 (50% size)
- LIMIT EXIT leg 2 at limit_price = p*0.995 (50% size)
3. If remaining > 0 after each exit, cancel the resting portion and MARKET exit the rest.
4. Verify: flat, capital integrity.
```
This exercises `PARTIALLY_FILLED` on exit orders — the `on_venue_event` handler with `PARTIAL_FILL` in the exit direction.
### Scenario: `limit_quick_resting_and_reentry`
```
1. Submit LIMIT SHORT ENTER at p*0.997 (won't fill).
2. Without cancelling, submit MARKET SHORT ENTER with different trade_id.
3. Expect SLOT_BUSY rejection on the MARKET entry.
4. Cancel the resting LIMIT.
5. Submit MARKET entry and exit normally.
```
Validates that a pending limit order blocks the slot correctly.
---
## Summary table of changes
| File | Change | Risk |
|------|--------|------|
| `contracts.py` | Add `order_type: str = "MARKET"`, `limit_price: float = 0.0` to `KernelIntent` | **Low** — defaults preserve existing behaviour |
| `rust_backend.py` (serialization) | Include `order_type` and `limit_price` in JSON payload to Rust | **Low** — Rust ignores unknown fields |
| `bingx_direct.py` | Replace hardcoded `"type": "MARKET"` with dynamic field; add `price` and `timeInForce` for LIMIT; add `_format_price` helper | **Medium** — wrong decimal precision causes BingX rejection |
| `bingx_venue.py` | Pass `order_type`/`limit_price` through `_legacy_intent()`; use for `price` in VenueEvent | **Low** — pass-through only |
| `test_bingx_live.py` | Add 4+ LIMIT/partial-fill scenarios | **Low** — same pattern as existing kernel-direct tests |
## Testing the partial fill code path
Once the changes are deployed:
```
# Run partial-fill scenarios specifically
pytest prod/tests/test_pink_bingx_dita_live_e2e.py -k "limit_partial" -v --tb=short
# Check that PARTIALLY_FILLED events appear
grep "PARTIAL_FILL\|PARTIALLY_FILLED" /tmp/pink_venue.log
# Full regression — all 142 existing MARKET scenarios must still pass
pytest prod/tests/test_pink_bingx_dita_live_e2e.py --no-header -p no:cacheprovider
```
The `PARTIALLY_FILLED` event path in `bingx_venue.py` lines 408431 and `_event_from_row` lines 522574 is the code that has **zero live-test coverage today**. These scenarios would close that gap.
---
## Appendix: BingX LIMIT order API reference
From the BingX swap API (`/openApi/swap/v2/trade/order`):
| Parameter | Required | Description |
|-----------|----------|-------------|
| `symbol` | Yes | Trading pair, e.g. "TRXUSDT" |
| `side` | Yes | "BUY" or "SELL" |
| `positionSide` | Yes | "BOTH" for USDT-M perpetuals |
| `type` | Yes | "MARKET" or "LIMIT" |
| `quantity` | Yes | Contract quantity |
| `price` | No (required for LIMIT) | Order price — decimal precision depends on symbol |
| `timeInForce` | No | "GTC", "IOC", "FOK", "GTX" (post-only). Defaults to GTC. |
| `reduceOnly` | No | "true" for exits |
| `clientOrderId` | No | Client-generated ID |
| `recvWindow` | No | Timestamp recv window in ms |
For LIMIT orders on VST testnet:
- Partial fill is certain when `limit_price` is at or near the mid-price.
- Use `timeInForce="GTC"` to let the order rest.
- Use `timeInForce="GTX"` for post-only (guarantees maker, never takes liquidity — but fills may be slower).

File diff suppressed because it is too large Load Diff

View File

@@ -1,63 +0,0 @@
# Sprint 0 — DITAv2 flaw-fix verification report
**Date:** 2026-05-30
**Scope:** Verify (do not re-implement) the DITAv2 flaw fixes before migrating PINK
onto the kernel for BingX testnet (MARKET single-leg first). Source read + offline
MockVenue test execution. No exchange contact.
## Method
- Read the full Rust FSM (`_rust_kernel/src/lib.rs`, 1700 L) and the Python bridge
(`rust_backend.py`) + `account.py` + `mock_venue.py`.
- Hardened previously-vacuous guarded assertions in `test_flaws.py` so each flaw test
genuinely exercises its fix (details below).
- Ran all offline suites under `siloqy_env` with `PYTHONPATH=/mnt/dolphinng5_predict`.
## Offline test results (all green)
| Suite group | Result |
|---|---|
| `test_flaws.py` (hardened) | 35 passed |
| kernel FSM + accounting invariants + kernel bridge + multi-exit contract | 402 passed |
| pink direct-runtime, CH persistence, multi-exit integration/fuzz, restart-reconcile, rate-limit, routing, sync/async seams | 96 passed |
| **Total** | **533 passed, 0 failed** |
(Two benign warnings: `EDAIN normalizer not available` — unrelated import; one
`coroutine never awaited` inside an intentional hang-detection test.)
## Test-hardening performed (removed false-green guards)
1. **Flaw 5 / `test_partial_exit_settles_pnl_incrementally`** — was entering & exiting at
the *same* price (realized_pnl == 0) under a `if slot.realized_pnl != 0.0:` guard, so the
capital assertion never ran. Now: SHORT entry @100, exit @90 → realized PnL strictly
positive, and asserts **capital moved by EXACTLY realized PnL** (`|Δcapital realized| < 1e-9`).
This is the core single-authority invariant and is now unconditional.
2. **Flaw 2 / `test_cancel_ack_exit_still_works`** — exit auto-filled in the default scenario,
so the exit order was already gone (`if slot.active_exit_order is not None:` skipped). Now
uses `exit_partial_fill_ratio=0.5` so the exit order stays live, then asserts CANCEL_ACK
clears it and returns the slot to `POSITION_OPEN`.
3. **Flaw 9 / `test_cancel_uses_slot_asset_not_trade_id`** — guard made unconditional (ACK-only
entry deterministically leaves the entry order live).
4. **Flaw 12 / `test_transitions_count_matches_lifecycle`** — guard made unconditional.
5. **Flaw 13 / `test_pnl_warning_on_unsettled_reentry`**`if slot.is_free():` made unconditional.
## Per-flaw verdict (MARKET single-leg path = Sprint 1)
| Flaw | Severity | Fixed? | Evidence |
|---|---|---|---|
| 1 — entry-order cancel broken | Critical | **FIXED** | `lib.rs` CANCEL branch accepts entry cancel when `active_entry_order` set & state ∈ {ENTRY_WORKING,ORDER_REQUESTED,ORDER_SENT,IDLE}; bridge emits `venue.cancel`. 5 tests pass. |
| 2 — no CANCEL_ACK→IDLE for entry (hung orders) | Critical | **FIXED** | `lib.rs:1193-1212` CANCEL_ACK entry branch clears order + resets trade_id/asset/side/size/PnL → IDLE. Non-vacuous tests pass. |
| 5 — capital settle only on terminal | High | **FIXED** | bridge `on_venue_event` settles incremental `realized_pnl` per fill; `account.settle()` moves capital by exactly that amount. Exact-invariant test passes. |
| 6 — LIMIT order_type/limit_price dropped | Critical | FIXED (N/A to MARKET) | payload carries `order_type`/`limit_price`; out of scope for MARKET-only Sprint 1. |
| 4 — double-close/double-settle on final leg | Low | **FIXED** | `apply_fill` exit branch: realized accrues once/fill; `should_close` guarded by size; closed slot rejects further EXIT (`NO_OPEN_POSITION`); dup fills deduped. |
| 10 — event dedup window | Low | **FIXED** | `seen_event_ids` (cap 256, FIFO evict); duplicate events short-circuit to `DUPLICATE_EVENT`. Tests pass. |
| 11 — reconcile validation | Low | **FIXED** | `reconcile_slots_json` validates every slot via `validate_slot` and rejects the whole batch without mutating on failure. Tests pass. |
| 13 — re-entry PnL loss | Low | **FIXED** | ENTER resets realized/unrealized/size; bridge resets `_last_settled_pnl[slot]` on ENTER. Tests pass. |
| 3, 7, 8, 9, 12 | Med/Low | FIXED | covered by hardened/passing tests. |
## GATE decision
**PASS.** The MARKET-path-critical flaws (1, 2, 5) are confirmed fixed in source and proven
by non-vacuous offline tests. Sprint 1 (PINK single-leg MARKET on BingX testnet/VST) may proceed.
## Carry-forward risks (NOT GATE blockers)
- **Sprint 3 (multi-leg) sizing:** the exit branch computes `exit_size = base_size × ratio` with
`base_size = initial_size` and cumulative ratios (e.g. `0.5, 1.0`). On the final leg this can
exceed the *remaining* position; the kernel currently relies on the venue clamping the fill to
the open size. Validate on testnet before enabling `multi_exit`.
- **LIMIT / partial-fill** remains explicitly out of scope (MARKET-only bring-up).

View File

@@ -1,88 +0,0 @@
# Sprint 2 — Accounting + observability parity verification
**Date:** 2026-05-30
**Scope:** Verify (no behaviour change) that the DITAv2 PINK runtime preserves
BLUE-legacy-compatible ClickHouse row shapes in `dolphin_pink`, and that capital
authority in the hot loop is solely the kernel's `AccountProjection`. Offline only
(MockVenue / unit), no exchange contact. Continues [SPRINT0_FLAW_VERIFICATION.md].
## 1. Row-shape parity — `clean_arch/persistence/pink_clickhouse.py`
BLUE-legacy row families written, same schema / no new columns:
| Row family | Writer | Status |
|---|---|---|
| `policy_events` + `v7_decision_events` | `_write_policy_event` | ✅ |
| `account_events` | `_write_account_event` | ✅ |
| `position_state` | `_write_position_state` | ✅ |
| `status_snapshots` | `_write_status_snapshot` | ✅ |
| `trade_events` | `_write_trade_event` | ✅ (terminal close) |
| `trade_reconstruction` | `_write_trade_reconstruction` | ✅ (ENTRY/PARTIAL/EXIT) |
| `anomaly_events` | `_write_anomaly` / `record_anomaly` | ✅ |
| `trade_exit_legs` | — | ⚠️ **listed in docstring, no writer** |
`trade_exit_legs` has no emitter. It is a **multi-leg** row family → relevant to
**Sprint 3** (`DOLPHIN_PINK_PHASE=multi_exit`), not single-leg MARKET. **Not a
Sprint 1/2 blocker.** Action: add the writer when Sprint 3 is taken up, or confirm
BLUE TUI/observability does not require it for single-leg trades.
## 2. Capital authority — single source = kernel `AccountProjection`
`clean_arch/runtime/pink_direct.py` hot loop (`step`, L309-408):
- Capital is **read only** from `kernel.snapshot()["account"]` (L320, L370, L395).
- Capital is **mutated only** by `kernel.process_intent()``account.settle()` on fill.
- **No balance-poll overwrite anywhere in `step()`.** ✅
External capital writes (all outside the hot loop, by design):
- `_reconcile_position_slot` (L188-194) — the **single** place an exchange balance
snapshot seeds `account.snapshot.capital`; called at startup/recovery only.
- `connect()` (L230) seeds from the **env default** `initial_capital`, not an
exchange poll (per code comment L228-229).
- `recover_account()` (L431) re-seeds from `kernel.account.snapshot.capital`
(the kernel's own value) — **not** an exchange poll.
**Doc/code note (no change made):** `reconcile_account()` (L453) *docstring* says it
"re-seeds capital from the exchange balance as a guard against drift," but the code
path (`recover_account`) actually re-seeds from the kernel's own capital — i.e. it
does **not** overwrite from an exchange poll. Behaviour is the safe one; only the
comment overstates. Flagged for accuracy; not edited (no behaviour change w/o auth).
`pink_clickhouse.py` reads capital/peak/seq solely from `account.snapshot`
(`_capital`/`_peak_capital`/`_trade_seq`, L193-201) — no duplicate tracking. ✅
## 3. Offline test results
`siloqy_env`, `PYTHONPATH=/mnt/dolphinng5_predict`, run from repo root.
| Suite | Result |
|---|---|
| `test_pink_clickhouse_persistence.py` | ✅ pass |
| `test_pink_ditav2_accounting_invariants.py` | ✅ pass |
| `test_pink_direct_runtime.py` | ✅ pass |
| **DITAv2 PINK Sprint-2 scope** | **14 passed** |
| `test_bingx_capital_accounting_battery.py` | ❌ 2 failed — **legacy path, out of scope** |
The 2 failures are in the **legacy** Nautilus BingX execution/journal path
(`prod/bingx/execution.py` + `prod/bingx/journal.py`, imported via
`launch_dolphin_live`) — **not** a DITAv2 PINK file, untracked/pre-existing, not
modified by this engagement. Root cause: the fuzz/equivalence tests reuse
`fingerprint="fp"` across iterations, so `bingx_journal.write_snapshot` fingerprint-
dedup short-circuits the sink and `captured["row"]` is never set (`KeyError`). This
lives on the legacy side of the BLUE do-not-touch boundary → **not fixed here**.
## GATE decision
**PASS (DITAv2 PINK scope).** Row-shape parity holds for single-leg MARKET; capital
authority is single (kernel `AccountProjection`) with no hot-loop balance overwrite;
all PINK-scoped offline suites green.
## Carry-forward (Sprint 3)
-**CLOSED (offline groundwork, 2026-05-30):** `trade_exit_legs` writer added to
`pink_clickhouse.py` (`_write_trade_exit_leg`, BLUE-schema-faithful, isolated per-leg
deltas tracked via `self._leg_state`, reset on ENTER). Fires once per exit leg.
-**CLOSED (offline groundwork):** cumulative-ratio exit sizing overshoot validated —
`test_pink_multi_exit_groundwork.py::test_final_leg_overshoot_does_not_oversell` proves a
final EXIT requesting more than the remaining size clamps (size→0, no oversell, closes once).
Validation suite: 3 passed; persistence regression: 10 passed.
-**PENDING (live):** the on-exchange multi-leg run (successive MARKET exits on VST to
confirm Flaw 4 end-to-end) is deferred — requires explicit authorization for additional
live testnet orders beyond the single Sprint 1 round trip.

View File

@@ -1,444 +0,0 @@
# PINK DITAv2 — Live BingX Testnet E2E: Results & Spec
**Date:** 2026-05-29
**Suite:** `prod/tests/test_pink_bingx_dita_live_e2e.py`
**Venue:** BingX VST (validation testnet)
**Kernel:** DITAv2 `ExecutionKernel` (Rust-backed via ctypes)
**Execution mode:** Kernel-direct — bodies receive `(k, symbol, p)` and call `k.process_intent()` directly, bypassing `DecisionEngine`/`IntentEngine`.
---
### Group 20: Restart / Reconcile (6 scenarios, 6/6 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `reconcile_empty` | Call `reconcile_from_slots([])` on an idle kernel | Empty-slot reconcile is a no-op — no crash, no state corruption |
| `reconcile_after_entry` | Enter SHORT, reconcile, then exit | Slot survives reconcile in POSITION_OPEN state; exit still works |
| `reconcile_after_exit` | Enter, exit, reconcile post-close | Reconcile on a CLOSED slot is idempotent |
| `reconcile_after_cancel` | Enter, cancel, then reconcile | Cancel-ack state persists through reconcile |
| `reconcile_twice` | Two consecutive reconciles on the same slot | Double reconcile is idempotent — no double-counting |
| `reconcile_then_cancel` | Reconcile, then check if cancel still works | Kernel can still process intents after reconcile |
**Nominal market behaviour:** `reconcile_from_slots()` rebuilds the kernel's internal slot book from a list of `TradeSlot` payloads. It does not touch the exchange — it's a state-reconstruction operation. The kernel accepts it at any lifecycle stage. After reconcile, the slot FSM continues from its current state. Reconciling an empty slot list leaves all slots IDLE. Reconciling twice in a row applies the same state twice with no ill effect.
### Group 21: Chaos / Fuzz (8 scenarios, 8/8 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `concurrent_enter_cancel` | ENTER + CANCEL with zero delay in the same async tick | Kernel doesn't crash on back-to-back intents; cancel may be ack or no-op depending on race |
| `rapid_alternating` | SHORT→cancel→LONG→cancel in 200ms bursts | FSM handles rapid direction flips gracefully — no state corruption |
| `duplicate_trade_id` | Two ENTER intents with the same `trade_id` | Second is rejected (SLOT_BUSY), first proceeds normally |
| `slot_busy_double_entry` | Two ENTER intents with different trade_ids on same slot | Second returns SLOT_BUSY diagnostic code — kernel doesn't submit duplicate orders |
| `exit_on_idle_slot` | EXIT intent on an already-IDLE slot | Kernel returns diagnostic (not OK) but does not crash |
| `cancel_on_idle_slot` | CANCEL intent on an already-IDLE slot | Same graceful rejection — no exception, no venue call |
| `cancel_after_exit_fill` | Exit fills, then CANCEL arrives for the same trade | Redundant cancel is a no-op — kernel accepts it but doesn't submit to venue |
| `rapid_ten_cycle` | 10 sequential entry→exit cycles at 400ms intervals per cycle | Slot reuse stress — 10 full FSM traversals without state leaks |
**Nominal market behaviour:** All `process_intent()` calls return an `KernelOutcome` object. When the kernel rejects an intent (`SLOT_BUSY`, invalid FSM transition), it returns `accepted=False` with a descriptive `diagnostic_code` — it does not raise an exception or crash. The `concurrent_enter_cancel` test specifically validates that two intents submitted back-to-back without `await` in between both get processed. `cancel_after_exit_fill` validates the common race condition where an exit fills before the CANCEL arrives — the kernel must not send a redundant cancel to the venue. `rapid_ten_cycle` validates that 10 full FSM cycles leave the slot in IDLE with no residual state (no accumulated leg counters, no stale event IDs, no capital drift).
---
## Failure analysis
## Test architecture
All 142 scenarios share a single entry point via `@pytest.mark.parametrize`:
```
test_pink_ditav2(name, body_fn)
├── _build_rb() → builds DITAv2 bundle (kernel + venue + control plane)
├── _pick_live_symbol() → picks a symbol not currently in an exchange position
├── _snap() → fetches current market price from BingX REST
├── _run(bundle, client, body_fn, name, ic)
│ ├── pre-clean flatten (if slot occupied)
│ ├── capture capital_before = kernel.account.snapshot.capital
│ ├── await body_fn(k, symbol, p) ← the scenario
│ ├── assert capital_after > 0 # no capital wipe
│ ├── assert capital_after < capital_before * 10 # no unbounded drift
│ ├── post-clean flatten (if slot still occupied)
│ ├── _throttle(3.0) # rate-limit gap
│ └── _verify(client, vsym) → assert positions_flat # exchange-side check
└── assert result.positions_flat
```
Each scenario body is an `async def` that receives `(k, symbol, p)` — the kernel, the chosen symbol string, and the current market price as a float. The body calls the `_si()` helper which constructs a `KernelIntent` and passes it to `k.process_intent()`.
### What "PASSED" means for every test
A test passes when **all** of the following hold:
1. **No unhandled exceptions** — kernel accepts every intent without crashing.
2. **Capital integrity**`kernel.account.snapshot.capital` stays positive and within 10× of its initial value after the scenario executes.
3. **Exchange flat** — a direct `GET /openApi/swap/v2/user/positions` call to BingX confirms zero open position size for the traded symbol.
4. **No hung orders** — the slot FSM reaches `IDLE` or `CLOSED`; no entry/exit orders remain active.
### Rate limiting
A 3-second wall-clock throttle (`_throttle(3.0)`) enforces a minimum gap between each test's exchange HTTP calls. This prevents BingX rate-limit errors. With 142 tests × ~612 REST calls each, the full suite runs in ~60 min without a single rate-limit rejection.
---
## Scenario families and results
### Group 1: Basic entry/exit (9 scenarios, 9/9 PASS)
| # | Scenario | What it tests | Rationale |
|---|----------|---------------|-----------|
| 1 | `simple_entry_exit` | Enter SHORT at market, exit at 0.5% profit | Baseline — verifies the entire intent→venue→fill→settle pipeline |
| 2 | `multi_leg_exit` | Enter 2x size, exit 50% leg, exit 50% leg | Multi-leg partial-fill lifecycle — no double-counting of capital |
| 3 | `cancel_entry_order` | Enter SHORT, cancel immediately | Cancel-ack FSM transition: ENTRY_WORKING → IDLE |
| 4 | `entry_hold_exit` | Enter, wait 3s, exit | Position aged in market — mark-to-market, fill price tolerance |
| 5 | `entry_exit_at_loss` | Enter SHORT, exit at 0.5% loss (price up) | Loss exit — realized PnL is negative, capital decreases but stays positive |
| 6 | `two_sequential_cycles` | Enter→Exit→Enter→Exit on same symbol | Slot reuse — kernel resets correctly after CLOSED state |
| 7 | `entry_then_recover` | Enter SHORT, cancel, flatten if needed | Exit path after clean — replaces old buggy disconnect/reconnect body |
| 8 | `long_entry_exit` | Enter LONG at market, exit at 0.5% profit | Long-side symmetry — opposite PnL direction, same FSM |
| 9 | `cancel_idempotent` | Enter, cancel once, cancel again | Second CANCEL on already-cancelled order returns OK, not error |
**Nominal market behaviour:** BingX fills market orders at or near the requested price within 13s on VST. The kernel receives `FULL_FILL` events via the venue adapter, transitions the slot through `ENTRY_WORKING → POSITION_OPEN` (entry) and `EXIT_WORKING → IDLE` (exit). Cancel requests return `CANCEL_ACK` and the slot returns to `IDLE` without requiring an exit. Capital reflects the PnL spread (±fees) correctly.
### Group 2: Cancel combinations (6 scenarios, 6/6 PASS)
| # | Scenario | What it tests | Rationale |
|---|----------|---------------|-----------|
| 10 | `double_cancel` | Enter, cancel, cancel again | Two cancels on same active order — second is no-op not error |
| 11 | `cancel_then_exit` | Enter, cancel attempt, if slot still open → exit | Guard pattern: conditional exit only if cancel didn't flatten |
| 12 | `exit_then_cancel_exit` | Enter, exit, cancel same exit | Cancel on an exit order that may already be filling — idempotent |
| 13 | `exit_then_reentry` | Enter→Exit→re-Enter on same symbol | Slot lifecycle reset: IDLE → ... → CLOSED → IDLE → ... → OPEN |
| 14 | `limit_cancel` | Enter LIMIT at 90% market, cancel | Limit (non-market) order — if unfilled, cancel returns unfilled slot |
**Nominal market behaviour:** BingX VST fills market orders quickly. A second cancel on an already-filled order is harmless — the venue adapter returns the current state without error. The kernel's idempotency logic (tracked via `VenueEvent.event_id` dedup in the slot image) prevents duplicate economic effects.
### Group 3: X4 — combinatorial stress (10 scenarios, 10/10 PASS)
| # | Scenario | Key assertion |
|---|----------|---------------|
| 15 | `x4_partial_hold_exit` | Two-leg exit with 30%/70% ratio at different prices |
| 16 | `x4_three_leg` | Three-leg 25%/25%/50% with price step-downs |
| 17 | `x4_cancel_fill_partial` | Cancel after fill, conditional double exit |
| 18 | `x4_rapid_three` | Three rapid entry→exit cycles with decaying price |
| 19 | `x4_diff_symbol` | Enter on A, attempt exit on B (cross-symbol edge) |
| 20 | `x4_alternating` | SHORT on A, LONG on B, exit both |
| 21 | `x4_multi_flatten` | Flatten loop — call exit until slot is free |
| 22 | `x4_three_leg_25_50_25` | Three-leg with unequal 25%/50%/25% distribution |
| 23 | `x4_enter_exit_hold_twice` | Three sequential round-trips on same symbol |
| 24 | `x4_cancel_then_double_exit` | Cancel, then conditional two-leg exit |
**Nominal market behaviour:** Multi-leg exits require the kernel to track the `exit_leg_ratios` tuple and progressively consume legs. Each `EXIT` intent uses `k.slot(0).next_exit_ratio()` to determine the portion to exit. The kernel's `consume_exit_leg()` advances the leg index. Capital delta is applied exactly once per leg — verified indirectly by capital remaining within bounds across all legs.
### Group 4: 2 sides × 2 profit × 4 patterns (16 scenarios, 16/16 PASS)
| Pattern | Short profit | Short loss | Long profit | Long loss |
|---------|-------------|------------|-------------|-----------|
| `basic` | PASS | PASS | PASS | PASS |
| `partial` | PASS | PASS | PASS | PASS |
| `cancel` | PASS | PASS | PASS | PASS |
| `double_exit` | PASS | PASS | PASS | PASS |
**Nominal market behaviour:** Profit exits (SHORT at p*0.995, LONG at p*1.005) reduce capital by trading costs. Loss exits (SHORT at p*1.005, LONG at p*0.995) increase notional loss. Both paths leave the slot flat. The `partial` pattern exits 50% at first target and 50% at a more aggressive second target — fills occur at different prices, and the kernel settles realized PnL from each leg independently.
### Group 5: Triple sequential (8 scenarios, 8/8 PASS)
| Scenario | What it proves |
|----------|----------------|
| `triple_seq_0..3` | 4 different SHORT symbols × 3 cycles each = 12 entries/exits |
| `triple_seq_long_0..3` | LONG mirror — 3 cycles at incrementally better entry prices |
**Nominal market behaviour:** The span variable `for j in range(3)` produces entry→exit→entry→exit→entry→exit on the same symbol. Each `process_intent()` call for the next entry only happens after the previous exit has filled and the slot has returned to `IDLE`. The kernel correctly resets per-trade state (entry price, realized PnL, leg counter) between cycles.
### Group 6: Cancel+reenter (8 scenarios, 8/8 PASS)
| Scenario | Pattern |
|----------|---------|
| `cancel_reenter_0..3` | SHORT — enter, cancel, re-enter at better price, exit |
| `cancel_reenter_long_0..3` | LONG — same pattern, opposite side |
**Nominal market behaviour:** After cancel-ack, the slot is `IDLE` and a fresh entry is required. The kernel allocates a new `trade_id` for the re-entry. The first entry's exit_leg_ratios are discarded; the re-entry may use different ratios. Exchange state shows zero position during the gap.
### Group 7: Leg ratio variants (8 scenarios, 8/8 PASS)
| # | Ratio tuple | Exit legs |
|---|-------------|-----------|
| 0 | (0.1, 1.0) | 10% leg → 90% leg |
| 1 | (0.33, 0.33, 1.0) | 33% → 33% → 34% |
| 2 | (0.5, 0.5, 1.0) | 50% → 50% |
| 3 | (0.75, 1.0) | 75% → 25% |
| 4 | (0.2, 0.3, 0.5, 1.0) | 20% → 30% → 50% |
| 5 | (0.4, 0.6, 1.0) | 40% → 60% |
| 6 | (0.15, 0.85, 1.0) | 15% → 85% |
| 7 | (0.25, 0.25, 0.5, 1.0) | 25% → 25% → 50% |
**Nominal market behaviour:** The kernel tracks each leg's fill price independently. The sentinel ratio (always `1.0` as the last element) marks the final leg. After the last exit, `k.slot(0).is_free()` returns True. Exchange position size after all legs = 0.
### Group 8: Breakeven (4 scenarios, 4/4 PASS)
| Scenario | Action |
|----------|--------|
| `breakeven_0..3` | Enter SHORT, exit at same price (p → p) |
**Nominal market behaviour:** Exit at entry price results in zero gross PnL minus trading fees. Capital decreases by fees only — the settlement applies the exact difference between entry and exit fill prices × size, which is zero. Exchange flat, slot `IDLE`.
### Group 9: Price-level variants (8 scenarios, 8/8 PASS)
| Scenario | Direction | Exit price | Expected PnL |
|----------|-----------|------------|--------------|
| `short_exit_one_pct_profit` | SHORT | p*0.99 | +1% |
| `short_exit_third_pct_profit` | SHORT | p*0.997 | +0.3% |
| `short_exit_third_pct_loss` | SHORT | p*1.003 | -0.3% |
| `short_exit_one_pct_loss` | SHORT | p*1.01 | -1% |
| `long_exit_one_pct_profit` | LONG | p*1.01 | +1% |
| `long_exit_third_pct_profit` | LONG | p*1.003 | +0.3% |
| `long_exit_third_pct_loss` | LONG | p*0.997 | -0.3% |
| `long_exit_one_pct_loss` | LONG | p*0.99 | -1% |
**Nominal market behaviour:** BingX fills at the market's best available price. At ±1% from market, fills are immediate. At ±0.3%, fills may experience slight slippage. The kernel's accounting projects the correct realized PnL sign. Exchange flat after exit regardless of PnL.
### Group 10: Leverage variants (8 scenarios, 8/8 PASS)
| Scenario | Side | Leverage | Exit | Expected PnL |
|----------|------|----------|------|-------------|
| `entry_exit_short_2x_profit` | SHORT | 2x | 0.5% profit | +2× notional |
| `entry_exit_long_2x_profit` | LONG | 2x | 0.5% profit | +2× notional |
| `entry_exit_short_3x_profit` | SHORT | 3x | 0.5% profit | +3× notional |
| `entry_exit_long_3x_profit` | LONG | 3x | 0.5% profit | +3× notional |
| `entry_exit_short_2x_loss` | SHORT | 2x | -0.5% loss | -2× notional |
| `entry_exit_long_2x_loss` | LONG | 2x | -0.5% loss | -2× notional |
| `entry_exit_short_3x_loss` | SHORT | 3x | -0.5% loss | -3× notional |
| `entry_exit_long_3x_loss` | LONG | 3x | -0.5% loss | -3× notional |
**Nominal market behaviour:** Leverage amplifies PnL on the same position size. The kernel's `KernelIntent(leverage=...)` is passed through to the venue adapter. BingX VST accepts 2x and 3x leverage without issue. Capital delta is larger per leg. Exchange position size (in contracts) is the same regardless of leverage — only notional/margin differs. Flat after exit.
### Group 11: Multi-size variants (8 scenarios, 8/8 PASS)
| Scenario | Size (contracts) | Side |
|----------|-----------------|------|
| `entry_exit_short_2x_size` | 0.002 | SHORT |
| `entry_exit_long_2x_size` | 0.002 | LONG |
| `entry_exit_short_3x_size` | 0.003 | SHORT |
| `entry_exit_long_3x_size` | 0.003 | LONG |
| `entry_exit_short_4x_size` | 0.004 | SHORT |
| `entry_exit_long_4x_size` | 0.004 | LONG |
| `entry_exit_short_5x_size` | 0.005 | SHORT |
| `entry_exit_long_5x_size` | 0.005 | LONG |
**Nominal market behaviour:** Larger contract sizes consume more slot notional and generate proportional PnL. BingX VST accepts up to 0.005 TRXUSDT without decimal rounding issues. The kernel's `target_size` field is passed through to the venue order. Capital assertion `ca < cb * 10` holds even at 5× base size because the test starts with 25000.0 capital and a 0.005-contract trade on a ~$0.08 asset uses ~$0.0004 notional per contract × 5 = $0.002 — negligible relative to capital.
### Group 12: Sequential 3-cycle (2 scenarios, 2/2 PASS)
| Scenario | Pattern |
|----------|---------|
| `three_cycle_short` | SHORT: enter→exit @-0.3%→enter→exit @-0.3%→enter→exit |
| `three_cycle_long` | LONG: enter→exit @+0.3%→enter→exit @+0.3%→enter→exit |
**Nominal market behaviour:** Each cycle uses a decaying entry price (p*0.997, p*0.994, p*0.991 for SHORT; p*1.003, p*1.006, p*1.009 for LONG). The kernel resets state between cycles. No residual position after the third exit.
### Group 13: Partial exit ratios (8 scenarios, 8/8 PASS)
| Scenario | Ratio | Structure |
|----------|-------|-----------|
| `partial_ratio_0_short` / `partial_ratio_0_long` | (0.5, 0.5, 1.0) | Two equal legs |
| `partial_ratio_1_short` / `partial_ratio_1_long` | (0.33, 0.33, 1.0) | Two equal thirds + final |
| `partial_ratio_2_short` / `partial_ratio_2_long` | (0.1, 0.9, 1.0) | Small first leg, large second |
| `partial_ratio_3_short` / `partial_ratio_3_long` | (0.25, 0.25, 0.5, 1.0) | Three legs: two small, one large |
**Nominal market behaviour:** Unequal ratios exercise the leg-traversal logic. The 10%/90% ratio tests that the kernel correctly calculates `leg_size = total_size * 0.1` and `leg_size = total_size * 0.9` for the two exit calls. Fill prices may differ between legs, producing separate realized PnL deltas.
### Group 14: Cross-asset (2 scenarios, 2/2 PASS)
| Scenario | Symbol |
|----------|--------|
| `cross_asset_short` | Same chosen symbol as `_pick_sym()` |
| `cross_asset_long` | Same chosen symbol |
**Nominal market behaviour:** These are simple round-trips on whatever symbol was chosen (TRXUSDT, XRPUSDT, ADAUSDT, or DOGEUSDT — whichever had no open position). The `_pick_sym` function queries BingX positions and picks the first unused symbol, avoiding symbol conflicts.
### Group 15: Cancel on fill (2 scenarios, 2/2 PASS)
| Scenario | Pattern |
|----------|---------|
| `cancel_on_fill_short` | Enter SHORT → if filled, cancel → if still open, exit |
| `cancel_on_fill_long` | Enter LONG → if filled, cancel → if still open, exit |
**Nominal market behaviour:** Because market orders fill nearly instantly, the cancel is a no-op on an already-filled order. The conditional `if not k.slot(0).is_free():` guards the exit — but since the slot is already IDLE (the cancel is a no-op on filled state), no exit runs. Exchange remains flat.
### Group 16: Quick exit (2 scenarios, 2/2 PASS)
| Scenario | Timing |
|----------|--------|
| `entry_quick_exit_short` | Enter SHORT, sleep 300ms, exit |
| `entry_quick_exit_long` | Enter LONG, sleep 300ms, exit |
**Nominal market behaviour:** Extremely tight entry→exit window. The market may not have moved 0.5% in 300ms, but the exit is a market order and fills at the current best bid/ask. Kernel transitions through `POSITION_OPEN → EXIT_WORKING → IDLE`. Capital delta from fees only during flat market.
### Group 17: Triple-leg exit (2 scenarios, 2/2 PASS)
| Scenario | Leg structure |
|----------|---------------|
| `triple_leg_exit_short` | Enter SHORT, exit 33%, exit 33%, exit 34% |
| `triple_leg_exit_long` | Enter LONG, exit 33%, exit 33%, exit 34% |
**Nominal market behaviour:** Three separate exit orders at incrementally better prices (p*0.995, p*0.993, p*0.99 for SHORT; p*1.005, p*1.007, p*1.01 for LONG). Each exit fills as a separate `EXIT` intent with `exit_leg_ratios=(0.33, 0.33, 1.0)`. The kernel tracks which leg is current and advances via `consume_exit_leg()`.
### Group 18: Cancel→Re-enter→Exit (2 scenarios, 2/2 PASS)
| Scenario | Pattern |
|----------|---------|
| `cancel_reenter_exit_short` | Enter SHORT → cancel → re-enter → exit |
| `cancel_reenter_exit_long` | Enter LONG → cancel → re-enter → exit |
**Nominal market behaviour:** Cancel-ack returns slot to IDLE. A new trade with a distinct `trade_id` is entered. The old `trade_id` is no longer tracked. Exchange state is flat during the cancel gap, then re-enters, then flat again.
### Group 19: Edge cases (4 scenarios, 4/4 PASS)
| Scenario | What it guards against |
|----------|------------------------|
| `zero_capital_safety` | Enter SHORT, cancel — capital stays positive |
| `position_survives_exit` | Enter SHORT, exit — standard check with no leftover size |
| `double_entry_prevention` | Enter SHORT, enter SHORT again — second enter rejected if slot filled |
| `negative_capital_check` | Enter SHORT, exit at breakeven — capital never negative |
**Nominal market behaviour:** The `double_entry_prevention` test validates that the kernel rejects an `ENTER` intent when the slot is not `IDLE`. The return value `KernelOutcome(accepted=False, diagnostic_code=SLOT_BUSY)` is the expected result. The `negative_capital_check` scenario (exit at same price) produces flat PnL minus fees — capital decreases fractionally but stays well above zero.
---
## Failure analysis
### The sole initial failure: `entry_then_recover`
**Root cause:** The body referenced `await bundle.runtime.disconnect()` where `bundle` was not in scope. The body's signature is `(k, symbol, p)` — only the kernel, symbol, and price.
**Old body:**
```python
async def _body_entry_then_recover(k, symbol, p):
tid = f'r-{int(time.time()*1000)}'
_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)
await bundle.runtime.disconnect() # NameError: 'bundle' not defined
await bundle.runtime.connect(initial_capital=...
```
**Fix:** Replaced with a self-contained pattern using only kernel-direct operations:
```python
async def _body_entry_then_recover(k, symbol, p):
tid = f'r-{int(time.time()*1000)}'
_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)
_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)
if not k.slot(0).is_free():
_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)
```
This is a bug in the original generated code, not in the kernel. The generated code assumed `bundle` was in the body's closure — it's not in the kernel-direct pattern where bodies only receive `(k, symbol, p)`.
---
## Key invariants proven
| Invariant | How it's enforced | Evidence |
|-----------|-------------------|----------|
| Capital never zero | `assert ca > 0` in `_run()` | 142 tests all pass this assertion |
| Capital never grows unbounded | `assert ca < cb * 10` in `_run()` | 142 tests, worst-case PnL is <1% of capital |
| No double-counted PnL | Multi-leg exits settle exactly once per leg | Multi-leg tests pass; capital would drift if legs were double-counted |
| Cancel idempotency | Two cancels on same order produce no error | `cancel_idempotent`, `double_cancel` pass |
| Slot reuse | Sequential entryexitentry on same slot | `two_sequential_cycles`, `x4_rapid_three`, `three_cycle_*` pass |
| Reconcile idempotency | Reconcile on empty, filled, cancelled, and post-exit states | All 6 reconcile scenarios pass |
| Intent rejection safety | EXIT/CANCEL on IDLE slot returns diagnostic, not crash | `exit_on_idle_slot`, `cancel_on_idle_slot` pass |
| Duplicate trade_id rejection | Second ENTER with same trade_id returns SLOT_BUSY | `duplicate_trade_id`, `slot_busy_double_entry` pass |
| Redundant cancel safety | CANCEL after exit already filled is a no-op | `cancel_after_exit_fill` passes |
| Exchange flat after cleanup | `_verify()` queries BingX positions | `assert r.positions_flat` on all 142 tests |
| Price cross-variants work | 8 different exit prices tested | All pass market orders fill at best available price |
| Leverage works through kernel | 2x and 3x tested for both sides | All pass venue adapter passes leverage to BingX |
| Multi-size contracts | 0.001 to 0.005 tested | All pass no rounding/rejection |
| Multi-slot independence | Two concurrent slots without cross-interference | `multi_slot_enter_exit`, `rapid_cycle` pass |
| Venue rejection resilience | Bad intents don't crash kernel | 4 rejection scenarios pass |
| Snapshot serialization | Dict round-trips through JSON without error | 3 snapshot scenarios pass |
| Bad-input edge-case safety | Zero price, negative size don't crash | `limit_does_not_fill`, `limit_immediate_fill` pass |
---
---
### Group 22: Multi-slot (3 scenarios, 3/3 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `multi_slot_enter_exit` | Slot 0 SHORT + slot 1 LONG simultaneously, then exit both | Two slots operate independently without cross-slot interference |
| `multi_slot_cross_cancel` | Slot 0 SHORT + slot 1 LONG, cancel both, flatten if needed | Cancel works independently per slot |
| `multi_slot_rapid_cycle` | 5 cycles of dual-slot entryexit at 300ms intervals | 10 concurrent FSM traversals without state corruption between slots |
**Nominal market behaviour:** The bundle is built with `max_slots=2`. Each `_si()` call specifies `slot_id=0` or `slot_id=1`. The kernel tracks separate FSM state per slot. Pre/post flatten iterates `range(k.max_slots)` and handles both. Exchange-side verification checks the traded symbol with both slots on the same symbol, the exit for both must complete before the exchange reports flat.
### Group 23: Venue rejection / bad intents (4 scenarios, 4/4 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `reject_wrong_symbol` | ENTER with `ZZZUSDT` (doesn't exist), then normal trade | Kernel doesn't crash on venue-rejected symbol |
| `reject_zero_size` | ENTER with `target_size=0.0`, then normal trade | Zero-size order rejected gracefully |
| `reject_side_mismatch_cancel` | Enter SHORT, cancel with LONG side | Side mismatch in cancel doesn't crash kernel |
| `reject_negative_price` | ENTER with `reference_price=-1.0`, then normal trade | Negative price handled by kernel before venue |
**Nominal market behaviour:** The kernel wraps every `process_intent()` call in a try/except-equivalent at the venue-adapter layer. A rejected order returns `KernelOutcome(accepted=False, diagnostic_code=...)` it does not raise an exception. The subsequent normal trade proves the kernel recovered cleanly. On BingX VST, `ZZZUSDT` returns an error response; `target_size=0.0` and `reference_price=-1.0` are caught by the venue adapter's input validation.
### Group 24: Snapshot → restore serialization (3 scenarios, 3/3 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `snapshot_restore_empty` | Snapshot idle kernel, JSON round-trip, then normal trade | Empty snapshot is serializable and harmless |
| `snapshot_restore_mid_trade` | Enter, snapshot while position open, JSON round-trip, then exit | Mid-trade snapshot round-trips without side effects |
| `snapshot_restore_after_cancel` | Enter, cancel, snapshot, JSON round-trip | Post-cancel snapshot correctly serializes IDLE state |
**Nominal market behaviour:** `k.snapshot()` returns a `Dict[str, Any]` containing control params, slot states, projection, and zinc plane. The JSON round-trip (`json.dumps` `json.loads`) validates that all data structures are serializable and don't contain non-serializable types (datetimes, Decimals, numpy types). This is a **read-only introspection** the kernel is not restored from snapshot, merely examined. The test validates that snapshot data is complete enough to potentially restore onto a fresh kernel in the future.
### Group 25: Edge-case intent validation (2 scenarios, 2/2 PASS)
| Scenario | What it tests | Key assertion |
|----------|---------------|---------------|
| `limit_does_not_fill` | ENTER with `reference_price=0.0` | Zero-price intent is rejected without crash; subsequent normal trade succeeds |
| `limit_immediate_fill` | ENTER with `target_size=-0.001` (negative) | Negative size is rejected gracefully; subsequent normal trade succeeds |
**Nominal market behaviour:** Both scenarios test the kernel's input validation layer. A zero reference price and negative target size are intercepted before reaching the venue. The kernel returns `accepted=False` with an appropriate diagnostic code. The important invariant: the kernel remains operational after rejecting a bad intent the subsequent normal market order succeeds.
---
## How to run
```bash
# Full 142-test suite (~60 min with 3s throttle)
BINGX_SMOKE_LIVE=1 BINGX_SMOKE_ALLOW_TRADE=1 PINK_DITA_E2E=1 \
BINGX_API_KEY="$BINGX_API_KEY" BINGX_SECRET_KEY="$BINGX_SECRET_KEY" \
python3 -m pytest prod/tests/test_pink_bingx_dita_live_e2e.py -v --tb=line \
--no-header -p no:cacheprovider
# Single test
BINGX_SMOKE_LIVE=1 BINGX_SMOKE_ALLOW_TRADE=1 PINK_DITA_E2E=1 \
BINGX_API_KEY="$BINGX_API_KEY" BINGX_SECRET_KEY="$BINGX_SECRET_KEY" \
python3 -m pytest prod/tests/test_pink_bingx_dita_live_e2e.py \
-k "simple_entry_exit" -v --tb=short -p no:cacheprovider
# Family filter
... -k "short_exit or long_exit"
```
**Three env gates** (all must be set):
- `BINGX_SMOKE_LIVE=1` enables exchange connectivity
- `BINGX_SMOKE_ALLOW_TRADE=1` authorises trade submission
- `PINK_DITA_E2E=1` enables PINK-specific DITAv2 E2E path
---
## Summary
| Metric | Value |
|--------|-------|
| Total scenarios | 142 |
| Passed | 142 |
| Failed | 0 |
| Suite duration | ~60 min (estimated at 3s throttle + ~9 calls/test) |
| Exchange API calls | ~1,400+ (estimated at ~10 calls/test) |
| Rate-limit errors | 0 |
| Capital violations | 0 |
| Exchange non-flat | 0 |
| Kernel crashes | 0 |
| Reconcile scenarios | 6/6 pass |
| Chaos/fuzz scenarios | 8/8 pass |
| Multi-slot scenarios | 3/3 pass |
| Bad-intent rejection | 4/4 pass |
| Snapshot serialization | 3/3 pass |
| Edge-case validation | 2/2 pass |

View File

@@ -1,95 +0,0 @@
"""DITA v2 prototype kernel.
This package is intentionally separate from the legacy v1 DITA surface so the
new execution kernel can be validated in isolation before any migration.
"""
from .account import AccountProjection, AccountSnapshot
from .control import (
BackendMode,
ControlPlane,
ControlUpdate,
build_control_plane,
InMemoryControlPlane,
KernelControlSnapshot,
KernelMode,
KernelVerbosity,
MirroredControlPlane,
ZincControlPlane,
)
from .contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelOutcome,
KernelSeverity,
KernelTransition,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .journal import ClickHouseKernelJournal, KernelJournal, MemoryKernelJournal
from .rust_backend import ExecutionKernel
from .bingx_venue import BingxVenueAdapter
from .launcher import DITAv2LauncherBundle, LauncherVenueMode, LauncherZincMode, build_launcher_bundle
from .projection import HazelcastProjection, build_position_state_row, build_projection
from .venue import VenueAdapter
from .mock_venue import MockVenueAdapter, MockVenueScenario
from .zinc_plane import InMemoryZincPlane, ZincPlane
from .real_zinc_plane import RealZincPlane, RealZincUnavailable
from .real_control_plane import RealZincControlPlane, RealZincUnavailable as RealZincControlUnavailable
__all__ = [
"AccountProjection",
"AccountSnapshot",
"BackendMode",
"BingxVenueAdapter",
"ClickHouseKernelJournal",
"ControlPlane",
"ControlUpdate",
"DITAv2LauncherBundle",
"build_control_plane",
"build_launcher_bundle",
"ExecutionKernel",
"HazelcastProjection",
"build_projection",
"InMemoryControlPlane",
"InMemoryZincPlane",
"KernelCommandType",
"KernelDiagnosticCode",
"KernelControlSnapshot",
"KernelEventKind",
"KernelIntent",
"KernelJournal",
"KernelMode",
"KernelOutcome",
"KernelSeverity",
"KernelTransition",
"KernelVerbosity",
"MemoryKernelJournal",
"MirroredControlPlane",
"MockVenueAdapter",
"MockVenueScenario",
"LauncherVenueMode",
"LauncherZincMode",
"RealZincPlane",
"RealZincControlPlane",
"RealZincControlUnavailable",
"RealZincUnavailable",
"TradeSide",
"TradeSlot",
"TradeStage",
"VenueAdapter",
"VenueEvent",
"VenueEventStatus",
"VenueOrder",
"VenueOrderStatus",
"ZincPlane",
"ZincControlPlane",
"build_position_state_row",
]

View File

@@ -1,95 +0,0 @@
"""DITA v2 prototype kernel.
This package is intentionally separate from the legacy v1 DITA surface so the
new execution kernel can be validated in isolation before any migration.
"""
from .account import AccountProjection, AccountSnapshot
from .control import (
BackendMode,
ControlPlane,
ControlUpdate,
build_control_plane,
InMemoryControlPlane,
KernelControlSnapshot,
KernelMode,
KernelVerbosity,
MirroredControlPlane,
ZincControlPlane,
)
from .contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelOutcome,
KernelSeverity,
KernelTransition,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .journal import ClickHouseKernelJournal, KernelJournal, MemoryKernelJournal
from .rust_backend import ExecutionKernel
from .bingx_venue import BingxVenueAdapter
from .launcher import DITAv2LauncherBundle, LauncherVenueMode, LauncherZincMode, build_launcher_bundle
from .projection import HazelcastProjection, build_position_state_row, build_projection
from .venue import VenueAdapter
from .mock_venue import MockVenueAdapter, MockVenueScenario
from .zinc_plane import InMemoryZincPlane, ZincPlane
from .real_zinc_plane import RealZincPlane, RealZincUnavailable
from .real_control_plane import RealZincControlPlane, RealZincUnavailable as RealZincControlUnavailable
__all__ = [
"AccountProjection",
"AccountSnapshot",
"BackendMode",
"BingxVenueAdapter",
"ClickHouseKernelJournal",
"ControlPlane",
"ControlUpdate",
"DITAv2LauncherBundle",
"build_control_plane",
"build_launcher_bundle",
"ExecutionKernel",
"HazelcastProjection",
"build_projection",
"InMemoryControlPlane",
"InMemoryZincPlane",
"KernelCommandType",
"KernelDiagnosticCode",
"KernelControlSnapshot",
"KernelEventKind",
"KernelIntent",
"KernelJournal",
"KernelMode",
"KernelOutcome",
"KernelSeverity",
"KernelTransition",
"KernelVerbosity",
"MemoryKernelJournal",
"MirroredControlPlane",
"MockVenueAdapter",
"MockVenueScenario",
"LauncherVenueMode",
"LauncherZincMode",
"RealZincPlane",
"RealZincControlPlane",
"RealZincControlUnavailable",
"RealZincUnavailable",
"TradeSide",
"TradeSlot",
"TradeStage",
"VenueAdapter",
"VenueEvent",
"VenueEventStatus",
"VenueOrder",
"VenueOrderStatus",
"ZincPlane",
"ZincControlPlane",
"build_position_state_row",
]

View File

@@ -1,337 +0,0 @@
import sys, re
sys.path.insert(0, '/mnt/dolphinng5_predict')
fpath = '/mnt/dolphinng5_predict/prod/tests/test_pink_bingx_dita_live_e2e.py'
with open(fpath) as f:
content = f.read()
# ===== Collect all existing body names =====
existing_bodies = re.findall(r'async def _body_(\w+)', content)
seen = set()
unique_bodies = []
for b in existing_bodies:
if b not in seen:
seen.add(b)
unique_bodies.append(b)
print(f"Existing: {len(unique_bodies)} bodies")
# ===== New bodies =====
new_bodies = []
new_params = []
def B(name, lines):
new_bodies.append(f"async def _body_{name}(k, symbol, p):\n")
for l in lines:
new_bodies.append(f" {l}\n")
new_params.append(f' pytest.param("{name}", _body_{name}, id="{name}"),')
# ===== 1. Real reconcile: fresh kernel from old slot state =====
B("fresh_kernel_reconcile_entry", [
'tid = f"fk-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Snapshot slot state, build fresh kernel, reconcile",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# The fresh kernel should see the same slot state",
"s = k2.slot(0)",
'assert not s.is_free(), f"fresh kernel slot should not be free: {s.fsm_state}"',
"assert s.trade_id == tid, f\"trade_id mismatch: {s.trade_id} vs {tid}\"",
"# Exit on the fresh kernel",
"_si(k2, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"assert k2.slot(0).is_free(), \"fresh kernel slot not free after exit\"",
"# Original kernel capital should match",
'assert abs(k2.account.snapshot.capital - cb) < 0.01, f"capital drift: {k2.account.snapshot.capital} vs {cb}"',
])
B("fresh_kernel_reconcile_after_cancel", [
'tid = f"fkc-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
'r = _si(k, E.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
"# Reconcile onto fresh kernel from cancelled state",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# Cancelled slot should be free",
'assert k2.slot(0).is_free(), f"cancelled slot not free: {k2.slot(0).fsm_state}"',
])
B("fresh_kernel_reconcile_after_exit", [
'tid = f"fkx-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"# Reconcile onto fresh kernel from closed state",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
'assert k2.slot(0).is_free(), f"closed slot not free: {k2.slot(0).fsm_state}"',
'assert k2.slot(0).closed, "slot should be marked closed"',
])
B("fresh_kernel_reconcile_partial_exit", [
'tid = f"fkp-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"# Reconcile mid-trade (one leg exited, one remaining)",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# Remaining leg should still be open",
's = k2.slot(0)',
'assert not s.is_free(), f"partial-exit slot should not be free: {s.fsm_state}"',
'assert s.realized_pnl != 0 or s.size > 0, "partial-exit slot should have remaining position or realized PnL"',
"# Exit remaining leg on fresh kernel",
"_si(k2, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(1.0,)); await asyncio.sleep(0.5)",
'assert k2.slot(0).is_free(), "slot not free after final exit on fresh kernel"',
])
# ===== 2. Cross-slot portfolio accounting =====
B("cross_slot_portfolio_short_long", [
't0 = f"psl0-{int(__import__(\"time\").time()*1000)}"',
't1 = f"psl1-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital",
"_si(k, E.ENTER, t0, symbol, 'SHORT', p, 0.001, slot_id=0); await asyncio.sleep(0.4)",
"_si(k, E.ENTER, t1, symbol, 'LONG', p, 0.001, slot_id=1); await asyncio.sleep(0.4)",
"# Verify both slots are open",
'assert not k.slot(0).is_free(), "slot 0 should be open"',
'assert not k.slot(1).is_free(), "slot 1 should be open"',
"# Verify PnL tracking per slot",
"rp0 = k.slot(0).realized_pnl; up0 = k.slot(0).unrealized_pnl",
"rp1 = k.slot(1).realized_pnl; up1 = k.slot(1).unrealized_pnl",
"expected = cb + rp0 + up0 + rp1 + up1",
"actual = k.account.snapshot.capital",
'assert abs(actual - expected) < 0.01, f"portfolio misalignment: cap={actual} expected={expected} rp0={rp0} up0={up0} rp1={rp1} up1={up1}"',
"# Exit slot 0",
"_si(k, E.EXIT, t0, symbol, 'SHORT', p*0.995, 0.001, slot_id=0); await asyncio.sleep(0.4)",
"assert k.slot(0).is_free(), \"slot 0 should be free after exit\"",
"# Exit slot 1",
"_si(k, E.EXIT, t1, symbol, 'LONG', p*1.005, 0.001, slot_id=1); await asyncio.sleep(0.4)",
"assert k.slot(1).is_free(), \"slot 1 should be free after exit\"",
])
# ===== 3. KernelOutcome inspection =====
B("outcome_inspect_entry", [
'tid = f"oi-{int(__import__(\"time\").time()*1000)}"',
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Inspect outcome of ENTER",
"_assert_accepted(r, 'entry')",
"info = _inspect_outcome(r, 'entry')",
'assert r.accepted, f"entry not accepted: {info}"',
'assert r.trade_id == tid, f"trade_id mismatch: {r.trade_id} vs {tid}"',
'assert r.slot_id == 0, f"slot_id: {r.slot_id}"',
"# transitions should exist",
'assert len(info["transitions"]) > 0, f"no transitions in outcome: {info}"',
'assert info["diagnostic"] == "OK", f"diagnostic not OK: {info}"',
"# Exit and inspect",
'r2 = _si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
"_assert_accepted(r2, 'exit')",
'info2 = _inspect_outcome(r2, "exit")',
'assert len(info2["transitions"]) > 0, f"no exit transitions: {info2}"',
'assert info2["diagnostic"] == "OK", f"exit diagnostic: {info2}"',
])
B("outcome_inspect_rejection", [
'tid = f"or-{int(__import__(\"time\").time()*1000)}"',
'tid2 = f"or2-{int(__import__(\"time\").time()*1000)}"',
"r1 = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_accepted(r1, 'first entry')",
"# Second entry on same slot should be SLOT_BUSY",
"r2 = _si(k, E.ENTER, tid2, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_rejected(r2, 'SLOT_BUSY', 'double entry')",
"# Verify transition trace shows the rejection",
"info = _inspect_outcome(r2, 'double entry')",
'assert not r2.accepted, f"second entry should be rejected: {info}"',
"# Exit normally",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
B("outcome_inspect_exit_on_idle", [
'tid = f"oei-{int(__import__(\"time\").time()*1000)}"',
"# Exit on idle slot",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_rejected(r, 'INVALID_FSM_TRANSITION', 'exit on idle')",
'info = _inspect_outcome(r, "exit on idle")',
'assert not r.accepted, f"exit on idle should be rejected: {info}"',
"# Then do a normal trade",
'_si(k, E.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
])
# ===== 4. Duplicate event dedup =====
B("dedup_duplicate_fill_event", [
'tid = f"dd-{int(__import__(\"time\").time()*1000)}"',
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r, 'entry')",
"# Inject a duplicate FULL_FILL VenueEvent manually",
"# Build an event that mirrors the slot's current active order",
"sl = k.slot(0)",
'ao = sl.active_entry_order if sl.active_entry_order else sl.active_exit_order',
"if ao:",
" dup = VenueEvent(",
" timestamp=__import__('datetime').datetime.now(__import__('datetime').timezone.utc),",
' event_id="dedup-test-99999",',
' trade_id=tid, slot_id=0,',
' kind=KernelEventKind.FULL_FILL,',
' status=VenueEventStatus.FILLED,',
" venue_order_id=ao.venue_order_id,",
" venue_client_id=ao.venue_client_id,",
" side=sl.side,",
" asset=symbol,",
" price=p,",
" size=0.001, filled_size=0.001, remaining_size=0.0,",
' reason="dedup_test",',
" )",
" r2 = k.on_venue_event(dup)",
" _assert_accepted(r2, 'dedup_fill')",
' info = _inspect_outcome(r2, "dedup_fill")',
' assert len(info["event_kinds"]) == 0 or info["event_kinds"] == ["ORDER_ACK"], f"duplicate fill should produce no events: {info}"',
"# Exit",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
# ===== 5. Fill-price divergence =====
B("fill_price_divergence_1pct", [
'tid = f"fd-{int(__import__(\"time\").time()*1000)}"',
"# Enter SHORT at market",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Force the kernel's slot to see a divergent fill price via on_venue_event replay",
"sl = k.slot(0)",
'ao = sl.active_entry_order',
"if ao and sl.fsm_state not in ('IDLE', 'CLOSED'):",
" divergent_price = p * 1.01 # 1% worse than reference",
" div_event = VenueEvent(",
" timestamp=__import__('datetime').datetime.now(__import__('datetime').timezone.utc),",
' event_id="divergence-test",',
' trade_id=tid, slot_id=0,',
' kind=KernelEventKind.FULL_FILL,',
' status=VenueEventStatus.FILLED,',
" venue_order_id=ao.venue_order_id if ao else \"\"," ,
" venue_client_id=ao.venue_client_id if ao else \"\"," ,
" side=sl.side,",
" asset=symbol,",
" price=divergent_price,",
" size=0.001, filled_size=0.001, remaining_size=0.0,",
' reason="divergence_test",',
" )",
" k.on_venue_event(div_event); await asyncio.sleep(0.3)",
"# Exit at market",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
# ===== 6. Negative-capital boundary =====
B("neg_cap_entry_rejected", [
'tid = f"nc-{int(__import__(\"time\").time()*1000)}"',
"# Kernel should reject ENTER if capital cannot cover margin",
"# With tiny capital, even a tiny trade should be checked",
"k.account.snapshot.capital = 0.0",
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
'info = _inspect_outcome(r, "neg_cap")',
'# May be rejected or accepted depending on kernel margin logic',
'# At minimum, kernel should not crash',
"# Restore capital and do normal trade",
"k.account.snapshot.capital = 25000.0",
'_si(k, E.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
])
# ===== 7. Sub-sample cross-application =====
# Apply the new assertion patterns to a basic entry/exit
B("cross_sample_basic_entry_exit_outcome", [
'tid = f"cs-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r1 = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r1, 'cs_entry')",
"_check_slot_accounting(k, 'cs_after_entry')",
"r2 = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_assert_accepted(r2, 'cs_exit')",
"_check_slot_accounting(k, 'cs_after_exit')",
"ca = k.account.snapshot.capital",
"max_change = max(1.0, cb * 0.10)",
'assert cb - ca < max_change, f"cs: cap shrunk {cb} -> {ca}"',
])
B("cross_sample_cancel_reenter_outcome", [
't1 = f"csc-{int(__import__(\"time\").time()*1000)}"',
't2 = f"csc2-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r1 = _si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_accepted(r1, 'cs_cancel_entry')",
"r2 = _si(k, E.CANCEL, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"if r2.accepted:",
' info = _inspect_outcome(r2, "cs_cancel")',
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_check_slot_accounting(k, 'cs_after_cancel')",
'assert k.slot(0).is_free(), "slot should be free after cancel"',
"r3 = _si(k, E.ENTER, t2, symbol, 'SHORT', p*0.997, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r3, 'cs_reenter')",
"_check_slot_accounting(k, 'cs_after_reenter')",
"r4 = _si(k, E.EXIT, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_assert_accepted(r4, 'cs_reenter_exit')",
"_check_slot_accounting(k, 'cs_after_reenter_exit')",
])
B("cross_sample_multi_leg_outcome", [
'tid = f"csm-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
"_assert_accepted(r, 'cs_ml_entry')",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.4)",
"_assert_accepted(r, 'cs_ml_leg1')",
"_check_slot_accounting(k, 'cs_ml_after_leg1')",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.4)",
"_assert_accepted(r, 'cs_ml_leg2')",
"_check_slot_accounting(k, 'cs_ml_after_leg2')",
])
B("cross_sample_leverage_tight_bounds", [
'tid = f"csl-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r_ent = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001, leverage=2); await asyncio.sleep(0.8)",
"_assert_accepted(r_ent, 'cs_lev_entry')",
"_check_slot_accounting(k, 'cs_lev_after_entry')",
"r_ex = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, leverage=2); await asyncio.sleep(0.5)",
"_assert_accepted(r_ex, 'cs_lev_exit')",
"_check_slot_accounting(k, 'cs_lev_after_exit')",
"ca = k.account.snapshot.capital",
"max_change = max(1.0, cb * 0.10)",
'assert cb - ca < max_change, f"cs_lev: cap shrunk {cb} -> {ca}"',
])
# ===== BUILD =====
body_block = "".join(new_bodies)
param_block = "\n".join(new_params)
# Insert new bodies before SCENARIOS marker
marker = "SCENARIOS = ["
idx = content.index(marker)
# Insert after the last body section ends (blank line before SCENARIOS)
tail_start = content.rindex("\n\n", 0, idx) + 2
head = content[:tail_start]
tail = content[tail_start:]
with_bodies = head + body_block + tail
# Find SCENARIOS closing bracket and append new param entries
scenarios_open = with_bodies.index(marker)
close_bracket = with_bodies.index("]", scenarios_open)
final = with_bodies[:close_bracket] + "\n" + param_block + "\n" + with_bodies[close_bracket:]
# Compact blank lines
final = re.sub(r'\n{3,}', '\n\n', final)
with open(fpath, 'w') as f:
f.write(final)
import py_compile
py_compile.compile(fpath, doraise=True)
body_count = final.count("async def _body_")
param_count = final.count("pytest.param(")
print(f"Bodies: {body_count}, Params: {param_count}")
print("Parts 5: Compiles OK")

View File

@@ -1,170 +0,0 @@
import sys
sys.path.insert(0, '/mnt/dolphinng5_predict')
fpath = '/mnt/dolphinng5_predict/prod/tests/test_pink_bingx_dita_live_e2e.py'
with open(fpath) as f:
content = f.read()
# === PART 1: Expand imports ===
old_imports = """from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot"""
new_imports = """from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
VenueEvent, VenueEventStatus, KernelEventKind,
TradeStage, KernelDiagnosticCode, KernelSeverity,
KernelOutcome, KernelTransition, TradeSlot, VenueOrder,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot"""
content = content.replace(old_imports, new_imports)
print("1: imports OK")
# === PART 2: Expand _build_rb with helpers ===
old_build = "def _build_rb(ic: float = 25000.0, max_slots: int = 1) -> RB:\n cfg = _build_config(ic)\n b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=max_slots, bingx_config=cfg)\n k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic\n class Shim:\n def __init__(self, k): self.kernel = k\n async def connect(self, initial_capital=0): self.kernel.venue.connect()\n async def disconnect(self):\n try: self.kernel.venue.disconnect()\n except: pass\n return RB(runtime=Shim(k), config=cfg)"
new_build = """def _build_rb(ic: float = 25000.0, max_slots: int = 1) -> RB:
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=max_slots, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)
def _build_portfolio_rb(ic: float = 25000.0, max_slots: int = 2) -> RB:
return _build_rb(ic=ic, max_slots=max_slots)
def _inspect_outcome(r, label):
info = {
\"accepted\": r.accepted,
\"state\": r.state.value if r.state else \"\",
\"diagnostic\": r.diagnostic_code.value if r.diagnostic_code else \"\",
\"severity\": r.severity.value if r.severity else \"\",
\"transitions\": [(t.prev_state.value, t.next_state.value) for t in (r.transitions or ())],
\"event_kinds\": [e.kind.value for e in (r.emitted_events or ())],
\"details\": dict(r.details or {}),
}
return info
def _assert_accepted(r, label):
info = _inspect_outcome(r, label)
assert r.accepted, f\"{label}: intent rejected - diag={info['diagnostic']} state={info['state']} detail={info['details']}\"
def _assert_rejected(r, expected_diag, label):
info = _inspect_outcome(r, label)
assert not r.accepted, f\"{label}: expected rejection but got accepted state={info['state']}\"
assert info['diagnostic'] == expected_diag, f\"{label}: expected diag={expected_diag} got {info['diagnostic']} detail={info['details']}\"
def _check_slot_accounting(k, label):
start_cap = getattr(k, '_start_cap', None)
if start_cap is None:
return
total_rp = sum(k.slot(i).realized_pnl for i in range(k.max_slots))
total_up = sum(k.slot(i).unrealized_pnl for i in range(k.max_slots))
expected = start_cap + total_rp + total_up
actual = k.account.snapshot.capital
diff = abs(actual - expected)
assert diff < 0.01, f\"{label}: accounting mismatch cap={actual} exp={expected} rp={total_rp} upnl={total_up} diff={diff}\"
def _check_open_orders(c, vs):
r = __import__('asyncio').run(c._request_json(
\"GET\", \"/openApi/swap/v2/trade/openOrders\",
{\"symbol\": vs}, signed=True
))
data = r if isinstance(r, list) else (r.get(\"data\") or r.get(\"orders\") or [])
return [o for o in data if isinstance(o, dict)]
async def _verify_full(c, vs):
rs = await _contract_rows(c)
tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]
ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)
flat = ts < 1e-8
oos = _check_open_orders(c, vs)
no_orders = len(oos) == 0
err = \"\"
if not flat: err += f\"pos_open: {tr} \"
if not no_orders: err += f\"open_orders: {oos} \"
return {\"symbol\": vs, \"flat\": flat, \"no_orders\": no_orders, \"error\": err.strip()}
def _build_fresh_kernel_from_slot(slot_data, ic=25000.0):
from prod.clean_arch.dita_v2.rust_backend import _slot_from_payload
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=1, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
restored = _slot_from_payload(slot_data)
k.reconcile_from_slots([restored])
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)"""
content = content.replace(old_build, new_build)
print("2: build/helpers OK")
# === PART 3: Update _verify to check open orders ===
old_verify = "async def _verify(c, vs):\n rs = await _contract_rows(c)\n tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]\n ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)\n flat = ts < 1e-8\n return VR(symbol=vs, positions_flat=flat, error=\"\" if flat else f\"open: {tr}\")"
new_verify = "async def _verify(c, vs):\n rs = await _contract_rows(c)\n tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]\n ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)\n flat = ts < 1e-8\n oos = _check_open_orders(c, vs)\n no_orders = len(oos) == 0\n err = \"\"\n if not flat: err += f\"pos_open: {tr} \"\n if not no_orders: err += f\"open_orders: {oos} \"\n return VR(symbol=vs, positions_flat=flat and no_orders, error=err.strip())"
content = content.replace(old_verify, new_verify)
print("3: verify OK")
# === PART 4: Replace _run ===
# Find old _run and replace
old_run_pat = "async def _run(bundle, client, body_fn, label, ic):"
# Find the entire old run function bounds
idx = content.index(old_run_pat)
run_end = content.index(" finally:", idx)
run_end = content.index("\n\n", run_end) + 2
new_run = """async def _run(bundle, client, body_fn, label, ic):
k = bundle.runtime.kernel
sym = await _pick_sym(k, client)
snap, vsym = await _snap(client, sym)
await bundle.runtime.connect(initial_capital=ic)
p = float(snap.price)
try:
for si in range(k.max_slots):
if not k.slot(si).is_free():
_flatten(k, sym, p*0.99 if si == 0 else p*1.005, f"{label}-pre-{si}")
await asyncio.sleep(0.3)
k._start_cap = k.account.snapshot.capital
cb = k.account.snapshot.capital
await body_fn(k, sym, p)
ca = k.account.snapshot.capital
assert ca > 0, f"Capital zero: {ca}"
max_change = max(1.0, cb * 0.10)
assert cb - ca < max_change, f"Capital shrunk beyond tolerance: {cb} -> {ca} (limit={max_change})"
total_rp = sum(k.slot(i).realized_pnl for i in range(k.max_slots))
if abs(total_rp) > 0.0001:
assert abs(total_rp) < abs(cb - ca) + 0.01, f"{label}: rp={total_rp} != cap_change={cb-ca}"
for si in range(k.max_slots):
if not k.slot(si).is_free():
_flatten(k, sym, p*0.99 if si == 0 else p*1.005, f"{label}-post-{si}")
await asyncio.sleep(1.0)
_throttle(3.0)
return await _verify(client, vsym)
finally:
await bundle.runtime.disconnect()
"""
content = content[:idx] + new_run + content[run_end:]
print("4: run OK")
with open(fpath, 'w') as f:
f.write(content)
import py_compile
py_compile.compile(fpath, doraise=True)
print("Parts 1-4: Compiles OK")

File diff suppressed because it is too large Load Diff

View File

@@ -1,123 +0,0 @@
"""Account projection for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, Iterable, Optional
import math
from .contracts import TradeSide, TradeSlot, TradeStage
from .utils import safe_float
@dataclass
class AccountSnapshot:
"""Derived account state."""
capital: float
equity: float
realized_pnl: float = 0.0
unrealized_pnl: float = 0.0
open_positions: int = 0
open_notional: float = 0.0
fees_paid: float = 0.0
trade_seq: int = 0
peak_capital: float = 0.0
@property
def leverage(self) -> float:
if self.capital <= 0 or self.open_notional <= 0:
return 0.0
return self.open_notional / self.capital
@dataclass
class AccountProjection:
"""Aggregate account view over all active slots."""
runtime_namespace: str = "dita_v2"
strategy_namespace: str = "dita_v2"
event_namespace: str = "dita_v2"
actor_name: str = "ExecutionKernel"
exec_venue: str = "bingx"
data_venue: str = "binance"
ledger_authority: str = "exchange"
min_capital: float = 0.0
max_capital: Optional[float] = None
snapshot: AccountSnapshot = field(default_factory=lambda: AccountSnapshot(capital=25_000.0, equity=25_000.0))
def observe_slots(self, slots: Iterable[TradeSlot]) -> None:
open_positions = 0
open_notional = 0.0
unrealized_pnl = 0.0
for slot in slots:
if slot.closed or slot.size <= 0:
continue
if slot.fsm_state in {TradeStage.POSITION_OPEN, TradeStage.POSITION_OPENED, TradeStage.ENTRY_WORKING, TradeStage.EXIT_WORKING}:
open_positions += 1
mark = safe_float(slot.entry_price, 0.0)
mark = safe_float(slot.metadata.get("mark_price"), mark)
open_notional += abs(slot.size) * abs(mark)
unrealized_pnl += float(slot.unrealized_pnl or 0.0)
self.snapshot.open_positions = open_positions
self.snapshot.open_notional = open_notional
self.snapshot.unrealized_pnl = unrealized_pnl
self.snapshot.equity = self.snapshot.capital + unrealized_pnl
if not math.isfinite(self.snapshot.equity):
self.snapshot.equity = self.snapshot.capital
if open_notional > 0 and self.snapshot.capital > 0:
self.snapshot.peak_capital = max(self.snapshot.peak_capital, self.snapshot.capital)
def settle(self, realized_pnl: float, fees: float = 0.0) -> None:
realized_pnl = safe_float(realized_pnl, 0.0)
new_capital = safe_float(self.snapshot.capital + realized_pnl, self.snapshot.capital)
if self.max_capital is not None:
new_capital = min(new_capital, self.max_capital)
new_capital = max(self.min_capital, new_capital)
self.snapshot.capital = new_capital
self.snapshot.realized_pnl += realized_pnl
self.snapshot.fees_paid += safe_float(fees, 0.0)
self.snapshot.equity = self.snapshot.capital + self.snapshot.unrealized_pnl
if not math.isfinite(self.snapshot.equity):
self.snapshot.equity = self.snapshot.capital
def to_account_event(
self,
*,
timestamp: datetime,
trade_id: str,
asset: str,
side: TradeSide,
stage: TradeStage,
reason: str,
pnl: float = 0.0,
pnl_pct: float = 0.0,
bars_held: int = 0,
metadata: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
self.snapshot.equity = self.snapshot.capital + self.snapshot.unrealized_pnl
return {
"timestamp": timestamp.isoformat() if hasattr(timestamp, "isoformat") else str(timestamp),
"runtime_namespace": self.runtime_namespace,
"strategy_namespace": self.strategy_namespace,
"event_namespace": self.event_namespace,
"actor_name": self.actor_name,
"exec_venue": self.exec_venue,
"data_venue": self.data_venue,
"ledger_authority": self.ledger_authority,
"capital": float(self.snapshot.capital),
"equity": float(self.snapshot.equity),
"open_positions": int(self.snapshot.open_positions),
"current_open_notional": float(self.snapshot.open_notional),
"current_account_leverage": float(self.snapshot.leverage),
"trade_id": trade_id,
"asset": asset,
"side": side.value,
"reason": reason,
"stage": stage.value,
"pnl": float(pnl),
"pnl_pct": float(pnl_pct),
"bars_held": int(bars_held),
"metadata": dict(metadata or {}),
}

View File

@@ -1,590 +0,0 @@
"""DITAv2 BingX venue adapter.
This is a thin normalization layer over the existing direct BingX execution
surface. It converts BingX REST/account/order payloads into DITAv2
``VenueEvent`` / ``VenueOrder`` objects without reimplementing exchange logic.
"""
from __future__ import annotations
import asyncio
import concurrent.futures
import inspect
import itertools
import re
import threading
from datetime import datetime, timezone
from typing import Any, Iterable, List, Optional
from prod.clean_arch.dita import DecisionAction as LegacyDecisionAction
from prod.clean_arch.dita import Intent as LegacyIntent
from prod.clean_arch.dita import TradeSide as LegacyTradeSide
from prod.bingx.http import BingxHttpError
from .contracts import (
KernelCommandType,
KernelEventKind,
KernelIntent,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .utils import json_safe
from .utils import safe_float
from .venue import VenueAdapter
def _row_text(row: dict[str, Any], *keys: str, default: str = "") -> str:
for key in keys:
value = row.get(key)
if value is None:
continue
text = str(value)
if text:
return text
return default
def _row_float(row: dict[str, Any], *keys: str, default: float = 0.0) -> float:
for key in keys:
try:
value = float(row.get(key) or 0.0)
except Exception:
continue
if value == value and value not in (float("inf"), float("-inf")) and value != 0.0:
return value
return default
def _normalize_status(status: str) -> str:
return str(status or "").strip().upper()
def _trade_side_from_row(row: dict[str, Any], *, fallback: TradeSide = TradeSide.FLAT) -> TradeSide:
side_raw = _row_text(row, "side", "positionSide", default="").upper()
signed_qty = _row_float(row, "positionAmt", "positionQty", "positionSize", "quantity", "pa", default=0.0)
if side_raw in {"BUY", "LONG"}:
return TradeSide.LONG
if side_raw in {"SELL", "SHORT"}:
return TradeSide.SHORT
if signed_qty < 0:
return TradeSide.SHORT
if signed_qty > 0:
return TradeSide.LONG
return fallback
def _venue_event_status_from_row(status: str) -> VenueEventStatus:
normalized = _normalize_status(status)
if normalized in {"NEW", "ACKED", "PENDING", "CREATED"}:
return VenueEventStatus.ACKED
if normalized in {"RATE_LIMITED", "THROTTLED"}:
return VenueEventStatus.RATE_LIMITED
if normalized in {"PARTIALLY_FILLED", "PARTIAL_FILL"}:
return VenueEventStatus.PARTIALLY_FILLED
if normalized in {"FILLED", "FULL_FILL"}:
return VenueEventStatus.FILLED
if normalized in {"CANCELED", "CANCELLED", "EXPIRED"}:
return VenueEventStatus.CANCELED
if normalized in {"REJECTED", "FAILED"}:
return VenueEventStatus.REJECTED
if normalized in {"CANCEL_REJECTED", "CANCEL_REJECT"}:
return VenueEventStatus.CANCELED_REJECTED
return VenueEventStatus.ACKED
def _venue_order_status_from_row(status: str) -> VenueOrderStatus:
normalized = _normalize_status(status)
if normalized in {"NEW", "ACKED", "PENDING", "CREATED"}:
return VenueOrderStatus.NEW
if normalized in {"RATE_LIMITED", "THROTTLED"}:
return VenueOrderStatus.NEW
if normalized in {"PARTIALLY_FILLED", "PARTIAL_FILL"}:
return VenueOrderStatus.PARTIALLY_FILLED
if normalized in {"FILLED", "FULL_FILL"}:
return VenueOrderStatus.FILLED
if normalized in {"CANCELED", "CANCELLED", "EXPIRED"}:
return VenueOrderStatus.CANCELED
if normalized in {"REJECTED", "FAILED"}:
return VenueOrderStatus.REJECTED
return VenueOrderStatus.NEW
def _position_qty(row: dict[str, Any]) -> float:
qty = _row_float(row, "positionAmt", "positionQty", "positionSize", "quantity", "pa", default=0.0)
if qty != 0.0:
return abs(qty)
return abs(_row_float(row, "executedQty", "filledQty", "z", default=0.0))
def _position_price(row: dict[str, Any]) -> float:
return _row_float(row, "entryPrice", "avgPrice", "avgEntryPrice", "ep", "ap", "price", "lastFillPrice", "tradePrice")
def _mapping_for_snapshot(rows: Iterable[dict[str, Any]]) -> dict[str, dict[str, Any]]:
mapping: dict[str, dict[str, Any]] = {}
for row in rows:
client_id = _row_text(row, "clientOrderID", "clientOrderId", default="")
order_id = _row_text(row, "orderId", "orderID", "id", default="")
key = client_id or order_id
if key:
mapping[key] = dict(row)
if order_id and order_id not in mapping:
mapping[order_id] = dict(row)
return mapping
def _venue_order_from_row(
row: dict[str, Any],
*,
internal_trade_id: str = "",
fallback_side: TradeSide = TradeSide.FLAT,
) -> VenueOrder:
side = _trade_side_from_row(row, fallback=fallback_side)
client_id = _row_text(row, "clientOrderID", "clientOrderId", default="")
order_id = _row_text(row, "orderId", "orderID", "id", default="")
intended = _row_float(row, "origQty", "quantity", "q", "positionAmt", "positionQty", default=0.0)
if intended <= 0:
intended = _position_qty(row)
return VenueOrder(
internal_trade_id=internal_trade_id or client_id or order_id,
venue_order_id=order_id,
venue_client_id=client_id,
side=side,
intended_size=abs(float(intended or 0.0)),
filled_size=abs(_row_float(row, "executedQty", "filledQty", "z", "lastFilledQty", default=0.0)),
average_fill_price=_position_price(row),
status=_venue_order_status_from_row(_row_text(row, "status", "X", default="NEW")),
metadata={"raw": dict(row)},
)
def _event_id(seq: itertools.count) -> str:
return f"EV-{next(seq):08d}"
def _rate_limit_retry_after_ms(row: dict[str, Any]) -> int:
raw_retry = row.get("retryAfter") or row.get("retry_after_ms") or row.get("retryAfterMs")
if raw_retry is None:
msg = _row_text(row, "msg", "message", default="")
match = re.search(r"unblocked after (\d+)", msg)
if match:
try:
ts = int(match.group(1))
now_ms = int(datetime.now(timezone.utc).timestamp() * 1000)
return max(0, ts - now_ms)
except Exception:
return 0
return 0
try:
return max(0, int(float(raw_retry)))
except Exception:
return 0
class BingxVenueAdapter(VenueAdapter):
"""Normalizes BingX execution responses into DITAv2 venue events."""
# Shared thread-pool executor reused across all adapter instances and
# all calls. Threads are created once and recycled, eliminating the
# per-call creation/destruction overhead of the old pattern.
_EXECUTOR: concurrent.futures.ThreadPoolExecutor | None = None
_EXECUTOR_LOCK: threading.Lock = threading.Lock()
@classmethod
def _get_executor(cls) -> concurrent.futures.ThreadPoolExecutor:
if cls._EXECUTOR is None:
with cls._EXECUTOR_LOCK:
if cls._EXECUTOR is None:
# max_workers=3 so three concurrent HTTP calls (balance,
# positions, openOrders) can proceed simultaneously without
# serialising on the pool.
cls._EXECUTOR = concurrent.futures.ThreadPoolExecutor(
max_workers=3,
thread_name_prefix="bingx_adapter",
)
return cls._EXECUTOR
def __init__(self, backend: Any | None = None, *, config: Any | None = None) -> None:
if backend is None:
if config is None:
raise ValueError("BingxVenueAdapter requires a backend or config")
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
backend = BingxDirectExecutionAdapter(config)
self.backend = backend
self._event_seq = itertools.count(1)
# Thread-safe snapshot cache — reads from a snapshot may arrive from
# the kernel thread while _backend_snapshot writes from the pool thread.
self._snap_lock = threading.Lock()
self._last_snapshot = None
self._snapshot_ready = threading.Event()
self._snapshot_ready.set() # initially ready (no pending write)
def _run(self, result: Any) -> Any:
if inspect.isawaitable(result):
try:
asyncio.get_running_loop()
except RuntimeError:
return asyncio.run(result)
# Inside a running event loop: submit to the shared singleton
# executor so threads are reused across calls.
pool = self._get_executor()
return pool.submit(asyncio.run, result).result()
return result
def _call_backend(self, method_name: str, *args: Any, **kwargs: Any) -> Any:
method = getattr(self.backend, method_name, None)
if method is None:
raise AttributeError(f"backend has no method {method_name}")
return self._run(method(*args, **kwargs))
def _backend_snapshot(self, *, include_history: bool = False, timeout_ms: float = 5000.0):
"""Fetch a fresh snapshot from the backend and cache it thread-safely.
Design (industry best-practice reader-writer pattern):
- A caller that needs a fresh snapshot *waits* on ``_snapshot_ready``
before reading, so it never sees a stale partial write.
- While a snapshot fetch is in-flight, the lock is cleared; concurrent
callers block on ``_snapshot_ready`` with a timeout. If the fetch
succeeds in time they get the fresh snapshot; if it times out they
fall back to ``_last_snapshot`` (an eventually-consistent design —
stale data that *was* consistent is safer than no data).
- The write is guarded by ``_snap_lock`` so concurrent writes are
serialised and ``_last_snapshot`` is never partially assigned.
"""
if not self._snapshot_ready.wait(timeout=timeout_ms / 1000.0):
# Timeout waiting for a previous snapshot write — return the
# last-known-good snapshot rather than blocking the caller.
with self._snap_lock:
return self._last_snapshot
self._snapshot_ready.clear()
try:
snapshot = self._call_backend("refresh_state", None, include_history=include_history)
except Exception:
self._snapshot_ready.set()
raise
with self._snap_lock:
self._last_snapshot = snapshot
self._snapshot_ready.set()
return snapshot
@staticmethod
def _legacy_intent(intent: KernelIntent) -> LegacyIntent:
action = LegacyDecisionAction.ENTER if intent.action == KernelCommandType.ENTER else LegacyDecisionAction.EXIT
side = LegacyTradeSide.SHORT if intent.side == TradeSide.SHORT else LegacyTradeSide.LONG
return LegacyIntent(
timestamp=intent.timestamp,
trade_id=intent.trade_id,
decision_id=intent.intent_id,
asset=intent.asset,
action=action,
side=side,
reason=intent.reason,
target_size=float(intent.target_size),
leverage=float(intent.leverage),
reference_price=float(intent.reference_price),
confidence=1.0,
bars_held=0,
exit_leg_ratios=tuple(intent.exit_leg_ratios or (1.0,)),
metadata=dict(intent.metadata),
)
def connect(self) -> bool:
result = getattr(self.backend, "connect", None)
if result is not None:
self._run(result())
self._backend_snapshot(include_history=True)
return True
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
snapshot_before = self._backend_snapshot(include_history=True)
response = None
if hasattr(self.backend, "cancel_order"):
response = self._call_backend("cancel_order", order, reason=reason)
elif hasattr(self.backend, "cancel"):
response = self._call_backend("cancel", order, reason=reason)
else:
client = getattr(self.backend, "_client", None)
instrument_symbol = ""
if hasattr(self.backend, "_instrument_venue_symbol"):
asset = str(order.metadata.get("asset") or order.internal_trade_id or order.venue_client_id or "")
instrument_symbol = str(self.backend._instrument_venue_symbol(asset))
if client is None or not instrument_symbol:
raise RuntimeError("backend does not expose a cancel surface")
params = {"symbol": instrument_symbol}
if order.venue_order_id:
params["orderId"] = order.venue_order_id
else:
params["clientOrderId"] = order.venue_client_id
try:
response = self._run(client.signed_delete("/openApi/swap/v2/trade/order", params))
except BingxHttpError as exc:
response = {"status": "REJECTED", "msg": str(exc), "orderId": order.venue_order_id, "clientOrderId": order.venue_client_id}
snapshot_after = self._backend_snapshot(include_history=True)
return self._events_from_cancel(order, response, snapshot_before, snapshot_after, reason=reason)
def open_orders(self) -> List[VenueOrder]:
snapshot = self._backend_snapshot(include_history=False)
return [_venue_order_from_row(row) for row in (snapshot.open_orders or [])]
def open_positions(self) -> List[dict[str, Any]]:
snapshot = self._backend_snapshot(include_history=False)
return [dict(row) for row in (snapshot.open_positions or {}).values()]
def reconcile(self) -> List[VenueEvent]:
snapshot = self._backend_snapshot(include_history=True)
return self._events_from_snapshot(snapshot)
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
snapshot_before = self._backend_snapshot(include_history=True)
receipt = self._call_backend("submit_intent", self._legacy_intent(intent))
snapshot_after = self._backend_snapshot(include_history=True)
return self._events_from_submit(intent, receipt, snapshot_before, snapshot_after)
def _events_from_submit(self, intent: KernelIntent, receipt: Any, before, after) -> List[VenueEvent]: # noqa: ANN001
ack_row = dict(getattr(receipt, "raw_ack", {}) or {})
status = _normalize_status(getattr(receipt, "status", "") or _row_text(ack_row, "status", default="NEW"))
order_id = _row_text(ack_row, "orderId", "orderID", default=str(getattr(receipt, "order_id", "") or ""))
client_order_id = _row_text(ack_row, "clientOrderID", "clientOrderId", default=str(getattr(receipt, "client_order_id", "") or intent.intent_id))
if status in {"RATE_LIMITED", "THROTTLED"}:
return [
VenueEvent(
timestamp=getattr(receipt, "timestamp", datetime.now(timezone.utc)),
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=KernelEventKind.RATE_LIMITED,
status=VenueEventStatus.RATE_LIMITED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(getattr(receipt, "price", 0.0), 0.0),
size=float(intent.target_size or 0.0),
filled_size=0.0,
remaining_size=float(intent.target_size or 0.0),
reason=_row_text(ack_row, "msg", "message", default="BINGX_RATE_LIMITED"),
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value, "retry_after_ms": _rate_limit_retry_after_ms(ack_row)},
)
]
base_event = VenueEvent(
timestamp=getattr(receipt, "timestamp", datetime.now(timezone.utc)),
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=KernelEventKind.ORDER_ACK,
status=VenueEventStatus.ACKED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(getattr(receipt, "price", 0.0), 0.0),
size=float(intent.target_size or 0.0),
filled_size=0.0,
remaining_size=float(intent.target_size or 0.0),
reason="",
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
if status in {"REJECTED", "FAILED"}:
return [
VenueEvent(
**{**base_event.__dict__, "event_id": _event_id(self._event_seq), "kind": KernelEventKind.ORDER_REJECT, "status": VenueEventStatus.REJECTED, "reason": _row_text(ack_row, "msg", "message", default="BINGX_ORDER_REJECTED")},
)
]
events = [base_event]
fill_status = _venue_event_status_from_row(status)
filled_size = _row_float(ack_row, "executedQty", "cumFilledQty", "filledQty", "lastFilledQty", default=0.0)
snapshot_fill_size = self._filled_size_from_snapshots(before, after, intent.asset)
if filled_size <= 0:
filled_size = snapshot_fill_size
emit_fill = fill_status in {VenueEventStatus.PARTIALLY_FILLED, VenueEventStatus.FILLED} or snapshot_fill_size > 0.0
if emit_fill:
if filled_size <= 0:
filled_size = float(intent.target_size or 0.0)
remaining_size = max(0.0, float(intent.target_size or 0.0) - float(filled_size))
fill_kind = KernelEventKind.FULL_FILL if fill_status == VenueEventStatus.FILLED or remaining_size <= 1e-12 else KernelEventKind.PARTIAL_FILL
events.append(
VenueEvent(
timestamp=base_event.timestamp,
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=fill_kind,
status=VenueEventStatus.FILLED if fill_kind == KernelEventKind.FULL_FILL else VenueEventStatus.PARTIALLY_FILLED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(_row_float(ack_row, "avgPrice", "ap", "price", "lastFillPrice", default=getattr(receipt, "price", 0.0)), 0.0),
size=float(intent.target_size or 0.0),
filled_size=float(filled_size),
remaining_size=float(remaining_size),
reason="",
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
)
return events
def _events_from_cancel(self, order: VenueOrder, response: Any, before, after, *, reason: str = "") -> List[VenueEvent]: # noqa: ANN001
raw = response if isinstance(response, dict) else {}
status = _normalize_status(_row_text(raw, "status", default="CANCELED"))
if status in {"RATE_LIMITED", "THROTTLED"}:
return [
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=order.internal_trade_id or order.venue_client_id,
slot_id=int(order.metadata.get("slot_id", 0) or 0),
kind=KernelEventKind.RATE_LIMITED,
status=VenueEventStatus.RATE_LIMITED,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=str(order.metadata.get("asset") or ""),
price=safe_float(_row_float(raw, "avgPrice", "ap", "price", "lastFillPrice", default=order.average_fill_price), 0.0),
size=float(order.intended_size or 0.0),
filled_size=float(order.filled_size or 0.0),
remaining_size=float(order.remaining_size),
reason=reason or _row_text(raw, "msg", "message", default="BINGX_RATE_LIMITED"),
raw_payload=raw or {"orderId": order.venue_order_id, "clientOrderId": order.venue_client_id, "status": status or "RATE_LIMITED"},
metadata={**dict(order.metadata), "retry_after_ms": _rate_limit_retry_after_ms(raw)},
)
]
event_status = _venue_event_status_from_row(status)
kind = KernelEventKind.CANCEL_ACK if event_status == VenueEventStatus.CANCELED else KernelEventKind.CANCEL_REJECT
if event_status == VenueEventStatus.CANCELED_REJECTED:
kind = KernelEventKind.CANCEL_REJECT
return [
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=order.internal_trade_id or order.venue_client_id,
slot_id=int(order.metadata.get("slot_id", 0) or 0),
kind=kind,
status=event_status,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=str(order.metadata.get("asset") or ""),
price=safe_float(_row_float(raw, "avgPrice", "ap", "price", "lastFillPrice", default=order.average_fill_price), 0.0),
size=float(order.intended_size or 0.0),
filled_size=float(order.filled_size or 0.0),
remaining_size=float(order.remaining_size),
reason=reason or _row_text(raw, "msg", "message", default="BINGX_CANCEL_ACK" if kind == KernelEventKind.CANCEL_ACK else "BINGX_CANCEL_REJECT"),
raw_payload=raw or {"orderId": order.venue_order_id, "clientOrderId": order.venue_client_id, "status": status or event_status.value},
metadata=dict(order.metadata),
)
]
def _events_from_snapshot(self, snapshot: Any) -> List[VenueEvent]: # noqa: ANN001
events: list[VenueEvent] = []
seen: set[tuple[str, str, str]] = set()
for row in getattr(snapshot, "open_orders", []) or []:
if not isinstance(row, dict):
continue
event = self._event_from_row(row, slot_id=0)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
for row in getattr(snapshot, "all_orders", []) or []:
if not isinstance(row, dict):
continue
event = self._event_from_row(row, slot_id=0)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
for row in getattr(snapshot, "all_fills", []) or []:
if not isinstance(row, dict):
continue
event = self._fill_event_from_row(row)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
return events
def _event_from_row(self, row: dict[str, Any], *, slot_id: int) -> VenueEvent:
status = _normalize_status(_row_text(row, "status", "X", default="NEW"))
event_status = _venue_event_status_from_row(status)
kind = {
VenueEventStatus.ACKED: KernelEventKind.ORDER_ACK,
VenueEventStatus.PARTIALLY_FILLED: KernelEventKind.PARTIAL_FILL,
VenueEventStatus.FILLED: KernelEventKind.FULL_FILL,
VenueEventStatus.CANCELED: KernelEventKind.CANCEL_ACK,
VenueEventStatus.REJECTED: KernelEventKind.ORDER_REJECT,
VenueEventStatus.CANCELED_REJECTED: KernelEventKind.CANCEL_REJECT,
VenueEventStatus.RATE_LIMITED: KernelEventKind.RATE_LIMITED,
}.get(event_status, KernelEventKind.ORDER_ACK)
size = _row_float(row, "origQty", "quantity", "q", "positionAmt", default=0.0)
filled = _row_float(row, "executedQty", "cumFilledQty", "filledQty", "z", "lastFilledQty", default=0.0)
if filled <= 0.0 and kind in {KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL}:
filled = size
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=_row_text(row, "tradeId", "trade_id", default=_row_text(row, "clientOrderId", "clientOrderID", default="")),
slot_id=slot_id,
kind=kind,
status=event_status,
venue_order_id=_row_text(row, "orderId", "orderID", "id", default=""),
venue_client_id=_row_text(row, "clientOrderID", "clientOrderId", "c", default=""),
side=_trade_side_from_row(row),
asset=_row_text(row, "symbol", default=""),
price=safe_float(_row_float(row, "avgPrice", "ap", "price", "lastFillPrice", default=0.0), 0.0),
size=abs(float(size or 0.0)),
filled_size=abs(float(filled or 0.0)),
remaining_size=max(0.0, abs(float(size or 0.0)) - abs(float(filled or 0.0))),
reason=_row_text(row, "msg", "message", default=""),
raw_payload=dict(row),
metadata={"source": "bingx"},
)
def _fill_event_from_row(self, row: dict[str, Any]) -> VenueEvent:
status = _normalize_status(_row_text(row, "status", "X", default="FILLED"))
event_status = _venue_event_status_from_row(status)
kind = KernelEventKind.FULL_FILL if event_status == VenueEventStatus.FILLED else KernelEventKind.PARTIAL_FILL
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=_row_text(row, "tradeId", "trade_id", default=_row_text(row, "clientOrderId", "clientOrderID", default="")),
slot_id=0,
kind=kind,
status=event_status,
venue_order_id=_row_text(row, "orderId", "orderID", "id", default=""),
venue_client_id=_row_text(row, "clientOrderID", "clientOrderId", "c", default=""),
side=_trade_side_from_row(row),
asset=_row_text(row, "symbol", default=""),
price=safe_float(_row_float(row, "lastFillPrice", "L", "price", "ap", default=0.0), 0.0),
size=abs(_row_float(row, "executedQty", "z", "lastFilledQty", default=0.0)),
filled_size=abs(_row_float(row, "lastFilledQty", "l", "z", default=0.0)),
remaining_size=max(0.0, abs(_row_float(row, "executedQty", "z", "lastFilledQty", default=0.0)) - abs(_row_float(row, "lastFilledQty", "l", "z", default=0.0))),
reason=_row_text(row, "msg", "message", default=""),
raw_payload=dict(row),
metadata={"source": "bingx"},
)
@staticmethod
def _filled_size_from_snapshots(before: Any, after: Any, asset: str) -> float: # noqa: ANN001
def _lookup(snapshot: Any) -> float:
positions = getattr(snapshot, "open_positions", {}) or {}
for key, row in positions.items():
symbol = _row_text(row, "symbol", default=str(key))
if symbol.replace("-", "").replace("_", "").upper() == asset.replace("-", "").replace("_", "").upper():
return _position_qty(row)
return 0.0
before_qty = _lookup(before)
after_qty = _lookup(after)
diff = abs(before_qty - after_qty)
return diff

View File

@@ -1,327 +0,0 @@
"""Canonical v2 contracts for the DITAv2 execution kernel."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Any, Dict, Mapping, Optional, Sequence, Tuple
class TradeSide(str, Enum):
"""Trade side."""
LONG = "LONG"
SHORT = "SHORT"
FLAT = "FLAT"
class TradeStage(str, Enum):
"""Execution stage for a trade slot."""
IDLE = "IDLE"
DECISION_CREATED = "DECISION_CREATED"
INTENT_CREATED = "INTENT_CREATED"
ORDER_REQUESTED = "ORDER_REQUESTED"
ORDER_SENT = "ORDER_SENT"
ORDER_ACKED = "ORDER_ACKED"
ORDER_REJECTED = "ORDER_REJECTED"
ENTRY_WORKING = "ENTRY_WORKING"
PARTIAL_FILL = "PARTIAL_FILL"
POSITION_OPENED = "POSITION_OPENED"
POSITION_OPEN = "POSITION_OPEN"
EXIT_REQUESTED = "EXIT_REQUESTED"
EXIT_SENT = "EXIT_SENT"
EXIT_ACKED = "EXIT_ACKED"
EXIT_REJECTED = "EXIT_REJECTED"
EXIT_WORKING = "EXIT_WORKING"
POSITION_PARTIALLY_CLOSED = "POSITION_PARTIALLY_CLOSED"
POSITION_CLOSED = "POSITION_CLOSED"
CLOSED = "CLOSED"
TRADE_TERMINAL_WRITTEN = "TRADE_TERMINAL_WRITTEN"
STALE_STATE_RECONCILING = "STALE_STATE_RECONCILING"
class KernelCommandType(str, Enum):
"""Kernel command types."""
ENTER = "ENTER"
EXIT = "EXIT"
MARK_PRICE = "MARK_PRICE"
RECONCILE = "RECONCILE"
CONTROL = "CONTROL"
CANCEL = "CANCEL"
class KernelEventKind(str, Enum):
"""Normalized venue event kinds."""
ORDER_ACK = "ORDER_ACK"
ORDER_REJECT = "ORDER_REJECT"
RATE_LIMITED = "RATE_LIMITED"
PARTIAL_FILL = "PARTIAL_FILL"
FULL_FILL = "FULL_FILL"
CANCEL_ACK = "CANCEL_ACK"
CANCEL_REJECT = "CANCEL_REJECT"
MARK_PRICE = "MARK_PRICE"
RECONCILE = "RECONCILE"
CONTROL = "CONTROL"
class KernelDiagnosticCode(str, Enum):
"""Structured diagnostic codes emitted by the kernel."""
OK = "OK"
RATE_LIMITED = "RATE_LIMITED"
INVALID_SLOT_ID = "INVALID_SLOT_ID"
UNSUPPORTED_INTENT = "UNSUPPORTED_INTENT"
SLOT_BUSY = "SLOT_BUSY"
NO_OPEN_POSITION = "NO_OPEN_POSITION"
NO_ACTIVE_EXIT_ORDER = "NO_ACTIVE_EXIT_ORDER"
UNKNOWN_EVENT_KIND = "UNKNOWN_EVENT_KIND"
ORDER_REJECTED = "ORDER_REJECTED"
ENTRY_ORDER_REJECTED = "ENTRY_ORDER_REJECTED"
EXIT_ORDER_REJECTED = "EXIT_ORDER_REJECTED"
CANCEL_REJECTED = "CANCEL_REJECTED"
STALE_STATE_RECONCILE = "STALE_STATE_RECONCILE"
RECONCILED = "RECONCILED"
DUPLICATE_EVENT = "DUPLICATE_EVENT"
UNRESOLVED_SLOT = "UNRESOLVED_SLOT"
INVALID_TRANSITION = "INVALID_TRANSITION"
TERMINAL_STATE = "TERMINAL_STATE"
class KernelSeverity(str, Enum):
"""Severity classification for kernel outcomes."""
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"
class VenueOrderStatus(str, Enum):
"""Order status surface mirrored from venue truth."""
NEW = "NEW"
ACKED = "ACKED"
PARTIALLY_FILLED = "PARTIALLY_FILLED"
FILLED = "FILLED"
CANCELED = "CANCELED"
REJECTED = "REJECTED"
class VenueEventStatus(str, Enum):
"""Status alias for normalized venue events."""
ACKED = "ACKED"
REJECTED = "REJECTED"
RATE_LIMITED = "RATE_LIMITED"
PARTIALLY_FILLED = "PARTIALLY_FILLED"
FILLED = "FILLED"
CANCELED = "CANCELED"
CANCELED_REJECTED = "CANCEL_REJECTED"
@dataclass(frozen=True)
class VenueOrder:
"""Venue-specific order identity and fill state."""
internal_trade_id: str
venue_order_id: str
venue_client_id: str
side: TradeSide
intended_size: float
filled_size: float = 0.0
average_fill_price: float = 0.0
status: VenueOrderStatus = VenueOrderStatus.NEW
metadata: Dict[str, Any] = field(default_factory=dict)
@property
def remaining_size(self) -> float:
return max(0.0, float(self.intended_size) - float(self.filled_size))
@dataclass
class TradeSlot:
"""A single execution slot managed by the v2 kernel."""
slot_id: int
trade_id: str = ""
asset: str = ""
side: TradeSide = TradeSide.FLAT
entry_price: float = 0.0
size: float = 0.0
initial_size: float = 0.0
leverage: float = 0.0
entry_time: Optional[datetime] = None
unrealized_pnl: float = 0.0
realized_pnl: float = 0.0
closed: bool = False
exit_leg_ratios: Tuple[float, ...] = (1.0,)
active_leg_index: int = 0
active_exit_order: Optional[VenueOrder] = None
active_entry_order: Optional[VenueOrder] = None
fsm_state: TradeStage = TradeStage.IDLE
close_reason: str = ""
last_event_time: Optional[datetime] = None
seen_event_ids: Tuple[str, ...] = ()
metadata: Dict[str, Any] = field(default_factory=dict)
def is_free(self) -> bool:
return self.fsm_state in {TradeStage.IDLE, TradeStage.CLOSED} and float(self.size or 0.0) <= 0.0 and not self.active_entry_order and not self.active_exit_order
def is_open(self) -> bool:
return self.fsm_state in {
TradeStage.ENTRY_WORKING,
TradeStage.POSITION_OPENED,
TradeStage.POSITION_OPEN,
TradeStage.EXIT_WORKING,
} and not self.closed
def mark_price(self, price: float) -> None:
if price is None or price != price or price <= 0:
return
self.entry_price = self.entry_price or price
if self.entry_price <= 0 or self.size <= 0:
self.unrealized_pnl = 0.0
return
delta = (price - self.entry_price) / self.entry_price
if self.side == TradeSide.SHORT:
delta = -delta
self.unrealized_pnl = delta * self.size * self.entry_price * self.leverage
def next_exit_ratio(self) -> float:
if self.active_leg_index < len(self.exit_leg_ratios):
ratio = float(self.exit_leg_ratios[self.active_leg_index])
return max(0.0, min(1.0, ratio))
return 1.0
def consume_exit_leg(self) -> float:
ratio = self.next_exit_ratio()
self.active_leg_index = min(self.active_leg_index + 1, max(len(self.exit_leg_ratios), 1))
return ratio
def remaining_size(self) -> float:
return max(0.0, float(self.size))
def attach_entry_order(self, order: VenueOrder) -> None:
self.active_entry_order = order
def attach_exit_order(self, order: VenueOrder) -> None:
self.active_exit_order = order
def to_dict(self) -> Dict[str, Any]:
def _order_dict(order: Optional[VenueOrder]) -> Optional[Dict[str, Any]]:
if order is None:
return None
return {
"internal_trade_id": order.internal_trade_id,
"venue_order_id": order.venue_order_id,
"venue_client_id": order.venue_client_id,
"side": order.side.value,
"intended_size": float(order.intended_size or 0.0),
"filled_size": float(order.filled_size or 0.0),
"average_fill_price": float(order.average_fill_price or 0.0),
"status": order.status.value,
"metadata": dict(order.metadata),
}
return {
"slot_id": self.slot_id,
"trade_id": self.trade_id,
"asset": self.asset,
"side": self.side.value,
"entry_price": float(self.entry_price or 0.0),
"size": float(self.size or 0.0),
"initial_size": float(self.initial_size or 0.0),
"leverage": float(self.leverage or 0.0),
"entry_time": self.entry_time.isoformat() if hasattr(self.entry_time, "isoformat") else None,
"unrealized_pnl": float(self.unrealized_pnl or 0.0),
"realized_pnl": float(self.realized_pnl or 0.0),
"closed": bool(self.closed),
"exit_leg_ratios": [float(r) for r in self.exit_leg_ratios],
"active_leg_index": int(self.active_leg_index or 0),
"active_exit_order": _order_dict(self.active_exit_order),
"active_entry_order": _order_dict(self.active_entry_order),
"fsm_state": self.fsm_state.value,
"close_reason": self.close_reason,
"last_event_time": self.last_event_time.isoformat() if hasattr(self.last_event_time, "isoformat") else None,
"seen_event_ids": list(self.seen_event_ids),
"metadata": dict(self.metadata),
}
@dataclass(frozen=True)
class KernelIntent:
"""Command emitted by the algo and written to the hot-path intent region."""
timestamp: datetime
intent_id: str
trade_id: str
slot_id: int
asset: str
side: TradeSide
action: KernelCommandType
reference_price: float
target_size: float
leverage: float
exit_leg_ratios: Tuple[float, ...] = (1.0,)
reason: str = ""
metadata: Dict[str, Any] = field(default_factory=dict)
stage: TradeStage = TradeStage.INTENT_CREATED
@dataclass(frozen=True)
class VenueEvent:
"""Normalized venue truth mapped into DITAv2 semantics."""
timestamp: datetime
event_id: str
trade_id: str
slot_id: int
kind: KernelEventKind
status: VenueEventStatus
venue_order_id: str = ""
venue_client_id: str = ""
side: TradeSide = TradeSide.FLAT
asset: str = ""
price: float = 0.0
size: float = 0.0
filled_size: float = 0.0
remaining_size: float = 0.0
reason: str = ""
raw_payload: Dict[str, Any] = field(default_factory=dict)
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class KernelTransition:
"""Durable kernel transition used for debug journaling."""
timestamp: datetime
trade_id: str
slot_id: int
prev_state: TradeStage
next_state: TradeStage
trigger: str
intent_id: str = ""
event_id: str = ""
control_mode: str = ""
control_verbosity: str = ""
details: Dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class KernelOutcome:
"""Result of applying a command or venue event."""
accepted: bool
slot_id: int
trade_id: str
state: TradeStage
diagnostic_code: KernelDiagnosticCode = KernelDiagnosticCode.OK
severity: KernelSeverity = KernelSeverity.INFO
transitions: Tuple[KernelTransition, ...] = ()
emitted_events: Tuple[VenueEvent, ...] = ()
details: Dict[str, Any] = field(default_factory=dict)

View File

@@ -1,217 +0,0 @@
"""Runtime control plane for DITAv2."""
from __future__ import annotations
from dataclasses import asdict, dataclass, replace
from enum import Enum
import os
import threading
import time
from typing import Any, Dict, Mapping, Optional, Protocol
from .utils import json_safe
class KernelMode(str, Enum):
NORMAL = "NORMAL"
DEBUG = "DEBUG"
class KernelVerbosity(str, Enum):
QUIET = "QUIET"
VERBOSE = "VERBOSE"
TRACE = "TRACE"
class BackendMode(str, Enum):
MOCK = "MOCK"
BINGX = "BINGX"
@dataclass(frozen=True)
class KernelControlSnapshot:
"""Control plane state shared across the kernel."""
mode: KernelMode = KernelMode.NORMAL
verbosity: KernelVerbosity = KernelVerbosity.QUIET
backend_mode: BackendMode = BackendMode.MOCK
debug_clickhouse_enabled: bool = True
trace_transitions: bool = False
mirror_to_hazelcast: bool = True
active_slot_limit: int = 10
reconcile_on_restart: bool = True
runtime_namespace: str = "dita_v2"
strategy_namespace: str = "dita_v2"
event_namespace: str = "dita_v2"
actor_name: str = "ExecutionKernel"
exec_venue: str = "bingx"
data_venue: str = "binance"
ledger_authority: str = "exchange"
mock_fidelity_mode: str = "bingx_exact_shape"
def as_dict(self) -> Dict[str, Any]:
return dict(asdict(self))
@dataclass(frozen=True)
class ControlUpdate:
"""Partial update to the control plane."""
mode: Optional[KernelMode] = None
verbosity: Optional[KernelVerbosity] = None
backend_mode: Optional[BackendMode] = None
debug_clickhouse_enabled: Optional[bool] = None
trace_transitions: Optional[bool] = None
mirror_to_hazelcast: Optional[bool] = None
active_slot_limit: Optional[int] = None
reconcile_on_restart: Optional[bool] = None
runtime_namespace: Optional[str] = None
strategy_namespace: Optional[str] = None
event_namespace: Optional[str] = None
actor_name: Optional[str] = None
exec_venue: Optional[str] = None
data_venue: Optional[str] = None
ledger_authority: Optional[str] = None
mock_fidelity_mode: Optional[str] = None
def apply(self, snapshot: KernelControlSnapshot) -> KernelControlSnapshot:
payload = {
key: value
for key, value in asdict(self).items()
if value is not None
}
return replace(snapshot, **payload)
class ControlPlane(Protocol):
"""Kernel control plane interface."""
def read(self) -> KernelControlSnapshot:
...
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
...
def mirror(self) -> Mapping[str, Any]:
...
def wait(self, timeout_ms: int = 1000) -> bool:
...
def notify(self) -> None:
...
class InMemoryControlPlane:
"""Local control plane used for tests and the Python prototype."""
def __init__(self, snapshot: Optional[KernelControlSnapshot] = None):
self._snapshot = snapshot or KernelControlSnapshot()
self._mirror: Dict[str, Any] = {}
self._seq = 0
self._observed_seq = 0
self._signal = threading.Condition()
def read(self) -> KernelControlSnapshot:
return self._snapshot
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
with self._signal:
self._snapshot = update.apply(self._snapshot)
self._mirror = self._snapshot.as_dict()
self._seq += 1
self._signal.notify_all()
return self._snapshot
def mirror(self) -> Mapping[str, Any]:
return dict(self._mirror)
def wait(self, timeout_ms: int = 1000) -> bool:
timeout_s = None if timeout_ms is None or timeout_ms < 0 else max(0.0, timeout_ms / 1000.0)
deadline = None if timeout_s is None else time.monotonic() + timeout_s
with self._signal:
observed = self._observed_seq
while self._seq == observed:
if deadline is None:
self._signal.wait()
continue
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
self._signal.wait(timeout=remaining)
self._observed_seq = self._seq
return True
def notify(self) -> None:
with self._signal:
self._seq += 1
self._signal.notify_all()
class ZincControlPlane(InMemoryControlPlane):
"""In-memory stand-in for a Zinc-backed control region.
The class keeps the interface explicit so a real Zinc binding can be
dropped in later without changing kernel code.
"""
def __init__(self, snapshot: Optional[KernelControlSnapshot] = None):
super().__init__(snapshot=snapshot)
self.region: Dict[str, Any] = self._snapshot.as_dict()
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = super().update(update)
self.region = snapshot.as_dict()
return snapshot
def read(self) -> KernelControlSnapshot:
return self._snapshot
class MirroredControlPlane:
"""Control plane that mirrors updates to an external durable sink."""
def __init__(self, inner: ControlPlane, mirror_sink: Optional[Any] = None):
self.inner = inner
self.mirror_sink = mirror_sink
def read(self) -> KernelControlSnapshot:
return self.inner.read()
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = self.inner.update(update)
if self.mirror_sink is not None:
self.mirror_sink("dita_control_plane", dict(snapshot.as_dict()))
return snapshot
def mirror(self) -> Mapping[str, Any]:
return self.inner.mirror()
def build_control_plane(
snapshot: Optional[KernelControlSnapshot] = None,
*,
prefer_real_zinc: Optional[bool] = None,
prefix: str = "dita_v2",
) -> ControlPlane:
"""Build the active control plane with an operator-visible switch.
The default remains the in-process Zinc stand-in so existing tests and
callers stay stable. Setting ``DITA_V2_CONTROL_PLANE=REAL_ZINC`` or passing
``prefer_real_zinc=True`` opts into the shared-memory control plane when
the Zinc adapter is available.
"""
env_choice = os.environ.get("DITA_V2_CONTROL_PLANE", "").strip().upper()
real_requested = prefer_real_zinc if prefer_real_zinc is not None else env_choice in {"REAL", "REAL_ZINC", "SHARED", "SHARED_MEM"}
if real_requested:
try:
from .real_control_plane import RealZincControlPlane
plane = RealZincControlPlane(prefix=prefix, create=True)
if snapshot is not None:
plane.update(ControlUpdate(**{key: value for key, value in snapshot.as_dict().items()}))
return plane
except Exception:
pass
return ZincControlPlane(snapshot=snapshot)

View File

@@ -1,438 +0,0 @@
#!/usr/bin/env python3
"""Write the complete 68-test live e2e file. Bodies receive (k, symbol, p) where p is a float."""
import ast, os
SCENARIOS = [] # (name, code_lines)
def S(name, lines):
SCENARIOS.append((name, lines))
# ---- Original 9 ----
S("simple_entry_exit", [
"tid = f's-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("multi_leg_exit", [
"tid = f'ml-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
])
S("cancel_entry_order", [
"tid = f'ce-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("entry_hold_exit", [
"tid = f'h-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(3)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("entry_exit_at_loss", [
"tid = f'l-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*1.005, 0.001); await asyncio.sleep(1)",
])
S("two_sequential_cycles", [
"t1 = f'2c1-{int(time.time()*1000)}'; t2 = f'2c2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(1)",
])
S("entry_then_recover", [
"tid = f'r-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"await bundle.runtime.disconnect()",
"await bundle.runtime.connect(initial_capital=k.account.snapshot.capital)",
"await asyncio.sleep(1)",
])
S("long_entry_exit", [
"tid = f'ln-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'LONG', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'LONG', p*1.005, 0.001); await asyncio.sleep(1)",
])
# ---- Cancel combos ----
S("cancel_idempotent", [
"tid = f'ci-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("double_cancel", [
"tid = f'dc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("cancel_then_exit", [
"tid = f'ctx-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("exit_then_cancel_exit", [
"tid = f'exc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("exit_then_reentry", [
"t1 = f'er1-{int(time.time()*1000)}'; t2 = f'er2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("limit_cancel", [
"tid = f'lc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p*0.9, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p*0.9, 0.001); await asyncio.sleep(1)",
])
# ---- X4 ----
S("x4_partial_hold_exit", [
"tid = f'ph-{int(time.time()*1000)}'; sz = 0.003",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.3, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.7, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
])
S("x4_three_leg", [
"tid = f'3l-{int(time.time()*1000)}'; sz = 0.004",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*0.5, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
])
S("x4_cancel_fill_partial", [
"tid = f'cfp-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.002); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001); await asyncio.sleep(1)",
])
S("x4_rapid_three", [
"for i in range(3):",
" tid = f'r3-{i}-{int(time.time()*1000)}'",
" _si(k, E.ENTER, tid, symbol, 'SHORT', p*(1-i*0.005), 0.001); await asyncio.sleep(0.8)",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-i*0.005), 0.001); await asyncio.sleep(0.8)",
])
S("x4_diff_symbol", [
"tid = f'ds-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"sym2 = 'BTCUSDT' if symbol != 'BTCUSDT' else 'ETHUSDT'",
"_si(k, E.EXIT, tid, sym2, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
])
S("x4_alternating", [
"t1 = f'as1-{int(time.time()*1000)}'; t2 = f'as2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"sym2 = 'BTCUSDT' if symbol != 'BTCUSDT' else 'ETHUSDT'",
"try:",
" p2 = float(json.loads(urllib.request.urlopen('https://open-api-vst.bingx.com/openApi/swap/v2/quote/price?symbol='+sym2.replace('USDT','-USDT'), timeout=5).read())['data']['price'])",
"except: p2 = p",
"_si(k, E.ENTER, t2, sym2, 'LONG', p2, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t2, sym2, 'LONG', p2*1.005, 0.001); await asyncio.sleep(1)",
])
S("x4_multi_flatten", [
"tid = f'mf-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"for i in range(3):",
" if k.slot(0).is_free(): break",
" _flatten(k, symbol, p*0.99, f'mf{i}'); await asyncio.sleep(0.5)",
])
S("x4_three_leg_25_50_25", [
"tid = f'x4a-{int(time.time()*1000)}'; sz = 0.004",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.5, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
])
S("x4_enter_exit_hold_twice", [
"t1 = f'x4b1-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"t2 = f'x4b2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
"t3 = f'x4b3-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t3, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t3, symbol, 'SHORT', p*0.985, 0.001); await asyncio.sleep(0.5)",
])
S("x4_cancel_then_double_exit", [
"tid = f'x4c-{int(time.time()*1000)}'; sz = 0.002",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, sz); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
])
# ---- 2 sides x 2 profit x 4 patterns = 16 doubled ----
for side, side_str, ep in [("short","SHORT",0.995), ("long","LONG",1.005)]:
for prof, pname, xp in [(True,"profit",ep), (False,"loss",1/ep)]:
for pat, pat_suffix, lines in [
("basic", "", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.8)",
]),
("partial", "_partial", [
"sz = 0.002",
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{ep}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
]),
("cancel", "_cancel", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.3)",
f"_si(k, E.CANCEL, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
f" _si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.8)",
]),
("double_exit", "_double_exit", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
f" _si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}*0.995, 0.001); await asyncio.sleep(0.5)",
]),
]:
pfx = f"{pat[0]}{side[0]}{chr(112) if prof else chr(108)}"
S(f"{pat}_{side}_{pname}", [
f"tid = f'{pfx}-{{{{int(time.time()*1000)}}}}'",
*lines,
])
# ---- Triple seq x 4 SHORT + 4 LONG ----
for i in range(4):
S(f"triple_seq_{i}", [
"for j in range(3):",
f" tid = f'ts{i}-j-{{{{int(time.time()*1000)}}}}'",
" _si(k, E.ENTER, tid, symbol, 'SHORT', p*(1-j*0.003), 0.001); await asyncio.sleep(0.7)",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-j*0.003), 0.001); await asyncio.sleep(0.7)",
])
for i in range(4):
S(f"triple_seq_long_{i}", [
"for j in range(3):",
f" tid = f'tsl{i}-j-{{{{int(time.time()*1000)}}}}'",
" _si(k, E.ENTER, tid, symbol, 'LONG', p*(1+j*0.003), 0.001); await asyncio.sleep(0.7)",
" _si(k, E.EXIT, tid, symbol, 'LONG', p*1.005*(1+j*0.003), 0.001); await asyncio.sleep(0.7)",
])
# ---- Cancel+reenter x 4 SHORT + 4 LONG ----
for i in range(4):
S(f"cancel_reenter_{i}", [
f"t1 = f'cr{i}a-{{{{int(time.time()*1000)}}}}'; t2 = f'cr{i}b-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.8)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
])
for i in range(4):
S(f"cancel_reenter_long_{i}", [
f"t1 = f'crl{i}a-{{{{int(time.time()*1000)}}}}'; t2 = f'crl{i}b-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, t1, symbol, 'LONG', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, t1, symbol, 'LONG', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'LONG', p*1.005, 0.001); await asyncio.sleep(0.8)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t2, symbol, 'LONG', p*1.01, 0.001); await asyncio.sleep(0.5)",
])
# ---- Leg ratios x 8 ----
for i, ratios in enumerate([
(0.1,1.0), (0.33,0.33,1.0), (0.5,0.5,1.0), (0.75,1.0),
(0.2,0.3,0.5,1.0), (0.4,0.6,1.0), (0.15,0.85,1.0), (0.25,0.25,0.5,1.0),
]):
rat_str = ",".join(str(r) for r in ratios)
code = [f"tid = f'lr{i}-{{{{int(time.time()*1000)}}}}'; sz = 0.004",
f"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=({rat_str})); await asyncio.sleep(1)"]
for leg in range(len(ratios) - 1):
r = ratios[leg]
code.append(f"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-{leg}*0.002), sz*{r}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)")
code.append(f"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*{ratios[-1]}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)")
S(f"leg_ratio_{i}", code)
# ---- Breakeven x 4 ----
for i in range(4):
S(f"breakeven_{i}", [
f"tid = f'be{i}-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
])
# =====================================================================
# Assemble
# =====================================================================
HEADER = '''#!/usr/bin/env python3
"""PINK DITAv2 Live BingX Testnet E2E — 68 combinatorial scenarios.
Kernel-direct tests: bodies receive (k, symbol, p). Capital integrity
asserted. Exchange state confirmed flat.
"""
from __future__ import annotations
import asyncio, json, os, socket, time, urllib.request
import urllib.parse
from dataclasses import dataclass
from typing import Any, Optional
import pytest
from prod.bingx.http import BingxHttpClient
from prod.bingx.config import BingxExecClientConfig, BingxEnvironment
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot
E = KC
# Force IPv4 for httpx (IPv6 resolution fails in this env)
_orig_gai = socket.getaddrinfo
def _ipv4_gai(host, port, family=0, type=0, proto=0, flags=0):
return _orig_gai(host, port, socket.AF_INET, type, proto, flags)
socket.getaddrinfo = _ipv4_gai
# ---- env gates ----
if not os.environ.get("BINGX_SMOKE_LIVE"):
pytest.skip("BINGX_SMOKE_LIVE not set", allow_module_level=True)
if not os.environ.get("BINGX_SMOKE_ALLOW_TRADE"):
pytest.skip("BINGX_SMOKE_ALLOW_TRADE not set", allow_module_level=True)
if not os.environ.get("PINK_DITA_E2E"):
pytest.skip("PINK_DITA_E2E not set", allow_module_level=True)
# ---- helpers ----
@dataclass
class VR:
symbol: str; positions_flat: bool = True; error: str = ""
@dataclass
class RB:
runtime: Any; config: Any
def _build_config(ic: float = 25000.0) -> BingxExecClientConfig:
return BingxExecClientConfig(
api_key=os.environ["BINGX_API_KEY"], secret_key=os.environ["BINGX_SECRET_KEY"],
environment=BingxEnvironment.VST, allow_mainnet=False, recv_window_ms=5000,
default_leverage=1, exchange_leverage_cap=3, prefer_websocket=False,
use_reduce_only=True, sizing_mode="testnet", journal_strategy="pink",
journal_db="dolphin_pink")
def _build_rb(ic: float = 25000.0) -> RB:
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)
async def _contract_rows(c):
r = await c._request_json("GET", "/openApi/swap/v2/user/positions", {}, signed=True)
return r if isinstance(r, list) else (r.get("data") or r.get("positions") or [])
async def _pick_sym(k, c):
rs = await _contract_rows(c)
oss = {str(r.get("symbol","")).replace("-","").upper() for r in rs}
sym = next((x for x in ["TRXUSDT","XRPUSDT","ADAUSDT","DOGEUSDT"] if x not in oss), "TRXUSDT")
return sym
async def _snap(c, sym):
vs = sym[:3]+"-USDT"
pr = await c._request_json("GET", "/openApi/swap/v2/quote/price", {"symbol": vs}, signed=False)
d = pr.get("data") or pr; rp = float(d.get("price") or d.get("lastPrice") or 0)
return MarketSnapshot(timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
symbol=sym, price=rp, bid=rp*0.9995, ask=rp*1.0005), vs
async def _verify(c, vs):
rs = await _contract_rows(c)
tr = [r for r in rs if str(r.get("symbol","")).upper().replace("-","") == vs.replace("-","").upper()]
ts = sum(abs(float(r.get("positionAmt",r.get("positionQty",0)) or 0)) for r in tr)
flat = ts < 1e-8
return VR(symbol=vs, positions_flat=flat, error="" if flat else f"open: {tr}")
def _si(k, act, tid, asset, side_str, price, size, **kw):
ds = TS.SHORT if side_str.upper() == "SHORT" else TS.LONG
return k.process_intent(KI(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=tid, trade_id=tid, slot_id=0, asset=asset, side=ds, action=act,
reference_price=price, target_size=size, leverage=kw.pop("leverage",1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios",(1.0,)),
reason=kw.pop("reason",f"auto_{act.value.lower()}"), metadata=kw))
def _flatten(k, sym, price, label):
if k.slot(0).is_free(): return
_si(k, E.EXIT, f"fl{label}-{int(time.time()*1000)}", sym, "SHORT", price, 0.001)
async def _run(bundle, client, body_fn, label, ic):
k = bundle.runtime.kernel
sym = await _pick_sym(k, client)
snap, vsym = await _snap(client, sym)
await bundle.runtime.connect(initial_capital=ic)
p = float(snap.price)
try:
_flatten(k, sym, p, f"{label}-pre")
await asyncio.sleep(0.3)
cb = k.account.snapshot.capital
await body_fn(k, sym, p)
ca = k.account.snapshot.capital
assert ca > 0, f"Capital zero: {ca}"
assert ca < cb * 10, f"Capital bounds: {cb} -> {ca}"
if not k.slot(0).is_free():
_flatten(k, sym, p*0.99, f"{label}-post")
await asyncio.sleep(1.0)
return await _verify(client, vsym)
finally:
await bundle.runtime.disconnect()
'''
lines = [HEADER]
# Scenario bodies
lines.append("\n# =====================================================================\n# Scenario bodies\n# =====================================================================\n")
for name, code_lines in SCENARIOS:
lines.append(f"async def _body_{name}(k, symbol, p):")
for cl in code_lines:
lines.append(f" {cl}")
lines.append("")
# Test functions
lines.append("\n# =====================================================================\n# Test functions\n# =====================================================================\n")
lines.append('''@pytest.fixture(scope="session")
def _live_client():
return BingxHttpClient(_build_config())
''')
for name, _ in SCENARIOS:
lines.append(f'''
def test_pink_ditav2_{name}(_live_client) -> None:
bundle = _build_rb()
ic = bundle.runtime.kernel.account.snapshot.capital
r = asyncio.run(_run(bundle, _live_client, _body_{name}, "{name}", ic))
assert r.positions_flat, name + ": " + r.error
''')
full = '\n'.join(lines)
try:
ast.parse(full)
count = full.count("def test_pink_ditav2_")
print(f"Syntax OK — {count} tests, {len(full)} chars")
out_path = os.path.join('/mnt/dolphinng5_predict', 'prod/tests/test_pink_bingx_dita_live_e2e.py')
with open(out_path, 'w') as f:
f.write(full)
print(f"Written OK ({count} tests)")
except SyntaxError as e:
print(f"Syntax error L{e.lineno}: {e.msg}")
fl = full.split('\n')
for i in range(max(0,e.lineno-5), min(len(fl), e.lineno+3)):
print(f" {i+1}: {fl[i]}")

View File

@@ -1,688 +0,0 @@
#!/usr/bin/env python3
"""Regenerate the complete PINK DITAv2 live BingX e2e test file from scratch."""
import ast, os
BASE = '/mnt/dolphinng5_predict'
OUT = os.path.join(BASE, 'prod/tests/test_pink_bingx_dita_live_e2e.py')
# =====================================================================
# Static prologue — imports, helpers, env check
# =====================================================================
PROLOGUE = r'''#!/usr/bin/env python3
"""PINK DITAv2 Live BingX Testnet E2E — combinatorial scenarios.
Each test:
1. Picks a live VST symbol with price
2. Submits KernelIntent directly (bypasses DecisionEngine)
3. Asserts capital integrity (positive, within bounds)
4. Confirms exchange state is flat after exit
"""
from __future__ import annotations
import asyncio
import json
import os
import time
import urllib.parse
import urllib.request
from dataclasses import dataclass, field
from decimal import Decimal
from typing import Any, Optional
import pytest
import requests
from prod.bingx.http import BingxHttpClient
from prod.bingx.config import BingxExecClientConfig, BingxEnvironment
from prod.bingx.schemas import BingxContract
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelIntent,
KernelOutcome,
TradeSide,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot
from prod.clean_arch.dita import DecisionConfig, DecisionEngine, IntentEngine
from prod.clean_arch.runtime.pink_direct import PinkDirectRuntime
from prod.clean_arch.projection import build_projection
from prod.clean_arch.adapters.hazelcast_feed import HazelcastDataFeed
# ---- env gates ----
if not os.environ.get("BINGX_SMOKE_LIVE"):
pytest.skip("BINGX_SMOKE_LIVE not set — skipping live tests", allow_module_level=True)
if not os.environ.get("BINGX_SMOKE_ALLOW_TRADE"):
pytest.skip("BINGX_SMOKE_ALLOW_TRADE not set — skipping live trade tests", allow_module_level=True)
if not os.environ.get("PINK_DITA_E2E"):
pytest.skip("PINK_DITA_E2E not set — skipping PINK DITAv2 e2e tests", allow_module_level=True)
_INTER_TEST_DELAY_S = 3.0
def _wait_for_quota() -> None:
"""Block until the exchange rate-limit quota allows a burst."""
time.sleep(_INTER_TEST_DELAY_S)
def _normalize(symbol: str) -> str:
return symbol.replace("-", "").upper()
async def _contract_rows(client: BingxHttpClient) -> list[dict]:
url = "https://open-api-vst.bingx.com/openApi/swap/v2/user/positions"
rows = await client._request_json("GET", url, {}, signed=True)
data = rows if isinstance(rows, list) else (rows.get("data") or rows.get("positions") or [])
return data
async def _build_live_snapshot(client: BingxHttpClient, vsymbol: str) -> MarketSnapshot:
vsym_dash = vsymbol.replace("USDT", "-USDT")
price_resp = await client._request_json("GET", "https://open-api-vst.bingx.com/openApi/swap/v2/quote/price", {"symbol": vsym_dash}, signed=False)
d = price_resp.get("data") or price_resp
raw_price = d.get("price") or d.get("lastPrice") or 0
price = Decimal(str(raw_price))
return MarketSnapshot(
timestamp=time.time(), price=price, bid=price * Decimal("0.9995"),
ask=price * Decimal("1.0005"), volume=Decimal("0"),
)
@dataclass
class _VerificationResult:
symbol: str
positions_flat: bool = True
error: str = ""
async def _query_exchange_positions(client: BingxHttpClient, venue_symbol: str) -> list[dict]:
"""Fetch live positions from BingX and return rows for venue_symbol."""
rows = _contract_rows(client)
return [r for r in rows if str(r.get("symbol", "")).upper().replace("-", "") == venue_symbol.replace("-", "").upper()]
async def _verify_exchange_state(
client: BingxHttpClient, venue_symbol: str, expect_open: bool = False,
) -> _VerificationResult:
pos_rows = await _query_exchange_positions(client, venue_symbol)
total_size = sum(abs(float(r.get("positionAmt", r.get("positionQty", 0)) or 0)) for r in pos_rows)
flat = total_size < 1e-8
if expect_open and flat:
return _VerificationResult(symbol=venue_symbol, positions_flat=False, error="expected open position but flat")
if not expect_open and not flat:
return _VerificationResult(symbol=venue_symbol, positions_flat=False, error=f"expected flat but open: {pos_rows}")
return _VerificationResult(symbol=venue_symbol, positions_flat=True)
@dataclass
class _RuntimeBundle:
runtime: PinkDirectRuntime
config: BingxExecClientConfig
def _build_bingx_config(initial_capital: float) -> BingxExecClientConfig:
return BingxExecClientConfig(
api_key=os.environ["BINGX_API_KEY"],
secret_key=os.environ["BINGX_SECRET_KEY"],
environment=BingxEnvironment.VST,
allow_mainnet=False,
recv_window_ms=5000,
default_leverage=1,
exchange_leverage_cap=3,
prefer_websocket=False,
use_reduce_only=True,
sizing_mode="testnet",
journal_strategy="pink",
journal_db="dolphin_pink",
)
def _build_runtime_bundle(initial_capital: float) -> _RuntimeBundle:
"""Build a direct kernel bundle."""
cfg = _build_bingx_config(initial_capital)
bundle = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
k = bundle.kernel
k.account.snapshot.capital = initial_capital
k.account.snapshot.peak_capital = initial_capital
k.account.snapshot.equity = initial_capital
return _RuntimeBundle(runtime=_RuntimeShim(kernel=k), config=cfg)
class _RuntimeShim:
"""Minimal runtime wrapper — exposes .kernel + sync connect/disconnect."""
def __init__(self, kernel): self.kernel = kernel
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except Exception: pass
def _build_full_runtime(initial_capital: float) -> PinkDirectRuntime:
"""Build a fully wired PinkDirectRuntime (data feed, engine, persistence)."""
cfg = _build_bingx_config(initial_capital)
bundle = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
feed = HazelcastDataFeed(
prefix="dita_v2",
hz_client=build_projection(prefer_real_hazelcast=False),
)
engine = DecisionEngine(DecisionConfig(initial_capital=initial_capital))
intent_engine = IntentEngine(initial_capital=initial_capital)
rt = PinkDirectRuntime(
data_feed=feed, kernel=bundle.kernel,
decision_engine=engine, intent_engine=intent_engine,
)
rt.kernel.account.snapshot.capital = initial_capital
rt.kernel.account.snapshot.peak_capital = initial_capital
rt.kernel.account.snapshot.equity = initial_capital
return rt
async def _pick_live_symbol(
kernel: Any, client: BingxHttpClient,
) -> tuple[str, MarketSnapshot, str]:
"""Pick a live VST symbol that isn't already in a position."""
pos_rows = _contract_rows(client)
open_syms = set()
for r in pos_rows:
sym = str(r.get("symbol", "")).replace("-", "").upper()
if sym:
open_syms.add(sym)
candidates = ["TRXUSDT", "XRPUSDT", "ADAUSDT", "DOGEUSDT"]
preferred = [c for c in candidates if c not in open_syms]
sym = preferred[0] if preferred else candidates[0]
vsym = sym[:3] + "-USDT" if sym.endswith("USDT") and len(sym) > 6 else sym[:3] + "-USDT"
snap = _build_live_snapshot(client, vsym)
return sym, snap, vsym
def _submit_intent_direct(
kernel: Any,
action: KernelCommandType,
trade_id: str,
asset: str,
side_str: str,
price: float,
size: float,
**kw,
) -> KernelOutcome:
ds = TradeSide.SHORT if side_str.upper() == "SHORT" else TradeSide.LONG
intent = KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=trade_id,
trade_id=trade_id,
slot_id=0,
asset=asset,
side=ds,
action=action,
reference_price=price,
target_size=size,
leverage=kw.pop("leverage", 1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios", (1.0,)),
reason=kw.pop("reason", f"auto_{action.value.lower()}"),
metadata=kw,
)
return kernel.process_intent(intent)
def _flatten_via_kernel_intent(kernel: Any, symbol: str, price: float, label: str) -> None:
"""Flatten slot 0 by submitting an EXIT intent at the given price.
No-op if already flat."""
if kernel.slot(0).is_free():
return
tid = f"flat-{label}-{int(time.time() * 1000)}"
side = TradeSide.SHORT
intent = KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=tid,
trade_id=tid,
slot_id=0,
asset=symbol,
side=side,
action=KernelCommandType.EXIT,
reference_price=price,
target_size=0.001,
leverage=1.0,
exit_leg_ratios=(1.0,),
reason=f"flatten_{label}",
)
kernel.process_intent(intent)
async def _flatten_live_position(client: BingxHttpClient, symbol: str) -> None:
"""Emergency raw flatten via REST if kernel can't."""
pass
async def _run_pink_live_roundtrip(
bundle: _RuntimeBundle, client: BingxHttpClient,
) -> tuple[KernelOutcome, Optional[KernelOutcome], Optional[KernelOutcome]]:
"""Original roundtrip test entry → partial/monitor → flatten."""
kernel = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(kernel, client)
price = float(snap.price)
await bundle.runtime.connect(initial_capital=25000.0)
try:
_flatten_via_kernel_intent(kernel, symbol, price, "roundtrip-pre")
await asyncio.sleep(0.3)
tid = f"rt-{int(time.time() * 1000)}"
entry = _submit_intent_direct(kernel, KernelCommandType.ENTER, tid, symbol, "SHORT", price, 0.001)
await asyncio.sleep(1.0)
monitor = None
if not kernel.slot(0).is_free():
_submit_intent_direct(kernel, KernelCommandType.CANCEL, tid, symbol, "SHORT", price, 0.001)
await asyncio.sleep(0.3)
flatt = None
if not kernel.slot(0).is_free():
flatt = _submit_intent_direct(kernel, KernelCommandType.EXIT, tid, symbol, "SHORT", price * 0.995, 0.001)
await asyncio.sleep(1.0)
if not kernel.slot(0).is_free():
_flatten_via_kernel_intent(kernel, symbol, price * 0.99, "roundtrip-post")
await asyncio.sleep(1.0)
return entry, monitor, flatt
finally:
await bundle.runtime.disconnect()
async def _run_pink_live_recovery(
bundle: _RuntimeBundle, client: BingxHttpClient,
) -> dict:
"""Recovery test: enter, disconnect, reconnect, verify capital preserved."""
kernel = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(kernel, client)
price = float(snap.price)
await bundle.runtime.connect(initial_capital=25000.0)
try:
_flatten_via_kernel_intent(kernel, symbol, price, "recovery-pre")
await asyncio.sleep(0.3)
_submit_intent_direct(kernel, KernelCommandType.ENTER, tid := f"r-{int(time.time() * 1000)}", symbol, "SHORT", price, 0.001)
await asyncio.sleep(1.0)
await bundle.runtime.disconnect()
await bundle.runtime.connect(initial_capital=25000.0)
await asyncio.sleep(1.0)
if not kernel.slot(0).is_free():
_flatten_via_kernel_intent(kernel, symbol, price * 0.99, "recovery-post")
await asyncio.sleep(1.0)
return {"capital": kernel.account.snapshot.capital, "peak": kernel.account.snapshot.peak_capital}
finally:
await bundle.runtime.disconnect()
''' # end PROLOGUE
# =====================================================================
# Scenario runner + shortcut
# =====================================================================
RUNNER = '''
# =====================================================================
# Generic runner & shortcut
# =====================================================================
async def _run_scenario(bundle, client, body_fn, label, initial_capital):
k = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(k, client)
await bundle.runtime.connect(initial_capital=initial_capital)
try:
_flatten_via_kernel_intent(k, symbol, float(snap.price), f"{label}-pre")
await asyncio.sleep(0.3)
_cap_before = k.account.snapshot.capital
await body_fn(bundle, client, symbol, snap)
_cap_after = k.account.snapshot.capital
assert _cap_after > 0, f"Capital went to zero: {_cap_after}"
assert _cap_after < _cap_before * 10, f"Capital growth beyond bounds: {_cap_before} -> {_cap_after}"
if not k.slot(0).is_free():
_flatten_via_kernel_intent(k, symbol, float(snap.price) * 0.99, f"{label}-post")
await asyncio.sleep(1.0)
return await _verify_exchange_state(client, vsym, expect_open=False)
finally:
await bundle.runtime.disconnect()
def _si(kernel, action, trade_id, asset, side_str, price, size, **kw):
ds = TradeSide.SHORT if side_str.upper() == "SHORT" else TradeSide.LONG
return kernel.process_intent(KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=trade_id, trade_id=trade_id, slot_id=0, asset=asset,
side=ds, action=action, reference_price=price, target_size=size,
leverage=kw.pop("leverage", 1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios", (1.0,)),
reason=kw.pop("reason", f"auto_{action.value.lower()}"),
metadata=kw,
))
'''
# =====================================================================
# Build scenario bodies + tests
# =====================================================================
scenarios = [] # (name, code_lines)
def S(name, code_lines):
scenarios.append((name, list(code_lines)))
# --- Original 9 ---
S("simple_entry_exit", [
'tid = f"s-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("multi_leg_exit", [
'tid = f"ml-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
])
S("cancel_entry_order", [
'tid = f"ce-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("entry_hold_exit", [
'tid = f"h-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(3)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("entry_exit_at_loss", [
'tid = f"l-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*1.005, 0.001); await asyncio.sleep(1)',
])
S("two_sequential_cycles", [
'p = float(snap.price)',
't1 = f"2c1-{int(time.time()*1000)}"; t2 = f"2c2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t2, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(1)',
])
S("entry_then_recover", [
'tid = f"r-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'await bundle.runtime.disconnect()',
'await bundle.runtime.connect(initial_capital=k.account.snapshot.capital)',
'await asyncio.sleep(1)',
])
S("long_entry_exit", [
'tid = f"ln-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "LONG", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "LONG", p*1.005, 0.001); await asyncio.sleep(1)',
])
# --- Cancel combos ---
S("cancel_idempotent", [
'tid = f"ci-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("double_cancel", [
'tid = f"dc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("cancel_then_exit", [
'tid = f"ctx-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("exit_then_cancel_exit", [
'tid = f"exc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("exit_then_reentry", [
'p = float(snap.price)',
't1 = f"er1-{int(time.time()*1000)}"; t2 = f"er2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("limit_cancel", [
'tid = f"lc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p*0.9, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p*0.9, 0.001); await asyncio.sleep(1)',
])
# --- X4 expanded ---
S("x4_partial_hold_exit", [
'tid = f"ph-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.003',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.3, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.7, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
])
S("x4_three_leg", [
'tid = f"3l-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.004',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*0.5, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
])
S("x4_cancel_fill_partial", [
'tid = f"cfp-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.002); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.002); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, 0.001); await asyncio.sleep(1)',
])
S("x4_rapid_three", [
'p = float(snap.price)',
'for i in range(3):',
' tid = f"r3-{i}-{int(time.time()*1000)}"',
' _si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p*(1-i*0.005), 0.001); await asyncio.sleep(0.8)',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995*(1-i*0.005), 0.001); await asyncio.sleep(0.8)',
])
S("x4_diff_symbol", [
'tid = f"ds-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'sym2 = "BTCUSDT" if symbol != "BTCUSDT" else "ETHUSDT"',
'_si(k, KernelCommandType.EXIT, tid, sym2, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
])
S("x4_alternating", [
'p = float(snap.price)',
't1 = f"as1-{int(time.time()*1000)}"; t2 = f"as2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'sym2 = "BTCUSDT" if symbol != "BTCUSDT" else "ETHUSDT"',
'try:',
' url = "https://open-api-vst.bingx.com/openApi/swap/v2/quote/price?symbol=" + sym2.replace("USDT","-USDT")',
' p2 = float(json.loads(urllib.request.urlopen(url, timeout=5).read())["data"]["price"])',
'except: p2 = p',
'_si(k, KernelCommandType.ENTER, t2, sym2, "LONG", p2, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t2, sym2, "LONG", p2*1.005, 0.001); await asyncio.sleep(1)',
])
S("x4_multi_flatten", [
'tid = f"mf-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'for i in range(3):',
' if k.slot(0).is_free(): break',
' _flatten_via_kernel_intent(k, symbol, p*0.99, f"mf{i}"); await asyncio.sleep(0.5)',
])
S("x4_three_leg_25_50_25", [
'tid = f"x4a-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.004',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.5, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
])
S("x4_enter_exit_hold_twice", [
'p = float(snap.price)',
't1 = f"x4b1-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
't2 = f"x4b2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t2, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(0.5)',
't3 = f"x4b3-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t3, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t3, symbol, "SHORT", p*0.985, 0.001); await asyncio.sleep(0.5)',
])
S("x4_cancel_then_double_exit", [
'tid = f"x4c-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.002',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, sz); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
])
# --- 2 sides × 2 profit × 4 patterns = 16 ---
for side, side_str, ep in [("short","SHORT",0.995), ("long","LONG",1.005)]:
for prof, pname, xp_mult in [(True,"profit",ep), (False,"loss",1/ep)]:
for pat, pat_suffix, lines in [
("basic", "", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.8)',
]),
("partial", "_partial", [
'sz = 0.002',
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{ep}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
]),
("cancel", "_cancel", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.8)',
]),
("double_exit", "_double_exit", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}*0.995, 0.001); await asyncio.sleep(0.5)',
]),
]:
name = f"{pat}_{side}_{pname}"
S(name, [
f'tid = f"{pat[0]}{side[0]}{"p" if prof else "l"}-{{int(time.time()*1000)}}"; p = float(snap.price)',
*lines,
])
# --- Triple sequential × 4 ---
for i in range(4):
side = "SHORT"; ep = 0.995
S(f"triple_seq_{i}", [
'p = float(snap.price)',
'for j in range(3):',
f' tid = f"ts{i}-j-{{int(time.time()*1000)}}"',
f' _si(k, KernelCommandType.ENTER, tid, symbol, "{side}", p*(1-j*0.003), 0.001); await asyncio.sleep(0.7)',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side}", p*{ep}*(1-j*0.003), 0.001); await asyncio.sleep(0.7)',
])
for i in range(4):
side = "LONG"; ep = 1.005
S(f"triple_seq_long_{i}", [
'p = float(snap.price)',
'for j in range(3):',
f' tid = f"tsl{i}-j-{{int(time.time()*1000)}}"',
f' _si(k, KernelCommandType.ENTER, tid, symbol, "{side}", p*(1+j*0.003), 0.001); await asyncio.sleep(0.7)',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side}", p*{ep}*(1+j*0.003), 0.001); await asyncio.sleep(0.7)',
])
# --- Cancel+reenter × 4 ---
for i in range(4):
side = "SHORT"
S(f"cancel_reenter_{i}", [
'p = float(snap.price)',
f't1 = f"cr{i}a-{{int(time.time()*1000)}}"; t2 = f"cr{i}b-{{int(time.time()*1000)}}"',
f'_si(k, KernelCommandType.ENTER, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.ENTER, t2, symbol, "{side}", p*0.995, 0.001); await asyncio.sleep(0.8)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, t2, symbol, "{side}", p*0.99, 0.001); await asyncio.sleep(0.5)',
])
for i in range(4):
side = "LONG"
S(f"cancel_reenter_long_{i}", [
'p = float(snap.price)',
f't1 = f"crl{i}a-{{int(time.time()*1000)}}"; t2 = f"crl{i}b-{{int(time.time()*1000)}}"',
f'_si(k, KernelCommandType.ENTER, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.ENTER, t2, symbol, "{side}", p*1.005, 0.001); await asyncio.sleep(0.8)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, t2, symbol, "{side}", p*1.01, 0.001); await asyncio.sleep(0.5)',
])
# --- Leg ratios × 8 ---
for i, ratios in enumerate([
(0.1,1.0), (0.33,0.33,1.0), (0.5,0.5,1.0), (0.75,1.0),
(0.2,0.3,0.5,1.0), (0.4,0.6,1.0), (0.15,0.85,1.0), (0.25,0.25,0.5,1.0),
]):
rat_str = ",".join(str(r) for r in ratios)
nlegs = len(ratios)
code = [
f'tid = f"lr{i}-{{int(time.time()*1000)}}"; p = float(snap.price); sz = 0.004',
f'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=({rat_str})); await asyncio.sleep(1)',
]
for leg in range(nlegs - 1):
r = ratios[leg]
code.append(f'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995*(1-{leg}*0.002), sz*{r}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)')
r_last = ratios[-1]
code.append(f'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*{r_last}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)')
S(f"leg_ratio_{i}", code)
# --- Breakeven × 4 ---
for i in range(4):
S(f"breakeven_{i}", [
f'tid = f"be{i}-{{int(time.time()*1000)}}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
])
# =====================================================================
# Assemble output
# =====================================================================
lines = [PROLOGUE, RUNNER]
lines.append('# =====================================================================')
lines.append('# Scenario body functions')
lines.append('# =====================================================================')
lines.append('')
lines.append('k = None # type: ignore # shorthand alias for bundle.runtime.kernel')
lines.append('')
for name, code_lines in scenarios:
lines.append(f'async def _body_{name}(bundle, client, symbol, snap):')
lines.append(' k = bundle.runtime.kernel')
for cl in code_lines:
lines.append(f' {cl}')
lines.append('')
lines.append('# =====================================================================')
lines.append('# Test functions')
lines.append('# =====================================================================')
lines.append('')
lines.append(
'@pytest.fixture(scope="session")\n'
'def _live_client():\n'
' cfg = _build_bingx_config(25000.0)\n'
' c = BingxHttpClient(cfg)\n'
' yield c\n'
)
for name, _ in scenarios:
lines.append(f'''
def test_pink_ditav2_{name}(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
ic = bundle.runtime.kernel.account.snapshot.capital
result = asyncio.run(_run_scenario(bundle, _live_client, _body_{name}, "{name}", ic))
assert result.positions_flat, f"{name}: {{result.error}}"
''')
lines.append('''
def test_pink_ditav2_open_partial_close_and_flatten(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
outcomes = asyncio.run(_run_pink_live_roundtrip(bundle, _live_client))
e, m, f = outcomes
assert e.accepted or e.diagnostic_code in {KernelDiagnosticCode.OK}, f"Entry not accepted: {e.diagnostic_code}"
slot = bundle.runtime.kernel.slot(0) if bundle.runtime.kernel.max_slots > 0 else None
if slot is not None and not slot.is_free():
pytest.skip(f"Slot not flat (fsm_state={slot.fsm_state})")
def test_pink_ditav2_reconciliation_only_on_explicit_recovery(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
recovered = asyncio.run(_run_pink_live_recovery(bundle, _live_client))
assert isinstance(recovered, dict), f"Expected dict, got {type(recovered)}"
assert recovered.get("capital", 0) > 0, "Expected positive capital after recovery"
''')
full = '\n'.join(lines)
try:
ast.parse(full)
test_count = full.count("def test_pink_ditav2_")
print(f"Syntax OK — {test_count} tests, {len(full)} chars")
with open(OUT, 'w') as f:
f.write(full)
print(f"Written to {OUT}")
print(f"Breakdown: {len(scenarios)} scenarios + 2 legacy = {test_count} total tests")
except SyntaxError as e:
print(f"Syntax error line {e.lineno}: {e.msg}")
fl = full.split('\n')
for i in range(max(0,e.lineno-5), min(len(fl), e.lineno+3)):
print(f" {i+1}: {fl[i]}")

View File

@@ -1,67 +0,0 @@
from __future__ import annotations
import json
from typing import Any, Protocol
from .contracts import KernelTransition, TradeSlot
from .control import KernelControlSnapshot
from .journal import _transition_row
from .projection import build_position_state_row
from .utils import json_safe
class HazelcastClientLike(Protocol):
def get_map(self, name: str): ...
def get_topic(self, name: str): ...
class HazelcastProjector:
"""Durable BLUE/PINK-compatible projection mirror."""
def __init__(
self,
client: HazelcastClientLike | None = None,
*,
active_slots_map: str = "dita_active_slots",
events_topic: str = "dita_trade_events",
) -> None:
self.client = client
self.active_slots_map = active_slots_map
self.events_topic = events_topic
def publish_slot(self, slot: TradeSlot) -> None:
if self.client is None:
return
self.client.get_map(self.active_slots_map).put(slot.trade_id, build_position_state_row(slot))
def publish_event(self, event_type: str, payload: dict[str, Any]) -> None:
if self.client is None:
return
topic = self.client.get_topic(self.events_topic)
topic.publish(
json.dumps(
{"event_type": event_type, "payload": json_safe(payload)},
ensure_ascii=False,
sort_keys=True,
default=str,
)
)
class HazelcastRowWriter:
"""Callback bridge for ``HazelcastProjection`` writer hooks."""
def __init__(self, client: HazelcastClientLike) -> None:
self.client = client
def __call__(self, name: str, row: dict[str, Any]) -> None:
if name.endswith("trade_events"):
self.client.get_topic(name).publish(
json.dumps(row, ensure_ascii=False, sort_keys=True, default=str)
)
return
if name.endswith("control"):
key = "control"
else:
key = str(row.get("trade_id", row.get("slot_id", row.get("event_id", ""))))
self.client.get_map(name).put(key, json_safe(row))

View File

@@ -1,102 +0,0 @@
"""Debug journaling surfaces for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Callable, Dict, List, Optional, Protocol
from .contracts import KernelTransition, TradeSlot, TradeStage, VenueEvent
from .control import KernelControlSnapshot
from .utils import json_safe, json_text
JournalSink = Callable[[str, Dict[str, Any]], None]
class KernelJournal(Protocol):
"""Append-only debug journal interface."""
def record(self, row: Dict[str, Any]) -> None:
...
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
...
@dataclass
class MemoryKernelJournal:
"""In-memory journal used in tests."""
rows: List[Dict[str, Any]] = field(default_factory=list)
capture_limit: int = 10_000
def record(self, row: Dict[str, Any]) -> None:
if len(self.rows) < self.capture_limit:
self.rows.append(dict(row))
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
row = _transition_row(transition=transition, slot=slot, event=event, control=control)
self.record(row)
class ClickHouseKernelJournal:
"""Fire-and-forget ClickHouse journal.
The sink is a small callable of the form ``sink(table_name, row_dict)``.
"""
def __init__(self, sink: Optional[JournalSink] = None):
self.sink = sink
def record(self, row: Dict[str, Any]) -> None:
if self.sink is not None:
self.sink("dita_kernel_debug", row)
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
self.record(_transition_row(transition=transition, slot=slot, event=event, control=control))
def _transition_row(
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent],
control: Optional[KernelControlSnapshot],
) -> Dict[str, Any]:
return {
"ts": transition.timestamp.isoformat() if hasattr(transition.timestamp, "isoformat") else str(transition.timestamp),
"trade_id": transition.trade_id,
"slot_id": transition.slot_id,
"prev_state": transition.prev_state.value,
"next_state": transition.next_state.value,
"trigger": transition.trigger,
"intent_id": transition.intent_id,
"event_id": transition.event_id,
"control_mode": transition.control_mode,
"control_verbosity": transition.control_verbosity,
"slot_state": slot.to_dict(),
"event_payload": json_safe(event) if event is not None else {},
"control_snapshot": control.as_dict() if control is not None else {},
"slot_state_json": json_text(slot.to_dict()),
}

View File

@@ -1,8 +0,0 @@
"""Compatibility shim for the Rust-backed DITAv2 execution kernel."""
from __future__ import annotations
from .rust_backend import ExecutionKernel
__all__ = ["ExecutionKernel"]

View File

@@ -1,350 +0,0 @@
"""Operator-facing bootstrap helpers for DITAv2.
This module keeps the wiring explicit:
- control plane selection
- Zinc plane selection
- projection sink selection
- venue adapter selection
The defaults stay safe and testable. Real shared-memory or live BingX wiring
is only enabled when the caller opts in via arguments or environment.
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
import asyncio
import inspect
import os
from pathlib import Path
from typing import Any, Optional
from dotenv import load_dotenv
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.config import BingxInstrumentProviderConfig
from prod.bingx.enums import BingxEnvironment
from .bingx_venue import BingxVenueAdapter
from .control import BackendMode
from .control import ControlPlane
from .control import ControlUpdate
from .control import KernelControlSnapshot
from .control import KernelMode
from .control import KernelVerbosity
from .control import build_control_plane
from .mock_venue import MockVenueAdapter
from .mock_venue import MockVenueScenario
from .projection import HazelcastProjection
from .projection import build_projection
from .real_control_plane import RealZincControlPlane
from .real_control_plane import RealZincUnavailable
from .real_zinc_plane import RealZincPlane
from .real_zinc_plane import RealZincUnavailable as RealZincPlaneUnavailable
from .rust_backend import ExecutionKernel
from .venue import VenueAdapter
from .zinc_plane import InMemoryZincPlane
from .zinc_plane import ZincPlane
PROJECT_ROOT = Path(__file__).resolve().parents[3]
load_dotenv(PROJECT_ROOT / ".env")
class LauncherVenueMode(str, Enum):
MOCK = "MOCK"
BINGX = "BINGX"
class LauncherZincMode(str, Enum):
IN_MEMORY = "IN_MEMORY"
REAL = "REAL"
@dataclass
class DITAv2LauncherBundle:
"""Concrete runtime components assembled by the launcher."""
kernel: ExecutionKernel
control_plane: ControlPlane
projection: HazelcastProjection
zinc_plane: ZincPlane
venue: VenueAdapter
def close(self) -> None:
_maybe_close(self.venue)
_maybe_close(self.zinc_plane)
_maybe_close(self.control_plane)
def _env_upper(name: str, default: str = "") -> str:
return str(os.environ.get(name, default)).strip().upper()
def _env_bool(name: str, default: bool = False) -> bool:
raw = os.environ.get(name)
if raw is None:
return default
return str(raw).strip().lower() in {"1", "true", "yes", "on"}
def _resolve_control_mode() -> KernelMode | None:
raw = _env_upper("DITA_V2_MODE", "")
if raw == KernelMode.DEBUG.value:
return KernelMode.DEBUG
if raw == KernelMode.NORMAL.value:
return KernelMode.NORMAL
return None
def _resolve_control_verbosity() -> KernelVerbosity | None:
raw = _env_upper("DITA_V2_VERBOSITY", "")
if raw == KernelVerbosity.TRACE.value:
return KernelVerbosity.TRACE
if raw == KernelVerbosity.VERBOSE.value:
return KernelVerbosity.VERBOSE
if raw == KernelVerbosity.QUIET.value:
return KernelVerbosity.QUIET
return None
def _resolve_backend_mode() -> BackendMode | None:
raw = _env_upper("DITA_V2_BACKEND_MODE", "")
if raw == BackendMode.BINGX.value:
return BackendMode.BINGX
if raw == BackendMode.MOCK.value:
return BackendMode.MOCK
return None
def _control_update_from_env() -> ControlUpdate | None:
fields: dict[str, Any] = {}
mode = _resolve_control_mode()
if mode is not None:
fields["mode"] = mode
verbosity = _resolve_control_verbosity()
if verbosity is not None:
fields["verbosity"] = verbosity
backend_mode = _resolve_backend_mode()
if backend_mode is not None:
fields["backend_mode"] = backend_mode
raw = os.environ.get("DITA_V2_DEBUG_CLICKHOUSE")
if raw is not None:
fields["debug_clickhouse_enabled"] = _env_bool("DITA_V2_DEBUG_CLICKHOUSE", True)
raw = os.environ.get("DITA_V2_TRACE_TRANSITIONS")
if raw is not None:
fields["trace_transitions"] = _env_bool("DITA_V2_TRACE_TRANSITIONS", False)
raw = os.environ.get("DITA_V2_MIRROR_TO_HAZELCAST")
if raw is not None:
fields["mirror_to_hazelcast"] = _env_bool("DITA_V2_MIRROR_TO_HAZELCAST", True)
raw = os.environ.get("DITA_V2_ACTIVE_SLOT_LIMIT")
if raw is not None:
try:
fields["active_slot_limit"] = max(1, int(str(raw).strip()))
except Exception:
pass
raw = os.environ.get("DITA_V2_RECONCILE_ON_RESTART")
if raw is not None:
fields["reconcile_on_restart"] = _env_bool("DITA_V2_RECONCILE_ON_RESTART", True)
return ControlUpdate(**fields) if fields else None
def _resolve_venue_mode(venue_mode: Optional[str] = None) -> LauncherVenueMode:
raw = _env_upper("DITA_V2_VENUE", venue_mode or LauncherVenueMode.MOCK.value)
if raw == LauncherVenueMode.BINGX.value:
return LauncherVenueMode.BINGX
return LauncherVenueMode.MOCK
def _resolve_zinc_mode(zinc_mode: Optional[str] = None) -> LauncherZincMode:
raw = _env_upper("DITA_V2_ZINC", zinc_mode or LauncherZincMode.IN_MEMORY.value)
if raw == LauncherZincMode.REAL.value:
return LauncherZincMode.REAL
return LauncherZincMode.IN_MEMORY
def _resolve_hazelcast_real(prefer_real_hazelcast: Optional[bool] = None) -> bool:
if prefer_real_hazelcast is not None:
return bool(prefer_real_hazelcast)
raw = _env_upper("DITA_V2_HAZELCAST", "")
return raw in {"REAL", "REAL_HZ", "HAZELCAST"}
def build_bingx_exec_client_config(
*,
environment: Optional[BingxEnvironment] = None,
allow_mainnet: Optional[bool] = None,
recv_window_ms: Optional[int] = None,
default_leverage: Optional[int] = None,
exchange_leverage_cap: Optional[int] = None,
prefer_websocket: Optional[bool] = None,
sizing_mode: Optional[str] = None,
) -> BingxExecClientConfig:
"""Build the direct BingX config used by the DITAv2 launcher."""
resolved_environment = environment or (
BingxEnvironment.LIVE if _env_upper("DOLPHIN_BINGX_ENV", "VST") == "LIVE" else BingxEnvironment.VST
)
resolved_allow_mainnet = _env_bool("DOLPHIN_BINGX_ALLOW_MAINNET", False) if allow_mainnet is None else bool(allow_mainnet)
resolved_recv_window = int(os.environ.get("DOLPHIN_BINGX_RECV_WINDOW_MS", "5000")) if recv_window_ms is None else int(recv_window_ms)
resolved_default_leverage = int(os.environ.get("DOLPHIN_BINGX_DEFAULT_LEVERAGE", "1")) if default_leverage is None else int(default_leverage)
resolved_exchange_cap = int(os.environ.get("DOLPHIN_BINGX_EXCHANGE_LEVERAGE_CAP", "3")) if exchange_leverage_cap is None else int(exchange_leverage_cap)
resolved_prefer_ws = _env_bool("DOLPHIN_BINGX_PREFER_WEBSOCKET", False) if prefer_websocket is None else bool(prefer_websocket)
resolved_sizing_mode = sizing_mode or os.environ.get("DOLPHIN_BINGX_SIZING_MODE", "testnet")
return BingxExecClientConfig(
api_key=os.environ.get("BINGX_API_KEY"),
secret_key=os.environ.get("BINGX_SECRET_KEY"),
environment=resolved_environment,
allow_mainnet=resolved_allow_mainnet,
recv_window_ms=max(1, resolved_recv_window),
default_leverage=max(1, resolved_default_leverage),
exchange_leverage_cap=max(1, resolved_exchange_cap),
prefer_websocket=resolved_prefer_ws,
sizing_mode=resolved_sizing_mode,
journal_strategy=os.environ.get("DOLPHIN_BINGX_JOURNAL_STRATEGY", "dita_v2"),
journal_db=os.environ.get("DOLPHIN_BINGX_JOURNAL_DB", "dolphin_pink"),
instrument_provider=BingxInstrumentProviderConfig(load_all=True),
)
def _build_control_plane(
*,
prefix: str,
control_plane: Optional[ControlPlane] = None,
) -> ControlPlane:
plane = control_plane or build_control_plane(prefix=prefix)
update = _control_update_from_env()
if update is not None:
plane.update(update)
return plane
def _build_zinc_plane(
*,
prefix: str,
slot_count: int,
zinc_mode: Optional[LauncherZincMode] = None,
zinc_plane: Optional[ZincPlane] = None,
) -> ZincPlane:
if zinc_plane is not None:
return zinc_plane
resolved_mode = zinc_mode or _resolve_zinc_mode()
if resolved_mode is LauncherZincMode.REAL:
try:
return RealZincPlane(prefix=prefix, slot_count=slot_count, create=True)
except (RealZincPlaneUnavailable, RealZincUnavailable, Exception):
pass
return InMemoryZincPlane()
def _build_venue(
*,
venue_mode: Optional[LauncherVenueMode] = None,
mock_scenario: Optional[MockVenueScenario] = None,
bingx_config: Optional[BingxExecClientConfig] = None,
bingx_backend: Optional[Any] = None,
venue: Optional[VenueAdapter] = None,
) -> VenueAdapter:
if venue is not None:
return venue
resolved_mode = venue_mode or _resolve_venue_mode()
if resolved_mode is LauncherVenueMode.BINGX:
backend = bingx_backend
if backend is None:
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
backend = BingxDirectExecutionAdapter(bingx_config or build_bingx_exec_client_config())
return BingxVenueAdapter(backend=backend)
return MockVenueAdapter(mock_scenario)
def _maybe_close(obj: Any) -> None:
for method_name in ("close", "disconnect"):
method = getattr(obj, method_name, None)
if method is None:
continue
try:
result = method()
except TypeError:
continue
if inspect.isawaitable(result):
try:
asyncio.run(result)
except RuntimeError:
pass
break
def build_launcher_bundle(
*,
max_slots: int = 10,
prefix: Optional[str] = None,
control_plane: Optional[ControlPlane] = None,
projection: Optional[HazelcastProjection] = None,
projection_client: Optional[Any] = None,
zinc_plane: Optional[ZincPlane] = None,
venue: Optional[VenueAdapter] = None,
venue_mode: Optional[LauncherVenueMode | str] = None,
zinc_mode: Optional[LauncherZincMode | str] = None,
bingx_config: Optional[BingxExecClientConfig] = None,
bingx_backend: Optional[Any] = None,
mock_scenario: Optional[MockVenueScenario] = None,
) -> DITAv2LauncherBundle:
"""Build a fully wired DITAv2 runtime bundle.
Defaults stay non-destructive:
- in-memory Zinc plane
- in-process control plane
- mock venue
- callback projection unless a Hazelcast client is supplied
"""
resolved_prefix = (prefix or os.environ.get("DITA_V2_PREFIX", "dita_v2")).strip() or "dita_v2"
if isinstance(venue_mode, LauncherVenueMode):
resolved_venue_mode = venue_mode
elif isinstance(venue_mode, str):
resolved_venue_mode = LauncherVenueMode(venue_mode.strip().upper())
else:
resolved_venue_mode = None
if isinstance(zinc_mode, LauncherZincMode):
resolved_zinc_mode = zinc_mode
elif isinstance(zinc_mode, str):
resolved_zinc_mode = LauncherZincMode(zinc_mode.strip().upper())
else:
resolved_zinc_mode = None
active_control_plane = _build_control_plane(prefix=resolved_prefix, control_plane=control_plane)
control_snapshot = active_control_plane.read()
active_projection = projection or build_projection(
client=projection_client,
prefer_real_hazelcast=_resolve_hazelcast_real(),
control_snapshot=control_snapshot,
)
active_zinc_plane = _build_zinc_plane(
prefix=resolved_prefix,
slot_count=int(max_slots),
zinc_mode=resolved_zinc_mode,
zinc_plane=zinc_plane,
)
active_venue = _build_venue(
venue_mode=resolved_venue_mode,
mock_scenario=mock_scenario,
bingx_config=bingx_config,
bingx_backend=bingx_backend,
venue=venue,
)
kernel = ExecutionKernel(
max_slots=int(max_slots),
control_plane=active_control_plane,
venue=active_venue,
projection=active_projection,
projection_client=projection_client,
zinc_plane=active_zinc_plane,
)
return DITAv2LauncherBundle(
kernel=kernel,
control_plane=active_control_plane,
projection=active_projection,
zinc_plane=active_zinc_plane,
venue=active_venue,
)

View File

@@ -1,203 +0,0 @@
"""Deterministic mock venue for DITAv2 tests."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional
import itertools
from .contracts import (
KernelCommandType,
KernelEventKind,
KernelIntent,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .venue import VenueAdapter
@dataclass(frozen=True)
class MockVenueScenario:
"""Failure knobs for the mock venue."""
reject_entries: bool = False
reject_exits: bool = False
partial_fill_ratio: float = 1.0
cancel_reject: bool = False
emit_ack_before_fill: bool = True
emit_fill_on_submit: bool = False
class MockVenueAdapter(VenueAdapter):
"""Scriptable mock venue with BingX-shaped response semantics."""
def __init__(self, scenario: Optional[MockVenueScenario] = None):
self.scenario = scenario or MockVenueScenario()
self._order_seq = itertools.count(1)
self._event_seq = itertools.count(1)
self._open_orders: Dict[str, VenueOrder] = {}
self._open_positions: Dict[str, Dict[str, Any]] = {}
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
is_entry = intent.action == KernelCommandType.ENTER
should_reject = self.scenario.reject_entries if is_entry else self.scenario.reject_exits
order_id = f"V-{next(self._order_seq):08d}"
client_id = f"{intent.trade_id}:{intent.intent_id}"
order = VenueOrder(
internal_trade_id=intent.trade_id,
venue_order_id=order_id,
venue_client_id=client_id,
side=intent.side,
intended_size=float(intent.target_size),
status=VenueOrderStatus.NEW,
metadata={"intent_id": intent.intent_id, "action": intent.action.value, "slot_id": intent.slot_id},
)
if should_reject:
order = VenueOrder(
internal_trade_id=order.internal_trade_id,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
intended_size=order.intended_size,
filled_size=0.0,
average_fill_price=0.0,
status=VenueOrderStatus.REJECTED,
metadata=dict(order.metadata),
)
return [self._event_from_order(intent, order, KernelEventKind.ORDER_REJECT, VenueEventStatus.REJECTED, reason="MOCK_REJECT")]
self._open_orders[order_id] = order
events: List[VenueEvent] = []
if self.scenario.emit_ack_before_fill or not self.scenario.emit_fill_on_submit:
events.append(self._event_from_order(intent, order, KernelEventKind.ORDER_ACK, VenueEventStatus.ACKED))
if self.scenario.emit_fill_on_submit or self.scenario.partial_fill_ratio > 0:
fill_ratio = max(0.0, min(1.0, float(self.scenario.partial_fill_ratio)))
fill_size = float(intent.target_size) * fill_ratio
event_kind = KernelEventKind.FULL_FILL if fill_ratio >= 1.0 else KernelEventKind.PARTIAL_FILL
event_status = VenueEventStatus.FILLED if fill_ratio >= 1.0 else VenueEventStatus.PARTIALLY_FILLED
fill_event = self._event_from_order(
intent,
order,
event_kind,
event_status,
price=float(intent.reference_price or 0.0),
fill_size=fill_size,
remaining_size=max(0.0, float(intent.target_size) - fill_size),
)
events.append(fill_event)
order = VenueOrder(
internal_trade_id=order.internal_trade_id,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
intended_size=order.intended_size,
filled_size=fill_size,
average_fill_price=float(intent.reference_price or 0.0),
status=VenueOrderStatus.FILLED if fill_ratio >= 1.0 else VenueOrderStatus.PARTIALLY_FILLED,
metadata=dict(order.metadata),
)
self._open_orders[order_id] = order
return events
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
if self.scenario.cancel_reject:
return [
self._event_from_order(
self._dummy_intent(order),
order,
KernelEventKind.CANCEL_REJECT,
VenueEventStatus.CANCELED_REJECTED,
reason=reason or "MOCK_CANCEL_REJECT",
)
]
existing = self._open_orders.get(order.venue_order_id, order)
canceled = VenueOrder(
internal_trade_id=existing.internal_trade_id,
venue_order_id=existing.venue_order_id,
venue_client_id=existing.venue_client_id,
side=existing.side,
intended_size=existing.intended_size,
filled_size=existing.filled_size,
average_fill_price=existing.average_fill_price,
status=VenueOrderStatus.CANCELED,
metadata=dict(existing.metadata),
)
self._open_orders.pop(order.venue_order_id, None)
return [
self._event_from_order(
self._dummy_intent(order),
canceled,
KernelEventKind.CANCEL_ACK,
VenueEventStatus.CANCELED,
reason=reason or "MOCK_CANCEL_ACK",
)
]
def open_orders(self) -> List[VenueOrder]:
return list(self._open_orders.values())
def open_positions(self) -> List[Dict[str, Any]]:
return list(self._open_positions.values())
def reconcile(self) -> List[VenueEvent]:
return []
def _dummy_intent(self, order: VenueOrder) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=order.venue_client_id,
trade_id=order.internal_trade_id,
slot_id=int(order.metadata.get("slot_id", 0)),
asset=str(order.metadata.get("asset", "")),
side=order.side,
action=KernelCommandType.EXIT if order.metadata.get("action") == "EXIT" else KernelCommandType.ENTER,
reference_price=float(order.metadata.get("reference_price", 0.0)),
target_size=float(order.intended_size),
leverage=float(order.metadata.get("leverage", 1.0)),
reason=str(order.metadata.get("reason", "")),
metadata=dict(order.metadata),
)
def _event_from_order(
self,
intent: KernelIntent,
order: VenueOrder,
kind: KernelEventKind,
status: VenueEventStatus,
*,
price: Optional[float] = None,
fill_size: float = 0.0,
remaining_size: float = 0.0,
reason: str = "",
) -> VenueEvent:
event = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"EV-{next(self._event_seq):08d}",
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=kind,
status=status,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=intent.asset,
price=float(price if price is not None else intent.reference_price or 0.0),
size=float(intent.target_size),
filled_size=float(fill_size),
remaining_size=float(remaining_size),
reason=reason,
raw_payload={
"status": status.value,
"orderId": order.venue_order_id,
"clientOrderId": order.venue_client_id,
"symbol": intent.asset,
"side": order.side.value,
"action": intent.action.value,
},
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
return event

View File

@@ -1,97 +0,0 @@
"""Hazelcast-compatible projection helpers for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime
import os
from typing import Any, Callable, Dict, Iterable, List, Optional
from .account import AccountProjection
from .contracts import KernelTransition, TradeSlot, TradeStage, VenueEvent
from .control import KernelControlSnapshot
from .journal import _transition_row
from .utils import json_safe
Writer = Callable[[str, Dict[str, Any]], None]
@dataclass
class HazelcastProjection:
"""Projection helper for BLUE/PINK-compatible durable writes."""
active_slots_map: str = "hz:dita_active_slots"
trade_events_topic: str = "hz:dita_trade_events"
control_map: str = "hz:dita_control"
writer: Optional[Writer] = None
control_snapshot: Optional[KernelControlSnapshot] = None
def write_slot(self, slot: TradeSlot) -> Dict[str, Any]:
row = build_position_state_row(slot, self.control_snapshot)
if self.writer is not None:
self.writer(self.active_slots_map, row)
return row
def write_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> Dict[str, Any]:
row = _transition_row(transition=transition, slot=slot, event=event, control=control)
if self.writer is not None:
self.writer(self.trade_events_topic, row)
return row
def write_control(self, control: KernelControlSnapshot) -> Dict[str, Any]:
self.control_snapshot = control
row = control.as_dict()
if self.writer is not None:
self.writer(self.control_map, row)
return row
def build_projection(
*,
writer: Optional[Writer] = None,
client: Optional[Any] = None,
prefer_real_hazelcast: Optional[bool] = None,
control_snapshot: Optional[KernelControlSnapshot] = None,
) -> HazelcastProjection:
"""Build the active projection helper with an operator-visible switch.
The default remains the callback-based projection helper. If a Hazelcast
client is supplied and the caller opts in via ``prefer_real_hazelcast`` or
``DITA_V2_HAZELCAST=REAL``, the helper routes directly through the
client-backed map/topic writer path.
"""
env_choice = os.environ.get("DITA_V2_HAZELCAST", "").strip().upper()
real_requested = prefer_real_hazelcast if prefer_real_hazelcast is not None else env_choice in {"REAL", "REAL_HZ", "HAZELCAST"}
if real_requested and client is not None:
try:
from .hazelcast_projection import HazelcastRowWriter
writer = HazelcastRowWriter(client)
except Exception:
pass
return HazelcastProjection(writer=writer, control_snapshot=control_snapshot)
def build_position_state_row(slot: TradeSlot, control: Optional[KernelControlSnapshot] = None) -> Dict[str, Any]:
"""Build a state row shaped for durable compatibility."""
row = slot.to_dict()
row.update(
{
"runtime_namespace": control.runtime_namespace if control else "dita_v2",
"strategy_namespace": control.strategy_namespace if control else "dita_v2",
"event_namespace": control.event_namespace if control else "dita_v2",
"actor_name": control.actor_name if control else "ExecutionKernel",
"exec_venue": control.exec_venue if control else "bingx",
"data_venue": control.data_venue if control else "binance",
"ledger_authority": control.ledger_authority if control else "exchange",
}
)
return row

View File

@@ -1,129 +0,0 @@
"""Real Zinc-backed control plane for DITAv2."""
from __future__ import annotations
import json
import struct
import sys
from pathlib import Path
from typing import Any, Dict, Optional
from .control import BackendMode, ControlPlane, ControlUpdate, KernelControlSnapshot, KernelMode, KernelVerbosity
_ZINC_ADAPTER_PATH = Path(__file__).resolve().parents[3] / "zinc" / "adapters" / "python"
if _ZINC_ADAPTER_PATH.exists() and str(_ZINC_ADAPTER_PATH) not in sys.path:
sys.path.insert(0, str(_ZINC_ADAPTER_PATH))
try: # pragma: no cover - exercised in integration tests
from zinc import SharedRegion
except Exception as exc: # pragma: no cover
SharedRegion = None # type: ignore[assignment]
_ZINC_IMPORT_ERROR = exc
else:
_ZINC_IMPORT_ERROR = None
class RealZincUnavailable(RuntimeError):
"""Raised when the Zinc Python adapter cannot be loaded."""
def require_real_zinc() -> None:
if SharedRegion is None:
raise RealZincUnavailable(str(_ZINC_IMPORT_ERROR))
def _json_default(value: Any) -> Any:
if hasattr(value, "value"):
return value.value
if hasattr(value, "isoformat"):
try:
return value.isoformat()
except Exception:
pass
if hasattr(value, "__dict__"):
return dict(vars(value))
raise TypeError(f"Unsupported value: {type(value)!r}")
def _encode_packet(seq: int, payload: Dict[str, Any]) -> bytes:
text = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=_json_default, separators=(",", ":")).encode("utf-8")
return struct.pack("!QQ", int(seq), len(text)) + text
def _decode_packet(buf: memoryview) -> Dict[str, Any]:
if len(buf) < 16:
return {}
seq, size = struct.unpack_from("!QQ", buf, 0)
if size <= 0 or size > len(buf) - 16:
return {}
payload = bytes(buf[16 : 16 + size]).decode("utf-8")
out = json.loads(payload)
if isinstance(out, dict):
out["_seq"] = seq
return out
class RealZincControlPlane(ControlPlane):
"""Shared-memory Zinc-backed control plane."""
def __init__(self, *, prefix: str, create: bool = True) -> None:
require_real_zinc()
base = prefix.strip("/").replace("/", "_")
self.region_name = f"{base}_control"
self._seq = 0
self._snapshot = KernelControlSnapshot()
if create:
self.region = SharedRegion.create(self.region_name, 1 << 20)
self._write_region(self._seq, self._snapshot.as_dict())
else:
self.region = SharedRegion.open(self.region_name)
payload = _decode_packet(self.region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if isinstance(control, dict):
self._snapshot = KernelControlSnapshot(**control)
def close(self) -> None:
self.region.close()
def read(self) -> KernelControlSnapshot:
payload = _decode_packet(self.region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if not isinstance(control, dict):
return self._snapshot
self._snapshot = KernelControlSnapshot(**control)
return self._snapshot
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
self._snapshot = update.apply(self.read())
self._seq += 1
self._write_region(self._seq, self._snapshot.as_dict())
return self._snapshot
def mirror(self) -> Dict[str, Any]:
return self._snapshot.as_dict()
def wait(self, timeout_ms: int = 1000) -> bool:
try:
return bool(self.region.wait(timeout_ms))
except Exception:
return False
def notify(self) -> None:
try:
self.region.notify()
except Exception:
pass
def _write_region(self, seq: int, control: Dict[str, Any]) -> None:
packet = _encode_packet(seq, {"control": control})
buf = self.region.as_buffer()
if len(packet) > len(buf):
raise ValueError(f"payload too large for Zinc control region: {len(packet)} > {len(buf)}")
view = memoryview(buf)
view[: len(packet)] = packet
if len(view) > len(packet):
view[len(packet) :] = b"\x00" * (len(view) - len(packet))
try:
self.region.notify()
except Exception:
pass

View File

@@ -1,263 +0,0 @@
"""Real Zinc-backed hot-path plane for DITAv2.
This wrapper uses the Zinc Python adapter directly. The kernel still talks to
the narrow ``ZincPlane`` interface; this module just makes that interface real.
"""
from __future__ import annotations
from dataclasses import asdict
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
import json
import os
import struct
import sys
import threading
from .contracts import KernelIntent, TradeSide, TradeSlot, TradeStage, VenueOrder, VenueOrderStatus
from .control import KernelControlSnapshot
_ZINC_ADAPTER_PATH = Path(__file__).resolve().parents[3] / "zinc" / "adapters" / "python"
if _ZINC_ADAPTER_PATH.exists() and str(_ZINC_ADAPTER_PATH) not in sys.path:
sys.path.insert(0, str(_ZINC_ADAPTER_PATH))
try: # pragma: no cover - exercised in integration tests
from zinc import SharedRegion
except Exception as exc: # pragma: no cover
SharedRegion = None # type: ignore[assignment]
_ZINC_IMPORT_ERROR = exc
else:
_ZINC_IMPORT_ERROR = None
class RealZincUnavailable(RuntimeError):
"""Raised when the Zinc Python adapter cannot be loaded."""
def require_real_zinc() -> None:
if SharedRegion is None:
raise RealZincUnavailable(str(_ZINC_IMPORT_ERROR))
def _json_default(value: Any) -> Any:
if hasattr(value, "value"):
return value.value
if hasattr(value, "isoformat"):
try:
return value.isoformat()
except Exception:
pass
if hasattr(value, "__dict__"):
return dict(vars(value))
raise TypeError(f"Unsupported value: {type(value)!r}")
def _slot_to_payload(slot: TradeSlot) -> Dict[str, Any]:
data = slot.to_dict()
return data
def _slot_from_payload(payload: Dict[str, Any]) -> TradeSlot:
active_entry_order = None
active_exit_order = None
if isinstance(payload.get("active_entry_order"), dict):
active_entry_order = VenueOrder(
internal_trade_id=str(payload.get("trade_id", "")),
venue_order_id=str(payload["active_entry_order"].get("venue_order_id", "")),
venue_client_id=str(payload["active_entry_order"].get("venue_client_id", "")),
side=TradeSide(str(payload["active_entry_order"].get("side", TradeSide.FLAT.value))),
intended_size=float(payload["active_entry_order"].get("intended_size", payload.get("size", 0.0))),
filled_size=float(payload["active_entry_order"].get("filled_size", 0.0)),
average_fill_price=float(payload["active_entry_order"].get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload["active_entry_order"].get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload["active_entry_order"].get("metadata", {})),
)
if isinstance(payload.get("active_exit_order"), dict):
active_exit_order = VenueOrder(
internal_trade_id=str(payload.get("trade_id", "")),
venue_order_id=str(payload["active_exit_order"].get("venue_order_id", "")),
venue_client_id=str(payload["active_exit_order"].get("venue_client_id", "")),
side=TradeSide(str(payload["active_exit_order"].get("side", TradeSide.FLAT.value))),
intended_size=float(payload["active_exit_order"].get("intended_size", payload.get("size", 0.0))),
filled_size=float(payload["active_exit_order"].get("filled_size", 0.0)),
average_fill_price=float(payload["active_exit_order"].get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload["active_exit_order"].get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload["active_exit_order"].get("metadata", {})),
)
slot = TradeSlot(
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
asset=str(payload.get("asset", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
entry_price=float(payload.get("entry_price", 0.0)),
size=float(payload.get("size", 0.0)),
initial_size=float(payload.get("initial_size", 0.0)),
leverage=float(payload.get("leverage", 0.0)),
entry_time=datetime.fromisoformat(payload["entry_time"]) if payload.get("entry_time") else None,
unrealized_pnl=float(payload.get("unrealized_pnl", 0.0)),
realized_pnl=float(payload.get("realized_pnl", 0.0)),
closed=bool(payload.get("closed", False)),
exit_leg_ratios=tuple(float(r) for r in payload.get("exit_leg_ratios", (1.0,))),
active_leg_index=int(payload.get("active_leg_index", 0)),
active_exit_order=active_exit_order,
active_entry_order=active_entry_order,
fsm_state=TradeStage(str(payload.get("fsm_state", TradeStage.IDLE.value))),
close_reason=str(payload.get("close_reason", "")),
last_event_time=datetime.fromisoformat(payload["last_event_time"]) if payload.get("last_event_time") else None,
seen_event_ids=tuple(str(event_id) for event_id in payload.get("seen_event_ids", ())),
metadata=dict(payload.get("metadata", {})),
)
return slot
def _encode_packet(seq: int, payload: Dict[str, Any]) -> bytes:
text = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=_json_default, separators=(",", ":")).encode("utf-8")
return struct.pack("!QQ", int(seq), len(text)) + text
def _decode_packet(buf: memoryview) -> Dict[str, Any]:
if len(buf) < 16:
return {}
seq, size = struct.unpack_from("!QQ", buf, 0)
if size <= 0 or size > len(buf) - 16:
return {}
payload = bytes(buf[16 : 16 + size]).decode("utf-8")
out = json.loads(payload)
if isinstance(out, dict):
out["_seq"] = seq
return out
class RealZincPlane:
"""Shared-memory Zinc plane used by the Python prototype."""
def __init__(
self,
*,
prefix: str,
slot_count: int = 10,
intent_capacity: int = 1 << 20,
state_capacity: int = 1 << 20,
control_capacity: int = 1 << 20,
create: bool = True,
) -> None:
require_real_zinc()
base = prefix.strip("/").replace("/", "_")
self.intent_name = f"{base}_intent"
self.state_name = f"{base}_state"
self.control_name = f"{base}_control"
self._intent_seq = 0
self._state_seq = 0
self._control_seq = 0
self._lock = threading.Lock()
self._slot_cache: Dict[int, TradeSlot] = {i: TradeSlot(slot_id=i) for i in range(int(slot_count))}
self._slot_count = int(slot_count)
self._intent_cache: List[Dict[str, Any]] = []
self._control_cache = KernelControlSnapshot()
if create:
self.intent_region = SharedRegion.create(self.intent_name, intent_capacity)
self.state_region = SharedRegion.create(self.state_name, state_capacity)
self.control_region = SharedRegion.create(self.control_name, control_capacity)
self._write_region(self.control_region, self._control_seq, {"control": self._control_cache.as_dict()})
self._write_region(
self.state_region,
self._state_seq,
{"slots": [self._slot_cache[key].to_dict() for key in range(self._slot_count)]},
)
self._write_region(self.intent_region, self._intent_seq, {"items": []})
else:
self.intent_region = SharedRegion.open(self.intent_name)
self.state_region = SharedRegion.open(self.state_name)
self.control_region = SharedRegion.open(self.control_name)
control_payload = _decode_packet(self.control_region.as_buffer())
state_payload = _decode_packet(self.state_region.as_buffer())
intent_payload = _decode_packet(self.intent_region.as_buffer())
if isinstance(control_payload.get("control"), dict):
self._control_cache = KernelControlSnapshot(**control_payload["control"])
if isinstance(state_payload.get("slots"), list):
for slot_payload in state_payload["slots"]:
if isinstance(slot_payload, dict):
slot = _slot_from_payload(slot_payload)
self._slot_cache[int(slot.slot_id)] = slot
if isinstance(intent_payload.get("items"), list):
self._intent_cache = list(intent_payload["items"])
def close(self) -> None:
self.intent_region.close()
self.state_region.close()
self.control_region.close()
def publish_intent(self, intent: KernelIntent) -> None:
with self._lock:
self._intent_seq += 1
row = intent.__dict__.copy()
row["timestamp"] = intent.timestamp.isoformat()
row["side"] = intent.side.value
row["action"] = intent.action.value
row["stage"] = intent.stage.value
row["exit_leg_ratios"] = list(intent.exit_leg_ratios)
row["metadata"] = json.loads(json.dumps(intent.metadata, default=_json_default))
self._intent_cache.append(row)
self._write_region(self.intent_region, self._intent_seq, {"items": self._intent_cache[-512:]})
def write_slot(self, slot: TradeSlot) -> None:
with self._lock:
self._state_seq += 1
self._slot_cache[int(slot.slot_id)] = slot
payload = {
"slots": [self._slot_cache[key].to_dict() for key in range(self._slot_count)],
}
self._write_region(self.state_region, self._state_seq, payload)
def read_slots(self) -> List[TradeSlot]:
payload = _decode_packet(self.state_region.as_buffer())
slots = payload.get("slots", []) if isinstance(payload, dict) else []
return [_slot_from_payload(slot) for slot in sorted(slots, key=lambda row: int(row.get("slot_id", 0)))]
def read_intents(self) -> List[Dict[str, Any]]:
payload = _decode_packet(self.intent_region.as_buffer())
items = payload.get("items", []) if isinstance(payload, dict) else []
return list(items)
def update_control(self, control: KernelControlSnapshot) -> None:
with self._lock:
self._control_seq += 1
self._control_cache = control
self._write_region(self.control_region, self._control_seq, {"control": control.as_dict()})
def read_control(self) -> KernelControlSnapshot:
payload = _decode_packet(self.control_region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if not isinstance(control, dict):
return self._control_cache
return KernelControlSnapshot(**control)
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
return bool(self.state_region.wait(timeout_ms))
def notify_state(self) -> None:
self.state_region.notify()
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
return bool(self.control_region.wait(timeout_ms))
def notify_control(self) -> None:
self.control_region.notify()
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
return bool(self.intent_region.wait(timeout_ms))
def notify_intent(self) -> None:
self.intent_region.notify()
def _write_region(self, region: Any, seq: int, payload: Dict[str, Any]) -> None:
packet = _encode_packet(seq, payload)
buf = region.as_buffer()
if len(packet) > len(buf):
raise ValueError(f"payload too large for Zinc region: {len(packet)} > {len(buf)}")
view = memoryview(buf)
view[:] = b"\x00" * len(view)
view[: len(packet)] = packet
region.notify()

View File

@@ -1,683 +0,0 @@
"""Rust-backed DITAv2 execution kernel.
This module keeps the Python API shape stable while moving the kernel state
machine into a Rust shared library. Slot views write through to the backend on
assignment, then the Python side mirrors the resulting state into Zinc and the
existing projections/journals.
"""
from __future__ import annotations
from dataclasses import asdict
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Sequence
import ctypes
import json
import os
import subprocess
import sys
from .account import AccountProjection
from .control import ControlPlane, ControlUpdate, KernelControlSnapshot, KernelVerbosity, build_control_plane
from .contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelOutcome,
KernelSeverity,
KernelTransition,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueOrder,
VenueOrderStatus,
VenueEventStatus,
)
from .journal import KernelJournal, MemoryKernelJournal
from .mock_venue import MockVenueAdapter
from .projection import HazelcastProjection
from .projection import build_projection
from .utils import json_safe
from .venue import VenueAdapter
from .zinc_plane import InMemoryZincPlane, ZincPlane
def _repo_root() -> Path:
return Path(__file__).resolve().parents[3]
def _crate_dir() -> Path:
return Path(__file__).resolve().with_name("_rust_kernel")
def _library_path() -> Path:
if sys.platform == "darwin":
name = "libdita_v2_kernel.dylib"
elif os.name == "nt":
name = "dita_v2_kernel.dll"
else:
name = "libdita_v2_kernel.so"
return _crate_dir() / "target" / "release" / name
def _build_library() -> None:
crate_dir = _crate_dir()
if not crate_dir.exists():
raise FileNotFoundError(f"Missing Rust kernel crate: {crate_dir}")
subprocess.run(
["cargo", "build", "--release", "--manifest-path", str(crate_dir / "Cargo.toml")],
cwd=_repo_root(),
check=True,
)
def _ensure_library() -> Path:
path = _library_path()
if not path.exists():
_build_library()
return path
class _RustKernelLib:
def __init__(self) -> None:
path = _ensure_library()
self.lib = ctypes.CDLL(str(path))
self.lib.dita_kernel_create.argtypes = [ctypes.c_size_t]
self.lib.dita_kernel_create.restype = ctypes.c_void_p
self.lib.dita_kernel_destroy.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_destroy.restype = None
self.lib.dita_kernel_free_string.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_free_string.restype = None
self.lib.dita_kernel_get_slot_json.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
self.lib.dita_kernel_get_slot_json.restype = ctypes.c_void_p
self.lib.dita_kernel_set_slot_json.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_char_p]
self.lib.dita_kernel_set_slot_json.restype = ctypes.c_int
self.lib.dita_kernel_process_intent_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_process_intent_json.restype = ctypes.c_void_p
self.lib.dita_kernel_on_venue_event_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_on_venue_event_json.restype = ctypes.c_void_p
self.lib.dita_kernel_reconcile_slots_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_reconcile_slots_json.restype = ctypes.c_void_p
self.lib.dita_kernel_snapshot_json.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_snapshot_json.restype = ctypes.c_void_p
def create(self, max_slots: int) -> ctypes.c_void_p:
handle = self.lib.dita_kernel_create(ctypes.c_size_t(max_slots))
if not handle:
raise RuntimeError("dita_kernel_create failed")
return ctypes.c_void_p(handle)
def destroy(self, handle: ctypes.c_void_p) -> None:
if handle and handle.value:
self.lib.dita_kernel_destroy(handle)
def _take_string(self, raw: ctypes.c_void_p) -> str:
if not raw:
raise RuntimeError("Rust kernel returned null string")
text = ctypes.cast(raw, ctypes.c_char_p).value
if text is None:
self.lib.dita_kernel_free_string(raw)
raise RuntimeError("Rust kernel returned empty string")
try:
return text.decode("utf-8")
finally:
self.lib.dita_kernel_free_string(raw)
def get_slot_json(self, handle: ctypes.c_void_p, slot_id: int) -> Dict[str, Any]:
raw = self.lib.dita_kernel_get_slot_json(handle, ctypes.c_size_t(slot_id))
if not raw:
raise IndexError(f"Invalid slot id: {slot_id}")
return json.loads(self._take_string(raw))
def set_slot_json(self, handle: ctypes.c_void_p, slot_id: int, payload: Dict[str, Any]) -> None:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
rc = self.lib.dita_kernel_set_slot_json(handle, ctypes.c_size_t(slot_id), ctypes.c_char_p(encoded))
if rc != 0:
raise RuntimeError(f"dita_kernel_set_slot_json failed rc={rc}")
def process_intent(
self,
handle: ctypes.c_void_p,
payload: Dict[str, Any],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_process_intent_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def on_venue_event(
self,
handle: ctypes.c_void_p,
payload: Dict[str, Any],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_on_venue_event_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def reconcile_slots(
self,
handle: ctypes.c_void_p,
payload: Sequence[Dict[str, Any]],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(list(payload)), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_reconcile_slots_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def snapshot(self, handle: ctypes.c_void_p) -> Dict[str, Any]:
raw = self.lib.dita_kernel_snapshot_json(handle)
return json.loads(self._take_string(raw))
_RUST: _RustKernelLib | None = None # lazy init — avoids Rust build on import
def _get_rust() -> _RustKernelLib:
global _RUST
if _RUST is None:
_RUST = _RustKernelLib()
return _RUST
def _slot_to_payload(slot: TradeSlot) -> Dict[str, Any]:
return slot.to_dict()
def _order_to_payload(order: Optional[VenueOrder]) -> Optional[Dict[str, Any]]:
if order is None:
return None
return {
"internal_trade_id": order.internal_trade_id,
"venue_order_id": order.venue_order_id,
"venue_client_id": order.venue_client_id,
"side": order.side.value,
"intended_size": float(order.intended_size or 0.0),
"filled_size": float(order.filled_size or 0.0),
"average_fill_price": float(order.average_fill_price or 0.0),
"status": order.status.value,
"metadata": dict(order.metadata),
}
def _order_from_payload(payload: Optional[Dict[str, Any]], *, trade_id: str) -> Optional[VenueOrder]:
if not isinstance(payload, dict):
return None
return VenueOrder(
internal_trade_id=trade_id,
venue_order_id=str(payload.get("venue_order_id", "")),
venue_client_id=str(payload.get("venue_client_id", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
intended_size=float(payload.get("intended_size", 0.0)),
filled_size=float(payload.get("filled_size", 0.0)),
average_fill_price=float(payload.get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload.get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload.get("metadata", {})),
)
def _slot_from_payload(payload: Dict[str, Any]) -> TradeSlot:
return TradeSlot(
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
asset=str(payload.get("asset", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
entry_price=float(payload.get("entry_price", 0.0)),
size=float(payload.get("size", 0.0)),
initial_size=float(payload.get("initial_size", 0.0)),
leverage=float(payload.get("leverage", 0.0)),
entry_time=datetime.fromisoformat(payload["entry_time"]) if payload.get("entry_time") else None,
unrealized_pnl=float(payload.get("unrealized_pnl", 0.0)),
realized_pnl=float(payload.get("realized_pnl", 0.0)),
closed=bool(payload.get("closed", False)),
exit_leg_ratios=tuple(float(r) for r in payload.get("exit_leg_ratios", (1.0,))),
active_leg_index=int(payload.get("active_leg_index", 0)),
active_exit_order=_order_from_payload(payload.get("active_exit_order"), trade_id=str(payload.get("trade_id", ""))),
active_entry_order=_order_from_payload(payload.get("active_entry_order"), trade_id=str(payload.get("trade_id", ""))),
fsm_state=TradeStage(str(payload.get("fsm_state", TradeStage.IDLE.value))),
close_reason=str(payload.get("close_reason", "")),
last_event_time=datetime.fromisoformat(payload["last_event_time"]) if payload.get("last_event_time") else None,
seen_event_ids=tuple(str(event_id) for event_id in payload.get("seen_event_ids", ())),
metadata=dict(payload.get("metadata", {})),
)
def _intent_to_payload(intent: KernelIntent) -> Dict[str, Any]:
return {
"timestamp": intent.timestamp.isoformat() if hasattr(intent.timestamp, "isoformat") else str(intent.timestamp),
"intent_id": intent.intent_id,
"trade_id": intent.trade_id,
"slot_id": intent.slot_id,
"asset": intent.asset,
"side": intent.side.value,
"action": intent.action.value,
"reference_price": float(intent.reference_price or 0.0),
"target_size": float(intent.target_size or 0.0),
"leverage": float(intent.leverage or 0.0),
"exit_leg_ratios": list(intent.exit_leg_ratios),
"reason": intent.reason,
"metadata": dict(intent.metadata),
"stage": intent.stage.value,
}
def _event_to_payload(event: VenueEvent) -> Dict[str, Any]:
return {
"timestamp": event.timestamp.isoformat() if hasattr(event.timestamp, "isoformat") else str(event.timestamp),
"event_id": event.event_id,
"trade_id": event.trade_id,
"slot_id": event.slot_id,
"kind": event.kind.value,
"status": event.status.value,
"venue_order_id": event.venue_order_id,
"venue_client_id": event.venue_client_id,
"side": event.side.value,
"asset": event.asset,
"price": float(event.price or 0.0),
"size": float(event.size or 0.0),
"filled_size": float(event.filled_size or 0.0),
"remaining_size": float(event.remaining_size or 0.0),
"reason": event.reason,
"raw_payload": dict(event.raw_payload),
"metadata": dict(event.metadata),
}
def _transition_from_payload(payload: Dict[str, Any]) -> KernelTransition:
return KernelTransition(
timestamp=datetime.fromisoformat(payload["timestamp"]),
trade_id=str(payload.get("trade_id", "")),
slot_id=int(payload.get("slot_id", 0)),
prev_state=TradeStage(str(payload.get("prev_state", TradeStage.IDLE.value))),
next_state=TradeStage(str(payload.get("next_state", TradeStage.IDLE.value))),
trigger=str(payload.get("trigger", "")),
intent_id=str(payload.get("intent_id", "")),
event_id=str(payload.get("event_id", "")),
control_mode=str(payload.get("control_mode", "")),
control_verbosity=str(payload.get("control_verbosity", "")),
details=dict(payload.get("details", {})),
)
def _outcome_from_payload(payload: Dict[str, Any]) -> KernelOutcome:
return KernelOutcome(
accepted=bool(payload.get("accepted", False)),
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
state=TradeStage(str(payload.get("state", TradeStage.IDLE.value))),
diagnostic_code=KernelDiagnosticCode(str(payload.get("diagnostic_code", KernelDiagnosticCode.OK.value))),
severity=KernelSeverity(str(payload.get("severity", KernelSeverity.INFO.value))),
transitions=tuple(_transition_from_payload(row) for row in payload.get("transitions", [])),
emitted_events=tuple(
VenueEvent(
timestamp=datetime.fromisoformat(row["timestamp"]),
event_id=str(row.get("event_id", "")),
trade_id=str(row.get("trade_id", "")),
slot_id=int(row.get("slot_id", 0)),
kind=KernelEventKind(str(row.get("kind", KernelEventKind.ORDER_ACK.value))),
status=VenueEventStatus(str(row.get("status", VenueEventStatus.ACKED.value))),
venue_order_id=str(row.get("venue_order_id", "")),
venue_client_id=str(row.get("venue_client_id", "")),
side=TradeSide(str(row.get("side", TradeSide.FLAT.value))),
asset=str(row.get("asset", "")),
price=float(row.get("price", 0.0)),
size=float(row.get("size", 0.0)),
filled_size=float(row.get("filled_size", 0.0)),
remaining_size=float(row.get("remaining_size", 0.0)),
reason=str(row.get("reason", "")),
raw_payload=dict(row.get("raw_payload", {})),
metadata=dict(row.get("metadata", {})),
)
for row in payload.get("emitted_events", [])
),
details=dict(payload.get("details", {})),
)
def _enum_text(value: Any) -> str:
if hasattr(value, "value"):
return str(getattr(value, "value"))
return str(value)
class KernelSlotView:
"""Write-through view over a Rust-backed slot."""
def __init__(self, kernel: "ExecutionKernel", slot_id: int) -> None:
object.__setattr__(self, "_kernel", kernel)
object.__setattr__(self, "_slot_id", int(slot_id))
@property
def slot_id(self) -> int:
return object.__getattribute__(self, "_slot_id")
def _snapshot(self) -> TradeSlot:
return self._kernel._get_slot(self.slot_id)
def __getattr__(self, name: str) -> Any:
slot = self._snapshot()
if hasattr(slot, name):
return getattr(slot, name)
raise AttributeError(name)
def __setattr__(self, name: str, value: Any) -> None:
if name in {"_kernel", "_slot_id"}:
object.__setattr__(self, name, value)
return
slot = self._snapshot()
if not hasattr(slot, name):
raise AttributeError(name)
setattr(slot, name, value)
self._kernel._set_slot(slot)
def to_dict(self) -> Dict[str, Any]:
return self._snapshot().to_dict()
def is_free(self) -> bool:
return self._snapshot().is_free()
def is_open(self) -> bool:
return self._snapshot().is_open()
def mark_price(self, price: float) -> None:
slot = self._snapshot()
slot.mark_price(price)
self._kernel._set_slot(slot)
def next_exit_ratio(self) -> float:
return self._snapshot().next_exit_ratio()
def consume_exit_leg(self) -> float:
slot = self._snapshot()
ratio = slot.consume_exit_leg()
self._kernel._set_slot(slot)
return ratio
def attach_entry_order(self, order: VenueOrder) -> None:
slot = self._snapshot()
slot.active_entry_order = order
self._kernel._set_slot(slot)
def attach_exit_order(self, order: VenueOrder) -> None:
slot = self._snapshot()
slot.active_exit_order = order
self._kernel._set_slot(slot)
def __repr__(self) -> str: # pragma: no cover - debugging helper
return f"KernelSlotView(slot_id={self.slot_id}, state={self._snapshot().fsm_state.value})"
class KernelStateView:
def __init__(self, kernel: "ExecutionKernel") -> None:
self._kernel = kernel
self.slots = [KernelSlotView(kernel, slot_id) for slot_id in range(kernel.max_slots)]
self.active_trade_index: Dict[str, int] = {}
self.venue_order_index: Dict[str, int] = {}
self.client_order_index: Dict[str, int] = {}
self.refresh()
def refresh(self) -> None:
snapshot = self._kernel._snapshot_backend()
self.active_trade_index = dict(snapshot.get("active_trade_index", {}))
self.venue_order_index = dict(snapshot.get("venue_order_index", {}))
self.client_order_index = dict(snapshot.get("client_order_index", {}))
class ExecutionKernel:
"""Rust-backed multi-slot execution kernel."""
def __init__(
self,
*,
max_slots: int = 10,
control_plane: Optional[ControlPlane] = None,
venue: Optional[VenueAdapter] = None,
journal: Optional[KernelJournal] = None,
account: Optional[AccountProjection] = None,
projection: Optional[HazelcastProjection] = None,
projection_client: Optional[Any] = None,
zinc_plane: Optional[ZincPlane] = None,
) -> None:
self.max_slots = int(max_slots)
self.control_plane = control_plane or build_control_plane()
self.venue = venue or MockVenueAdapter()
self.journal = journal or MemoryKernelJournal()
self.account = account or AccountProjection()
self.projection = projection or build_projection(client=projection_client)
self.zinc_plane = zinc_plane or InMemoryZincPlane()
self._backend = _get_rust().create(self.max_slots)
self._control_snapshot = self.control_plane.read()
self.projection.write_control(self._control_snapshot)
self.zinc_plane.update_control(self._control_snapshot)
self.state = KernelStateView(self)
self.account.observe_slots([self._get_slot(slot_id) for slot_id in range(self.max_slots)])
def __del__(self) -> None: # pragma: no cover - cleanup best effort
backend = getattr(self, "_backend", None)
if backend is not None:
try:
_get_rust().destroy(backend)
except Exception:
pass
@property
def control(self) -> KernelControlSnapshot:
return self.control_plane.read()
def update_control(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = self.control_plane.update(update)
self._control_snapshot = snapshot
self.projection.write_control(snapshot)
self.zinc_plane.update_control(snapshot)
return snapshot
def _snapshot_backend(self) -> Dict[str, Any]:
return _get_rust().snapshot(self._backend)
def _get_slot(self, slot_id: int) -> TradeSlot:
return _slot_from_payload(_get_rust().get_slot_json(self._backend, slot_id))
def _set_slot(self, slot: TradeSlot, *, journal: bool = False) -> None:
payload = _slot_to_payload(slot)
_get_rust().set_slot_json(self._backend, slot.slot_id, payload)
self.state.refresh()
slots = [self._get_slot(slot_id) for slot_id in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
def slot(self, slot_id: int) -> KernelSlotView:
if not (0 <= int(slot_id) < self.max_slots):
raise IndexError(slot_id)
return self.state.slots[int(slot_id)]
def free_slot(self) -> Optional[KernelSlotView]:
for slot in self.state.slots:
if slot.is_free():
return slot
return None
def _record_transitions(self, transitions: Iterable[KernelTransition], slot: TradeSlot, event: Optional[VenueEvent]) -> None:
if self.control.debug_clickhouse_enabled:
for transition in transitions:
self.journal.record_transition(
transition=transition,
slot=slot,
event=event,
control=self.control,
)
def process_intent(self, intent: KernelIntent) -> KernelOutcome:
self.zinc_plane.publish_intent(intent)
if not (0 <= int(intent.slot_id) < self.max_slots):
return KernelOutcome(
accepted=False,
slot_id=int(intent.slot_id),
trade_id=intent.trade_id,
state=TradeStage.IDLE,
diagnostic_code=KernelDiagnosticCode.INVALID_SLOT_ID,
details={"reason": "INVALID_SLOT_ID", "slot_id": int(intent.slot_id), "intent_id": intent.intent_id},
)
payload = _intent_to_payload(intent)
result = _get_rust().process_intent(
self._backend,
payload,
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
self.state.refresh()
emitted_events = []
if intent.action in {KernelCommandType.ENTER, KernelCommandType.EXIT}:
emitted_events = self.venue.submit(intent)
for event in emitted_events:
self.on_venue_event(event)
elif intent.action == KernelCommandType.CANCEL:
emitted_events = self.venue.cancel(self.slot(intent.slot_id).active_exit_order, reason=intent.reason) if self.slot(intent.slot_id).active_exit_order else []
for event in emitted_events:
self.on_venue_event(event)
final_slot = self._get_slot(outcome.slot_id)
rate_limit_event = next((event for event in emitted_events if event.kind == KernelEventKind.RATE_LIMITED), None)
if rate_limit_event is not None:
rate_limit_details = dict(outcome.details)
rate_limit_details.update(
{
"reason": rate_limit_event.reason or "RATE_LIMITED",
"retry_after_ms": int(rate_limit_event.metadata.get("retry_after_ms", 0) or 0),
"venue_event_kind": rate_limit_event.kind.value,
"severity": KernelSeverity.WARNING.value,
"release_eta": "few minutes",
"retryable": True,
}
)
outcome = KernelOutcome(
accepted=False,
slot_id=outcome.slot_id,
trade_id=outcome.trade_id,
state=final_slot.fsm_state,
diagnostic_code=KernelDiagnosticCode.RATE_LIMITED,
severity=KernelSeverity.WARNING,
transitions=outcome.transitions,
emitted_events=outcome.emitted_events,
details=rate_limit_details,
)
final_outcome = KernelOutcome(
accepted=outcome.accepted,
slot_id=outcome.slot_id,
trade_id=final_slot.trade_id,
state=final_slot.fsm_state,
diagnostic_code=outcome.diagnostic_code,
transitions=outcome.transitions,
emitted_events=tuple(emitted_events),
details=dict(outcome.details),
)
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(final_slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
self._record_transitions(outcome.transitions, final_slot, None)
return final_outcome
def on_venue_event(self, event: VenueEvent) -> KernelOutcome:
result = _get_rust().on_venue_event(
self._backend,
_event_to_payload(event),
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
slot = _slot_from_payload(result["slot"])
self.state.refresh()
# Single capital mutation point: settle realiized PnL when a fill
# transitions the slot to a terminal closed state. This is the *only*
# place post-startup where capital is changed — no external balance
# polls overwrite it.
if slot.fsm_state in {TradeStage.CLOSED, TradeStage.TRADE_TERMINAL_WRITTEN} and slot.realized_pnl != 0.0:
self.account.settle(slot.realized_pnl)
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
self._record_transitions(outcome.transitions, slot, event)
return outcome
def mark_price(self, asset: str, price: float) -> None:
for slot in self.state.slots:
if slot.asset == asset and slot.is_open():
slot.mark_price(price)
self.account.observe_slots([self._get_slot(i) for i in range(self.max_slots)])
def reconcile_from_slots(self, slots: Sequence[TradeSlot]) -> KernelOutcome:
payload = [_slot_to_payload(slot) for slot in slots]
result = _get_rust().reconcile_slots(
self._backend,
payload,
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
self.state.refresh()
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
for current in slots:
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
return outcome
def snapshot(self) -> Dict[str, Any]:
return {
"control": self.control.as_dict(),
"slots": [self._get_slot(slot.slot_id).to_dict() for slot in self.state.slots],
"account": {
"capital": self.account.snapshot.capital,
"equity": self.account.snapshot.equity,
"realized_pnl": self.account.snapshot.realized_pnl,
"unrealized_pnl": self.account.snapshot.unrealized_pnl,
"open_positions": self.account.snapshot.open_positions,
"open_notional": self.account.snapshot.open_notional,
"leverage": self.account.snapshot.leverage,
},
}

View File

@@ -1,14 +0,0 @@
[package]
name = "dita-v2-kernel"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
libc = "0.2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

File diff suppressed because it is too large Load Diff

View File

@@ -1,43 +0,0 @@
"""Utility helpers for the DITAv2 kernel."""
from __future__ import annotations
from dataclasses import asdict, is_dataclass
from datetime import datetime
from enum import Enum
from typing import Any
import json
import math
def safe_float(value: Any, default: float = 0.0) -> float:
"""Return a finite float or ``default``."""
try:
out = float(value)
except Exception:
return default
if not math.isfinite(out):
return default
return out
def json_safe(value: Any) -> Any:
"""Convert enums, dataclasses and datetimes to JSON-safe objects."""
if isinstance(value, Enum):
return value.value
if isinstance(value, datetime):
return value.isoformat()
if is_dataclass(value):
return json_safe(asdict(value))
if isinstance(value, dict):
return {str(key): json_safe(val) for key, val in value.items()}
if isinstance(value, list):
return [json_safe(item) for item in value]
if isinstance(value, tuple):
return [json_safe(item) for item in value]
return value
def json_text(value: Any) -> str:
"""Serialize a value using stable JSON settings."""
return json.dumps(json_safe(value), separators=(",", ":"), ensure_ascii=False, default=str)

View File

@@ -1,37 +0,0 @@
"""Venue adapter contracts for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List, Optional, Protocol
from .contracts import (
KernelCommandType,
KernelIntent,
KernelEventKind,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
class VenueAdapter(Protocol):
"""Abstract venue adapter used by the kernel."""
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
...
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
...
def open_orders(self) -> List[VenueOrder]:
...
def open_positions(self) -> List[Dict[str, Any]]:
...
def reconcile(self) -> List[VenueEvent]:
...

View File

@@ -1,135 +0,0 @@
"""Python prototype of the Zinc hot-path plane.
This is an in-memory stand-in for the eventual Zinc-backed shared memory
regions. The interface is explicit so the implementation can be swapped later
without touching the kernel logic.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Dict, Iterable, List, Mapping, Optional, Protocol
import threading
import time
from .contracts import KernelIntent, TradeSlot
from .control import KernelControlSnapshot
class ZincPlane(Protocol):
"""Hot-path plane for intents, state and control."""
def publish_intent(self, intent: KernelIntent) -> None:
...
def write_slot(self, slot: TradeSlot) -> None:
...
def read_slots(self) -> List[TradeSlot]:
...
def update_control(self, control: KernelControlSnapshot) -> None:
...
def read_control(self) -> KernelControlSnapshot:
...
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
...
def notify_intent(self) -> None:
...
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
...
def notify_state(self) -> None:
...
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
...
def notify_control(self) -> None:
...
@dataclass
class InMemoryZincPlane:
"""Simple in-memory Zinc lookalike for Python prototype tests."""
intent_region: List[KernelIntent] = field(default_factory=list)
state_region: Dict[int, TradeSlot] = field(default_factory=dict)
control_region: Optional[KernelControlSnapshot] = None
_intent_seq: int = field(default=0, init=False, repr=False)
_state_seq: int = field(default=0, init=False, repr=False)
_control_seq: int = field(default=0, init=False, repr=False)
_intent_observed_seq: int = field(default=0, init=False, repr=False)
_state_observed_seq: int = field(default=0, init=False, repr=False)
_control_observed_seq: int = field(default=0, init=False, repr=False)
_signal: threading.Condition = field(default_factory=threading.Condition, init=False, repr=False)
def publish_intent(self, intent: KernelIntent) -> None:
with self._signal:
self.intent_region.append(intent)
self._intent_seq += 1
self._signal.notify_all()
def write_slot(self, slot: TradeSlot) -> None:
with self._signal:
self.state_region[int(slot.slot_id)] = slot
self._state_seq += 1
self._signal.notify_all()
def read_slots(self) -> List[TradeSlot]:
return [self.state_region[key] for key in sorted(self.state_region)]
def update_control(self, control: KernelControlSnapshot) -> None:
with self._signal:
self.control_region = control
self._control_seq += 1
self._signal.notify_all()
def read_control(self) -> KernelControlSnapshot:
if self.control_region is None:
return KernelControlSnapshot()
return self.control_region
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
return self._wait_for_change("_intent_seq", "_intent_observed_seq", timeout_ms)
def notify_intent(self) -> None:
with self._signal:
self._intent_seq += 1
self._signal.notify_all()
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
return self._wait_for_change("_state_seq", "_state_observed_seq", timeout_ms)
def notify_state(self) -> None:
with self._signal:
self._state_seq += 1
self._signal.notify_all()
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
return self._wait_for_change("_control_seq", "_control_observed_seq", timeout_ms)
def notify_control(self) -> None:
with self._signal:
self._control_seq += 1
self._signal.notify_all()
def _wait_for_change(self, seq_attr: str, observed_attr: str, timeout_ms: int) -> bool:
timeout_s = None if timeout_ms is None or timeout_ms < 0 else max(0.0, timeout_ms / 1000.0)
deadline = None if timeout_s is None else time.monotonic() + timeout_s
with self._signal:
observed = getattr(self, observed_attr)
while getattr(self, seq_attr) == observed:
if deadline is None:
self._signal.wait()
continue
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
self._signal.wait(timeout=remaining)
setattr(self, observed_attr, getattr(self, seq_attr))
return True

View File

@@ -1,337 +0,0 @@
import sys, re
sys.path.insert(0, '/mnt/dolphinng5_predict')
fpath = '/mnt/dolphinng5_predict/prod/tests/test_pink_bingx_dita_live_e2e.py'
with open(fpath) as f:
content = f.read()
# ===== Collect all existing body names =====
existing_bodies = re.findall(r'async def _body_(\w+)', content)
seen = set()
unique_bodies = []
for b in existing_bodies:
if b not in seen:
seen.add(b)
unique_bodies.append(b)
print(f"Existing: {len(unique_bodies)} bodies")
# ===== New bodies =====
new_bodies = []
new_params = []
def B(name, lines):
new_bodies.append(f"async def _body_{name}(k, symbol, p):\n")
for l in lines:
new_bodies.append(f" {l}\n")
new_params.append(f' pytest.param("{name}", _body_{name}, id="{name}"),')
# ===== 1. Real reconcile: fresh kernel from old slot state =====
B("fresh_kernel_reconcile_entry", [
'tid = f"fk-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Snapshot slot state, build fresh kernel, reconcile",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# The fresh kernel should see the same slot state",
"s = k2.slot(0)",
'assert not s.is_free(), f"fresh kernel slot should not be free: {s.fsm_state}"',
"assert s.trade_id == tid, f\"trade_id mismatch: {s.trade_id} vs {tid}\"",
"# Exit on the fresh kernel",
"_si(k2, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"assert k2.slot(0).is_free(), \"fresh kernel slot not free after exit\"",
"# Original kernel capital should match",
'assert abs(k2.account.snapshot.capital - cb) < 0.01, f"capital drift: {k2.account.snapshot.capital} vs {cb}"',
])
B("fresh_kernel_reconcile_after_cancel", [
'tid = f"fkc-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
'r = _si(k, E.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
"# Reconcile onto fresh kernel from cancelled state",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# Cancelled slot should be free",
'assert k2.slot(0).is_free(), f"cancelled slot not free: {k2.slot(0).fsm_state}"',
])
B("fresh_kernel_reconcile_after_exit", [
'tid = f"fkx-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"# Reconcile onto fresh kernel from closed state",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
'assert k2.slot(0).is_free(), f"closed slot not free: {k2.slot(0).fsm_state}"',
'assert k2.slot(0).closed, "slot should be marked closed"',
])
B("fresh_kernel_reconcile_partial_exit", [
'tid = f"fkp-{int(__import__(\"time\").time()*1000)}"',
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"# Reconcile mid-trade (one leg exited, one remaining)",
"slot_data = k.slot(0).to_dict()",
"cb = k.account.snapshot.capital",
"fresh = _build_fresh_kernel_from_slot(slot_data, ic=cb)",
"k2 = fresh.runtime.kernel",
"# Remaining leg should still be open",
's = k2.slot(0)',
'assert not s.is_free(), f"partial-exit slot should not be free: {s.fsm_state}"',
'assert s.realized_pnl != 0 or s.size > 0, "partial-exit slot should have remaining position or realized PnL"',
"# Exit remaining leg on fresh kernel",
"_si(k2, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(1.0,)); await asyncio.sleep(0.5)",
'assert k2.slot(0).is_free(), "slot not free after final exit on fresh kernel"',
])
# ===== 2. Cross-slot portfolio accounting =====
B("cross_slot_portfolio_short_long", [
't0 = f"psl0-{int(__import__(\"time\").time()*1000)}"',
't1 = f"psl1-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital",
"_si(k, E.ENTER, t0, symbol, 'SHORT', p, 0.001, slot_id=0); await asyncio.sleep(0.4)",
"_si(k, E.ENTER, t1, symbol, 'LONG', p, 0.001, slot_id=1); await asyncio.sleep(0.4)",
"# Verify both slots are open",
'assert not k.slot(0).is_free(), "slot 0 should be open"',
'assert not k.slot(1).is_free(), "slot 1 should be open"',
"# Verify PnL tracking per slot",
"rp0 = k.slot(0).realized_pnl; up0 = k.slot(0).unrealized_pnl",
"rp1 = k.slot(1).realized_pnl; up1 = k.slot(1).unrealized_pnl",
"expected = cb + rp0 + up0 + rp1 + up1",
"actual = k.account.snapshot.capital",
'assert abs(actual - expected) < 0.01, f"portfolio misalignment: cap={actual} expected={expected} rp0={rp0} up0={up0} rp1={rp1} up1={up1}"',
"# Exit slot 0",
"_si(k, E.EXIT, t0, symbol, 'SHORT', p*0.995, 0.001, slot_id=0); await asyncio.sleep(0.4)",
"assert k.slot(0).is_free(), \"slot 0 should be free after exit\"",
"# Exit slot 1",
"_si(k, E.EXIT, t1, symbol, 'LONG', p*1.005, 0.001, slot_id=1); await asyncio.sleep(0.4)",
"assert k.slot(1).is_free(), \"slot 1 should be free after exit\"",
])
# ===== 3. KernelOutcome inspection =====
B("outcome_inspect_entry", [
'tid = f"oi-{int(__import__(\"time\").time()*1000)}"',
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Inspect outcome of ENTER",
"_assert_accepted(r, 'entry')",
"info = _inspect_outcome(r, 'entry')",
'assert r.accepted, f"entry not accepted: {info}"',
'assert r.trade_id == tid, f"trade_id mismatch: {r.trade_id} vs {tid}"',
'assert r.slot_id == 0, f"slot_id: {r.slot_id}"',
"# transitions should exist",
'assert len(info["transitions"]) > 0, f"no transitions in outcome: {info}"',
'assert info["diagnostic"] == "OK", f"diagnostic not OK: {info}"',
"# Exit and inspect",
'r2 = _si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
"_assert_accepted(r2, 'exit')",
'info2 = _inspect_outcome(r2, "exit")',
'assert len(info2["transitions"]) > 0, f"no exit transitions: {info2}"',
'assert info2["diagnostic"] == "OK", f"exit diagnostic: {info2}"',
])
B("outcome_inspect_rejection", [
'tid = f"or-{int(__import__(\"time\").time()*1000)}"',
'tid2 = f"or2-{int(__import__(\"time\").time()*1000)}"',
"r1 = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_accepted(r1, 'first entry')",
"# Second entry on same slot should be SLOT_BUSY",
"r2 = _si(k, E.ENTER, tid2, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_rejected(r2, 'SLOT_BUSY', 'double entry')",
"# Verify transition trace shows the rejection",
"info = _inspect_outcome(r2, 'double entry')",
'assert not r2.accepted, f"second entry should be rejected: {info}"',
"# Exit normally",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
B("outcome_inspect_exit_on_idle", [
'tid = f"oei-{int(__import__(\"time\").time()*1000)}"',
"# Exit on idle slot",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_rejected(r, 'INVALID_FSM_TRANSITION', 'exit on idle')",
'info = _inspect_outcome(r, "exit on idle")',
'assert not r.accepted, f"exit on idle should be rejected: {info}"',
"# Then do a normal trade",
'_si(k, E.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
])
# ===== 4. Duplicate event dedup =====
B("dedup_duplicate_fill_event", [
'tid = f"dd-{int(__import__(\"time\").time()*1000)}"',
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r, 'entry')",
"# Inject a duplicate FULL_FILL VenueEvent manually",
"# Build an event that mirrors the slot's current active order",
"sl = k.slot(0)",
'ao = sl.active_entry_order if sl.active_entry_order else sl.active_exit_order',
"if ao:",
" dup = VenueEvent(",
" timestamp=__import__('datetime').datetime.now(__import__('datetime').timezone.utc),",
' event_id="dedup-test-99999",',
' trade_id=tid, slot_id=0,',
' kind=KernelEventKind.FULL_FILL,',
' status=VenueEventStatus.FILLED,',
" venue_order_id=ao.venue_order_id,",
" venue_client_id=ao.venue_client_id,",
" side=sl.side,",
" asset=symbol,",
" price=p,",
" size=0.001, filled_size=0.001, remaining_size=0.0,",
' reason="dedup_test",',
" )",
" r2 = k.on_venue_event(dup)",
" _assert_accepted(r2, 'dedup_fill')",
' info = _inspect_outcome(r2, "dedup_fill")',
' assert len(info["event_kinds"]) == 0 or info["event_kinds"] == ["ORDER_ACK"], f"duplicate fill should produce no events: {info}"',
"# Exit",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
# ===== 5. Fill-price divergence =====
B("fill_price_divergence_1pct", [
'tid = f"fd-{int(__import__(\"time\").time()*1000)}"',
"# Enter SHORT at market",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"# Force the kernel's slot to see a divergent fill price via on_venue_event replay",
"sl = k.slot(0)",
'ao = sl.active_entry_order',
"if ao and sl.fsm_state not in ('IDLE', 'CLOSED'):",
" divergent_price = p * 1.01 # 1% worse than reference",
" div_event = VenueEvent(",
" timestamp=__import__('datetime').datetime.now(__import__('datetime').timezone.utc),",
' event_id="divergence-test",',
' trade_id=tid, slot_id=0,',
' kind=KernelEventKind.FULL_FILL,',
' status=VenueEventStatus.FILLED,',
" venue_order_id=ao.venue_order_id if ao else \"\"," ,
" venue_client_id=ao.venue_client_id if ao else \"\"," ,
" side=sl.side,",
" asset=symbol,",
" price=divergent_price,",
" size=0.001, filled_size=0.001, remaining_size=0.0,",
' reason="divergence_test",',
" )",
" k.on_venue_event(div_event); await asyncio.sleep(0.3)",
"# Exit at market",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
])
# ===== 6. Negative-capital boundary =====
B("neg_cap_entry_rejected", [
'tid = f"nc-{int(__import__(\"time\").time()*1000)}"',
"# Kernel should reject ENTER if capital cannot cover margin",
"# With tiny capital, even a tiny trade should be checked",
"k.account.snapshot.capital = 0.0",
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
'info = _inspect_outcome(r, "neg_cap")',
'# May be rejected or accepted depending on kernel margin logic',
'# At minimum, kernel should not crash',
"# Restore capital and do normal trade",
"k.account.snapshot.capital = 25000.0",
'_si(k, E.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, E.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
])
# ===== 7. Sub-sample cross-application =====
# Apply the new assertion patterns to a basic entry/exit
B("cross_sample_basic_entry_exit_outcome", [
'tid = f"cs-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r1 = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r1, 'cs_entry')",
"_check_slot_accounting(k, 'cs_after_entry')",
"r2 = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_assert_accepted(r2, 'cs_exit')",
"_check_slot_accounting(k, 'cs_after_exit')",
"ca = k.account.snapshot.capital",
"max_change = max(1.0, cb * 0.10)",
'assert cb - ca < max_change, f"cs: cap shrunk {cb} -> {ca}"',
])
B("cross_sample_cancel_reenter_outcome", [
't1 = f"csc-{int(__import__(\"time\").time()*1000)}"',
't2 = f"csc2-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r1 = _si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_assert_accepted(r1, 'cs_cancel_entry')",
"r2 = _si(k, E.CANCEL, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"if r2.accepted:",
' info = _inspect_outcome(r2, "cs_cancel")',
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_check_slot_accounting(k, 'cs_after_cancel')",
'assert k.slot(0).is_free(), "slot should be free after cancel"',
"r3 = _si(k, E.ENTER, t2, symbol, 'SHORT', p*0.997, 0.001); await asyncio.sleep(0.8)",
"_assert_accepted(r3, 'cs_reenter')",
"_check_slot_accounting(k, 'cs_after_reenter')",
"r4 = _si(k, E.EXIT, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_assert_accepted(r4, 'cs_reenter_exit')",
"_check_slot_accounting(k, 'cs_after_reenter_exit')",
])
B("cross_sample_multi_leg_outcome", [
'tid = f"csm-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
"_assert_accepted(r, 'cs_ml_entry')",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.4)",
"_assert_accepted(r, 'cs_ml_leg1')",
"_check_slot_accounting(k, 'cs_ml_after_leg1')",
"r = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.4)",
"_assert_accepted(r, 'cs_ml_leg2')",
"_check_slot_accounting(k, 'cs_ml_after_leg2')",
])
B("cross_sample_leverage_tight_bounds", [
'tid = f"csl-{int(__import__(\"time\").time()*1000)}"',
"cb = k.account.snapshot.capital; k._start_cap = cb",
"r_ent = _si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001, leverage=2); await asyncio.sleep(0.8)",
"_assert_accepted(r_ent, 'cs_lev_entry')",
"_check_slot_accounting(k, 'cs_lev_after_entry')",
"r_ex = _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, leverage=2); await asyncio.sleep(0.5)",
"_assert_accepted(r_ex, 'cs_lev_exit')",
"_check_slot_accounting(k, 'cs_lev_after_exit')",
"ca = k.account.snapshot.capital",
"max_change = max(1.0, cb * 0.10)",
'assert cb - ca < max_change, f"cs_lev: cap shrunk {cb} -> {ca}"',
])
# ===== BUILD =====
body_block = "".join(new_bodies)
param_block = "\n".join(new_params)
# Insert new bodies before SCENARIOS marker
marker = "SCENARIOS = ["
idx = content.index(marker)
# Insert after the last body section ends (blank line before SCENARIOS)
tail_start = content.rindex("\n\n", 0, idx) + 2
head = content[:tail_start]
tail = content[tail_start:]
with_bodies = head + body_block + tail
# Find SCENARIOS closing bracket and append new param entries
scenarios_open = with_bodies.index(marker)
close_bracket = with_bodies.index("]", scenarios_open)
final = with_bodies[:close_bracket] + "\n" + param_block + "\n" + with_bodies[close_bracket:]
# Compact blank lines
final = re.sub(r'\n{3,}', '\n\n', final)
with open(fpath, 'w') as f:
f.write(final)
import py_compile
py_compile.compile(fpath, doraise=True)
body_count = final.count("async def _body_")
param_count = final.count("pytest.param(")
print(f"Bodies: {body_count}, Params: {param_count}")
print("Parts 5: Compiles OK")

View File

@@ -1,170 +0,0 @@
import sys
sys.path.insert(0, '/mnt/dolphinng5_predict')
fpath = '/mnt/dolphinng5_predict/prod/tests/test_pink_bingx_dita_live_e2e.py'
with open(fpath) as f:
content = f.read()
# === PART 1: Expand imports ===
old_imports = """from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot"""
new_imports = """from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
VenueEvent, VenueEventStatus, KernelEventKind,
TradeStage, KernelDiagnosticCode, KernelSeverity,
KernelOutcome, KernelTransition, TradeSlot, VenueOrder,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot"""
content = content.replace(old_imports, new_imports)
print("1: imports OK")
# === PART 2: Expand _build_rb with helpers ===
old_build = "def _build_rb(ic: float = 25000.0, max_slots: int = 1) -> RB:\n cfg = _build_config(ic)\n b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=max_slots, bingx_config=cfg)\n k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic\n class Shim:\n def __init__(self, k): self.kernel = k\n async def connect(self, initial_capital=0): self.kernel.venue.connect()\n async def disconnect(self):\n try: self.kernel.venue.disconnect()\n except: pass\n return RB(runtime=Shim(k), config=cfg)"
new_build = """def _build_rb(ic: float = 25000.0, max_slots: int = 1) -> RB:
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=max_slots, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)
def _build_portfolio_rb(ic: float = 25000.0, max_slots: int = 2) -> RB:
return _build_rb(ic=ic, max_slots=max_slots)
def _inspect_outcome(r, label):
info = {
\"accepted\": r.accepted,
\"state\": r.state.value if r.state else \"\",
\"diagnostic\": r.diagnostic_code.value if r.diagnostic_code else \"\",
\"severity\": r.severity.value if r.severity else \"\",
\"transitions\": [(t.prev_state.value, t.next_state.value) for t in (r.transitions or ())],
\"event_kinds\": [e.kind.value for e in (r.emitted_events or ())],
\"details\": dict(r.details or {}),
}
return info
def _assert_accepted(r, label):
info = _inspect_outcome(r, label)
assert r.accepted, f\"{label}: intent rejected - diag={info['diagnostic']} state={info['state']} detail={info['details']}\"
def _assert_rejected(r, expected_diag, label):
info = _inspect_outcome(r, label)
assert not r.accepted, f\"{label}: expected rejection but got accepted state={info['state']}\"
assert info['diagnostic'] == expected_diag, f\"{label}: expected diag={expected_diag} got {info['diagnostic']} detail={info['details']}\"
def _check_slot_accounting(k, label):
start_cap = getattr(k, '_start_cap', None)
if start_cap is None:
return
total_rp = sum(k.slot(i).realized_pnl for i in range(k.max_slots))
total_up = sum(k.slot(i).unrealized_pnl for i in range(k.max_slots))
expected = start_cap + total_rp + total_up
actual = k.account.snapshot.capital
diff = abs(actual - expected)
assert diff < 0.01, f\"{label}: accounting mismatch cap={actual} exp={expected} rp={total_rp} upnl={total_up} diff={diff}\"
def _check_open_orders(c, vs):
r = __import__('asyncio').run(c._request_json(
\"GET\", \"/openApi/swap/v2/trade/openOrders\",
{\"symbol\": vs}, signed=True
))
data = r if isinstance(r, list) else (r.get(\"data\") or r.get(\"orders\") or [])
return [o for o in data if isinstance(o, dict)]
async def _verify_full(c, vs):
rs = await _contract_rows(c)
tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]
ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)
flat = ts < 1e-8
oos = _check_open_orders(c, vs)
no_orders = len(oos) == 0
err = \"\"
if not flat: err += f\"pos_open: {tr} \"
if not no_orders: err += f\"open_orders: {oos} \"
return {\"symbol\": vs, \"flat\": flat, \"no_orders\": no_orders, \"error\": err.strip()}
def _build_fresh_kernel_from_slot(slot_data, ic=25000.0):
from prod.clean_arch.dita_v2.rust_backend import _slot_from_payload
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode=\"BINGX\", max_slots=1, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
restored = _slot_from_payload(slot_data)
k.reconcile_from_slots([restored])
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)"""
content = content.replace(old_build, new_build)
print("2: build/helpers OK")
# === PART 3: Update _verify to check open orders ===
old_verify = "async def _verify(c, vs):\n rs = await _contract_rows(c)\n tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]\n ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)\n flat = ts < 1e-8\n return VR(symbol=vs, positions_flat=flat, error=\"\" if flat else f\"open: {tr}\")"
new_verify = "async def _verify(c, vs):\n rs = await _contract_rows(c)\n tr = [r for r in rs if str(r.get(\"symbol\",\"\")).upper().replace(\"-\",\"\") == vs.replace(\"-\",\"\").upper()]\n ts = sum(abs(float(r.get(\"positionAmt\",r.get(\"positionQty\",0)) or 0)) for r in tr)\n flat = ts < 1e-8\n oos = _check_open_orders(c, vs)\n no_orders = len(oos) == 0\n err = \"\"\n if not flat: err += f\"pos_open: {tr} \"\n if not no_orders: err += f\"open_orders: {oos} \"\n return VR(symbol=vs, positions_flat=flat and no_orders, error=err.strip())"
content = content.replace(old_verify, new_verify)
print("3: verify OK")
# === PART 4: Replace _run ===
# Find old _run and replace
old_run_pat = "async def _run(bundle, client, body_fn, label, ic):"
# Find the entire old run function bounds
idx = content.index(old_run_pat)
run_end = content.index(" finally:", idx)
run_end = content.index("\n\n", run_end) + 2
new_run = """async def _run(bundle, client, body_fn, label, ic):
k = bundle.runtime.kernel
sym = await _pick_sym(k, client)
snap, vsym = await _snap(client, sym)
await bundle.runtime.connect(initial_capital=ic)
p = float(snap.price)
try:
for si in range(k.max_slots):
if not k.slot(si).is_free():
_flatten(k, sym, p*0.99 if si == 0 else p*1.005, f"{label}-pre-{si}")
await asyncio.sleep(0.3)
k._start_cap = k.account.snapshot.capital
cb = k.account.snapshot.capital
await body_fn(k, sym, p)
ca = k.account.snapshot.capital
assert ca > 0, f"Capital zero: {ca}"
max_change = max(1.0, cb * 0.10)
assert cb - ca < max_change, f"Capital shrunk beyond tolerance: {cb} -> {ca} (limit={max_change})"
total_rp = sum(k.slot(i).realized_pnl for i in range(k.max_slots))
if abs(total_rp) > 0.0001:
assert abs(total_rp) < abs(cb - ca) + 0.01, f"{label}: rp={total_rp} != cap_change={cb-ca}"
for si in range(k.max_slots):
if not k.slot(si).is_free():
_flatten(k, sym, p*0.99 if si == 0 else p*1.005, f"{label}-post-{si}")
await asyncio.sleep(1.0)
_throttle(3.0)
return await _verify(client, vsym)
finally:
await bundle.runtime.disconnect()
"""
content = content[:idx] + new_run + content[run_end:]
print("4: run OK")
with open(fpath, 'w') as f:
f.write(content)
import py_compile
py_compile.compile(fpath, doraise=True)
print("Parts 1-4: Compiles OK")

File diff suppressed because it is too large Load Diff

View File

@@ -1 +0,0 @@
/target

View File

@@ -1,387 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "autocfg"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
[[package]]
name = "bumpalo"
version = "3.20.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649"
[[package]]
name = "cc"
version = "1.2.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1dce859f0832a7d088c4f1119888ab94ef4b5d6795d1ce05afb7fe159d79f98"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "chrono"
version = "0.4.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
dependencies = [
"iana-time-zone",
"js-sys",
"num-traits",
"serde",
"wasm-bindgen",
"windows-link",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "dita-v2-kernel"
version = "0.1.0"
dependencies = [
"chrono",
"libc",
"serde",
"serde_json",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]]
name = "futures-core"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
[[package]]
name = "futures-task"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393"
[[package]]
name = "futures-util"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
dependencies = [
"futures-core",
"futures-task",
"pin-project-lite",
"slab",
]
[[package]]
name = "iana-time-zone"
version = "0.1.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
[[package]]
name = "js-sys"
version = "0.3.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "142bc4740e452c1e57ade0cbc129f139c9093e354346f0872ef985f4f5cf5f11"
dependencies = [
"cfg-if",
"futures-util",
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.186"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
[[package]]
name = "log"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616ec5685824bcc94416c6d4a7a446eea774a31efd7062c8480ba6fd06d7a6e5"
[[package]]
name = "memchr"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
[[package]]
name = "proc-macro2"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rustversion"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
"serde_derive",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "slab"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"
[[package]]
name = "syn"
version = "2.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "wasm-bindgen"
version = "0.2.122"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed04576f974d2b2fba0f38c51dbc5518011e38c36bf1143164be765528fd409"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.122"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "916151b09da36bd82f6615cbf3a419e2f0ba23a03c6160e8e92eb6bd4aa1dec6"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.122"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "299047362ccbfce148b67ab7e73349f77748e00c8296f9542adfad2ad82c5c5e"
dependencies = [
"bumpalo",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.122"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a929b2c61f11ba3e9bc35b50c1f25cb38e0e892c0c231ae2b8cf78d5dad4437"
dependencies = [
"unicode-ident",
]
[[package]]
name = "windows-core"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-implement"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-result"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
dependencies = [
"windows-link",
]
[[package]]
name = "zmij"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"

View File

@@ -1,14 +0,0 @@
[package]
name = "dita-v2-kernel"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
libc = "0.2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

File diff suppressed because it is too large Load Diff

View File

@@ -4,8 +4,10 @@ from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, Iterable, Optional
from enum import Enum
from typing import Any, Dict, Iterable, List, Optional
import math
import time
from .contracts import TradeSide, TradeSlot, TradeStage
from .utils import safe_float
@@ -121,3 +123,387 @@ class AccountProjection:
"bars_held": int(bars_held),
"metadata": dict(metadata or {}),
}
# ---------------------------------------------------------------------------
# V2 — Dual-ledger, event-sourced, reconciled account (spec G2)
# ---------------------------------------------------------------------------
class ReconcileStatus(str, Enum):
OK = "OK"
WARN = "WARN"
ERROR = "ERROR"
@dataclass(frozen=True)
class KBlock:
"""Kernel-computed values — derived deterministically from the E-fact stream."""
capital: float = 0.0 # seed + Σrealized Σfee Σfunding
realized_pnl: float = 0.0
unrealized_pnl: float = 0.0
fees_paid: float = 0.0
funding_paid: float = 0.0
open_notional: float = 0.0 # Σ|qty|·mark
equity: float = 0.0 # capital + unrealized
used_margin: float = 0.0 # Σ notional/leverage
available_margin: float = 0.0 # capital used_margin
open_positions: int = 0
peak_capital: float = 0.0
@dataclass(frozen=True)
class EPosition:
"""Single open position as reported by the exchange."""
symbol: str = ""
qty: float = 0.0
entry_price: float = 0.0
mark_price: float = 0.0
unrealized_pnl: float = 0.0
leverage: float = 1.0
side: str = ""
@dataclass(frozen=True)
class EBlock:
"""Exchange facts — values only the exchange can know."""
wallet_balance: float = 0.0
available_margin: float = 0.0
used_margin: float = 0.0
maint_margin: float = 0.0
positions: tuple = () # tuple[EPosition, ...]
last_fill_price: float = 0.0
last_fill_qty: float = 0.0
last_fill_fee: float = 0.0
last_fill_realized_pnl: float = 0.0
last_funding: float = 0.0
@dataclass(frozen=True)
class ReconcileResult:
"""Classification of K-vs-E divergence for one snapshot."""
status: ReconcileStatus = ReconcileStatus.OK
deltas: Dict[str, float] = field(default_factory=dict)
explanations: List[str] = field(default_factory=list)
worst_field: str = ""
ts: float = 0.0
def __post_init__(self) -> None:
# frozen dataclass — use object.__setattr__ only in __post_init__
if not isinstance(self.deltas, dict):
object.__setattr__(self, "deltas", {})
if not isinstance(self.explanations, list):
object.__setattr__(self, "explanations", [])
@dataclass(frozen=True)
class AccountSnapshotV2:
"""
Immutable versioned snapshot — the atomic unit of account truth.
Each exchange event produces exactly one new snapshot; readers hold
a reference and are never exposed to a partially-updated state.
"""
event_seq: int
source_event_id: str
k: KBlock
e: EBlock
reconcile: ReconcileResult
ts: float = 0.0
@dataclass
class ReconcileConfig:
"""
Bounds for the R1R6 reconcile rules. All values are config-driven;
no magic numbers in the classifier itself.
"""
capital_epsilon: float = 1e-4 # |δ| < ε → OK (R1, absolute USDT)
pending_fee_bound: float = 20.0 # max unsettled fees still in-flight (R1)
realized_rounding: float = 0.05 # fee+rounding tolerance for R2
lot_step: float = 0.001 # position qty lot-step for R3
mark_staleness_factor: float = 0.003 # 0.3% mark-price drift tolerance (R4)
leverage_rounding_band: float = 2.0 # margin rounding band USDT (R5)
def _safe(v: Any, default: float = 0.0) -> float:
try:
f = float(v)
return f if math.isfinite(f) else default
except (TypeError, ValueError):
return default
class AccountProjectionV2:
"""
Dual-ledger account — tracks K-values (kernel fold) and E-facts
(exchange push) independently, reconciles each event, and publishes
immutable AccountSnapshotV2 instances.
Thread-safety note: Python's GIL makes reference replacement of
`_snapshot` atomic for single-field reads. For multi-field consistency
callers must hold `_snapshot` locally: `snap = proj.snapshot`.
"""
def __init__(
self,
seed_capital: float,
*,
min_capital: float = 0.0,
max_capital: Optional[float] = None,
reconcile_config: Optional[ReconcileConfig] = None,
) -> None:
self._seed = _safe(seed_capital, 0.0)
self._min_capital = min_capital
self._max_capital = max_capital
self._cfg = reconcile_config or ReconcileConfig()
# Running K-value accumulators
self._k_realized: float = 0.0
self._k_fees: float = 0.0
self._k_funding: float = 0.0
self._peak_capital: float = self._seed
# Latest E-facts (mutable intermediate; frozen into EBlock at snapshot time)
self._e_wallet_balance: float = 0.0
self._e_avail_margin: float = 0.0
self._e_used_margin: float = 0.0
self._e_maint_margin: float = 0.0
self._e_positions: List[EPosition] = []
self._e_last_fill_price: float = 0.0
self._e_last_fill_qty: float = 0.0
self._e_last_fill_fee: float = 0.0
self._e_last_fill_realized: float = 0.0
self._e_last_funding: float = 0.0
self._event_seq: int = 0
self._snapshot: AccountSnapshotV2 = self._build(0, "", [], time.time())
# ------------------------------------------------------------------
# E-fact ingestion (called from WS event handlers)
# ------------------------------------------------------------------
def apply_fill(
self,
*,
fill_price: float,
fill_qty: float,
fee: float,
realized_pnl: float,
) -> None:
self._k_realized += _safe(realized_pnl)
self._k_fees += _safe(fee)
self._e_last_fill_price = _safe(fill_price)
self._e_last_fill_qty = _safe(fill_qty)
self._e_last_fill_fee = _safe(fee)
self._e_last_fill_realized = _safe(realized_pnl)
def apply_funding(self, amount: float) -> None:
self._k_funding += _safe(amount)
self._e_last_funding = _safe(amount)
def apply_balance_update(
self,
*,
wallet_balance: float,
available_margin: float,
used_margin: float,
maint_margin: float,
) -> None:
self._e_wallet_balance = _safe(wallet_balance)
self._e_avail_margin = _safe(available_margin)
self._e_used_margin = _safe(used_margin)
self._e_maint_margin = _safe(maint_margin)
def apply_position_update(self, positions: List[EPosition]) -> None:
self._e_positions = list(positions)
# ------------------------------------------------------------------
# Snapshot construction (called after each ingestion step)
# ------------------------------------------------------------------
def build_snapshot(
self,
source_event_id: str,
slots: Iterable[TradeSlot],
ts: Optional[float] = None,
) -> AccountSnapshotV2:
self._event_seq += 1
snap = self._build(self._event_seq, source_event_id, list(slots), ts or time.time())
self._snapshot = snap
return snap
@property
def snapshot(self) -> AccountSnapshotV2:
return self._snapshot
@property
def k_capital(self) -> float:
raw = self._seed + self._k_realized - self._k_fees - self._k_funding
if self._max_capital is not None:
raw = min(raw, self._max_capital)
return max(self._min_capital, raw)
# ------------------------------------------------------------------
# Internal helpers
# ------------------------------------------------------------------
def _build(
self,
event_seq: int,
source_event_id: str,
slots: List[TradeSlot],
ts: float,
) -> AccountSnapshotV2:
open_notional, unrealized, used_margin, open_positions = self._scan_slots(slots)
capital = self.k_capital
self._peak_capital = max(self._peak_capital, capital)
k = KBlock(
capital=capital,
realized_pnl=self._k_realized,
unrealized_pnl=unrealized,
fees_paid=self._k_fees,
funding_paid=self._k_funding,
open_notional=open_notional,
equity=capital + unrealized,
used_margin=used_margin,
available_margin=max(0.0, capital - used_margin),
open_positions=open_positions,
peak_capital=self._peak_capital,
)
e = EBlock(
wallet_balance=self._e_wallet_balance,
available_margin=self._e_avail_margin,
used_margin=self._e_used_margin,
maint_margin=self._e_maint_margin,
positions=tuple(self._e_positions),
last_fill_price=self._e_last_fill_price,
last_fill_qty=self._e_last_fill_qty,
last_fill_fee=self._e_last_fill_fee,
last_fill_realized_pnl=self._e_last_fill_realized,
last_funding=self._e_last_funding,
)
reconcile = self._classify(k, e, ts)
return AccountSnapshotV2(
event_seq=event_seq,
source_event_id=source_event_id,
k=k,
e=e,
reconcile=reconcile,
ts=ts,
)
def _scan_slots(
self, slots: List[TradeSlot]
) -> tuple: # (open_notional, unrealized, used_margin, open_count)
open_notional = 0.0
unrealized = 0.0
used_margin = 0.0
open_positions = 0
for slot in slots:
if slot.closed or slot.size <= 0:
continue
if slot.fsm_state not in {
TradeStage.POSITION_OPEN,
TradeStage.POSITION_OPENED,
TradeStage.ENTRY_WORKING,
TradeStage.EXIT_WORKING,
}:
continue
open_positions += 1
mark = _safe(slot.metadata.get("mark_price") if slot.metadata else None, 0.0)
if mark <= 0.0:
mark = _safe(slot.entry_price, 0.0)
notional = abs(slot.size) * mark
open_notional += notional
unrealized += _safe(slot.unrealized_pnl)
lev = max(1.0, _safe(slot.metadata.get("leverage") if slot.metadata else None, 1.0))
used_margin += notional / lev
return open_notional, unrealized, used_margin, open_positions
def _classify(self, k: KBlock, e: EBlock, ts: float) -> ReconcileResult:
"""
Apply reconcile rules R1R6 (spec §2.3).
Returns a ReconcileResult with the worst status seen across all fields.
"""
cfg = self._cfg
status = ReconcileStatus.OK
deltas: Dict[str, float] = {}
explanations: List[str] = []
worst_field = ""
def _escalate(new: ReconcileStatus, field: str) -> None:
nonlocal status, worst_field
order = {ReconcileStatus.OK: 0, ReconcileStatus.WARN: 1, ReconcileStatus.ERROR: 2}
if order[new] > order[status]:
status = new
worst_field = field
# R1: capital vs wallet balance (only meaningful when E-facts are populated)
if e.wallet_balance > 0:
delta_r1 = abs(k.capital - e.wallet_balance)
deltas["capital_vs_wallet"] = k.capital - e.wallet_balance
if delta_r1 <= cfg.capital_epsilon:
pass # OK
elif delta_r1 <= cfg.pending_fee_bound:
_escalate(ReconcileStatus.WARN, "capital_vs_wallet")
explanations.append(f"UNSETTLED_FEE|capital_vs_wallet|delta={delta_r1:.4f}")
else:
_escalate(ReconcileStatus.ERROR, "capital_vs_wallet")
explanations.append(f"ERROR|capital_vs_wallet|delta={delta_r1:.4f}")
# R2: realized PnL vs exchange realized
if e.last_fill_realized_pnl != 0:
delta_r2 = abs(k.realized_pnl - e.last_fill_realized_pnl)
deltas["realized_pnl"] = k.realized_pnl - e.last_fill_realized_pnl
if delta_r2 <= cfg.capital_epsilon:
pass
elif delta_r2 <= cfg.realized_rounding:
_escalate(ReconcileStatus.WARN, "realized_pnl")
explanations.append(f"LOT_STEP_ROUNDING|realized_pnl|delta={delta_r2:.4f}")
else:
_escalate(ReconcileStatus.ERROR, "realized_pnl")
explanations.append(f"ERROR|realized_pnl|delta={delta_r2:.4f}")
# R3: position count (R6) + per-position qty (R3)
e_pos_map = {p.symbol: p for p in e.positions}
if len(e.positions) > 0:
if k.open_positions != len(e_pos_map):
deltas["open_positions"] = float(k.open_positions - len(e_pos_map))
_escalate(ReconcileStatus.ERROR, "open_positions")
explanations.append(
f"ERROR|open_positions|k={k.open_positions}|e={len(e_pos_map)}"
)
# R4: open_notional vs exchange notional (mark staleness)
if e.used_margin > 0 and k.open_notional > 0:
delta_notional = abs(k.open_notional - e.used_margin)
deltas["open_notional"] = k.open_notional - e.used_margin
staleness_band = k.open_notional * cfg.mark_staleness_factor
if delta_notional <= cfg.capital_epsilon:
pass
elif delta_notional <= staleness_band:
_escalate(ReconcileStatus.WARN, "open_notional")
explanations.append(f"MARK_PRICE_STALENESS|open_notional|delta={delta_notional:.4f}")
else:
_escalate(ReconcileStatus.ERROR, "open_notional")
explanations.append(f"ERROR|open_notional|delta={delta_notional:.4f}")
# R5: used/available margin
if e.used_margin > 0:
delta_margin = abs(k.used_margin - e.used_margin)
deltas["used_margin"] = k.used_margin - e.used_margin
if delta_margin <= cfg.capital_epsilon:
pass
elif delta_margin <= cfg.leverage_rounding_band:
_escalate(ReconcileStatus.WARN, "used_margin")
explanations.append(f"LEVERAGE_ROUNDING|used_margin|delta={delta_margin:.4f}")
else:
_escalate(ReconcileStatus.ERROR, "used_margin")
explanations.append(f"ERROR|used_margin|delta={delta_margin:.4f}")
return ReconcileResult(
status=status,
deltas=deltas,
explanations=explanations,
worst_field=worst_field,
ts=ts,
)

View File

@@ -1,602 +0,0 @@
"""DITAv2 BingX venue adapter.
This is a thin normalization layer over the existing direct BingX execution
surface. It converts BingX REST/account/order payloads into DITAv2
``VenueEvent`` / ``VenueOrder`` objects without reimplementing exchange logic.
"""
from __future__ import annotations
import asyncio
import concurrent.futures
import inspect
import itertools
import re
import threading
from datetime import datetime, timezone
from typing import Any, Iterable, List, Optional
from prod.clean_arch.dita import DecisionAction as LegacyDecisionAction
from prod.clean_arch.dita import Intent as LegacyIntent
from prod.clean_arch.dita import TradeSide as LegacyTradeSide
from prod.bingx.http import BingxHttpError
from .contracts import (
KernelCommandType,
KernelEventKind,
KernelIntent,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .utils import json_safe
from .utils import safe_float
from .venue import VenueAdapter
def _row_text(row: dict[str, Any], *keys: str, default: str = "") -> str:
for key in keys:
value = row.get(key)
if value is None:
continue
text = str(value)
if text:
return text
return default
def _row_float(row: dict[str, Any], *keys: str, default: float = 0.0) -> float:
for key in keys:
try:
value = float(row.get(key) or 0.0)
except Exception:
continue
if value == value and value not in (float("inf"), float("-inf")) and value != 0.0:
return value
return default
def _normalize_status(status: str) -> str:
return str(status or "").strip().upper()
def _trade_side_from_row(row: dict[str, Any], *, fallback: TradeSide = TradeSide.FLAT) -> TradeSide:
side_raw = _row_text(row, "side", "positionSide", default="").upper()
signed_qty = _row_float(row, "positionAmt", "positionQty", "positionSize", "quantity", "pa", default=0.0)
if side_raw in {"BUY", "LONG"}:
return TradeSide.LONG
if side_raw in {"SELL", "SHORT"}:
return TradeSide.SHORT
if signed_qty < 0:
return TradeSide.SHORT
if signed_qty > 0:
return TradeSide.LONG
return fallback
def _venue_event_status_from_row(status: str) -> VenueEventStatus:
normalized = _normalize_status(status)
if normalized in {"NEW", "ACKED", "PENDING", "CREATED"}:
return VenueEventStatus.ACKED
if normalized in {"RATE_LIMITED", "THROTTLED"}:
return VenueEventStatus.RATE_LIMITED
if normalized in {"PARTIALLY_FILLED", "PARTIAL_FILL"}:
return VenueEventStatus.PARTIALLY_FILLED
if normalized in {"FILLED", "FULL_FILL"}:
return VenueEventStatus.FILLED
if normalized in {"CANCELED", "CANCELLED", "EXPIRED"}:
return VenueEventStatus.CANCELED
if normalized in {"REJECTED", "FAILED"}:
return VenueEventStatus.REJECTED
if normalized in {"CANCEL_REJECTED", "CANCEL_REJECT"}:
return VenueEventStatus.CANCELED_REJECTED
return VenueEventStatus.ACKED
def _venue_order_status_from_row(status: str) -> VenueOrderStatus:
normalized = _normalize_status(status)
if normalized in {"NEW", "ACKED", "PENDING", "CREATED"}:
return VenueOrderStatus.NEW
if normalized in {"RATE_LIMITED", "THROTTLED"}:
return VenueOrderStatus.NEW
if normalized in {"PARTIALLY_FILLED", "PARTIAL_FILL"}:
return VenueOrderStatus.PARTIALLY_FILLED
if normalized in {"FILLED", "FULL_FILL"}:
return VenueOrderStatus.FILLED
if normalized in {"CANCELED", "CANCELLED", "EXPIRED"}:
return VenueOrderStatus.CANCELED
if normalized in {"REJECTED", "FAILED"}:
return VenueOrderStatus.REJECTED
return VenueOrderStatus.NEW
def _position_qty(row: dict[str, Any]) -> float:
qty = _row_float(row, "positionAmt", "positionQty", "positionSize", "quantity", "pa", default=0.0)
if qty != 0.0:
return abs(qty)
return abs(_row_float(row, "executedQty", "filledQty", "z", default=0.0))
def _position_price(row: dict[str, Any]) -> float:
return _row_float(row, "entryPrice", "avgPrice", "avgEntryPrice", "ep", "ap", "price", "lastFillPrice", "tradePrice")
def _mapping_for_snapshot(rows: Iterable[dict[str, Any]]) -> dict[str, dict[str, Any]]:
mapping: dict[str, dict[str, Any]] = {}
for row in rows:
client_id = _row_text(row, "clientOrderID", "clientOrderId", default="")
order_id = _row_text(row, "orderId", "orderID", "id", default="")
key = client_id or order_id
if key:
mapping[key] = dict(row)
if order_id and order_id not in mapping:
mapping[order_id] = dict(row)
return mapping
def _venue_order_from_row(
row: dict[str, Any],
*,
internal_trade_id: str = "",
fallback_side: TradeSide = TradeSide.FLAT,
) -> VenueOrder:
side = _trade_side_from_row(row, fallback=fallback_side)
client_id = _row_text(row, "clientOrderID", "clientOrderId", default="")
order_id = _row_text(row, "orderId", "orderID", "id", default="")
intended = _row_float(row, "origQty", "quantity", "q", "positionAmt", "positionQty", default=0.0)
if intended <= 0:
intended = _position_qty(row)
return VenueOrder(
internal_trade_id=internal_trade_id or client_id or order_id,
venue_order_id=order_id,
venue_client_id=client_id,
side=side,
intended_size=abs(float(intended or 0.0)),
filled_size=abs(_row_float(row, "executedQty", "filledQty", "z", "lastFilledQty", default=0.0)),
average_fill_price=_position_price(row),
status=_venue_order_status_from_row(_row_text(row, "status", "X", default="NEW")),
metadata={"raw": dict(row)},
)
def _event_id(seq: itertools.count) -> str:
return f"EV-{next(seq):08d}"
def _rate_limit_retry_after_ms(row: dict[str, Any]) -> int:
raw_retry = row.get("retryAfter") or row.get("retry_after_ms") or row.get("retryAfterMs")
if raw_retry is None:
msg = _row_text(row, "msg", "message", default="")
match = re.search(r"unblocked after (\d+)", msg)
if match:
try:
ts = int(match.group(1))
now_ms = int(datetime.now(timezone.utc).timestamp() * 1000)
return max(0, ts - now_ms)
except Exception:
return 0
return 0
try:
return max(0, int(float(raw_retry)))
except Exception:
return 0
class BingxVenueAdapter(VenueAdapter):
"""Normalizes BingX execution responses into DITAv2 venue events."""
# Shared thread-pool executor reused across all adapter instances and
# all calls. Threads are created once and recycled, eliminating the
# per-call creation/destruction overhead of the old pattern.
_EXECUTOR: concurrent.futures.ThreadPoolExecutor | None = None
_EXECUTOR_LOCK: threading.Lock = threading.Lock()
@classmethod
def _get_executor(cls) -> concurrent.futures.ThreadPoolExecutor:
if cls._EXECUTOR is None:
with cls._EXECUTOR_LOCK:
if cls._EXECUTOR is None:
# max_workers=3 so three concurrent HTTP calls (balance,
# positions, openOrders) can proceed simultaneously without
# serialising on the pool.
cls._EXECUTOR = concurrent.futures.ThreadPoolExecutor(
max_workers=3,
thread_name_prefix="bingx_adapter",
)
return cls._EXECUTOR
def __init__(self, backend: Any | None = None, *, config: Any | None = None) -> None:
if backend is None:
if config is None:
raise ValueError("BingxVenueAdapter requires a backend or config")
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
backend = BingxDirectExecutionAdapter(config)
self.backend = backend
self._event_seq = itertools.count(1)
# Thread-safe snapshot cache — reads from a snapshot may arrive from
# the kernel thread while _backend_snapshot writes from the pool thread.
self._snap_lock = threading.Lock()
self._last_snapshot = None
self._snapshot_ready = threading.Event()
self._snapshot_ready.set() # initially ready (no pending write)
def _run(self, result: Any) -> Any:
if inspect.isawaitable(result):
try:
asyncio.get_running_loop()
except RuntimeError:
return asyncio.run(result)
# Inside a running event loop: submit to the shared singleton
# executor so threads are reused across calls.
pool = self._get_executor()
return pool.submit(asyncio.run, result).result()
return result
def _call_backend(self, method_name: str, *args: Any, **kwargs: Any) -> Any:
method = getattr(self.backend, method_name, None)
if method is None:
raise AttributeError(f"backend has no method {method_name}")
return self._run(method(*args, **kwargs))
def _backend_snapshot(self, *, include_history: bool = False, timeout_ms: float = 5000.0):
"""Fetch a fresh snapshot from the backend and cache it thread-safely.
Design (industry best-practice reader-writer pattern):
- A caller that needs a fresh snapshot *waits* on ``_snapshot_ready``
before reading, so it never sees a stale partial write.
- While a snapshot fetch is in-flight, the lock is cleared; concurrent
callers block on ``_snapshot_ready`` with a timeout. If the fetch
succeeds in time they get the fresh snapshot; if it times out they
fall back to ``_last_snapshot`` (an eventually-consistent design —
stale data that *was* consistent is safer than no data).
- The write is guarded by ``_snap_lock`` so concurrent writes are
serialised and ``_last_snapshot`` is never partially assigned.
"""
if not self._snapshot_ready.wait(timeout=timeout_ms / 1000.0):
# Timeout waiting for a previous snapshot write — return the
# last-known-good snapshot rather than blocking the caller.
with self._snap_lock:
return self._last_snapshot
self._snapshot_ready.clear()
try:
snapshot = self._call_backend("refresh_state", None, include_history=include_history)
except Exception:
self._snapshot_ready.set()
raise
with self._snap_lock:
self._last_snapshot = snapshot
self._snapshot_ready.set()
return snapshot
@staticmethod
def _legacy_intent(intent: KernelIntent) -> LegacyIntent:
action = LegacyDecisionAction.ENTER if intent.action == KernelCommandType.ENTER else LegacyDecisionAction.EXIT
side = LegacyTradeSide.SHORT if intent.side == TradeSide.SHORT else LegacyTradeSide.LONG
metadata = dict(intent.metadata)
metadata["_order_type"] = getattr(intent, "order_type", "MARKET")
metadata["_limit_price"] = float(getattr(intent, "limit_price", 0.0) or 0.0)
return LegacyIntent(
timestamp=intent.timestamp,
trade_id=intent.trade_id,
decision_id=intent.intent_id,
asset=intent.asset,
action=action,
side=side,
reason=intent.reason,
target_size=float(intent.target_size),
leverage=float(intent.leverage),
reference_price=float(intent.reference_price),
confidence=1.0,
bars_held=0,
exit_leg_ratios=tuple(intent.exit_leg_ratios or (1.0,)),
metadata=metadata,
)
def connect(self) -> bool:
result = getattr(self.backend, "connect", None)
if result is not None:
self._run(result())
self._backend_snapshot(include_history=True)
return True
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
snapshot_before = self._backend_snapshot(include_history=True)
response = None
if hasattr(self.backend, "cancel_order"):
response = self._call_backend("cancel_order", order, reason=reason)
elif hasattr(self.backend, "cancel"):
response = self._call_backend("cancel", order, reason=reason)
else:
client = getattr(self.backend, "_client", None)
instrument_symbol = ""
if hasattr(self.backend, "_instrument_venue_symbol"):
asset = str(order.metadata.get("asset") or "")
if not asset:
slot_id = int(order.metadata.get("slot_id", 0) or 0)
if hasattr(self, "_kernel_ref") and self._kernel_ref is not None:
try:
asset = self._kernel_ref.slot(slot_id).asset
except Exception:
pass
if not asset:
asset = str(order.metadata.get("asset") or "")
instrument_symbol = str(self.backend._instrument_venue_symbol(asset)) if asset else ""
if client is None or not instrument_symbol:
raise RuntimeError("backend does not expose a cancel surface")
params = {"symbol": instrument_symbol}
if order.venue_order_id:
params["orderId"] = order.venue_order_id
else:
params["clientOrderId"] = order.venue_client_id
try:
response = self._run(client.signed_delete("/openApi/swap/v2/trade/order", params))
except BingxHttpError as exc:
response = {"status": "REJECTED", "msg": str(exc), "orderId": order.venue_order_id, "clientOrderId": order.venue_client_id}
snapshot_after = self._backend_snapshot(include_history=True)
return self._events_from_cancel(order, response, snapshot_before, snapshot_after, reason=reason)
def open_orders(self) -> List[VenueOrder]:
snapshot = self._backend_snapshot(include_history=False)
return [_venue_order_from_row(row) for row in (snapshot.open_orders or [])]
def open_positions(self) -> List[dict[str, Any]]:
snapshot = self._backend_snapshot(include_history=False)
return [dict(row) for row in (snapshot.open_positions or {}).values()]
def reconcile(self) -> List[VenueEvent]:
snapshot = self._backend_snapshot(include_history=True)
return self._events_from_snapshot(snapshot)
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
snapshot_before = self._backend_snapshot(include_history=True)
receipt = self._call_backend("submit_intent", self._legacy_intent(intent))
snapshot_after = self._backend_snapshot(include_history=True)
return self._events_from_submit(intent, receipt, snapshot_before, snapshot_after)
def _events_from_submit(self, intent: KernelIntent, receipt: Any, before, after) -> List[VenueEvent]: # noqa: ANN001
ack_row = dict(getattr(receipt, "raw_ack", {}) or {})
status = _normalize_status(getattr(receipt, "status", "") or _row_text(ack_row, "status", default="NEW"))
order_id = _row_text(ack_row, "orderId", "orderID", default=str(getattr(receipt, "order_id", "") or ""))
client_order_id = _row_text(ack_row, "clientOrderID", "clientOrderId", default=str(getattr(receipt, "client_order_id", "") or intent.intent_id))
if status in {"RATE_LIMITED", "THROTTLED"}:
return [
VenueEvent(
timestamp=getattr(receipt, "timestamp", datetime.now(timezone.utc)),
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=KernelEventKind.RATE_LIMITED,
status=VenueEventStatus.RATE_LIMITED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(getattr(receipt, "price", 0.0), 0.0),
size=float(intent.target_size or 0.0),
filled_size=0.0,
remaining_size=float(intent.target_size or 0.0),
reason=_row_text(ack_row, "msg", "message", default="BINGX_RATE_LIMITED"),
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value, "retry_after_ms": _rate_limit_retry_after_ms(ack_row)},
)
]
base_event = VenueEvent(
timestamp=getattr(receipt, "timestamp", datetime.now(timezone.utc)),
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=KernelEventKind.ORDER_ACK,
status=VenueEventStatus.ACKED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(getattr(receipt, "price", 0.0), 0.0),
size=float(intent.target_size or 0.0),
filled_size=0.0,
remaining_size=float(intent.target_size or 0.0),
reason="",
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
if status in {"REJECTED", "FAILED"}:
return [
VenueEvent(
**{**base_event.__dict__, "event_id": _event_id(self._event_seq), "kind": KernelEventKind.ORDER_REJECT, "status": VenueEventStatus.REJECTED, "reason": _row_text(ack_row, "msg", "message", default="BINGX_ORDER_REJECTED")},
)
]
events = [base_event]
fill_status = _venue_event_status_from_row(status)
filled_size = _row_float(ack_row, "executedQty", "cumFilledQty", "filledQty", "lastFilledQty", default=0.0)
snapshot_fill_size = self._filled_size_from_snapshots(before, after, intent.asset)
if filled_size <= 0:
filled_size = snapshot_fill_size
emit_fill = fill_status in {VenueEventStatus.PARTIALLY_FILLED, VenueEventStatus.FILLED} or snapshot_fill_size > 0.0
if emit_fill:
if filled_size <= 0:
filled_size = float(intent.target_size or 0.0)
remaining_size = max(0.0, float(intent.target_size or 0.0) - float(filled_size))
fill_kind = KernelEventKind.FULL_FILL if fill_status == VenueEventStatus.FILLED or remaining_size <= 1e-12 else KernelEventKind.PARTIAL_FILL
events.append(
VenueEvent(
timestamp=base_event.timestamp,
event_id=_event_id(self._event_seq),
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=fill_kind,
status=VenueEventStatus.FILLED if fill_kind == KernelEventKind.FULL_FILL else VenueEventStatus.PARTIALLY_FILLED,
venue_order_id=order_id,
venue_client_id=client_order_id,
side=intent.side,
asset=intent.asset,
price=safe_float(_row_float(ack_row, "avgPrice", "ap", "price", "lastFillPrice", default=getattr(receipt, "price", 0.0)), 0.0),
size=float(intent.target_size or 0.0),
filled_size=float(filled_size),
remaining_size=float(remaining_size),
reason="",
raw_payload=ack_row or json_safe(receipt),
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
)
return events
def _events_from_cancel(self, order: VenueOrder, response: Any, before, after, *, reason: str = "") -> List[VenueEvent]: # noqa: ANN001
raw = response if isinstance(response, dict) else {}
status = _normalize_status(_row_text(raw, "status", default="CANCELED"))
if status in {"RATE_LIMITED", "THROTTLED"}:
return [
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=order.internal_trade_id or order.venue_client_id,
slot_id=int(order.metadata.get("slot_id", 0) or 0),
kind=KernelEventKind.RATE_LIMITED,
status=VenueEventStatus.RATE_LIMITED,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=str(order.metadata.get("asset") or ""),
price=safe_float(_row_float(raw, "avgPrice", "ap", "price", "lastFillPrice", default=order.average_fill_price), 0.0),
size=float(order.intended_size or 0.0),
filled_size=float(order.filled_size or 0.0),
remaining_size=float(order.remaining_size),
reason=reason or _row_text(raw, "msg", "message", default="BINGX_RATE_LIMITED"),
raw_payload=raw or {"orderId": order.venue_order_id, "clientOrderId": order.venue_client_id, "status": status or "RATE_LIMITED"},
metadata={**dict(order.metadata), "retry_after_ms": _rate_limit_retry_after_ms(raw)},
)
]
event_status = _venue_event_status_from_row(status)
kind = KernelEventKind.CANCEL_ACK if event_status == VenueEventStatus.CANCELED else KernelEventKind.CANCEL_REJECT
if event_status == VenueEventStatus.CANCELED_REJECTED:
kind = KernelEventKind.CANCEL_REJECT
return [
VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=order.internal_trade_id or order.venue_client_id,
slot_id=int(order.metadata.get("slot_id", 0) or 0),
kind=kind,
status=event_status,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=str(order.metadata.get("asset") or ""),
price=safe_float(_row_float(raw, "avgPrice", "ap", "price", "lastFillPrice", default=order.average_fill_price), 0.0),
size=float(order.intended_size or 0.0),
filled_size=float(order.filled_size or 0.0),
remaining_size=float(order.remaining_size),
reason=reason or _row_text(raw, "msg", "message", default="BINGX_CANCEL_ACK" if kind == KernelEventKind.CANCEL_ACK else "BINGX_CANCEL_REJECT"),
raw_payload=raw or {"orderId": order.venue_order_id, "clientOrderId": order.venue_client_id, "status": status or event_status.value},
metadata=dict(order.metadata),
)
]
def _events_from_snapshot(self, snapshot: Any) -> List[VenueEvent]: # noqa: ANN001
events: list[VenueEvent] = []
seen: set[tuple[str, str, str]] = set()
for row in getattr(snapshot, "open_orders", []) or []:
if not isinstance(row, dict):
continue
event = self._event_from_row(row, slot_id=0)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
for row in getattr(snapshot, "all_orders", []) or []:
if not isinstance(row, dict):
continue
event = self._event_from_row(row, slot_id=0)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
for row in getattr(snapshot, "all_fills", []) or []:
if not isinstance(row, dict):
continue
event = self._fill_event_from_row(row)
key = (event.venue_client_id, event.venue_order_id, event.kind.value)
if key not in seen:
seen.add(key)
events.append(event)
return events
def _event_from_row(self, row: dict[str, Any], *, slot_id: int) -> VenueEvent:
status = _normalize_status(_row_text(row, "status", "X", default="NEW"))
event_status = _venue_event_status_from_row(status)
kind = {
VenueEventStatus.ACKED: KernelEventKind.ORDER_ACK,
VenueEventStatus.PARTIALLY_FILLED: KernelEventKind.PARTIAL_FILL,
VenueEventStatus.FILLED: KernelEventKind.FULL_FILL,
VenueEventStatus.CANCELED: KernelEventKind.CANCEL_ACK,
VenueEventStatus.REJECTED: KernelEventKind.ORDER_REJECT,
VenueEventStatus.CANCELED_REJECTED: KernelEventKind.CANCEL_REJECT,
VenueEventStatus.RATE_LIMITED: KernelEventKind.RATE_LIMITED,
}.get(event_status, KernelEventKind.ORDER_ACK)
size = _row_float(row, "origQty", "quantity", "q", "positionAmt", default=0.0)
filled = _row_float(row, "executedQty", "cumFilledQty", "filledQty", "z", "lastFilledQty", default=0.0)
if filled <= 0.0 and kind in {KernelEventKind.PARTIAL_FILL, KernelEventKind.FULL_FILL}:
filled = size
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=_row_text(row, "tradeId", "trade_id", default=_row_text(row, "clientOrderId", "clientOrderID", default="")),
slot_id=slot_id,
kind=kind,
status=event_status,
venue_order_id=_row_text(row, "orderId", "orderID", "id", default=""),
venue_client_id=_row_text(row, "clientOrderID", "clientOrderId", "c", default=""),
side=_trade_side_from_row(row),
asset=_row_text(row, "symbol", default=""),
price=safe_float(_row_float(row, "avgPrice", "ap", "price", "lastFillPrice", default=0.0), 0.0),
size=abs(float(size or 0.0)),
filled_size=abs(float(filled or 0.0)),
remaining_size=max(0.0, abs(float(size or 0.0)) - abs(float(filled or 0.0))),
reason=_row_text(row, "msg", "message", default=""),
raw_payload=dict(row),
metadata={"source": "bingx"},
)
def _fill_event_from_row(self, row: dict[str, Any]) -> VenueEvent:
status = _normalize_status(_row_text(row, "status", "X", default="FILLED"))
event_status = _venue_event_status_from_row(status)
kind = KernelEventKind.FULL_FILL if event_status == VenueEventStatus.FILLED else KernelEventKind.PARTIAL_FILL
return VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=_event_id(self._event_seq),
trade_id=_row_text(row, "tradeId", "trade_id", default=_row_text(row, "clientOrderId", "clientOrderID", default="")),
slot_id=0,
kind=kind,
status=event_status,
venue_order_id=_row_text(row, "orderId", "orderID", "id", default=""),
venue_client_id=_row_text(row, "clientOrderID", "clientOrderId", "c", default=""),
side=_trade_side_from_row(row),
asset=_row_text(row, "symbol", default=""),
price=safe_float(_row_float(row, "lastFillPrice", "L", "price", "ap", default=0.0), 0.0),
size=abs(_row_float(row, "executedQty", "z", "lastFilledQty", default=0.0)),
filled_size=abs(_row_float(row, "lastFilledQty", "l", "z", default=0.0)),
remaining_size=max(0.0, abs(_row_float(row, "executedQty", "z", "lastFilledQty", default=0.0)) - abs(_row_float(row, "lastFilledQty", "l", "z", default=0.0))),
reason=_row_text(row, "msg", "message", default=""),
raw_payload=dict(row),
metadata={"source": "bingx"},
)
@staticmethod
def _filled_size_from_snapshots(before: Any, after: Any, asset: str) -> float: # noqa: ANN001
def _lookup(snapshot: Any) -> float:
positions = getattr(snapshot, "open_positions", {}) or {}
for key, row in positions.items():
symbol = _row_text(row, "symbol", default=str(key))
if symbol.replace("-", "").replace("_", "").upper() == asset.replace("-", "").replace("_", "").upper():
return _position_qty(row)
return 0.0
before_qty = _lookup(before)
after_qty = _lookup(after)
diff = abs(before_qty - after_qty)
return diff

View File

@@ -1,330 +0,0 @@
"""Canonical v2 contracts for the DITAv2 execution kernel."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Any, Dict, Mapping, Optional, Sequence, Tuple
class TradeSide(str, Enum):
"""Trade side."""
LONG = "LONG"
SHORT = "SHORT"
FLAT = "FLAT"
class TradeStage(str, Enum):
"""Execution stage for a trade slot."""
IDLE = "IDLE"
DECISION_CREATED = "DECISION_CREATED"
INTENT_CREATED = "INTENT_CREATED"
ORDER_REQUESTED = "ORDER_REQUESTED"
ORDER_SENT = "ORDER_SENT"
ORDER_ACKED = "ORDER_ACKED"
ORDER_REJECTED = "ORDER_REJECTED"
ENTRY_WORKING = "ENTRY_WORKING"
PARTIAL_FILL = "PARTIAL_FILL"
POSITION_OPENED = "POSITION_OPENED"
POSITION_OPEN = "POSITION_OPEN"
EXIT_REQUESTED = "EXIT_REQUESTED"
EXIT_SENT = "EXIT_SENT"
EXIT_ACKED = "EXIT_ACKED"
EXIT_REJECTED = "EXIT_REJECTED"
EXIT_WORKING = "EXIT_WORKING"
POSITION_PARTIALLY_CLOSED = "POSITION_PARTIALLY_CLOSED"
POSITION_CLOSED = "POSITION_CLOSED"
CLOSED = "CLOSED"
TRADE_TERMINAL_WRITTEN = "TRADE_TERMINAL_WRITTEN"
STALE_STATE_RECONCILING = "STALE_STATE_RECONCILING"
class KernelCommandType(str, Enum):
"""Kernel command types."""
ENTER = "ENTER"
EXIT = "EXIT"
MARK_PRICE = "MARK_PRICE"
RECONCILE = "RECONCILE"
CONTROL = "CONTROL"
CANCEL = "CANCEL"
class KernelEventKind(str, Enum):
"""Normalized venue event kinds."""
ORDER_ACK = "ORDER_ACK"
ORDER_REJECT = "ORDER_REJECT"
RATE_LIMITED = "RATE_LIMITED"
PARTIAL_FILL = "PARTIAL_FILL"
FULL_FILL = "FULL_FILL"
CANCEL_ACK = "CANCEL_ACK"
CANCEL_REJECT = "CANCEL_REJECT"
MARK_PRICE = "MARK_PRICE"
RECONCILE = "RECONCILE"
CONTROL = "CONTROL"
class KernelDiagnosticCode(str, Enum):
"""Structured diagnostic codes emitted by the kernel."""
OK = "OK"
RATE_LIMITED = "RATE_LIMITED"
INVALID_SLOT_ID = "INVALID_SLOT_ID"
INVALID_INTENT = "INVALID_INTENT"
UNSUPPORTED_INTENT = "UNSUPPORTED_INTENT"
SLOT_BUSY = "SLOT_BUSY"
NO_OPEN_POSITION = "NO_OPEN_POSITION"
NO_ACTIVE_EXIT_ORDER = "NO_ACTIVE_EXIT_ORDER"
UNKNOWN_EVENT_KIND = "UNKNOWN_EVENT_KIND"
ORDER_REJECTED = "ORDER_REJECTED"
ENTRY_ORDER_REJECTED = "ENTRY_ORDER_REJECTED"
EXIT_ORDER_REJECTED = "EXIT_ORDER_REJECTED"
CANCEL_REJECTED = "CANCEL_REJECTED"
STALE_STATE_RECONCILE = "STALE_STATE_RECONCILE"
RECONCILED = "RECONCILED"
DUPLICATE_EVENT = "DUPLICATE_EVENT"
UNRESOLVED_SLOT = "UNRESOLVED_SLOT"
INVALID_TRANSITION = "INVALID_TRANSITION"
TERMINAL_STATE = "TERMINAL_STATE"
class KernelSeverity(str, Enum):
"""Severity classification for kernel outcomes."""
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"
class VenueOrderStatus(str, Enum):
"""Order status surface mirrored from venue truth."""
NEW = "NEW"
ACKED = "ACKED"
PARTIALLY_FILLED = "PARTIALLY_FILLED"
FILLED = "FILLED"
CANCELED = "CANCELED"
REJECTED = "REJECTED"
class VenueEventStatus(str, Enum):
"""Status alias for normalized venue events."""
ACKED = "ACKED"
REJECTED = "REJECTED"
RATE_LIMITED = "RATE_LIMITED"
PARTIALLY_FILLED = "PARTIALLY_FILLED"
FILLED = "FILLED"
CANCELED = "CANCELED"
CANCELED_REJECTED = "CANCEL_REJECTED"
@dataclass(frozen=True)
class VenueOrder:
"""Venue-specific order identity and fill state."""
internal_trade_id: str
venue_order_id: str
venue_client_id: str
side: TradeSide
intended_size: float
filled_size: float = 0.0
average_fill_price: float = 0.0
status: VenueOrderStatus = VenueOrderStatus.NEW
metadata: Dict[str, Any] = field(default_factory=dict)
@property
def remaining_size(self) -> float:
return max(0.0, float(self.intended_size) - float(self.filled_size))
@dataclass
class TradeSlot:
"""A single execution slot managed by the v2 kernel."""
slot_id: int
trade_id: str = ""
asset: str = ""
side: TradeSide = TradeSide.FLAT
entry_price: float = 0.0
size: float = 0.0
initial_size: float = 0.0
leverage: float = 0.0
entry_time: Optional[datetime] = None
unrealized_pnl: float = 0.0
realized_pnl: float = 0.0
closed: bool = False
exit_leg_ratios: Tuple[float, ...] = (1.0,)
active_leg_index: int = 0
active_exit_order: Optional[VenueOrder] = None
active_entry_order: Optional[VenueOrder] = None
fsm_state: TradeStage = TradeStage.IDLE
close_reason: str = ""
last_event_time: Optional[datetime] = None
seen_event_ids: Tuple[str, ...] = ()
metadata: Dict[str, Any] = field(default_factory=dict)
def is_free(self) -> bool:
return self.fsm_state in {TradeStage.IDLE, TradeStage.CLOSED} and float(self.size or 0.0) <= 0.0 and not self.active_entry_order and not self.active_exit_order
def is_open(self) -> bool:
return self.fsm_state in {
TradeStage.ENTRY_WORKING,
TradeStage.POSITION_OPENED,
TradeStage.POSITION_OPEN,
TradeStage.EXIT_WORKING,
} and not self.closed
def mark_price(self, price: float) -> None:
if price is None or price != price or price <= 0:
return
self.entry_price = self.entry_price or price
if self.entry_price <= 0 or self.size <= 0:
self.unrealized_pnl = 0.0
return
delta = (price - self.entry_price) / self.entry_price
if self.side == TradeSide.SHORT:
delta = -delta
self.unrealized_pnl = delta * self.size * self.entry_price * self.leverage
def next_exit_ratio(self) -> float:
if self.active_leg_index < len(self.exit_leg_ratios):
ratio = float(self.exit_leg_ratios[self.active_leg_index])
return max(0.0, min(1.0, ratio))
return 1.0
def consume_exit_leg(self) -> float:
ratio = self.next_exit_ratio()
self.active_leg_index = min(self.active_leg_index + 1, max(len(self.exit_leg_ratios), 1))
return ratio
def remaining_size(self) -> float:
return max(0.0, float(self.size))
def attach_entry_order(self, order: VenueOrder) -> None:
self.active_entry_order = order
def attach_exit_order(self, order: VenueOrder) -> None:
self.active_exit_order = order
def to_dict(self) -> Dict[str, Any]:
def _order_dict(order: Optional[VenueOrder]) -> Optional[Dict[str, Any]]:
if order is None:
return None
return {
"internal_trade_id": order.internal_trade_id,
"venue_order_id": order.venue_order_id,
"venue_client_id": order.venue_client_id,
"side": order.side.value,
"intended_size": float(order.intended_size or 0.0),
"filled_size": float(order.filled_size or 0.0),
"average_fill_price": float(order.average_fill_price or 0.0),
"status": order.status.value,
"metadata": dict(order.metadata),
}
return {
"slot_id": self.slot_id,
"trade_id": self.trade_id,
"asset": self.asset,
"side": self.side.value,
"entry_price": float(self.entry_price or 0.0),
"size": float(self.size or 0.0),
"initial_size": float(self.initial_size or 0.0),
"leverage": float(self.leverage or 0.0),
"entry_time": self.entry_time.isoformat() if hasattr(self.entry_time, "isoformat") else None,
"unrealized_pnl": float(self.unrealized_pnl or 0.0),
"realized_pnl": float(self.realized_pnl or 0.0),
"closed": bool(self.closed),
"exit_leg_ratios": [float(r) for r in self.exit_leg_ratios],
"active_leg_index": int(self.active_leg_index or 0),
"active_exit_order": _order_dict(self.active_exit_order),
"active_entry_order": _order_dict(self.active_entry_order),
"fsm_state": self.fsm_state.value,
"close_reason": self.close_reason,
"last_event_time": self.last_event_time.isoformat() if hasattr(self.last_event_time, "isoformat") else None,
"seen_event_ids": list(self.seen_event_ids),
"metadata": dict(self.metadata),
}
@dataclass(frozen=True)
class KernelIntent:
"""Command emitted by the algo and written to the hot-path intent region."""
timestamp: datetime
intent_id: str
trade_id: str
slot_id: int
asset: str
side: TradeSide
action: KernelCommandType
reference_price: float
target_size: float
leverage: float
exit_leg_ratios: Tuple[float, ...] = (1.0,)
reason: str = ""
metadata: Dict[str, Any] = field(default_factory=dict)
stage: TradeStage = TradeStage.INTENT_CREATED
order_type: str = "MARKET"
limit_price: float = 0.0
@dataclass(frozen=True)
class VenueEvent:
"""Normalized venue truth mapped into DITAv2 semantics."""
timestamp: datetime
event_id: str
trade_id: str
slot_id: int
kind: KernelEventKind
status: VenueEventStatus
venue_order_id: str = ""
venue_client_id: str = ""
side: TradeSide = TradeSide.FLAT
asset: str = ""
price: float = 0.0
size: float = 0.0
filled_size: float = 0.0
remaining_size: float = 0.0
reason: str = ""
raw_payload: Dict[str, Any] = field(default_factory=dict)
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class KernelTransition:
"""Durable kernel transition used for debug journaling."""
timestamp: datetime
trade_id: str
slot_id: int
prev_state: TradeStage
next_state: TradeStage
trigger: str
intent_id: str = ""
event_id: str = ""
control_mode: str = ""
control_verbosity: str = ""
details: Dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True)
class KernelOutcome:
"""Result of applying a command or venue event."""
accepted: bool
slot_id: int
trade_id: str
state: TradeStage
diagnostic_code: KernelDiagnosticCode = KernelDiagnosticCode.OK
severity: KernelSeverity = KernelSeverity.INFO
transitions: Tuple[KernelTransition, ...] = ()
emitted_events: Tuple[VenueEvent, ...] = ()
details: Dict[str, Any] = field(default_factory=dict)

View File

@@ -1,217 +0,0 @@
"""Runtime control plane for DITAv2."""
from __future__ import annotations
from dataclasses import asdict, dataclass, replace
from enum import Enum
import os
import threading
import time
from typing import Any, Dict, Mapping, Optional, Protocol
from .utils import json_safe
class KernelMode(str, Enum):
NORMAL = "NORMAL"
DEBUG = "DEBUG"
class KernelVerbosity(str, Enum):
QUIET = "QUIET"
VERBOSE = "VERBOSE"
TRACE = "TRACE"
class BackendMode(str, Enum):
MOCK = "MOCK"
BINGX = "BINGX"
@dataclass(frozen=True)
class KernelControlSnapshot:
"""Control plane state shared across the kernel."""
mode: KernelMode = KernelMode.NORMAL
verbosity: KernelVerbosity = KernelVerbosity.QUIET
backend_mode: BackendMode = BackendMode.MOCK
debug_clickhouse_enabled: bool = True
trace_transitions: bool = False
mirror_to_hazelcast: bool = True
active_slot_limit: int = 10
reconcile_on_restart: bool = True
runtime_namespace: str = "dita_v2"
strategy_namespace: str = "dita_v2"
event_namespace: str = "dita_v2"
actor_name: str = "ExecutionKernel"
exec_venue: str = "bingx"
data_venue: str = "binance"
ledger_authority: str = "exchange"
mock_fidelity_mode: str = "bingx_exact_shape"
def as_dict(self) -> Dict[str, Any]:
return dict(asdict(self))
@dataclass(frozen=True)
class ControlUpdate:
"""Partial update to the control plane."""
mode: Optional[KernelMode] = None
verbosity: Optional[KernelVerbosity] = None
backend_mode: Optional[BackendMode] = None
debug_clickhouse_enabled: Optional[bool] = None
trace_transitions: Optional[bool] = None
mirror_to_hazelcast: Optional[bool] = None
active_slot_limit: Optional[int] = None
reconcile_on_restart: Optional[bool] = None
runtime_namespace: Optional[str] = None
strategy_namespace: Optional[str] = None
event_namespace: Optional[str] = None
actor_name: Optional[str] = None
exec_venue: Optional[str] = None
data_venue: Optional[str] = None
ledger_authority: Optional[str] = None
mock_fidelity_mode: Optional[str] = None
def apply(self, snapshot: KernelControlSnapshot) -> KernelControlSnapshot:
payload = {
key: value
for key, value in asdict(self).items()
if value is not None
}
return replace(snapshot, **payload)
class ControlPlane(Protocol):
"""Kernel control plane interface."""
def read(self) -> KernelControlSnapshot:
...
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
...
def mirror(self) -> Mapping[str, Any]:
...
def wait(self, timeout_ms: int = 1000) -> bool:
...
def notify(self) -> None:
...
class InMemoryControlPlane:
"""Local control plane used for tests and the Python prototype."""
def __init__(self, snapshot: Optional[KernelControlSnapshot] = None):
self._snapshot = snapshot or KernelControlSnapshot()
self._mirror: Dict[str, Any] = {}
self._seq = 0
self._observed_seq = 0
self._signal = threading.Condition()
def read(self) -> KernelControlSnapshot:
return self._snapshot
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
with self._signal:
self._snapshot = update.apply(self._snapshot)
self._mirror = self._snapshot.as_dict()
self._seq += 1
self._signal.notify_all()
return self._snapshot
def mirror(self) -> Mapping[str, Any]:
return dict(self._mirror)
def wait(self, timeout_ms: int = 1000) -> bool:
timeout_s = None if timeout_ms is None or timeout_ms < 0 else max(0.0, timeout_ms / 1000.0)
deadline = None if timeout_s is None else time.monotonic() + timeout_s
with self._signal:
observed = self._observed_seq
while self._seq == observed:
if deadline is None:
self._signal.wait()
continue
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
self._signal.wait(timeout=remaining)
self._observed_seq = self._seq
return True
def notify(self) -> None:
with self._signal:
self._seq += 1
self._signal.notify_all()
class ZincControlPlane(InMemoryControlPlane):
"""In-memory stand-in for a Zinc-backed control region.
The class keeps the interface explicit so a real Zinc binding can be
dropped in later without changing kernel code.
"""
def __init__(self, snapshot: Optional[KernelControlSnapshot] = None):
super().__init__(snapshot=snapshot)
self.region: Dict[str, Any] = self._snapshot.as_dict()
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = super().update(update)
self.region = snapshot.as_dict()
return snapshot
def read(self) -> KernelControlSnapshot:
return self._snapshot
class MirroredControlPlane:
"""Control plane that mirrors updates to an external durable sink."""
def __init__(self, inner: ControlPlane, mirror_sink: Optional[Any] = None):
self.inner = inner
self.mirror_sink = mirror_sink
def read(self) -> KernelControlSnapshot:
return self.inner.read()
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = self.inner.update(update)
if self.mirror_sink is not None:
self.mirror_sink("dita_control_plane", dict(snapshot.as_dict()))
return snapshot
def mirror(self) -> Mapping[str, Any]:
return self.inner.mirror()
def build_control_plane(
snapshot: Optional[KernelControlSnapshot] = None,
*,
prefer_real_zinc: Optional[bool] = None,
prefix: str = "dita_v2",
) -> ControlPlane:
"""Build the active control plane with an operator-visible switch.
The default remains the in-process Zinc stand-in so existing tests and
callers stay stable. Setting ``DITA_V2_CONTROL_PLANE=REAL_ZINC`` or passing
``prefer_real_zinc=True`` opts into the shared-memory control plane when
the Zinc adapter is available.
"""
env_choice = os.environ.get("DITA_V2_CONTROL_PLANE", "").strip().upper()
real_requested = prefer_real_zinc if prefer_real_zinc is not None else env_choice in {"REAL", "REAL_ZINC", "SHARED", "SHARED_MEM"}
if real_requested:
try:
from .real_control_plane import RealZincControlPlane
plane = RealZincControlPlane(prefix=prefix, create=True)
if snapshot is not None:
plane.update(ControlUpdate(**{key: value for key, value in snapshot.as_dict().items()}))
return plane
except Exception:
pass
return ZincControlPlane(snapshot=snapshot)

View File

@@ -1,438 +0,0 @@
#!/usr/bin/env python3
"""Write the complete 68-test live e2e file. Bodies receive (k, symbol, p) where p is a float."""
import ast, os
SCENARIOS = [] # (name, code_lines)
def S(name, lines):
SCENARIOS.append((name, lines))
# ---- Original 9 ----
S("simple_entry_exit", [
"tid = f's-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("multi_leg_exit", [
"tid = f'ml-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)",
])
S("cancel_entry_order", [
"tid = f'ce-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("entry_hold_exit", [
"tid = f'h-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(3)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("entry_exit_at_loss", [
"tid = f'l-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*1.005, 0.001); await asyncio.sleep(1)",
])
S("two_sequential_cycles", [
"t1 = f'2c1-{int(time.time()*1000)}'; t2 = f'2c2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(1)",
])
S("entry_then_recover", [
"tid = f'r-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"await bundle.runtime.disconnect()",
"await bundle.runtime.connect(initial_capital=k.account.snapshot.capital)",
"await asyncio.sleep(1)",
])
S("long_entry_exit", [
"tid = f'ln-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'LONG', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'LONG', p*1.005, 0.001); await asyncio.sleep(1)",
])
# ---- Cancel combos ----
S("cancel_idempotent", [
"tid = f'ci-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("double_cancel", [
"tid = f'dc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
])
S("cancel_then_exit", [
"tid = f'ctx-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("exit_then_cancel_exit", [
"tid = f'exc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("exit_then_reentry", [
"t1 = f'er1-{int(time.time()*1000)}'; t2 = f'er2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
])
S("limit_cancel", [
"tid = f'lc-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p*0.9, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p*0.9, 0.001); await asyncio.sleep(1)",
])
# ---- X4 ----
S("x4_partial_hold_exit", [
"tid = f'ph-{int(time.time()*1000)}'; sz = 0.003",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.3, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.7, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)",
])
S("x4_three_leg", [
"tid = f'3l-{int(time.time()*1000)}'; sz = 0.004",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*0.5, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)",
])
S("x4_cancel_fill_partial", [
"tid = f'cfp-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.002); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, 0.002); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, 0.001); await asyncio.sleep(1)",
])
S("x4_rapid_three", [
"for i in range(3):",
" tid = f'r3-{i}-{int(time.time()*1000)}'",
" _si(k, E.ENTER, tid, symbol, 'SHORT', p*(1-i*0.005), 0.001); await asyncio.sleep(0.8)",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-i*0.005), 0.001); await asyncio.sleep(0.8)",
])
S("x4_diff_symbol", [
"tid = f'ds-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"sym2 = 'BTCUSDT' if symbol != 'BTCUSDT' else 'ETHUSDT'",
"_si(k, E.EXIT, tid, sym2, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
])
S("x4_alternating", [
"t1 = f'as1-{int(time.time()*1000)}'; t2 = f'as2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"sym2 = 'BTCUSDT' if symbol != 'BTCUSDT' else 'ETHUSDT'",
"try:",
" p2 = float(json.loads(urllib.request.urlopen('https://open-api-vst.bingx.com/openApi/swap/v2/quote/price?symbol='+sym2.replace('USDT','-USDT'), timeout=5).read())['data']['price'])",
"except: p2 = p",
"_si(k, E.ENTER, t2, sym2, 'LONG', p2, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(1)",
"_si(k, E.EXIT, t2, sym2, 'LONG', p2*1.005, 0.001); await asyncio.sleep(1)",
])
S("x4_multi_flatten", [
"tid = f'mf-{int(time.time()*1000)}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(1)",
"for i in range(3):",
" if k.slot(0).is_free(): break",
" _flatten(k, symbol, p*0.99, f'mf{i}'); await asyncio.sleep(0.5)",
])
S("x4_three_leg_25_50_25", [
"tid = f'x4a-{int(time.time()*1000)}'; sz = 0.004",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.5, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)",
])
S("x4_enter_exit_hold_twice", [
"t1 = f'x4b1-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t1, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"t2 = f'x4b2-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
"t3 = f'x4b3-{int(time.time()*1000)}'",
"_si(k, E.ENTER, t3, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
"_si(k, E.EXIT, t3, symbol, 'SHORT', p*0.985, 0.001); await asyncio.sleep(0.5)",
])
S("x4_cancel_then_double_exit", [
"tid = f'x4c-{int(time.time()*1000)}'; sz = 0.002",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"_si(k, E.CANCEL, tid, symbol, 'SHORT', p, sz); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.993, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)",
])
# ---- 2 sides x 2 profit x 4 patterns = 16 doubled ----
for side, side_str, ep in [("short","SHORT",0.995), ("long","LONG",1.005)]:
for prof, pname, xp in [(True,"profit",ep), (False,"loss",1/ep)]:
for pat, pat_suffix, lines in [
("basic", "", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.8)",
]),
("partial", "_partial", [
"sz = 0.002",
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{ep}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)",
]),
("cancel", "_cancel", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.3)",
f"_si(k, E.CANCEL, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
f" _si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.8)",
]),
("double_exit", "_double_exit", [
f"_si(k, E.ENTER, tid, symbol, '{side_str}', p, 0.001); await asyncio.sleep(0.8)",
f"_si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}, 0.001); await asyncio.sleep(0.3)",
"if not k.slot(0).is_free():",
f" _si(k, E.EXIT, tid, symbol, '{side_str}', p*{xp}*0.995, 0.001); await asyncio.sleep(0.5)",
]),
]:
pfx = f"{pat[0]}{side[0]}{chr(112) if prof else chr(108)}"
S(f"{pat}_{side}_{pname}", [
f"tid = f'{pfx}-{{{{int(time.time()*1000)}}}}'",
*lines,
])
# ---- Triple seq x 4 SHORT + 4 LONG ----
for i in range(4):
S(f"triple_seq_{i}", [
"for j in range(3):",
f" tid = f'ts{i}-j-{{{{int(time.time()*1000)}}}}'",
" _si(k, E.ENTER, tid, symbol, 'SHORT', p*(1-j*0.003), 0.001); await asyncio.sleep(0.7)",
" _si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-j*0.003), 0.001); await asyncio.sleep(0.7)",
])
for i in range(4):
S(f"triple_seq_long_{i}", [
"for j in range(3):",
f" tid = f'tsl{i}-j-{{{{int(time.time()*1000)}}}}'",
" _si(k, E.ENTER, tid, symbol, 'LONG', p*(1+j*0.003), 0.001); await asyncio.sleep(0.7)",
" _si(k, E.EXIT, tid, symbol, 'LONG', p*1.005*(1+j*0.003), 0.001); await asyncio.sleep(0.7)",
])
# ---- Cancel+reenter x 4 SHORT + 4 LONG ----
for i in range(4):
S(f"cancel_reenter_{i}", [
f"t1 = f'cr{i}a-{{{{int(time.time()*1000)}}}}'; t2 = f'cr{i}b-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, t1, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'SHORT', p*0.995, 0.001); await asyncio.sleep(0.8)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t2, symbol, 'SHORT', p*0.99, 0.001); await asyncio.sleep(0.5)",
])
for i in range(4):
S(f"cancel_reenter_long_{i}", [
f"t1 = f'crl{i}a-{{{{int(time.time()*1000)}}}}'; t2 = f'crl{i}b-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, t1, symbol, 'LONG', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.CANCEL, t1, symbol, 'LONG', p, 0.001); await asyncio.sleep(0.3)",
"_si(k, E.ENTER, t2, symbol, 'LONG', p*1.005, 0.001); await asyncio.sleep(0.8)",
"if not k.slot(0).is_free():",
" _si(k, E.EXIT, t2, symbol, 'LONG', p*1.01, 0.001); await asyncio.sleep(0.5)",
])
# ---- Leg ratios x 8 ----
for i, ratios in enumerate([
(0.1,1.0), (0.33,0.33,1.0), (0.5,0.5,1.0), (0.75,1.0),
(0.2,0.3,0.5,1.0), (0.4,0.6,1.0), (0.15,0.85,1.0), (0.25,0.25,0.5,1.0),
]):
rat_str = ",".join(str(r) for r in ratios)
code = [f"tid = f'lr{i}-{{{{int(time.time()*1000)}}}}'; sz = 0.004",
f"_si(k, E.ENTER, tid, symbol, 'SHORT', p, sz, exit_leg_ratios=({rat_str})); await asyncio.sleep(1)"]
for leg in range(len(ratios) - 1):
r = ratios[leg]
code.append(f"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.995*(1-{leg}*0.002), sz*{r}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)")
code.append(f"_si(k, E.EXIT, tid, symbol, 'SHORT', p*0.99, sz*{ratios[-1]}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)")
S(f"leg_ratio_{i}", code)
# ---- Breakeven x 4 ----
for i in range(4):
S(f"breakeven_{i}", [
f"tid = f'be{i}-{{{{int(time.time()*1000)}}}}'",
"_si(k, E.ENTER, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
"_si(k, E.EXIT, tid, symbol, 'SHORT', p, 0.001); await asyncio.sleep(0.8)",
])
# =====================================================================
# Assemble
# =====================================================================
HEADER = '''#!/usr/bin/env python3
"""PINK DITAv2 Live BingX Testnet E2E — 68 combinatorial scenarios.
Kernel-direct tests: bodies receive (k, symbol, p). Capital integrity
asserted. Exchange state confirmed flat.
"""
from __future__ import annotations
import asyncio, json, os, socket, time, urllib.request
import urllib.parse
from dataclasses import dataclass
from typing import Any, Optional
import pytest
from prod.bingx.http import BingxHttpClient
from prod.bingx.config import BingxExecClientConfig, BingxEnvironment
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType as KC, KernelIntent as KI, TradeSide as TS,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot
E = KC
# Force IPv4 for httpx (IPv6 resolution fails in this env)
_orig_gai = socket.getaddrinfo
def _ipv4_gai(host, port, family=0, type=0, proto=0, flags=0):
return _orig_gai(host, port, socket.AF_INET, type, proto, flags)
socket.getaddrinfo = _ipv4_gai
# ---- env gates ----
if not os.environ.get("BINGX_SMOKE_LIVE"):
pytest.skip("BINGX_SMOKE_LIVE not set", allow_module_level=True)
if not os.environ.get("BINGX_SMOKE_ALLOW_TRADE"):
pytest.skip("BINGX_SMOKE_ALLOW_TRADE not set", allow_module_level=True)
if not os.environ.get("PINK_DITA_E2E"):
pytest.skip("PINK_DITA_E2E not set", allow_module_level=True)
# ---- helpers ----
@dataclass
class VR:
symbol: str; positions_flat: bool = True; error: str = ""
@dataclass
class RB:
runtime: Any; config: Any
def _build_config(ic: float = 25000.0) -> BingxExecClientConfig:
return BingxExecClientConfig(
api_key=os.environ["BINGX_API_KEY"], secret_key=os.environ["BINGX_SECRET_KEY"],
environment=BingxEnvironment.VST, allow_mainnet=False, recv_window_ms=5000,
default_leverage=1, exchange_leverage_cap=3, prefer_websocket=False,
use_reduce_only=True, sizing_mode="testnet", journal_strategy="pink",
journal_db="dolphin_pink")
def _build_rb(ic: float = 25000.0) -> RB:
cfg = _build_config(ic)
b = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
k = b.kernel; k.account.snapshot.capital = ic; k.account.snapshot.peak_capital = ic; k.account.snapshot.equity = ic
class Shim:
def __init__(self, k): self.kernel = k
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except: pass
return RB(runtime=Shim(k), config=cfg)
async def _contract_rows(c):
r = await c._request_json("GET", "/openApi/swap/v2/user/positions", {}, signed=True)
return r if isinstance(r, list) else (r.get("data") or r.get("positions") or [])
async def _pick_sym(k, c):
rs = await _contract_rows(c)
oss = {str(r.get("symbol","")).replace("-","").upper() for r in rs}
sym = next((x for x in ["TRXUSDT","XRPUSDT","ADAUSDT","DOGEUSDT"] if x not in oss), "TRXUSDT")
return sym
async def _snap(c, sym):
vs = sym[:3]+"-USDT"
pr = await c._request_json("GET", "/openApi/swap/v2/quote/price", {"symbol": vs}, signed=False)
d = pr.get("data") or pr; rp = float(d.get("price") or d.get("lastPrice") or 0)
return MarketSnapshot(timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
symbol=sym, price=rp, bid=rp*0.9995, ask=rp*1.0005), vs
async def _verify(c, vs):
rs = await _contract_rows(c)
tr = [r for r in rs if str(r.get("symbol","")).upper().replace("-","") == vs.replace("-","").upper()]
ts = sum(abs(float(r.get("positionAmt",r.get("positionQty",0)) or 0)) for r in tr)
flat = ts < 1e-8
return VR(symbol=vs, positions_flat=flat, error="" if flat else f"open: {tr}")
def _si(k, act, tid, asset, side_str, price, size, **kw):
ds = TS.SHORT if side_str.upper() == "SHORT" else TS.LONG
return k.process_intent(KI(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=tid, trade_id=tid, slot_id=0, asset=asset, side=ds, action=act,
reference_price=price, target_size=size, leverage=kw.pop("leverage",1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios",(1.0,)),
reason=kw.pop("reason",f"auto_{act.value.lower()}"), metadata=kw))
def _flatten(k, sym, price, label):
if k.slot(0).is_free(): return
_si(k, E.EXIT, f"fl{label}-{int(time.time()*1000)}", sym, "SHORT", price, 0.001)
async def _run(bundle, client, body_fn, label, ic):
k = bundle.runtime.kernel
sym = await _pick_sym(k, client)
snap, vsym = await _snap(client, sym)
await bundle.runtime.connect(initial_capital=ic)
p = float(snap.price)
try:
_flatten(k, sym, p, f"{label}-pre")
await asyncio.sleep(0.3)
cb = k.account.snapshot.capital
await body_fn(k, sym, p)
ca = k.account.snapshot.capital
assert ca > 0, f"Capital zero: {ca}"
assert ca < cb * 10, f"Capital bounds: {cb} -> {ca}"
if not k.slot(0).is_free():
_flatten(k, sym, p*0.99, f"{label}-post")
await asyncio.sleep(1.0)
return await _verify(client, vsym)
finally:
await bundle.runtime.disconnect()
'''
lines = [HEADER]
# Scenario bodies
lines.append("\n# =====================================================================\n# Scenario bodies\n# =====================================================================\n")
for name, code_lines in SCENARIOS:
lines.append(f"async def _body_{name}(k, symbol, p):")
for cl in code_lines:
lines.append(f" {cl}")
lines.append("")
# Test functions
lines.append("\n# =====================================================================\n# Test functions\n# =====================================================================\n")
lines.append('''@pytest.fixture(scope="session")
def _live_client():
return BingxHttpClient(_build_config())
''')
for name, _ in SCENARIOS:
lines.append(f'''
def test_pink_ditav2_{name}(_live_client) -> None:
bundle = _build_rb()
ic = bundle.runtime.kernel.account.snapshot.capital
r = asyncio.run(_run(bundle, _live_client, _body_{name}, "{name}", ic))
assert r.positions_flat, name + ": " + r.error
''')
full = '\n'.join(lines)
try:
ast.parse(full)
count = full.count("def test_pink_ditav2_")
print(f"Syntax OK — {count} tests, {len(full)} chars")
out_path = os.path.join('/mnt/dolphinng5_predict', 'prod/tests/test_pink_bingx_dita_live_e2e.py')
with open(out_path, 'w') as f:
f.write(full)
print(f"Written OK ({count} tests)")
except SyntaxError as e:
print(f"Syntax error L{e.lineno}: {e.msg}")
fl = full.split('\n')
for i in range(max(0,e.lineno-5), min(len(fl), e.lineno+3)):
print(f" {i+1}: {fl[i]}")

View File

@@ -1,688 +0,0 @@
#!/usr/bin/env python3
"""Regenerate the complete PINK DITAv2 live BingX e2e test file from scratch."""
import ast, os
BASE = '/mnt/dolphinng5_predict'
OUT = os.path.join(BASE, 'prod/tests/test_pink_bingx_dita_live_e2e.py')
# =====================================================================
# Static prologue — imports, helpers, env check
# =====================================================================
PROLOGUE = r'''#!/usr/bin/env python3
"""PINK DITAv2 Live BingX Testnet E2E — combinatorial scenarios.
Each test:
1. Picks a live VST symbol with price
2. Submits KernelIntent directly (bypasses DecisionEngine)
3. Asserts capital integrity (positive, within bounds)
4. Confirms exchange state is flat after exit
"""
from __future__ import annotations
import asyncio
import json
import os
import time
import urllib.parse
import urllib.request
from dataclasses import dataclass, field
from decimal import Decimal
from typing import Any, Optional
import pytest
import requests
from prod.bingx.http import BingxHttpClient
from prod.bingx.config import BingxExecClientConfig, BingxEnvironment
from prod.bingx.schemas import BingxContract
from prod.clean_arch.dita_v2.launcher import build_launcher_bundle
from prod.clean_arch.dita_v2.contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelIntent,
KernelOutcome,
TradeSide,
)
from prod.clean_arch.ports.data_feed import MarketSnapshot
from prod.clean_arch.dita import DecisionConfig, DecisionEngine, IntentEngine
from prod.clean_arch.runtime.pink_direct import PinkDirectRuntime
from prod.clean_arch.projection import build_projection
from prod.clean_arch.adapters.hazelcast_feed import HazelcastDataFeed
# ---- env gates ----
if not os.environ.get("BINGX_SMOKE_LIVE"):
pytest.skip("BINGX_SMOKE_LIVE not set — skipping live tests", allow_module_level=True)
if not os.environ.get("BINGX_SMOKE_ALLOW_TRADE"):
pytest.skip("BINGX_SMOKE_ALLOW_TRADE not set — skipping live trade tests", allow_module_level=True)
if not os.environ.get("PINK_DITA_E2E"):
pytest.skip("PINK_DITA_E2E not set — skipping PINK DITAv2 e2e tests", allow_module_level=True)
_INTER_TEST_DELAY_S = 3.0
def _wait_for_quota() -> None:
"""Block until the exchange rate-limit quota allows a burst."""
time.sleep(_INTER_TEST_DELAY_S)
def _normalize(symbol: str) -> str:
return symbol.replace("-", "").upper()
async def _contract_rows(client: BingxHttpClient) -> list[dict]:
url = "https://open-api-vst.bingx.com/openApi/swap/v2/user/positions"
rows = await client._request_json("GET", url, {}, signed=True)
data = rows if isinstance(rows, list) else (rows.get("data") or rows.get("positions") or [])
return data
async def _build_live_snapshot(client: BingxHttpClient, vsymbol: str) -> MarketSnapshot:
vsym_dash = vsymbol.replace("USDT", "-USDT")
price_resp = await client._request_json("GET", "https://open-api-vst.bingx.com/openApi/swap/v2/quote/price", {"symbol": vsym_dash}, signed=False)
d = price_resp.get("data") or price_resp
raw_price = d.get("price") or d.get("lastPrice") or 0
price = Decimal(str(raw_price))
return MarketSnapshot(
timestamp=time.time(), price=price, bid=price * Decimal("0.9995"),
ask=price * Decimal("1.0005"), volume=Decimal("0"),
)
@dataclass
class _VerificationResult:
symbol: str
positions_flat: bool = True
error: str = ""
async def _query_exchange_positions(client: BingxHttpClient, venue_symbol: str) -> list[dict]:
"""Fetch live positions from BingX and return rows for venue_symbol."""
rows = _contract_rows(client)
return [r for r in rows if str(r.get("symbol", "")).upper().replace("-", "") == venue_symbol.replace("-", "").upper()]
async def _verify_exchange_state(
client: BingxHttpClient, venue_symbol: str, expect_open: bool = False,
) -> _VerificationResult:
pos_rows = await _query_exchange_positions(client, venue_symbol)
total_size = sum(abs(float(r.get("positionAmt", r.get("positionQty", 0)) or 0)) for r in pos_rows)
flat = total_size < 1e-8
if expect_open and flat:
return _VerificationResult(symbol=venue_symbol, positions_flat=False, error="expected open position but flat")
if not expect_open and not flat:
return _VerificationResult(symbol=venue_symbol, positions_flat=False, error=f"expected flat but open: {pos_rows}")
return _VerificationResult(symbol=venue_symbol, positions_flat=True)
@dataclass
class _RuntimeBundle:
runtime: PinkDirectRuntime
config: BingxExecClientConfig
def _build_bingx_config(initial_capital: float) -> BingxExecClientConfig:
return BingxExecClientConfig(
api_key=os.environ["BINGX_API_KEY"],
secret_key=os.environ["BINGX_SECRET_KEY"],
environment=BingxEnvironment.VST,
allow_mainnet=False,
recv_window_ms=5000,
default_leverage=1,
exchange_leverage_cap=3,
prefer_websocket=False,
use_reduce_only=True,
sizing_mode="testnet",
journal_strategy="pink",
journal_db="dolphin_pink",
)
def _build_runtime_bundle(initial_capital: float) -> _RuntimeBundle:
"""Build a direct kernel bundle."""
cfg = _build_bingx_config(initial_capital)
bundle = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
k = bundle.kernel
k.account.snapshot.capital = initial_capital
k.account.snapshot.peak_capital = initial_capital
k.account.snapshot.equity = initial_capital
return _RuntimeBundle(runtime=_RuntimeShim(kernel=k), config=cfg)
class _RuntimeShim:
"""Minimal runtime wrapper — exposes .kernel + sync connect/disconnect."""
def __init__(self, kernel): self.kernel = kernel
async def connect(self, initial_capital=0): self.kernel.venue.connect()
async def disconnect(self):
try: self.kernel.venue.disconnect()
except Exception: pass
def _build_full_runtime(initial_capital: float) -> PinkDirectRuntime:
"""Build a fully wired PinkDirectRuntime (data feed, engine, persistence)."""
cfg = _build_bingx_config(initial_capital)
bundle = build_launcher_bundle(venue_mode="BINGX", max_slots=1, bingx_config=cfg)
feed = HazelcastDataFeed(
prefix="dita_v2",
hz_client=build_projection(prefer_real_hazelcast=False),
)
engine = DecisionEngine(DecisionConfig(initial_capital=initial_capital))
intent_engine = IntentEngine(initial_capital=initial_capital)
rt = PinkDirectRuntime(
data_feed=feed, kernel=bundle.kernel,
decision_engine=engine, intent_engine=intent_engine,
)
rt.kernel.account.snapshot.capital = initial_capital
rt.kernel.account.snapshot.peak_capital = initial_capital
rt.kernel.account.snapshot.equity = initial_capital
return rt
async def _pick_live_symbol(
kernel: Any, client: BingxHttpClient,
) -> tuple[str, MarketSnapshot, str]:
"""Pick a live VST symbol that isn't already in a position."""
pos_rows = _contract_rows(client)
open_syms = set()
for r in pos_rows:
sym = str(r.get("symbol", "")).replace("-", "").upper()
if sym:
open_syms.add(sym)
candidates = ["TRXUSDT", "XRPUSDT", "ADAUSDT", "DOGEUSDT"]
preferred = [c for c in candidates if c not in open_syms]
sym = preferred[0] if preferred else candidates[0]
vsym = sym[:3] + "-USDT" if sym.endswith("USDT") and len(sym) > 6 else sym[:3] + "-USDT"
snap = _build_live_snapshot(client, vsym)
return sym, snap, vsym
def _submit_intent_direct(
kernel: Any,
action: KernelCommandType,
trade_id: str,
asset: str,
side_str: str,
price: float,
size: float,
**kw,
) -> KernelOutcome:
ds = TradeSide.SHORT if side_str.upper() == "SHORT" else TradeSide.LONG
intent = KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=trade_id,
trade_id=trade_id,
slot_id=0,
asset=asset,
side=ds,
action=action,
reference_price=price,
target_size=size,
leverage=kw.pop("leverage", 1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios", (1.0,)),
reason=kw.pop("reason", f"auto_{action.value.lower()}"),
metadata=kw,
)
return kernel.process_intent(intent)
def _flatten_via_kernel_intent(kernel: Any, symbol: str, price: float, label: str) -> None:
"""Flatten slot 0 by submitting an EXIT intent at the given price.
No-op if already flat."""
if kernel.slot(0).is_free():
return
tid = f"flat-{label}-{int(time.time() * 1000)}"
side = TradeSide.SHORT
intent = KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=tid,
trade_id=tid,
slot_id=0,
asset=symbol,
side=side,
action=KernelCommandType.EXIT,
reference_price=price,
target_size=0.001,
leverage=1.0,
exit_leg_ratios=(1.0,),
reason=f"flatten_{label}",
)
kernel.process_intent(intent)
async def _flatten_live_position(client: BingxHttpClient, symbol: str) -> None:
"""Emergency raw flatten via REST if kernel can't."""
pass
async def _run_pink_live_roundtrip(
bundle: _RuntimeBundle, client: BingxHttpClient,
) -> tuple[KernelOutcome, Optional[KernelOutcome], Optional[KernelOutcome]]:
"""Original roundtrip test entry → partial/monitor → flatten."""
kernel = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(kernel, client)
price = float(snap.price)
await bundle.runtime.connect(initial_capital=25000.0)
try:
_flatten_via_kernel_intent(kernel, symbol, price, "roundtrip-pre")
await asyncio.sleep(0.3)
tid = f"rt-{int(time.time() * 1000)}"
entry = _submit_intent_direct(kernel, KernelCommandType.ENTER, tid, symbol, "SHORT", price, 0.001)
await asyncio.sleep(1.0)
monitor = None
if not kernel.slot(0).is_free():
_submit_intent_direct(kernel, KernelCommandType.CANCEL, tid, symbol, "SHORT", price, 0.001)
await asyncio.sleep(0.3)
flatt = None
if not kernel.slot(0).is_free():
flatt = _submit_intent_direct(kernel, KernelCommandType.EXIT, tid, symbol, "SHORT", price * 0.995, 0.001)
await asyncio.sleep(1.0)
if not kernel.slot(0).is_free():
_flatten_via_kernel_intent(kernel, symbol, price * 0.99, "roundtrip-post")
await asyncio.sleep(1.0)
return entry, monitor, flatt
finally:
await bundle.runtime.disconnect()
async def _run_pink_live_recovery(
bundle: _RuntimeBundle, client: BingxHttpClient,
) -> dict:
"""Recovery test: enter, disconnect, reconnect, verify capital preserved."""
kernel = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(kernel, client)
price = float(snap.price)
await bundle.runtime.connect(initial_capital=25000.0)
try:
_flatten_via_kernel_intent(kernel, symbol, price, "recovery-pre")
await asyncio.sleep(0.3)
_submit_intent_direct(kernel, KernelCommandType.ENTER, tid := f"r-{int(time.time() * 1000)}", symbol, "SHORT", price, 0.001)
await asyncio.sleep(1.0)
await bundle.runtime.disconnect()
await bundle.runtime.connect(initial_capital=25000.0)
await asyncio.sleep(1.0)
if not kernel.slot(0).is_free():
_flatten_via_kernel_intent(kernel, symbol, price * 0.99, "recovery-post")
await asyncio.sleep(1.0)
return {"capital": kernel.account.snapshot.capital, "peak": kernel.account.snapshot.peak_capital}
finally:
await bundle.runtime.disconnect()
''' # end PROLOGUE
# =====================================================================
# Scenario runner + shortcut
# =====================================================================
RUNNER = '''
# =====================================================================
# Generic runner & shortcut
# =====================================================================
async def _run_scenario(bundle, client, body_fn, label, initial_capital):
k = bundle.runtime.kernel
symbol, snap, vsym = await _pick_live_symbol(k, client)
await bundle.runtime.connect(initial_capital=initial_capital)
try:
_flatten_via_kernel_intent(k, symbol, float(snap.price), f"{label}-pre")
await asyncio.sleep(0.3)
_cap_before = k.account.snapshot.capital
await body_fn(bundle, client, symbol, snap)
_cap_after = k.account.snapshot.capital
assert _cap_after > 0, f"Capital went to zero: {_cap_after}"
assert _cap_after < _cap_before * 10, f"Capital growth beyond bounds: {_cap_before} -> {_cap_after}"
if not k.slot(0).is_free():
_flatten_via_kernel_intent(k, symbol, float(snap.price) * 0.99, f"{label}-post")
await asyncio.sleep(1.0)
return await _verify_exchange_state(client, vsym, expect_open=False)
finally:
await bundle.runtime.disconnect()
def _si(kernel, action, trade_id, asset, side_str, price, size, **kw):
ds = TradeSide.SHORT if side_str.upper() == "SHORT" else TradeSide.LONG
return kernel.process_intent(KernelIntent(
timestamp=__import__("datetime").datetime.now(__import__("datetime").timezone.utc),
intent_id=trade_id, trade_id=trade_id, slot_id=0, asset=asset,
side=ds, action=action, reference_price=price, target_size=size,
leverage=kw.pop("leverage", 1.0),
exit_leg_ratios=kw.pop("exit_leg_ratios", (1.0,)),
reason=kw.pop("reason", f"auto_{action.value.lower()}"),
metadata=kw,
))
'''
# =====================================================================
# Build scenario bodies + tests
# =====================================================================
scenarios = [] # (name, code_lines)
def S(name, code_lines):
scenarios.append((name, list(code_lines)))
# --- Original 9 ---
S("simple_entry_exit", [
'tid = f"s-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("multi_leg_exit", [
'tid = f"ml-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.002, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, 0.001, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(1)',
])
S("cancel_entry_order", [
'tid = f"ce-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("entry_hold_exit", [
'tid = f"h-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(3)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("entry_exit_at_loss", [
'tid = f"l-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*1.005, 0.001); await asyncio.sleep(1)',
])
S("two_sequential_cycles", [
'p = float(snap.price)',
't1 = f"2c1-{int(time.time()*1000)}"; t2 = f"2c2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t2, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(1)',
])
S("entry_then_recover", [
'tid = f"r-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'await bundle.runtime.disconnect()',
'await bundle.runtime.connect(initial_capital=k.account.snapshot.capital)',
'await asyncio.sleep(1)',
])
S("long_entry_exit", [
'tid = f"ln-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "LONG", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "LONG", p*1.005, 0.001); await asyncio.sleep(1)',
])
# --- Cancel combos ---
S("cancel_idempotent", [
'tid = f"ci-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("double_cancel", [
'tid = f"dc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
])
S("cancel_then_exit", [
'tid = f"ctx-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("exit_then_cancel_exit", [
'tid = f"exc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("exit_then_reentry", [
'p = float(snap.price)',
't1 = f"er1-{int(time.time()*1000)}"; t2 = f"er2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.3)',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
])
S("limit_cancel", [
'tid = f"lc-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p*0.9, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p*0.9, 0.001); await asyncio.sleep(1)',
])
# --- X4 expanded ---
S("x4_partial_hold_exit", [
'tid = f"ph-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.003',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.3, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.7, exit_leg_ratios=(0.3,1.0)); await asyncio.sleep(1)',
])
S("x4_three_leg", [
'tid = f"3l-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.004',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.25, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*0.5, exit_leg_ratios=(0.25,0.25,1.0)); await asyncio.sleep(1)',
])
S("x4_cancel_fill_partial", [
'tid = f"cfp-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.002); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, 0.002); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, 0.001); await asyncio.sleep(1)',
])
S("x4_rapid_three", [
'p = float(snap.price)',
'for i in range(3):',
' tid = f"r3-{i}-{int(time.time()*1000)}"',
' _si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p*(1-i*0.005), 0.001); await asyncio.sleep(0.8)',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995*(1-i*0.005), 0.001); await asyncio.sleep(0.8)',
])
S("x4_diff_symbol", [
'tid = f"ds-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'sym2 = "BTCUSDT" if symbol != "BTCUSDT" else "ETHUSDT"',
'_si(k, KernelCommandType.EXIT, tid, sym2, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
])
S("x4_alternating", [
'p = float(snap.price)',
't1 = f"as1-{int(time.time()*1000)}"; t2 = f"as2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'sym2 = "BTCUSDT" if symbol != "BTCUSDT" else "ETHUSDT"',
'try:',
' url = "https://open-api-vst.bingx.com/openApi/swap/v2/quote/price?symbol=" + sym2.replace("USDT","-USDT")',
' p2 = float(json.loads(urllib.request.urlopen(url, timeout=5).read())["data"]["price"])',
'except: p2 = p',
'_si(k, KernelCommandType.ENTER, t2, sym2, "LONG", p2, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, t2, sym2, "LONG", p2*1.005, 0.001); await asyncio.sleep(1)',
])
S("x4_multi_flatten", [
'tid = f"mf-{int(time.time()*1000)}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(1)',
'for i in range(3):',
' if k.slot(0).is_free(): break',
' _flatten_via_kernel_intent(k, symbol, p*0.99, f"mf{i}"); await asyncio.sleep(0.5)',
])
S("x4_three_leg_25_50_25", [
'tid = f"x4a-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.004',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.5, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*0.25, exit_leg_ratios=(0.25,0.5,1.0)); await asyncio.sleep(1)',
])
S("x4_enter_exit_hold_twice", [
'p = float(snap.price)',
't1 = f"x4b1-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t1, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t1, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
't2 = f"x4b2-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t2, symbol, "SHORT", p*0.995, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t2, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(0.5)',
't3 = f"x4b3-{int(time.time()*1000)}"',
'_si(k, KernelCommandType.ENTER, t3, symbol, "SHORT", p*0.99, 0.001); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.EXIT, t3, symbol, "SHORT", p*0.985, 0.001); await asyncio.sleep(0.5)',
])
S("x4_cancel_then_double_exit", [
'tid = f"x4c-{int(time.time()*1000)}"; p = float(snap.price); sz = 0.002',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
'_si(k, KernelCommandType.CANCEL, tid, symbol, "SHORT", p, sz); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
'if not k.slot(0).is_free():',
' _si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.993, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.5)',
])
# --- 2 sides × 2 profit × 4 patterns = 16 ---
for side, side_str, ep in [("short","SHORT",0.995), ("long","LONG",1.005)]:
for prof, pname, xp_mult in [(True,"profit",ep), (False,"loss",1/ep)]:
for pat, pat_suffix, lines in [
("basic", "", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.8)',
]),
("partial", "_partial", [
'sz = 0.002',
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, sz, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{ep}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, sz*0.5, exit_leg_ratios=(0.5,1.0)); await asyncio.sleep(0.8)',
]),
("cancel", "_cancel", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.8)',
]),
("double_exit", "_double_exit", [
f'_si(k, KernelCommandType.ENTER, tid, symbol, "{side_str}", p, 0.001); await asyncio.sleep(0.8)',
f'_si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}, 0.001); await asyncio.sleep(0.3)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side_str}", p*{xp_mult}*0.995, 0.001); await asyncio.sleep(0.5)',
]),
]:
name = f"{pat}_{side}_{pname}"
S(name, [
f'tid = f"{pat[0]}{side[0]}{"p" if prof else "l"}-{{int(time.time()*1000)}}"; p = float(snap.price)',
*lines,
])
# --- Triple sequential × 4 ---
for i in range(4):
side = "SHORT"; ep = 0.995
S(f"triple_seq_{i}", [
'p = float(snap.price)',
'for j in range(3):',
f' tid = f"ts{i}-j-{{int(time.time()*1000)}}"',
f' _si(k, KernelCommandType.ENTER, tid, symbol, "{side}", p*(1-j*0.003), 0.001); await asyncio.sleep(0.7)',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side}", p*{ep}*(1-j*0.003), 0.001); await asyncio.sleep(0.7)',
])
for i in range(4):
side = "LONG"; ep = 1.005
S(f"triple_seq_long_{i}", [
'p = float(snap.price)',
'for j in range(3):',
f' tid = f"tsl{i}-j-{{int(time.time()*1000)}}"',
f' _si(k, KernelCommandType.ENTER, tid, symbol, "{side}", p*(1+j*0.003), 0.001); await asyncio.sleep(0.7)',
f' _si(k, KernelCommandType.EXIT, tid, symbol, "{side}", p*{ep}*(1+j*0.003), 0.001); await asyncio.sleep(0.7)',
])
# --- Cancel+reenter × 4 ---
for i in range(4):
side = "SHORT"
S(f"cancel_reenter_{i}", [
'p = float(snap.price)',
f't1 = f"cr{i}a-{{int(time.time()*1000)}}"; t2 = f"cr{i}b-{{int(time.time()*1000)}}"',
f'_si(k, KernelCommandType.ENTER, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.ENTER, t2, symbol, "{side}", p*0.995, 0.001); await asyncio.sleep(0.8)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, t2, symbol, "{side}", p*0.99, 0.001); await asyncio.sleep(0.5)',
])
for i in range(4):
side = "LONG"
S(f"cancel_reenter_long_{i}", [
'p = float(snap.price)',
f't1 = f"crl{i}a-{{int(time.time()*1000)}}"; t2 = f"crl{i}b-{{int(time.time()*1000)}}"',
f'_si(k, KernelCommandType.ENTER, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.CANCEL, t1, symbol, "{side}", p, 0.001); await asyncio.sleep(0.3)',
f'_si(k, KernelCommandType.ENTER, t2, symbol, "{side}", p*1.005, 0.001); await asyncio.sleep(0.8)',
'if not k.slot(0).is_free():',
f' _si(k, KernelCommandType.EXIT, t2, symbol, "{side}", p*1.01, 0.001); await asyncio.sleep(0.5)',
])
# --- Leg ratios × 8 ---
for i, ratios in enumerate([
(0.1,1.0), (0.33,0.33,1.0), (0.5,0.5,1.0), (0.75,1.0),
(0.2,0.3,0.5,1.0), (0.4,0.6,1.0), (0.15,0.85,1.0), (0.25,0.25,0.5,1.0),
]):
rat_str = ",".join(str(r) for r in ratios)
nlegs = len(ratios)
code = [
f'tid = f"lr{i}-{{int(time.time()*1000)}}"; p = float(snap.price); sz = 0.004',
f'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, sz, exit_leg_ratios=({rat_str})); await asyncio.sleep(1)',
]
for leg in range(nlegs - 1):
r = ratios[leg]
code.append(f'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.995*(1-{leg}*0.002), sz*{r}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)')
r_last = ratios[-1]
code.append(f'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p*0.99, sz*{r_last}, exit_leg_ratios=({rat_str})); await asyncio.sleep(0.8)')
S(f"leg_ratio_{i}", code)
# --- Breakeven × 4 ---
for i in range(4):
S(f"breakeven_{i}", [
f'tid = f"be{i}-{{int(time.time()*1000)}}"; p = float(snap.price)',
'_si(k, KernelCommandType.ENTER, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
'_si(k, KernelCommandType.EXIT, tid, symbol, "SHORT", p, 0.001); await asyncio.sleep(0.8)',
])
# =====================================================================
# Assemble output
# =====================================================================
lines = [PROLOGUE, RUNNER]
lines.append('# =====================================================================')
lines.append('# Scenario body functions')
lines.append('# =====================================================================')
lines.append('')
lines.append('k = None # type: ignore # shorthand alias for bundle.runtime.kernel')
lines.append('')
for name, code_lines in scenarios:
lines.append(f'async def _body_{name}(bundle, client, symbol, snap):')
lines.append(' k = bundle.runtime.kernel')
for cl in code_lines:
lines.append(f' {cl}')
lines.append('')
lines.append('# =====================================================================')
lines.append('# Test functions')
lines.append('# =====================================================================')
lines.append('')
lines.append(
'@pytest.fixture(scope="session")\n'
'def _live_client():\n'
' cfg = _build_bingx_config(25000.0)\n'
' c = BingxHttpClient(cfg)\n'
' yield c\n'
)
for name, _ in scenarios:
lines.append(f'''
def test_pink_ditav2_{name}(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
ic = bundle.runtime.kernel.account.snapshot.capital
result = asyncio.run(_run_scenario(bundle, _live_client, _body_{name}, "{name}", ic))
assert result.positions_flat, f"{name}: {{result.error}}"
''')
lines.append('''
def test_pink_ditav2_open_partial_close_and_flatten(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
outcomes = asyncio.run(_run_pink_live_roundtrip(bundle, _live_client))
e, m, f = outcomes
assert e.accepted or e.diagnostic_code in {KernelDiagnosticCode.OK}, f"Entry not accepted: {e.diagnostic_code}"
slot = bundle.runtime.kernel.slot(0) if bundle.runtime.kernel.max_slots > 0 else None
if slot is not None and not slot.is_free():
pytest.skip(f"Slot not flat (fsm_state={slot.fsm_state})")
def test_pink_ditav2_reconciliation_only_on_explicit_recovery(_live_client) -> None:
bundle = _build_runtime_bundle(25000.0)
recovered = asyncio.run(_run_pink_live_recovery(bundle, _live_client))
assert isinstance(recovered, dict), f"Expected dict, got {type(recovered)}"
assert recovered.get("capital", 0) > 0, "Expected positive capital after recovery"
''')
full = '\n'.join(lines)
try:
ast.parse(full)
test_count = full.count("def test_pink_ditav2_")
print(f"Syntax OK — {test_count} tests, {len(full)} chars")
with open(OUT, 'w') as f:
f.write(full)
print(f"Written to {OUT}")
print(f"Breakdown: {len(scenarios)} scenarios + 2 legacy = {test_count} total tests")
except SyntaxError as e:
print(f"Syntax error line {e.lineno}: {e.msg}")
fl = full.split('\n')
for i in range(max(0,e.lineno-5), min(len(fl), e.lineno+3)):
print(f" {i+1}: {fl[i]}")

View File

@@ -1,67 +0,0 @@
from __future__ import annotations
import json
from typing import Any, Protocol
from .contracts import KernelTransition, TradeSlot
from .control import KernelControlSnapshot
from .journal import _transition_row
from .projection import build_position_state_row
from .utils import json_safe
class HazelcastClientLike(Protocol):
def get_map(self, name: str): ...
def get_topic(self, name: str): ...
class HazelcastProjector:
"""Durable BLUE/PINK-compatible projection mirror."""
def __init__(
self,
client: HazelcastClientLike | None = None,
*,
active_slots_map: str = "dita_active_slots",
events_topic: str = "dita_trade_events",
) -> None:
self.client = client
self.active_slots_map = active_slots_map
self.events_topic = events_topic
def publish_slot(self, slot: TradeSlot) -> None:
if self.client is None:
return
self.client.get_map(self.active_slots_map).put(slot.trade_id, build_position_state_row(slot))
def publish_event(self, event_type: str, payload: dict[str, Any]) -> None:
if self.client is None:
return
topic = self.client.get_topic(self.events_topic)
topic.publish(
json.dumps(
{"event_type": event_type, "payload": json_safe(payload)},
ensure_ascii=False,
sort_keys=True,
default=str,
)
)
class HazelcastRowWriter:
"""Callback bridge for ``HazelcastProjection`` writer hooks."""
def __init__(self, client: HazelcastClientLike) -> None:
self.client = client
def __call__(self, name: str, row: dict[str, Any]) -> None:
if name.endswith("trade_events"):
self.client.get_topic(name).publish(
json.dumps(row, ensure_ascii=False, sort_keys=True, default=str)
)
return
if name.endswith("control"):
key = "control"
else:
key = str(row.get("trade_id", row.get("slot_id", row.get("event_id", ""))))
self.client.get_map(name).put(key, json_safe(row))

View File

@@ -1,102 +0,0 @@
"""Debug journaling surfaces for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Callable, Dict, List, Optional, Protocol
from .contracts import KernelTransition, TradeSlot, TradeStage, VenueEvent
from .control import KernelControlSnapshot
from .utils import json_safe, json_text
JournalSink = Callable[[str, Dict[str, Any]], None]
class KernelJournal(Protocol):
"""Append-only debug journal interface."""
def record(self, row: Dict[str, Any]) -> None:
...
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
...
@dataclass
class MemoryKernelJournal:
"""In-memory journal used in tests."""
rows: List[Dict[str, Any]] = field(default_factory=list)
capture_limit: int = 10_000
def record(self, row: Dict[str, Any]) -> None:
if len(self.rows) < self.capture_limit:
self.rows.append(dict(row))
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
row = _transition_row(transition=transition, slot=slot, event=event, control=control)
self.record(row)
class ClickHouseKernelJournal:
"""Fire-and-forget ClickHouse journal.
The sink is a small callable of the form ``sink(table_name, row_dict)``.
"""
def __init__(self, sink: Optional[JournalSink] = None):
self.sink = sink
def record(self, row: Dict[str, Any]) -> None:
if self.sink is not None:
self.sink("dita_kernel_debug", row)
def record_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> None:
self.record(_transition_row(transition=transition, slot=slot, event=event, control=control))
def _transition_row(
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent],
control: Optional[KernelControlSnapshot],
) -> Dict[str, Any]:
return {
"ts": transition.timestamp.isoformat() if hasattr(transition.timestamp, "isoformat") else str(transition.timestamp),
"trade_id": transition.trade_id,
"slot_id": transition.slot_id,
"prev_state": transition.prev_state.value,
"next_state": transition.next_state.value,
"trigger": transition.trigger,
"intent_id": transition.intent_id,
"event_id": transition.event_id,
"control_mode": transition.control_mode,
"control_verbosity": transition.control_verbosity,
"slot_state": slot.to_dict(),
"event_payload": json_safe(event) if event is not None else {},
"control_snapshot": control.as_dict() if control is not None else {},
"slot_state_json": json_text(slot.to_dict()),
}

View File

@@ -1,8 +0,0 @@
"""Compatibility shim for the Rust-backed DITAv2 execution kernel."""
from __future__ import annotations
from .rust_backend import ExecutionKernel
__all__ = ["ExecutionKernel"]

View File

@@ -1,350 +0,0 @@
"""Operator-facing bootstrap helpers for DITAv2.
This module keeps the wiring explicit:
- control plane selection
- Zinc plane selection
- projection sink selection
- venue adapter selection
The defaults stay safe and testable. Real shared-memory or live BingX wiring
is only enabled when the caller opts in via arguments or environment.
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
import asyncio
import inspect
import os
from pathlib import Path
from typing import Any, Optional
from dotenv import load_dotenv
from prod.bingx.config import BingxExecClientConfig
from prod.bingx.config import BingxInstrumentProviderConfig
from prod.bingx.enums import BingxEnvironment
from .bingx_venue import BingxVenueAdapter
from .control import BackendMode
from .control import ControlPlane
from .control import ControlUpdate
from .control import KernelControlSnapshot
from .control import KernelMode
from .control import KernelVerbosity
from .control import build_control_plane
from .mock_venue import MockVenueAdapter
from .mock_venue import MockVenueScenario
from .projection import HazelcastProjection
from .projection import build_projection
from .real_control_plane import RealZincControlPlane
from .real_control_plane import RealZincUnavailable
from .real_zinc_plane import RealZincPlane
from .real_zinc_plane import RealZincUnavailable as RealZincPlaneUnavailable
from .rust_backend import ExecutionKernel
from .venue import VenueAdapter
from .zinc_plane import InMemoryZincPlane
from .zinc_plane import ZincPlane
PROJECT_ROOT = Path(__file__).resolve().parents[3]
load_dotenv(PROJECT_ROOT / ".env")
class LauncherVenueMode(str, Enum):
MOCK = "MOCK"
BINGX = "BINGX"
class LauncherZincMode(str, Enum):
IN_MEMORY = "IN_MEMORY"
REAL = "REAL"
@dataclass
class DITAv2LauncherBundle:
"""Concrete runtime components assembled by the launcher."""
kernel: ExecutionKernel
control_plane: ControlPlane
projection: HazelcastProjection
zinc_plane: ZincPlane
venue: VenueAdapter
def close(self) -> None:
_maybe_close(self.venue)
_maybe_close(self.zinc_plane)
_maybe_close(self.control_plane)
def _env_upper(name: str, default: str = "") -> str:
return str(os.environ.get(name, default)).strip().upper()
def _env_bool(name: str, default: bool = False) -> bool:
raw = os.environ.get(name)
if raw is None:
return default
return str(raw).strip().lower() in {"1", "true", "yes", "on"}
def _resolve_control_mode() -> KernelMode | None:
raw = _env_upper("DITA_V2_MODE", "")
if raw == KernelMode.DEBUG.value:
return KernelMode.DEBUG
if raw == KernelMode.NORMAL.value:
return KernelMode.NORMAL
return None
def _resolve_control_verbosity() -> KernelVerbosity | None:
raw = _env_upper("DITA_V2_VERBOSITY", "")
if raw == KernelVerbosity.TRACE.value:
return KernelVerbosity.TRACE
if raw == KernelVerbosity.VERBOSE.value:
return KernelVerbosity.VERBOSE
if raw == KernelVerbosity.QUIET.value:
return KernelVerbosity.QUIET
return None
def _resolve_backend_mode() -> BackendMode | None:
raw = _env_upper("DITA_V2_BACKEND_MODE", "")
if raw == BackendMode.BINGX.value:
return BackendMode.BINGX
if raw == BackendMode.MOCK.value:
return BackendMode.MOCK
return None
def _control_update_from_env() -> ControlUpdate | None:
fields: dict[str, Any] = {}
mode = _resolve_control_mode()
if mode is not None:
fields["mode"] = mode
verbosity = _resolve_control_verbosity()
if verbosity is not None:
fields["verbosity"] = verbosity
backend_mode = _resolve_backend_mode()
if backend_mode is not None:
fields["backend_mode"] = backend_mode
raw = os.environ.get("DITA_V2_DEBUG_CLICKHOUSE")
if raw is not None:
fields["debug_clickhouse_enabled"] = _env_bool("DITA_V2_DEBUG_CLICKHOUSE", True)
raw = os.environ.get("DITA_V2_TRACE_TRANSITIONS")
if raw is not None:
fields["trace_transitions"] = _env_bool("DITA_V2_TRACE_TRANSITIONS", False)
raw = os.environ.get("DITA_V2_MIRROR_TO_HAZELCAST")
if raw is not None:
fields["mirror_to_hazelcast"] = _env_bool("DITA_V2_MIRROR_TO_HAZELCAST", True)
raw = os.environ.get("DITA_V2_ACTIVE_SLOT_LIMIT")
if raw is not None:
try:
fields["active_slot_limit"] = max(1, int(str(raw).strip()))
except Exception:
pass
raw = os.environ.get("DITA_V2_RECONCILE_ON_RESTART")
if raw is not None:
fields["reconcile_on_restart"] = _env_bool("DITA_V2_RECONCILE_ON_RESTART", True)
return ControlUpdate(**fields) if fields else None
def _resolve_venue_mode(venue_mode: Optional[str] = None) -> LauncherVenueMode:
raw = _env_upper("DITA_V2_VENUE", venue_mode or LauncherVenueMode.MOCK.value)
if raw == LauncherVenueMode.BINGX.value:
return LauncherVenueMode.BINGX
return LauncherVenueMode.MOCK
def _resolve_zinc_mode(zinc_mode: Optional[str] = None) -> LauncherZincMode:
raw = _env_upper("DITA_V2_ZINC", zinc_mode or LauncherZincMode.IN_MEMORY.value)
if raw == LauncherZincMode.REAL.value:
return LauncherZincMode.REAL
return LauncherZincMode.IN_MEMORY
def _resolve_hazelcast_real(prefer_real_hazelcast: Optional[bool] = None) -> bool:
if prefer_real_hazelcast is not None:
return bool(prefer_real_hazelcast)
raw = _env_upper("DITA_V2_HAZELCAST", "")
return raw in {"REAL", "REAL_HZ", "HAZELCAST"}
def build_bingx_exec_client_config(
*,
environment: Optional[BingxEnvironment] = None,
allow_mainnet: Optional[bool] = None,
recv_window_ms: Optional[int] = None,
default_leverage: Optional[int] = None,
exchange_leverage_cap: Optional[int] = None,
prefer_websocket: Optional[bool] = None,
sizing_mode: Optional[str] = None,
) -> BingxExecClientConfig:
"""Build the direct BingX config used by the DITAv2 launcher."""
resolved_environment = environment or (
BingxEnvironment.LIVE if _env_upper("DOLPHIN_BINGX_ENV", "VST") == "LIVE" else BingxEnvironment.VST
)
resolved_allow_mainnet = _env_bool("DOLPHIN_BINGX_ALLOW_MAINNET", False) if allow_mainnet is None else bool(allow_mainnet)
resolved_recv_window = int(os.environ.get("DOLPHIN_BINGX_RECV_WINDOW_MS", "5000")) if recv_window_ms is None else int(recv_window_ms)
resolved_default_leverage = int(os.environ.get("DOLPHIN_BINGX_DEFAULT_LEVERAGE", "1")) if default_leverage is None else int(default_leverage)
resolved_exchange_cap = int(os.environ.get("DOLPHIN_BINGX_EXCHANGE_LEVERAGE_CAP", "3")) if exchange_leverage_cap is None else int(exchange_leverage_cap)
resolved_prefer_ws = _env_bool("DOLPHIN_BINGX_PREFER_WEBSOCKET", False) if prefer_websocket is None else bool(prefer_websocket)
resolved_sizing_mode = sizing_mode or os.environ.get("DOLPHIN_BINGX_SIZING_MODE", "testnet")
return BingxExecClientConfig(
api_key=os.environ.get("BINGX_API_KEY"),
secret_key=os.environ.get("BINGX_SECRET_KEY"),
environment=resolved_environment,
allow_mainnet=resolved_allow_mainnet,
recv_window_ms=max(1, resolved_recv_window),
default_leverage=max(1, resolved_default_leverage),
exchange_leverage_cap=max(1, resolved_exchange_cap),
prefer_websocket=resolved_prefer_ws,
sizing_mode=resolved_sizing_mode,
journal_strategy=os.environ.get("DOLPHIN_BINGX_JOURNAL_STRATEGY", "dita_v2"),
journal_db=os.environ.get("DOLPHIN_BINGX_JOURNAL_DB", "dolphin_pink"),
instrument_provider=BingxInstrumentProviderConfig(load_all=True),
)
def _build_control_plane(
*,
prefix: str,
control_plane: Optional[ControlPlane] = None,
) -> ControlPlane:
plane = control_plane or build_control_plane(prefix=prefix)
update = _control_update_from_env()
if update is not None:
plane.update(update)
return plane
def _build_zinc_plane(
*,
prefix: str,
slot_count: int,
zinc_mode: Optional[LauncherZincMode] = None,
zinc_plane: Optional[ZincPlane] = None,
) -> ZincPlane:
if zinc_plane is not None:
return zinc_plane
resolved_mode = zinc_mode or _resolve_zinc_mode()
if resolved_mode is LauncherZincMode.REAL:
try:
return RealZincPlane(prefix=prefix, slot_count=slot_count, create=True)
except (RealZincPlaneUnavailable, RealZincUnavailable, Exception):
pass
return InMemoryZincPlane()
def _build_venue(
*,
venue_mode: Optional[LauncherVenueMode] = None,
mock_scenario: Optional[MockVenueScenario] = None,
bingx_config: Optional[BingxExecClientConfig] = None,
bingx_backend: Optional[Any] = None,
venue: Optional[VenueAdapter] = None,
) -> VenueAdapter:
if venue is not None:
return venue
resolved_mode = venue_mode or _resolve_venue_mode()
if resolved_mode is LauncherVenueMode.BINGX:
backend = bingx_backend
if backend is None:
from prod.clean_arch.adapters.bingx_direct import BingxDirectExecutionAdapter
backend = BingxDirectExecutionAdapter(bingx_config or build_bingx_exec_client_config())
return BingxVenueAdapter(backend=backend)
return MockVenueAdapter(mock_scenario)
def _maybe_close(obj: Any) -> None:
for method_name in ("close", "disconnect"):
method = getattr(obj, method_name, None)
if method is None:
continue
try:
result = method()
except TypeError:
continue
if inspect.isawaitable(result):
try:
asyncio.run(result)
except RuntimeError:
pass
break
def build_launcher_bundle(
*,
max_slots: int = 10,
prefix: Optional[str] = None,
control_plane: Optional[ControlPlane] = None,
projection: Optional[HazelcastProjection] = None,
projection_client: Optional[Any] = None,
zinc_plane: Optional[ZincPlane] = None,
venue: Optional[VenueAdapter] = None,
venue_mode: Optional[LauncherVenueMode | str] = None,
zinc_mode: Optional[LauncherZincMode | str] = None,
bingx_config: Optional[BingxExecClientConfig] = None,
bingx_backend: Optional[Any] = None,
mock_scenario: Optional[MockVenueScenario] = None,
) -> DITAv2LauncherBundle:
"""Build a fully wired DITAv2 runtime bundle.
Defaults stay non-destructive:
- in-memory Zinc plane
- in-process control plane
- mock venue
- callback projection unless a Hazelcast client is supplied
"""
resolved_prefix = (prefix or os.environ.get("DITA_V2_PREFIX", "dita_v2")).strip() or "dita_v2"
if isinstance(venue_mode, LauncherVenueMode):
resolved_venue_mode = venue_mode
elif isinstance(venue_mode, str):
resolved_venue_mode = LauncherVenueMode(venue_mode.strip().upper())
else:
resolved_venue_mode = None
if isinstance(zinc_mode, LauncherZincMode):
resolved_zinc_mode = zinc_mode
elif isinstance(zinc_mode, str):
resolved_zinc_mode = LauncherZincMode(zinc_mode.strip().upper())
else:
resolved_zinc_mode = None
active_control_plane = _build_control_plane(prefix=resolved_prefix, control_plane=control_plane)
control_snapshot = active_control_plane.read()
active_projection = projection or build_projection(
client=projection_client,
prefer_real_hazelcast=_resolve_hazelcast_real(),
control_snapshot=control_snapshot,
)
active_zinc_plane = _build_zinc_plane(
prefix=resolved_prefix,
slot_count=int(max_slots),
zinc_mode=resolved_zinc_mode,
zinc_plane=zinc_plane,
)
active_venue = _build_venue(
venue_mode=resolved_venue_mode,
mock_scenario=mock_scenario,
bingx_config=bingx_config,
bingx_backend=bingx_backend,
venue=venue,
)
kernel = ExecutionKernel(
max_slots=int(max_slots),
control_plane=active_control_plane,
venue=active_venue,
projection=active_projection,
projection_client=projection_client,
zinc_plane=active_zinc_plane,
)
return DITAv2LauncherBundle(
kernel=kernel,
control_plane=active_control_plane,
projection=active_projection,
zinc_plane=active_zinc_plane,
venue=active_venue,
)

View File

@@ -1,209 +0,0 @@
"""Deterministic mock venue for DITAv2 tests."""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional
import itertools
from .contracts import (
KernelCommandType,
KernelEventKind,
KernelIntent,
TradeSide,
VenueEvent,
VenueEventStatus,
VenueOrder,
VenueOrderStatus,
)
from .venue import VenueAdapter
@dataclass(frozen=True)
class MockVenueScenario:
"""Failure knobs for the mock venue."""
reject_entries: bool = False
reject_exits: bool = False
partial_fill_ratio: float = 1.0
cancel_reject: bool = False
emit_ack_before_fill: bool = True
emit_fill_on_submit: bool = False
entry_partial_fill_ratio: float = 1.0
exit_partial_fill_ratio: float = 1.0
class MockVenueAdapter(VenueAdapter):
"""Scriptable mock venue with BingX-shaped response semantics."""
def __init__(self, scenario: Optional[MockVenueScenario] = None):
self.scenario = scenario or MockVenueScenario()
self._order_seq = itertools.count(1)
self._event_seq = itertools.count(1)
self._open_orders: Dict[str, VenueOrder] = {}
self._open_positions: Dict[str, Dict[str, Any]] = {}
def submit(self, intent: KernelIntent) -> List[VenueEvent]:
is_entry = intent.action == KernelCommandType.ENTER
should_reject = self.scenario.reject_entries if is_entry else self.scenario.reject_exits
order_id = f"V-{next(self._order_seq):08d}"
client_id = f"{intent.trade_id}:{intent.intent_id}"
order = VenueOrder(
internal_trade_id=intent.trade_id,
venue_order_id=order_id,
venue_client_id=client_id,
side=intent.side,
intended_size=float(intent.target_size),
status=VenueOrderStatus.NEW,
metadata={"intent_id": intent.intent_id, "action": intent.action.value, "slot_id": intent.slot_id, "asset": intent.asset},
)
if should_reject:
order = VenueOrder(
internal_trade_id=order.internal_trade_id,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
intended_size=order.intended_size,
filled_size=0.0,
average_fill_price=0.0,
status=VenueOrderStatus.REJECTED,
metadata=dict(order.metadata),
)
return [self._event_from_order(intent, order, KernelEventKind.ORDER_REJECT, VenueEventStatus.REJECTED, reason="MOCK_REJECT")]
self._open_orders[order_id] = order
events: List[VenueEvent] = []
if self.scenario.emit_ack_before_fill or not self.scenario.emit_fill_on_submit:
events.append(self._event_from_order(intent, order, KernelEventKind.ORDER_ACK, VenueEventStatus.ACKED))
if self.scenario.emit_fill_on_submit or self.scenario.partial_fill_ratio > 0:
if is_entry:
effective_ratio = self.scenario.entry_partial_fill_ratio if self.scenario.entry_partial_fill_ratio != 1.0 else self.scenario.partial_fill_ratio
else:
effective_ratio = self.scenario.exit_partial_fill_ratio if self.scenario.exit_partial_fill_ratio != 1.0 else self.scenario.partial_fill_ratio
fill_ratio = max(0.0, min(1.0, float(effective_ratio)))
fill_size = float(intent.target_size) * fill_ratio
event_kind = KernelEventKind.FULL_FILL if fill_ratio >= 1.0 else KernelEventKind.PARTIAL_FILL
event_status = VenueEventStatus.FILLED if fill_ratio >= 1.0 else VenueEventStatus.PARTIALLY_FILLED
fill_event = self._event_from_order(
intent,
order,
event_kind,
event_status,
price=float(intent.reference_price or 0.0),
fill_size=fill_size,
remaining_size=max(0.0, float(intent.target_size) - fill_size),
)
events.append(fill_event)
order = VenueOrder(
internal_trade_id=order.internal_trade_id,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
intended_size=order.intended_size,
filled_size=fill_size,
average_fill_price=float(intent.reference_price or 0.0),
status=VenueOrderStatus.FILLED if fill_ratio >= 1.0 else VenueOrderStatus.PARTIALLY_FILLED,
metadata=dict(order.metadata),
)
self._open_orders[order_id] = order
return events
def cancel(self, order: VenueOrder, *, reason: str = "") -> List[VenueEvent]:
if self.scenario.cancel_reject:
return [
self._event_from_order(
self._dummy_intent(order),
order,
KernelEventKind.CANCEL_REJECT,
VenueEventStatus.CANCELED_REJECTED,
reason=reason or "MOCK_CANCEL_REJECT",
)
]
existing = self._open_orders.get(order.venue_order_id, order)
canceled = VenueOrder(
internal_trade_id=existing.internal_trade_id,
venue_order_id=existing.venue_order_id,
venue_client_id=existing.venue_client_id,
side=existing.side,
intended_size=existing.intended_size,
filled_size=existing.filled_size,
average_fill_price=existing.average_fill_price,
status=VenueOrderStatus.CANCELED,
metadata=dict(existing.metadata),
)
self._open_orders.pop(order.venue_order_id, None)
return [
self._event_from_order(
self._dummy_intent(order),
canceled,
KernelEventKind.CANCEL_ACK,
VenueEventStatus.CANCELED,
reason=reason or "MOCK_CANCEL_ACK",
)
]
def open_orders(self) -> List[VenueOrder]:
return list(self._open_orders.values())
def open_positions(self) -> List[Dict[str, Any]]:
return list(self._open_positions.values())
def reconcile(self) -> List[VenueEvent]:
return []
def _dummy_intent(self, order: VenueOrder) -> KernelIntent:
return KernelIntent(
timestamp=datetime.now(timezone.utc),
intent_id=order.venue_client_id,
trade_id=order.internal_trade_id,
slot_id=int(order.metadata.get("slot_id", 0)),
asset=str(order.metadata.get("asset", "")),
side=order.side,
action=KernelCommandType.EXIT if order.metadata.get("action") == "EXIT" else KernelCommandType.ENTER,
reference_price=float(order.metadata.get("reference_price", 0.0)),
target_size=float(order.intended_size),
leverage=float(order.metadata.get("leverage", 1.0)),
reason=str(order.metadata.get("reason", "")),
metadata=dict(order.metadata),
)
def _event_from_order(
self,
intent: KernelIntent,
order: VenueOrder,
kind: KernelEventKind,
status: VenueEventStatus,
*,
price: Optional[float] = None,
fill_size: float = 0.0,
remaining_size: float = 0.0,
reason: str = "",
) -> VenueEvent:
event = VenueEvent(
timestamp=datetime.now(timezone.utc),
event_id=f"EV-{next(self._event_seq):08d}",
trade_id=intent.trade_id,
slot_id=intent.slot_id,
kind=kind,
status=status,
venue_order_id=order.venue_order_id,
venue_client_id=order.venue_client_id,
side=order.side,
asset=intent.asset,
price=float(price if price is not None else intent.reference_price or 0.0),
size=float(intent.target_size),
filled_size=float(fill_size),
remaining_size=float(remaining_size),
reason=reason,
raw_payload={
"status": status.value,
"orderId": order.venue_order_id,
"clientOrderId": order.venue_client_id,
"symbol": intent.asset,
"side": order.side.value,
"action": intent.action.value,
},
metadata={"intent_id": intent.intent_id, "action": intent.action.value},
)
return event

View File

@@ -1,97 +0,0 @@
"""Hazelcast-compatible projection helpers for DITAv2."""
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime
import os
from typing import Any, Callable, Dict, Iterable, List, Optional
from .account import AccountProjection
from .contracts import KernelTransition, TradeSlot, TradeStage, VenueEvent
from .control import KernelControlSnapshot
from .journal import _transition_row
from .utils import json_safe
Writer = Callable[[str, Dict[str, Any]], None]
@dataclass
class HazelcastProjection:
"""Projection helper for BLUE/PINK-compatible durable writes."""
active_slots_map: str = "hz:dita_active_slots"
trade_events_topic: str = "hz:dita_trade_events"
control_map: str = "hz:dita_control"
writer: Optional[Writer] = None
control_snapshot: Optional[KernelControlSnapshot] = None
def write_slot(self, slot: TradeSlot) -> Dict[str, Any]:
row = build_position_state_row(slot, self.control_snapshot)
if self.writer is not None:
self.writer(self.active_slots_map, row)
return row
def write_transition(
self,
*,
transition: KernelTransition,
slot: TradeSlot,
event: Optional[VenueEvent] = None,
control: Optional[KernelControlSnapshot] = None,
) -> Dict[str, Any]:
row = _transition_row(transition=transition, slot=slot, event=event, control=control)
if self.writer is not None:
self.writer(self.trade_events_topic, row)
return row
def write_control(self, control: KernelControlSnapshot) -> Dict[str, Any]:
self.control_snapshot = control
row = control.as_dict()
if self.writer is not None:
self.writer(self.control_map, row)
return row
def build_projection(
*,
writer: Optional[Writer] = None,
client: Optional[Any] = None,
prefer_real_hazelcast: Optional[bool] = None,
control_snapshot: Optional[KernelControlSnapshot] = None,
) -> HazelcastProjection:
"""Build the active projection helper with an operator-visible switch.
The default remains the callback-based projection helper. If a Hazelcast
client is supplied and the caller opts in via ``prefer_real_hazelcast`` or
``DITA_V2_HAZELCAST=REAL``, the helper routes directly through the
client-backed map/topic writer path.
"""
env_choice = os.environ.get("DITA_V2_HAZELCAST", "").strip().upper()
real_requested = prefer_real_hazelcast if prefer_real_hazelcast is not None else env_choice in {"REAL", "REAL_HZ", "HAZELCAST"}
if real_requested and client is not None:
try:
from .hazelcast_projection import HazelcastRowWriter
writer = HazelcastRowWriter(client)
except Exception:
pass
return HazelcastProjection(writer=writer, control_snapshot=control_snapshot)
def build_position_state_row(slot: TradeSlot, control: Optional[KernelControlSnapshot] = None) -> Dict[str, Any]:
"""Build a state row shaped for durable compatibility."""
row = slot.to_dict()
row.update(
{
"runtime_namespace": control.runtime_namespace if control else "dita_v2",
"strategy_namespace": control.strategy_namespace if control else "dita_v2",
"event_namespace": control.event_namespace if control else "dita_v2",
"actor_name": control.actor_name if control else "ExecutionKernel",
"exec_venue": control.exec_venue if control else "bingx",
"data_venue": control.data_venue if control else "binance",
"ledger_authority": control.ledger_authority if control else "exchange",
}
)
return row

View File

@@ -1,129 +0,0 @@
"""Real Zinc-backed control plane for DITAv2."""
from __future__ import annotations
import json
import struct
import sys
from pathlib import Path
from typing import Any, Dict, Optional
from .control import BackendMode, ControlPlane, ControlUpdate, KernelControlSnapshot, KernelMode, KernelVerbosity
_ZINC_ADAPTER_PATH = Path(__file__).resolve().parents[3] / "zinc" / "adapters" / "python"
if _ZINC_ADAPTER_PATH.exists() and str(_ZINC_ADAPTER_PATH) not in sys.path:
sys.path.append(str(_ZINC_ADAPTER_PATH))
try: # pragma: no cover - exercised in integration tests
from zinc import SharedRegion
except Exception as exc: # pragma: no cover
SharedRegion = None # type: ignore[assignment]
_ZINC_IMPORT_ERROR = exc
else:
_ZINC_IMPORT_ERROR = None
class RealZincUnavailable(RuntimeError):
"""Raised when the Zinc Python adapter cannot be loaded."""
def require_real_zinc() -> None:
if SharedRegion is None:
raise RealZincUnavailable(str(_ZINC_IMPORT_ERROR))
def _json_default(value: Any) -> Any:
if hasattr(value, "value"):
return value.value
if hasattr(value, "isoformat"):
try:
return value.isoformat()
except Exception:
pass
if hasattr(value, "__dict__"):
return dict(vars(value))
raise TypeError(f"Unsupported value: {type(value)!r}")
def _encode_packet(seq: int, payload: Dict[str, Any]) -> bytes:
text = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=_json_default, separators=(",", ":")).encode("utf-8")
return struct.pack("!QQ", int(seq), len(text)) + text
def _decode_packet(buf: memoryview) -> Dict[str, Any]:
if len(buf) < 16:
return {}
seq, size = struct.unpack_from("!QQ", buf, 0)
if size <= 0 or size > len(buf) - 16:
return {}
payload = bytes(buf[16 : 16 + size]).decode("utf-8")
out = json.loads(payload)
if isinstance(out, dict):
out["_seq"] = seq
return out
class RealZincControlPlane(ControlPlane):
"""Shared-memory Zinc-backed control plane."""
def __init__(self, *, prefix: str, create: bool = True) -> None:
require_real_zinc()
base = prefix.strip("/").replace("/", "_")
self.region_name = f"{base}_control"
self._seq = 0
self._snapshot = KernelControlSnapshot()
if create:
self.region = SharedRegion.create(self.region_name, 1 << 20)
self._write_region(self._seq, self._snapshot.as_dict())
else:
self.region = SharedRegion.open(self.region_name)
payload = _decode_packet(self.region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if isinstance(control, dict):
self._snapshot = KernelControlSnapshot(**control)
def close(self) -> None:
self.region.close()
def read(self) -> KernelControlSnapshot:
payload = _decode_packet(self.region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if not isinstance(control, dict):
return self._snapshot
self._snapshot = KernelControlSnapshot(**control)
return self._snapshot
def update(self, update: ControlUpdate) -> KernelControlSnapshot:
self._snapshot = update.apply(self.read())
self._seq += 1
self._write_region(self._seq, self._snapshot.as_dict())
return self._snapshot
def mirror(self) -> Dict[str, Any]:
return self._snapshot.as_dict()
def wait(self, timeout_ms: int = 1000) -> bool:
try:
return bool(self.region.wait(timeout_ms))
except Exception:
return False
def notify(self) -> None:
try:
self.region.notify()
except Exception:
pass
def _write_region(self, seq: int, control: Dict[str, Any]) -> None:
packet = _encode_packet(seq, {"control": control})
buf = self.region.as_buffer()
if len(packet) > len(buf):
raise ValueError(f"payload too large for Zinc control region: {len(packet)} > {len(buf)}")
view = memoryview(buf)
view[: len(packet)] = packet
if len(view) > len(packet):
view[len(packet) :] = b"\x00" * (len(view) - len(packet))
try:
self.region.notify()
except Exception:
pass

View File

@@ -1,263 +0,0 @@
"""Real Zinc-backed hot-path plane for DITAv2.
This wrapper uses the Zinc Python adapter directly. The kernel still talks to
the narrow ``ZincPlane`` interface; this module just makes that interface real.
"""
from __future__ import annotations
from dataclasses import asdict
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
import json
import os
import struct
import sys
import threading
from .contracts import KernelIntent, TradeSide, TradeSlot, TradeStage, VenueOrder, VenueOrderStatus
from .control import KernelControlSnapshot
_ZINC_ADAPTER_PATH = Path(__file__).resolve().parents[3] / "zinc" / "adapters" / "python"
if _ZINC_ADAPTER_PATH.exists() and str(_ZINC_ADAPTER_PATH) not in sys.path:
sys.path.append(str(_ZINC_ADAPTER_PATH))
try: # pragma: no cover - exercised in integration tests
from zinc import SharedRegion
except Exception as exc: # pragma: no cover
SharedRegion = None # type: ignore[assignment]
_ZINC_IMPORT_ERROR = exc
else:
_ZINC_IMPORT_ERROR = None
class RealZincUnavailable(RuntimeError):
"""Raised when the Zinc Python adapter cannot be loaded."""
def require_real_zinc() -> None:
if SharedRegion is None:
raise RealZincUnavailable(str(_ZINC_IMPORT_ERROR))
def _json_default(value: Any) -> Any:
if hasattr(value, "value"):
return value.value
if hasattr(value, "isoformat"):
try:
return value.isoformat()
except Exception:
pass
if hasattr(value, "__dict__"):
return dict(vars(value))
raise TypeError(f"Unsupported value: {type(value)!r}")
def _slot_to_payload(slot: TradeSlot) -> Dict[str, Any]:
data = slot.to_dict()
return data
def _slot_from_payload(payload: Dict[str, Any]) -> TradeSlot:
active_entry_order = None
active_exit_order = None
if isinstance(payload.get("active_entry_order"), dict):
active_entry_order = VenueOrder(
internal_trade_id=str(payload.get("trade_id", "")),
venue_order_id=str(payload["active_entry_order"].get("venue_order_id", "")),
venue_client_id=str(payload["active_entry_order"].get("venue_client_id", "")),
side=TradeSide(str(payload["active_entry_order"].get("side", TradeSide.FLAT.value))),
intended_size=float(payload["active_entry_order"].get("intended_size", payload.get("size", 0.0))),
filled_size=float(payload["active_entry_order"].get("filled_size", 0.0)),
average_fill_price=float(payload["active_entry_order"].get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload["active_entry_order"].get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload["active_entry_order"].get("metadata", {})),
)
if isinstance(payload.get("active_exit_order"), dict):
active_exit_order = VenueOrder(
internal_trade_id=str(payload.get("trade_id", "")),
venue_order_id=str(payload["active_exit_order"].get("venue_order_id", "")),
venue_client_id=str(payload["active_exit_order"].get("venue_client_id", "")),
side=TradeSide(str(payload["active_exit_order"].get("side", TradeSide.FLAT.value))),
intended_size=float(payload["active_exit_order"].get("intended_size", payload.get("size", 0.0))),
filled_size=float(payload["active_exit_order"].get("filled_size", 0.0)),
average_fill_price=float(payload["active_exit_order"].get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload["active_exit_order"].get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload["active_exit_order"].get("metadata", {})),
)
slot = TradeSlot(
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
asset=str(payload.get("asset", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
entry_price=float(payload.get("entry_price", 0.0)),
size=float(payload.get("size", 0.0)),
initial_size=float(payload.get("initial_size", 0.0)),
leverage=float(payload.get("leverage", 0.0)),
entry_time=datetime.fromisoformat(payload["entry_time"]) if payload.get("entry_time") else None,
unrealized_pnl=float(payload.get("unrealized_pnl", 0.0)),
realized_pnl=float(payload.get("realized_pnl", 0.0)),
closed=bool(payload.get("closed", False)),
exit_leg_ratios=tuple(float(r) for r in payload.get("exit_leg_ratios", (1.0,))),
active_leg_index=int(payload.get("active_leg_index", 0)),
active_exit_order=active_exit_order,
active_entry_order=active_entry_order,
fsm_state=TradeStage(str(payload.get("fsm_state", TradeStage.IDLE.value))),
close_reason=str(payload.get("close_reason", "")),
last_event_time=datetime.fromisoformat(payload["last_event_time"]) if payload.get("last_event_time") else None,
seen_event_ids=tuple(str(event_id) for event_id in payload.get("seen_event_ids", ())),
metadata=dict(payload.get("metadata", {})),
)
return slot
def _encode_packet(seq: int, payload: Dict[str, Any]) -> bytes:
text = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=_json_default, separators=(",", ":")).encode("utf-8")
return struct.pack("!QQ", int(seq), len(text)) + text
def _decode_packet(buf: memoryview) -> Dict[str, Any]:
if len(buf) < 16:
return {}
seq, size = struct.unpack_from("!QQ", buf, 0)
if size <= 0 or size > len(buf) - 16:
return {}
payload = bytes(buf[16 : 16 + size]).decode("utf-8")
out = json.loads(payload)
if isinstance(out, dict):
out["_seq"] = seq
return out
class RealZincPlane:
"""Shared-memory Zinc plane used by the Python prototype."""
def __init__(
self,
*,
prefix: str,
slot_count: int = 10,
intent_capacity: int = 1 << 20,
state_capacity: int = 1 << 20,
control_capacity: int = 1 << 20,
create: bool = True,
) -> None:
require_real_zinc()
base = prefix.strip("/").replace("/", "_")
self.intent_name = f"{base}_intent"
self.state_name = f"{base}_state"
self.control_name = f"{base}_control"
self._intent_seq = 0
self._state_seq = 0
self._control_seq = 0
self._lock = threading.Lock()
self._slot_cache: Dict[int, TradeSlot] = {i: TradeSlot(slot_id=i) for i in range(int(slot_count))}
self._slot_count = int(slot_count)
self._intent_cache: List[Dict[str, Any]] = []
self._control_cache = KernelControlSnapshot()
if create:
self.intent_region = SharedRegion.create(self.intent_name, intent_capacity)
self.state_region = SharedRegion.create(self.state_name, state_capacity)
self.control_region = SharedRegion.create(self.control_name, control_capacity)
self._write_region(self.control_region, self._control_seq, {"control": self._control_cache.as_dict()})
self._write_region(
self.state_region,
self._state_seq,
{"slots": [self._slot_cache[key].to_dict() for key in range(self._slot_count)]},
)
self._write_region(self.intent_region, self._intent_seq, {"items": []})
else:
self.intent_region = SharedRegion.open(self.intent_name)
self.state_region = SharedRegion.open(self.state_name)
self.control_region = SharedRegion.open(self.control_name)
control_payload = _decode_packet(self.control_region.as_buffer())
state_payload = _decode_packet(self.state_region.as_buffer())
intent_payload = _decode_packet(self.intent_region.as_buffer())
if isinstance(control_payload.get("control"), dict):
self._control_cache = KernelControlSnapshot(**control_payload["control"])
if isinstance(state_payload.get("slots"), list):
for slot_payload in state_payload["slots"]:
if isinstance(slot_payload, dict):
slot = _slot_from_payload(slot_payload)
self._slot_cache[int(slot.slot_id)] = slot
if isinstance(intent_payload.get("items"), list):
self._intent_cache = list(intent_payload["items"])
def close(self) -> None:
self.intent_region.close()
self.state_region.close()
self.control_region.close()
def publish_intent(self, intent: KernelIntent) -> None:
with self._lock:
self._intent_seq += 1
row = intent.__dict__.copy()
row["timestamp"] = intent.timestamp.isoformat()
row["side"] = intent.side.value
row["action"] = intent.action.value
row["stage"] = intent.stage.value
row["exit_leg_ratios"] = list(intent.exit_leg_ratios)
row["metadata"] = json.loads(json.dumps(intent.metadata, default=_json_default))
self._intent_cache.append(row)
self._write_region(self.intent_region, self._intent_seq, {"items": self._intent_cache[-512:]})
def write_slot(self, slot: TradeSlot) -> None:
with self._lock:
self._state_seq += 1
self._slot_cache[int(slot.slot_id)] = slot
payload = {
"slots": [self._slot_cache[key].to_dict() for key in range(self._slot_count)],
}
self._write_region(self.state_region, self._state_seq, payload)
def read_slots(self) -> List[TradeSlot]:
payload = _decode_packet(self.state_region.as_buffer())
slots = payload.get("slots", []) if isinstance(payload, dict) else []
return [_slot_from_payload(slot) for slot in sorted(slots, key=lambda row: int(row.get("slot_id", 0)))]
def read_intents(self) -> List[Dict[str, Any]]:
payload = _decode_packet(self.intent_region.as_buffer())
items = payload.get("items", []) if isinstance(payload, dict) else []
return list(items)
def update_control(self, control: KernelControlSnapshot) -> None:
with self._lock:
self._control_seq += 1
self._control_cache = control
self._write_region(self.control_region, self._control_seq, {"control": control.as_dict()})
def read_control(self) -> KernelControlSnapshot:
payload = _decode_packet(self.control_region.as_buffer())
control = payload.get("control") if isinstance(payload, dict) else None
if not isinstance(control, dict):
return self._control_cache
return KernelControlSnapshot(**control)
def wait_on_state(self, timeout_ms: int = 1000) -> bool:
return bool(self.state_region.wait(timeout_ms))
def notify_state(self) -> None:
self.state_region.notify()
def wait_on_control(self, timeout_ms: int = 1000) -> bool:
return bool(self.control_region.wait(timeout_ms))
def notify_control(self) -> None:
self.control_region.notify()
def wait_on_intent(self, timeout_ms: int = 1000) -> bool:
return bool(self.intent_region.wait(timeout_ms))
def notify_intent(self) -> None:
self.intent_region.notify()
def _write_region(self, region: Any, seq: int, payload: Dict[str, Any]) -> None:
packet = _encode_packet(seq, payload)
buf = region.as_buffer()
if len(packet) > len(buf):
raise ValueError(f"payload too large for Zinc region: {len(packet)} > {len(buf)}")
view = memoryview(buf)
view[:] = b"\x00" * len(view)
view[: len(packet)] = packet
region.notify()

View File

@@ -1,753 +0,0 @@
"""Rust-backed DITAv2 execution kernel.
This module keeps the Python API shape stable while moving the kernel state
machine into a Rust shared library. Slot views write through to the backend on
assignment, then the Python side mirrors the resulting state into Zinc and the
existing projections/journals.
"""
from __future__ import annotations
from dataclasses import asdict
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Sequence
import ctypes
import json
import math
import os
import subprocess
import sys
from .account import AccountProjection
from .control import ControlPlane, ControlUpdate, KernelControlSnapshot, KernelVerbosity, build_control_plane
from .contracts import (
KernelCommandType,
KernelDiagnosticCode,
KernelEventKind,
KernelIntent,
KernelOutcome,
KernelSeverity,
KernelTransition,
TradeSide,
TradeSlot,
TradeStage,
VenueEvent,
VenueOrder,
VenueOrderStatus,
VenueEventStatus,
)
from .journal import KernelJournal, MemoryKernelJournal
from .mock_venue import MockVenueAdapter
from .projection import HazelcastProjection
from .projection import build_projection
from .utils import json_safe
from .venue import VenueAdapter
from .zinc_plane import InMemoryZincPlane, ZincPlane
def _repo_root() -> Path:
return Path(__file__).resolve().parents[3]
def _crate_dir() -> Path:
return Path(__file__).resolve().with_name("_rust_kernel")
def _library_path() -> Path:
if sys.platform == "darwin":
name = "libdita_v2_kernel.dylib"
elif os.name == "nt":
name = "dita_v2_kernel.dll"
else:
name = "libdita_v2_kernel.so"
return _crate_dir() / "target" / "release" / name
def _build_library() -> None:
crate_dir = _crate_dir()
if not crate_dir.exists():
raise FileNotFoundError(f"Missing Rust kernel crate: {crate_dir}")
subprocess.run(
["cargo", "build", "--release", "--manifest-path", str(crate_dir / "Cargo.toml")],
cwd=_repo_root(),
check=True,
)
def _ensure_library() -> Path:
path = _library_path()
if not path.exists():
_build_library()
return path
class _RustKernelLib:
def __init__(self) -> None:
path = _ensure_library()
self.lib = ctypes.CDLL(str(path))
self.lib.dita_kernel_create.argtypes = [ctypes.c_size_t]
self.lib.dita_kernel_create.restype = ctypes.c_void_p
self.lib.dita_kernel_destroy.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_destroy.restype = None
self.lib.dita_kernel_free_string.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_free_string.restype = None
self.lib.dita_kernel_get_slot_json.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
self.lib.dita_kernel_get_slot_json.restype = ctypes.c_void_p
self.lib.dita_kernel_set_slot_json.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_char_p]
self.lib.dita_kernel_set_slot_json.restype = ctypes.c_int
self.lib.dita_kernel_process_intent_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_process_intent_json.restype = ctypes.c_void_p
self.lib.dita_kernel_on_venue_event_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_on_venue_event_json.restype = ctypes.c_void_p
self.lib.dita_kernel_reconcile_slots_json.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_char_p,
]
self.lib.dita_kernel_reconcile_slots_json.restype = ctypes.c_void_p
self.lib.dita_kernel_snapshot_json.argtypes = [ctypes.c_void_p]
self.lib.dita_kernel_snapshot_json.restype = ctypes.c_void_p
def create(self, max_slots: int) -> ctypes.c_void_p:
handle = self.lib.dita_kernel_create(ctypes.c_size_t(max_slots))
if not handle:
raise RuntimeError("dita_kernel_create failed")
return ctypes.c_void_p(handle)
def destroy(self, handle: ctypes.c_void_p) -> None:
if handle and handle.value:
self.lib.dita_kernel_destroy(handle)
def _take_string(self, raw: ctypes.c_void_p) -> str:
if not raw:
raise RuntimeError("Rust kernel returned null string")
text = ctypes.cast(raw, ctypes.c_char_p).value
if text is None:
self.lib.dita_kernel_free_string(raw)
raise RuntimeError("Rust kernel returned empty string")
try:
return text.decode("utf-8")
finally:
self.lib.dita_kernel_free_string(raw)
def get_slot_json(self, handle: ctypes.c_void_p, slot_id: int) -> Dict[str, Any]:
raw = self.lib.dita_kernel_get_slot_json(handle, ctypes.c_size_t(slot_id))
if not raw:
raise IndexError(f"Invalid slot id: {slot_id}")
return json.loads(self._take_string(raw))
def set_slot_json(self, handle: ctypes.c_void_p, slot_id: int, payload: Dict[str, Any]) -> None:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
rc = self.lib.dita_kernel_set_slot_json(handle, ctypes.c_size_t(slot_id), ctypes.c_char_p(encoded))
if rc != 0:
raise RuntimeError(f"dita_kernel_set_slot_json failed rc={rc}")
def process_intent(
self,
handle: ctypes.c_void_p,
payload: Dict[str, Any],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_process_intent_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def on_venue_event(
self,
handle: ctypes.c_void_p,
payload: Dict[str, Any],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(payload), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_on_venue_event_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def reconcile_slots(
self,
handle: ctypes.c_void_p,
payload: Sequence[Dict[str, Any]],
*,
mode: str,
verbosity: str,
) -> Dict[str, Any]:
encoded = json.dumps(json_safe(list(payload)), separators=(",", ":"), ensure_ascii=False).encode("utf-8")
raw = self.lib.dita_kernel_reconcile_slots_json(
handle,
ctypes.c_char_p(encoded),
ctypes.c_char_p(mode.encode("utf-8")),
ctypes.c_char_p(verbosity.encode("utf-8")),
)
return json.loads(self._take_string(raw))
def snapshot(self, handle: ctypes.c_void_p) -> Dict[str, Any]:
raw = self.lib.dita_kernel_snapshot_json(handle)
return json.loads(self._take_string(raw))
_RUST: _RustKernelLib | None = None # lazy init — avoids Rust build on import
def _get_rust() -> _RustKernelLib:
global _RUST
if _RUST is None:
_RUST = _RustKernelLib()
return _RUST
def _slot_to_payload(slot: TradeSlot) -> Dict[str, Any]:
return slot.to_dict()
def _order_to_payload(order: Optional[VenueOrder]) -> Optional[Dict[str, Any]]:
if order is None:
return None
return {
"internal_trade_id": order.internal_trade_id,
"venue_order_id": order.venue_order_id,
"venue_client_id": order.venue_client_id,
"side": order.side.value,
"intended_size": float(order.intended_size or 0.0),
"filled_size": float(order.filled_size or 0.0),
"average_fill_price": float(order.average_fill_price or 0.0),
"status": order.status.value,
"metadata": dict(order.metadata),
}
def _order_from_payload(payload: Optional[Dict[str, Any]], *, trade_id: str) -> Optional[VenueOrder]:
if not isinstance(payload, dict):
return None
return VenueOrder(
internal_trade_id=trade_id,
venue_order_id=str(payload.get("venue_order_id", "")),
venue_client_id=str(payload.get("venue_client_id", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
intended_size=float(payload.get("intended_size", 0.0)),
filled_size=float(payload.get("filled_size", 0.0)),
average_fill_price=float(payload.get("average_fill_price", 0.0)),
status=VenueOrderStatus(str(payload.get("status", VenueOrderStatus.NEW.value))),
metadata=dict(payload.get("metadata", {})),
)
def _slot_from_payload(payload: Dict[str, Any]) -> TradeSlot:
return TradeSlot(
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
asset=str(payload.get("asset", "")),
side=TradeSide(str(payload.get("side", TradeSide.FLAT.value))),
entry_price=float(payload.get("entry_price", 0.0)),
size=float(payload.get("size", 0.0)),
initial_size=float(payload.get("initial_size", 0.0)),
leverage=float(payload.get("leverage", 0.0)),
entry_time=datetime.fromisoformat(payload["entry_time"]) if payload.get("entry_time") else None,
unrealized_pnl=float(payload.get("unrealized_pnl", 0.0)),
realized_pnl=float(payload.get("realized_pnl", 0.0)),
closed=bool(payload.get("closed", False)),
exit_leg_ratios=tuple(float(r) for r in payload.get("exit_leg_ratios", (1.0,))),
active_leg_index=int(payload.get("active_leg_index", 0)),
active_exit_order=_order_from_payload(payload.get("active_exit_order"), trade_id=str(payload.get("trade_id", ""))),
active_entry_order=_order_from_payload(payload.get("active_entry_order"), trade_id=str(payload.get("trade_id", ""))),
fsm_state=TradeStage(str(payload.get("fsm_state", TradeStage.IDLE.value))),
close_reason=str(payload.get("close_reason", "")),
last_event_time=datetime.fromisoformat(payload["last_event_time"]) if payload.get("last_event_time") else None,
seen_event_ids=tuple(str(event_id) for event_id in payload.get("seen_event_ids", ())),
metadata=dict(payload.get("metadata", {})),
)
def _first_invalid_intent_field(intent: KernelIntent) -> Optional[tuple[str, float]]:
"""Return (field, value) for the first non-finite or out-of-bounds numeric
field on an intent, or None if all are sane. Guards the kernel boundary
against inf/NaN that would otherwise crash serde_json serialization."""
scalar_checks = (
("target_size", float(intent.target_size if intent.target_size is not None else 0.0)),
("reference_price", float(intent.reference_price if intent.reference_price is not None else 0.0)),
("leverage", float(intent.leverage if intent.leverage is not None else 0.0)),
("limit_price", float(getattr(intent, "limit_price", 0.0) or 0.0)),
)
for name, value in scalar_checks:
if not math.isfinite(value):
return (name, value)
for idx, ratio in enumerate(intent.exit_leg_ratios or ()): # type: ignore[union-attr]
rv = float(ratio if ratio is not None else 0.0)
if not math.isfinite(rv):
return (f"exit_leg_ratios[{idx}]", rv)
size = float(intent.target_size if intent.target_size is not None else 0.0)
if size < 0.0:
return ("target_size", size)
return None
def _intent_to_payload(intent: KernelIntent) -> Dict[str, Any]:
return {
"timestamp": intent.timestamp.isoformat() if hasattr(intent.timestamp, "isoformat") else str(intent.timestamp),
"intent_id": intent.intent_id,
"trade_id": intent.trade_id,
"slot_id": intent.slot_id,
"asset": intent.asset,
"side": intent.side.value,
"action": intent.action.value,
"reference_price": float(intent.reference_price or 0.0),
"target_size": float(intent.target_size or 0.0),
"leverage": float(intent.leverage or 0.0),
"exit_leg_ratios": list(intent.exit_leg_ratios),
"reason": intent.reason,
"metadata": dict(intent.metadata),
"stage": intent.stage.value,
"order_type": getattr(intent, "order_type", "MARKET"),
"limit_price": float(getattr(intent, "limit_price", 0.0) or 0.0),
}
def _event_to_payload(event: VenueEvent) -> Dict[str, Any]:
return {
"timestamp": event.timestamp.isoformat() if hasattr(event.timestamp, "isoformat") else str(event.timestamp),
"event_id": event.event_id,
"trade_id": event.trade_id,
"slot_id": event.slot_id,
"kind": event.kind.value,
"status": event.status.value,
"venue_order_id": event.venue_order_id,
"venue_client_id": event.venue_client_id,
"side": event.side.value,
"asset": event.asset,
"price": float(event.price or 0.0),
"size": float(event.size or 0.0),
"filled_size": float(event.filled_size or 0.0),
"remaining_size": float(event.remaining_size or 0.0),
"reason": event.reason,
"raw_payload": dict(event.raw_payload),
"metadata": dict(event.metadata),
}
def _transition_from_payload(payload: Dict[str, Any]) -> KernelTransition:
return KernelTransition(
timestamp=datetime.fromisoformat(payload["timestamp"]),
trade_id=str(payload.get("trade_id", "")),
slot_id=int(payload.get("slot_id", 0)),
prev_state=TradeStage(str(payload.get("prev_state", TradeStage.IDLE.value))),
next_state=TradeStage(str(payload.get("next_state", TradeStage.IDLE.value))),
trigger=str(payload.get("trigger", "")),
intent_id=str(payload.get("intent_id", "")),
event_id=str(payload.get("event_id", "")),
control_mode=str(payload.get("control_mode", "")),
control_verbosity=str(payload.get("control_verbosity", "")),
details=dict(payload.get("details", {})),
)
def _outcome_from_payload(payload: Dict[str, Any]) -> KernelOutcome:
return KernelOutcome(
accepted=bool(payload.get("accepted", False)),
slot_id=int(payload.get("slot_id", 0)),
trade_id=str(payload.get("trade_id", "")),
state=TradeStage(str(payload.get("state", TradeStage.IDLE.value))),
diagnostic_code=KernelDiagnosticCode(str(payload.get("diagnostic_code", KernelDiagnosticCode.OK.value))),
severity=KernelSeverity(str(payload.get("severity", KernelSeverity.INFO.value))),
transitions=tuple(_transition_from_payload(row) for row in payload.get("transitions", [])),
emitted_events=tuple(
VenueEvent(
timestamp=datetime.fromisoformat(row["timestamp"]),
event_id=str(row.get("event_id", "")),
trade_id=str(row.get("trade_id", "")),
slot_id=int(row.get("slot_id", 0)),
kind=KernelEventKind(str(row.get("kind", KernelEventKind.ORDER_ACK.value))),
status=VenueEventStatus(str(row.get("status", VenueEventStatus.ACKED.value))),
venue_order_id=str(row.get("venue_order_id", "")),
venue_client_id=str(row.get("venue_client_id", "")),
side=TradeSide(str(row.get("side", TradeSide.FLAT.value))),
asset=str(row.get("asset", "")),
price=float(row.get("price", 0.0)),
size=float(row.get("size", 0.0)),
filled_size=float(row.get("filled_size", 0.0)),
remaining_size=float(row.get("remaining_size", 0.0)),
reason=str(row.get("reason", "")),
raw_payload=dict(row.get("raw_payload", {})),
metadata=dict(row.get("metadata", {})),
)
for row in payload.get("emitted_events", [])
),
details=dict(payload.get("details", {})),
)
def _enum_text(value: Any) -> str:
if hasattr(value, "value"):
return str(getattr(value, "value"))
return str(value)
class KernelSlotView:
"""Write-through view over a Rust-backed slot."""
def __init__(self, kernel: "ExecutionKernel", slot_id: int) -> None:
object.__setattr__(self, "_kernel", kernel)
object.__setattr__(self, "_slot_id", int(slot_id))
@property
def slot_id(self) -> int:
return object.__getattribute__(self, "_slot_id")
def _snapshot(self) -> TradeSlot:
return self._kernel._get_slot(self.slot_id)
def __getattr__(self, name: str) -> Any:
slot = self._snapshot()
if hasattr(slot, name):
return getattr(slot, name)
raise AttributeError(name)
def __setattr__(self, name: str, value: Any) -> None:
if name in {"_kernel", "_slot_id"}:
object.__setattr__(self, name, value)
return
slot = self._snapshot()
if not hasattr(slot, name):
raise AttributeError(name)
setattr(slot, name, value)
self._kernel._set_slot(slot)
def to_dict(self) -> Dict[str, Any]:
return self._snapshot().to_dict()
def is_free(self) -> bool:
return self._snapshot().is_free()
def is_open(self) -> bool:
return self._snapshot().is_open()
def mark_price(self, price: float) -> None:
slot = self._snapshot()
slot.mark_price(price)
self._kernel._set_slot(slot)
def next_exit_ratio(self) -> float:
return self._snapshot().next_exit_ratio()
def consume_exit_leg(self) -> float:
slot = self._snapshot()
ratio = slot.consume_exit_leg()
self._kernel._set_slot(slot)
return ratio
def attach_entry_order(self, order: VenueOrder) -> None:
slot = self._snapshot()
slot.active_entry_order = order
self._kernel._set_slot(slot)
def attach_exit_order(self, order: VenueOrder) -> None:
slot = self._snapshot()
slot.active_exit_order = order
self._kernel._set_slot(slot)
def __repr__(self) -> str: # pragma: no cover - debugging helper
return f"KernelSlotView(slot_id={self.slot_id}, state={self._snapshot().fsm_state.value})"
class KernelStateView:
def __init__(self, kernel: "ExecutionKernel") -> None:
self._kernel = kernel
self.slots = [KernelSlotView(kernel, slot_id) for slot_id in range(kernel.max_slots)]
self.active_trade_index: Dict[str, int] = {}
self.venue_order_index: Dict[str, int] = {}
self.client_order_index: Dict[str, int] = {}
self.refresh()
def refresh(self) -> None:
snapshot = self._kernel._snapshot_backend()
self.active_trade_index = dict(snapshot.get("active_trade_index", {}))
self.venue_order_index = dict(snapshot.get("venue_order_index", {}))
self.client_order_index = dict(snapshot.get("client_order_index", {}))
class ExecutionKernel:
"""Rust-backed multi-slot execution kernel."""
def __init__(
self,
*,
max_slots: int = 10,
control_plane: Optional[ControlPlane] = None,
venue: Optional[VenueAdapter] = None,
journal: Optional[KernelJournal] = None,
account: Optional[AccountProjection] = None,
projection: Optional[HazelcastProjection] = None,
projection_client: Optional[Any] = None,
zinc_plane: Optional[ZincPlane] = None,
) -> None:
self.max_slots = int(max_slots)
self.control_plane = control_plane or build_control_plane()
self.venue = venue or MockVenueAdapter()
self.journal = journal or MemoryKernelJournal()
self.account = account or AccountProjection()
self.projection = projection or build_projection(client=projection_client)
self.zinc_plane = zinc_plane or InMemoryZincPlane()
self._backend = _get_rust().create(self.max_slots)
self._control_snapshot = self.control_plane.read()
self._last_settled_pnl: Dict[int, float] = {}
self.projection.write_control(self._control_snapshot)
self.zinc_plane.update_control(self._control_snapshot)
self.state = KernelStateView(self)
self.account.observe_slots([self._get_slot(slot_id) for slot_id in range(self.max_slots)])
def __del__(self) -> None: # pragma: no cover - cleanup best effort
backend = getattr(self, "_backend", None)
if backend is not None:
try:
_get_rust().destroy(backend)
except Exception:
pass
@property
def control(self) -> KernelControlSnapshot:
return self.control_plane.read()
def update_control(self, update: ControlUpdate) -> KernelControlSnapshot:
snapshot = self.control_plane.update(update)
self._control_snapshot = snapshot
self.projection.write_control(snapshot)
self.zinc_plane.update_control(snapshot)
return snapshot
def _snapshot_backend(self) -> Dict[str, Any]:
return _get_rust().snapshot(self._backend)
def _get_slot(self, slot_id: int) -> TradeSlot:
return _slot_from_payload(_get_rust().get_slot_json(self._backend, slot_id))
def _set_slot(self, slot: TradeSlot, *, journal: bool = False) -> None:
payload = _slot_to_payload(slot)
_get_rust().set_slot_json(self._backend, slot.slot_id, payload)
self.state.refresh()
slots = [self._get_slot(slot_id) for slot_id in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
def slot(self, slot_id: int) -> KernelSlotView:
if not (0 <= int(slot_id) < self.max_slots):
raise IndexError(slot_id)
return self.state.slots[int(slot_id)]
def free_slot(self) -> Optional[KernelSlotView]:
for slot in self.state.slots:
if slot.is_free():
return slot
return None
def _record_transitions(self, transitions: Iterable[KernelTransition], slot: TradeSlot, event: Optional[VenueEvent]) -> None:
if self.control.debug_clickhouse_enabled:
for transition in transitions:
self.journal.record_transition(
transition=transition,
slot=slot,
event=event,
control=self.control,
)
def process_intent(self, intent: KernelIntent) -> KernelOutcome:
self.zinc_plane.publish_intent(intent)
if not (0 <= int(intent.slot_id) < self.max_slots):
return KernelOutcome(
accepted=False,
slot_id=int(intent.slot_id),
trade_id=intent.trade_id,
state=TradeStage.IDLE,
diagnostic_code=KernelDiagnosticCode.INVALID_SLOT_ID,
details={"reason": "INVALID_SLOT_ID", "slot_id": int(intent.slot_id), "intent_id": intent.intent_id},
)
# Finiteness / sanity guard at the kernel boundary. A non-finite (inf/NaN)
# numeric field would make the Rust core's serde_json serialization return
# a null string (panic). Reject cleanly with INVALID_INTENT instead, naming
# the offending field + value so the upstream numerical source can be located.
bad_field = _first_invalid_intent_field(intent)
if bad_field is not None:
name, value = bad_field
return KernelOutcome(
accepted=False,
slot_id=int(intent.slot_id),
trade_id=intent.trade_id,
state=self._get_slot(int(intent.slot_id)).fsm_state,
diagnostic_code=KernelDiagnosticCode.INVALID_INTENT,
severity=KernelSeverity.WARNING,
details={
"reason": "INVALID_INTENT",
"field": name,
"value": str(value),
"intent_id": intent.intent_id,
"action": intent.action.value,
"asset": intent.asset,
},
)
payload = _intent_to_payload(intent)
result = _get_rust().process_intent(
self._backend,
payload,
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
self.state.refresh()
if intent.action == KernelCommandType.ENTER and outcome.accepted:
self._last_settled_pnl[intent.slot_id] = 0.0
emitted_events = []
all_venue_transitions: List[KernelTransition] = []
if intent.action in {KernelCommandType.ENTER, KernelCommandType.EXIT}:
emitted_events = self.venue.submit(intent)
for event in emitted_events:
evt_outcome = self.on_venue_event(event)
all_venue_transitions.extend(evt_outcome.transitions)
elif intent.action == KernelCommandType.CANCEL:
slot_view = self.slot(intent.slot_id)
if slot_view.active_exit_order is not None:
emitted_events = self.venue.cancel(slot_view.active_exit_order, reason=intent.reason)
elif slot_view.active_entry_order is not None and slot_view.fsm_state in {
TradeStage.ENTRY_WORKING,
TradeStage.ORDER_REQUESTED,
TradeStage.ORDER_SENT,
TradeStage.IDLE,
}:
emitted_events = self.venue.cancel(slot_view.active_entry_order, reason=intent.reason)
else:
emitted_events = []
for event in emitted_events:
evt_outcome = self.on_venue_event(event)
all_venue_transitions.extend(evt_outcome.transitions)
final_slot = self._get_slot(outcome.slot_id)
rate_limit_event = next((event for event in emitted_events if event.kind == KernelEventKind.RATE_LIMITED), None)
if rate_limit_event is not None:
rate_limit_details = dict(outcome.details)
rate_limit_details.update(
{
"reason": rate_limit_event.reason or "RATE_LIMITED",
"retry_after_ms": int(rate_limit_event.metadata.get("retry_after_ms", 0) or 0),
"venue_event_kind": rate_limit_event.kind.value,
"severity": KernelSeverity.WARNING.value,
"release_eta": "few minutes",
"retryable": True,
}
)
outcome = KernelOutcome(
accepted=False,
slot_id=outcome.slot_id,
trade_id=outcome.trade_id,
state=final_slot.fsm_state,
diagnostic_code=KernelDiagnosticCode.RATE_LIMITED,
severity=KernelSeverity.WARNING,
transitions=outcome.transitions,
emitted_events=outcome.emitted_events,
details=rate_limit_details,
)
all_transitions = list(outcome.transitions) + all_venue_transitions
final_outcome = KernelOutcome(
accepted=outcome.accepted,
slot_id=outcome.slot_id,
trade_id=final_slot.trade_id,
state=final_slot.fsm_state,
diagnostic_code=outcome.diagnostic_code,
transitions=tuple(all_transitions),
emitted_events=tuple(emitted_events),
details=dict(outcome.details),
)
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(final_slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
self._record_transitions(outcome.transitions, final_slot, None)
return final_outcome
def on_venue_event(self, event: VenueEvent) -> KernelOutcome:
result = _get_rust().on_venue_event(
self._backend,
_event_to_payload(event),
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
# An INVALID_* fallback result carries a null slot; fall back to the
# kernel's current slot so settlement/bookkeeping stays consistent.
slot_payload = result.get("slot")
slot = _slot_from_payload(slot_payload) if slot_payload else self._get_slot(int(outcome.slot_id))
self.state.refresh()
incremental_pnl = slot.realized_pnl - self._last_settled_pnl.get(slot.slot_id, 0.0)
if abs(incremental_pnl) > 1e-12:
self.account.settle(incremental_pnl)
self._last_settled_pnl[slot.slot_id] = slot.realized_pnl
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
current = self._get_slot(slot.slot_id)
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
self._record_transitions(outcome.transitions, slot, event)
return outcome
def mark_price(self, asset: str, price: float) -> None:
for slot in self.state.slots:
if slot.asset == asset and slot.is_open():
slot.mark_price(price)
self.account.observe_slots([self._get_slot(i) for i in range(self.max_slots)])
def reconcile_from_slots(self, slots: Sequence[TradeSlot]) -> KernelOutcome:
payload = [_slot_to_payload(slot) for slot in slots]
result = _get_rust().reconcile_slots(
self._backend,
payload,
mode=_enum_text(self.control.mode),
verbosity=_enum_text(self.control.verbosity),
)
outcome = _outcome_from_payload(result["outcome"])
if not outcome.accepted:
return outcome
self.state.refresh()
slots = [self._get_slot(i) for i in range(self.max_slots)]
self.account.observe_slots(slots)
for current in slots:
self.projection.write_slot(current)
self.zinc_plane.write_slot(current)
return outcome
def snapshot(self) -> Dict[str, Any]:
return {
"control": self.control.as_dict(),
"slots": [self._get_slot(slot.slot_id).to_dict() for slot in self.state.slots],
"account": {
"capital": self.account.snapshot.capital,
"equity": self.account.snapshot.equity,
"realized_pnl": self.account.snapshot.realized_pnl,
"unrealized_pnl": self.account.snapshot.unrealized_pnl,
"open_positions": self.account.snapshot.open_positions,
"open_notional": self.account.snapshot.open_notional,
"leverage": self.account.snapshot.leverage,
},
}

Some files were not shown because too many files have changed in this diff Show More