Compare commits
103 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b8aa00b883 | |||
| ba2c23e602 | |||
| 646c64dacd | |||
| a702b2090d | |||
| c6bf0f31cc | |||
| 68e0b009e9 | |||
| 4baa1eca62 | |||
| 035cd1dff3 | |||
| b8bf0c186c | |||
| af68bc44b4 | |||
| 074ebe0379 | |||
| 7f2db19a7c | |||
| 369a77b5cf | |||
| 2aa5646aeb | |||
| 6a9e2c28b1 | |||
| 37558a34f5 | |||
| 14f476dd09 | |||
| 05c7b5e89e | |||
| 6655e425fa | |||
| 23b7273e71 | |||
| 9c871d1d86 | |||
| 8b767da5e7 | |||
| 1c0058ec3b | |||
| 220e510d5e | |||
| 9742df3a1f | |||
| 21b5cf1eae | |||
| a29748e3d8 | |||
| fa11cca2bc | |||
| 6526c6e6e3 | |||
| ccf9f7a33c | |||
| 19a3592a20 | |||
| 5202eb517b | |||
| 898b24b6a3 | |||
| b6f48e46fc | |||
| 0fd31d52ec | |||
| a43157cd44 | |||
| 436613bfde | |||
| f55e4f00c5 | |||
| 96e08ff78f | |||
| 9c3b5ad586 | |||
| f875df31b4 | |||
| 720b2d58d7 | |||
| 96bbd716ec | |||
| 30add35906 | |||
| 8caa526727 | |||
| 289df4b81f | |||
| 2b5da4d1fc | |||
| 37bf64012b | |||
| b02be64831 | |||
| 08f1585ab2 | |||
| cd4c3131d9 | |||
| b6539802e0 | |||
| 7d766173a4 | |||
| 4d79e66d68 | |||
| 4b9cded966 | |||
| 14f130aa5a | |||
| b86dbdc9ee | |||
| a66f97fb0e | |||
| 073200313c | |||
| 03f723f7fc | |||
| 8e5efde219 | |||
| 45f273f591 | |||
| 9d1ef8adcf | |||
| 67ae6ff74e | |||
| 1a1dfb7a73 | |||
| 3fcad79f8d | |||
| 242724ba05 | |||
| 4c184bb5f7 | |||
| cf42dd85f3 | |||
| bf70acc322 | |||
| 597815a106 | |||
| ba4eb09a71 | |||
| 0e01de156f | |||
| 4c119a109e | |||
| a12aead3e5 | |||
| ec80af9f26 | |||
| c38311e5fa | |||
| 8ec45c5c1b | |||
| 9344395760 | |||
| 6f6fbb30a0 | |||
| 171f554916 | |||
| 9e740cbcbd | |||
| 2acba2077b | |||
| 0486e19829 | |||
| 2f38562e23 | |||
| 56e22584d9 | |||
| 5f28884974 | |||
| 7b790b1bc3 | |||
| 41e26cbe5b | |||
| 9c53995f23 | |||
| 68637d1102 | |||
| 36cbfadb40 | |||
| 2014ed3815 | |||
| 22a934a6cf | |||
| 9d1f97cff3 | |||
| 0e9489bf88 | |||
| 3e9a4efcc2 | |||
| 30dbba4d74 | |||
| c6cb32325e | |||
| 1a171acfb2 | |||
| 9d0deb3ae0 | |||
| d3662f6098 | |||
| 23c9e37f94 |
@@ -0,0 +1,58 @@
|
||||
# Git
|
||||
.git
|
||||
.gitignore
|
||||
.gitattributes
|
||||
|
||||
# Python caches
|
||||
__pycache__
|
||||
*.py[cod]
|
||||
*.egg-info
|
||||
.venv
|
||||
venv
|
||||
.pytest_cache
|
||||
.mypy_cache
|
||||
.ruff_cache
|
||||
|
||||
# Editors / OS
|
||||
.vscode
|
||||
.idea
|
||||
.DS_Store
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# Secrets — montati via env_file nel compose, mai dentro l'immagine
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
*.pem
|
||||
*.key
|
||||
|
||||
# Artefatti runtime — vivono come bind mount, non nell'immagine
|
||||
runs.db
|
||||
runs.db-journal
|
||||
runs.db-wal
|
||||
runs.db-shm
|
||||
data/
|
||||
series/
|
||||
state/
|
||||
*.parquet
|
||||
*.feather
|
||||
checkpoints/
|
||||
logs/
|
||||
*.log
|
||||
|
||||
# Build / dist
|
||||
build/
|
||||
dist/
|
||||
|
||||
# Docs grandi — non servono in immagine
|
||||
docs/
|
||||
*.md
|
||||
!README.md
|
||||
|
||||
# Test — non servono in runtime (l'immagine non gira pytest)
|
||||
tests/
|
||||
|
||||
# OMC / claude metadata
|
||||
.omc/
|
||||
.claude/
|
||||
+28
-5
@@ -11,14 +11,37 @@ OPENROUTER_API_KEY=
|
||||
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
|
||||
|
||||
# Models per tier (override Phase 1 defaults if needed)
|
||||
LLM_MODEL_TIER_S=anthropic/claude-opus-4-7
|
||||
LLM_MODEL_TIER_A=anthropic/claude-sonnet-4-6
|
||||
LLM_MODEL_TIER_B=anthropic/claude-sonnet-4-6
|
||||
LLM_MODEL_TIER_S=google/gemini-3-flash-preview
|
||||
LLM_MODEL_TIER_A=deepseek/deepseek-v4-flash
|
||||
LLM_MODEL_TIER_B=deepseek/deepseek-v4-flash
|
||||
LLM_MODEL_TIER_C=qwen/qwen-2.5-72b-instruct
|
||||
LLM_MODEL_TIER_D=meta-llama/llama-3.3-70b-instruct
|
||||
LLM_MODEL_TIER_D=openai/gpt-oss-20b
|
||||
|
||||
# Run config
|
||||
RUN_NAME=phase1-spike-001
|
||||
DATA_DIR=./data
|
||||
SERIES_DIR=./series
|
||||
DB_PATH=./runs.db
|
||||
|
||||
# Database paths (split per dominio):
|
||||
# - GA_DB_PATH: tabelle GA universali (runs, generations, genomes, evaluations)
|
||||
# - STRATEGY_CRYPTO_DB_PATH: tabelle paper_trading_* per la strategia crypto
|
||||
GA_DB_PATH=./state/runs.db
|
||||
STRATEGY_CRYPTO_DB_PATH=./state/strategy_crypto.db
|
||||
|
||||
# Docker / Traefik (usati SOLO da docker-compose.yml)
|
||||
# Dominio base: traefik espone le dashboard su swarm.${DOMAIN_NAME}/...
|
||||
DOMAIN_NAME=tielogic.xyz
|
||||
# Porta interna della NiceGUI dashboard (Traefik fa il TLS davanti)
|
||||
SWARM_DASHBOARD_PORT=8080
|
||||
# Subpath URL del dashboard NiceGUI — ora PER-SERVIZIO nel docker-compose.yml:
|
||||
# strategy-crypto-gui -> DASHBOARD_ROOT_PATH=/strategy_crypto_gui
|
||||
# multi-swarm-core-gui -> DASHBOARD_ROOT_PATH=/multi_swarm_core_gui
|
||||
# In sviluppo locale lascia vuoto (nessun subpath).
|
||||
DASHBOARD_ROOT_PATH=
|
||||
|
||||
# Paper-trading runner — override del command nel compose (opzionali)
|
||||
PAPER_RUN_NAME=phase3-papertrade-prod
|
||||
PAPER_INITIAL_CAPITAL=1000
|
||||
PAPER_FEES_BP=5.0
|
||||
PAPER_POLL_SECONDS=300
|
||||
PAPER_LOOKBACK_BARS=500
|
||||
|
||||
+11
@@ -14,6 +14,7 @@ venv/
|
||||
.DS_Store
|
||||
*.swp
|
||||
*.swo
|
||||
.claude/
|
||||
|
||||
# Env / secrets
|
||||
.env
|
||||
@@ -23,6 +24,10 @@ venv/
|
||||
*.key
|
||||
|
||||
# Project artefacts (non versionati: troppo grandi o rigenerabili)
|
||||
state/*.db
|
||||
state/*.db-journal
|
||||
state/*.db-wal
|
||||
state/*.db-shm
|
||||
runs.db
|
||||
runs.db-journal
|
||||
runs.db-wal
|
||||
@@ -35,7 +40,13 @@ checkpoints/
|
||||
logs/
|
||||
*.log
|
||||
|
||||
# OMC state (auto-orchestration)
|
||||
.omc/
|
||||
|
||||
# Build / dist
|
||||
build/
|
||||
dist/
|
||||
*.egg
|
||||
|
||||
# Pythagoras source PDFs (local only, not tracked)
|
||||
src/strategy_pythagoras/Pythagoras/*.pdf
|
||||
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
# syntax=docker/dockerfile:1.7
|
||||
#
|
||||
# Multi-Swarm Coevolutive — immagine unica usata da due servizi del compose:
|
||||
# * paper-trading runner (scripts/run_paper_trading.py)
|
||||
# * NiceGUI dashboard (strategy_crypto.frontend.nicegui_app)
|
||||
#
|
||||
# uv workspace: pyproject root coordina due member packages
|
||||
# (multi-swarm-core + strategy-crypto). Il `uv sync --frozen` installa
|
||||
# entrambi come editable nella venv del builder.
|
||||
# Runtime stage: copia solo /app + scripts/ e gira come utente non-root.
|
||||
# data/, series/, state/ sono bind mount dal compose; strategies/ è
|
||||
# bind-mounted dal path src/strategy_crypto/strategy_crypto/strategies.
|
||||
|
||||
FROM python:3.13-slim AS builder
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
build-essential curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
RUN pip install --no-cache-dir "uv>=0.5,<0.9"
|
||||
WORKDIR /app
|
||||
COPY pyproject.toml uv.lock README.md ./
|
||||
COPY src ./src
|
||||
RUN uv sync --frozen --no-dev
|
||||
|
||||
|
||||
FROM python:3.13-slim AS runtime
|
||||
LABEL org.opencontainers.image.title="multi-swarm-coevolutive" \
|
||||
org.opencontainers.image.version="0.1.0" \
|
||||
org.opencontainers.image.source="https://git.tielogic.xyz/Adriano/Multi_Swarm_Coevolutive"
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder /app /app
|
||||
COPY scripts ./scripts
|
||||
|
||||
ENV PATH="/app/.venv/bin:$PATH" \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PYTHONDONTWRITEBYTECODE=1
|
||||
# NO PYTHONPATH: con uv workspace + layout doppio-nest, PYTHONPATH=/app/src
|
||||
# farebbe ombra alla venv risolvendo le member-dir (multi_swarm_core/,
|
||||
# strategy_crypto/) come namespace packages senza i sub-package del codice.
|
||||
# I pacchetti sono installati come editable dal `uv sync --frozen` del builder
|
||||
# e risolvibili direttamente via /app/.venv/.
|
||||
|
||||
RUN useradd -m -u 1000 app \
|
||||
&& mkdir -p /app/data /app/series /app/state /app/strategies \
|
||||
&& chown -R app:app /app
|
||||
USER app
|
||||
|
||||
# Healthcheck di default: import dei due package del workspace.
|
||||
# I servizi reali lo sovrascrivono nel compose (es. NiceGUI HTTP).
|
||||
HEALTHCHECK --interval=60s --timeout=5s --retries=3 --start-period=10s \
|
||||
CMD python -c "import multi_swarm_core, strategy_crypto" || exit 1
|
||||
|
||||
# Nessun CMD di default: il compose specifica entrypoint/command
|
||||
# per ognuno dei due servizi.
|
||||
@@ -1,88 +1,117 @@
|
||||
# Multi_Swarm_Coevolutive
|
||||
|
||||
Proof-of-concept di sistema co-evolutivo multi-agente per trading quantitativo. Un genetic algorithm fa evolvere una popolazione di agenti LLM (Hypothesis swarm) che generano strategie di trading espresse in JSON strutturato; un layer Falsification deterministico le backtesta su dati storici BTC-PERPETUAL via Cerbero MCP; un layer Adversarial euristico le sottopone a red-team checks; la fitness combina Deflated Sharpe Ratio (Bailey & López 2014), Sharpe normalizzato e penalizzazione di drawdown. Il tutto è ispirato alla filosofia di Renaissance Technologies adattata a un contesto retail single-author con LLM agents.
|
||||
Proof-of-concept di sistema co-evolutivo multi-agente per trading quantitativo. Un genetic algorithm fa evolvere una popolazione di agenti LLM (Hypothesis swarm) che generano strategie di trading espresse in JSON strutturato; un layer Falsification deterministico le backtesta su dati storici (default BTC-PERPETUAL Deribit) via Cerbero MCP; un layer Adversarial euristico le sottopone a red-team checks; la fitness combina Deflated Sharpe Ratio (Bailey & López 2014), Sharpe normalizzato e penalizzazione di drawdown, con opzioni v2 soft-kill e combined IS/OOS per Walk-Forward Validation.
|
||||
|
||||
## Repository
|
||||
|
||||
Gitea Tielogic (privato, accesso SSH):
|
||||
|
||||
```bash
|
||||
git clone ssh://git@git.tielogic.xyz:222/Adriano/Multi_Swarm_Coevolutive.git
|
||||
```
|
||||
|
||||
## Layout monorepo (uv workspace)
|
||||
|
||||
Il repo è un **workspace uv** con due member packages indipendenti, principio "**core = framework, strategy = contenuto**":
|
||||
|
||||
```
|
||||
multi_swarm_coevolutive/ repo root (workspace coordinator)
|
||||
├── pyproject.toml workspace + dev deps + ruff/mypy/pytest
|
||||
├── docker-compose.yml 3 servizi su immagine condivisa
|
||||
├── Dockerfile immagine multi-swarm-coevolutive:dev
|
||||
├── uv.lock lock unico del workspace
|
||||
├── data/, series/, state/ cache OHLCV + DB (runtime, gitignored)
|
||||
├── scripts/ CLI entrypoints (run_phase1, run_paper_trading, ...)
|
||||
└── src/
|
||||
├── multi_swarm_core/ WORKSPACE MEMBER (wheel: multi-swarm-core)
|
||||
│ ├── pyproject.toml core deps (pandas, numpy, openai, pydantic, nicegui, ...)
|
||||
│ ├── multi_swarm_core/ GA + genome + protocol + backtest + cerbero +
|
||||
│ │ data + llm + agents + ga + orchestrator +
|
||||
│ │ metrics + persistence + config + dashboard (GA-only)
|
||||
│ ├── tests/ unit + integration
|
||||
│ └── docs/ design/ + decisions/ + reports/
|
||||
│
|
||||
└── strategy_crypto/ WORKSPACE MEMBER (wheel: strategy-crypto)
|
||||
├── pyproject.toml deps: multi-swarm-core (workspace) + nicegui + plotly
|
||||
├── README.md overview strategia + pattern per nuove strategie
|
||||
├── strategy_crypto/
|
||||
│ ├── backend/ paper-trading (executor, portfolio, persistence, schema)
|
||||
│ ├── frontend/ NiceGUI paper-only dashboard
|
||||
│ ├── strategies/ JSON freezate (btc_*.json, eth_*.json)
|
||||
│ └── prompts.json v3.2: agent_role/pattern_guidance/instruction/
|
||||
│ domain_warnings/anti_patterns/output_priorities +
|
||||
│ 7 stili cognitive (directive + focus_metrics)
|
||||
└── tests/ smoke regression
|
||||
```
|
||||
|
||||
**DB separati per dominio:** `state/runs.db` (GA core universale) + `state/strategy_crypto.db` (paper della strategia crypto). Pattern scala a N strategie senza naming collision.
|
||||
|
||||
**Pattern N strategie future:** aggiungere `src/strategy_<asset>/` con stesso scheletro (`backend/`, `frontend/`, `strategies/`, `tests/`, `prompts.json`), DB dedicato `state/strategy_<asset>.db`, servizi Docker `strategy-<asset>-paper` + `strategy-<asset>-gui`, GUI su `/strategy_<asset>_gui`. **Zero modifiche al core** richieste.
|
||||
|
||||
## Architettura prompt (v3.2)
|
||||
|
||||
**Compositor**: il SYSTEM prompt al LLM viene COMPOSTO at-runtime da scaffold core + contenuto strategy:
|
||||
|
||||
```
|
||||
[1] agent_role ← strategy (prompts.json — chi è l'agente)
|
||||
[2] cognitive_style + directive ← genome (evoluti dal GA)
|
||||
[3] GRAMMAR_SPEC ← core scaffold (operatori, indicatori, units rules)
|
||||
[4] pattern_guidance ← strategy (forme di curva astratte, no indicatori prescritti)
|
||||
[5] domain_warnings ← strategy (es. "crypto trada 24/7, NON inferire funding rate")
|
||||
[6] CONSTRAINTS ← core scaffold (validator semantics)
|
||||
[7] anti_patterns ← strategy (7 voci: no >4 AND, no chattering, isteresi, ecc.)
|
||||
[8] output_priorities ← strategy (5 voci, #1 coerenza con lente cognitiva)
|
||||
[9] EXAMPLE ← core scaffold
|
||||
```
|
||||
|
||||
**Input USER (calcolato da `build_market_summary`):**
|
||||
- Base (5): mean, std, skew, kurt, vol_regime
|
||||
- Regime recente rolling 500 (6): autocorr_lag1 (recent + baseline), hurst, vol_percentile, seasonality (hour + dow)
|
||||
- Geometria & frattali (7): efficiency_ratio (Kaufman), tail_index (left + right Hill), structural_uptrend (HH/HL), compression, spectral_entropy, dominant_cycle (gated)
|
||||
- Feature accessibili dal genome + lookback_window
|
||||
- **Focus per la tua lente**: blocco style-aware (4 metriche prioritarie da `focus_metrics` di prompts.json)
|
||||
- Instruction finale (da strategy)
|
||||
|
||||
**Grammar protocol JSON (8 indicatori):**
|
||||
| Indicatore | Output | Range |
|
||||
|------------|--------|-------|
|
||||
| `sma(length)` | media mobile | unità prezzo |
|
||||
| `sma_pct(length)` | (close-sma)/sma | ±0.1 frazione |
|
||||
| `rsi(length)` | oscillator | 0-100 |
|
||||
| `atr(length)` | true range | unità prezzo |
|
||||
| `atr_pct(length)` | atr/close | 0-0.1 frazione |
|
||||
| `realized_vol(window)` | std returns | 0-0.1 frazione |
|
||||
| `macd(fast,slow,signal)` | momentum | unità prezzo |
|
||||
| `macd_pct(...)` | macd/close | ±0.02 frazione |
|
||||
|
||||
**7 stili cognitive** (in `prompts.json`, editable): physicist, biologist, historian, meteorologist, engineer, military_strategist, psychologist. Ognuno con directive 800-950 char, ASCII-strict, archetipo dominante + lookback consigliato + 4 focus_metrics.
|
||||
|
||||
## Stato del progetto
|
||||
|
||||
**Phase 1 (lean spike) completata** il 10 maggio 2026 con tutti i 5 hard gate passati (loop convergence, parse success 100%, top-5 ratio 1116x, entropy 0.914, costo $0.069 vs cap $700). Decisione strategica: **GO Phase 2** con tre aggiustamenti (Adversarial soglie più strette, speciation, walk-forward 70/30).
|
||||
**Phase 3 (paper-trading forward-test) in corso** dal 13 maggio 2026 su VPS. Runner `scripts/run_paper_trading.py` long-running in Docker, dashboard NiceGUI su `https://swarm.tielogic.xyz/strategy_crypto_gui/`. Due strategie freezate in `src/strategy_crypto/strategy_crypto/strategies/`:
|
||||
|
||||
Documenti chiave:
|
||||
- `btc_fb63e851.json` — BTC-PERPETUAL, RSI estremi + ATR vs SMA + filtro orario 9-17 (Sharpe OOS +0,26 su 7,33 anni).
|
||||
- `eth_facd6af85d5d.json` — ETH-PERPETUAL, regime-based (con `atr_pct` post-fix bug unità).
|
||||
|
||||
- [Decisione strategica](docs/superpowers/specs/2026-05-09-decisione-strategica-design.md) — perché Phase 1 prima, Phase 2 poi, Phase 3 forward-test.
|
||||
- [Piano implementativo Phase 1](docs/superpowers/plans/2026-05-09-phase1-lean-spike.md) — 38 task TDD-driven.
|
||||
- [Decision memo gate Phase 1](docs/decisions/2026-05-10-gate-phase1.md) — valutazione formale dei 5 hard gate.
|
||||
- [Technical report Phase 1](docs/reports/2026-05-10-phase1-technical-report.md) — risultati, ispezione top genomi, threats to validity.
|
||||
Phase 1 → 2.7 chiuse (30 run GA, $3.74 cumulato LLM). Sessione refactor 15 maggio 2026:
|
||||
- Split repo invertito, monorepo unificato come uv workspace
|
||||
- Family `*_pct` completa (atr_pct, sma_pct, macd_pct) per fix bug unità
|
||||
- Dashboard split: core (GA) vs strategy (paper)
|
||||
- Prompt architecture compositor + prompts.json v3.2 (vedi decision log)
|
||||
|
||||
Documenti di contesto pre-implementazione:
|
||||
Documenti:
|
||||
- [**Stato progetto e roadmap (14 maggio 2026)**](src/multi_swarm_core/docs/reports/2026-05-14-stato-progetto-e-roadmap.md)
|
||||
- Decision log: [`src/multi_swarm_core/docs/decisions/`](src/multi_swarm_core/docs/decisions/) (gate Phase 1, nemotron, atr_pct fix)
|
||||
- Design docs concettuali: [`src/multi_swarm_core/docs/design/`](src/multi_swarm_core/docs/design/)
|
||||
|
||||
- `00_documento_zero.md` — framework concettuale (Renaissance → swarm co-evolutivo LLM).
|
||||
- `coevolutive_swarm_system.md` — design Filone A (sistema completo, 12-18 mesi).
|
||||
- `poc_trading_swarm.md` — design Filone B (PoC trading, fonte di Phase 1).
|
||||
|
||||
## Architettura
|
||||
|
||||
```
|
||||
src/multi_swarm/
|
||||
├── config.py Settings Pydantic (.env)
|
||||
├── data/
|
||||
│ ├── cerbero_ohlcv.py OHLCV loader via Cerbero MCP + cache parquet
|
||||
│ └── splits.py Walk-forward expanding splits
|
||||
├── backtest/
|
||||
│ ├── orders.py Side/Order/Position/Trade
|
||||
│ └── engine.py Event-driven backtest, 1-bar exec delay
|
||||
├── metrics/
|
||||
│ ├── basic.py Sharpe, max drawdown, total return
|
||||
│ └── dsr.py Deflated Sharpe Ratio (Bailey & López 2014)
|
||||
├── cerbero/
|
||||
│ ├── client.py HTTP client (bearer + bot-tag + retry tenacity)
|
||||
│ └── tools.py Wrapper tool MCP (sma/rsi/atr/macd/realized_vol/funding)
|
||||
├── protocol/
|
||||
│ ├── grammar.py Vocabolario operatori, indicatori, feature
|
||||
│ ├── parser.py json.loads → AST dataclass tipizzato
|
||||
│ ├── validator.py Arity checks, no-nesting indicators, whitelist
|
||||
│ └── compiler.py AST → Callable[[df], Series[Side]]
|
||||
├── genome/
|
||||
│ ├── hypothesis.py HypothesisAgentGenome (id deterministico)
|
||||
│ ├── mutation.py 4 operatori (temp, lookback, features, style)
|
||||
│ └── crossover.py Uniform crossover
|
||||
├── llm/
|
||||
│ ├── client.py Unified LLMClient via OpenRouter (tier S/A/B/C/D)
|
||||
│ └── cost_tracker.py Pricing per tier, breakdown
|
||||
├── agents/
|
||||
│ ├── hypothesis.py LLM call + JSON extract + retry-with-feedback
|
||||
│ ├── falsification.py Compile → backtest → DSR
|
||||
│ ├── adversarial.py Red-team heuristics (no_trades/degenerate/over/under)
|
||||
│ └── market_summary.py Stats di mercato per il prompt
|
||||
├── ga/
|
||||
│ ├── selection.py Tournament + elitism
|
||||
│ ├── fitness.py v1 continua: dsr + tanh(sharpe) × penalty(dd)
|
||||
│ ├── loop.py next_generation step
|
||||
│ ├── summary.py median/max/p90/entropy per gen
|
||||
│ └── initial.py Popolazione iniziale (6 cognitive style)
|
||||
├── persistence/
|
||||
│ ├── schema.py SQLite DDL: 6 tabelle + 3 indici
|
||||
│ └── repository.py CRUD per runs/genomes/evals/cost/findings/gen_summary
|
||||
├── orchestrator/
|
||||
│ └── run.py End-to-end pipeline + persistence
|
||||
└── dashboard/
|
||||
├── streamlit_app.py Hub multipage
|
||||
├── data.py Lettura runs.db per le pagine
|
||||
├── aquarium.py Helper canvas HTML5 (fish data + JS template)
|
||||
└── pages/
|
||||
├── 01_overview.py Run + metriche aggregate
|
||||
├── 02_ga_convergence.py Fitness convergence + entropy plot
|
||||
├── 03_genomes.py Top-10 + ispezione system_prompt
|
||||
└── 04_aquarium.py Acquario 2D con click → info + lineage
|
||||
```
|
||||
|
||||
Stack: Python 3.13, uv, pytest+pytest-mock+responses, openai SDK (verso OpenRouter), requests+tenacity, pandas+numpy+scipy, sqlmodel+sqlite, streamlit+plotly.
|
||||
Stack: Python 3.13, uv workspace, hatchling, pytest+pytest-mock+responses, openai SDK (verso OpenRouter), requests+tenacity, pandas+numpy+scipy, sqlmodel+sqlite, nicegui+plotly, yfinance.
|
||||
|
||||
## Setup
|
||||
|
||||
```bash
|
||||
uv sync
|
||||
cp .env.example .env # compilare CERBERO_*_TOKEN e OPENROUTER_API_KEY
|
||||
uv run pytest # verifica che tutto installi (141 test attesi)
|
||||
uv sync # installa entrambi i workspace member come editable
|
||||
cp .env.example .env # compila CERBERO_*_TOKEN e OPENROUTER_API_KEY
|
||||
uv run pytest # 252 test attesi (248 core + 4 smoke strategy_crypto)
|
||||
```
|
||||
|
||||
### Variabili .env richieste
|
||||
@@ -96,70 +125,125 @@ CERBERO_BOT_TAG=swarm-poc-phase1
|
||||
|
||||
# LLM provider (unico endpoint via OpenRouter)
|
||||
OPENROUTER_API_KEY=<sk-or-v1-...>
|
||||
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
|
||||
|
||||
# Modelli per tier (override dei default se serve)
|
||||
LLM_MODEL_TIER_S=anthropic/claude-opus-4-7
|
||||
LLM_MODEL_TIER_A=anthropic/claude-sonnet-4-6
|
||||
LLM_MODEL_TIER_B=anthropic/claude-sonnet-4-6
|
||||
LLM_MODEL_TIER_C=qwen/qwen-2.5-72b-instruct
|
||||
LLM_MODEL_TIER_D=meta-llama/llama-3.3-70b-instruct
|
||||
# DB paths (split per dominio: core GA vs paper della strategia)
|
||||
GA_DB_PATH=./state/runs.db
|
||||
STRATEGY_CRYPTO_DB_PATH=./state/strategy_crypto.db
|
||||
|
||||
# Deploy Docker — DASHBOARD_ROOT_PATH ora per-servizio (vedi docker-compose.yml)
|
||||
DOMAIN_NAME=tielogic.xyz
|
||||
SWARM_DASHBOARD_PORT=8080
|
||||
```
|
||||
|
||||
### Cerbero MCP
|
||||
|
||||
Phase 1 fetcha OHLCV via Cerbero MCP (sostituisce ccxt). Avviare Cerbero locale prima di un run reale:
|
||||
|
||||
```bash
|
||||
cd /home/adriano/Documenti/Git_XYZ/CerberoSuite/Cerbero_mcp
|
||||
uv sync
|
||||
uv run cerbero-mcp # ascolta su porta da .env (default 9001 se 9000 è occupato)
|
||||
```
|
||||
|
||||
In alternativa usare il VPS esistente `https://cerbero-mcp.tielogic.xyz` (richiede bearer).
|
||||
Backcompat: `DB_PATH` legacy continua a funzionare come alias di `GA_DB_PATH`.
|
||||
|
||||
## Comandi principali
|
||||
|
||||
```bash
|
||||
# Quality gates
|
||||
uv run pytest # tutti i test (141 PASSED attesi)
|
||||
uv run pytest tests/unit -v # solo unit
|
||||
uv run pytest tests/integration -v # solo integration
|
||||
uv run ruff check src/ tests/ scripts/
|
||||
uv run pytest # tutti i test
|
||||
uv run pytest src/multi_swarm_core/tests/unit -v # solo unit core
|
||||
uv run pytest src/strategy_crypto/tests/ -v # smoke strategy_crypto
|
||||
uv run ruff check src/ scripts/
|
||||
uv run mypy src/ scripts/
|
||||
|
||||
# Smoke run (MockLLM + OHLCV sintetico, no API calls)
|
||||
uv run python scripts/smoke_run.py
|
||||
|
||||
# Run reale Phase 1 (Cerbero + OpenRouter, ~$0.07 per run K=20 10gen)
|
||||
# Run reale Phase 1/2 (Cerbero + OpenRouter, ~$0.15-0.25 per run K=20 10gen,
|
||||
# ~$0.40-0.55 per run esteso K=40 20gen con WFA OOS).
|
||||
# Default --start ora 2018-09-01 (7.3y, copre bear+halving+covid+ATH+winter+ETF).
|
||||
uv run python scripts/run_phase1.py \
|
||||
--name phase1-run-XXX \
|
||||
--name run-XXX \
|
||||
--exchange deribit --symbol BTC-PERPETUAL --timeframe 1h \
|
||||
--start 2024-01-01T00:00:00+00:00 \
|
||||
--end 2026-01-01T00:00:00+00:00 \
|
||||
--population-size 20 --n-generations 10
|
||||
--population-size 20 --n-generations 10 \
|
||||
--prompt-mutation-weight 0.30 --fitness-v2 \
|
||||
--llm-concurrency 8 # 5-8x speedup wall time (default 1)
|
||||
# fitness-v2 hardened: hard-kill su {no_trades, degenerate, undertrading,
|
||||
# fees_eat_alpha, negative_net_pnl}. Override via --fitness-hard-kill CSV.
|
||||
# Default --prompt-library: importlib.resources del package strategy_crypto/prompts.json
|
||||
|
||||
# Dashboard
|
||||
DB_PATH=./runs.db uv run streamlit run src/multi_swarm/dashboard/streamlit_app.py
|
||||
# Multi-fold validation di un run esistente (anti-overfit, 7y expanding-window)
|
||||
uv run python scripts/validate_run.py \
|
||||
--run-id <run_id> \
|
||||
--top-k 10 --n-folds 4 --train-ratio 0.5 \
|
||||
--start 2018-09-01T00:00:00+00:00 --end 2026-01-01T00:00:00+00:00 \
|
||||
--fitness-v2 \
|
||||
--output-json state/validation-XXX.json
|
||||
# Ranking per "robust_score" = min(fitness_oos) su tutti i fold.
|
||||
|
||||
# Backtest standalone di una strategia JSON
|
||||
uv run python scripts/backtest_strategy.py \
|
||||
--strategy src/strategy_crypto/strategy_crypto/strategies/btc_fb63e851.json \
|
||||
--start 2018-09-01 --end 2026-01-01
|
||||
|
||||
# Paper-trading forward-test (Phase 3)
|
||||
uv run python scripts/run_paper_trading.py \
|
||||
--name phase3-papertrade-XXX \
|
||||
--initial-capital 1000 --poll-seconds 300
|
||||
|
||||
# Dashboard NiceGUI locale (2 distinte)
|
||||
uv run python -m multi_swarm_core.dashboard.nicegui_app # GA core (/, /convergence, /genomes)
|
||||
uv run python -m strategy_crypto.frontend.nicegui_app # Strategy crypto (/ paper)
|
||||
```
|
||||
|
||||
## Dashboard
|
||||
## Performance & Validation
|
||||
|
||||
Streamlit multipage su `http://localhost:8501` (override con `--server.port`):
|
||||
**Backtest engine vettorializzato** (`backtest/engine.py`): rimosso il loop `pd.iterrows()` a favore di state machine numpy. Speedup misurati:
|
||||
|
||||
- **Overview**: lista runs, status, costo, metriche aggregate evaluations (parse success %, top fitness, median).
|
||||
- **GA Convergence**: fitness median/max/p90 per generazione, entropy con hline a soglia gate (0.5).
|
||||
- **Genomes**: top-10 ordinati per fitness, click su row per ispezione system_prompt + raw_text JSON strategy.
|
||||
- **Aquarium**: visualizzazione 2D canvas HTML5 con un pesce per agente; dimensione ∝ fitness, colore per cognitive_style, halo sui top-3, click su pesce → panel info completo + lineage BFS (parents → grandparents → ...).
|
||||
| Dataset | Before (iterrows) | After (vectorized) | Speedup |
|
||||
|---------|-------------------|--------------------|---------|
|
||||
| 2 anni (17545 bar) | 470 ms | **28 ms** | **16.8×** |
|
||||
| 7 anni (64297 bar) | 1744 ms | **154 ms** | **11.3×** |
|
||||
|
||||
## Costi tipici Phase 1
|
||||
Equivalenza numerica garantita: 5 parity test parametrici vs. reference implementation legacy (`test_backtest_engine_vectorized.py`).
|
||||
|
||||
Tier C (qwen-2.5-72b via OpenRouter): ~$0.40/1M token. Run K=20 × 10gen ≈ $0.07. Phase 1 totale (5 run incluse iterazioni bug-fix): $0.19.
|
||||
**Parallel propose LLM** (`orchestrator/run.py`): `--llm-concurrency N` lancia N chiamate `hypothesis_agent.propose()` concorrenti per generazione tramite `ThreadPoolExecutor`. OpenRouter qwen-2.5 regge 6-10 concorrenti senza rate-limit. Default 1 = backward-compat.
|
||||
|
||||
Per Phase 2 con tier mix B/C (Sonnet 4.6 = $3/$15 input/output) stima: $3-15 per ablation completa.
|
||||
**Multi-fold validation tool** (`scripts/validate_run.py`): qualunque run completato puo' essere rivalutato post-hoc su N fold expanding-window di un dataset esteso (tipicamente 7 anni). Vital per evitare il single-hold-out overfit: il GA puo' selezionare un genome con `fitness_is` alta che collassa OOS (osservato su `phase1-extended-001`: elite IS Sharpe 1.93, OOS Sharpe -1.00). Ranking finale per `robust_score = min(fitness_oos)`. Output JSON con per-fold breakdown + aggregati mean/min/std.
|
||||
|
||||
## Dashboard (split core + strategy)
|
||||
|
||||
Due NiceGUI dashboard distinte (dark palette, palette neon):
|
||||
|
||||
**Core GA** — `multi_swarm_core.dashboard.nicegui_app` → `https://swarm.tielogic.xyz/multi_swarm_core_gui/`:
|
||||
- **Overview** (`/`): lista runs GA, costo cumulato, metriche aggregate evaluations.
|
||||
- **GA Convergence** (`/convergence`): fitness median/max/p90 per generazione + entropy.
|
||||
- **Genomes** (`/genomes`): top-K ordinati per fitness, ispezione system_prompt + JSON strategy.
|
||||
|
||||
**Strategy crypto** — `strategy_crypto.frontend.nicegui_app` → `https://swarm.tielogic.xyz/strategy_crypto_gui/`:
|
||||
- **Paper** (`/`): forward-test live con equity curve, posizioni aperte, trade list, tick log.
|
||||
|
||||
In produzione subpath gestiti via `DASHBOARD_ROOT_PATH` (per-servizio) + Traefik `replacepathregex` (NB: NON `stripprefix`, vedi sezione Deploy). La root del dominio resta libera per future GUI di altre strategie.
|
||||
|
||||
## Deploy
|
||||
|
||||
`docker-compose.yml` definisce 3 servizi su immagine condivisa `multi-swarm-coevolutive:dev`:
|
||||
|
||||
- **`strategy-crypto-paper`** — runner `scripts/run_paper_trading.py` long-running.
|
||||
- **`strategy-crypto-gui`** — NiceGUI paper dashboard su `https://swarm.${DOMAIN_NAME}/strategy_crypto_gui/`.
|
||||
- **`multi-swarm-core-gui`** — NiceGUI GA dashboard su `https://swarm.${DOMAIN_NAME}/multi_swarm_core_gui/`.
|
||||
|
||||
Persistenza via bind mount: `./data/`, `./series/`, `./state/`. Strategie JSON bind-mounted in read-only dal package: `./src/strategy_crypto/strategy_crypto/strategies/`.
|
||||
|
||||
```bash
|
||||
docker compose up -d --build
|
||||
docker compose logs -f strategy-crypto-paper
|
||||
docker compose ps
|
||||
```
|
||||
|
||||
Note operative:
|
||||
|
||||
- Bind-mount dir devono essere `chown 1000:1000` (uid utente `app` nel container). **Anche `src/strategy_crypto/strategy_crypto/strategies/`** (creata da `git mv`, default `root:root`).
|
||||
- Override del command paper via env (`PAPER_RUN_NAME`, `PAPER_INITIAL_CAPITAL`, ecc.).
|
||||
- `SWARM_DASHBOARD_PORT` controlla la porta interna del container (Traefik fa il TLS).
|
||||
- **Traefik subpath**: usa `replacepathregex` middleware (NON `stripprefix`) per evitare doppio root_path (uvicorn legge `X-Forwarded-Prefix` da stripprefix + applica `root_path` di NiceGUI = doppio prefix). Vedi commit `436613b`.
|
||||
- Dopo cambio label Traefik: `docker restart traefik-traefik-1` per forzare refresh discovery.
|
||||
|
||||
## Sviluppo
|
||||
|
||||
Conventional commits con prefix `feat:` `fix:` `chore:` `docs:` `refactor:` `test:`. Body italiano. Footer `Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>` su ogni commit collaborativo.
|
||||
|
||||
Branch attuale: `main`. Nessun feature branch in Phase 1 (single author, lean spike). Phase 2 valuterà feature branch per ablation paralleli.
|
||||
Branch attuale: `main`. Workspace single-repo, monorepo unificato dal 15 maggio 2026 (split temporaneo monorepo→figlio invertito, vedi tag `v0.1.0-pre-split` come bookmark).
|
||||
|
||||
**Modificare il prompt LLM** senza toccare codice: edita `src/strategy_crypto/strategy_crypto/prompts.json`. Schema documentato in `_design_invariants` del JSON stesso. I 3 regression guard test (`test_strategy_crypto_directives_ascii_safe`, `..._have_archetype_marker`, `..._have_lookback_hint`) bloccano regressioni accidentali sulle invarianti di design.
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
# docker-compose.yml — Multi-Swarm Coevolutive
|
||||
#
|
||||
# Tre servizi della strategia crypto, condividono la stessa immagine
|
||||
# `multi-swarm-coevolutive:dev` buildata dal Dockerfile root (uv workspace):
|
||||
#
|
||||
# * strategy-crypto-paper — paper-trading runner long-running
|
||||
# (scripts/run_paper_trading.py)
|
||||
# * strategy-crypto-gui — NiceGUI dashboard esposta da Traefik su
|
||||
# https://swarm.${DOMAIN_NAME:-tielogic.xyz}/strategy_crypto_gui
|
||||
# * multi-swarm-core-gui — NiceGUI dashboard GA esposta su
|
||||
# https://swarm.${DOMAIN_NAME:-tielogic.xyz}/multi_swarm_core_gui
|
||||
#
|
||||
# Entrambi joinano la rete external `traefik` cosi' il client Cerbero
|
||||
# risolve direttamente l'host `cerbero-mcp` (porta 9000) senza passare
|
||||
# dal gateway pubblico ne' dal TLS.
|
||||
#
|
||||
# Pattern N strategie future: aggiungere strategy-<asset>-paper + strategy-<asset>-gui
|
||||
# con PathPrefix(/strategy_<asset>_gui) e DASHBOARD_ROOT_PATH dedicato.
|
||||
#
|
||||
# Dati persistenti via bind mount dalla cartella del repo:
|
||||
# ./data cache OHLCV intermedia
|
||||
# ./series cache parquet per timeframe/symbol
|
||||
# ./state contiene runs.db (GA) + strategy_crypto.db (paper) + WAL/SHM
|
||||
# ./src/strategy_crypto/strategy_crypto/strategies JSON freezate (ro)
|
||||
#
|
||||
# Secrets (token Cerbero + OpusAgent): caricati da .env via env_file.
|
||||
|
||||
networks:
|
||||
traefik:
|
||||
external: true
|
||||
|
||||
x-swarm-env: &swarm-env
|
||||
# Override: rotta interna verso cerbero-mcp (no TLS, no traefik hop)
|
||||
CERBERO_BASE_URL: http://cerbero-mcp:9000
|
||||
# Override: path container per persistenza
|
||||
DATA_DIR: /app/data
|
||||
SERIES_DIR: /app/series
|
||||
# DB separati per dominio:
|
||||
GA_DB_PATH: /app/state/runs.db
|
||||
STRATEGY_CRYPTO_DB_PATH: /app/state/strategy_crypto.db
|
||||
# DASHBOARD_ROOT_PATH e' ora per-servizio (vedi environment blocks sotto).
|
||||
# IMPORTANT: NON usare StripPrefix middleware con questo. NiceGUI/Starlette
|
||||
# gestisce internamente il root_path su request path che ARRIVANO con prefix.
|
||||
# StripPrefix causa doppio prefix negli asset URL (NiceGUI prefixa + uvicorn
|
||||
# rilegge X-Forwarded-Prefix e prefixa di nuovo).
|
||||
|
||||
services:
|
||||
strategy-crypto-paper:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: multi-swarm-coevolutive:dev
|
||||
container_name: strategy-crypto-paper
|
||||
restart: unless-stopped
|
||||
networks: [traefik]
|
||||
env_file: .env
|
||||
environment:
|
||||
<<: *swarm-env
|
||||
volumes:
|
||||
- ./data:/app/data
|
||||
- ./series:/app/series
|
||||
- ./state:/app/state
|
||||
- ./src/strategy_crypto/strategy_crypto/strategies:/app/strategies:ro
|
||||
# Niente HTTP da controllare: ci affidiamo a `restart: unless-stopped`
|
||||
# e ai log per la liveness del runner.
|
||||
command:
|
||||
- python
|
||||
- /app/scripts/run_paper_trading.py
|
||||
- --name=${PAPER_RUN_NAME:-phase3-papertrade-prod}
|
||||
- --initial-capital=${PAPER_INITIAL_CAPITAL:-1000}
|
||||
- --fees-bp=${PAPER_FEES_BP:-5.0}
|
||||
- --poll-seconds=${PAPER_POLL_SECONDS:-300}
|
||||
- --lookback-bars=${PAPER_LOOKBACK_BARS:-500}
|
||||
- --strategies-dir=/app/strategies
|
||||
labels:
|
||||
- com.centurylinklabs.watchtower.enable=true
|
||||
|
||||
strategy-crypto-gui:
|
||||
image: multi-swarm-coevolutive:dev
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
container_name: strategy-crypto-gui
|
||||
restart: unless-stopped
|
||||
networks: [traefik]
|
||||
env_file: .env
|
||||
environment:
|
||||
<<: *swarm-env
|
||||
DASHBOARD_ROOT_PATH: /strategy_crypto_gui
|
||||
volumes:
|
||||
# RW richiesto: SQLite WAL mode richiede write-access dal reader per SHM.
|
||||
- ./state:/app/state
|
||||
entrypoint:
|
||||
- python
|
||||
- -m
|
||||
- strategy_crypto.frontend.nicegui_app
|
||||
command: []
|
||||
healthcheck:
|
||||
test:
|
||||
- "CMD"
|
||||
- "python"
|
||||
- "-c"
|
||||
- "import os, urllib.request; urllib.request.urlopen(f'http://localhost:{os.environ.get(\"SWARM_DASHBOARD_PORT\",\"8080\")}/', timeout=3).close()"
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
labels:
|
||||
- traefik.enable=true
|
||||
- traefik.docker.network=traefik
|
||||
- "traefik.http.routers.strategy-crypto-gui.rule=Host(`swarm.${DOMAIN_NAME:-tielogic.xyz}`) && PathPrefix(`/strategy_crypto_gui`)"
|
||||
- traefik.http.routers.strategy-crypto-gui.tls=true
|
||||
- traefik.http.routers.strategy-crypto-gui.entrypoints=websecure
|
||||
- traefik.http.routers.strategy-crypto-gui.tls.certresolver=mytlschallenge
|
||||
- "traefik.http.services.strategy-crypto-gui.loadbalancer.server.port=${SWARM_DASHBOARD_PORT:-8080}"
|
||||
# replacepathregex (NON stripprefix): strippa il prefix dalla request senza
|
||||
# aggiungere X-Forwarded-Prefix header. uvicorn vede solo root_path da
|
||||
# ui.run(root_path=...), quindi gli asset URL vengono prefissati una sola
|
||||
# volta (no doppio prefix come succederebbe con stripprefix).
|
||||
- "traefik.http.middlewares.strategy-crypto-replace.replacepathregex.regex=^/strategy_crypto_gui(/.*|$$)"
|
||||
- "traefik.http.middlewares.strategy-crypto-replace.replacepathregex.replacement=$$1"
|
||||
- "traefik.http.routers.strategy-crypto-gui.middlewares=strategy-crypto-replace"
|
||||
- com.centurylinklabs.watchtower.enable=true
|
||||
|
||||
multi-swarm-core-gui:
|
||||
image: multi-swarm-coevolutive:dev
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
container_name: multi-swarm-core-gui
|
||||
restart: unless-stopped
|
||||
networks: [traefik]
|
||||
env_file: .env
|
||||
environment:
|
||||
<<: *swarm-env
|
||||
DASHBOARD_ROOT_PATH: /multi_swarm_core_gui
|
||||
volumes:
|
||||
- ./state:/app/state:ro
|
||||
entrypoint: [python, -m, multi_swarm_core.dashboard.nicegui_app]
|
||||
command: []
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import os, urllib.request; urllib.request.urlopen(f'http://localhost:{os.environ.get(\"SWARM_DASHBOARD_PORT\",\"8080\")}/', timeout=3).close()"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
labels:
|
||||
- traefik.enable=true
|
||||
- traefik.docker.network=traefik
|
||||
- "traefik.http.routers.multi-swarm-core-gui.rule=Host(`swarm.${DOMAIN_NAME:-tielogic.xyz}`) && PathPrefix(`/multi_swarm_core_gui`)"
|
||||
- traefik.http.routers.multi-swarm-core-gui.tls=true
|
||||
- traefik.http.routers.multi-swarm-core-gui.entrypoints=websecure
|
||||
- traefik.http.routers.multi-swarm-core-gui.tls.certresolver=mytlschallenge
|
||||
- "traefik.http.services.multi-swarm-core-gui.loadbalancer.server.port=${SWARM_DASHBOARD_PORT:-8080}"
|
||||
- "traefik.http.middlewares.multi-swarm-core-replace.replacepathregex.regex=^/multi_swarm_core_gui(/.*|$$)"
|
||||
- "traefik.http.middlewares.multi-swarm-core-replace.replacepathregex.replacement=$$1"
|
||||
- "traefik.http.routers.multi-swarm-core-gui.middlewares=multi-swarm-core-replace"
|
||||
- com.centurylinklabs.watchtower.enable=true
|
||||
@@ -1,282 +0,0 @@
|
||||
# Phase 1 Lean Spike — Rapporto Tecnico
|
||||
|
||||
**Autore**: Adriano Dal Pastro
|
||||
**Data**: 10 maggio 2026
|
||||
**Versione**: 1.0 (finalizzato)
|
||||
**Status**: ✅ Phase 1 chiusa, tutti 5 hard gate passati
|
||||
|
||||
**Documenti correlati**:
|
||||
- `docs/superpowers/specs/2026-05-09-decisione-strategica-design.md` (decisione strategica B3)
|
||||
- `docs/superpowers/plans/2026-05-09-phase1-lean-spike.md` (piano implementativo)
|
||||
- `docs/decisions/2026-05-10-gate-phase1.md` (decision memo finale)
|
||||
|
||||
---
|
||||
|
||||
## 1. Setup sperimentale
|
||||
|
||||
L'obiettivo della Phase 1 lean spike è dimostrare che il loop tecnico (LLM hypothesis → backtest falsification → adversarial check → GA selection) funziona end-to-end e produce output formalizzabile. I cinque hard gate definiti nello spec sez. 4.4 misurano feasibility, non alpha edge — quella è valutazione di Phase 2.
|
||||
|
||||
### 1.1 Configurazione del run di riferimento
|
||||
|
||||
Il run `phase1-real-005` (id `1c526996160446b18c0fb57d94874975`) è il primo a superare tutti i gate dopo 4 iterazioni di bug-fix (vedi sez. 3 del decision memo).
|
||||
|
||||
| Parametro | Valore |
|
||||
|---|---|
|
||||
| Population size (K) | 20 |
|
||||
| Generazioni | 10 |
|
||||
| Elite k | 2 |
|
||||
| Tournament k | 3 |
|
||||
| Crossover probability | 0.5 |
|
||||
| Random seed | 42 |
|
||||
| Symbol | BTC-PERPETUAL (Deribit) |
|
||||
| Timeframe | 1h |
|
||||
| Range storico | 2024-01-01 → 2026-01-01 (2 anni, 17545 candele) |
|
||||
| Fees backtest | 5 basis points |
|
||||
| n_trials_dsr | 50 |
|
||||
| Tier LLM dominante | C (qwen3-235b-a22b-2507 via OpenRouter) |
|
||||
| Cerbero MCP endpoint | http://localhost:9001 (locale) |
|
||||
| Durata wall-clock | 29 minuti |
|
||||
| Costo LLM | $0.069 |
|
||||
|
||||
### 1.2 Stack tecnologico
|
||||
|
||||
Python 3.13, uv 0.10.9. Test framework: pytest + pytest-mock + responses. Persistence: sqlite3 + sqlmodel. Parsing strategia: `json.loads` con dataclass-based AST. Analytics: pandas + numpy + scipy. LLM: openai SDK con base URL OpenRouter (route unica per tutti i tier S/A/B/C/D). HTTP: requests + tenacity. Dashboard: streamlit + plotly + canvas HTML5 custom.
|
||||
|
||||
### 1.3 Architettura del run
|
||||
|
||||
L'orchestrator (`src/multi_swarm/orchestrator/run.py`, 184 righe) coordina la pipeline end-to-end:
|
||||
|
||||
1. **OHLCV loading**: `CerberoOHLCVLoader` chiama `mcp-deribit/tools/get_historical` paginando in chunk da 4500 barre (cap soft Deribit ~5000). Cache parquet su sha1 della query — il run v5 ha riusato cache popolata dai run precedenti, fetch istantaneo.
|
||||
2. **Market summary**: statistiche return (mean, std, skew, kurt) + classificazione regime volatilità.
|
||||
3. **Initial population**: 20 genomi distribuiti uniformemente sui 6 cognitive style (physicist, biologist, historian, meteorologist, ecologist, engineer), temperature random in [0.7, 1.2], lookback random in {100, 150, 200, 300}.
|
||||
4. **Per ogni generazione (10 totali)**:
|
||||
- **Hypothesis**: chiamata LLM con prompt SYSTEM (regole grammar) + USER (market summary). Output JSON estratto via regex fence ```json. Se parse/validation fallisce: retry 1x con error message nel prompt utente.
|
||||
- **Falsification**: AST compilato in `Callable[[df], Series[Side]]`, backtest event-driven con 1-bar exec delay, calcolo Sharpe + Deflated Sharpe (Bailey & López 2014, n_trials=50).
|
||||
- **Adversarial**: 4 check euristici (no_trades, degenerate, overtrading, undertrading).
|
||||
- **Fitness**: `0.5*dsr + 0.25*(tanh(sharpe)+1)` × `1/(1+max_dd)`, range [0, ~1]. Kill (=0) su zero trade o HIGH adversarial finding.
|
||||
- **Next generation**: elitism 2 + tournament 3 + 50% crossover / 50% mutation.
|
||||
5. **Persistence SQLite**: ogni genome, evaluation, cost_record, adversarial_finding, generation summary persistito con indici per query rapide della dashboard.
|
||||
|
||||
### 1.4 Caveat metodologici noti
|
||||
|
||||
- **In-sample**: il backtest in Phase 1 lean spike non usa walk-forward; tutto il range 2024-2026 viene usato sia per la generazione delle ipotesi sia per la loro valutazione. La sopravvivenza out-of-sample è esplicitamente fuori scope di Phase 1 (gate Phase 2 #2).
|
||||
- **Compiler con indicatori built-in**: il compiler JSON-based (`src/multi_swarm/protocol/compiler.py`) calcola RSI, SMA, ATR, MACD, realized_vol localmente con pandas. `CerberoTools` è plumbed ma non chiamato durante l'esecuzione delle strategie — è disponibile per agenti future-tense ma il fitness Phase 1 dipende solo dagli indicatori locali.
|
||||
- **RSI epsilon-floor**: il compiler ha un epsilon sul `roll_down` per evitare RSI=100 esatto su serie monotonicamente crescenti (artefatto matematico irrilevante su dati reali ma documentato).
|
||||
- **Top-1 strategia con DSR marginale**: vedi sez. 3.
|
||||
|
||||
---
|
||||
|
||||
## 2. Loop convergence
|
||||
|
||||
### 2.1 Fitness per generazione
|
||||
|
||||
| Gen | Median | Max | P90 | Entropy |
|
||||
|---|---|---|---|---|
|
||||
| 0 | 0.0001 | 0.0601 | 0.0165 | 0.588 |
|
||||
| 1 | 0.0042 | 0.1893 | 0.0731 | 1.261 |
|
||||
| 2 | 0.0188 | 0.3347 | 0.2039 | 1.333 |
|
||||
| 3 | 0.0069 | 0.3347 | 0.3347 | 1.347 |
|
||||
| 4 | 0.0910 | 0.3347 | 0.3347 | 1.415 |
|
||||
| 5 | 0.0016 | 0.3347 | 0.3347 | 0.611 |
|
||||
| 6 | 0.0040 | 0.3347 | 0.3347 | 0.886 |
|
||||
| 7 | 0.0151 | 0.3347 | 0.3347 | 0.982 |
|
||||
| 8 | 0.0066 | 0.3347 | 0.3347 | 0.746 |
|
||||
| 9 | 0.0061 | 0.3347 | 0.3347 | 0.914 |
|
||||
|
||||
### 2.2 Lettura
|
||||
|
||||
**Convergenza tre-step iniziale**: gen 0→1→2 mostra crescita mediana 4x-50x (0.0001 → 0.0042 → 0.0188) e crescita max 3x-6x (0.06 → 0.19 → 0.33). Gate 1 PASS su questa finestra.
|
||||
|
||||
**Plateau dell'elite da gen 2**: max stabile a 0.3347 per le restanti 7 generazioni — comportamento atteso con `elite_k=2` che preserva il top performer attraverso le generazioni. P90 si allinea al max da gen 3, segno che almeno 2 elite mantengono la top fitness.
|
||||
|
||||
**Median oscillante**: dopo il picco a gen 4 (0.091), la median fluttua fra 0.0016 e 0.0151 nelle generazioni successive. Causa: turnover stocastico della popolazione (mutation + crossover) introduce genomi nuovi, alcuni dei quali parse correctly ma falliscono Adversarial (no_trades) e si attestano a fitness 0, abbassando la median. Non è regressione strutturale del GA.
|
||||
|
||||
**Entropy**: oscilla 0.6-1.4 dopo gen 0, sempre sopra soglia 0.5 → diversità di fitness preservata anche durante plateau dell'elite.
|
||||
|
||||
---
|
||||
|
||||
## 3. Top-5 genomi: ispezione qualitativa
|
||||
|
||||
| Rank | Genome ID | Gen | Style | Fitness | DSR | Sharpe | Max DD | Trades | Temp |
|
||||
|---|---|---|---|---|---|---|---|---|---|
|
||||
| 1 | `696052b8...` | 2 | physicist | 0.3347 | 0.0021 | 0.381 | 0.0215 | 33 | 0.68 |
|
||||
| 2 | `169376a2...` | 1 | engineer | 0.3347 | 0.0021 | 0.381 | 0.0215 | 33 | 0.78 |
|
||||
| 3 | `eb0265ad...` | 3 | ecologist | 0.2453 | 0.0006 | −0.019 | 0.0011 | 1 | 1.14 |
|
||||
| 4 | `38d4c1d9...` | 1 | engineer | 0.1893 | 0.0001 | −0.245 | 0.0028 | 1 | 0.82 |
|
||||
| 5 | `3e355975...` | 1 | physicist | 0.1893 | 0.0001 | −0.245 | 0.0028 | 1 | 0.78 |
|
||||
|
||||
### 3.1 Top-1 strategia (ispezione approfondita)
|
||||
|
||||
**System prompt** (engineer): *"Cerca segnali con rapporto S/N favorevole, filtri causali, robustezza a perturbazioni di calibrazione."*
|
||||
|
||||
**Strategia JSON** (3 regole, evaluation in ordine):
|
||||
|
||||
- **LONG**: `SMA(10) crossover SMA(30)` AND `realized_vol(20) > 0.3%` AND `RSI(14) < 45`.
|
||||
- **SHORT**: `SMA(10) crossunder SMA(30)` AND `realized_vol(20) > 0.3%` AND `RSI(14) > 55`.
|
||||
- **EXIT**: (`RSI(14) > 70` AND `close crossover SMA(50)`) OR `realized_vol(20) < 0.1%`.
|
||||
|
||||
**Lettura economica**: trend-following SMA-cross fast/slow modulato da filtro volatilità (entra solo quando il regime è abbastanza mosso, esce quando è troppo calmo) e filtro RSI come momentum confirmation (long solo se non già ipercomprato; short solo se non già ipervenduto). L'EXIT è sofisticato: esce su overbought confermato da break sopra MA50, OPPURE su collasso di volatilità.
|
||||
|
||||
**Performance**: 33 trade su 17545 candele (1 trade ogni 532 candele = 1 ogni 22 giorni). Sharpe positivo modesto, max drawdown 2.15% (basso). DSR praticamente zero (0.0021) — il segnale non è statisticamente significativo dopo correzione multiple testing, perché 33 trade su 2 anni è sample piccolo.
|
||||
|
||||
**Plausibilità**: pattern economicamente sensato, non casuale. Reminiscente di strategie trend-following classiche (Donchian, turtle-style) con filtri di regime. Lo stile cognitivo "engineer" (S/N favorable, filtri causali) si riflette nella struttura.
|
||||
|
||||
### 3.2 Top-2/3/4/5 brevemente
|
||||
|
||||
- Top-2 è una replica funzionale di Top-1 con metriche identiche. Plausibile elite duplicato o convergenza indipendente sulla stessa strategia (verifica per Phase 2: signal correlation fra duplicati).
|
||||
- Top-3, 4, 5 hanno **1 trade ciascuno** su 2 anni. Sono "lucky shot": una posizione tenuta a lungo che casualmente termina con leggera vincita. Adversarial flagga MEDIUM `undertrading` ma non HIGH, quindi sopravvivono. La fitness function continua dà loro valore non-zero perché `tanh(sharpe)` è leggermente sopra 0.5 e penalty drawdown è quasi 1.0 (max_dd <0.5%).
|
||||
|
||||
### 3.3 Ratio top-1 / median
|
||||
|
||||
Median fitness su 98 evals: 0.0003.
|
||||
Top-1 fitness: 0.3347.
|
||||
**Ratio**: 1116x — Gate 3 soddisfatto con margine drammatico (soglia 1.5x).
|
||||
|
||||
---
|
||||
|
||||
## 4. Parser failure modes
|
||||
|
||||
### 4.1 Statistiche aggregate v5
|
||||
|
||||
- Evaluations totali: 98
|
||||
- Parse success: **98 (100.0%)**
|
||||
- Parse failure: **0 (0.0%)**
|
||||
|
||||
### 4.2 Confronto con iterazioni precedenti
|
||||
|
||||
| Run | Grammar | Parse success | Note |
|
||||
|---|---|---|---|
|
||||
| v1 | S-expression | 33% | LLM nesta indicators non supportati |
|
||||
| v4 | S-expression (con arity check post-fix) | 36% | 89 di 98 errori = `indicator nested` |
|
||||
| v5 | **JSON Schema** | **100%** | Refactor commit `44eb643` |
|
||||
|
||||
Il salto da 36% a 100% deriva interamente dal cambio di grammar. JSON è natively supported dal training dei modelli LLM moderni; S-expression è esotica e induce hallucination di sintassi creative.
|
||||
|
||||
### 4.3 Retry-with-feedback (commit `d4fcb42`)
|
||||
|
||||
Il sistema accetta 1 retry con error feedback. Nel run v5 il retry **non è mai stato usato** (zero retry per parse, dato il 100% di success). Il retry rimane comunque architetturalmente presente per Phase 2 / casi edge.
|
||||
|
||||
---
|
||||
|
||||
## 5. Costi reali vs preventivo
|
||||
|
||||
### 5.1 Breakdown costi LLM v5
|
||||
|
||||
| Tier | Calls | Input tokens | Output tokens | Cost USD |
|
||||
|---|---|---|---|---|
|
||||
| C (qwen3-235b) | 113 | 112369 | 60060 | $0.069 |
|
||||
|
||||
### 5.2 Costo cumulativo Phase 1 (5 run, inclusi bug-fix iterations)
|
||||
|
||||
| Run | Cost | Note |
|
||||
|---|---|---|
|
||||
| v1 (aborted) | $0.034 | 67% parse_error, max_dd bug |
|
||||
| v2 (aborted) | $0.018 | macd 3 args, OHLCV cap discovery |
|
||||
| v3 (aborted) | $0.015 | crash su indicator arity |
|
||||
| v4 (completed FAIL) | $0.057 | 36% parse, fitness tutti 0 |
|
||||
| v5 (completed PASS) | $0.069 | tutti gate passati |
|
||||
| **Totale Phase 1** | **$0.193** | — |
|
||||
|
||||
### 5.3 Confronto con preventivo
|
||||
|
||||
- Preventivo originale (basato su pricing Anthropic Sonnet): $500-700.
|
||||
- Spesa reale Phase 1 totale: **$0.19**.
|
||||
- Deviazione: −99.97%.
|
||||
|
||||
La differenza non è dovuta a underuse — il run v5 ha fatto 113 chiamate LLM = full saturazione del budget previsto di calls. È un cambio di ordine di grandezza nei prezzi dovuto al pricing aggressivo di OpenRouter per modelli open-weights (qwen3-235b è 7.5x più economico di Sonnet su input, 37x su output). Il preventivo originale era calibrato su Sonnet 4.6.
|
||||
|
||||
### 5.4 Implicazioni per Phase 2
|
||||
|
||||
Il margine economico permette di pianificare Phase 2 con maggiore aggressività senza superare il cap ($700-1100):
|
||||
- K=40 (×2), gen=15 (×1.5), tier mix 30% B / 70% C, ablation runs multiple.
|
||||
- Estrapolazione lineare conservativa: $0.07 × 2 × 1.5 × ~3 (tier B factor) × 5 (ablation) = ~$3 totali. Possibile spingere a $30-50 senza preoccupazioni se serve per ablation più ricche.
|
||||
|
||||
**Rischio cost-trap inverso**: tentazione di sovra-dimensionare Phase 2 perché "tanto costa nulla". Mantenere disciplina budget invariata — investire i $700 cap in PIÙ ablation, non in run più grandi.
|
||||
|
||||
---
|
||||
|
||||
## 6. Diversity metrics
|
||||
|
||||
### 6.1 Entropy fitness per generazione
|
||||
|
||||
Vedi tabella sez. 2.1 colonna entropy. Mai sotto 0.5, picco a gen 4 (1.415).
|
||||
|
||||
### 6.2 Cognitive style sopravvissuti gen 9
|
||||
|
||||
| Stile | Count gen 9 | Avg fitness | Note |
|
||||
|---|---|---|---|
|
||||
| engineer | 3 | 0.0 | Dominante numericamente ma fitness 0 (genomi recent, non valutati su elite) |
|
||||
| physicist | 1 | 0.0598 | Solo presente nel top-K |
|
||||
| historian | 1 | 0.0002 | — |
|
||||
| biologist | 0 | — | Estinto |
|
||||
| meteorologist | 0 | — | Estinto |
|
||||
| ecologist | 0 | — | Estinto |
|
||||
|
||||
**Lettura**: pressione selettiva ha eliminato 3 di 6 stili cognitivi alla generazione finale. Engineer è dominante numericamente, physicist domina nel valore (l'unico con fitness >0 della popolazione "live" gen 9). Phase 2 deve introdurre speciation esplicita per evitare questo collasso (minimum 2-3 specie protette).
|
||||
|
||||
### 6.3 Trade distribution sui 98 evals
|
||||
|
||||
| Categoria | n | % |
|
||||
|---|---|---|
|
||||
| Zero trade (HIGH no_trades, kill) | 42 | 42.9% |
|
||||
| Undertrading (1-4 trade, MEDIUM) | 5 | 5.1% |
|
||||
| Normal (5-100 trade) | 9 | 9.2% |
|
||||
| Overtrading (>100 trade, NON flaggato) | 42 | 42.9% |
|
||||
|
||||
**Issue identificato**: il 42.9% di overtrading non viene catturato dall'Adversarial perché la soglia attuale è `n_trades > n_bars/5 = 3509` — troppo alta per essere triggerata su 1000-2000 trade. Phase 2 dovrebbe abbassare a `n_bars/20 = 877` o usare metrica relativa al regime.
|
||||
|
||||
### 6.4 Adversarial findings totali
|
||||
|
||||
| Finding | Severity | Count |
|
||||
|---|---|---|
|
||||
| no_trades | HIGH | 42 |
|
||||
| undertrading | MEDIUM | 5 |
|
||||
|
||||
Niente `degenerate` né `overtrading` flaggato. Il primo è raro (richiede strategia sempre-LONG o sempre-SHORT puro), il secondo soffre della soglia troppo alta.
|
||||
|
||||
---
|
||||
|
||||
## 7. Threats to validity
|
||||
|
||||
Lista esplicita dei limiti metodologici da non sovra-interpretare:
|
||||
|
||||
1. **In-sample fitting**: tutto il backtest è in-sample. Il top-1 ha Sharpe 0.38 ottenuto guardando i dati su cui è stato selezionato. Phase 2 (walk-forward + hold-out Q1-Q2 2026 intoccabile) misura overfitting reale.
|
||||
2. **Tier C unico**: nessun confronto contro tier B/S. Possibile underperformance del LLM economico vs Sonnet/Opus. Phase 2 introduce ablation multi-tier.
|
||||
3. **Adversarial hand-crafted**: 4 check euristici (no_trades, degenerate, overtrading, undertrading). Phase 2 introduce 5 prompt LLM-driven dedicati (data snooping, lookahead, regime fragility, crowding, transaction cost erosion).
|
||||
4. **Fitness function v1**: lineare in DSR + tanh(Sharpe) normalizzato + drawdown moltiplicativa. Non multi-livello (per-team, anti-collusion). Phase 2 introduce.
|
||||
5. **No speciation, no novelty bonus**: cognitive style scendono da 6 a 3 a gen 9. Phase 2 deve mitigare.
|
||||
6. **DSR del top-1 = 0.0021**: il "successo" del Gate 3 è guidato da Sharpe (positivo modesto), non da significatività statistica vera. Senza walk-forward + multiple testing rigoroso, non si può affermare alpha edge.
|
||||
7. **Top-3/4/5 sono "lucky shot" 1-trade**: la fitness function continua li promuove perché drawdown bassissimo + sharpe leggermente negativo, ma sono artefatti. Phase 2 promuove undertrading a HIGH se `n_trades < 10`.
|
||||
8. **Cerbero/Deribit data quality**: nessuna detection di gap, outlier, exchange downtime. Da affrontare prima di forward-test (Phase 3).
|
||||
9. **Cost predictability inverso**: Phase 2 deve resistere alla tentazione di sovra-dimensionare perché Phase 1 è costata $0.19.
|
||||
|
||||
---
|
||||
|
||||
## 8. Conclusioni e implicazioni per Phase 2
|
||||
|
||||
**Hard gate sintesi**: ✅ 5 su 5 passati.
|
||||
|
||||
**Decisione finale**: **GO Phase 2** (formalizzata nel decision memo).
|
||||
|
||||
**Apprendimenti chiave per Phase 2**:
|
||||
|
||||
1. **JSON >> S-expression** per grammar LLM-generated. Phase 2 non rivisita.
|
||||
2. **Fitness continua è essenziale** per dare gradient al GA, ma può promuovere strategie degeneri (1-trade) che vanno killate diversamente.
|
||||
3. **OpenRouter qwen3-235b** è sorprendentemente capace per generare strategie strutturate, dato un prompt schema-rigoroso. Tier B (Sonnet) potrebbe non essere necessario al 30% come pianificato; ablation Phase 2 misurerà il vero contributo.
|
||||
4. **Cerbero MCP come single source of truth** funziona: paginazione, cache parquet, audit log integrati senza fragility.
|
||||
5. **Bug-fix discovery via run reale** è efficiente: 4 cicli, ognuno ha esposto un problema specifico (max_dd math, macd arity, validator arity, fitness clamp, grammar choice). Phase 2 può aspettarsi pattern simile per nuove componenti (speciation edge cases, OOS overfitting, multi-tier dispatch).
|
||||
|
||||
**Riusabilità del codebase Phase 1**: il design modulare (data, backtest, metrics, cerbero, protocol, genome, llm, agents, ga, persistence, orchestrator, dashboard) è riusabile direttamente. Estensioni Phase 2:
|
||||
- `ga/speciation.py` (nuovo) — clustering cosine similarity prompt, quota tournament per specie.
|
||||
- `ga/fitness.py` — versione v2 con novelty bonus + per-team aggregation.
|
||||
- `orchestrator/run.py` — integrazione walk-forward.
|
||||
- `agents/adversarial_llm.py` (nuovo) — 5 prompt LLM-driven.
|
||||
- `baseline/random_forest.py` (nuovo) — RF baseline per benchmark.
|
||||
|
||||
**Costo stimato Phase 2**: $3-15 (estrapolazione molto conservativa). Cap rimane $700-1100 invariato per disciplina.
|
||||
|
||||
**Tempo stimato Phase 2**: 4-6 settimane di lavoro calendar, includendo i 3 aggiustamenti del decision memo (Adversarial soglie, speciation, walk-forward).
|
||||
|
||||
---
|
||||
|
||||
*Documento finalizzato 10 maggio 2026. Versione 1.0.*
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,427 +0,0 @@
|
||||
# Decisione Strategica PoC Multi-Swarm Coevolutivo — Design
|
||||
|
||||
**Autore**: Adriano Dal Pastro
|
||||
**Data**: 9 maggio 2026
|
||||
**Status**: Design strategico approvato per implementazione
|
||||
**Versione**: 1.0
|
||||
**Documenti correlati**:
|
||||
- `00_documento_zero.md` (framework concettuale)
|
||||
- `coevolutive_swarm_system.md` (Filone A, sistema completo)
|
||||
- `poc_trading_swarm.md` (Filone B, design PoC trading)
|
||||
|
||||
---
|
||||
|
||||
## 1. Executive Summary
|
||||
|
||||
Questo documento formalizza la decisione strategica su come avviare il progetto Multi-Swarm Coevolutivo. La scelta cade sulla variante **B3 — PoC trading incrementale a tre fasi con gate go/no-go**, una declinazione disciplinata dell'opzione Smart Spike (Filone B) descritta nel documento zero.
|
||||
|
||||
La forma incrementale tripartita non sostituisce il design del PoC contenuto in `poc_trading_swarm.md`, ne organizza l'esecuzione in fasi successive con kill-switch numerici espliciti. Il principio guida è **applicare al progetto stesso la disciplina che il sistema dovrà applicare alle proprie ipotesi**: spegnere ciò che non funziona quando i dati lo dicono, senza sconti emotivi e senza giudizi soggettivi sui hard gate.
|
||||
|
||||
**Vincoli operativi adottati**:
|
||||
|
||||
| Dimensione | Valore |
|
||||
|---|---|
|
||||
| Obiettivo primario | Sistema produttivo che generi valore (trading reale, fase posteriore al PoC) |
|
||||
| Dominio iniziale | Derivati crypto BTC/ETH |
|
||||
| Tempo committed | Full-time, oltre 30h settimanali |
|
||||
| Budget LLM cap | $2.200 hard, segmentato per fase |
|
||||
| Capitale a rischio | $500-2.000 (solo nella Phase 3 forward-test mainnet) |
|
||||
| Tempo calendario | 14-18 settimane atteso, 20 settimane hard cap |
|
||||
| Setup tecnico di partenza | Cerbero_mcp operativo (multi-exchange, indicatori, audit, dual env) |
|
||||
|
||||
**Esito atteso**: alla fine delle tre fasi, una decisione binaria documentata con razionale numerico fra (a) avviare il sistema completo Filone A con confidence empirica forte, (b) iterare la Phase 2 su debolezze identificate, (c) pivotare su un dominio diverso (offerte commerciali Tielogic, code review), oppure (d) chiudere il progetto con learnings registrati.
|
||||
|
||||
---
|
||||
|
||||
## 2. Razionale della scelta strategica
|
||||
|
||||
### 2.1 Le tre opzioni del documento zero, oggi
|
||||
|
||||
Il documento zero presenta tre opzioni: A (Big Bet, sistema completo, 12-18 mesi), B (Smart Spike, PoC trading, 3-4 mesi), C (Research Dive, paper review più PoC minimo, 1-2 mesi). Alla luce dei vincoli operativi sopra elencati, la valutazione cambia rispetto al momento in cui il documento zero è stato scritto.
|
||||
|
||||
L'opzione A è esclusa dal vincolo budget. Le stime conservative del Filone A indicano costi LLM nell'ordine di $10.000-30.000 anche solo per la fase iniziale, contro un cap committed di $2.200. Non è una questione di tempo: il tempo full-time c'è. È che il rapporto fra costo del run e disponibilità non lo rende sostenibile senza una validazione empirica preliminare.
|
||||
|
||||
L'opzione C sotto-utilizza due asset materiali. Il primo è la disponibilità full-time, che rende il vincolo "non posso permettermi di costruire infrastruttura" non applicabile. Il secondo è Cerbero_mcp, già operativo: leggere paper per due settimane senza scrivere codice produttivo significherebbe lasciare ferma un'infrastruttura già pronta a essere wrappata come tool layer per agenti LLM.
|
||||
|
||||
Resta l'opzione B. Il documento zero la indicava come scelta preferita; questo documento la conferma e la struttura.
|
||||
|
||||
### 2.2 Perché B3 e non B1 o B2
|
||||
|
||||
Tre varianti di B sono state considerate.
|
||||
|
||||
La variante **B1 (Lean / single-shot)** comprime il PoC in un'unica run con popolazione ridotta a 20-30 agenti e tier C unico. Coerente con il budget tight, ma rischiosa: la dimensione della popolazione è il principale moltiplicatore di diversità nel sistema, e una popolazione undersized rischia di produrre un falso negativo. Si decreterebbe il sistema "non funzionante" quando in realtà non gli abbiamo dato il numero di tentativi necessari.
|
||||
|
||||
La variante **B2 (Canonical / come da documento)** segue fedelmente `poc_trading_swarm.md` con K=50, mix tier B/C, full set di ablation. Tecnicamente solida ma sfora il budget cap di un fattore 1.5-2x. Adottabile solo accettando di alzare il cap a $3-4K, decisione non giustificabile senza segnali empirici preliminari.
|
||||
|
||||
La variante **B3 (Incrementale)**, scelta, articola il PoC in tre fasi sequenziali con cap budget per fase e gate decisionali quantitativi fra una fase e la successiva. Phase 1 valida il loop tecnico con popolazione minima e tier economico. Phase 2 esegue il PoC canonico solo se Phase 1 passa. Phase 3 forward-testa con capitale reale solo se Phase 2 passa. Il budget totale resta entro $2.2K e il rischio di falso negativo viene ridotto dal fatto che la popolazione completa di Phase 2 non viene mai tagliata: viene messa al lavoro solo dopo che il loop è stato validato.
|
||||
|
||||
La struttura tripartita ha anche un beneficio non monetario: i deliverable di Phase 1 e Phase 2 valgono anche se le fasi successive falliscono. Il backtest engine, il GA harness, la Cerbero integration, la dashboard Streamlit, la pipeline DSR sono tutti riusabili in caso di pivot di dominio. Solo il capitale di Phase 3 è genuinamente "a rischio" come costo della validazione.
|
||||
|
||||
### 2.3 Coerenza con la filosofia del progetto
|
||||
|
||||
Il documento zero al §3.3 identifica come takeaway fondamentale di Renaissance la disciplina di spegnere strategie quando l'edge svanisce, senza decisioni emotive. Il design B3 applica questa disciplina al progetto stesso, prima ancora che al sistema. I gate sono numerici, le soglie sono fissate prima di vedere i dati, l'azione di stop è meccanica quando un hard gate fallisce. Non c'è spazio per "magari un'altra generazione" o "i risultati erano quasi ottimi": la decisione è già scritta nel design.
|
||||
|
||||
---
|
||||
|
||||
## 3. Vincoli di terminazione globali
|
||||
|
||||
Tre kill-switch globali sopra le singole fasi:
|
||||
|
||||
1. **Cap budget LLM globale**: $2.200 hard. Spesa effettiva monitorata da pagina Overview della dashboard, contatore aggiornato dopo ogni batch. Sforamento previsto entro la fase corrente → riformulazione scope, non incremento cap.
|
||||
2. **Cap tempo calendario**: 14-18 settimane attese, **20 settimane hard cap** dalla settimana 1 della Phase 1 alla decisione formale post-Phase 3. Sforamento previsto del cap → decisione anticipata sulla base dei dati raccolti, non estensione del cap.
|
||||
3. **Hard gate falliti**: per costruzione, un hard gate fallito chiude la fase corrente e non apre quella successiva. La decisione fra stop, pivot, o iterazione è formalizzata nel decision memo della fase chiusa, non nel passaggio automatico alla successiva.
|
||||
|
||||
---
|
||||
|
||||
## 4. Phase 1 — Lean Spike
|
||||
|
||||
**Obiettivo**: dimostrare che il loop tecnico funziona end-to-end. Non si misura ancora alpha: si misura se il sistema gira come progettato, se l'output LLM è formalizzabile, se la GA converge, se i costi sono prevedibili.
|
||||
|
||||
### 4.1 Scope IN
|
||||
|
||||
- Infrastruttura backtest minima: dataset 2 anni (2024-2026) di OHLCV 1h BTC/ETH, engine event-driven semplificato (no microstructure, no slippage modelling complesso, fees fissi a 5 basis points), walk-forward expanding 70/30.
|
||||
- Wrapper Cerbero come tool layer per agenti: i tool MCP esistenti (`indicators`, `vol_cone`, `oi_weighted_skew`, indicatori options/microstructure/stats) tradotti in funzioni callable dagli agenti. Niente reimplementazione, solo wrapping.
|
||||
- Protocollo S-expression fisso: 12-15 verbi disegnati manualmente come da `poc_trading_swarm.md` §2.2. Nessuna evoluzione del protocollo in questa fase.
|
||||
- Hypothesis Swarm con K=20 agenti, tier C unico (Qwen 2.5 72B via OpenRouter). Mutazione e crossover prompt come da documento PoC. Nessuna speciation, nessun novelty bonus.
|
||||
- Falsification e Adversarial layer hand-crafted, un agente fisso per ognuno, prompt manuali. Tier B (Sonnet 4.6) chiamato solo per i top-5 candidati a fine generazione, per contenere costi.
|
||||
- Fitness function v0: DSR in-sample con drawdown penalty. No multi-livello, no out-of-sample ancora.
|
||||
- GA loop: 8-12 generazioni, tournament selection, elitism k=2.
|
||||
|
||||
### 4.2 Scope OUT (esplicito)
|
||||
|
||||
- Multi-tier ablation comparativa.
|
||||
- Out-of-sample DSR e hold-out finale.
|
||||
- Random Forest baseline.
|
||||
- Speciation, novelty, diversity metrics.
|
||||
- Forward-test con capitale reale.
|
||||
- Domini diversi da BTC/ETH.
|
||||
|
||||
### 4.3 Budget e tempo
|
||||
|
||||
- LLM: $500-700. Stima base: 20 agenti × ~8.000 token output medi × 10 generazioni × pricing Qwen ≈ $300, più 50% di overhead per Adversarial, Falsification e iterazioni di sviluppo, totale stimato $500-700.
|
||||
- Tempo: 4-6 settimane full-time. Settimane 1-2 backtest engine e Cerbero wrapper, settimana 3 GA infrastructure e parser S-expression, settimane 4-5 tuning e run completo, settimana 6 analisi e decisione gate.
|
||||
- Capitale a rischio: zero.
|
||||
|
||||
### 4.4 Gate go/no-go (tutti AND)
|
||||
|
||||
Cinque hard gate:
|
||||
|
||||
1. **Loop converge**: la fitness mediana della popolazione cresce per almeno tre generazioni consecutive prima di plateau.
|
||||
2. **Output formalizzabile**: almeno l'80% delle proposte LLM passano il parser S-expression senza intervento manuale.
|
||||
3. **Tail superiore esiste**: i top-5 genomi hanno DSR in-sample pari ad almeno 1.5x la mediana di popolazione, segnale che esiste struttura e non solo rumore.
|
||||
4. **Diversità non collassa**: entropia della distribuzione di fitness in popolazione superiore a 0.5 a fine run, evita la convergenza monocoltura.
|
||||
5. **Cost predictability**: spesa effettiva entro ±30% della stima preventivata.
|
||||
|
||||
Anche un solo hard gate fallito chiude la Phase 1. Decisione successiva (pivot, ridiscussione design, stop) presa nel decision memo, non automaticamente in Phase 2.
|
||||
|
||||
### 4.5 Deliverable Phase 1
|
||||
|
||||
- Codice testato (pytest): backtest engine, Cerbero wrapper, GA loop, protocol parser.
|
||||
- Report tecnico (~5 pagine): loop convergence con grafici, ispezione qualitativa dei top-5 genomi, parser failure modes osservati, costi reali vs preventivo, diversity metrics.
|
||||
- Decision memo: vai/non-vai a Phase 2 con eventuali aggiustamenti di scope.
|
||||
|
||||
---
|
||||
|
||||
## 5. Phase 2 — Canonical PoC
|
||||
|
||||
**Obiettivo**: rispondere ai cinque test del PoC originale (`poc_trading_swarm.md` §1) con popolazione e infrastruttura adeguate. Solo questa fase produce una vera misura dell'edge potenziale del sistema.
|
||||
|
||||
### 5.1 Scope IN (in aggiunta a Phase 1)
|
||||
|
||||
- Hypothesis Swarm K=40, scaling della popolazione a un livello canonico ridotto (K=50 documento → K=40 per disciplina budget).
|
||||
- Tier mix B/C: circa 70% Qwen/DeepSeek (tier C), 30% Sonnet 4.6 (tier B). L'ablation comparativa misura il valore aggiunto del tier B.
|
||||
- Speciation di base: clustering dei genomi per cosine similarity dei prompt più cognitive_style. Si mantengono almeno 3 specie attive, ognuna con quote di tournament protette.
|
||||
- Novelty bonus: fitness composta α·DSR_OOS + β·novelty_score, dove la novelty è calcolata come distanza behavioural dei segnali rispetto a un archivio di elite.
|
||||
- Walk-forward expanding più hold-out finale: training Q1-2024 a Q4-2025 in walk-forward, hold-out intoccabile Q1-Q2 2026.
|
||||
- Random Forest baseline: feature engineering classico (returns multi-orizzonte, RSI/MACD/ATR, vol cone, funding rate, OI changes), classificazione long/flat/short su orizzonti 1d e 4h, valutato sulla stessa hold-out.
|
||||
- Adversarial layer hand-crafted potenziato: cinque prompt distinti (data snooping, lookahead, regime fragility, crowding, transaction cost erosion) eseguiti sui top-10 candidati prima della valutazione OOS.
|
||||
- Falsification con Deflated Sharpe Ratio (Bailey & López de Prado), correzione Bonferroni sul numero totale di ipotesi testate.
|
||||
- Fitness multi-livello: per-agent (contributo DSR), per-team (DSR portfolio), diversity penalty per ridurre collusione.
|
||||
|
||||
### 5.2 Scope OUT
|
||||
|
||||
- Co-evolution del protocollo (Filone A).
|
||||
- Forward-test con capitale reale (Phase 3).
|
||||
- Speciation NEAT-style completa.
|
||||
- Idiom emergence.
|
||||
- Domini diversi da trading.
|
||||
|
||||
### 5.3 Budget e tempo
|
||||
|
||||
- LLM: $700-1.100. Dettaglio stimato: tier C ~$500, tier B ~$400, overhead per ablation iterativa ~$200. Range ampio per consentire più cicli di ablation se i primi risultati lo richiedono.
|
||||
- Tempo: 4-6 settimane full-time. Settimana 1 porting K=40, speciation, novelty. Settimane 2-3 ablation runs (tier C only, tier B only, mix; con e senza speciation). Settimana 4 hold-out evaluation, RF baseline, Adversarial sweep. Settimane 5-6 analisi statistica, report, decisione gate.
|
||||
- Capitale a rischio: zero.
|
||||
|
||||
### 5.4 Gate go/no-go
|
||||
|
||||
**Hard gate (tutti AND, altrimenti stop)**:
|
||||
|
||||
1. **Significatività statistica**: top genoma su hold-out con DSR > 1.0 e p-value < 0.05 dopo correzione Bonferroni.
|
||||
2. **Sopravvivenza regime change**: DSR hold-out almeno 0.5x del DSR walk-forward — limite contro overfitting catastrofico.
|
||||
3. **Batte baseline**: top-3 genomi con Sharpe OOS superiore al Sharpe RF baseline OOS, effect size non trascurabile (Cohen's d > 0.3 sul rolling Sharpe).
|
||||
|
||||
**Soft gate (informano, non killano)**:
|
||||
|
||||
4. **Diversità**: almeno 3 specie distinte sopravvivono a fine run, top-3 genomi non identici per signal correlation (ρ < 0.7).
|
||||
5. **Tier B aggiunge valore**: l'ablation mostra Δ Sharpe OOS misurabile per tier mix vs tier C only. In caso negativo, Phase 3 può girare tier C only e il decision memo ne prende nota.
|
||||
|
||||
Hard gate passati → Phase 3. Hard gate falliti → stop o pivot. Soft gate falliti → Phase 3 con scope ridotto e annotazioni nel report.
|
||||
|
||||
### 5.5 Deliverable Phase 2
|
||||
|
||||
- Codice testato: speciation, novelty, ablation harness, RF baseline, DSR pipeline, Adversarial battery.
|
||||
- Report scientifico (~15-20 pagine): metodologia, risultati per ogni gate, ablation table, top-5 strategie ispezionate qualitativamente, threats to validity.
|
||||
- Decision memo: vai/non-vai a Phase 3, scope di Phase 3 (capitale, exchange, leva, durata) calibrato sui risultati.
|
||||
|
||||
---
|
||||
|
||||
## 6. Phase 3 — Forward-test mainnet
|
||||
|
||||
**Obiettivo**: vedere se l'edge sopravvive in produzione. Anche il backtest più rigoroso ha bias inevitabili (look-ahead microscopico, slippage idealizzato, assenza di information leakage da fonti che non esistevano in periodo storico). Solo il forward-test live risponde alla domanda finale.
|
||||
|
||||
### 6.1 Scope IN
|
||||
|
||||
- Selezione strategie: top-3 genomi out-of-sample dalla Phase 2, dopo passaggio completo dell'Adversarial battery. Niente cherry-picking ex-post post-Phase 2.
|
||||
- Capitale: $500-2.000 totali, distribuiti sui tre genomi con allocazione equal weight oppure risk-parity sulla volatilità OOS attesa, scelta motivata nel decision memo Phase 2.
|
||||
- Exchange: scelta condizionata alle strategie selezionate. Default raccomandati Bybit (perp, fees competitive, liquidità BTC/ETH adeguata) oppure Hyperliquid (no KYC, transparent funding). Cerbero supporta entrambi nativamente.
|
||||
- Leva: massimo 2x in forward-test. Anche se lo Sharpe OOS suggerisse di più, in fase di validazione la leva resta bassa per non confondere edge della strategia con leva.
|
||||
- Durata: 6-8 settimane continue. Razionale: in crypto questa finestra copre tipicamente uno o due micro-regime change, sufficienti a stressare il modello senza catastrofi statistiche da campione troppo piccolo.
|
||||
- Monitoring: dashboard giornaliera (sezione Live Monitor) con Sharpe live realized vs Sharpe OOS atteso, drawdown realtime, violation count (segnali generati ma non eseguiti per qualunque ragione), audit log Cerbero per ogni order.
|
||||
- Decision triggers automatici: kill-switch se il drawdown live supera 1.5x il peggiore osservato in walk-forward; pause se Sharpe rolling 14d resta negativo per 14 giorni consecutivi.
|
||||
- Adversarial post-mortem settimanale: l'agente Adversarial gira nuovamente sul signal log della settimana per identificare degradazione dell'edge (regime drift detection).
|
||||
|
||||
### 6.2 Scope OUT
|
||||
|
||||
- Capital scaling oltre $2.000 in Phase 3. Se i risultati promettono, la decisione di scaling è esplicitamente fuori dal PoC e viene presa dopo il decision memo finale.
|
||||
- Multi-strategy portfolio rebalancing dinamico. Allocazione statica sui tre genomi.
|
||||
- Hedging cross-exchange. Confonderebbe la lettura dell'edge della strategia.
|
||||
- Aggiunta di nuovi genomi in corsa. I tre genomi sono fissati a inizio fase.
|
||||
|
||||
### 6.3 Budget e tempo
|
||||
|
||||
- LLM: $200-400. La popolazione GA non gira più, gli agenti sono richiamati solo per Adversarial post-mortem settimanale e per occasionali refresh quando i decision triggers scattano.
|
||||
- Capitale a rischio: $500-2.000. Trattato come **costo della validazione**, non come investimento. Se il capitale va a zero, il dato che ne ricaviamo vale comunque.
|
||||
- Tempo: 6-8 settimane di calendario, monitoring operativo circa 5h/settimana — non full-time. Le settimane libere sono allocate a documentazione finale, lavoro su Tielogic e altri progetti, prima esplorazione Filone A in caso di decisione GO.
|
||||
- Costo infra extra: circa $10-30/mese VPS per dashboard più monitoring, in larga parte già coperto dal setup Hostinger esistente.
|
||||
|
||||
### 6.4 Gate decisionale finale del PoC
|
||||
|
||||
**Hard gate per "GO sistema completo / Filone A"**:
|
||||
|
||||
1. **Edge sopravvive live**: Sharpe live realized almeno 0.5x dello Sharpe OOS atteso, su finestra di almeno 4 settimane.
|
||||
2. **No catastrophic failure**: max drawdown live al massimo 1.5x del peggior drawdown walk-forward.
|
||||
3. **Reproducibility**: almeno 2 dei 3 genomi performano in linea con previsione — la fortuna non si concentra su uno solo.
|
||||
|
||||
**Soft gate (qualitativi, informano la decisione)**:
|
||||
|
||||
4. **Audit Adversarial settimanali**: nessuna scoperta critica come lookahead nascosto emerso solo live, oppure data leakage da provider.
|
||||
5. **Cost economy**: edge dopo costi reali (slippage effettivo, fees, funding) resta positivo.
|
||||
|
||||
**Esiti possibili**:
|
||||
|
||||
- Hard gate e soft gate tutti passati → GO Filone A con confidence empirica forte. Si apre la ridiscussione del budget e della roadmap del sistema completo, fuori dal perimetro di questo documento.
|
||||
- Hard gate passati, soft gate falliti → iterazione Phase 2 mirata sulle debolezze identificate, no Filone A immediato.
|
||||
- Hard gate falliti, ma senza catastrofi → l'idea regge concettualmente ma non scala live retail. Decision memo con due opzioni: pivot dominio (offerte Tielogic, code review) oppure chiusura del progetto.
|
||||
- Hard gate falliti più drawdown catastrofico → l'idea non regge live. Stop del progetto. Documento di chiusura con learnings registrati per progetti futuri.
|
||||
|
||||
### 6.5 Deliverable Phase 3 e finali
|
||||
|
||||
- Report finale del PoC (~20-30 pagine): metodologia completa, risultati Phase 1+2+3, comparison Sharpe in-sample / OOS / live, ispezione qualitativa delle strategie, learnings, threats to validity confermati o respinti.
|
||||
- Decision memo strategico: GO Filone A / iterate Phase 2 / pivot dominio / stop, con razionale quantitativo ancorato ai gate.
|
||||
- Codebase pubblicabile (anche se repo privato): backtest, GA, Cerbero integration, speciation, DSR pipeline, RF baseline, monitoring dashboard, tutto documentato.
|
||||
|
||||
---
|
||||
|
||||
## 7. GUI Streamlit incrementale
|
||||
|
||||
Una dashboard è essenziale per ispezionare cosa fa il sistema, decidere i gate in modo informato, e produrre i grafici che entreranno nei report.
|
||||
|
||||
### 7.1 Architettura
|
||||
|
||||
- Tech stack: Streamlit single-app multi-page, dati letti da SQLite locale (`runs.db`) e Parquet per series numerici pesanti.
|
||||
- SQLite per stato: genomi, generazioni, fitness, ablation results, adversarial findings, trade log. Schema relazionale stabile, query veloci, nessun DB server da gestire.
|
||||
- Parquet per series: equity curves, signal time series, OHLCV. File-based, columnar, leggero.
|
||||
- Auto-refresh ogni 10 secondi nella sola pagina Live Monitor (Phase 3). Sufficiente per uno scope a decisioni minute-level, non HFT.
|
||||
- Single app, multipage: tutto sotto `dashboard/streamlit_app.py` più `pages/`. Deploy locale con `streamlit run`, niente VPS frontend.
|
||||
|
||||
### 7.2 Pagine costruite incrementalmente
|
||||
|
||||
**Phase 1 (3-4 giorni di lavoro)**:
|
||||
|
||||
- *Overview*: ultima run, generazione corrente, stato (running/completed/failed), spesa LLM cumulata vs cap.
|
||||
- *GA Convergence*: line plot fitness mediana / max / 90° percentile per generazione, distribuzione fitness ultima generazione (histogram), counter chiamate LLM e costo.
|
||||
- *Genomes (basic)*: tabella top-10 genomi correnti con DSR, cognitive_style, temperature, lookback. Click su riga apre side panel con system_prompt completo, feature_access, lineage parent_id.
|
||||
|
||||
**Phase 2 (9-12 giorni distribuiti)**:
|
||||
|
||||
- *Genomes (avanzato)*: lineage tree interattivo (parent → children su 15 generazioni), speciation cluster view (UMAP/t-SNE su prompt embedding più parametri, colori per specie), filtri per specie / tier / cognitive_style.
|
||||
- *Performance*: equity curve in-sample, walk-forward, OOS, hold-out per ogni genoma. Sharpe, DSR, drawdown. Trade distribution per regime di volatilità, asset, orario. Per-strategy e portfolio view.
|
||||
- *Ablation*: confronto runs (tier C only, tier B only, mix) side-by-side. Δ Sharpe OOS, costo per percentile fitness, breakeven analysis.
|
||||
- *Adversarial*: per ogni top-10 genoma, le cinque critiche (data snooping, lookahead, regime fragility, crowding, transaction cost). Click espande prompt completo, risposta LLM, decisione pass/fail con rationale.
|
||||
- *RF Baseline*: Sharpe RF baseline OOS, feature importance, comparison vs top-3 swarm, Cohen's d effect size.
|
||||
|
||||
**Phase 3 (4-5 giorni)**:
|
||||
|
||||
- *Live Monitor*: P&L realtime per strategia e portfolio, equity curve da inizio Phase 3, drawdown rolling. Auto-refresh 10s.
|
||||
- *Live vs OOS*: Sharpe live realized vs Sharpe OOS atteso (con confidence interval), gauge "edge sopravvive?", cumulative deviation tracker.
|
||||
- *Triggers state*: stato kill-switch per strategia, distance from threshold, history pause/resume, audit log decisioni recenti.
|
||||
- *Adversarial weekly*: report settimanale di regime drift detection, diff vs settimana precedente.
|
||||
|
||||
### 7.3 Costi GUI
|
||||
|
||||
- Effort totale: 16-21 giorni netti, distribuiti come da fasi sopra.
|
||||
- LLM: zero (codice e visualizzazione Python locale).
|
||||
- Infra: zero (esecuzione locale, SQLite locale, Parquet locale).
|
||||
|
||||
### 7.4 Trade-off accettati
|
||||
|
||||
- Refresh non realtime sub-secondo: accettabile per scope decisionale minute/hour.
|
||||
- Niente login né multi-utente: dashboard personale solo locale.
|
||||
- Niente alert push esterni in default: alert appaiono in dashboard. Per Phase 3 si può aggiungere webhook Telegram/email se necessario, mezza giornata extra di lavoro.
|
||||
- Streamlit re-runs full-page on interaction: gestibile con `@st.cache_data` su query SQLite e dataset PoC di dimensioni contenute.
|
||||
|
||||
### 7.5 Deliverable GUI
|
||||
|
||||
- Codice `dashboard/` testato (smoke test e data layer test).
|
||||
- Schema SQLite versionato con migrazioni semplici (Alembic light o script SQL).
|
||||
- README con istruzioni `streamlit run` e descrizione di ogni pagina.
|
||||
|
||||
---
|
||||
|
||||
## 8. Hardware e infrastruttura
|
||||
|
||||
Principio: tutto locale più Cerbero come unico servizio remoto. Nessuna GPU, nessun cloud compute, nessun costo infra ricorrente significativo.
|
||||
|
||||
### 8.1 Compute
|
||||
|
||||
- Backtest e GA loop: PC locale Linux. Tutto CPU-bound, parallelizzabile su core (joblib o multiprocessing). Dataset 2 anni OHLCV 1h BTC+ETH circa 30-50 MB. Anche granularità 1m sarebbe sotto i 2 GB, gestibile.
|
||||
- LLM: tutte chiamate via API esterne (OpenRouter per tier C, Anthropic per tier B). Nessun model locale, nessuna GPU.
|
||||
- Streamlit dashboard: locale (`streamlit run` su `localhost:8501`).
|
||||
|
||||
### 8.2 Cerbero_mcp
|
||||
|
||||
Già configurato e operativo. Modalità d'uso durante PoC:
|
||||
|
||||
- Locale via Docker compose durante development e Phase 1-2 (testnet only). Riduce latenza, niente costi VPS, debug più rapido.
|
||||
- VPS Hostinger durante Phase 3 forward-test (mainnet). Già setup `/opt/cerbero-mcp` con `deploy-vps.sh` e branch V2.0.0.
|
||||
- Token bearer: `TESTNET_TOKEN` per Phase 1-2 backtest replay, `MAINNET_TOKEN` solo per Phase 3.
|
||||
- Bot tag dedicato per il PoC (`X-Bot-Tag: swarm-poc-<phase>`). L'audit log Cerbero traccia ogni call separatamente per fase, utile per ricostruzioni post-mortem.
|
||||
|
||||
### 8.3 Storage
|
||||
|
||||
- `runs.db` SQLite per stato GA, genomi, generazioni, fitness, adversarial, trade log. Backup giornaliero su disco esterno o cloud personale.
|
||||
- `series/` Parquet per equity curves, signal time series, OHLCV cache. Versionato fuori da git (Git LFS o cartella esterna trackata in `.gitignore`).
|
||||
- Audit log Cerbero: già JSONL con rotazione 30 giorni (`AUDIT_LOG_BACKUP_DAYS=30`). Per Phase 3 aumentare a 90 giorni per coprire intera fase più post-mortem.
|
||||
|
||||
### 8.4 Networking
|
||||
|
||||
- LLM API via OpenRouter (tier C) e Anthropic (tier B). Nessun setup speciale.
|
||||
- Cerbero locale: porta 9000 default, nessuna esposizione pubblica.
|
||||
- Cerbero VPS: già protetto da Traefik più bearer più allowlist IP. Nessun lavoro extra.
|
||||
|
||||
### 8.5 Costi infra ricorrenti
|
||||
|
||||
- VPS Hostinger: già pagato per altri progetti, costo marginale zero.
|
||||
- Storage backup: trascurabile.
|
||||
- Domini e TLS: già coperti (cerbero-mcp.tielogic.xyz).
|
||||
|
||||
### 8.6 Decisioni hardware non bloccanti
|
||||
|
||||
- Se la Phase 2 ablation richiedesse parallelizzazione massiva (ad esempio cento backtest concorrenti), valutare spot instance AWS o Hetzner. Probabilmente non necessario, le strategie a granularità 1h sono veloci da backtestare anche su CPU desktop.
|
||||
- Backup off-site di `runs.db`: decisione di lifecycle, non bloccante per Phase 1.
|
||||
|
||||
---
|
||||
|
||||
## 9. Cadenza review e disciplina autoriale
|
||||
|
||||
Regola personale dell'autore: mai self-approve, separare author pass da review pass. Applicata sistematicamente ai gate del PoC.
|
||||
|
||||
### 9.1 Cadenza working
|
||||
|
||||
- Daily: lavoro full-time. Self-review informale a fine giornata, una riga nel commit message su cosa è andato e cosa no.
|
||||
- Settimanale (venerdì): review formale con dump strutturato che include fitness convergence plot, top-5 genomi della settimana, spesa LLM accumulata vs cap di fase, eventuali findings Adversarial significativi, aggiornamento di `progress.md`.
|
||||
- Bi-settimanale: snapshot completo più decision check sul fatto se siamo ancora on-track per il gate. Se due bi-weekly consecutivi mostrano off-track materiale, decisione anticipata di pivot o iterate, non attesa fino a fine fase.
|
||||
|
||||
### 9.2 Gate review (decisione formale fine fase)
|
||||
|
||||
Per ognuno dei tre gate (fine Phase 1, fine Phase 2, fine Phase 3): author pass e review pass separati.
|
||||
|
||||
- **Author pass**: l'autore scrive il decision memo con tutti i numeri, gate per gate, conclusione raccomandata.
|
||||
- **Review pass**: secondo passaggio con approccio adversarial. Tre opzioni equivalenti:
|
||||
- Subagent Claude con prompt esplicitamente "red team" che riceve memo più dati grezzi e produce critica strutturata (cherry-picking, debolezze statistiche, omissioni).
|
||||
- Collega umano disponibile, se esiste un contesto Tielogic adatto.
|
||||
- Rilettura dopo 48 ore con timer, fresh eyes pass.
|
||||
- **Sintesi**: solo dopo il review pass la decisione viene formalizzata e committata.
|
||||
|
||||
### 9.3 Decision triggers oggettivi
|
||||
|
||||
I gate hard di ogni fase sono numerici (DSR, p-value, drawdown ratio). La decisione GO/STOP è meccanica sui hard gate:
|
||||
|
||||
- Hard gate fallito → STOP automatico, non in discussione.
|
||||
- Hard gate passato → si valuta se i soft gate danno motivo di iterazione invece di procedere.
|
||||
|
||||
Questa è la disciplina Renaissance applicata al progetto: niente "magari un'altra generazione" se il numero non lo dice.
|
||||
|
||||
### 9.4 Documentazione del processo
|
||||
|
||||
- `docs/runs/YYYY-MM-DD-phase{1,2,3}-run-N.md` per ogni run completato: configurazione, risultati, anomalie, learning.
|
||||
- `docs/decisions/YYYY-MM-DD-gate-phase{1,2,3}.md` per ogni decisione gate: author pass, review pass, decisione finale, razionale.
|
||||
- Questo documento (`docs/superpowers/specs/2026-05-09-decisione-strategica-design.md`) come north star strategico, aggiornato se cambiano vincoli o decisioni macro.
|
||||
|
||||
---
|
||||
|
||||
## 10. Catena di ragionamento (tracciabilità)
|
||||
|
||||
Per riferimento futuro, la sequenza logica che ha portato al design B3:
|
||||
|
||||
1. Obiettivo dichiarato sistema produttivo che generi valore → esclude C (research dive senza output produttivo).
|
||||
2. Dominio iniziale trading crypto → allinea il PoC al design già scritto in `poc_trading_swarm.md`.
|
||||
3. Tempo full-time disponibile → scioglie il vincolo "non posso costruire infrastruttura", apre spazio per fasi sequenziali.
|
||||
4. Budget LLM tight ($1-2K) → esclude A (Filone completo) e impone disciplina su B.
|
||||
5. Setup Cerbero_mcp esistente → riduce settimane di plumbing, gli agenti chiamano tool MCP nativi.
|
||||
6. Forward-test mainnet con capitale piccolo come parte del successo → richiede una Phase 3 dedicata, distinta dalla validazione statistica.
|
||||
7. Disciplina "spegnere ciò che non funziona" → struttura tripartita con kill-switch numerici.
|
||||
8. Mai self-approve → separazione author pass / review pass nei gate.
|
||||
9. Necessità di ispezionare cosa fa il sistema → GUI Streamlit come componente orizzontale, non opzionale.
|
||||
|
||||
---
|
||||
|
||||
## 11. Decisioni risolte e decisioni ancora aperte
|
||||
|
||||
### 11.1 Risolte da questo documento
|
||||
|
||||
- Opzione strategica: B3.
|
||||
- Dominio iniziale: trading derivati crypto BTC/ETH.
|
||||
- Numero di fasi: tre, con gate go/no-go fra una e l'altra.
|
||||
- Budget cap globale: $2.200 LLM più $500-2.000 capitale a rischio Phase 3.
|
||||
- Cap calendario: 18 settimane.
|
||||
- Tier mix: solo C in Phase 1, mix B/C in Phase 2-3.
|
||||
- Tech stack GUI: Streamlit più SQLite più Parquet.
|
||||
- Infrastruttura: locale più Cerbero_mcp esistente.
|
||||
- Cadenza review: settimanale, bi-settimanale per check, gate con author/review pass separati.
|
||||
|
||||
### 11.2 Aperte (non bloccanti per Phase 1)
|
||||
|
||||
- Allocazione capitale Phase 3 (equal weight vs risk-parity): decisione formalizzata nel decision memo Phase 2 sulla base dei risultati OOS.
|
||||
- Exchange Phase 3 (Bybit vs Hyperliquid): scelta dipendente dalle strategie selezionate, decisa nel decision memo Phase 2.
|
||||
- Approccio review pass (subagent vs umano vs fresh eyes): decisione tattica per gate, nessun lock-in.
|
||||
- Webhook alert Telegram/email per Phase 3: opzionale, decidibile a inizio Phase 3.
|
||||
|
||||
### 11.3 Esplicitamente fuori scope
|
||||
|
||||
- Filone A (sistema completo) come fase corrente. Decisione su A presa solo dopo decision memo finale Phase 3.
|
||||
- Filone C (applicazioni non-trading: offerte Tielogic, code review, doc Swagger). Possibile pivot in caso di hard gate falliti, non azione preventiva.
|
||||
- Co-evolution del protocollo. Nessuna delle tre fasi PoC la include.
|
||||
- Capital scaling oltre $2.000 in Phase 3. Decisione di scaling appartiene a una fase successiva al PoC.
|
||||
|
||||
---
|
||||
|
||||
## 12. Prossimi passi
|
||||
|
||||
Esecuzione di Phase 1.
|
||||
|
||||
Il piano implementativo dettagliato di Phase 1 (settimana per settimana, task atomici, dipendenze, verifiche) sarà oggetto del documento successivo, da costruire con l'invocazione dello skill `writing-plans` su questo design.
|
||||
|
||||
---
|
||||
|
||||
*Documento approvato il 9 maggio 2026. Versione 1.0. Aggiornare in caso di modifica dei vincoli operativi o di esiti di gate che richiedano revisione strategica complessiva.*
|
||||
@@ -0,0 +1,343 @@
|
||||
# strategy_pythagoras — Design
|
||||
|
||||
**Data:** 2026-05-19
|
||||
**Autore design:** Adriano Dal Pastro (con Claude/brainstorming)
|
||||
**Stato:** Design approvato per parti 1-4, in attesa di review utente sulla spec consolidata
|
||||
**Audience:** implementatori (Claude executor o umano)
|
||||
|
||||
## 0. Riassunto esecutivo
|
||||
|
||||
Nuovo workspace member `strategy_pythagoras` parallelo a `strategy_crypto`. Replica il pattern coevolutivo GA del monorepo applicato a un dominio diverso: la **scoperta di pattern frattali ricorrenti** sui mercati crypto secondo il framework Pythagoras-Malanga (Fourier + frattali + geometria Evideon), descritto nei due PDF in `src/strategy_pythagoras/Pythagoras/`.
|
||||
|
||||
**Non cambia il genoma del core:** gli agenti restano `HypothesisAgentGenome` e producono `Strategy JSON` nella stessa DSL S-expression. Cambiano:
|
||||
- 7 stili cognitivi Pythagoras-themed (in `prompts.json`)
|
||||
- 3 nuovi indicatori candle (`candle_pattern`, `pythagorean_ratio`, `fractal_mirror`)
|
||||
- Fitness con bonus di asset-invariance BTC↔ETH
|
||||
- Output: strategie JSON freezate + dashboard NiceGUI + report markdown di analisi cross-book
|
||||
|
||||
**Deliverable di chiusura task:** scaffolding completo + 1 GA run short (smoke test) + `docs/analysis_first_run.md` con sintesi numerica e top winners.
|
||||
|
||||
---
|
||||
|
||||
## 1. Architettura
|
||||
|
||||
### Layout package
|
||||
|
||||
```
|
||||
src/strategy_pythagoras/
|
||||
├── pyproject.toml # workspace member, dipende da multi-swarm-core
|
||||
├── README.md
|
||||
├── tests/
|
||||
│ └── test_indicators.py # unit-test per i 3 nuovi indicatori
|
||||
└── strategy_pythagoras/
|
||||
├── __init__.py
|
||||
├── prompts.json # 7 stili Pythagoras-aligned (schema v3.2 di strategy_crypto)
|
||||
├── indicators.py # candle_pattern, pythagorean_ratio, fractal_mirror
|
||||
├── backend/
|
||||
│ ├── __init__.py
|
||||
│ ├── schema.py # tabelle paper_trading_*
|
||||
│ ├── executor.py # PaperExecutor (port da strategy_crypto)
|
||||
│ ├── portfolio.py # Portfolio (port da strategy_crypto)
|
||||
│ └── persistence.py # PaperRepository
|
||||
├── frontend/
|
||||
│ ├── __init__.py
|
||||
│ ├── nicegui_app.py # /strategy_pythagoras_gui
|
||||
│ └── data.py # dual-reader: GA db + paper db + invariance metrics
|
||||
└── strategies/ # JSON winners shippati col package
|
||||
└── (vuoto al t0)
|
||||
```
|
||||
|
||||
### Workspace registration
|
||||
|
||||
In `pyproject.toml` (root):
|
||||
```toml
|
||||
[tool.uv.workspace]
|
||||
members = ["src/multi_swarm_core", "src/strategy_crypto", "src/strategy_pythagoras"]
|
||||
|
||||
[tool.uv.sources]
|
||||
strategy-pythagoras = { workspace = true }
|
||||
```
|
||||
|
||||
Aggiungere `strategy-pythagoras` a `[project].dependencies` per il deployable root.
|
||||
|
||||
### Riuso
|
||||
|
||||
| Componente | Source | Note |
|
||||
|---|---|---|
|
||||
| GA loop, mutation, crossover | `multi_swarm_core.ga` | invariato |
|
||||
| Protocol parser/validator/compiler | `multi_swarm_core.protocol` | esteso con 3 indicatori (vedi §2) |
|
||||
| Backtest engine | `multi_swarm_core.backtest` | invariato |
|
||||
| LLM / OpenRouter / Anthropic clients | `multi_swarm_core.llm` | invariato |
|
||||
| PaperExecutor + Portfolio | `strategy_crypto.backend` | **port** (non import), per isolamento DB |
|
||||
| NiceGUI dashboard shell | `strategy_crypto.frontend` | **port** + adatta tabs |
|
||||
|
||||
### Persistence
|
||||
|
||||
```
|
||||
state/strategy_pythagoras.db # GA: genomi, generazioni, fitness history
|
||||
state/strategy_pythagoras_paper.db # paper-trading post-deploy
|
||||
strategy_pythagoras/strategies/ # JSON winners shippati
|
||||
docs/analysis_first_run.md # report cross-book
|
||||
docs/analysis_runs/<run-id>/ # per-run dump
|
||||
```
|
||||
|
||||
Env vars:
|
||||
- `STRATEGY_PYTHAGORAS_DB_PATH` (default `state/strategy_pythagoras.db`)
|
||||
- `STRATEGY_PYTHAGORAS_PAPER_DB_PATH` (default `state/strategy_pythagoras_paper.db`)
|
||||
- `GA_INVARIANCE_ALPHA` (default 0.3)
|
||||
- `GA_INVARIANCE_TOLERANCE_BARS` (default 36 = ±3h su 5m TF)
|
||||
|
||||
### Routing GUI
|
||||
|
||||
`/strategy_pythagoras_gui` (allinea a [[gui_subpath_routing]] in memory: ogni asset GUI su subpath, root dominio libera).
|
||||
|
||||
### Docker (Phase 2, fuori scope di questa spec)
|
||||
|
||||
Servizi paralleli a quelli di strategy_crypto:
|
||||
- `strategy-pythagoras-paper` (runner)
|
||||
- `strategy-pythagoras-gui` (dashboard)
|
||||
Rete Traefik external, bind mount uid 1000 (vedi [[production_deployment]]).
|
||||
|
||||
---
|
||||
|
||||
## 2. Genoma e DSL
|
||||
|
||||
### Il genoma non cambia
|
||||
|
||||
`HypothesisAgentGenome` di `multi_swarm_core.genome.hypothesis` resta identico:
|
||||
- `system_prompt: str`
|
||||
- `feature_access: list[str]`
|
||||
- `temperature: float`
|
||||
- `top_p: float`
|
||||
- `model_tier: ModelTier`
|
||||
- `lookback_window: int` — vincolo 12 ≤ lw ≤ 200
|
||||
- `cognitive_style: str` — uno dei 7 nuovi stili
|
||||
- `parent_ids, generation, id` — invariati
|
||||
|
||||
### 3 nuovi indicatori (`strategy_pythagoras/indicators.py`)
|
||||
|
||||
| Nome | Params (JSON) | Output | Semantica operativa |
|
||||
|---|---|---|---|
|
||||
| `candle_pattern` | `[seq_str]` es. `"UDU"`, `"UUD0U"` | 1.0 se le ultime k=len(seq_str) candele matchano la sequenza, 0.0 altrimenti | `"U"` = close>open; `"D"` = close<open; `"0"` = `abs(close-open)/open < 0.001` |
|
||||
| `pythagorean_ratio` | `[lookback: int]` | float = `max(close[-lookback:]) / min(close[-lookback:])` | Ratio prezzo, da confrontare con literal vicini a φ=1.618, π=3.1416, √2=1.414, e=2.718 |
|
||||
| `fractal_mirror` | `[k: int, axis: str]` axis ∈ `{"h","v"}` | float ∈ [-1, +1] | Correlazione di Pearson tra ultime k candele e loro mirror: `"h"` = mirror tempo (inversione sequenza); `"v"` = mirror prezzo (1 - close/max) |
|
||||
|
||||
Vincoli del compiler:
|
||||
- `candle_pattern`: `len(seq_str)` ∈ [3, 12], simboli ∈ `{U,D,0}`
|
||||
- `pythagorean_ratio`: `lookback` ∈ [12, 200]
|
||||
- `fractal_mirror`: `k` ∈ [3, 12], `axis` ∈ `{"h","v"}`
|
||||
|
||||
Tutti e 3 vanno aggiunti a `KNOWN_INDICATORS` in `multi_swarm_core.protocol.grammar`.
|
||||
|
||||
### Esempio strategy JSON tipica
|
||||
|
||||
```json
|
||||
{
|
||||
"rules": [
|
||||
{
|
||||
"condition": {
|
||||
"op": "and",
|
||||
"args": [
|
||||
{"op": "eq", "args": [
|
||||
{"kind": "indicator", "name": "candle_pattern", "params": ["UDDU"]},
|
||||
{"kind": "literal", "value": 1.0}
|
||||
]},
|
||||
{"op": "gt", "args": [
|
||||
{"kind": "indicator", "name": "pythagorean_ratio", "params": [89]},
|
||||
{"kind": "literal", "value": 1.618}
|
||||
]}
|
||||
]
|
||||
},
|
||||
"action": "entry-long"
|
||||
},
|
||||
{
|
||||
"condition": {
|
||||
"op": "or", "args": [
|
||||
{"op": "gt", "args": [
|
||||
{"kind": "indicator", "name": "pythagorean_ratio", "params": [89]},
|
||||
{"kind": "literal", "value": 2.618}
|
||||
]},
|
||||
{"op": "crossunder", "args": [
|
||||
{"kind": "indicator", "name": "fractal_mirror", "params": [12, "h"]},
|
||||
{"kind": "literal", "value": 0.0}
|
||||
]}
|
||||
]
|
||||
},
|
||||
"action": "exit"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Vincoli protettivi (anti-overfitting)
|
||||
|
||||
- `lookback_window ≤ 200`
|
||||
- `candle_pattern` seq length ∈ [3, 12] (range 1+2 del Libro dei Frattali)
|
||||
- `time_in_market` monitorato come metric (red flag [[selectivity_red_flag]]); non hard-gate al primo run
|
||||
- Letterali con `pythagorean_ratio`: max 4 decimali (no `1.6180339`)
|
||||
- Max 4 condizioni in AND per regola (eredita da prompts.json)
|
||||
|
||||
---
|
||||
|
||||
## 3. Stili cognitivi (`strategy_pythagoras/prompts.json`)
|
||||
|
||||
### Schema
|
||||
|
||||
Schema v3.2 identico a `strategy_crypto/prompts.json` (campi `_schema`, `_changelog`, `_design_invariants`, `agent_role`, `pattern_guidance`, `instruction`, `domain_warnings`, `anti_patterns`, `output_priorities`, `styles`).
|
||||
|
||||
### `agent_role`
|
||||
|
||||
> Sei un agente di un sistema swarm coevolutivo che cerca pattern frattali ricorrenti sui mercati crypto secondo il framework Pythagoras-Malanga (frattali H-C, trasformata di Fourier, geometria Evideon). Sei parte di una popolazione che esplora collettivamente lo spazio dei pattern: la diversità delle ipotesi è un asset critico. Preferisci esplorare strutture meno ovvie per la tua lente cognitiva. La strategia che produci deve essere riconoscibile come emanata dal tuo stile.
|
||||
|
||||
### `pattern_guidance` (specifico Pythagoras)
|
||||
|
||||
- Sequenze candle 3-12 lunghezza in alfabeto `{U,D,0}` via `candle_pattern`
|
||||
- Mirror H/V come operatori di proiezione via `fractal_mirror`
|
||||
- Ratios di prezzo vicini a φ=1.618, 1/φ=0.618, √2=1.414, π/2=1.571, e/2=1.359 entro tolleranza 0.5%
|
||||
- Pattern composti: pattern lunghi (6-12) come concatenazione di pattern corti (Pattern 13 = Pattern 3 + Pattern 11, p. 53 del paper)
|
||||
- Cicli ricorrenti: stesso pattern firato a distanze regolari (Poincaré)
|
||||
|
||||
### `domain_warnings`
|
||||
|
||||
- Crypto 24/7, no CME gap
|
||||
- I "numeri sacri" (Solfeggio 396-852 Hz, 137.0359, etc.) sono prior teorici, NON leggi: usali come scale candidate, non come dogma
|
||||
- Il paper Pythagoras è esplicitamente non-falsificabile (cita "consapevolezza del trader" come jolly per il fallimento): il backtest è l'unico arbitro
|
||||
- `time_in_market > 80%` red flag (leveraged B&H camuffato)
|
||||
- Tolleranza ±3h del paper → su 5m TF = ±36 barre
|
||||
|
||||
### `anti_patterns`
|
||||
|
||||
- Sequenza `candle_pattern` con `len > 7` simboli vincolati → overfitting
|
||||
- `pythagorean_ratio` con tolleranza > 2% sui literal → numerologia spuriosa
|
||||
- `fractal_mirror` con `k == lookback_window` → tautologico
|
||||
- Letterali con più di 4 decimali
|
||||
- Più di 4 condizioni in AND
|
||||
- Crossover tra indicatori dello stesso tipo con lookback vicini → chattering
|
||||
|
||||
### `output_priorities`
|
||||
|
||||
1. **Coerenza con lente cognitiva** (es. `pythagorean` usa ratios, `candle_grammarian` usa sequenze esplicite)
|
||||
2. **Asset-invariance** (segnali che attivano sia su BTC che su ETH entro ±36 barre)
|
||||
3. **Selettività** (poche entry forti)
|
||||
4. **Composizionalità** (pattern lunghi come somma di corti)
|
||||
5. **Robustezza vs random baseline** (3σ richiesto da `skeptic_quant`)
|
||||
|
||||
### I 7 stili (`styles`)
|
||||
|
||||
Ogni stile mantiene shape v3.2 (directive 800-950 char ASCII-safe, `focus_metrics` 4 voci, ultimo periodo = "Archetipo dominante: X.", lookback range esplicito):
|
||||
|
||||
| `cognitive_style` | Archetipo / Metafora ancorante | `focus_metrics` | Lookback consigliato |
|
||||
|---|---|---|---|
|
||||
| `pythagorean` | Armonia di ratios sacri (φ, π, √2) | `pythagorean_ratio`, `candle_pattern`, `sma_pct`, `realized_vol` | 89-144 |
|
||||
| `fractal_geometer` | Autosimilarità: pattern di 3 candele si ripetono dilatati a 6, 12 | `candle_pattern`, `fractal_mirror`, `atr_pct`, `pythagorean_ratio` | 48-144 |
|
||||
| `fourier_analyst` | Somma di seni: frequenze ricorrenti dominanti | `sma_pct`, `realized_vol`, `candle_pattern`, `atr` | 60-200 |
|
||||
| `evideonic_projector` | Presente = passato proiettato via mirror H+V e scale | `fractal_mirror`, `pythagorean_ratio`, `candle_pattern`, `sma_pct` | 24-96 |
|
||||
| `candle_grammarian` | Lingua di 3 simboli (U,D,0); parole 3-12 lettere | `candle_pattern`, `volume`, `atr`, `realized_vol` | 12-48 |
|
||||
| `recurrence_theorist` | Per Poincaré, eventi tornano: cerca pattern di oggi che firarono ieri | `candle_pattern`, `fractal_mirror`, `pythagorean_ratio`, `sma_pct` | 100-200 |
|
||||
| `skeptic_quant` | Anticorpo all'unfalsifiability: solo edge 3σ vs random | `realized_vol`, `atr_pct`, `sma_pct`, `candle_pattern` | 60-150 |
|
||||
|
||||
Lo `skeptic_quant` è importante: la sua directive richiede esplicitamente che la strategia sia testabile e che il fitness sia confrontato contro random baseline.
|
||||
|
||||
### `_design_invariants`
|
||||
|
||||
Stessa filosofia di v3.2 di strategy_crypto:
|
||||
- ASCII-safe (no Unicode oltre U+007F nelle directive)
|
||||
- Ogni directive chiude con `Archetipo dominante: <metafora>.`
|
||||
- Ogni directive ha range lookback numerico esplicito
|
||||
- Prima frase: `Il mercato e ...`
|
||||
- Lunghezza 800-950 char
|
||||
|
||||
---
|
||||
|
||||
## 4. Fitness, run GA short, deliverable analisi
|
||||
|
||||
### Fitness con bonus invariance
|
||||
|
||||
```
|
||||
fitness(genome) = mean(sharpe_BTC, sharpe_ETH) × (1 + α × invariance_score)
|
||||
|
||||
invariance_score = corr_signal(entries_BTC, entries_ETH, tolerance_bars=GA_INVARIANCE_TOLERANCE_BARS)
|
||||
∈ [0, 1]
|
||||
α = GA_INVARIANCE_ALPHA (default 0.3)
|
||||
```
|
||||
|
||||
`corr_signal` = frazione di entries su BTC che hanno una entry corrispondente su ETH entro ±36 barre (=±3h su 5m TF).
|
||||
|
||||
Implementazione come callback al GA in `multi_swarm_core.ga`, registrata da `strategy_pythagoras` al startup. Il core non sa nulla di Pythagoras: riceve solo la callback.
|
||||
|
||||
### GA run short (smoke test)
|
||||
|
||||
| Parametro | Valore | Note |
|
||||
|---|---|---|
|
||||
| `population_size` | 20 | minimo per 7 stili (~3 per stile) |
|
||||
| `generations` | 5 | smoke test, non training |
|
||||
| `elite_fraction` | 0.2 | top-4 sopravvivono |
|
||||
| `mutation_rate` | 0.3 | invariato vs strategy_crypto |
|
||||
| `crossover_rate` | 0.5 | invariato |
|
||||
| `model_tier` distribuzione | 70% C (qwen-2.5-72b), 30% B (sonnet) | rispetta [[model_qwen_dependency]] |
|
||||
| `dataset` | BTC 5m + ETH 5m da `strategy_crypto/series/` | riusa serie esistenti |
|
||||
| `train_window` | 2024-07 → 2024-12 | copre le date Pythagoras (lug-ago 2024) |
|
||||
| `test_window` | 2025-01 (1 mese) | hold-out per validare invariance |
|
||||
| `name` | `pythagoras-smoke-001` | run id |
|
||||
|
||||
Lo smoke test verifica:
|
||||
1. Workspace member installato in venv (`uv sync` + `uv run python -c "import strategy_pythagoras"`)
|
||||
2. I 3 nuovi indicatori registrati nel grammar e compilabili
|
||||
3. `prompts.json` caricato, 7 stili producono genomi distinti (no collisioni di id)
|
||||
4. Bonus invariance impatta fitness (verifica via log)
|
||||
5. JSON winners atterrano in `strategy_pythagoras/strategies/`
|
||||
6. Dashboard NiceGUI si avvia e legge i due DB
|
||||
|
||||
### Deliverable analisi cross-book
|
||||
|
||||
`docs/analysis_first_run.md` con:
|
||||
|
||||
1. **Sintesi numerica dei riassunti** — riferimenti a `src/strategy_pythagoras/Pythagoras/Pythagoras_Trading_Prediction.summary.md` e `Libro_frattali.summary.md`
|
||||
2. **Top-5 winners** — id, cognitive_style, fitness, sharpe_BTC, sharpe_ETH, invariance_score
|
||||
3. **Pattern frattali emersi** — dump dei `candle_pattern` seq usate, conteggio per stile, % sovrapposizione con spazio teorico dei 57 pattern del Libro
|
||||
4. **Ratios di prezzo emersi** — distribuzione literal usati con `pythagorean_ratio`, distanza dai numeri universali (φ/π/√2/Solfeggio)
|
||||
5. **Cross-asset invariance osservata** — istogramma di `corr_signal` per top genomi
|
||||
6. **Conclusione onesta** — confronto vs random baseline, quanti winners superano sharpe>1.0 su test + invariance>0.3, cosa il framework Pythagoras predice e cosa NON regge al backtest
|
||||
|
||||
Niente "consapevolezza" come jolly. Solo numeri.
|
||||
|
||||
### Dashboard NiceGUI
|
||||
|
||||
`/strategy_pythagoras_gui`:
|
||||
- Tab **Genomes** — winners con stile/sharpe/invariance, click per drill su rules
|
||||
- Tab **Patterns** — heatmap delle sequenze `candle_pattern` emerse, frequenza per stile
|
||||
- Tab **Ratios** — istogramma literal vicini a costanti universali, bins centrati su φ, π, √2, ecc.
|
||||
- Tab **Invariance** — scatter sharpe_BTC vs sharpe_ETH per ogni winner
|
||||
|
||||
---
|
||||
|
||||
## 5. Out-of-scope (esplicito)
|
||||
|
||||
- **Asset oltre BTC/ETH** (Oro/Argento del paper): non in primo run. Estensione futura.
|
||||
- **Range candele oltre 12**: range 3-5 del Libro (12-56 candele). Estensione futura quando lo smoke test conferma stabilità.
|
||||
- **Live trading reale**: solo paper-trading via stesso pattern di strategy_crypto.
|
||||
- **OCR/Vision sulle figure del Libro dei Frattali**: esplicitamente ESCLUSO da request utente ("senza passare alle immagini").
|
||||
- **Modifica del genoma del core**: nessuna modifica a `HypothesisAgentGenome`. Solo extension del grammar (3 indicatori).
|
||||
- **Riferimenti pseudoscientifici operativizzati come legge**: i numeri sacri/Solfeggio sono prior teorici per literal candidati, non vincoli rigidi.
|
||||
|
||||
---
|
||||
|
||||
## 6. Criteri di accettazione
|
||||
|
||||
- [ ] `uv sync` riesce dalla root con `strategy_pythagoras` come member
|
||||
- [ ] `uv run python -c "from strategy_pythagoras.indicators import candle_pattern, pythagorean_ratio, fractal_mirror"` non solleva
|
||||
- [ ] `pytest src/strategy_pythagoras/tests/` verde (almeno unit-test per i 3 indicatori)
|
||||
- [ ] GA short run `pythagoras-smoke-001` completa 5 generazioni senza errori
|
||||
- [ ] Almeno 1 winner con fitness > 0 e `cognitive_style` ∈ {7 stili Pythagoras}
|
||||
- [ ] Dashboard avvia su `http://localhost:PORT/strategy_pythagoras_gui` e mostra winners
|
||||
- [ ] `docs/analysis_first_run.md` esiste e contiene tutte le sezioni elencate in §4
|
||||
|
||||
---
|
||||
|
||||
## 7. Riferimenti
|
||||
|
||||
- [Pythagoras Trading Prediction — riassunto](../../../src/strategy_pythagoras/Pythagoras/Pythagoras_Trading_Prediction.summary.md)
|
||||
- [Libro dei Frattali — riassunto](../../../src/strategy_pythagoras/Pythagoras/Libro_frattali.summary.md)
|
||||
- Memory: [[monorepo_uv_workspace]], [[gui_subpath_routing]], [[ownership_per_modulo]], [[production_deployment]], [[model_qwen_dependency]], [[selectivity_red_flag]]
|
||||
- Template: `src/strategy_crypto/` (paper-trading + GUI), `src/strategy_crypto/strategy_crypto/prompts.json` (schema v3.2)
|
||||
- Core: `src/multi_swarm_core/multi_swarm_core/genome/hypothesis.py`, `multi_swarm_core/protocol/grammar.py`
|
||||
@@ -1,831 +0,0 @@
|
||||
# PoC Trading Swarm — Validazione Strategica
|
||||
|
||||
**Autore**: Adriano Dal Pastro
|
||||
**Data**: Maggio 2026
|
||||
**Status**: Design document — pre-implementazione
|
||||
**Versione**: 0.1
|
||||
**Documento correlato**: `coevolutive_swarm_system.md` (sistema completo)
|
||||
|
||||
---
|
||||
|
||||
## 1. Razionale della deviazione
|
||||
|
||||
Invece di committere 12-18 mesi al sistema co-evolutivo completo descritto nel documento principale, partiamo con un **proof-of-concept strategico** focalizzato su trading, con architettura semplificata, per validare empiricamente se l'idea di base funziona prima di investire nel sistema full.
|
||||
|
||||
**Cosa il PoC valida**:
|
||||
1. Lo swarm produce strategie che superano il null hypothesis statistico (Deflated Sharpe Ratio significativo)?
|
||||
2. Le strategie sono qualitativamente diverse o cloni leggeri?
|
||||
3. Le strategie sopravvivono al regime change out-of-sample?
|
||||
4. Quanto del successo viene da modelli costosi vs economici (ablation multi-tier)?
|
||||
5. Lo swarm batte una baseline statistica tradizionale (random forest + feature engineering)?
|
||||
|
||||
**Tempo target**: 3-4 mesi a impegno significativo.
|
||||
**Budget LLM target**: $2-4K.
|
||||
|
||||
**Decisione post-PoC**:
|
||||
- Se passa tutti i 5 test → procedere con sistema completo (documento principale)
|
||||
- Se passa parzialmente → iterare sul PoC, identificare bottleneck
|
||||
- Se non passa → riformulare. Forse l'edge degli LLM agents è in altri domini, non in pattern discovery numerico
|
||||
|
||||
---
|
||||
|
||||
## 2. Architettura semplificata
|
||||
|
||||
### 2.1 Cosa cambia rispetto al sistema completo
|
||||
|
||||
| Aspetto | Sistema completo | PoC |
|
||||
|------------------------|-----------------------------|------------------------------------|
|
||||
| Popolazioni evolventi | 4 (3 layer + protocollo) | 1 (solo Hypothesis) |
|
||||
| Hypothesis layer | Evolve via GA | Evolve via GA (K=50) |
|
||||
| Falsification layer | Evolve via GA | Hand-crafted, 1 agente fisso |
|
||||
| Adversarial layer | Evolve via GA | Hand-crafted, 1 agente fisso |
|
||||
| Protocollo | Co-evolve | Fisso, designed manualmente |
|
||||
| Domini di applicazione | Multipli | Solo trading BTC/ETH |
|
||||
| Idiom emergence | Sì | No |
|
||||
| Speciation | Sì | Versione semplificata (clustering base) |
|
||||
| Tier multi-model | Sì (S/A/B/C/D) | Sì (semplificato a B/C principalmente) |
|
||||
| Human-in-the-loop | Strutturato ogni 20 gen | Review settimanale informale |
|
||||
|
||||
**Filosofia**: massimizzare apprendimento, minimizzare complessità implementativa.
|
||||
|
||||
### 2.2 Schema architetturale
|
||||
|
||||
```
|
||||
┌───────────────────────────────────┐
|
||||
│ PROTOCOLLO FISSO (S-expression) │
|
||||
│ ~15 verbi designed manualmente │
|
||||
└─────────────┬─────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌──────────────────────┐
|
||||
│ HYPOTHESIS SWARM │ ← UNICO layer che evolve
|
||||
│ K=50 agenti │
|
||||
│ Tier mix: B/C │
|
||||
│ GA: tournament, │
|
||||
│ speciation base, │
|
||||
│ novelty bonus │
|
||||
└──────────┬───────────┘
|
||||
│ ipotesi formalizzate
|
||||
▼
|
||||
┌──────────────────────┐
|
||||
│ FALSIFICATION (hand) │ ← FISSO
|
||||
│ 1 agente Tier-B │
|
||||
│ Funzione: traduce │
|
||||
│ ipotesi in regole + │
|
||||
│ chiama backtest + │
|
||||
│ valuta con DSR │
|
||||
└──────────┬───────────┘
|
||||
│ strategie validate
|
||||
▼
|
||||
┌──────────────────────┐
|
||||
│ ADVERSARIAL (hand) │ ← FISSO
|
||||
│ 1 agente Tier-A │
|
||||
│ Funzione: red team │
|
||||
│ con checklist statica│
|
||||
│ (lookahead, regime, │
|
||||
│ crowding) │
|
||||
└──────────┬───────────┘
|
||||
│ strategie sopravvissute
|
||||
▼
|
||||
┌──────────────────────┐
|
||||
│ FITNESS LOOP │
|
||||
│ Update agent_fitness│
|
||||
│ Selezione + GA │
|
||||
└──────────────────────┘
|
||||
│
|
||||
▼
|
||||
Generazione N+1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Le quattro trappole del backtesting su crypto
|
||||
|
||||
Queste sono le killer specifiche del dominio. Vanno mitigate by design, non come afterthought.
|
||||
|
||||
### 3.1 Look-ahead bias subdolo
|
||||
|
||||
**Problema**: molti dati "storici" su crypto sono in realtà revisionati ex-post.
|
||||
- Funding rates: spesso medie giornaliere ricalcolate, non valori real-time storici
|
||||
- On-chain metrics (MVRV, NUPL, SOPR): formule che cambiano nel tempo, applicate retroattivamente
|
||||
- Liste top-N token: survivorship bias massiccio
|
||||
- Sentiment storico: ricostruzioni post-hoc, non disponibili in tempo reale a quei momenti
|
||||
|
||||
**Mitigazione**:
|
||||
- Ogni feature deve avere un campo `availability_lag`: quante ore dopo il timestamp T la feature era effettivamente disponibile
|
||||
- Backtest engine rifiuta di usare feature prima del lag
|
||||
- Documentazione esplicita di come/quando ogni feature è stata raccolta
|
||||
- Preferire fonti che pubblicano archivi real-time (Kaiko, Tardis.dev, Amberdata) a quelle ricostruite
|
||||
|
||||
### 3.2 Multiple testing su scala industriale
|
||||
|
||||
**Problema**: con 10000 strategie testate, ~100 superano p<0.01 per puro caso. Senza correzione, il sistema produce sempre "vincitori" illusori.
|
||||
|
||||
**Mitigazione**:
|
||||
- **Deflated Sharpe Ratio (DSR)** come fitness primaria, non Sharpe naive
|
||||
- Tracking del numero totale di strategie testate fino a generazione N (impatta DSR)
|
||||
- Bonferroni-style correction quando si seleziona "top strategies" da reporting
|
||||
- Hold-out set finale **mai toccato durante evoluzione** per validation finale
|
||||
|
||||
### 3.3 Regime dependency
|
||||
|
||||
**Problema**: BTC/ETH hanno regimi macro molto diversi. Una strategia che funziona 2018-2024 può essere semplicemente "long-only momentum con leva". OOS 2026 fallisce.
|
||||
|
||||
**Periodi di regime distintivi**:
|
||||
- 2017: bull mania retail (escluso dal dataset, troppo anomalo)
|
||||
- 2018-2019: bear lungo
|
||||
- 2020-2021: DeFi summer + bull istituzionale + COVID
|
||||
- 2022: collapse cycle (LUNA, FTX, Celsius)
|
||||
- 2023-2024: ripresa + ETF spot
|
||||
- 2025-2026: post-ETF, regime nuovo
|
||||
|
||||
**Mitigazione**:
|
||||
- Walk-forward con **purged cross-validation** (López de Prado 2018)
|
||||
- Train su finestra mobile, test su successiva, **gap di purging** in mezzo per evitare leakage
|
||||
- Fitness penalizza strategie che funzionano solo su 1-2 regimi
|
||||
- OOS finale obbligatoriamente su periodo diverso dal training
|
||||
|
||||
### 3.4 Backtest ≠ live execution
|
||||
|
||||
**Problema**: anche backtest perfetto ha gap col live.
|
||||
- Slippage non lineare con size (su crypto particolarmente)
|
||||
- Fees variabili (maker/taker, volume rebates)
|
||||
- Funding rate sui perp può mangiarsi l'edge
|
||||
- Liquidità evapora nei momenti che contano
|
||||
- API outages, exchange downtime
|
||||
|
||||
**Mitigazione**:
|
||||
- Modello di slippage realistico (Almgren-Chriss o simile, non costante)
|
||||
- Fees accurate (struttura tier per exchange)
|
||||
- Funding payments simulati per posizioni perp
|
||||
- Cap su size per evitare strategie che funzionano solo a $100, non a $100K
|
||||
|
||||
---
|
||||
|
||||
## 4. Dataset Specification
|
||||
|
||||
### 4.1 Coverage
|
||||
|
||||
- **Asset**: BTC, ETH (focus iniziale)
|
||||
- **Periodo**: 2018-01-01 → 2025-12-31
|
||||
- **Train/OOS split**: train 2018-2023, OOS validation 2024, OOS final hold-out 2025
|
||||
- **Frequenza base**: 1-hour bars (compromesso tra granularità e gestibilità)
|
||||
- **Frequenze derivate**: 4h, 1d aggregate per features di lungo periodo
|
||||
|
||||
### 4.2 Feature catalog
|
||||
|
||||
**Price/Volume (sempre disponibili real-time, lag=0)**:
|
||||
- OHLCV su 1h, 4h, 1d
|
||||
- Returns log su orizzonti multipli
|
||||
- Volatility realized (Garman-Klass, Parkinson, RV)
|
||||
- Volume profile, VWAP
|
||||
|
||||
**Derivatives (lag tipicamente 5-15min)**:
|
||||
- Funding rates (Bybit, Binance, Hyperliquid, Deribit)
|
||||
- Open Interest (per exchange e aggregato)
|
||||
- Put/Call ratio (Deribit options)
|
||||
- Implied volatility surface (Deribit)
|
||||
- Skew, term structure
|
||||
- Liquidations (volume e direzione)
|
||||
|
||||
**On-chain (lag tipicamente 1-6 ore per finalization)**:
|
||||
- Active addresses
|
||||
- Transaction count + volume
|
||||
- Exchange inflows/outflows (Glassnode-style)
|
||||
- Miner flows
|
||||
- Whale transactions (>$1M)
|
||||
- MVRV, NUPL, SOPR (con cautela su revisionalità)
|
||||
|
||||
**Macro context (lag variabile)**:
|
||||
- DXY, gold, S&P 500, yield 10Y (per correlazioni)
|
||||
- Crypto-specific: BTC dominance, ETH/BTC ratio, total market cap
|
||||
- Stablecoin supply (USDT, USDC, DAI)
|
||||
|
||||
**Sentiment (lag variabile, qualità incerta)**:
|
||||
- Funding rate come proxy sentiment
|
||||
- Open Interest variations come proxy speculation
|
||||
- (Skip Twitter/social per ora — qualità storica troppo bassa)
|
||||
|
||||
### 4.3 Data sources
|
||||
|
||||
**Preferiti** (real-time archivi):
|
||||
- Tardis.dev (derivatives, order book, trades — premium)
|
||||
- Kaiko (institutional grade — premium)
|
||||
- Amberdata (multi-source)
|
||||
|
||||
**Backup gratuiti/cheap**:
|
||||
- CCXT historical (OHLCV affidabile)
|
||||
- Binance/Bybit/Deribit API direct (con cautela su gap)
|
||||
- CoinGlass (derivatives aggregati, qualità media)
|
||||
- Glassnode free tier (on-chain, limitato)
|
||||
|
||||
**Da evitare**:
|
||||
- Aggregatori che non documentano metodologia
|
||||
- Source che hanno cambiato formula nel tempo senza versioning
|
||||
|
||||
### 4.4 Storage
|
||||
|
||||
```sql
|
||||
CREATE TABLE features (
|
||||
timestamp TIMESTAMPTZ NOT NULL,
|
||||
asset TEXT NOT NULL,
|
||||
feature_name TEXT NOT NULL,
|
||||
value DOUBLE PRECISION,
|
||||
availability_lag_seconds INT NOT NULL, -- CRITICO
|
||||
source TEXT,
|
||||
version TEXT, -- per gestire cambi di metodologia
|
||||
PRIMARY KEY (timestamp, asset, feature_name)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_feat_time ON features (timestamp);
|
||||
CREATE INDEX idx_feat_asset_name ON features (asset, feature_name);
|
||||
```
|
||||
|
||||
Considerare TimescaleDB se le query temporali diventano colli di bottiglia.
|
||||
|
||||
---
|
||||
|
||||
## 5. Backtest Engine
|
||||
|
||||
### 5.1 Requisiti
|
||||
|
||||
- **Determinismo**: stesso input, stesso output, sempre
|
||||
- **Velocità**: target ≥100 backtest/sec su CPU normale (per gestire 50 agenti × 10 ipotesi/gen)
|
||||
- **Anti-leakage by design**: rifiuta feature prima di availability_lag
|
||||
- **Walk-forward integrato**: non opzionale, parte del flow base
|
||||
- **Realistic execution**: slippage, fees, funding
|
||||
|
||||
### 5.2 Architettura
|
||||
|
||||
**Linguaggio**: Python+NumPy per il PoC. Rust+PyO3 solo se diventa bottleneck (probabile in fase scaling, non necessario per PoC).
|
||||
|
||||
**API base**:
|
||||
```python
|
||||
class BacktestEngine:
|
||||
def run(self,
|
||||
strategy: StrategySpec,
|
||||
features: FeatureSet,
|
||||
time_range: tuple[datetime, datetime],
|
||||
walk_forward_config: WFConfig) -> BacktestResult:
|
||||
...
|
||||
|
||||
def run_with_dsr(self,
|
||||
strategy: StrategySpec,
|
||||
...) -> tuple[BacktestResult, DSRStats]:
|
||||
...
|
||||
|
||||
@dataclass
|
||||
class StrategySpec:
|
||||
entry_rules: list[Rule] # condizioni di ingresso
|
||||
exit_rules: list[Rule] # condizioni di uscita
|
||||
sizing: SizingRule # position sizing
|
||||
instruments: list[str] # BTC, ETH, BTC-PERP, etc.
|
||||
constraints: list[Constraint] # max leverage, max DD, etc.
|
||||
|
||||
@dataclass
|
||||
class BacktestResult:
|
||||
pnl_curve: pd.Series
|
||||
sharpe: float
|
||||
sortino: float
|
||||
max_drawdown: float
|
||||
n_trades: int
|
||||
win_rate: float
|
||||
avg_holding_time: timedelta
|
||||
fees_paid: float
|
||||
funding_paid: float
|
||||
slippage_cost: float
|
||||
regime_breakdown: dict[str, float] # PnL per regime
|
||||
```
|
||||
|
||||
### 5.3 Execution model
|
||||
|
||||
**Slippage**:
|
||||
```
|
||||
slippage = base_spread + impact_factor * (size / avg_volume_5min) ^ 0.5
|
||||
```
|
||||
Calibrato su book reale per BTC/ETH. Più alto in regimi di alta volatilità.
|
||||
|
||||
**Fees**:
|
||||
- Maker: 0.02% (tipico)
|
||||
- Taker: 0.05% (tipico)
|
||||
- Tier per volume non simulato nel PoC (assume tier base)
|
||||
|
||||
**Funding** (per perp):
|
||||
- Pagato/ricevuto ogni 8 ore
|
||||
- Calcolato su mark price × position size × funding rate
|
||||
|
||||
**Constraints automatiche**:
|
||||
- Max leverage (configurabile, default 5x per il PoC)
|
||||
- Margin call simulati realisticamente
|
||||
- Liquidations forzate se margin scende sotto threshold
|
||||
|
||||
### 5.4 Walk-forward purged CV
|
||||
|
||||
```
|
||||
Time: |---------train-----------|gap|----test----|---next gap---|--next test--|
|
||||
Window 1: [2018-01 ──────── 2020-06] [2020-07 ──── 2020-12]
|
||||
Window 2: [2018-07 ──── 2021-06] [2021-07 ──── 2021-12]
|
||||
Window 3: [2019-01 ── 2022-06] [2022-07 ── 2022-12]
|
||||
...
|
||||
```
|
||||
|
||||
- Training window: 30 mesi
|
||||
- Gap (purging): 1 mese (evita leakage da event horizon)
|
||||
- Test window: 6 mesi
|
||||
- Step: 6 mesi
|
||||
- Embargo: ulteriori 2 settimane post-test prima del prossimo training (López de Prado embargo)
|
||||
|
||||
---
|
||||
|
||||
## 6. Hypothesis Swarm (l'unico layer che evolve)
|
||||
|
||||
### 6.1 Genome design
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class HypothesisAgentGenome:
|
||||
# Cognizione
|
||||
system_prompt: str
|
||||
cognitive_style: str # "physicist", "biologist", "engineer", "trader_oldschool"...
|
||||
|
||||
# Accesso a dati
|
||||
feature_access: list[str] # subset delle feature disponibili
|
||||
lookback_window_days: int # quanto storico vede
|
||||
timeframes: list[str] # ["1h", "4h", "1d"]
|
||||
|
||||
# Modello
|
||||
model_tier: ModelTier # B o C principalmente nel PoC
|
||||
temperature: float # 0.6 - 1.2
|
||||
|
||||
# Bias di output
|
||||
strategy_type_preference: list[str] # ["mean_reversion", "momentum", "vol_arb", "cross_asset"]
|
||||
timeframe_preference: str # "intraday", "swing", "position"
|
||||
|
||||
# Tracking
|
||||
parent_ids: list[UUID]
|
||||
generation: int
|
||||
species_id: UUID
|
||||
```
|
||||
|
||||
### 6.2 Output format (nel protocollo fisso)
|
||||
|
||||
```
|
||||
PROPOSE_STRATEGY(
|
||||
id=#strat_47,
|
||||
name="vol_skew_momentum",
|
||||
|
||||
entry_conditions=[
|
||||
AND(
|
||||
(deribit_skew_25d > rolling_mean(deribit_skew_25d, 30d) + 1.5*std),
|
||||
(btc_funding_8h > 0.01),
|
||||
(eth_funding_8h > btc_funding_8h)
|
||||
)
|
||||
],
|
||||
|
||||
exit_conditions=[
|
||||
OR(
|
||||
(skew normalized below mean),
|
||||
(max_holding > 7d),
|
||||
(drawdown > 0.03)
|
||||
)
|
||||
],
|
||||
|
||||
sizing=KELLY_FRACTIONAL(fraction=0.25),
|
||||
instruments=["ETH-PERP-HL"],
|
||||
side="long",
|
||||
|
||||
rationale="Skew elevato indica fear di puts, funding alto indica long crowding. Cross-section ETH/BTC funding suggerisce ETH outperformance attesa.",
|
||||
|
||||
expected_regime="normal volatility, trending",
|
||||
expected_failure_modes=["crash con depinning", "regime shift improvviso"]
|
||||
)
|
||||
```
|
||||
|
||||
### 6.3 Operatori genetici
|
||||
|
||||
**Mutazione del system prompt**:
|
||||
- LLM-as-mutator (Tier-B): "Modifica questo prompt cambiando un aspetto cognitivo, mantenendo intent"
|
||||
- Probabilità: 60% per agente selezionato
|
||||
|
||||
**Mutazione di feature_access**:
|
||||
- Add/remove 1-3 feature random
|
||||
- Probabilità: 30%
|
||||
|
||||
**Mutazione di temperature**:
|
||||
- Gaussiana, σ=0.1, clip [0.3, 1.4]
|
||||
- Probabilità: 20%
|
||||
|
||||
**Crossover**:
|
||||
- 50/50 di feature_access da due parent
|
||||
- Cognitive_style preso da parent migliore
|
||||
- System prompt: crossover sezione-by-sezione (role/context/instructions/output_format)
|
||||
- Probabilità: 50% per nuova generazione (resto è mutazione)
|
||||
|
||||
**Mutazione di model_tier**:
|
||||
- Rara (5%), perché impatta costo
|
||||
- Solo C↔B, non promozioni a A senza fitness eccezionale
|
||||
|
||||
### 6.4 Selection
|
||||
|
||||
**Tournament selection**, k=5.
|
||||
|
||||
**Speciation semplificata**:
|
||||
- Embedding del system prompt via Voyage o OpenAI ada
|
||||
- K-means con K=5-7 cluster
|
||||
- Fitness sharing dentro cluster
|
||||
|
||||
**Elitismo**: top 3 agenti per cluster sopravvivono non modificati.
|
||||
|
||||
**Immigrazione**: ogni 10 generazioni, 5 nuovi agenti random vengono inseriti (anti-stagnazione).
|
||||
|
||||
---
|
||||
|
||||
## 7. Falsification Agent (hand-crafted, fisso)
|
||||
|
||||
### 7.1 Ruolo
|
||||
|
||||
Prende ipotesi dal swarm, le esegue su backtest engine, riporta risultati con DSR e diagnostica.
|
||||
|
||||
**NON** valuta soggettivamente. Esegue test deterministici e li interpreta.
|
||||
|
||||
### 7.2 Modello
|
||||
|
||||
Tier-B (Qwen Max o equivalente). Sufficiente per:
|
||||
- Tradurre ipotesi in linguaggio naturale → StrategySpec strutturato
|
||||
- Chiamare backtest engine
|
||||
- Interpretare output numerico
|
||||
- Riportare in protocollo
|
||||
|
||||
### 7.3 System prompt template
|
||||
|
||||
```
|
||||
Sei un agente di falsification rigoroso. Il tuo ruolo è testare ipotesi
|
||||
trading senza pregiudizi e riportare risultati onesti.
|
||||
|
||||
Per ogni ipotesi ricevuta:
|
||||
1. Verifica che le entry/exit conditions siano formalizzabili in regole testabili
|
||||
2. Verifica che le feature richieste rispettino availability_lag
|
||||
3. Chiama il backtest engine con configurazione walk-forward standard
|
||||
4. Calcola Deflated Sharpe Ratio considerando il numero di test fatti finora ({total_trials})
|
||||
5. Riporta breakdown per regime (bear/bull/sideways)
|
||||
6. Identifica failure modes osservati nel backtest
|
||||
|
||||
Output strict in protocollo S-expression. Nessuna interpretazione narrativa.
|
||||
Se l'ipotesi non è formalizzabile, ritorna REJECT con motivo specifico.
|
||||
```
|
||||
|
||||
### 7.4 Output format
|
||||
|
||||
```
|
||||
REPORT_BACKTEST(
|
||||
target=#strat_47,
|
||||
|
||||
metrics=(
|
||||
sharpe=1.34,
|
||||
deflated_sharpe=0.87,
|
||||
p_value=0.04,
|
||||
max_drawdown=0.18,
|
||||
n_trades=143,
|
||||
avg_holding_h=42
|
||||
),
|
||||
|
||||
regime_performance=(
|
||||
bear_2018=0.42,
|
||||
bull_2020=2.1,
|
||||
crash_2022=-0.85,
|
||||
recovery_2023=1.1
|
||||
),
|
||||
|
||||
warnings=[
|
||||
"Performance dominated by 2020-2021 regime",
|
||||
"DSR significant but multiple testing burden high (8400 strategies tested)"
|
||||
],
|
||||
|
||||
verdict=PASS_WITH_WARNINGS // PASS / FAIL / PASS_WITH_WARNINGS
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. Adversarial Agent (hand-crafted, fisso)
|
||||
|
||||
### 8.1 Ruolo
|
||||
|
||||
Per ogni strategia che passa Falsification, applica una **checklist statica** di attacchi epistemici.
|
||||
|
||||
Nel PoC NON evolve. Ha vocabolario fisso di attacchi noti dalla letteratura (López de Prado, Bailey, etc.).
|
||||
|
||||
### 8.2 Modello
|
||||
|
||||
Tier-A (Sonnet). Qui il reasoning conta — riconoscere lookahead bias subtle richiede capacità.
|
||||
|
||||
### 8.3 Checklist di attacchi
|
||||
|
||||
```
|
||||
1. Lookahead bias check
|
||||
- Tutte le feature usate rispettano availability_lag?
|
||||
- Qualche feature è "future-derived" subdolamente?
|
||||
|
||||
2. Survivorship bias check
|
||||
- La strategia usa universo di asset dinamico?
|
||||
- Filtra solo asset sopravvissuti?
|
||||
|
||||
3. Regime dependency check
|
||||
- Performance concentrata in 1-2 regimi specifici?
|
||||
- Cosa succede se rimuovi il regime migliore?
|
||||
|
||||
4. Multiple testing severity
|
||||
- DSR resta significativo dopo Bonferroni stretto?
|
||||
- Confronto con random strategy baseline
|
||||
|
||||
5. Crowding plausibility
|
||||
- La logica è "ovvia"? Probabilmente già crowded
|
||||
- Edge size ragionevole o sospettosamente alto?
|
||||
|
||||
6. Implementation friction
|
||||
- Slippage assumption realistica per la size?
|
||||
- Funding payments inclusi correttamente?
|
||||
- Trade frequency compatibile con execution reale?
|
||||
|
||||
7. Feature stability
|
||||
- Le feature critiche hanno stessa metodologia in tutto il periodo?
|
||||
- Source provider ha cambiato formula?
|
||||
|
||||
8. Statistical robustness
|
||||
- Sharpe sensibile a rimozione top 5% trade?
|
||||
- Performance robusta a perturbazioni piccole nei parametri?
|
||||
```
|
||||
|
||||
### 8.4 Output
|
||||
|
||||
```
|
||||
ADVERSARIAL_REVIEW(
|
||||
target=#strat_47,
|
||||
attacks_passed=[1, 2, 5, 6, 7],
|
||||
attacks_failed=[3, 4, 8],
|
||||
|
||||
critical_concerns=[
|
||||
CONCERN(
|
||||
type=regime_dependency,
|
||||
severity=high,
|
||||
detail="60% of PnL from 2020-2021 bull regime. Removing it gives Sharpe 0.4."
|
||||
),
|
||||
CONCERN(
|
||||
type=multiple_testing,
|
||||
severity=medium,
|
||||
detail="DSR significant but only marginally (p=0.04). With 8400 trials, expect ~336 false positives at this level."
|
||||
)
|
||||
],
|
||||
|
||||
verdict=REJECT // ACCEPT / ACCEPT_CONDITIONAL / REJECT
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 9. Fitness Function
|
||||
|
||||
### 9.1 Agent fitness (per Hypothesis swarm)
|
||||
|
||||
```python
|
||||
def agent_fitness(agent: HypothesisAgentGenome,
|
||||
episodes: list[Episode]) -> float:
|
||||
|
||||
# Quality of contributions
|
||||
accepted_strats = [e for e in episodes
|
||||
if e.adversarial_verdict == "ACCEPT"]
|
||||
conditional_strats = [e for e in episodes
|
||||
if e.adversarial_verdict == "ACCEPT_CONDITIONAL"]
|
||||
|
||||
quality_score = (
|
||||
len(accepted_strats) * 1.0
|
||||
+ len(conditional_strats) * 0.3
|
||||
)
|
||||
|
||||
# Average DSR of accepted strategies
|
||||
avg_dsr = np.mean([e.dsr for e in accepted_strats]) if accepted_strats else 0
|
||||
|
||||
# Cost penalty (proporzionale al tier usato)
|
||||
cost_per_episode = TIER_COST[agent.model_tier] * agent.avg_tokens_per_episode
|
||||
cost_penalty = cost_per_episode * COST_PENALTY_LAMBDA
|
||||
|
||||
# Novelty bonus (semantica delle strategie prodotte)
|
||||
novelty_score = compute_novelty(agent, all_agents_in_generation)
|
||||
|
||||
# Diversity bonus (strategie diverse, non cloni)
|
||||
diversity_score = compute_internal_diversity(agent.strategies)
|
||||
|
||||
return (
|
||||
quality_score
|
||||
+ avg_dsr * DSR_WEIGHT
|
||||
+ novelty_score * NOVELTY_WEIGHT
|
||||
+ diversity_score * DIVERSITY_WEIGHT
|
||||
- cost_penalty
|
||||
)
|
||||
```
|
||||
|
||||
### 9.2 Pesi iniziali (da calibrare empiricamente)
|
||||
|
||||
```python
|
||||
DSR_WEIGHT = 2.0 # peso forte: vogliamo edge reale
|
||||
NOVELTY_WEIGHT = 0.5 # bonus moderato per esplorazione
|
||||
DIVERSITY_WEIGHT = 0.3 # leggero, evita cloni
|
||||
COST_PENALTY_LAMBDA = 0.1 # da calibrare in base a budget
|
||||
```
|
||||
|
||||
### 9.3 Anti-gaming
|
||||
|
||||
- Tracking globale del numero di test fatti per DSR correction
|
||||
- Cap su `quality_score` per evitare gaming via spam di ipotesi banali
|
||||
- Adversarial feedback va in fitness (strategie REJECTED penalizzano, non solo non bonificano)
|
||||
- Periodic audit umano: ogni 10 generazioni rivedere top 5 strategies, marcare "spurious" se gaming
|
||||
|
||||
---
|
||||
|
||||
## 10. Baseline non-LLM (anti-illusion check)
|
||||
|
||||
**Critico**: il PoC deve includere una baseline statistica tradizionale.
|
||||
|
||||
### 10.1 Specifica baseline
|
||||
|
||||
- **Metodo**: Random Forest + feature engineering manuale
|
||||
- **Features**: stesso pool del swarm
|
||||
- **Target**: returns N-bar future (predizione regression o classification)
|
||||
- **Validation**: stesso walk-forward purged CV
|
||||
- **Output**: trade signals → backtest engine identico
|
||||
|
||||
### 10.2 Confronto
|
||||
|
||||
| Metrica | Baseline RF | Swarm LLM |
|
||||
|----------------------------|-------------|-----------|
|
||||
| Best DSR found | ? | ? |
|
||||
| Avg DSR top-10 strategies | ? | ? |
|
||||
| N strategies passing adv. | ? | ? |
|
||||
| Diversity (semantic) | ? | ? |
|
||||
| Total cost | ~$50 | ~$3K |
|
||||
|
||||
**Interpretazione**:
|
||||
- Se Swarm batte significativamente Baseline → architettura LLM sta aggiungendo valore
|
||||
- Se Swarm pareggia → costo non giustificato per discovery numerica pura
|
||||
- Se Swarm perde → l'edge degli LLM agents è altrove (creatività su task non numerici, integrazione semi-strutturata)
|
||||
|
||||
**Punto importante**: anche se Swarm perde su pattern discovery numerico, NON significa che il sistema co-evolutivo completo è inutile. Significa che la value-add è in altri domini (ipotesi macro, integrazione narrative, generalizzazione cross-domain). È informazione preziosa.
|
||||
|
||||
---
|
||||
|
||||
## 11. Roadmap Implementativa (3-4 mesi)
|
||||
|
||||
### Settimane 1-3: Setup & Dataset
|
||||
|
||||
- Setup repo, environment, dependencies
|
||||
- Data sourcing: identificare provider, sottoscrivere se necessario
|
||||
- Data ingestion: pipeline dati storici BTC/ETH 2018-2025
|
||||
- Storage schema (PostgreSQL/TimescaleDB)
|
||||
- Audit di availability_lag per ogni feature
|
||||
- Sanity checks: confronto cross-source per validare integrity
|
||||
|
||||
**Deliverable**: dataset completo annotato, query-able, con feature catalog.
|
||||
|
||||
### Settimane 4-7: Backtest Engine
|
||||
|
||||
- Implementazione walk-forward purged CV
|
||||
- Slippage model (Almgren-Chriss)
|
||||
- Fees + funding
|
||||
- DSR computation
|
||||
- Test di non-leakage (deliberately inject lookahead → engine deve catcharlo)
|
||||
- Performance optimization (target ≥100 backtest/sec)
|
||||
|
||||
**Deliverable**: backtest engine standalone testato. Strategie semplici (buy & hold, 50/200 SMA crossover) producono risultati noti.
|
||||
|
||||
### Settimane 8-9: Agenti hand-crafted
|
||||
|
||||
- Falsification agent (Tier-B)
|
||||
- Adversarial agent (Tier-A)
|
||||
- Protocol fisso (S-expression parser + 15 verbi)
|
||||
- Message bus auditato semplificato
|
||||
- Test end-to-end con ipotesi hand-crafted
|
||||
|
||||
**Deliverable**: pipeline agente → falsification → adversarial → verdict, funzionante end-to-end con strategie inserite manualmente.
|
||||
|
||||
### Settimane 10-12: Hypothesis Swarm + GA
|
||||
|
||||
- Population manager (50 agenti)
|
||||
- Operatori genetici (mutazione + crossover)
|
||||
- Speciation con clustering embedding
|
||||
- Fitness computation
|
||||
- Tournament selection
|
||||
- Evoluzione per 50 generazioni baseline
|
||||
|
||||
**Deliverable**: swarm che evolve, fitness che migliora over generations, output di top strategies.
|
||||
|
||||
### Settimane 13-14: Baseline + Comparison
|
||||
|
||||
- Implementazione baseline Random Forest
|
||||
- Run baseline su stesso dataset/timeframe
|
||||
- Tabella comparison
|
||||
- Analisi qualitative delle strategie prodotte da entrambi
|
||||
|
||||
**Deliverable**: documento di confronto onesto.
|
||||
|
||||
### Settimane 15-16: Analisi & Report
|
||||
|
||||
- Run finale su hold-out set 2025
|
||||
- Analysis: ablation multi-tier, regime breakdown, diversity analysis
|
||||
- Documentazione lessons learned
|
||||
- Decision document: procedere con sistema completo, iterare PoC, o pivot
|
||||
|
||||
**Deliverable**: report finale con risposte alle 5 domande di validazione iniziali.
|
||||
|
||||
---
|
||||
|
||||
## 12. Costi PoC
|
||||
|
||||
### 12.1 LLM costs
|
||||
|
||||
| Fase | Calls stimate | Tokens stimati | Tier mix | Costo |
|
||||
|-------------------------|---------------|-----------------|--------------|--------|
|
||||
| Setup + agenti hand | ~500 | 2M | A/B | ~$50 |
|
||||
| Test pipeline | ~2000 | 8M | B/C | ~$30 |
|
||||
| GA run principale | 50 gen × 50 agent × 10 calls × 5K tok = 125M tok | mostly C, some B | ~$300-500 |
|
||||
| Repeat runs (3x) | (per ablation, tuning) | | | ~$1000-1500 |
|
||||
| Hold-out validation | ~1000 | 5M | A (judge) | ~$80 |
|
||||
| **Totale** | | | | **$1500-2500** |
|
||||
|
||||
Più conservativo se aggiungiamo iteration overhead: **budget $2-4K**.
|
||||
|
||||
### 12.2 Infrastruttura
|
||||
|
||||
- Compute (locale o cloud modesto): ~$200-400 totali
|
||||
- Storage (data + logs): ~$50/mese
|
||||
- Data subscription (Tardis o Kaiko per derivatives): variabile, $0-500/mese a seconda della qualità richiesta
|
||||
|
||||
### 12.3 Tempo
|
||||
|
||||
- Realistic estimate: **3-4 mesi** a 3-4 giorni/settimana effettivi
|
||||
- Pessimistic estimate: 5-6 mesi se data sourcing risulta più complesso del previsto
|
||||
|
||||
---
|
||||
|
||||
## 13. Rischi e Mitigazioni
|
||||
|
||||
| Rischio | Probabilità | Impatto | Mitigazione |
|
||||
|------------------------------------|-------------|---------|-----------------------------------------|
|
||||
| Data sourcing più complesso | Alta | Medio | Start con CCXT + Binance free, upgrade dopo |
|
||||
| Backtest engine ha bug subtle | Alta | Critico | Test deliberato di lookahead injection |
|
||||
| Swarm non batte baseline RF | Media | Alto | È un risultato comunque, non fallimento |
|
||||
| Costi over-budget | Media | Medio | Cap hard, monitor settimanale |
|
||||
| Time over-budget (5+ mesi) | Alta | Medio | Milestone bi-settimanali, taglio scope se necessario |
|
||||
| Convergenza prematura del swarm | Media | Alto | Speciation, novelty bonus, immigrazione |
|
||||
| OpenRouter qualità inconsistente | Media | Basso | Fallback policy, monitor success rate |
|
||||
|
||||
---
|
||||
|
||||
## 14. Decisioni Aperte
|
||||
|
||||
Da risolvere prima di Fase 1:
|
||||
|
||||
1. **Asset focus**: solo BTC, solo ETH, o entrambi? (suggerito: entrambi, per cross-asset features)
|
||||
2. **Data provider primario**: pagare Tardis/Kaiko o partire con free sources? (suggerito: free per Fase 1, upgrade se necessario)
|
||||
3. **Hardware**: locale o cloud? (suggerito: locale, dataset ~50-100GB gestibile)
|
||||
4. **Tier-A budget**: quanto reservare a Sonnet per Adversarial? (suggerito: $300-500 cap)
|
||||
5. **Frequency primaria**: 1h confermato o scendere a 15min? (suggerito: 1h, evita overfitting su microstructure)
|
||||
|
||||
---
|
||||
|
||||
## 15. Cosa il PoC NON fa (e va bene)
|
||||
|
||||
- NON evolve il protocollo (sistema completo)
|
||||
- NON co-evolve Falsification e Adversarial (sistema completo)
|
||||
- NON esplora idiom emergence (sistema completo)
|
||||
- NON applica a domini non-trading (sistema completo)
|
||||
- NON cerca breakthrough singoli, cerca **validazione architetturale**
|
||||
- NON è un trading bot pronto per capitale reale (mai dopo PoC, paper trading prima)
|
||||
|
||||
Il PoC è un **esperimento controllato**. Il sistema completo è il prodotto. Sono cose diverse, e va bene così.
|
||||
|
||||
---
|
||||
|
||||
## 16. Decision Triggers
|
||||
|
||||
Dopo PoC, queste sono le decisioni discrete:
|
||||
|
||||
**GO al sistema completo se**:
|
||||
- Swarm produce ≥5 strategie con DSR significativo dopo Adversarial
|
||||
- Swarm batte baseline RF di ≥30% in best-DSR e diversity
|
||||
- Strategie sopravvivono a OOS 2025 (regime change)
|
||||
- Multi-tier ablation conferma valore (no significant loss usando 70% Tier-C)
|
||||
|
||||
**ITERATE PoC se**:
|
||||
- Risultati borderline (alcuni indicatori sì, altri no)
|
||||
- Bug specifici identificabili (es. GA non diversifica → fix speciation)
|
||||
- Architettura sembra giusta ma implementazione perfettibile
|
||||
|
||||
**PIVOT se**:
|
||||
- Swarm decisamente perde vs baseline su discovery numerica
|
||||
- Ma: considerare pivot verso domini non-numerici (offerte commerciali, code review)
|
||||
- O: considerare uso del swarm come *generator* di ipotesi macro/strutturali, non pattern numerici
|
||||
|
||||
---
|
||||
|
||||
*Documento da aggiornare durante PoC. Questa è v0.1, scritta prima di qualunque implementazione.*
|
||||
*Documento principale (sistema completo): `coevolutive_swarm_system.md`*
|
||||
+18
-25
@@ -1,26 +1,26 @@
|
||||
[project]
|
||||
name = "multi-swarm"
|
||||
name = "multi-swarm-coevolutive"
|
||||
version = "0.1.0"
|
||||
description = "Multi-Swarm Coevolutive PoC trading swarm — Phase 1 lean spike"
|
||||
description = "Multi-Swarm Coevolutive: monorepo workspace (core + strategie)"
|
||||
authors = [{ name = "Adriano Dal Pastro", email = "adrianodalpastro@tielogic.com" }]
|
||||
requires-python = ">=3.13"
|
||||
# I due workspace member sono dipendenze runtime del deployable app root.
|
||||
# Cosi' `uv sync --frozen --no-dev` (nel builder Docker) li installa entrambi
|
||||
# come editable nella venv senza tirare pytest/ruff/mypy.
|
||||
dependencies = [
|
||||
"pandas>=2.2",
|
||||
"numpy>=2.1",
|
||||
"scipy>=1.14",
|
||||
"pydantic>=2.9",
|
||||
"pydantic-settings>=2.6",
|
||||
"sqlmodel>=0.0.22",
|
||||
"openai>=1.55",
|
||||
"httpx>=0.28",
|
||||
"requests>=2.32",
|
||||
"tenacity>=9.0",
|
||||
"pyyaml>=6.0",
|
||||
"streamlit>=1.40",
|
||||
"plotly>=5.24",
|
||||
"pyarrow>=18.0",
|
||||
"multi-swarm-core",
|
||||
"strategy-crypto",
|
||||
"strategy-pythagoras",
|
||||
]
|
||||
|
||||
[tool.uv.workspace]
|
||||
members = ["src/multi_swarm_core", "src/strategy_crypto", "src/strategy_pythagoras"]
|
||||
|
||||
[tool.uv.sources]
|
||||
multi-swarm-core = { workspace = true }
|
||||
strategy-crypto = { workspace = true }
|
||||
strategy-pythagoras = { workspace = true }
|
||||
|
||||
[dependency-groups]
|
||||
dev = [
|
||||
"pytest>=8.3",
|
||||
@@ -32,13 +32,6 @@ dev = [
|
||||
"types-requests>=2.32",
|
||||
]
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
packages = ["src/multi_swarm"]
|
||||
|
||||
[tool.ruff]
|
||||
line-length = 100
|
||||
target-version = "py313"
|
||||
@@ -51,8 +44,8 @@ python_version = "3.13"
|
||||
strict = true
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["tests"]
|
||||
addopts = "-v --tb=short"
|
||||
testpaths = ["src/multi_swarm_core/tests", "src/strategy_crypto/tests", "src/strategy_pythagoras/tests"]
|
||||
addopts = "-v --tb=short --import-mode=importlib"
|
||||
markers = [
|
||||
"integration: tests that require external services (Cerbero, LLM API)",
|
||||
"slow: tests that take more than 5 seconds",
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
"""Analisi per-trade dei top-K candidate del run BTC.
|
||||
|
||||
Per ciascun genome top-K, ri-esegue il backtest su ogni fold WFA e raccoglie:
|
||||
- n_trades, n_wins, n_losses, win_rate
|
||||
- max_drawdown
|
||||
- return, sharpe
|
||||
- list trade pnl summary
|
||||
|
||||
Output stampato a stdout, non scrive su disco.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from multi_swarm_core.agents.hypothesis import _try_parse
|
||||
from multi_swarm_core.backtest.engine import BacktestEngine
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm_core.data.splits import expanding_walk_forward
|
||||
from multi_swarm_core.metrics.basic import max_drawdown, sharpe_ratio, total_return
|
||||
from multi_swarm_core.persistence.repository import Repository
|
||||
from multi_swarm_core.protocol.compiler import compile_strategy
|
||||
|
||||
|
||||
def main() -> None:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--run-id", required=True)
|
||||
ap.add_argument("--top-k", type=int, default=10)
|
||||
ap.add_argument("--n-folds", type=int, default=4)
|
||||
ap.add_argument("--train-ratio", type=float, default=0.5)
|
||||
ap.add_argument("--symbol", default="BTC-PERPETUAL")
|
||||
ap.add_argument("--timeframe", default="1h")
|
||||
ap.add_argument("--start", default="2018-09-01T00:00:00+00:00")
|
||||
ap.add_argument("--end", default="2026-01-01T00:00:00+00:00")
|
||||
ap.add_argument("--fees-bp", type=float, default=5.0)
|
||||
args = ap.parse_args()
|
||||
|
||||
settings = load_settings()
|
||||
repo = Repository(settings.ga_db_path)
|
||||
repo.init_schema()
|
||||
all_evals = repo.list_evaluations(args.run_id)
|
||||
parseable = [
|
||||
e for e in all_evals
|
||||
if e.get("raw_text") and not e.get("parse_error") and e["fitness"] > 0
|
||||
]
|
||||
parseable.sort(key=lambda e: e["fitness"], reverse=True)
|
||||
seen: set[str] = set()
|
||||
top: list[dict] = []
|
||||
for e in parseable:
|
||||
if e["genome_id"] in seen:
|
||||
continue
|
||||
seen.add(e["genome_id"])
|
||||
top.append(e)
|
||||
if len(top) >= args.top_k:
|
||||
break
|
||||
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
else settings.cerbero_testnet_token.get_secret_value()
|
||||
)
|
||||
cerbero = CerberoClient(
|
||||
base_url=settings.cerbero_base_url,
|
||||
token=token,
|
||||
bot_tag=settings.cerbero_bot_tag,
|
||||
)
|
||||
loader = CerberoOHLCVLoader(client=cerbero, cache_dir=settings.series_dir)
|
||||
ohlcv = loader.load(OHLCVRequest(
|
||||
symbol=args.symbol,
|
||||
timeframe=args.timeframe,
|
||||
start=datetime.fromisoformat(args.start),
|
||||
end=datetime.fromisoformat(args.end),
|
||||
))
|
||||
splits = expanding_walk_forward(ohlcv.index, train_ratio=args.train_ratio, n_folds=args.n_folds)
|
||||
engine = BacktestEngine(fees_bp=args.fees_bp)
|
||||
|
||||
print(f"\n{'=' * 110}")
|
||||
print(f"PER-TRADE ANALYSIS — top-{len(top)} genomes × {len(splits)} folds")
|
||||
print(f"{'=' * 110}")
|
||||
|
||||
for ev in top:
|
||||
strat, err = _try_parse(ev["raw_text"] or "")
|
||||
if strat is None:
|
||||
print(f"\n>>> {ev['genome_id'][:16]} — parse error: {err}")
|
||||
continue
|
||||
|
||||
print(f"\n>>> {ev['genome_id']} (fit_IS={ev['fitness']:.4f}, sharpe_IS={ev['sharpe']:.3f})")
|
||||
print(f"{'fold':<5} {'period':<26} {'trades':>7} {'wins':>5} {'losses':>7} {'win%':>6} {'avg_w':>9} {'avg_l':>9} {'ret':>7} {'maxDD':>7} {'sharpe':>7}")
|
||||
for s in splits:
|
||||
test_df = ohlcv.loc[s.test_idx]
|
||||
try:
|
||||
signal_fn = compile_strategy(strat)
|
||||
signals = signal_fn(test_df)
|
||||
bt = engine.run(test_df, signals)
|
||||
except Exception as e:
|
||||
print(f" fold {s.fold}: error {e}")
|
||||
continue
|
||||
trades = bt.trades
|
||||
n_trades = len(trades)
|
||||
wins = [t.net_pnl for t in trades if t.net_pnl > 0]
|
||||
losses = [t.net_pnl for t in trades if t.net_pnl <= 0]
|
||||
n_wins = len(wins)
|
||||
n_losses = len(losses)
|
||||
win_rate = (n_wins / n_trades * 100) if n_trades else 0.0
|
||||
avg_w = (sum(wins) / n_wins) if n_wins else 0.0
|
||||
avg_l = (sum(losses) / n_losses) if n_losses else 0.0
|
||||
# Normalize equity for DD/return
|
||||
if n_trades > 0:
|
||||
notional = float(test_df["close"].iloc[0])
|
||||
equity_pos = (bt.equity_curve / notional) + 1.0
|
||||
ret_pct = total_return(equity_pos)
|
||||
dd = max_drawdown(equity_pos)
|
||||
sr = sharpe_ratio(bt.returns, periods_per_year=8760)
|
||||
else:
|
||||
ret_pct = dd = sr = 0.0
|
||||
period = f"{str(s.test_idx[0])[:10]}..{str(s.test_idx[-1])[:10]}"
|
||||
print(f"{s.fold:<5} {period:<26} {n_trades:>7} {n_wins:>5} {n_losses:>7} {win_rate:>5.1f}% {avg_w:>9.1f} {avg_l:>9.1f} {ret_pct:>6.2%} {dd:>6.2%} {sr:>7.3f}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,99 @@
|
||||
"""Backtest standalone di una strategia su range esteso.
|
||||
|
||||
Carica un JSON strategia (formato del Hypothesis Agent output), fetcha OHLCV
|
||||
via Cerbero, esegue BacktestEngine + FalsificationReport + AdversarialReport,
|
||||
stampa metriche annualizzate.
|
||||
|
||||
Esempio:
|
||||
uv run python scripts/backtest_strategy.py /tmp/strategy_e52604ba.json \
|
||||
--start 2019-01-01 --end 2026-01-01 --label flat-ablation-top
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from multi_swarm_core.agents.adversarial import AdversarialAgent
|
||||
from multi_swarm_core.agents.falsification import FalsificationAgent
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm_core.protocol.parser import parse_strategy
|
||||
from multi_swarm_core.protocol.validator import validate_strategy
|
||||
|
||||
|
||||
def main() -> None:
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("strategy_file", type=Path)
|
||||
p.add_argument("--start", default="2019-01-01T00:00:00+00:00")
|
||||
p.add_argument("--end", default="2026-01-01T00:00:00+00:00")
|
||||
p.add_argument("--exchange", default="deribit")
|
||||
p.add_argument("--symbol", default="BTC-PERPETUAL")
|
||||
p.add_argument("--timeframe", default="1h")
|
||||
p.add_argument("--fees-bp", type=float, default=5.0)
|
||||
p.add_argument("--n-trials-dsr", type=int, default=50)
|
||||
p.add_argument("--label", default="strategy")
|
||||
args = p.parse_args()
|
||||
|
||||
strategy_json = json.loads(args.strategy_file.read_text())
|
||||
raw = json.dumps(strategy_json)
|
||||
parsed = parse_strategy(raw)
|
||||
validate_strategy(parsed)
|
||||
print(f"Strategy '{args.label}' parsed OK: {len(parsed.rules)} rules")
|
||||
|
||||
settings = load_settings()
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
else settings.cerbero_testnet_token.get_secret_value()
|
||||
)
|
||||
cerbero = CerberoClient(
|
||||
base_url=settings.cerbero_base_url,
|
||||
token=token,
|
||||
bot_tag=settings.cerbero_bot_tag,
|
||||
)
|
||||
loader = CerberoOHLCVLoader(client=cerbero, cache_dir=settings.series_dir)
|
||||
req = OHLCVRequest(
|
||||
symbol=args.symbol,
|
||||
timeframe=args.timeframe,
|
||||
start=datetime.fromisoformat(args.start),
|
||||
end=datetime.fromisoformat(args.end),
|
||||
exchange=args.exchange,
|
||||
)
|
||||
ohlcv = loader.load(req)
|
||||
n_bars = len(ohlcv)
|
||||
years = n_bars / (24 * 365.25)
|
||||
print(
|
||||
f"OHLCV loaded: {n_bars} bars "
|
||||
f"({ohlcv.index[0]} → {ohlcv.index[-1]}, ~{years:.2f} anni)"
|
||||
)
|
||||
|
||||
fals_agent = FalsificationAgent(fees_bp=args.fees_bp, n_trials_dsr=args.n_trials_dsr)
|
||||
adv_agent = AdversarialAgent(fees_bp=args.fees_bp)
|
||||
fals = fals_agent.evaluate(parsed, ohlcv)
|
||||
adv = adv_agent.review(parsed, ohlcv)
|
||||
|
||||
cagr = (1.0 + float(fals.total_return)) ** (1.0 / years) - 1.0 if years > 0 else float("nan")
|
||||
calmar = (cagr / float(fals.max_drawdown)) if fals.max_drawdown > 0 else float("inf")
|
||||
|
||||
print(f"\n=== {args.label} on {args.symbol} {args.timeframe} ({years:.2f} anni) ===")
|
||||
print(f"n_trades: {fals.n_trades}")
|
||||
print(f"total_return: {fals.total_return:+.4f} ({fals.total_return * 100:+.2f}%)")
|
||||
print(f"CAGR: {cagr:+.4f} ({cagr * 100:+.2f}%)")
|
||||
print(f"Sharpe (ann): {fals.sharpe:+.3f}")
|
||||
print(f"DSR: {fals.dsr:.4f} (pvalue {fals.dsr_pvalue:.4f})")
|
||||
print(f"max_drawdown: {fals.max_drawdown:.4f} ({fals.max_drawdown * 100:.2f}%)")
|
||||
print(f"Calmar: {calmar:+.3f}")
|
||||
print(f"\nAdversarial findings:")
|
||||
if not adv.findings:
|
||||
print(" (none)")
|
||||
for f in adv.findings:
|
||||
print(f" [{f.severity.value:6s}] {f.name:30s} {f.detail}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,139 @@
|
||||
"""Confronto per-trade dei 4 winner cross-run (BTC/ETH × 1h/5m).
|
||||
|
||||
Per ogni winner: ri-esegue il backtest su 4 fold WFA expanding-window e raccoglie
|
||||
trade buoni/non buoni, win-rate, avg PnL, return, max DD, Sharpe.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from multi_swarm_core.agents.hypothesis import _try_parse
|
||||
from multi_swarm_core.backtest.engine import BacktestEngine
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm_core.data.splits import expanding_walk_forward
|
||||
from multi_swarm_core.metrics.basic import max_drawdown, sharpe_ratio, total_return
|
||||
from multi_swarm_core.persistence.repository import Repository
|
||||
from multi_swarm_core.protocol.compiler import compile_strategy
|
||||
|
||||
|
||||
# (run_name, genome_id, symbol, timeframe, label)
|
||||
WINNERS = [
|
||||
("phase1-btc-100-001", "238e481262c1594c", "BTC-PERPETUAL", "1h", "BTC 1h sharpshooter (Gen 7)"),
|
||||
("phase1-btc-100-001", "23a24989e2ed0f84", "BTC-PERPETUAL", "1h", "BTC 1h robust (Gen 0 elite)"),
|
||||
("phase1-eth-100-001", "4b45a72c13acf1d5", "ETH-PERPETUAL", "1h", "ETH 1h best-by-sharpe (killed)"),
|
||||
("phase1-btc-100-5m-001", "f8ca6642adf7e0cd", "BTC-PERPETUAL", "5m", "BTC 5m robust winner"),
|
||||
("phase1-eth-100-5m-001", "c04dff7086bb9588", "ETH-PERPETUAL", "5m", "ETH 5m OOS winner"),
|
||||
]
|
||||
|
||||
|
||||
def analyze_genome(run_id: str, genome_id: str, symbol: str, timeframe: str, label: str,
|
||||
settings, cerbero, loader) -> None:
|
||||
repo = Repository(settings.ga_db_path)
|
||||
repo.init_schema()
|
||||
evs = [e for e in repo.list_evaluations(run_id) if e["genome_id"] == genome_id]
|
||||
if not evs:
|
||||
print(f" no eval for {genome_id} in {run_id}")
|
||||
return
|
||||
ev = evs[0]
|
||||
strat, err = _try_parse(ev.get("raw_text") or "")
|
||||
if strat is None:
|
||||
print(f" parse error: {err}")
|
||||
return
|
||||
|
||||
req = OHLCVRequest(
|
||||
symbol=symbol, timeframe=timeframe,
|
||||
start=datetime.fromisoformat("2018-09-01T00:00:00+00:00"),
|
||||
end=datetime.fromisoformat("2026-01-01T00:00:00+00:00"),
|
||||
)
|
||||
ohlcv = loader.load(req)
|
||||
splits = expanding_walk_forward(ohlcv.index, train_ratio=0.5, n_folds=4)
|
||||
engine = BacktestEngine(fees_bp=5.0)
|
||||
|
||||
print(f"\n>>> {label}")
|
||||
print(f" {genome_id} | fit_IS={ev['fitness']:.4f} sharpe_IS={ev['sharpe']:.3f} trades_IS={ev['n_trades']}")
|
||||
print(f" {'fold':<5} {'period':<26} {'trades':>7} {'wins':>5} {'losses':>7} {'win%':>6} {'avg_w':>10} {'avg_l':>10} {'ret':>8} {'maxDD':>7} {'sharpe':>7}")
|
||||
|
||||
sum_ret = 0.0
|
||||
sum_trades = 0
|
||||
sum_wins = 0
|
||||
for s in splits:
|
||||
test_df = ohlcv.loc[s.test_idx]
|
||||
try:
|
||||
signal_fn = compile_strategy(strat)
|
||||
signals = signal_fn(test_df)
|
||||
bt = engine.run(test_df, signals)
|
||||
except Exception as e:
|
||||
print(f" fold {s.fold}: error {e}")
|
||||
continue
|
||||
|
||||
trades = bt.trades
|
||||
n = len(trades)
|
||||
wins = [t.net_pnl for t in trades if t.net_pnl > 0]
|
||||
losses = [t.net_pnl for t in trades if t.net_pnl <= 0]
|
||||
nw, nl = len(wins), len(losses)
|
||||
wr = (nw / n * 100) if n else 0.0
|
||||
aw = (sum(wins) / nw) if nw else 0.0
|
||||
al = (sum(losses) / nl) if nl else 0.0
|
||||
if n > 0:
|
||||
notional = float(test_df["close"].iloc[0])
|
||||
eq = (bt.equity_curve / notional) + 1.0
|
||||
ret = total_return(eq)
|
||||
dd = max_drawdown(eq)
|
||||
sr = sharpe_ratio(bt.returns, periods_per_year=8760)
|
||||
else:
|
||||
ret = dd = sr = 0.0
|
||||
period = f"{str(s.test_idx[0])[:10]}..{str(s.test_idx[-1])[:10]}"
|
||||
print(f" {s.fold:<5} {period:<26} {n:>7} {nw:>5} {nl:>7} {wr:>5.1f}% {aw:>10.1f} {al:>10.1f} {ret:>7.2%} {dd:>6.2%} {sr:>7.3f}")
|
||||
sum_ret += ret
|
||||
sum_trades += n
|
||||
sum_wins += nw
|
||||
overall_wr = (sum_wins / sum_trades * 100) if sum_trades else 0.0
|
||||
print(f" {'='*5} TOTALS: {sum_trades:>7} {sum_wins:>5} {sum_trades-sum_wins:>7} {overall_wr:>5.1f}% cum_ret={sum_ret:+.2%}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
settings = load_settings()
|
||||
repo = Repository(settings.ga_db_path)
|
||||
repo.init_schema()
|
||||
name_to_id: dict[str, str] = {}
|
||||
for w in WINNERS:
|
||||
run_name = w[0]
|
||||
if run_name in name_to_id:
|
||||
continue
|
||||
runs = repo.list_runs()
|
||||
for r in runs:
|
||||
if r["name"] == run_name:
|
||||
name_to_id[run_name] = r["id"]
|
||||
break
|
||||
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
else settings.cerbero_testnet_token.get_secret_value()
|
||||
)
|
||||
cerbero = CerberoClient(
|
||||
base_url=settings.cerbero_base_url,
|
||||
token=token,
|
||||
bot_tag=settings.cerbero_bot_tag,
|
||||
)
|
||||
loader = CerberoOHLCVLoader(client=cerbero, cache_dir=settings.series_dir)
|
||||
|
||||
print(f"{'='*120}")
|
||||
print(f"PER-TRADE COMPARISON — {len(WINNERS)} winner candidates × 4 folds WFA")
|
||||
print(f"{'='*120}")
|
||||
|
||||
for run_name, genome_id, symbol, timeframe, label in WINNERS:
|
||||
run_id = name_to_id.get(run_name)
|
||||
if not run_id:
|
||||
print(f"!!! run not found: {run_name}")
|
||||
continue
|
||||
analyze_genome(run_id, genome_id, symbol, timeframe, label, settings, cerbero, loader)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,112 @@
|
||||
"""2 winner cross-tick: BTC 238e4812 + ETH c04dff7086 su 5m / 15m / 1h.
|
||||
|
||||
Per ogni combinazione strategy × timeframe: backtest year-by-year (2019-2025)
|
||||
con metriche per-anno e totale 7y.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from multi_swarm_core.backtest.engine import BacktestEngine
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm_core.metrics.basic import max_drawdown, sharpe_ratio, total_return
|
||||
from multi_swarm_core.protocol.compiler import compile_strategy
|
||||
from multi_swarm_core.protocol.parser import parse_strategy
|
||||
|
||||
|
||||
WINNERS = [
|
||||
# (label, path, symbol)
|
||||
("BTC NEW (238e4812, native=1h)", "btc_238e4812.json", "BTC-PERPETUAL"),
|
||||
("ETH NEW (c04dff7086, native=5m)", "eth_c04dff7086.json", "ETH-PERPETUAL"),
|
||||
]
|
||||
TIMEFRAMES = ["5m", "15m", "1h"]
|
||||
|
||||
YEARS = [
|
||||
("2019", "2019-01-01T00:00:00+00:00", "2020-01-01T00:00:00+00:00"),
|
||||
("2020", "2020-01-01T00:00:00+00:00", "2021-01-01T00:00:00+00:00"),
|
||||
("2021", "2021-01-01T00:00:00+00:00", "2022-01-01T00:00:00+00:00"),
|
||||
("2022", "2022-01-01T00:00:00+00:00", "2023-01-01T00:00:00+00:00"),
|
||||
("2023", "2023-01-01T00:00:00+00:00", "2024-01-01T00:00:00+00:00"),
|
||||
("2024", "2024-01-01T00:00:00+00:00", "2025-01-01T00:00:00+00:00"),
|
||||
("2025", "2025-01-01T00:00:00+00:00", "2026-01-01T00:00:00+00:00"),
|
||||
]
|
||||
|
||||
|
||||
def evaluate(strat, ohlcv, engine, label, tf) -> None:
|
||||
print(f"\n >>> tick={tf} | {len(ohlcv)} bars")
|
||||
print(f" {'year':<6} {'trades':>7} {'wins':>5} {'losses':>7} {'win%':>6} {'ret':>8} {'maxDD':>7} {'sharpe':>7}")
|
||||
sum_ret = 0.0
|
||||
sum_trades = 0
|
||||
sum_wins = 0
|
||||
for year_label, start, end in YEARS:
|
||||
mask = (ohlcv.index >= datetime.fromisoformat(start)) & (ohlcv.index < datetime.fromisoformat(end))
|
||||
slice_df = ohlcv[mask]
|
||||
if len(slice_df) == 0:
|
||||
continue
|
||||
try:
|
||||
signal_fn = compile_strategy(strat)
|
||||
signals = signal_fn(slice_df)
|
||||
bt = engine.run(slice_df, signals)
|
||||
except Exception as e:
|
||||
print(f" {year_label:<6} ERROR: {e}")
|
||||
continue
|
||||
trades = bt.trades
|
||||
n = len(trades)
|
||||
wins = [t.net_pnl for t in trades if t.net_pnl > 0]
|
||||
losses = [t.net_pnl for t in trades if t.net_pnl <= 0]
|
||||
nw, nl = len(wins), len(losses)
|
||||
wr = (nw / n * 100) if n else 0.0
|
||||
if n > 0:
|
||||
notional = float(slice_df["close"].iloc[0])
|
||||
eq = (bt.equity_curve / notional) + 1.0
|
||||
ret = total_return(eq)
|
||||
dd = max_drawdown(eq)
|
||||
sr = sharpe_ratio(bt.returns, periods_per_year=8760)
|
||||
else:
|
||||
ret = dd = sr = 0.0
|
||||
print(f" {year_label:<6} {n:>7} {nw:>5} {nl:>7} {wr:>5.1f}% {ret:>7.2%} {dd:>6.2%} {sr:>7.3f}")
|
||||
sum_ret += ret
|
||||
sum_trades += n
|
||||
sum_wins += nw
|
||||
overall_wr = (sum_wins / sum_trades * 100) if sum_trades else 0.0
|
||||
print(f" ===== 7y TOT: {sum_trades:>7} {sum_wins:>5} {sum_trades-sum_wins:>7} {overall_wr:>5.1f}% cum_ret={sum_ret:+.2%}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
settings = load_settings()
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
else settings.cerbero_testnet_token.get_secret_value()
|
||||
)
|
||||
cerbero = CerberoClient(
|
||||
base_url=settings.cerbero_base_url,
|
||||
token=token,
|
||||
bot_tag=settings.cerbero_bot_tag,
|
||||
)
|
||||
loader = CerberoOHLCVLoader(client=cerbero, cache_dir=settings.series_dir)
|
||||
engine = BacktestEngine(fees_bp=5.0)
|
||||
strategies_dir = Path("/app/strategies")
|
||||
|
||||
for label, fname, symbol in WINNERS:
|
||||
path = strategies_dir / fname
|
||||
strat = parse_strategy(path.read_text())
|
||||
print(f"\n{'='*100}")
|
||||
print(f">>> {label} — symbol={symbol}")
|
||||
for tf in TIMEFRAMES:
|
||||
try:
|
||||
ohlcv = loader.load(OHLCVRequest(
|
||||
symbol=symbol, timeframe=tf,
|
||||
start=datetime.fromisoformat("2018-09-01T00:00:00+00:00"),
|
||||
end=datetime.fromisoformat("2026-01-01T00:00:00+00:00"),
|
||||
))
|
||||
evaluate(strat, ohlcv, engine, label, tf)
|
||||
except Exception as e:
|
||||
print(f"\n >>> tick={tf} FAILED TO LOAD: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,127 @@
|
||||
"""Replay diagnostico: per ciascuna strategia conta quanti bar avrebbero
|
||||
soddisfatto le condizioni di ciascuna regola sull'ultimo `--days` di storico.
|
||||
|
||||
Ouput tabellare per branch: total_bars, fires, fire_rate, primo/ultimo fire.
|
||||
Esegue anche un backtest grezzo (entry-on-signal, exit-on-flat) per stimare
|
||||
n_trades e total_return realistici nel periodo.
|
||||
|
||||
Esempio:
|
||||
docker compose exec multi-swarm-paper \
|
||||
python /app/scripts/replay_strategies_window.py --days 30
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm_core.protocol.compiler import _eval_node, compile_strategy
|
||||
from multi_swarm_core.protocol.parser import parse_strategy
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("--days", type=int, default=30)
|
||||
p.add_argument("--strategies-dir", default=str(PROJECT_ROOT / "strategies"))
|
||||
return p.parse_args()
|
||||
|
||||
|
||||
def fetch_window(loader: CerberoOHLCVLoader, symbol: str, days: int) -> pd.DataFrame:
|
||||
end = datetime.now(UTC).replace(minute=0, second=0, microsecond=0)
|
||||
start = end - timedelta(days=days)
|
||||
req = OHLCVRequest(
|
||||
symbol=symbol, timeframe="1h", start=start, end=end, exchange="deribit"
|
||||
)
|
||||
return loader._fetch(req) # noqa: SLF001 — bypass cache
|
||||
|
||||
|
||||
def per_branch_fires(strategy_path: Path, ohlcv: pd.DataFrame) -> list[dict]:
|
||||
raw = strategy_path.read_text()
|
||||
parsed = parse_strategy(raw)
|
||||
out = []
|
||||
for idx, rule in enumerate(parsed.rules):
|
||||
cond_series = _eval_node(rule.condition, ohlcv).fillna(False).astype(bool)
|
||||
n = int(cond_series.sum())
|
||||
first = ohlcv.index[cond_series.argmax()] if n > 0 else None
|
||||
# last fire: argmax on reversed
|
||||
last = ohlcv.index[len(cond_series) - 1 - cond_series[::-1].argmax()] if n > 0 else None
|
||||
out.append({
|
||||
"branch_idx": idx,
|
||||
"action": rule.action,
|
||||
"fires": n,
|
||||
"fire_rate_pct": round(100.0 * n / len(ohlcv), 2),
|
||||
"first_fire": first,
|
||||
"last_fire": last,
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def quick_pnl(strategy_path: Path, ohlcv: pd.DataFrame, fees_bp: float = 5.0) -> dict:
|
||||
"""Approx: at each bar evaluate compiled signal series (long/short/flat),
|
||||
apply position to next-bar return, charge fees on changes. No leverage."""
|
||||
raw = strategy_path.read_text()
|
||||
parsed = parse_strategy(raw)
|
||||
sig_fn = compile_strategy(parsed)
|
||||
signals = sig_fn(ohlcv) # series of "long"/"short"/"flat"
|
||||
# map to position: long=+1, short=-1, flat=0
|
||||
pos = signals.map({"long": 1, "short": -1, "flat": 0}).fillna(0).astype(int)
|
||||
rets = ohlcv["close"].pct_change().fillna(0.0)
|
||||
# next-bar execution: position decided at bar t applies to return t+1 -> shift
|
||||
pnl = pos.shift(1).fillna(0) * rets
|
||||
# fees on position changes
|
||||
changes = pos.diff().abs().fillna(0).astype(int)
|
||||
fee_per_change = fees_bp / 10_000.0
|
||||
pnl_after_fees = pnl - changes * fee_per_change
|
||||
cum = (1 + pnl_after_fees).prod() - 1
|
||||
n_trades = int((changes > 0).sum())
|
||||
time_in_market = float((pos != 0).mean())
|
||||
return {
|
||||
"n_trades": n_trades,
|
||||
"total_return_pct": round(100.0 * float(cum), 3),
|
||||
"time_in_market_pct": round(100.0 * time_in_market, 2),
|
||||
}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
settings = load_settings()
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
else settings.cerbero_testnet_token.get_secret_value()
|
||||
)
|
||||
cerbero = CerberoClient(
|
||||
base_url=settings.cerbero_base_url,
|
||||
token=token,
|
||||
bot_tag=settings.cerbero_bot_tag,
|
||||
)
|
||||
loader = CerberoOHLCVLoader(client=cerbero, cache_dir=settings.series_dir)
|
||||
|
||||
strategies_dir = Path(args.strategies_dir)
|
||||
pairs = [
|
||||
("BTC-PERPETUAL", sorted(strategies_dir.glob("btc_*.json"))[0]),
|
||||
("ETH-PERPETUAL", sorted(strategies_dir.glob("eth_*.json"))[0]),
|
||||
]
|
||||
|
||||
for symbol, strat_path in pairs:
|
||||
print(f"\n=== {symbol} strategy={strat_path.name} window={args.days}d ===")
|
||||
ohlcv = fetch_window(loader, symbol, args.days)
|
||||
print(f"bars: {len(ohlcv)} range: {ohlcv.index[0]} -> {ohlcv.index[-1]}")
|
||||
print("\n-- per branch --")
|
||||
for row in per_branch_fires(strat_path, ohlcv):
|
||||
print(json.dumps(row, default=str))
|
||||
print("\n-- quick pnl (next-bar exec, fees=5bp) --")
|
||||
print(json.dumps(quick_pnl(strat_path, ohlcv), default=str))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,238 @@
|
||||
"""Paper-trading runner Phase 3 — forward-test virtuale BTC + ETH.
|
||||
|
||||
Loop infinito (o limitato via --max-ticks) che ogni ``--poll-seconds``:
|
||||
1. fetch OHLCV 1h ultime ~500 barre via Cerbero
|
||||
2. per ogni strategia: compile + esegui ultimo bar
|
||||
3. apply segnale al portfolio multi-asset
|
||||
4. snapshot equity in DB
|
||||
|
||||
I bar 1h chiudono al minuto :00. Il loop riconosce un "nuovo bar chiuso"
|
||||
confrontando l'ultimo timestamp del DataFrame con quello dell'iterazione
|
||||
precedente. Tick consecutivi su stesso bar = hold (no double-trade).
|
||||
|
||||
Esempio:
|
||||
uv run python scripts/run_paper_trading.py \
|
||||
--name phase3-papertrade-001 \
|
||||
--initial-capital 1000 \
|
||||
--max-ticks 336 # 2 settimane * 24 ore
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import importlib.resources
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from strategy_crypto.backend import PaperExecutor, PaperRepository, Portfolio
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
# Mapping timeframe stringa Cerbero -> minuti del bar. Le strategie tradano
|
||||
# sul "bar appena chiuso", quindi end deve essere snappato al boundary del
|
||||
# loro timeframe (NON sempre al top dell'ora) per evitare la regressione in
|
||||
# cui ETH 5m veniva valutato una volta sola ogni 60 min.
|
||||
_TIMEFRAME_MINUTES: dict[str, int] = {
|
||||
"1m": 1,
|
||||
"5m": 5,
|
||||
"15m": 15,
|
||||
"30m": 30,
|
||||
"1h": 60,
|
||||
"4h": 240,
|
||||
"1d": 1440,
|
||||
}
|
||||
|
||||
|
||||
def _align_end_to_timeframe(now: datetime, timeframe: str) -> datetime:
|
||||
"""Snap ``now`` al boundary del bar timeframe (UTC, naive seconds).
|
||||
|
||||
Es.: now=14:37:42, tf="5m" -> 14:35:00
|
||||
now=14:37:42, tf="1h" -> 14:00:00
|
||||
now=14:00:00, tf="1h" -> 14:00:00
|
||||
"""
|
||||
bar_min = _TIMEFRAME_MINUTES[timeframe]
|
||||
aligned = now.replace(second=0, microsecond=0)
|
||||
if bar_min >= 1440:
|
||||
return aligned.replace(hour=0, minute=0)
|
||||
total_min = aligned.hour * 60 + aligned.minute
|
||||
snapped = (total_min // bar_min) * bar_min
|
||||
return aligned.replace(hour=snapped // 60, minute=snapped % 60)
|
||||
|
||||
|
||||
def _default_strategies_dir() -> Path:
|
||||
"""Cartella JSON shippata col package strategy_crypto."""
|
||||
return Path(str(importlib.resources.files("strategy_crypto") / "strategies"))
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AssetConfig:
|
||||
symbol: str # es. "BTC-PERPETUAL"
|
||||
strategy_file: Path
|
||||
exchange: str = "deribit"
|
||||
timeframe: str = "1h"
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
p = argparse.ArgumentParser(description="Paper-trading runner Phase 3")
|
||||
p.add_argument("--name", default="phase3-papertrade-001")
|
||||
p.add_argument("--initial-capital", type=float, default=1000.0)
|
||||
p.add_argument("--fees-bp", type=float, default=5.0)
|
||||
p.add_argument("--poll-seconds", type=int, default=300, help="Polling interval (5min default)")
|
||||
p.add_argument("--max-ticks", type=int, default=0, help="0 = infinito; per smoke test usa 1")
|
||||
p.add_argument("--lookback-bars", type=int, default=500, help="Quante bar fetchare per indicatori")
|
||||
p.add_argument(
|
||||
"--strategies-dir",
|
||||
default=str(_default_strategies_dir()),
|
||||
help="Cartella contenente btc_*.json e eth_*.json (default: package strategy_crypto/strategies)",
|
||||
)
|
||||
return p.parse_args()
|
||||
|
||||
|
||||
def load_assets(strategies_dir: Path) -> list[AssetConfig]:
|
||||
btc_files = sorted(strategies_dir.glob("btc_*.json"))
|
||||
eth_files = sorted(strategies_dir.glob("eth_*.json"))
|
||||
if not btc_files or not eth_files:
|
||||
raise FileNotFoundError(
|
||||
f"Expected btc_*.json and eth_*.json in {strategies_dir}"
|
||||
)
|
||||
# ETH winner c04dff7086 e' tunato su 5m: a 1h la strategia perde (cum_ret -33% 7y).
|
||||
# BTC winner 238e4812 e' tunato su 1h: tick native = paper tick.
|
||||
return [
|
||||
AssetConfig(symbol="BTC-PERPETUAL", strategy_file=btc_files[0], timeframe="1h"),
|
||||
AssetConfig(symbol="ETH-PERPETUAL", strategy_file=eth_files[0], timeframe="5m"),
|
||||
]
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
settings = load_settings()
|
||||
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
else settings.cerbero_testnet_token.get_secret_value()
|
||||
)
|
||||
cerbero = CerberoClient(
|
||||
base_url=settings.cerbero_base_url,
|
||||
token=token,
|
||||
bot_tag=settings.cerbero_bot_tag,
|
||||
)
|
||||
loader = CerberoOHLCVLoader(client=cerbero, cache_dir=settings.series_dir)
|
||||
|
||||
assets = load_assets(Path(args.strategies_dir))
|
||||
executors: list[PaperExecutor] = [
|
||||
PaperExecutor(strategy_json_path=a.strategy_file, symbol=a.symbol) for a in assets
|
||||
]
|
||||
print(f"Loaded {len(assets)} strategies:")
|
||||
for a, ex in zip(assets, executors, strict=True):
|
||||
print(f" {a.symbol}: {a.strategy_file.name} -> {len(ex._strategy.rules)} rules")
|
||||
|
||||
portfolio = Portfolio(
|
||||
initial_capital=args.initial_capital,
|
||||
fees_bp=args.fees_bp,
|
||||
n_sleeves=len(assets),
|
||||
)
|
||||
repo = PaperRepository(settings.strategy_crypto_db_path)
|
||||
repo.init_schema()
|
||||
config = {
|
||||
"assets": [
|
||||
{"symbol": a.symbol, "strategy": a.strategy_file.name, "exchange": a.exchange}
|
||||
for a in assets
|
||||
],
|
||||
"fees_bp": args.fees_bp,
|
||||
"poll_seconds": args.poll_seconds,
|
||||
"lookback_bars": args.lookback_bars,
|
||||
}
|
||||
run_id = repo.create_run(
|
||||
name=args.name, initial_capital=args.initial_capital, config=config
|
||||
)
|
||||
print(f"Paper run started: {run_id} ({args.name})")
|
||||
print(f" initial_capital=${args.initial_capital:.2f}, sleeve=${portfolio.sleeve_capital:.2f}")
|
||||
|
||||
tick_count = 0
|
||||
last_bars_seen: dict[str, datetime] = {}
|
||||
try:
|
||||
while args.max_ticks == 0 or tick_count < args.max_ticks:
|
||||
now = datetime.now(UTC)
|
||||
last_prices: dict[str, float] = {}
|
||||
for asset, executor in zip(assets, executors, strict=True):
|
||||
# fetch OHLCV most recent lookback bars: end snappato al timeframe
|
||||
# dell'asset, non sempre all'ora (altrimenti ETH 5m veniva valutato
|
||||
# solo ogni 60 min, regressione vs backtest tunato 5m).
|
||||
bar_min = _TIMEFRAME_MINUTES[asset.timeframe]
|
||||
end = _align_end_to_timeframe(now, asset.timeframe)
|
||||
start = end - timedelta(minutes=bar_min * (args.lookback_bars + 1))
|
||||
req = OHLCVRequest(
|
||||
symbol=asset.symbol,
|
||||
timeframe=asset.timeframe,
|
||||
start=start,
|
||||
end=end,
|
||||
exchange=asset.exchange,
|
||||
)
|
||||
# bypass cache for live data
|
||||
try:
|
||||
ohlcv = loader._fetch(req) # noqa: SLF001
|
||||
except Exception as e: # noqa: BLE001
|
||||
print(f"[{now.isoformat()}] {asset.symbol} fetch FAIL: {e}")
|
||||
continue
|
||||
if len(ohlcv) < 10:
|
||||
print(f"[{now.isoformat()}] {asset.symbol} too few bars ({len(ohlcv)})")
|
||||
continue
|
||||
|
||||
bar_ts = ohlcv.index[-1]
|
||||
last_bar_dt = bar_ts.to_pydatetime() if hasattr(bar_ts, "to_pydatetime") else bar_ts
|
||||
# skip se barra gia' processata in questo tick
|
||||
if last_bars_seen.get(asset.symbol) == last_bar_dt:
|
||||
last_prices[asset.symbol] = float(ohlcv["close"].iloc[-1])
|
||||
continue
|
||||
last_bars_seen[asset.symbol] = last_bar_dt
|
||||
|
||||
result = executor.execute_tick(portfolio, ohlcv, now)
|
||||
repo.save_tick(run_id, result)
|
||||
last_prices[asset.symbol] = result.close_price
|
||||
if result.action_taken != "hold":
|
||||
pnl_str = (
|
||||
f"pnl=${result.trade.net_pnl:+.2f}" if result.trade else ""
|
||||
)
|
||||
print(
|
||||
f"[{now.isoformat()}] {asset.symbol} bar={last_bar_dt} "
|
||||
f"close={result.close_price:.2f} signal={result.signal.value} "
|
||||
f"action={result.action_taken} {pnl_str}"
|
||||
)
|
||||
|
||||
repo.sync_open_positions(run_id, portfolio)
|
||||
eq, pos_val = portfolio.equity(last_prices)
|
||||
repo.save_equity_snapshot(run_id, now, eq, portfolio.cash, pos_val)
|
||||
|
||||
tick_count += 1
|
||||
print(
|
||||
f"[{now.isoformat()}] tick={tick_count} "
|
||||
f"equity=${eq:.2f} cash=${portfolio.cash:.2f} pos_val=${pos_val:.2f} "
|
||||
f"open={list(portfolio.positions.keys())}"
|
||||
)
|
||||
|
||||
if args.max_ticks > 0 and tick_count >= args.max_ticks:
|
||||
break
|
||||
time.sleep(args.poll_seconds)
|
||||
|
||||
repo.stop_run(run_id, status="completed")
|
||||
except KeyboardInterrupt:
|
||||
print("\nInterrupted by user")
|
||||
repo.stop_run(run_id, status="interrupted")
|
||||
except Exception as e: # noqa: BLE001
|
||||
print(f"Run failed: {e}")
|
||||
repo.stop_run(run_id, status="failed")
|
||||
raise
|
||||
|
||||
print(f"Paper run {run_id} stopped after {tick_count} ticks")
|
||||
print(f"Final equity: ${portfolio.equity({})[0]:.2f}")
|
||||
print(f"Trades closed: {len(portfolio.closed_trades)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+160
-9
@@ -1,14 +1,46 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import importlib.resources
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from multi_swarm.cerbero.client import CerberoClient
|
||||
from multi_swarm.config import load_settings
|
||||
from multi_swarm.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm.genome.hypothesis import ModelTier
|
||||
from multi_swarm.llm.client import LLMClient
|
||||
from multi_swarm.orchestrator.run import RunConfig, run_phase1
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm_core.genome.hypothesis import ModelTier
|
||||
from multi_swarm_core.genome.prompt_library import PromptLibrary
|
||||
from multi_swarm_core.llm.client import LLMClient
|
||||
from multi_swarm_core.orchestrator.run import RunConfig, run_phase1
|
||||
|
||||
|
||||
def _default_prompt_library_path() -> Path:
|
||||
"""Default: prompts.json shippato col package strategy_crypto."""
|
||||
return Path(str(importlib.resources.files("strategy_crypto") / "prompts.json"))
|
||||
|
||||
|
||||
# Default v2 hard-kill list: oltre ai degenerate originali, fees_eat_alpha e
|
||||
# negative_net_pnl sono deal-breaker non recuperabili via soft penalty (vedi
|
||||
# 7y-overfit incident 2026-05-16: elite IS Sharpe 1.93 -> net -5% su 7y per fees).
|
||||
_DEFAULT_V2_HARD_KILL: tuple[str, ...] = (
|
||||
"no_trades",
|
||||
"degenerate",
|
||||
"undertrading",
|
||||
"fees_eat_alpha",
|
||||
"negative_net_pnl",
|
||||
)
|
||||
|
||||
|
||||
def _resolve_hard_kill(args) -> tuple[str, ...] | None:
|
||||
"""Resolve la lista hard-kill da CLI args.
|
||||
|
||||
Priority: ``--fitness-hard-kill`` esplicito > default v2 > ``None`` (v1).
|
||||
"""
|
||||
if args.fitness_hard_kill:
|
||||
return tuple(s.strip() for s in args.fitness_hard_kill.split(",") if s.strip())
|
||||
if args.fitness_v2:
|
||||
return _DEFAULT_V2_HARD_KILL
|
||||
return None
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
@@ -27,10 +59,110 @@ def parse_args() -> argparse.Namespace:
|
||||
)
|
||||
p.add_argument("--symbol", default="BTC-PERPETUAL")
|
||||
p.add_argument("--timeframe", default="1h")
|
||||
p.add_argument("--start", default="2024-01-01T00:00:00+00:00")
|
||||
# Default esteso a 7.3 anni: copre bear 2018-19, halving 2020, COVID,
|
||||
# ATH 2021, winter 2022, ETF rally 2024, regime corrente. Una finestra
|
||||
# corta lascia il GA libero di overfit a un singolo regime.
|
||||
p.add_argument("--start", default="2018-09-01T00:00:00+00:00")
|
||||
p.add_argument("--end", default="2026-01-01T00:00:00+00:00")
|
||||
p.add_argument("--fees-bp", type=float, default=5.0)
|
||||
p.add_argument("--n-trials-dsr", type=int, default=50)
|
||||
p.add_argument(
|
||||
"--prompt-mutation-weight",
|
||||
type=float,
|
||||
default=0.0,
|
||||
help="Phase 2.5: probabilità (0-1) che la mutazione invochi LLM mutator tier B",
|
||||
)
|
||||
p.add_argument(
|
||||
"--fees-eat-alpha-threshold",
|
||||
type=float,
|
||||
default=0.5,
|
||||
help="Adversarial gate: kill se fees/gross_pnl > soglia (default 0.5, ablation 0.7)",
|
||||
)
|
||||
p.add_argument(
|
||||
"--flat-too-long-threshold",
|
||||
type=float,
|
||||
default=0.95,
|
||||
help="Adversarial gate: kill se signal flat > soglia delle bar (default 0.95, ablation 0.98)",
|
||||
)
|
||||
p.add_argument(
|
||||
"--undertrading-threshold",
|
||||
type=int,
|
||||
default=10,
|
||||
help="Adversarial: kill se n_trades < soglia (default 10, bump per filtrare lucky-shot)",
|
||||
)
|
||||
p.add_argument(
|
||||
"--fitness-v2",
|
||||
action="store_true",
|
||||
help=(
|
||||
"Attiva fitness v2: hard-kill su {no_trades, degenerate, undertrading, "
|
||||
"fees_eat_alpha, negative_net_pnl}; gli altri HIGH applicano soft penalty "
|
||||
"multiplicativa. Versione hardened post 7y-overfit incident: fees + "
|
||||
"net negativo non sono recuperabili."
|
||||
),
|
||||
)
|
||||
p.add_argument(
|
||||
"--fitness-soft-penalty",
|
||||
type=float,
|
||||
default=0.4,
|
||||
help="v2: fattore soft penalty per HIGH non-hard (default 0.4 → 1 HIGH → 0.71x)",
|
||||
)
|
||||
p.add_argument(
|
||||
"--fitness-hard-kill",
|
||||
type=str,
|
||||
default=None,
|
||||
help=(
|
||||
"Override comma-separated della lista di finding name che azzerano la "
|
||||
"fitness in modalita' v2. Es: 'no_trades,degenerate'. Default v2: "
|
||||
"no_trades,degenerate,undertrading,fees_eat_alpha,negative_net_pnl."
|
||||
),
|
||||
)
|
||||
p.add_argument(
|
||||
"--wfa-train-split",
|
||||
type=float,
|
||||
default=None,
|
||||
help="Walk-forward: frazione bar usate per training (es. 0.7 = primi 70%% in-sample, ultimi 30%% OOS)",
|
||||
)
|
||||
p.add_argument(
|
||||
"--wfa-top-k",
|
||||
type=int,
|
||||
default=5,
|
||||
help="Walk-forward: quanti top genomi rivalutare OOS (default 5)",
|
||||
)
|
||||
p.add_argument(
|
||||
"--eval-oos-during-loop",
|
||||
action="store_true",
|
||||
help=(
|
||||
"Multi-objective: eval ogni genome anche su test_ohlcv durante "
|
||||
"il loop e usa combined = alpha*IS + (1-alpha)*OOS per selection. "
|
||||
"Richiede --wfa-train-split. 2x costo backtest engine."
|
||||
),
|
||||
)
|
||||
p.add_argument(
|
||||
"--fitness-combined-alpha",
|
||||
type=float,
|
||||
default=0.5,
|
||||
help="Multi-objective: peso IS (1-alpha = OOS). 1.0=solo IS, 0.5=bilanciato, 0.0=solo OOS",
|
||||
)
|
||||
p.add_argument(
|
||||
"--prompt-library",
|
||||
type=Path,
|
||||
default=None,
|
||||
help=(
|
||||
"Path al file JSON con stili cognitivi + direttive system_prompt. "
|
||||
"Default: strategy_crypto/prompts.json shippato col package. "
|
||||
"Schema: {styles: {<name>: {directive: <testo>}}}"
|
||||
),
|
||||
)
|
||||
p.add_argument(
|
||||
"--llm-concurrency",
|
||||
type=int,
|
||||
default=1,
|
||||
help=(
|
||||
"Numero di propose() LLM concorrenti per generazione (default 1 = "
|
||||
"serial). OpusAgent processa in coda FIFO; concurrency > 1 accoda "
|
||||
"piu' richieste in parallelo."
|
||||
),
|
||||
)
|
||||
return p.parse_args()
|
||||
|
||||
|
||||
@@ -38,6 +170,13 @@ def main() -> None:
|
||||
args = parse_args()
|
||||
settings = load_settings()
|
||||
|
||||
prompt_lib_path = args.prompt_library or _default_prompt_library_path()
|
||||
prompt_library = PromptLibrary.from_json(prompt_lib_path)
|
||||
print(
|
||||
f"PromptLibrary loaded from {prompt_lib_path}: "
|
||||
f"{len(prompt_library.styles)} stili ({', '.join(prompt_library.cognitive_styles)})"
|
||||
)
|
||||
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
@@ -61,13 +200,13 @@ def main() -> None:
|
||||
print(f"OHLCV loaded: {len(ohlcv)} bars from {ohlcv.index[0]} to {ohlcv.index[-1]}")
|
||||
|
||||
llm = LLMClient(
|
||||
openrouter_api_key=settings.openrouter_api_key.get_secret_value(),
|
||||
opus_agent_api_key=settings.opus_agent_api_key.get_secret_value(),
|
||||
opus_agent_base_url=settings.opus_agent_base_url,
|
||||
model_tier_s=settings.llm_model_tier_s,
|
||||
model_tier_a=settings.llm_model_tier_a,
|
||||
model_tier_b=settings.llm_model_tier_b,
|
||||
model_tier_c=settings.llm_model_tier_c,
|
||||
model_tier_d=settings.llm_model_tier_d,
|
||||
openrouter_base_url=settings.openrouter_base_url,
|
||||
)
|
||||
|
||||
cfg = RunConfig(
|
||||
@@ -84,6 +223,18 @@ def main() -> None:
|
||||
fees_bp=args.fees_bp,
|
||||
n_trials_dsr=args.n_trials_dsr,
|
||||
db_path=settings.db_path,
|
||||
prompt_mutation_weight=args.prompt_mutation_weight,
|
||||
fees_eat_alpha_threshold=args.fees_eat_alpha_threshold,
|
||||
flat_too_long_threshold=args.flat_too_long_threshold,
|
||||
undertrading_threshold=args.undertrading_threshold,
|
||||
fitness_hard_kill_findings=_resolve_hard_kill(args),
|
||||
fitness_adversarial_soft_penalty=args.fitness_soft_penalty,
|
||||
wfa_train_split=args.wfa_train_split,
|
||||
wfa_top_k=args.wfa_top_k,
|
||||
eval_oos_during_loop=args.eval_oos_during_loop,
|
||||
fitness_combined_alpha=args.fitness_combined_alpha,
|
||||
prompt_library=prompt_library,
|
||||
llm_concurrency=args.llm_concurrency,
|
||||
)
|
||||
|
||||
run_id = run_phase1(cfg, ohlcv=ohlcv, llm=llm)
|
||||
|
||||
@@ -0,0 +1,475 @@
|
||||
"""Smoke test del GA per strategy_pythagoras.
|
||||
|
||||
Esegue 2 run di Phase 1 (BTC 5m + ETH 5m), poi cross-rank i genomi
|
||||
comuni applicando il bonus di asset-invariance (corr_signal sui pattern
|
||||
di entry entro +/-36 barre = +/-3h su 5m TF, vedi paper Pythagoras p.43).
|
||||
|
||||
Configurazione (per spec §4):
|
||||
- Population 20, 5 generations
|
||||
- Asset: BTC-PERPETUAL 5m + ETH-PERPETUAL 5m (Cerbero deribit)
|
||||
- Train window: 2024-07-01 -> 2024-12-31
|
||||
- Test window: 2025-01-01 -> 2025-01-31 (caricato come coda dello stesso
|
||||
range; non usato dal GA ma necessario per dataset continuo se in futuro
|
||||
si attiva WFA)
|
||||
- Stili cognitivi: 7 da strategy_pythagoras/prompts.json
|
||||
- Indicatori Pythagoras: candle_pattern, pythagorean_ratio, fractal_mirror
|
||||
(registrati nel compiler tramite import side-effect di strategy_pythagoras.indicators)
|
||||
- Fitness post-processing cross-asset: apply_invariance_bonus
|
||||
- Output: top 50 winners persisted in state/strategy_pythagoras.db
|
||||
(tabella pythagoras_winners)
|
||||
|
||||
Adattamento all'API reale di run_phase1 (Task 4.1 findings + verifica diretta):
|
||||
|
||||
- ``run_phase1(cfg: RunConfig, ohlcv: pd.DataFrame, llm: LLMClient) -> str``
|
||||
ritorna un ``run_id``. Non c'e' un fitness hook esterno: il GA loop
|
||||
invoca ``compute_fitness`` inline e persiste via
|
||||
``repo.save_evaluation``. Per il bonus invariance dobbiamo:
|
||||
1. lanciare due ``run_phase1`` indipendenti, uno per asset;
|
||||
2. caricare le evaluations via ``repo.list_evaluations(run_id)``;
|
||||
3. ricompilare la strategia (``_try_parse`` + ``compile_strategy``) sui
|
||||
segnali di ciascun OHLCV per estrarre gli entry index;
|
||||
4. calcolare ``corr_signal`` sugli entry binari (Series int-indexed)
|
||||
e applicare ``apply_invariance_bonus``.
|
||||
|
||||
- Le serie OHLCV NON sono shippate in repo come ``src/strategy_crypto/series/``:
|
||||
il default loader Cerbero le cachea in ``./series/{cache_key}.parquet``
|
||||
(cache key = sha1 di ``exchange|symbol|timeframe|start|end``). Riusiamo
|
||||
quel meccanismo: caricamento via ``CerberoOHLCVLoader``, identico a
|
||||
``scripts/run_phase1.py``.
|
||||
|
||||
Shape effettivo del dict ritornato da ``repo.list_evaluations(run_id)``
|
||||
(vedi ``persistence/repository.py:213`` e schema in ``schema.py``):
|
||||
|
||||
{
|
||||
'run_id', 'genome_id', 'fitness', 'dsr', 'dsr_pvalue', 'sharpe',
|
||||
'max_dd', 'total_return', 'n_trades', 'parse_error', 'raw_text',
|
||||
'eval_ts', 'fitness_oos', 'sharpe_oos', 'return_oos',
|
||||
'max_dd_oos', 'n_trades_oos'
|
||||
}
|
||||
|
||||
Nota: ``cognitive_style`` e ``generation`` NON sono nelle evaluations;
|
||||
vanno presi via ``repo.list_genomes(run_id)`` (payload_json del genoma).
|
||||
``raw_text`` contiene il completion grezzo del LLM, da cui si estrae
|
||||
nuovamente lo ``Strategy`` AST via ``_try_parse``.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
# Side-effect import: registra candle_pattern, pythagorean_ratio, fractal_mirror
|
||||
# in compiler.INDICATOR_FNS prima che il GA inizi a compilare strategie.
|
||||
# (Il compiler in protocol/compiler.py importa gia' i 3 simboli dal package
|
||||
# strategy_pythagoras.indicators, ma facciamo l'import esplicito qui per
|
||||
# rendere la dipendenza chiara e indipendente dall'ordine di import.)
|
||||
import strategy_pythagoras.indicators # noqa: F401
|
||||
from multi_swarm_core.agents.hypothesis import _try_parse
|
||||
from multi_swarm_core.backtest.orders import Side
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm_core.genome.hypothesis import ModelTier
|
||||
from multi_swarm_core.genome.prompt_library import PromptLibrary
|
||||
from multi_swarm_core.llm.client import LLMClient
|
||||
from multi_swarm_core.orchestrator.run import RunConfig, run_phase1
|
||||
from multi_swarm_core.persistence.repository import Repository
|
||||
from multi_swarm_core.protocol.compiler import compile_strategy
|
||||
from strategy_pythagoras.fitness_invariance import (
|
||||
apply_invariance_bonus,
|
||||
corr_signal,
|
||||
)
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DB_PATH = Path(
|
||||
os.getenv("STRATEGY_PYTHAGORAS_DB_PATH", str(ROOT / "state" / "strategy_pythagoras.db"))
|
||||
)
|
||||
PROMPTS_PATH = ROOT / "src" / "strategy_pythagoras" / "strategy_pythagoras" / "prompts.json"
|
||||
RUN_NAME = os.getenv("PYTHAGORAS_SMOKE_RUN_NAME", "pythagoras-smoke-001")
|
||||
|
||||
# GA configuration (smoke per spec §4)
|
||||
POPULATION = 20
|
||||
GENERATIONS = 5
|
||||
|
||||
# Data window
|
||||
TRAIN_START = datetime.fromisoformat("2024-07-01T00:00:00+00:00")
|
||||
TRAIN_END = datetime.fromisoformat("2024-12-31T23:55:00+00:00")
|
||||
# Carichiamo anche gennaio 2025 come coda (per usi futuri: WFA OOS).
|
||||
# Il GA loop in questa fase usa l'intero range; e' compito di un eventuale
|
||||
# wfa_train_split (non attivato qui per coerenza con spec §4 smoke).
|
||||
TEST_END = datetime.fromisoformat("2025-01-31T23:55:00+00:00")
|
||||
|
||||
ASSETS: list[tuple[str, str]] = [
|
||||
("BTC-PERPETUAL", "btc"),
|
||||
("ETH-PERPETUAL", "eth"),
|
||||
]
|
||||
TIMEFRAME = "5m"
|
||||
EXCHANGE = "deribit"
|
||||
TOP_K_PERSIST = 50
|
||||
|
||||
logger = logging.getLogger(RUN_NAME)
|
||||
|
||||
|
||||
def init_winners_table(con: sqlite3.Connection) -> None:
|
||||
"""Crea ``pythagoras_winners`` se non esiste (idempotente)."""
|
||||
con.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS pythagoras_winners (
|
||||
genome_id TEXT PRIMARY KEY,
|
||||
cognitive_style TEXT,
|
||||
fitness REAL,
|
||||
sharpe_btc REAL,
|
||||
sharpe_eth REAL,
|
||||
invariance_score REAL,
|
||||
rules_json TEXT,
|
||||
generation INTEGER,
|
||||
run_name TEXT
|
||||
)
|
||||
"""
|
||||
)
|
||||
con.commit()
|
||||
|
||||
|
||||
def _load_ohlcv(loader: CerberoOHLCVLoader, symbol: str) -> pd.DataFrame:
|
||||
"""Carica la finestra ``TRAIN_START -> TEST_END`` per ``symbol`` su 5m."""
|
||||
req = OHLCVRequest(
|
||||
symbol=symbol,
|
||||
timeframe=TIMEFRAME,
|
||||
start=TRAIN_START,
|
||||
end=TEST_END,
|
||||
exchange=EXCHANGE,
|
||||
)
|
||||
ohlcv = loader.load(req)
|
||||
logger.info(
|
||||
"OHLCV loaded for %s: %d bars (%s -> %s)",
|
||||
symbol, len(ohlcv),
|
||||
ohlcv.index[0] if len(ohlcv) else "n/a",
|
||||
ohlcv.index[-1] if len(ohlcv) else "n/a",
|
||||
)
|
||||
return ohlcv
|
||||
|
||||
|
||||
def _build_run_config(
|
||||
run_name: str, symbol: str, prompt_library: PromptLibrary, db_path: Path,
|
||||
) -> RunConfig:
|
||||
"""Costruisce il ``RunConfig`` per un singolo asset.
|
||||
|
||||
Usa lo stesso GA-core DB del progetto (``settings.ga_db_path`` se override
|
||||
non passato): vi vengono scritte ``runs``, ``generations``, ``genomes``,
|
||||
``evaluations`` per la run.
|
||||
"""
|
||||
return RunConfig(
|
||||
run_name=run_name,
|
||||
population_size=POPULATION,
|
||||
n_generations=GENERATIONS,
|
||||
elite_k=2,
|
||||
tournament_k=3,
|
||||
p_crossover=0.5,
|
||||
seed=42,
|
||||
model_tier=ModelTier.C,
|
||||
symbol=symbol,
|
||||
timeframe=TIMEFRAME,
|
||||
fees_bp=5.0,
|
||||
n_trials_dsr=50,
|
||||
db_path=db_path,
|
||||
prompt_library=prompt_library,
|
||||
# Smoke: niente WFA, niente eval OOS in loop, niente prompt mutation LLM.
|
||||
# I parametri restano sui default sicuri di RunConfig.
|
||||
)
|
||||
|
||||
|
||||
def run_ga_for_asset(
|
||||
asset_label: str,
|
||||
symbol: str,
|
||||
ohlcv: pd.DataFrame,
|
||||
prompt_library: PromptLibrary,
|
||||
llm: LLMClient,
|
||||
ga_db_path: Path,
|
||||
) -> tuple[str, Repository]:
|
||||
"""Lancia ``run_phase1`` per un asset.
|
||||
|
||||
Ritorna ``(run_id, repo)`` per il caller, che usera' ``repo`` per
|
||||
estrarre evaluations + genomes a fine del run.
|
||||
"""
|
||||
run_name = f"{RUN_NAME}-{asset_label}"
|
||||
cfg = _build_run_config(run_name, symbol, prompt_library, ga_db_path)
|
||||
logger.info("Starting GA run '%s' on %s (%d bars)", run_name, symbol, len(ohlcv))
|
||||
run_id = run_phase1(cfg, ohlcv=ohlcv, llm=llm)
|
||||
logger.info("Run '%s' completed: run_id=%s", run_name, run_id)
|
||||
repo = Repository(ga_db_path)
|
||||
return run_id, repo
|
||||
|
||||
|
||||
def _entries_series_from_eval(
|
||||
eval_row: dict[str, Any], ohlcv: pd.DataFrame,
|
||||
) -> pd.Series | None:
|
||||
"""Ricostruisce gli entries binari (Side.LONG/SHORT -> 1, altrimenti 0)
|
||||
a partire dal ``raw_text`` salvato nell'eval row.
|
||||
|
||||
Ritorna ``None`` se il raw_text non e' parsabile (caso parse_error).
|
||||
L'index della Series ritornata e' INTERO posizionale (0..N-1) come
|
||||
richiesto da ``corr_signal`` (vedi tests in
|
||||
``strategy_pythagoras/tests/test_fitness_invariance.py``).
|
||||
"""
|
||||
raw = eval_row.get("raw_text")
|
||||
if not raw:
|
||||
return None
|
||||
strategy, parse_err = _try_parse(raw)
|
||||
if strategy is None:
|
||||
logger.debug(
|
||||
"skip genome %s: parse error '%s'",
|
||||
eval_row.get("genome_id"), parse_err,
|
||||
)
|
||||
return None
|
||||
try:
|
||||
signal_fn = compile_strategy(strategy)
|
||||
signals = signal_fn(ohlcv)
|
||||
except Exception as exc:
|
||||
logger.debug(
|
||||
"skip genome %s: compile/exec error: %s",
|
||||
eval_row.get("genome_id"), exc,
|
||||
)
|
||||
return None
|
||||
# 1 dove il signal e' LONG o SHORT (entry attiva), 0 altrove.
|
||||
is_entry = signals.isin([Side.LONG, Side.SHORT]).fillna(False).astype(int)
|
||||
# Riassegna integer index per il match in corr_signal (che somma delta
|
||||
# interi all'index e fa il test ``ti + delta in b_set``).
|
||||
return pd.Series(is_entry.values, index=range(len(is_entry)), dtype="int64")
|
||||
|
||||
|
||||
def _collect_evaluations(
|
||||
repo: Repository, run_id: str, ohlcv: pd.DataFrame,
|
||||
) -> dict[str, dict[str, Any]]:
|
||||
"""Carica evaluations + genomes per ``run_id`` e li unisce per genome_id.
|
||||
|
||||
Returns: dict ``{genome_id: row}`` dove ``row`` contiene i campi
|
||||
dell'eval + ``cognitive_style``, ``generation``, ``strategy_json``
|
||||
(dict del genoma serializzato) e ``entries`` (pd.Series int-indexed).
|
||||
"""
|
||||
evals = repo.list_evaluations(run_id)
|
||||
genomes = repo.list_genomes(run_id)
|
||||
genome_by_id: dict[str, dict[str, Any]] = {}
|
||||
for grow in genomes:
|
||||
try:
|
||||
payload = json.loads(grow["payload_json"])
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
payload = {}
|
||||
genome_by_id[grow["id"]] = payload
|
||||
|
||||
out: dict[str, dict[str, Any]] = {}
|
||||
for ev in evals:
|
||||
gid = ev["genome_id"]
|
||||
payload = genome_by_id.get(gid, {})
|
||||
row = dict(ev)
|
||||
row["cognitive_style"] = payload.get("cognitive_style", "")
|
||||
row["generation"] = int(payload.get("generation", 0))
|
||||
# ``raw_text`` e' il completion grezzo; lo ri-parsiamo in
|
||||
# _entries_series_from_eval. Salviamo la rappresentazione canonica
|
||||
# ``strategy_json`` per persistenza (best-effort: se il parse fallisce
|
||||
# salviamo il raw_text come fallback).
|
||||
strategy, _err = _try_parse(row.get("raw_text") or "")
|
||||
if strategy is not None:
|
||||
# Strategy non e' direttamente JSON-serializable: serializziamo
|
||||
# la struttura nominale tramite dataclasses.asdict-like fallback.
|
||||
try:
|
||||
row["strategy_json"] = _strategy_to_jsonable(strategy)
|
||||
except Exception:
|
||||
row["strategy_json"] = {"raw_text": row.get("raw_text", "")}
|
||||
else:
|
||||
row["strategy_json"] = {"raw_text": row.get("raw_text", "")}
|
||||
row["entries"] = _entries_series_from_eval(ev, ohlcv)
|
||||
out[gid] = row
|
||||
return out
|
||||
|
||||
|
||||
def _strategy_to_jsonable(strategy: Any) -> dict[str, Any]:
|
||||
"""Serializza un ``Strategy`` AST in dict JSON-friendly.
|
||||
|
||||
Strategy/Rule/Node sono dataclass: usiamo ``dataclasses.asdict`` quando
|
||||
possibile, con fallback a ``str(strategy)`` se la struttura contiene
|
||||
membri non-serializzabili (es. enum non-Str).
|
||||
"""
|
||||
import dataclasses
|
||||
if dataclasses.is_dataclass(strategy):
|
||||
try:
|
||||
return dataclasses.asdict(strategy)
|
||||
except TypeError:
|
||||
pass
|
||||
return {"repr": repr(strategy)}
|
||||
|
||||
|
||||
def compute_invariance_for_pair(
|
||||
btc_evals: dict[str, dict[str, Any]],
|
||||
eth_evals: dict[str, dict[str, Any]],
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Per ogni genome_id presente in entrambi i run, calcola invariance + bonus.
|
||||
|
||||
Lo stesso ``genome_id`` puo' apparire in entrambi i run perche' l'id e'
|
||||
deterministico (sha1 di system_prompt+feature_access+temperature+...) e
|
||||
il seed del GA e' fisso: il founder set e i mutanti hanno alta probabilita'
|
||||
di collisione cross-asset. Quando il genoma compare in entrambi, le
|
||||
metriche ``sharpe`` IS sono comparabili e ha senso valutare l'invarianza.
|
||||
"""
|
||||
out: list[dict[str, Any]] = []
|
||||
common_ids = set(btc_evals) & set(eth_evals)
|
||||
logger.info(
|
||||
"Common genomes BTC ∩ ETH: %d (BTC: %d, ETH: %d)",
|
||||
len(common_ids), len(btc_evals), len(eth_evals),
|
||||
)
|
||||
for gid in common_ids:
|
||||
b = btc_evals[gid]
|
||||
e = eth_evals[gid]
|
||||
entries_btc: pd.Series | None = b.get("entries")
|
||||
entries_eth: pd.Series | None = e.get("entries")
|
||||
if entries_btc is None or entries_eth is None:
|
||||
inv = 0.0
|
||||
elif len(entries_btc) == 0 or len(entries_eth) == 0:
|
||||
inv = 0.0
|
||||
else:
|
||||
try:
|
||||
# Allineiamo a lunghezza minima: i due asset possono avere
|
||||
# un numero di bars leggermente diverso (gap nel feed Cerbero).
|
||||
# corr_signal lavora solo sugli index 1 -> il troncamento non
|
||||
# introduce bias asimmetrici.
|
||||
min_len = min(len(entries_btc), len(entries_eth))
|
||||
inv = corr_signal(
|
||||
entries_btc.iloc[:min_len].reset_index(drop=True),
|
||||
entries_eth.iloc[:min_len].reset_index(drop=True),
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning("corr_signal failed for %s: %s", gid, exc)
|
||||
inv = 0.0
|
||||
sharpe_btc = float(b.get("sharpe") or 0.0)
|
||||
sharpe_eth = float(e.get("sharpe") or 0.0)
|
||||
mean_sharpe = 0.5 * (sharpe_btc + sharpe_eth)
|
||||
boosted = apply_invariance_bonus(mean_sharpe, inv)
|
||||
out.append({
|
||||
"genome_id": gid,
|
||||
"cognitive_style": b.get("cognitive_style") or e.get("cognitive_style", ""),
|
||||
"fitness": float(boosted),
|
||||
"sharpe_btc": sharpe_btc,
|
||||
"sharpe_eth": sharpe_eth,
|
||||
"invariance_score": float(inv),
|
||||
"rules_json": json.dumps(b.get("strategy_json") or {}, default=str),
|
||||
"generation": int(b.get("generation", 0)),
|
||||
"run_name": RUN_NAME,
|
||||
})
|
||||
return sorted(out, key=lambda r: r["fitness"], reverse=True)
|
||||
|
||||
|
||||
def persist_winners(con: sqlite3.Connection, winners: list[dict[str, Any]]) -> None:
|
||||
if not winners:
|
||||
logger.warning("No winners to persist")
|
||||
return
|
||||
con.executemany(
|
||||
"""
|
||||
INSERT OR REPLACE INTO pythagoras_winners
|
||||
(genome_id, cognitive_style, fitness, sharpe_btc, sharpe_eth,
|
||||
invariance_score, rules_json, generation, run_name)
|
||||
VALUES (:genome_id, :cognitive_style, :fitness, :sharpe_btc, :sharpe_eth,
|
||||
:invariance_score, :rules_json, :generation, :run_name)
|
||||
""",
|
||||
winners,
|
||||
)
|
||||
con.commit()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||
)
|
||||
settings = load_settings()
|
||||
|
||||
# Prompt library Pythagoras (NON quello di strategy_crypto).
|
||||
if not PROMPTS_PATH.exists():
|
||||
raise FileNotFoundError(f"Prompts file not found: {PROMPTS_PATH}")
|
||||
prompt_library = PromptLibrary.from_json(PROMPTS_PATH)
|
||||
logger.info(
|
||||
"PromptLibrary loaded from %s: %d styles (%s)",
|
||||
PROMPTS_PATH, len(prompt_library.styles),
|
||||
", ".join(prompt_library.cognitive_styles),
|
||||
)
|
||||
|
||||
# Cerbero client + OHLCV loader (riusa la cache parquet in ./series).
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
else settings.cerbero_testnet_token.get_secret_value()
|
||||
)
|
||||
cerbero = CerberoClient(
|
||||
base_url=settings.cerbero_base_url,
|
||||
token=token,
|
||||
bot_tag=settings.cerbero_bot_tag,
|
||||
)
|
||||
loader = CerberoOHLCVLoader(client=cerbero, cache_dir=settings.series_dir)
|
||||
|
||||
llm = LLMClient(
|
||||
opus_agent_api_key=settings.opus_agent_api_key.get_secret_value(),
|
||||
opus_agent_base_url=settings.opus_agent_base_url,
|
||||
model_tier_s=settings.llm_model_tier_s,
|
||||
model_tier_a=settings.llm_model_tier_a,
|
||||
model_tier_b=settings.llm_model_tier_b,
|
||||
model_tier_c=settings.llm_model_tier_c,
|
||||
model_tier_d=settings.llm_model_tier_d,
|
||||
)
|
||||
|
||||
# Setup DB winners (separato dal GA core DB).
|
||||
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
con = sqlite3.connect(DB_PATH)
|
||||
try:
|
||||
init_winners_table(con)
|
||||
logger.info("Winners DB initialized at %s", DB_PATH)
|
||||
|
||||
# Carica OHLCV per entrambi gli asset PRIMA dei run GA, cosi' se la
|
||||
# rete o Cerbero sono giu' falliamo subito senza sprecare chiamate LLM.
|
||||
ohlcv_by_asset: dict[str, pd.DataFrame] = {}
|
||||
for symbol, label in ASSETS:
|
||||
ohlcv_by_asset[label] = _load_ohlcv(loader, symbol)
|
||||
|
||||
# Run GA per asset. Usa il GA-core DB definito in settings; ogni run
|
||||
# crea un proprio run_id e set di evaluations isolato.
|
||||
evals_by_asset: dict[str, dict[str, dict[str, Any]]] = {}
|
||||
for symbol, label in ASSETS:
|
||||
run_id, repo = run_ga_for_asset(
|
||||
asset_label=label,
|
||||
symbol=symbol,
|
||||
ohlcv=ohlcv_by_asset[label],
|
||||
prompt_library=prompt_library,
|
||||
llm=llm,
|
||||
ga_db_path=settings.ga_db_path,
|
||||
)
|
||||
evals_by_asset[label] = _collect_evaluations(
|
||||
repo, run_id, ohlcv_by_asset[label]
|
||||
)
|
||||
logger.info(
|
||||
"%s: %d evaluations collected", label.upper(),
|
||||
len(evals_by_asset[label]),
|
||||
)
|
||||
|
||||
# Cross-rank con invariance bonus.
|
||||
winners = compute_invariance_for_pair(
|
||||
evals_by_asset["btc"], evals_by_asset["eth"],
|
||||
)
|
||||
logger.info(
|
||||
"Computed invariance bonus for %d common genomes", len(winners),
|
||||
)
|
||||
|
||||
top = winners[:TOP_K_PERSIST]
|
||||
persist_winners(con, top)
|
||||
logger.info(
|
||||
"Persisted top %d winners to %s (table: pythagoras_winners)",
|
||||
len(top), DB_PATH,
|
||||
)
|
||||
finally:
|
||||
con.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -6,9 +6,9 @@ from pathlib import Path
|
||||
import numpy as np
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from multi_swarm.genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
from multi_swarm.llm.client import CompletionResult
|
||||
from multi_swarm.orchestrator.run import RunConfig, run_phase1
|
||||
from multi_swarm_core.genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
from multi_swarm_core.llm.client import CompletionResult
|
||||
from multi_swarm_core.orchestrator.run import RunConfig, run_phase1
|
||||
|
||||
_MOCK_STRATEGY = json.dumps(
|
||||
{
|
||||
|
||||
@@ -0,0 +1,271 @@
|
||||
"""Multi-fold validation di un run esistente.
|
||||
|
||||
Prende un ``run_id`` da ``state/runs.db``, seleziona i top-K genomi per fitness IS,
|
||||
e li rivaluta su N fold expanding-window di un dataset OHLCV (tipicamente piu'
|
||||
lungo del train del GA). Output: per-fold + aggregati (mean / min / std) della
|
||||
fitness OOS.
|
||||
|
||||
Use case: il GA puo' selezionare un "lucky-shot" overfit a uno specifico regime.
|
||||
Validare i top-K su finestre temporali diverse rivela quali strategie sono
|
||||
robuste vs overfitter.
|
||||
|
||||
Esempio::
|
||||
|
||||
python scripts/validate_run.py \\
|
||||
--run-id e263651598894da688d95fda90a34a96 \\
|
||||
--top-k 10 --n-folds 4 \\
|
||||
--symbol BTC-PERPETUAL --timeframe 1h \\
|
||||
--start 2018-09-01 --end 2026-01-01
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import statistics
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from multi_swarm_core.agents.adversarial import AdversarialAgent
|
||||
from multi_swarm_core.agents.falsification import FalsificationAgent
|
||||
from multi_swarm_core.agents.hypothesis import _try_parse
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm_core.data.splits import expanding_walk_forward
|
||||
from multi_swarm_core.ga.fitness import compute_fitness
|
||||
from multi_swarm_core.persistence.repository import Repository
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
p = argparse.ArgumentParser(description="Multi-fold validation di top-K genomi")
|
||||
p.add_argument("--run-id", required=True, help="run_id da validare")
|
||||
p.add_argument("--top-k", type=int, default=10, help="quanti genomi top valutare")
|
||||
p.add_argument("--n-folds", type=int, default=4, help="numero fold expanding-window")
|
||||
p.add_argument(
|
||||
"--train-ratio",
|
||||
type=float,
|
||||
default=0.5,
|
||||
help="frazione iniziale per il train iniziale (folds testano la coda)",
|
||||
)
|
||||
p.add_argument("--symbol", default="BTC-PERPETUAL")
|
||||
p.add_argument("--timeframe", default="1h")
|
||||
p.add_argument("--exchange", default="deribit", choices=["deribit", "bybit", "hyperliquid"])
|
||||
p.add_argument("--start", default="2018-09-01T00:00:00+00:00")
|
||||
p.add_argument("--end", default="2026-01-01T00:00:00+00:00")
|
||||
p.add_argument("--fees-bp", type=float, default=5.0)
|
||||
p.add_argument("--n-trials-dsr", type=int, default=50)
|
||||
p.add_argument(
|
||||
"--fees-eat-alpha-threshold", type=float, default=0.5,
|
||||
)
|
||||
p.add_argument(
|
||||
"--flat-too-long-threshold", type=float, default=0.95,
|
||||
)
|
||||
p.add_argument(
|
||||
"--undertrading-threshold", type=int, default=10,
|
||||
)
|
||||
p.add_argument(
|
||||
"--fitness-v2", action="store_true",
|
||||
help="Coerente con --fitness-v2 del run originale",
|
||||
)
|
||||
p.add_argument(
|
||||
"--fitness-soft-penalty", type=float, default=0.4,
|
||||
)
|
||||
p.add_argument(
|
||||
"--output-json",
|
||||
type=Path,
|
||||
default=None,
|
||||
help="Path JSON dove salvare i risultati (default: stdout solo)",
|
||||
)
|
||||
return p.parse_args()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
settings = load_settings()
|
||||
|
||||
# Repository: top-K genomi per fitness IS, con raw_text parsable.
|
||||
repo = Repository(settings.ga_db_path)
|
||||
repo.init_schema()
|
||||
run = repo.get_run(args.run_id)
|
||||
if run is None:
|
||||
raise SystemExit(f"run_id non trovato: {args.run_id}")
|
||||
print(f"Validating run: {run['name']} ({args.run_id})")
|
||||
print(f" status: {run['status']}, cost: ${run['total_cost_usd']:.4f}")
|
||||
|
||||
all_evals = repo.list_evaluations(args.run_id)
|
||||
parseable = [
|
||||
e for e in all_evals
|
||||
if e.get("raw_text") and not e.get("parse_error") and e["fitness"] > 0
|
||||
]
|
||||
parseable.sort(key=lambda e: e["fitness"], reverse=True)
|
||||
|
||||
# Dedup by genome_id (gli elite vengono salvati una sola volta ma possono apparire
|
||||
# in evaluations multiple se rivalutati con eval_oos_during_loop).
|
||||
seen_ids: set[str] = set()
|
||||
top_genomes: list[dict] = []
|
||||
for e in parseable:
|
||||
if e["genome_id"] in seen_ids:
|
||||
continue
|
||||
seen_ids.add(e["genome_id"])
|
||||
top_genomes.append(e)
|
||||
if len(top_genomes) >= args.top_k:
|
||||
break
|
||||
print(f" selected top-{len(top_genomes)} genomes for validation")
|
||||
|
||||
# OHLCV: carica il dataset esteso.
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
else settings.cerbero_testnet_token.get_secret_value()
|
||||
)
|
||||
cerbero = CerberoClient(
|
||||
base_url=settings.cerbero_base_url,
|
||||
token=token,
|
||||
bot_tag=settings.cerbero_bot_tag,
|
||||
)
|
||||
loader = CerberoOHLCVLoader(client=cerbero, cache_dir=settings.series_dir)
|
||||
req = OHLCVRequest(
|
||||
symbol=args.symbol,
|
||||
timeframe=args.timeframe,
|
||||
start=datetime.fromisoformat(args.start),
|
||||
end=datetime.fromisoformat(args.end),
|
||||
exchange=args.exchange,
|
||||
)
|
||||
ohlcv = loader.load(req)
|
||||
print(f" OHLCV: {len(ohlcv)} bars from {ohlcv.index[0]} to {ohlcv.index[-1]}")
|
||||
|
||||
splits = expanding_walk_forward(
|
||||
ohlcv.index, train_ratio=args.train_ratio, n_folds=args.n_folds,
|
||||
)
|
||||
print(f" generated {len(splits)} folds")
|
||||
for s in splits:
|
||||
print(f" fold {s.fold}: test [{s.test_idx[0]} -> {s.test_idx[-1]}] ({len(s.test_idx)} bars)")
|
||||
|
||||
fals_agent = FalsificationAgent(fees_bp=args.fees_bp, n_trials_dsr=args.n_trials_dsr)
|
||||
adv_agent = AdversarialAgent(
|
||||
fees_bp=args.fees_bp,
|
||||
fees_eat_alpha_threshold=args.fees_eat_alpha_threshold,
|
||||
flat_too_long_threshold=args.flat_too_long_threshold,
|
||||
undertrading_threshold=args.undertrading_threshold,
|
||||
)
|
||||
hard_kill = (
|
||||
("no_trades", "degenerate", "undertrading") if args.fitness_v2 else None
|
||||
)
|
||||
|
||||
# Itera per ogni genome + fold.
|
||||
results: list[dict] = []
|
||||
for gi, ev in enumerate(top_genomes):
|
||||
strategy, parse_err = _try_parse(ev["raw_text"] or "")
|
||||
if strategy is None:
|
||||
print(f" [{gi}] {ev['genome_id'][:12]} skip (parse error: {parse_err})")
|
||||
continue
|
||||
|
||||
per_fold: list[dict] = []
|
||||
for s in splits:
|
||||
test_df = ohlcv.loc[s.test_idx]
|
||||
try:
|
||||
fals = fals_agent.evaluate(strategy, test_df)
|
||||
adv = adv_agent.review(strategy, test_df)
|
||||
fit = compute_fitness(
|
||||
fals, adv,
|
||||
hard_kill_findings=hard_kill,
|
||||
adversarial_soft_penalty=args.fitness_soft_penalty,
|
||||
)
|
||||
except Exception as e:
|
||||
print(f" fold {s.fold} eval failed: {e}")
|
||||
continue
|
||||
per_fold.append({
|
||||
"fold": s.fold,
|
||||
"fitness": float(fit),
|
||||
"sharpe": float(fals.sharpe),
|
||||
"dsr": float(fals.dsr),
|
||||
"dsr_pvalue": float(fals.dsr_pvalue),
|
||||
"return": float(fals.total_return),
|
||||
"max_dd": float(fals.max_drawdown),
|
||||
"n_trades": int(fals.n_trades),
|
||||
"test_start": str(s.test_idx[0]),
|
||||
"test_end": str(s.test_idx[-1]),
|
||||
})
|
||||
|
||||
if not per_fold:
|
||||
continue
|
||||
|
||||
fits = [pf["fitness"] for pf in per_fold]
|
||||
sharps = [pf["sharpe"] for pf in per_fold]
|
||||
results.append({
|
||||
"genome_id": ev["genome_id"],
|
||||
"fitness_is": float(ev["fitness"]),
|
||||
"sharpe_is": float(ev["sharpe"]),
|
||||
"folds": per_fold,
|
||||
"fitness_oos_mean": statistics.mean(fits),
|
||||
"fitness_oos_min": min(fits),
|
||||
"fitness_oos_max": max(fits),
|
||||
"fitness_oos_std": statistics.pstdev(fits) if len(fits) > 1 else 0.0,
|
||||
"sharpe_oos_mean": statistics.mean(sharps),
|
||||
"sharpe_oos_min": min(sharps),
|
||||
"robust_score": min(fits), # min across folds = pessimismo
|
||||
})
|
||||
|
||||
# Ranking finale: per robust_score (min fitness) decrescente.
|
||||
results.sort(key=lambda r: r["robust_score"], reverse=True)
|
||||
|
||||
print()
|
||||
print(f"{'='*120}")
|
||||
print(f"VALIDATION RESULTS ({len(results)} genomes, {len(splits)} folds)")
|
||||
print(f"{'='*120}")
|
||||
print(
|
||||
f"{'rank':>4} {'genome':12} {'fit_is':>8} {'sh_is':>7} "
|
||||
f"{'fit_mean':>9} {'fit_min':>8} {'fit_max':>8} {'fit_std':>8} "
|
||||
f"{'sh_mean':>8} {'sh_min':>8} {'robust':>7}"
|
||||
)
|
||||
print("-" * 120)
|
||||
for rank, r in enumerate(results, 1):
|
||||
print(
|
||||
f"{rank:>4} {r['genome_id'][:12]:12} "
|
||||
f"{r['fitness_is']:>8.4f} {r['sharpe_is']:>7.3f} "
|
||||
f"{r['fitness_oos_mean']:>9.4f} {r['fitness_oos_min']:>8.4f} "
|
||||
f"{r['fitness_oos_max']:>8.4f} {r['fitness_oos_std']:>8.4f} "
|
||||
f"{r['sharpe_oos_mean']:>8.3f} {r['sharpe_oos_min']:>8.3f} "
|
||||
f"{r['robust_score']:>7.4f}"
|
||||
)
|
||||
|
||||
if results:
|
||||
winner = results[0]
|
||||
print()
|
||||
print(f"ROBUST WINNER: {winner['genome_id']}")
|
||||
print(f" fitness_is={winner['fitness_is']:.4f}, "
|
||||
f"fitness_oos_min={winner['fitness_oos_min']:.4f}, "
|
||||
f"fitness_oos_mean={winner['fitness_oos_mean']:.4f}")
|
||||
print(f" sharpe_is={winner['sharpe_is']:.3f}, "
|
||||
f"sharpe_oos_min={winner['sharpe_oos_min']:.3f}")
|
||||
print(f" per-fold breakdown:")
|
||||
for pf in winner["folds"]:
|
||||
print(
|
||||
f" fold {pf['fold']} [{pf['test_start'][:10]} .. {pf['test_end'][:10]}]: "
|
||||
f"fit={pf['fitness']:.4f} sharpe={pf['sharpe']:.3f} "
|
||||
f"ret={pf['return']:.3f} n_trades={pf['n_trades']}"
|
||||
)
|
||||
|
||||
if args.output_json:
|
||||
payload = {
|
||||
"run_id": args.run_id,
|
||||
"run_name": run["name"],
|
||||
"n_folds": len(splits),
|
||||
"top_k_requested": args.top_k,
|
||||
"top_k_evaluated": len(results),
|
||||
"symbol": args.symbol,
|
||||
"timeframe": args.timeframe,
|
||||
"start": args.start,
|
||||
"end": args.end,
|
||||
"ohlcv_bars": len(ohlcv),
|
||||
"results": results,
|
||||
}
|
||||
args.output_json.write_text(json.dumps(payload, indent=2, default=str))
|
||||
print(f"\nResults saved to: {args.output_json}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,112 @@
|
||||
"""Per-year breakdown delle 4 strategie: 2 NEW (BTC 238e4812 + ETH c04dff7086)
|
||||
+ 2 OLD freezate (btc_9cf506b8 hardened-001 + eth_facd6af85d5d).
|
||||
|
||||
Backtest anno-per-anno (2019-2025) sul tick di discovery di ciascuna strategia.
|
||||
Output: trade, wins/losses, win%, return%, max DD%, Sharpe per ogni anno.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from multi_swarm_core.backtest.engine import BacktestEngine
|
||||
from multi_swarm_core.cerbero.client import CerberoClient
|
||||
from multi_swarm_core.config import load_settings
|
||||
from multi_swarm_core.data.cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
from multi_swarm_core.metrics.basic import max_drawdown, sharpe_ratio, total_return
|
||||
from multi_swarm_core.protocol.compiler import compile_strategy
|
||||
from multi_swarm_core.protocol.parser import parse_strategy
|
||||
|
||||
|
||||
STRATEGIES = [
|
||||
# (label, path, symbol, timeframe)
|
||||
("BTC NEW (238e4812, paper attuale)", "btc_238e4812.json", "BTC-PERPETUAL", "1h"),
|
||||
("BTC OLD (9cf506b8, hardened-001 prev paper)", "archive/btc_9cf506b8.json", "BTC-PERPETUAL", "1h"),
|
||||
("ETH NEW (c04dff7086, paper attuale)", "eth_c04dff7086.json", "ETH-PERPETUAL", "5m"),
|
||||
("ETH OLD (facd6af85d5d, prev paper)", "archive/eth_facd6af85d5d.json", "ETH-PERPETUAL", "1h"),
|
||||
]
|
||||
|
||||
YEARS = [
|
||||
("2019", "2019-01-01T00:00:00+00:00", "2020-01-01T00:00:00+00:00"),
|
||||
("2020", "2020-01-01T00:00:00+00:00", "2021-01-01T00:00:00+00:00"),
|
||||
("2021", "2021-01-01T00:00:00+00:00", "2022-01-01T00:00:00+00:00"),
|
||||
("2022", "2022-01-01T00:00:00+00:00", "2023-01-01T00:00:00+00:00"),
|
||||
("2023", "2023-01-01T00:00:00+00:00", "2024-01-01T00:00:00+00:00"),
|
||||
("2024", "2024-01-01T00:00:00+00:00", "2025-01-01T00:00:00+00:00"),
|
||||
("2025", "2025-01-01T00:00:00+00:00", "2026-01-01T00:00:00+00:00"),
|
||||
]
|
||||
|
||||
|
||||
def main() -> None:
|
||||
settings = load_settings()
|
||||
token = (
|
||||
settings.cerbero_mainnet_token.get_secret_value()
|
||||
if settings.cerbero_mainnet_token
|
||||
else settings.cerbero_testnet_token.get_secret_value()
|
||||
)
|
||||
cerbero = CerberoClient(
|
||||
base_url=settings.cerbero_base_url,
|
||||
token=token,
|
||||
bot_tag=settings.cerbero_bot_tag,
|
||||
)
|
||||
loader = CerberoOHLCVLoader(client=cerbero, cache_dir=settings.series_dir)
|
||||
engine = BacktestEngine(fees_bp=5.0)
|
||||
strategies_dir = Path("/app/strategies")
|
||||
|
||||
for label, fname, symbol, timeframe in STRATEGIES:
|
||||
path = strategies_dir / fname
|
||||
strat = parse_strategy(path.read_text())
|
||||
|
||||
# Carica intero range una volta sola
|
||||
ohlcv = loader.load(OHLCVRequest(
|
||||
symbol=symbol, timeframe=timeframe,
|
||||
start=datetime.fromisoformat("2018-09-01T00:00:00+00:00"),
|
||||
end=datetime.fromisoformat("2026-01-01T00:00:00+00:00"),
|
||||
))
|
||||
|
||||
print(f"\n{'=' * 110}")
|
||||
print(f">>> {label}")
|
||||
print(f" symbol={symbol} timeframe={timeframe} | {len(ohlcv)} bars total")
|
||||
print(f" {'year':<6} {'bars':>6} {'trades':>7} {'wins':>5} {'losses':>7} {'win%':>6} {'avg_w':>10} {'avg_l':>10} {'ret':>8} {'maxDD':>7} {'sharpe':>7}")
|
||||
|
||||
sum_ret = 0.0
|
||||
sum_trades = 0
|
||||
sum_wins = 0
|
||||
for year_label, start, end in YEARS:
|
||||
mask = (ohlcv.index >= datetime.fromisoformat(start)) & (ohlcv.index < datetime.fromisoformat(end))
|
||||
slice_df = ohlcv[mask]
|
||||
if len(slice_df) == 0:
|
||||
continue
|
||||
try:
|
||||
signal_fn = compile_strategy(strat)
|
||||
signals = signal_fn(slice_df)
|
||||
bt = engine.run(slice_df, signals)
|
||||
except Exception as e:
|
||||
print(f" {year_label:<6} ERROR: {e}")
|
||||
continue
|
||||
trades = bt.trades
|
||||
n = len(trades)
|
||||
wins = [t.net_pnl for t in trades if t.net_pnl > 0]
|
||||
losses = [t.net_pnl for t in trades if t.net_pnl <= 0]
|
||||
nw, nl = len(wins), len(losses)
|
||||
wr = (nw / n * 100) if n else 0.0
|
||||
aw = (sum(wins) / nw) if nw else 0.0
|
||||
al = (sum(losses) / nl) if nl else 0.0
|
||||
if n > 0:
|
||||
notional = float(slice_df["close"].iloc[0])
|
||||
eq = (bt.equity_curve / notional) + 1.0
|
||||
ret = total_return(eq)
|
||||
dd = max_drawdown(eq)
|
||||
sr = sharpe_ratio(bt.returns, periods_per_year=8760)
|
||||
else:
|
||||
ret = dd = sr = 0.0
|
||||
print(f" {year_label:<6} {len(slice_df):>6} {n:>7} {nw:>5} {nl:>7} {wr:>5.1f}% {aw:>10.1f} {al:>10.1f} {ret:>7.2%} {dd:>6.2%} {sr:>7.3f}")
|
||||
sum_ret += ret
|
||||
sum_trades += n
|
||||
sum_wins += nw
|
||||
overall_wr = (sum_wins / sum_trades * 100) if sum_trades else 0.0
|
||||
print(f" {'='*5} TOTALS 7y: {sum_trades:>7} {sum_wins:>5} {sum_trades-sum_wins:>7} {overall_wr:>5.1f}% cum_ret={sum_ret:+.2%}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,286 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from ..genome.hypothesis import HypothesisAgentGenome
|
||||
from ..llm.client import CompletionResult, LLMClient
|
||||
from ..protocol.parser import ParseError, Strategy, parse_strategy
|
||||
from ..protocol.validator import ValidationError, validate_strategy
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MarketSummary:
|
||||
symbol: str
|
||||
timeframe: str
|
||||
n_bars: int
|
||||
return_mean: float
|
||||
return_std: float
|
||||
skew: float
|
||||
kurtosis: float
|
||||
volatility_regime: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HypothesisProposal:
|
||||
"""Risultato di una propose() del HypothesisAgent.
|
||||
|
||||
``completions`` contiene SEMPRE almeno un elemento: il primo tentativo.
|
||||
Se il primo tentativo fallisce e c'e' budget di retry, vengono accodate
|
||||
le completions successive, una per ogni retry effettuato.
|
||||
``n_attempts == len(completions)``. ``raw_text`` riflette l'ULTIMO output
|
||||
LLM osservato (quello che ha prodotto strategy o l'ultimo parse_error).
|
||||
"""
|
||||
|
||||
strategy: Strategy | None
|
||||
raw_text: str
|
||||
completions: list[CompletionResult] = field(default_factory=list)
|
||||
parse_error: str | None = None
|
||||
n_attempts: int = 1
|
||||
|
||||
|
||||
SYSTEM_TEMPLATE = """\
|
||||
Sei un agente generatore di ipotesi di trading quantitativo per un sistema swarm.
|
||||
|
||||
Il tuo stile cognitivo: {cognitive_style}
|
||||
Direttiva personale: {system_prompt}
|
||||
|
||||
Devi proporre una strategia di trading espressa in JSON STRETTO.
|
||||
La risposta deve essere un singolo oggetto JSON dentro fence ```json...```
|
||||
con questa shape:
|
||||
|
||||
```json
|
||||
{{
|
||||
"rules": [
|
||||
{{"condition": <nodo>, "action": "entry-long|entry-short|exit|flat"}}
|
||||
]
|
||||
}}
|
||||
```
|
||||
|
||||
NODI DISPONIBILI
|
||||
|
||||
Operatori logici:
|
||||
{{"op": "and", "args": [<nodo>, <nodo>, ...]}} // >=2 nodi
|
||||
{{"op": "or", "args": [<nodo>, <nodo>, ...]}} // >=2 nodi
|
||||
{{"op": "not", "args": [<nodo>]}} // 1 nodo
|
||||
|
||||
Comparatori (ritornano boolean series):
|
||||
{{"op": "gt", "args": [<a>, <b>]}} // a > b
|
||||
{{"op": "lt", "args": [<a>, <b>]}} // a < b
|
||||
{{"op": "eq", "args": [<a>, <b>]}} // a == b
|
||||
|
||||
Crossover (eventi su 2 serie):
|
||||
{{"op": "crossover", "args": [<serie_a>, <serie_b>]}}
|
||||
{{"op": "crossunder", "args": [<serie_a>, <serie_b>]}}
|
||||
|
||||
Leaf - indicatori (calcolati su close):
|
||||
{{"kind": "indicator", "name": "sma", "params": [<length>]}}
|
||||
{{"kind": "indicator", "name": "rsi", "params": [<length>]}}
|
||||
{{"kind": "indicator", "name": "atr", "params": [<length>]}}
|
||||
{{"kind": "indicator", "name": "realized_vol", "params": [<window>]}}
|
||||
{{"kind": "indicator", "name": "macd", "params": [<fast>, <slow>, <signal>]}}
|
||||
// 0-3 numeri (tutti opzionali con default 12, 26, 9)
|
||||
|
||||
Leaf - feature OHLCV:
|
||||
{{"kind": "feature", "name": "open|high|low|close|volume"}}
|
||||
|
||||
Leaf - letterale numerico:
|
||||
{{"kind": "literal", "value": 70.0}}
|
||||
|
||||
VINCOLI
|
||||
- Gli indicator NON sono annidabili: 'params' accetta solo numeri, mai altri nodi.
|
||||
- Le regole sono valutate in ordine; la prima che matcha vince per ogni timestamp.
|
||||
- Default action se nessuna regola matcha = flat.
|
||||
- 'op' e 'kind' sono mutuamente esclusivi sullo stesso nodo.
|
||||
|
||||
Rispondi SOLO con il fence ```json...``` contenente l'oggetto strategy.
|
||||
Esempio:
|
||||
|
||||
```json
|
||||
{{
|
||||
"rules": [
|
||||
{{
|
||||
"condition": {{"op": "gt", "args": [
|
||||
{{"kind": "indicator", "name": "rsi", "params": [14]}},
|
||||
{{"kind": "literal", "value": 70.0}}
|
||||
]}},
|
||||
"action": "entry-short"
|
||||
}},
|
||||
{{
|
||||
"condition": {{"op": "lt", "args": [
|
||||
{{"kind": "indicator", "name": "rsi", "params": [14]}},
|
||||
{{"kind": "literal", "value": 30.0}}
|
||||
]}},
|
||||
"action": "entry-long"
|
||||
}}
|
||||
]
|
||||
}}
|
||||
```
|
||||
"""
|
||||
|
||||
|
||||
USER_TEMPLATE = """\
|
||||
Mercato: {symbol} timeframe {timeframe}, {n_bars} barre osservate.
|
||||
Statistiche return: mean={return_mean:.5f}, std={return_std:.5f}, \
|
||||
skew={skew:.3f}, kurt={kurtosis:.3f}.
|
||||
Regime volatilità: {volatility_regime}.
|
||||
|
||||
Feature accessibili dal tuo genoma: {feature_access}.
|
||||
Lookback massimo che puoi usare nel ragionamento: {lookback_window} barre.
|
||||
|
||||
Genera una strategia che cerchi anomalie sfruttabili in questo regime.
|
||||
"""
|
||||
|
||||
|
||||
_RETRY_TEMPLATE = """\
|
||||
{original_user}
|
||||
|
||||
--- TENTATIVO PRECEDENTE FALLITO ---
|
||||
Output: {previous_raw}
|
||||
Errore: {previous_error}
|
||||
---
|
||||
Correggi l'errore e rispondi di nuovo con un singolo oggetto JSON valido
|
||||
dentro fence ```json...```, seguendo strettamente lo schema fornito nel
|
||||
SYSTEM message.
|
||||
"""
|
||||
|
||||
_RETRY_RAW_TRUNCATE = 800
|
||||
|
||||
|
||||
_JSON_FENCE_RE = re.compile(
|
||||
r"```(?:json)?\s*(\{[\s\S]*\})\s*```",
|
||||
re.MULTILINE,
|
||||
)
|
||||
|
||||
|
||||
def _balance_braces(s: str) -> str | None:
|
||||
"""Ritorna il prefix di ``s`` che chiude la prima ``{`` con bilanciamento.
|
||||
|
||||
Usato come fallback quando l'LLM ritorna JSON top-level senza fence ma
|
||||
seguito da prosa: troviamo dove finisce il primo oggetto e tagliamo.
|
||||
"""
|
||||
if not s.startswith("{"):
|
||||
return None
|
||||
depth = 0
|
||||
in_string = False
|
||||
escape = False
|
||||
for i, ch in enumerate(s):
|
||||
if in_string:
|
||||
if escape:
|
||||
escape = False
|
||||
elif ch == "\\":
|
||||
escape = True
|
||||
elif ch == '"':
|
||||
in_string = False
|
||||
continue
|
||||
if ch == '"':
|
||||
in_string = True
|
||||
elif ch == "{":
|
||||
depth += 1
|
||||
elif ch == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
return s[: i + 1]
|
||||
return None
|
||||
|
||||
|
||||
def _extract_json(text: str) -> str | None:
|
||||
"""Estrai un oggetto JSON dal testo del completion.
|
||||
|
||||
Strategie di estrazione, in ordine:
|
||||
1. Fence ```json...``` (greedy: cattura fino all'ultimo ``}`` prima della
|
||||
chiusura del fence).
|
||||
2. Testo che inizia direttamente con ``{`` (dopo strip), bilanciato a
|
||||
livello di parentesi graffe.
|
||||
"""
|
||||
m = _JSON_FENCE_RE.search(text)
|
||||
if m:
|
||||
return m.group(1)
|
||||
stripped = text.strip()
|
||||
return _balance_braces(stripped)
|
||||
|
||||
|
||||
def _try_parse(text: str) -> tuple[Strategy | None, str | None]:
|
||||
"""Estrai+parsea+valida. Ritorna (strategy, error). Esattamente uno e' None."""
|
||||
payload = _extract_json(text)
|
||||
if payload is None:
|
||||
return None, "no JSON object found in output"
|
||||
try:
|
||||
ast = parse_strategy(payload)
|
||||
validate_strategy(ast)
|
||||
except (ParseError, ValidationError) as e:
|
||||
return None, str(e)
|
||||
return ast, None
|
||||
|
||||
|
||||
class HypothesisAgent:
|
||||
def __init__(self, llm: LLMClient, max_retries: int = 1):
|
||||
if max_retries < 0:
|
||||
raise ValueError("max_retries must be >= 0")
|
||||
self._llm = llm
|
||||
self._max_retries = max_retries
|
||||
|
||||
def propose(
|
||||
self,
|
||||
genome: HypothesisAgentGenome,
|
||||
market: MarketSummary,
|
||||
) -> HypothesisProposal:
|
||||
system = SYSTEM_TEMPLATE.format(
|
||||
cognitive_style=genome.cognitive_style,
|
||||
system_prompt=genome.system_prompt,
|
||||
)
|
||||
original_user = USER_TEMPLATE.format(
|
||||
symbol=market.symbol,
|
||||
timeframe=market.timeframe,
|
||||
n_bars=market.n_bars,
|
||||
return_mean=market.return_mean,
|
||||
return_std=market.return_std,
|
||||
skew=market.skew,
|
||||
kurtosis=market.kurtosis,
|
||||
volatility_regime=market.volatility_regime,
|
||||
feature_access=", ".join(genome.feature_access),
|
||||
lookback_window=genome.lookback_window,
|
||||
)
|
||||
|
||||
completions: list[CompletionResult] = []
|
||||
errors: list[str] = []
|
||||
last_raw = ""
|
||||
max_attempts = 1 + self._max_retries
|
||||
|
||||
for attempt in range(max_attempts):
|
||||
if attempt == 0:
|
||||
user = original_user
|
||||
else:
|
||||
truncated = last_raw[:_RETRY_RAW_TRUNCATE]
|
||||
user = _RETRY_TEMPLATE.format(
|
||||
original_user=original_user,
|
||||
previous_raw=truncated,
|
||||
previous_error=errors[-1],
|
||||
)
|
||||
|
||||
completion = self._llm.complete(genome, system=system, user=user)
|
||||
completions.append(completion)
|
||||
last_raw = completion.text
|
||||
|
||||
strategy, err = _try_parse(completion.text)
|
||||
if strategy is not None:
|
||||
return HypothesisProposal(
|
||||
strategy=strategy,
|
||||
raw_text=completion.text,
|
||||
completions=completions,
|
||||
parse_error=None,
|
||||
n_attempts=len(completions),
|
||||
)
|
||||
assert err is not None
|
||||
errors.append(err)
|
||||
|
||||
chained = " | ".join(
|
||||
f"attempt {i + 1}: {e}" for i, e in enumerate(errors)
|
||||
)
|
||||
return HypothesisProposal(
|
||||
strategy=None,
|
||||
raw_text=last_raw,
|
||||
completions=completions,
|
||||
parse_error=chained,
|
||||
n_attempts=len(completions),
|
||||
)
|
||||
@@ -1,36 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
from scipy import stats # type: ignore[import-untyped]
|
||||
|
||||
from .hypothesis import MarketSummary
|
||||
|
||||
|
||||
def build_market_summary(
|
||||
ohlcv: pd.DataFrame,
|
||||
symbol: str,
|
||||
timeframe: str,
|
||||
) -> MarketSummary:
|
||||
returns = ohlcv["close"].pct_change().dropna()
|
||||
return_mean = float(returns.mean())
|
||||
return_std = float(returns.std(ddof=1))
|
||||
skew = float(stats.skew(returns, bias=False))
|
||||
kurt = float(stats.kurtosis(returns, fisher=True, bias=False))
|
||||
|
||||
if return_std < 0.005:
|
||||
regime = "low"
|
||||
elif return_std < 0.02:
|
||||
regime = "medium"
|
||||
else:
|
||||
regime = "high"
|
||||
|
||||
return MarketSummary(
|
||||
symbol=symbol,
|
||||
timeframe=timeframe,
|
||||
n_bars=len(ohlcv),
|
||||
return_mean=return_mean,
|
||||
return_std=return_std,
|
||||
skew=skew,
|
||||
kurtosis=kurt,
|
||||
volatility_regime=regime,
|
||||
)
|
||||
@@ -1,101 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from .orders import Position, Side, Trade
|
||||
|
||||
Signal = Side # alias semantico
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BacktestResult:
|
||||
equity_curve: pd.Series
|
||||
returns: pd.Series
|
||||
trades: list[Trade]
|
||||
|
||||
|
||||
class BacktestEngine:
|
||||
"""Engine event-driven sincrono: itera bar per bar, applica segnali con
|
||||
delay di 1 bar (segnale a t -> eseguito a t+1 open) per evitare lookahead.
|
||||
|
||||
Position sizing: 1 unit per posizione. Fees applicati su entry+exit.
|
||||
Niente leva, niente liquidation, niente funding (semplificazione Phase 1).
|
||||
"""
|
||||
|
||||
def __init__(self, fees_bp: float = 5.0) -> None:
|
||||
self.fees_bp = fees_bp
|
||||
|
||||
def run(self, ohlcv: pd.DataFrame, signals: pd.Series) -> BacktestResult:
|
||||
signals = signals.reindex(ohlcv.index).ffill().fillna(Side.FLAT)
|
||||
|
||||
# Esecuzione con delay 1: segnale a t-1 esegue a open di t.
|
||||
shifted = [Side.FLAT, *list(signals.iloc[:-1])]
|
||||
executed_side = pd.Series(shifted, index=ohlcv.index, dtype=object)
|
||||
|
||||
position: Position | None = None
|
||||
position_entry_ts: pd.Timestamp | None = None
|
||||
trades: list[Trade] = []
|
||||
equity = 0.0
|
||||
equity_history: list[float] = []
|
||||
returns_history: list[float] = []
|
||||
prev_equity = 0.0
|
||||
|
||||
for ts, row in ohlcv.iterrows():
|
||||
target_side = executed_side.loc[ts]
|
||||
current_side = position.side if position else Side.FLAT
|
||||
|
||||
if target_side != current_side:
|
||||
if position is not None:
|
||||
assert position_entry_ts is not None
|
||||
trade = Trade(
|
||||
entry_ts=position_entry_ts,
|
||||
exit_ts=ts,
|
||||
side=position.side,
|
||||
size=position.size,
|
||||
entry_price=position.entry_price,
|
||||
exit_price=float(row["open"]),
|
||||
fees_bp=self.fees_bp,
|
||||
)
|
||||
trades.append(trade)
|
||||
equity += trade.net_pnl
|
||||
position = None
|
||||
position_entry_ts = None
|
||||
if target_side in (Side.LONG, Side.SHORT):
|
||||
position = Position(
|
||||
side=target_side, entry_price=float(row["open"]), size=1.0
|
||||
)
|
||||
position_entry_ts = ts
|
||||
|
||||
mark = float(row["close"])
|
||||
mtm = position.unrealized_pnl(mark) if position else 0.0
|
||||
current_equity = equity + mtm
|
||||
equity_history.append(current_equity)
|
||||
returns_history.append(current_equity - prev_equity)
|
||||
prev_equity = current_equity
|
||||
|
||||
if position is not None:
|
||||
assert position_entry_ts is not None
|
||||
last_ts = ohlcv.index[-1]
|
||||
last_close = float(ohlcv["close"].iloc[-1])
|
||||
trade = Trade(
|
||||
entry_ts=position_entry_ts,
|
||||
exit_ts=last_ts,
|
||||
side=position.side,
|
||||
size=position.size,
|
||||
entry_price=position.entry_price,
|
||||
exit_price=last_close,
|
||||
fees_bp=self.fees_bp,
|
||||
)
|
||||
trades.append(trade)
|
||||
equity += trade.net_pnl
|
||||
equity_history[-1] = equity
|
||||
if len(returns_history) >= 2:
|
||||
returns_history[-1] = equity - equity_history[-2]
|
||||
|
||||
return BacktestResult(
|
||||
equity_curve=pd.Series(equity_history, index=ohlcv.index, name="equity"),
|
||||
returns=pd.Series(returns_history, index=ohlcv.index, name="returns"),
|
||||
trades=trades,
|
||||
)
|
||||
@@ -1,43 +0,0 @@
|
||||
"""Pydantic settings loader for Multi_Swarm_Coevolutive.
|
||||
|
||||
Loads configuration from environment variables and an optional ``.env`` file
|
||||
in the project root. Required secrets are validated at instantiation time.
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import Field, SecretStr
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8",
|
||||
extra="ignore",
|
||||
case_sensitive=False,
|
||||
)
|
||||
|
||||
cerbero_base_url: str = "http://localhost:9000"
|
||||
cerbero_testnet_token: SecretStr
|
||||
cerbero_mainnet_token: SecretStr | None = None
|
||||
cerbero_bot_tag: str = "swarm-poc-phase1"
|
||||
|
||||
openrouter_api_key: SecretStr
|
||||
|
||||
llm_model_tier_s: str = "anthropic/claude-opus-4-7"
|
||||
llm_model_tier_a: str = "anthropic/claude-sonnet-4-6"
|
||||
llm_model_tier_b: str = "anthropic/claude-sonnet-4-6"
|
||||
llm_model_tier_c: str = "qwen/qwen-2.5-72b-instruct"
|
||||
llm_model_tier_d: str = "meta-llama/llama-3.3-70b-instruct"
|
||||
openrouter_base_url: str = "https://openrouter.ai/api/v1"
|
||||
|
||||
run_name: str = "phase1-spike-001"
|
||||
data_dir: Path = Field(default=Path("./data"))
|
||||
series_dir: Path = Field(default=Path("./series"))
|
||||
db_path: Path = Field(default=Path("./runs.db"))
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
# Required fields are populated from environment / .env, not init kwargs.
|
||||
return Settings() # type: ignore[call-arg]
|
||||
@@ -1,590 +0,0 @@
|
||||
"""Aquarium 2D visualization helpers.
|
||||
|
||||
Builds fish records (with full genome attributes + ancestor lineage) and
|
||||
renders a self-contained HTML/JS canvas animation, embeddable in Streamlit
|
||||
via ``streamlit.components.v1.html``. Includes a click handler that opens
|
||||
an info panel showing genome details and BFS ancestor levels.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import math
|
||||
from typing import Any
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
# Color palette per cognitive style. Default fallback for unknown styles is grey.
|
||||
STYLE_COLORS: dict[str, str] = {
|
||||
"physicist": "#4cc9f0",
|
||||
"biologist": "#52b788",
|
||||
"historian": "#e76f51",
|
||||
"meteorologist": "#ffd166",
|
||||
"ecologist": "#a78bfa",
|
||||
"engineer": "#fb6f92",
|
||||
}
|
||||
DEFAULT_COLOR: str = "#94a3b8"
|
||||
|
||||
|
||||
def _is_nan(v: Any) -> bool:
|
||||
try:
|
||||
return bool(pd.isna(v))
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def _safe_float(v: Any, default: float = 0.0) -> float:
|
||||
if v is None or _is_nan(v):
|
||||
return default
|
||||
try:
|
||||
return float(v)
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
def _safe_int(v: Any, default: int = 0) -> int:
|
||||
if v is None or _is_nan(v):
|
||||
return default
|
||||
try:
|
||||
return int(v)
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
def _safe_str(v: Any, default: str = "") -> str:
|
||||
if v is None or _is_nan(v):
|
||||
return default
|
||||
return str(v)
|
||||
|
||||
|
||||
def _safe_list(v: Any) -> list[Any]:
|
||||
if v is None:
|
||||
return []
|
||||
if isinstance(v, list):
|
||||
return list(v)
|
||||
# pandas may store python lists in object cells; if it's e.g. a numpy array,
|
||||
# falling back to list() is fine. NaN scalar is excluded by _is_nan.
|
||||
if _is_nan(v):
|
||||
return []
|
||||
try:
|
||||
return list(v)
|
||||
except TypeError:
|
||||
return []
|
||||
|
||||
|
||||
def build_lineage_index(
|
||||
genomes_df: pd.DataFrame, evals_df: pd.DataFrame
|
||||
) -> dict[str, dict[str, Any]]:
|
||||
"""Build ``{genome_id: attrs}`` for every genome in the run.
|
||||
|
||||
``genomes_df`` must come from ``genomes_df(repo, run_id)`` (no gen filter):
|
||||
columns include ``id``, ``generation_idx``, ``system_prompt``,
|
||||
``feature_access``, ``temperature``, ``top_p``, ``model_tier``,
|
||||
``lookback_window``, ``cognitive_style``, ``parent_ids``, ``generation``.
|
||||
|
||||
``evals_df`` must come from ``evaluations_df(repo, run_id)``: columns
|
||||
include ``genome_id``, ``fitness``, ``dsr``, ``sharpe``, ``max_dd``,
|
||||
``n_trades``.
|
||||
"""
|
||||
if genomes_df.empty:
|
||||
return {}
|
||||
|
||||
if evals_df is None or evals_df.empty:
|
||||
merged = genomes_df.copy()
|
||||
for col in ("fitness", "dsr", "sharpe", "max_dd", "n_trades"):
|
||||
if col not in merged.columns:
|
||||
merged[col] = 0.0 if col != "n_trades" else 0
|
||||
else:
|
||||
merged = genomes_df.merge(
|
||||
evals_df,
|
||||
left_on="id",
|
||||
right_on="genome_id",
|
||||
how="left",
|
||||
suffixes=("", "_eval"),
|
||||
)
|
||||
|
||||
index: dict[str, dict[str, Any]] = {}
|
||||
for _, row in merged.iterrows():
|
||||
gid = _safe_str(row.get("id"))
|
||||
if not gid:
|
||||
continue
|
||||
# ``generation`` is the genome's evolutionary generation (from payload).
|
||||
# If absent, fall back to ``generation_idx`` (column added by the
|
||||
# repository). Defensive: both may be missing in edge cases.
|
||||
gen_val: Any = row.get("generation")
|
||||
if gen_val is None or _is_nan(gen_val):
|
||||
gen_val = row.get("generation_idx", 0)
|
||||
index[gid] = {
|
||||
"id": gid,
|
||||
"generation": _safe_int(gen_val, 0),
|
||||
"fitness": _safe_float(row.get("fitness"), 0.0),
|
||||
"dsr": _safe_float(row.get("dsr"), 0.0),
|
||||
"sharpe": _safe_float(row.get("sharpe"), 0.0),
|
||||
"max_dd": _safe_float(row.get("max_dd"), 0.0),
|
||||
"n_trades": _safe_int(row.get("n_trades"), 0),
|
||||
"cognitive_style": _safe_str(row.get("cognitive_style"), ""),
|
||||
"system_prompt": _safe_str(row.get("system_prompt"), ""),
|
||||
"temperature": _safe_float(row.get("temperature"), 0.0),
|
||||
"lookback_window": _safe_int(row.get("lookback_window"), 0),
|
||||
"feature_access": _safe_list(row.get("feature_access")),
|
||||
"model_tier": _safe_str(row.get("model_tier"), ""),
|
||||
"parent_ids": _safe_list(row.get("parent_ids")),
|
||||
}
|
||||
return index
|
||||
|
||||
|
||||
def trace_ancestors(
|
||||
genome_id: str,
|
||||
lineage_index: dict[str, dict[str, Any]],
|
||||
max_levels: int = 5,
|
||||
) -> list[list[dict[str, Any]]]:
|
||||
"""BFS over ``parent_ids`` returning levels of ancestors.
|
||||
|
||||
``levels[0]`` = direct parents, ``levels[1]`` = grandparents, etc. Each
|
||||
entry is a small dict (no ``system_prompt``, to keep JSON payload light):
|
||||
``{id, generation, fitness, cognitive_style}``. Cycles are guarded via a
|
||||
``seen`` set; missing parents (not in this run) are stubbed with sentinel
|
||||
values so the lineage display still renders the relationship.
|
||||
"""
|
||||
levels: list[list[dict[str, Any]]] = []
|
||||
root = lineage_index.get(genome_id, {})
|
||||
current_ids: list[str] = list(root.get("parent_ids", []))
|
||||
seen: set[str] = {genome_id}
|
||||
for _ in range(max_levels):
|
||||
if not current_ids:
|
||||
break
|
||||
level_entries: list[dict[str, Any]] = []
|
||||
next_ids: list[str] = []
|
||||
for pid in current_ids:
|
||||
if pid in seen:
|
||||
continue
|
||||
seen.add(pid)
|
||||
entry = lineage_index.get(pid)
|
||||
if entry is None:
|
||||
level_entries.append(
|
||||
{
|
||||
"id": pid,
|
||||
"generation": -1,
|
||||
"fitness": 0.0,
|
||||
"cognitive_style": "",
|
||||
}
|
||||
)
|
||||
continue
|
||||
level_entries.append(
|
||||
{
|
||||
"id": entry["id"],
|
||||
"generation": entry["generation"],
|
||||
"fitness": entry["fitness"],
|
||||
"cognitive_style": entry["cognitive_style"],
|
||||
}
|
||||
)
|
||||
next_ids.extend(entry.get("parent_ids", []))
|
||||
if not level_entries:
|
||||
break
|
||||
levels.append(level_entries)
|
||||
current_ids = next_ids
|
||||
return levels
|
||||
|
||||
|
||||
def build_fish_dataset(
|
||||
active_df: pd.DataFrame,
|
||||
lineage_index: dict[str, dict[str, Any]] | None = None,
|
||||
max_lineage_levels: int = 5,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Build full fish records for each active genome.
|
||||
|
||||
For every row in ``active_df`` the matching entry in ``lineage_index`` is
|
||||
looked up by ``genome_id`` (or ``id``) and attached together with the BFS
|
||||
ancestor levels. Rows whose id is not in the index are skipped.
|
||||
|
||||
Backward-compat: if ``lineage_index`` is ``None`` (legacy call site, e.g.
|
||||
test fixtures with simple merged DataFrames) we synthesize a minimal
|
||||
lineage from ``active_df`` itself so the function still returns useful
|
||||
fish records.
|
||||
"""
|
||||
if active_df.empty:
|
||||
return []
|
||||
|
||||
if lineage_index is None:
|
||||
# Legacy path: build a tiny index from the active df only.
|
||||
synth: dict[str, dict[str, Any]] = {}
|
||||
for _, row in active_df.iterrows():
|
||||
gid = _safe_str(row.get("genome_id") or row.get("id"))
|
||||
if not gid:
|
||||
continue
|
||||
fitness_val = _safe_float(row.get("fitness"), float("nan"))
|
||||
if math.isnan(fitness_val):
|
||||
continue
|
||||
synth[gid] = {
|
||||
"id": gid,
|
||||
"generation": _safe_int(row.get("generation"), 0),
|
||||
"fitness": fitness_val,
|
||||
"dsr": _safe_float(row.get("dsr"), 0.0),
|
||||
"sharpe": _safe_float(row.get("sharpe"), 0.0),
|
||||
"max_dd": _safe_float(row.get("max_dd"), 0.0),
|
||||
"n_trades": _safe_int(row.get("n_trades"), 0),
|
||||
"cognitive_style": _safe_str(row.get("cognitive_style"), "unknown"),
|
||||
"system_prompt": _safe_str(row.get("system_prompt"), ""),
|
||||
"temperature": _safe_float(row.get("temperature"), 0.0),
|
||||
"lookback_window": _safe_int(row.get("lookback_window"), 0),
|
||||
"feature_access": _safe_list(row.get("feature_access")),
|
||||
"model_tier": _safe_str(row.get("model_tier"), ""),
|
||||
"parent_ids": _safe_list(row.get("parent_ids")),
|
||||
}
|
||||
lineage_index = synth
|
||||
|
||||
fish: list[dict[str, Any]] = []
|
||||
for _, row in active_df.iterrows():
|
||||
gid = _safe_str(row.get("genome_id") or row.get("id"))
|
||||
if not gid:
|
||||
continue
|
||||
attrs = lineage_index.get(gid)
|
||||
if attrs is None:
|
||||
continue
|
||||
if math.isnan(attrs.get("fitness", 0.0)):
|
||||
continue
|
||||
ancestors = trace_ancestors(gid, lineage_index, max_lineage_levels)
|
||||
record = {**attrs, "ancestors": ancestors}
|
||||
fish.append(record)
|
||||
return fish
|
||||
|
||||
|
||||
def build_aquarium_html(
|
||||
fish: list[dict[str, Any]],
|
||||
canvas_w: int = 1000,
|
||||
canvas_h: int = 600,
|
||||
) -> str:
|
||||
"""Build the self-contained HTML/JS string for the aquarium canvas.
|
||||
|
||||
The output embeds a click handler: tapping a fish opens an info panel
|
||||
(top-right of the canvas) showing its genome attributes and BFS ancestor
|
||||
levels. Labels are no longer rendered on the canvas itself.
|
||||
"""
|
||||
fish_json = json.dumps(fish)
|
||||
palette_json = json.dumps(STYLE_COLORS)
|
||||
default_color = DEFAULT_COLOR
|
||||
|
||||
# All braces inside <style>/<script> are escaped to literals using {{ }}
|
||||
# so we can use Python f-string substitution for the few JSON payloads.
|
||||
return f"""
|
||||
<div style="position:relative;width:100%;height:{canvas_h}px;">
|
||||
<canvas id="aquarium" width="{canvas_w}" height="{canvas_h}"
|
||||
style="width:100%;height:{canvas_h}px;border-radius:12px;
|
||||
background:linear-gradient(180deg,#0a2540 0%,#1a4d80 100%);
|
||||
display:block;cursor:pointer;"></canvas>
|
||||
<div id="fish-info-panel" style="
|
||||
position:absolute;
|
||||
top:12px;
|
||||
right:12px;
|
||||
width:340px;
|
||||
max-height:580px;
|
||||
overflow-y:auto;
|
||||
background:rgba(8,16,32,0.92);
|
||||
color:#e2e8f0;
|
||||
border-radius:10px;
|
||||
padding:14px 16px;
|
||||
font-family:system-ui,-apple-system,sans-serif;
|
||||
font-size:12px;
|
||||
line-height:1.5;
|
||||
border:1px solid rgba(255,255,255,0.1);
|
||||
backdrop-filter:blur(6px);
|
||||
-webkit-backdrop-filter:blur(6px);
|
||||
display:none;
|
||||
z-index:10;
|
||||
">
|
||||
<div id="fish-info-content"></div>
|
||||
<button id="fish-info-close" style="
|
||||
position:absolute;top:8px;right:10px;
|
||||
background:transparent;color:#94a3b8;border:none;
|
||||
cursor:pointer;font-size:16px;
|
||||
">×</button>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
(function() {{
|
||||
const FISH_DATA = {fish_json};
|
||||
const STYLE_COLORS = {palette_json};
|
||||
const DEFAULT_COLOR = {json.dumps(default_color)};
|
||||
|
||||
const canvas = document.getElementById('aquarium');
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext('2d');
|
||||
const W = canvas.width;
|
||||
const H = canvas.height;
|
||||
|
||||
const panel = document.getElementById('fish-info-panel');
|
||||
const panelContent = document.getElementById('fish-info-content');
|
||||
const closeBtn = document.getElementById('fish-info-close');
|
||||
if (closeBtn) {{
|
||||
closeBtn.addEventListener('click', function() {{
|
||||
panel.style.display = 'none';
|
||||
}});
|
||||
}}
|
||||
|
||||
// Normalize fitness for sizing.
|
||||
let maxFit = 0.0;
|
||||
for (const f of FISH_DATA) {{
|
||||
if (typeof f.fitness === 'number' && f.fitness > maxFit) maxFit = f.fitness;
|
||||
}}
|
||||
|
||||
function lerp(a, b, t) {{ return a + (b - a) * t; }}
|
||||
|
||||
function radiusFor(fitness) {{
|
||||
if (maxFit <= 0) return 8;
|
||||
const t = Math.max(0.05, Math.min(1.0, fitness / maxFit));
|
||||
return lerp(8, 40, t);
|
||||
}}
|
||||
|
||||
function colorFor(style) {{
|
||||
return STYLE_COLORS[style] || DEFAULT_COLOR;
|
||||
}}
|
||||
|
||||
// Init fish state. Each entry keeps a reference to the original data dict
|
||||
// so the click handler can show full attributes + ancestors.
|
||||
const fishState = FISH_DATA.map(function(f, idx) {{
|
||||
const r = radiusFor(f.fitness);
|
||||
return {{
|
||||
data: f,
|
||||
color: colorFor(f.cognitive_style),
|
||||
radius: r,
|
||||
x: Math.random() * (W - 2 * r) + r,
|
||||
y: Math.random() * (H - 2 * r) + r,
|
||||
vx: (Math.random() - 0.5) * 1.5,
|
||||
vy: (Math.random() - 0.5) * 1.0,
|
||||
rank: idx,
|
||||
}};
|
||||
}});
|
||||
|
||||
// Bubbles for ambience.
|
||||
const N_BUBBLES = 25;
|
||||
const bubbles = Array.from({{length: N_BUBBLES}}, function() {{ return {{
|
||||
x: Math.random() * W,
|
||||
y: Math.random() * H,
|
||||
r: 1 + Math.random() * 3,
|
||||
vy: 0.3 + Math.random() * 0.7,
|
||||
}}; }});
|
||||
|
||||
function drawBubble(b) {{
|
||||
ctx.beginPath();
|
||||
ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2);
|
||||
ctx.fillStyle = 'rgba(255,255,255,0.18)';
|
||||
ctx.fill();
|
||||
}}
|
||||
|
||||
function updateBubble(b) {{
|
||||
b.y -= b.vy;
|
||||
if (b.y < -10) {{
|
||||
b.y = H + 5;
|
||||
b.x = Math.random() * W;
|
||||
}}
|
||||
}}
|
||||
|
||||
function drawFish(f) {{
|
||||
const facingLeft = f.vx < 0;
|
||||
ctx.save();
|
||||
ctx.translate(f.x, f.y);
|
||||
if (facingLeft) ctx.scale(-1, 1);
|
||||
|
||||
// Halo for top-3 fish.
|
||||
if (f.rank < 3) {{
|
||||
const grad = ctx.createRadialGradient(0, 0, f.radius * 0.5, 0, 0, f.radius * 2.0);
|
||||
grad.addColorStop(0, f.color + 'aa');
|
||||
grad.addColorStop(1, f.color + '00');
|
||||
ctx.fillStyle = grad;
|
||||
ctx.beginPath();
|
||||
ctx.arc(0, 0, f.radius * 2.0, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
}}
|
||||
|
||||
// Body (ellipse).
|
||||
ctx.fillStyle = f.color;
|
||||
ctx.beginPath();
|
||||
ctx.ellipse(0, 0, f.radius, f.radius * 0.6, 0, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
|
||||
// Tail.
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(-f.radius, 0);
|
||||
ctx.lineTo(-f.radius * 1.6, -f.radius * 0.5);
|
||||
ctx.lineTo(-f.radius * 1.6, f.radius * 0.5);
|
||||
ctx.closePath();
|
||||
ctx.fill();
|
||||
|
||||
// Eye.
|
||||
ctx.fillStyle = '#ffffff';
|
||||
ctx.beginPath();
|
||||
ctx.arc(f.radius * 0.45, -f.radius * 0.15, Math.max(1.5, f.radius * 0.12), 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.fillStyle = '#1a1a1a';
|
||||
ctx.beginPath();
|
||||
ctx.arc(f.radius * 0.50, -f.radius * 0.15, Math.max(0.8, f.radius * 0.06), 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
|
||||
ctx.restore();
|
||||
}}
|
||||
|
||||
function updateFish(f) {{
|
||||
f.vx += (Math.random() - 0.5) * 0.05;
|
||||
f.vy += (Math.random() - 0.5) * 0.05;
|
||||
|
||||
const speed = Math.hypot(f.vx, f.vy);
|
||||
const maxSpeed = 1.5;
|
||||
if (speed > maxSpeed) {{
|
||||
f.vx = (f.vx / speed) * maxSpeed;
|
||||
f.vy = (f.vy / speed) * maxSpeed;
|
||||
}}
|
||||
|
||||
f.x += f.vx;
|
||||
f.y += f.vy;
|
||||
|
||||
if (f.x < f.radius) {{ f.x = f.radius; f.vx = -f.vx; }}
|
||||
if (f.x > W - f.radius) {{ f.x = W - f.radius; f.vx = -f.vx; }}
|
||||
if (f.y < f.radius) {{ f.y = f.radius; f.vy = -f.vy; }}
|
||||
if (f.y > H - f.radius) {{ f.y = H - f.radius; f.vy = -f.vy; }}
|
||||
}}
|
||||
|
||||
function frame() {{
|
||||
ctx.clearRect(0, 0, W, H);
|
||||
|
||||
ctx.strokeStyle = 'rgba(255,255,255,0.04)';
|
||||
ctx.lineWidth = 1;
|
||||
for (let i = 0; i < 6; i++) {{
|
||||
const y = (H / 6) * i + (Date.now() / 50 % (H / 6));
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(0, y);
|
||||
ctx.lineTo(W, y);
|
||||
ctx.stroke();
|
||||
}}
|
||||
|
||||
for (const b of bubbles) {{
|
||||
updateBubble(b);
|
||||
drawBubble(b);
|
||||
}}
|
||||
for (const f of fishState) {{
|
||||
updateFish(f);
|
||||
drawFish(f);
|
||||
}}
|
||||
requestAnimationFrame(frame);
|
||||
}}
|
||||
|
||||
// CLICK HANDLER: hit-test in canvas pixel space (account for CSS scaling).
|
||||
canvas.addEventListener('click', function(e) {{
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const scaleX = canvas.width / rect.width;
|
||||
const scaleY = canvas.height / rect.height;
|
||||
const cx = (e.clientX - rect.left) * scaleX;
|
||||
const cy = (e.clientY - rect.top) * scaleY;
|
||||
let best = null;
|
||||
let bestDist = Infinity;
|
||||
for (const f of fishState) {{
|
||||
const dx = cx - f.x;
|
||||
const dy = cy - f.y;
|
||||
const d = Math.sqrt(dx * dx + dy * dy);
|
||||
const hit = Math.max(f.radius + 6, 14);
|
||||
if (d < hit && d < bestDist) {{
|
||||
best = f;
|
||||
bestDist = d;
|
||||
}}
|
||||
}}
|
||||
if (best) showFishInfo(best);
|
||||
}});
|
||||
|
||||
const ROW_STYLE = 'display:flex;justify-content:space-between;'
|
||||
+ 'padding:2px 0;border-bottom:1px solid rgba(255,255,255,0.05);';
|
||||
const PROMPT_STYLE = 'margin-top:10px;padding:8px;'
|
||||
+ 'background:rgba(255,255,255,0.04);border-radius:6px;'
|
||||
+ 'font-size:11px;font-style:italic;color:#cbd5e1;';
|
||||
const ANC_HEAD_STYLE = 'margin:14px 0 6px 0;color:#94a3b8;'
|
||||
+ 'text-transform:uppercase;font-size:10px;letter-spacing:1px;';
|
||||
const ANC_ROW_STYLE = 'display:flex;align-items:center;padding:4px 6px;'
|
||||
+ 'margin-bottom:2px;background:rgba(255,255,255,0.03);'
|
||||
+ 'border-radius:4px;border-left:3px solid ';
|
||||
const NO_ANC_STYLE = 'margin-top:10px;font-size:10px;color:#64748b;';
|
||||
const DASH = '\\u2014';
|
||||
|
||||
function metricRow(label, value) {{
|
||||
return '<div style="' + ROW_STYLE + '">'
|
||||
+ '<span style="color:#94a3b8;">' + label + '</span>'
|
||||
+ '<span style="color:#e2e8f0;">' + value + '</span></div>';
|
||||
}}
|
||||
|
||||
function escapeHtml(s) {{
|
||||
const div = document.createElement('div');
|
||||
div.appendChild(document.createTextNode(String(s)));
|
||||
return div.innerHTML;
|
||||
}}
|
||||
|
||||
function fmt(v, dp) {{
|
||||
if (v === null || v === undefined || typeof v !== 'number' || isNaN(v)) {{
|
||||
return DASH;
|
||||
}}
|
||||
return v.toFixed(dp);
|
||||
}}
|
||||
|
||||
function showFishInfo(fish) {{
|
||||
const data = fish.data;
|
||||
const styleColor = STYLE_COLORS[data.cognitive_style] || DEFAULT_COLOR;
|
||||
let html = '';
|
||||
const idShort = String(data.id || '').slice(0, 8);
|
||||
html += '<h4 style="margin:0 0 10px 0;color:' + styleColor + ';">';
|
||||
html += escapeHtml(idShort)
|
||||
+ ' <span style="color:#94a3b8;font-weight:normal;font-size:11px;">'
|
||||
+ 'gen ' + escapeHtml(data.generation) + '</span>';
|
||||
html += '</h4>';
|
||||
html += metricRow('fitness', fmt(data.fitness, 3));
|
||||
html += metricRow('DSR', fmt(data.dsr, 3));
|
||||
html += metricRow('Sharpe', fmt(data.sharpe, 3));
|
||||
html += metricRow('max DD', fmt(data.max_dd, 3));
|
||||
const trades = data.n_trades == null ? 0 : data.n_trades;
|
||||
html += metricRow('trades', escapeHtml(trades));
|
||||
html += metricRow('style', escapeHtml(data.cognitive_style || DASH));
|
||||
html += metricRow('tier', escapeHtml(data.model_tier || DASH));
|
||||
html += metricRow('temp', fmt(data.temperature, 2));
|
||||
const lookback = data.lookback_window == null ? DASH : data.lookback_window;
|
||||
html += metricRow('lookback', escapeHtml(lookback));
|
||||
const feats = (data.feature_access || []).join(', ');
|
||||
html += metricRow('features', escapeHtml(feats || DASH));
|
||||
if (data.system_prompt) {{
|
||||
html += '<div style="' + PROMPT_STYLE + '">'
|
||||
+ escapeHtml(data.system_prompt) + '</div>';
|
||||
}}
|
||||
if (data.ancestors && data.ancestors.length > 0) {{
|
||||
html += '<h5 style="' + ANC_HEAD_STYLE + '">Discendenza</h5>';
|
||||
data.ancestors.forEach(function(level, idx) {{
|
||||
html += '<div style="margin-bottom:8px;">';
|
||||
html += '<div style="font-size:10px;color:#64748b;margin-bottom:4px;">'
|
||||
+ 'Gen N\\u2212' + (idx + 1) + '</div>';
|
||||
level.forEach(function(ancestor) {{
|
||||
const c = STYLE_COLORS[ancestor.cognitive_style] || DEFAULT_COLOR;
|
||||
const aShort = String(ancestor.id || '').slice(0, 8);
|
||||
html += '<div style="' + ANC_ROW_STYLE + c + ';">';
|
||||
html += '<code style="color:' + c + ';font-size:10px;">'
|
||||
+ escapeHtml(aShort) + '</code>';
|
||||
const af = ancestor.fitness;
|
||||
const fitTxt = (typeof af === 'number' && !isNaN(af))
|
||||
? af.toFixed(2) : DASH;
|
||||
html += '<span style="margin-left:auto;color:#94a3b8;font-size:10px;">'
|
||||
+ 'fit ' + fitTxt + '</span>';
|
||||
html += '</div>';
|
||||
}});
|
||||
html += '</div>';
|
||||
}});
|
||||
}} else {{
|
||||
html += '<div style="' + NO_ANC_STYLE + '">'
|
||||
+ 'Genoma di prima generazione (no ancestors)</div>';
|
||||
}}
|
||||
panelContent.innerHTML = html;
|
||||
panel.style.display = 'block';
|
||||
}}
|
||||
|
||||
if (fishState.length === 0) {{
|
||||
ctx.fillStyle = 'rgba(255,255,255,0.7)';
|
||||
ctx.font = '16px sans-serif';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.fillText('Acquario vuoto: nessun genoma da mostrare.', W / 2, H / 2);
|
||||
}} else {{
|
||||
requestAnimationFrame(frame);
|
||||
}}
|
||||
}})();
|
||||
</script>
|
||||
"""
|
||||
@@ -1,47 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import streamlit as st
|
||||
|
||||
from multi_swarm.dashboard.data import (
|
||||
evaluations_df,
|
||||
get_repo,
|
||||
get_run_overview,
|
||||
list_runs_df,
|
||||
)
|
||||
|
||||
st.title("Overview")
|
||||
|
||||
db_path = st.session_state.get("db_path", "./runs.db")
|
||||
repo = get_repo(db_path)
|
||||
|
||||
runs = list_runs_df(repo)
|
||||
if runs.empty:
|
||||
st.info("Nessuna run nel database. Esegui `scripts/run_phase1.py` per generarne una.")
|
||||
st.stop()
|
||||
|
||||
st.subheader("Tutte le run")
|
||||
st.dataframe(runs[["id", "name", "started_at", "completed_at", "status", "total_cost_usd"]])
|
||||
|
||||
selected = st.selectbox("Seleziona run per dettaglio", runs["id"].tolist())
|
||||
overview = get_run_overview(repo, selected)
|
||||
|
||||
col1, col2, col3, col4 = st.columns(4)
|
||||
col1.metric("Status", overview["status"])
|
||||
col2.metric("Cost (USD)", f"{overview['total_cost_usd']:.4f}")
|
||||
col3.metric("Started", overview["started_at"])
|
||||
col4.metric("Completed", overview["completed_at"] or "—")
|
||||
|
||||
st.subheader("Statistiche evaluations")
|
||||
evals = evaluations_df(repo, selected)
|
||||
col5, col6, col7, col8 = st.columns(4)
|
||||
if not evals.empty:
|
||||
parse_success = 100 * (evals["parse_error"].isna().sum() / len(evals))
|
||||
col5.metric("Evaluations totali", len(evals))
|
||||
col6.metric("Parse success %", f"{parse_success:.1f}%")
|
||||
col7.metric("Top fitness", f"{evals['fitness'].max():.3f}")
|
||||
col8.metric("Median fitness", f"{evals['fitness'].median():.3f}")
|
||||
else:
|
||||
col5.metric("Evaluations totali", 0)
|
||||
|
||||
st.subheader("Config")
|
||||
st.json(overview["config"])
|
||||
@@ -1,68 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import plotly.graph_objects as go # type: ignore[import-untyped]
|
||||
import streamlit as st
|
||||
|
||||
from multi_swarm.dashboard.data import generations_df, get_repo, list_runs_df
|
||||
|
||||
st.title("GA Convergence")
|
||||
|
||||
db_path = st.session_state.get("db_path", "./runs.db")
|
||||
repo = get_repo(db_path)
|
||||
|
||||
runs = list_runs_df(repo)
|
||||
if runs.empty:
|
||||
st.info("Nessuna run.")
|
||||
st.stop()
|
||||
|
||||
selected = st.selectbox("Run", runs["id"].tolist())
|
||||
gens = generations_df(repo, selected)
|
||||
if gens.empty:
|
||||
st.warning("Nessuna generazione registrata per questa run.")
|
||||
st.stop()
|
||||
|
||||
fig = go.Figure()
|
||||
fig.add_trace(
|
||||
go.Scatter(
|
||||
x=gens["generation_idx"],
|
||||
y=gens["fitness_median"],
|
||||
name="median",
|
||||
mode="lines+markers",
|
||||
)
|
||||
)
|
||||
fig.add_trace(
|
||||
go.Scatter(
|
||||
x=gens["generation_idx"],
|
||||
y=gens["fitness_max"],
|
||||
name="max",
|
||||
mode="lines+markers",
|
||||
)
|
||||
)
|
||||
fig.add_trace(
|
||||
go.Scatter(
|
||||
x=gens["generation_idx"],
|
||||
y=gens["fitness_p90"],
|
||||
name="p90",
|
||||
mode="lines+markers",
|
||||
)
|
||||
)
|
||||
fig.update_layout(
|
||||
xaxis_title="generation",
|
||||
yaxis_title="fitness",
|
||||
title="Fitness convergence",
|
||||
)
|
||||
st.plotly_chart(fig, use_container_width=True)
|
||||
|
||||
st.subheader("Entropy")
|
||||
fig2 = go.Figure()
|
||||
fig2.add_trace(go.Scatter(x=gens["generation_idx"], y=gens["entropy"], mode="lines+markers"))
|
||||
fig2.add_hline(y=0.5, line_dash="dash", annotation_text="gate threshold (0.5)")
|
||||
fig2.update_layout(
|
||||
xaxis_title="generation",
|
||||
yaxis_title="entropy",
|
||||
title="Diversity (fitness entropy)",
|
||||
)
|
||||
st.plotly_chart(fig2, use_container_width=True)
|
||||
|
||||
st.subheader("Tabella generazioni")
|
||||
st.dataframe(gens)
|
||||
@@ -1,72 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import streamlit as st
|
||||
|
||||
from multi_swarm.dashboard.data import (
|
||||
evaluations_df,
|
||||
genomes_df,
|
||||
get_repo,
|
||||
list_runs_df,
|
||||
)
|
||||
|
||||
st.title("Genomes")
|
||||
|
||||
db_path = st.session_state.get("db_path", "./runs.db")
|
||||
repo = get_repo(db_path)
|
||||
|
||||
runs = list_runs_df(repo)
|
||||
if runs.empty:
|
||||
st.info("Nessuna run.")
|
||||
st.stop()
|
||||
|
||||
selected = st.selectbox("Run", runs["id"].tolist())
|
||||
evals = evaluations_df(repo, selected)
|
||||
genomes = genomes_df(repo, selected)
|
||||
|
||||
if evals.empty:
|
||||
st.warning("Nessuna evaluation.")
|
||||
st.stop()
|
||||
|
||||
merged = evals.merge(
|
||||
genomes, left_on="genome_id", right_on="id", how="left", suffixes=("", "_g")
|
||||
)
|
||||
top = merged.sort_values("fitness", ascending=False).head(10)
|
||||
|
||||
st.subheader("Top-10 genomi (per fitness)")
|
||||
display_cols = [
|
||||
"genome_id",
|
||||
"fitness",
|
||||
"dsr",
|
||||
"sharpe",
|
||||
"max_dd",
|
||||
"n_trades",
|
||||
"cognitive_style",
|
||||
"temperature",
|
||||
"lookback_window",
|
||||
"feature_access",
|
||||
]
|
||||
existing = [c for c in display_cols if c in top.columns]
|
||||
st.dataframe(top[existing])
|
||||
|
||||
st.subheader("Ispezione genoma")
|
||||
gid = st.selectbox("Seleziona genome_id", top["genome_id"].tolist())
|
||||
row = merged[merged["genome_id"] == gid].iloc[0]
|
||||
|
||||
col1, col2 = st.columns(2)
|
||||
with col1:
|
||||
st.metric("fitness", f"{row['fitness']:.3f}")
|
||||
st.metric("DSR", f"{row['dsr']:.3f}")
|
||||
st.metric("Sharpe", f"{row['sharpe']:.3f}")
|
||||
with col2:
|
||||
st.metric("max DD", f"{row['max_dd']:.3f}")
|
||||
st.metric("trades", int(row["n_trades"]))
|
||||
st.metric("style", str(row.get("cognitive_style", "—")))
|
||||
|
||||
st.subheader("System prompt")
|
||||
st.code(row.get("system_prompt", "—"))
|
||||
|
||||
st.subheader("Raw LLM output")
|
||||
st.code(row.get("raw_text", "—"))
|
||||
|
||||
if row.get("parse_error"):
|
||||
st.error(f"Parse error: {row['parse_error']}")
|
||||
@@ -1,87 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import streamlit as st
|
||||
import streamlit.components.v1 as components
|
||||
|
||||
from multi_swarm.dashboard.aquarium import (
|
||||
STYLE_COLORS,
|
||||
build_aquarium_html,
|
||||
build_fish_dataset,
|
||||
build_lineage_index,
|
||||
)
|
||||
from multi_swarm.dashboard.data import (
|
||||
evaluations_df,
|
||||
genomes_df,
|
||||
get_repo,
|
||||
list_runs_df,
|
||||
)
|
||||
|
||||
st.title("Aquarium 2D")
|
||||
st.caption(
|
||||
"Pesci colorati per stile cognitivo, dimensione proporzionale a fitness. "
|
||||
"Click su un pesce per dettaglio + discendenza."
|
||||
)
|
||||
|
||||
db_path = st.session_state.get("db_path", "./runs.db")
|
||||
repo = get_repo(db_path)
|
||||
|
||||
runs = list_runs_df(repo)
|
||||
if runs.empty:
|
||||
st.info("Nessuna run nel database.")
|
||||
st.stop()
|
||||
|
||||
selected_run = st.selectbox("Run", runs["id"].tolist())
|
||||
|
||||
# Fetch ALL genomes of the run (no gen filter): needed to build the lineage
|
||||
# index across generations. The active set is filtered afterwards.
|
||||
all_genomes = genomes_df(repo, selected_run)
|
||||
all_evals = evaluations_df(repo, selected_run)
|
||||
|
||||
if all_genomes.empty:
|
||||
st.warning("Nessun genoma per questa run.")
|
||||
st.stop()
|
||||
|
||||
available_gens = sorted(all_genomes["generation_idx"].unique().tolist())
|
||||
selected_gen = st.selectbox(
|
||||
"Generazione",
|
||||
available_gens,
|
||||
index=len(available_gens) - 1, # default ultima
|
||||
)
|
||||
|
||||
active_genomes = all_genomes[all_genomes["generation_idx"] == selected_gen]
|
||||
active_evals = (
|
||||
all_evals[all_evals["genome_id"].isin(active_genomes["id"])]
|
||||
if not all_evals.empty
|
||||
else all_evals
|
||||
)
|
||||
if not active_evals.empty:
|
||||
active_merged = active_genomes.merge(
|
||||
active_evals,
|
||||
left_on="id",
|
||||
right_on="genome_id",
|
||||
how="left",
|
||||
suffixes=("", "_eval"),
|
||||
)
|
||||
else:
|
||||
active_merged = active_genomes.copy()
|
||||
active_merged["genome_id"] = active_merged["id"]
|
||||
|
||||
lineage = build_lineage_index(all_genomes, all_evals)
|
||||
fish = build_fish_dataset(active_merged, lineage, max_lineage_levels=5)
|
||||
|
||||
if not fish:
|
||||
st.warning("Nessun agente attivo in questa generazione.")
|
||||
st.stop()
|
||||
|
||||
st.caption(f"{len(fish)} agenti in generazione {selected_gen}")
|
||||
|
||||
html_str = build_aquarium_html(fish, canvas_w=1000, canvas_h=600)
|
||||
components.html(html_str, height=620, scrolling=False)
|
||||
|
||||
with st.expander("Legenda colori"):
|
||||
legend_md = "\n".join(
|
||||
f"- <span style='color:{color};font-weight:bold;'>●</span> "
|
||||
f"`{style}`"
|
||||
for style, color in STYLE_COLORS.items()
|
||||
)
|
||||
st.markdown(legend_md, unsafe_allow_html=True)
|
||||
@@ -1,22 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import streamlit as st
|
||||
|
||||
st.set_page_config(page_title="Multi-Swarm Phase 1", layout="wide")
|
||||
st.title("Multi-Swarm Coevolutivo — Phase 1 dashboard")
|
||||
st.markdown(
|
||||
"""
|
||||
Naviga le pagine nel menu a sinistra:
|
||||
- **Overview**: ultima run e stato globale.
|
||||
- **GA Convergence**: fitness per generazione.
|
||||
- **Genomes**: top-K genomi e ispezione qualitativa.
|
||||
- **Aquarium**: visualizzazione 2D dei genomi come pesci animati.
|
||||
"""
|
||||
)
|
||||
|
||||
db_path = os.environ.get("DB_PATH", "./runs.db")
|
||||
st.session_state["db_path"] = db_path
|
||||
st.caption(f"DB path: `{Path(db_path).resolve()}`")
|
||||
@@ -1,71 +0,0 @@
|
||||
"""Fitness function v1 della Phase 1.
|
||||
|
||||
Combina :class:`FalsificationReport` (metriche di robustezza) e
|
||||
:class:`AdversarialReport` (findings euristici) in uno scalare ``>= 0`` che il
|
||||
GA usa per selezione e ranking.
|
||||
|
||||
Versione v1: rispetto alla v0 (DSR meno penalita' lineare di drawdown, clamp
|
||||
a zero) la formula e' continua e quasi sempre strettamente positiva, in modo
|
||||
da fornire un gradient anche su strategie mediocri o con Sharpe negativo.
|
||||
Restano due kill-switch hard (no-trade, finding HIGH adversarial) che azzerano
|
||||
la fitness.
|
||||
|
||||
Formula::
|
||||
|
||||
sharpe_norm = 0.5 * (tanh(sharpe) + 1.0) # in [0, 1]
|
||||
base = dsr_weight * dsr + sharpe_weight * sharpe_norm
|
||||
penalty = 1.0 / (1.0 + drawdown_penalty * max_drawdown)
|
||||
fitness = max(0.0, base * penalty)
|
||||
|
||||
Con i default ``dsr_weight = sharpe_weight = 0.5`` la base e' in ``[0, 1]`` e
|
||||
``penalty`` in ``(0, 1]``: fitness e' bounded in ``[0, 1]`` per input sani e
|
||||
mai esattamente zero finche' Sharpe e' finito e ``max_dd`` finito.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
from ..agents.adversarial import AdversarialReport, Severity
|
||||
from ..agents.falsification import FalsificationReport
|
||||
|
||||
|
||||
def compute_fitness(
|
||||
falsification: FalsificationReport,
|
||||
adversarial: AdversarialReport,
|
||||
drawdown_penalty: float = 1.0,
|
||||
dsr_weight: float = 0.5,
|
||||
sharpe_weight: float = 0.5,
|
||||
) -> float:
|
||||
"""Calcola la fitness scalare di una strategia (v1, continua).
|
||||
|
||||
Args:
|
||||
falsification: report con DSR, Sharpe, max_drawdown, n_trades.
|
||||
adversarial: report con eventuali findings euristici.
|
||||
drawdown_penalty: peso del max drawdown nel denominatore della
|
||||
penalita' moltiplicativa (default 1.0). Valori piu' alti
|
||||
penalizzano piu' severamente strategie con DD alto.
|
||||
dsr_weight: peso del DSR nella base (default 0.5).
|
||||
sharpe_weight: peso dello Sharpe normalizzato nella base
|
||||
(default 0.5).
|
||||
|
||||
Returns:
|
||||
Fitness ``>= 0``. Zero indica strategia da scartare (no-trade o
|
||||
kill adversarial). Valori tipici per strategie sane: ``[0.05, 1.0]``.
|
||||
|
||||
Logica:
|
||||
1. ``n_trades == 0`` → 0 (nessuna evidenza, sega subito).
|
||||
2. Almeno un finding ``HIGH`` adversarial → 0 (kill).
|
||||
3. Altrimenti combina DSR e ``tanh(sharpe)`` normalizzato in
|
||||
``[0, 1]``, modulato da una penalita' continua del drawdown
|
||||
``1 / (1 + k * max_dd)``.
|
||||
"""
|
||||
if falsification.n_trades == 0:
|
||||
return 0.0
|
||||
if any(f.severity == Severity.HIGH for f in adversarial.findings):
|
||||
return 0.0
|
||||
dsr = max(0.0, min(1.0, float(falsification.dsr)))
|
||||
sharpe_norm = 0.5 * (math.tanh(float(falsification.sharpe)) + 1.0)
|
||||
base = dsr_weight * dsr + sharpe_weight * sharpe_norm
|
||||
penalty = 1.0 / (1.0 + drawdown_penalty * float(falsification.max_drawdown))
|
||||
return max(0.0, float(base * penalty))
|
||||
@@ -1,99 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import openai
|
||||
from openai import OpenAI
|
||||
from tenacity import (
|
||||
retry,
|
||||
retry_if_exception_type,
|
||||
stop_after_attempt,
|
||||
wait_exponential,
|
||||
)
|
||||
|
||||
from ..genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
|
||||
# Modelli configurati per Phase 1 — tutti via OpenRouter
|
||||
MODEL_TIER_S = "anthropic/claude-opus-4-7"
|
||||
MODEL_TIER_A = "anthropic/claude-sonnet-4-6"
|
||||
MODEL_TIER_B = "anthropic/claude-sonnet-4-6"
|
||||
MODEL_TIER_C = "qwen/qwen-2.5-72b-instruct"
|
||||
MODEL_TIER_D = "meta-llama/llama-3.3-70b-instruct"
|
||||
OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1"
|
||||
|
||||
# Errori transient: retry. RateLimit/Auth/InvalidRequest: NO retry.
|
||||
_RETRYABLE_EXCEPTIONS: tuple[type[BaseException], ...] = (
|
||||
openai.APIConnectionError,
|
||||
openai.APITimeoutError,
|
||||
openai.InternalServerError,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CompletionResult:
|
||||
text: str
|
||||
input_tokens: int
|
||||
output_tokens: int
|
||||
tier: ModelTier
|
||||
model: str
|
||||
|
||||
|
||||
class LLMClient:
|
||||
def __init__(
|
||||
self,
|
||||
openrouter_api_key: str,
|
||||
model_tier_s: str = MODEL_TIER_S,
|
||||
model_tier_a: str = MODEL_TIER_A,
|
||||
model_tier_b: str = MODEL_TIER_B,
|
||||
model_tier_c: str = MODEL_TIER_C,
|
||||
model_tier_d: str = MODEL_TIER_D,
|
||||
openrouter_base_url: str = OPENROUTER_BASE_URL,
|
||||
) -> None:
|
||||
self.model_tier_s = model_tier_s
|
||||
self.model_tier_a = model_tier_a
|
||||
self.model_tier_b = model_tier_b
|
||||
self.model_tier_c = model_tier_c
|
||||
self.model_tier_d = model_tier_d
|
||||
self.openrouter_base_url = openrouter_base_url
|
||||
self._tier_models: dict[ModelTier, str] = {
|
||||
ModelTier.S: model_tier_s,
|
||||
ModelTier.A: model_tier_a,
|
||||
ModelTier.B: model_tier_b,
|
||||
ModelTier.C: model_tier_c,
|
||||
ModelTier.D: model_tier_d,
|
||||
}
|
||||
self._client = OpenAI(api_key=openrouter_api_key, base_url=openrouter_base_url)
|
||||
|
||||
@retry(
|
||||
stop=stop_after_attempt(3),
|
||||
wait=wait_exponential(multiplier=1.0, min=2.0, max=10.0),
|
||||
retry=retry_if_exception_type(_RETRYABLE_EXCEPTIONS),
|
||||
reraise=True,
|
||||
)
|
||||
def complete(
|
||||
self,
|
||||
genome: HypothesisAgentGenome,
|
||||
system: str,
|
||||
user: str,
|
||||
max_tokens: int = 2000,
|
||||
) -> CompletionResult:
|
||||
model = self._tier_models[genome.model_tier]
|
||||
resp = self._client.chat.completions.create(
|
||||
model=model,
|
||||
messages=[
|
||||
{"role": "system", "content": system},
|
||||
{"role": "user", "content": user},
|
||||
],
|
||||
temperature=genome.temperature,
|
||||
top_p=genome.top_p,
|
||||
max_tokens=max_tokens,
|
||||
)
|
||||
usage = resp.usage
|
||||
assert usage is not None
|
||||
return CompletionResult(
|
||||
text=resp.choices[0].message.content or "",
|
||||
input_tokens=usage.prompt_tokens,
|
||||
output_tokens=usage.completion_tokens,
|
||||
tier=genome.model_tier,
|
||||
model=model,
|
||||
)
|
||||
@@ -1,27 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
|
||||
def sharpe_ratio(returns: pd.Series, periods_per_year: int = 8760, rf: float = 0.0) -> float:
|
||||
"""Sharpe annualizzato. periods_per_year=8760 per dati orari."""
|
||||
excess = returns - rf / periods_per_year
|
||||
std = excess.std(ddof=1)
|
||||
if std == 0 or np.isnan(std):
|
||||
return 0.0
|
||||
return float(np.sqrt(periods_per_year) * excess.mean() / std)
|
||||
|
||||
|
||||
def max_drawdown(equity: pd.Series) -> float:
|
||||
"""Max drawdown percentuale (positivo)."""
|
||||
peak = equity.cummax()
|
||||
dd = (peak - equity) / peak.replace(0, np.nan)
|
||||
dd = dd.fillna(0.0)
|
||||
return float(dd.max())
|
||||
|
||||
|
||||
def total_return(equity: pd.Series) -> float:
|
||||
if equity.iloc[0] == 0:
|
||||
return float(equity.iloc[-1])
|
||||
return float(equity.iloc[-1] / equity.iloc[0] - 1.0)
|
||||
@@ -1,186 +0,0 @@
|
||||
"""End-to-end orchestrator per un run di Phase 1.
|
||||
|
||||
Pipeline per ogni generazione:
|
||||
|
||||
population -> hypothesis_agent.propose -> falsification + adversarial
|
||||
-> compute_fitness -> persistenza -> next_generation
|
||||
|
||||
Tutto e' loggato sulla repository SQLite (runs, generations, genomes,
|
||||
evaluations, cost_records, adversarial_findings) cosi' che la GUI Streamlit
|
||||
possa leggere lo stato a run terminato (o in corso).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from ..agents.adversarial import AdversarialAgent
|
||||
from ..agents.falsification import FalsificationAgent
|
||||
from ..agents.hypothesis import HypothesisAgent
|
||||
from ..agents.market_summary import build_market_summary
|
||||
from ..ga.fitness import compute_fitness
|
||||
from ..ga.initial import build_initial_population
|
||||
from ..ga.loop import GAConfig, next_generation
|
||||
from ..ga.summary import generation_summary
|
||||
from ..genome.hypothesis import ModelTier
|
||||
from ..llm.client import LLMClient
|
||||
from ..llm.cost_tracker import CostTracker
|
||||
from ..persistence.repository import Repository
|
||||
|
||||
|
||||
@dataclass
|
||||
class RunConfig:
|
||||
"""Parametri di un run end-to-end della Phase 1."""
|
||||
|
||||
run_name: str
|
||||
population_size: int = 20
|
||||
n_generations: int = 10
|
||||
elite_k: int = 2
|
||||
tournament_k: int = 3
|
||||
p_crossover: float = 0.5
|
||||
seed: int = 42
|
||||
model_tier: ModelTier = ModelTier.C
|
||||
symbol: str = "BTC/USDT"
|
||||
timeframe: str = "1h"
|
||||
fees_bp: float = 5.0
|
||||
n_trials_dsr: int = 50
|
||||
db_path: Path = field(default_factory=lambda: Path("./runs.db"))
|
||||
|
||||
|
||||
def run_phase1(
|
||||
cfg: RunConfig,
|
||||
ohlcv: pd.DataFrame,
|
||||
llm: LLMClient,
|
||||
) -> str:
|
||||
"""Esegue il loop GA end-to-end e ritorna l'``id`` del run.
|
||||
|
||||
Su qualunque eccezione marca il run come ``failed`` e rilancia.
|
||||
"""
|
||||
rng = random.Random(cfg.seed)
|
||||
|
||||
repo = Repository(cfg.db_path)
|
||||
repo.init_schema()
|
||||
config_dict = {
|
||||
**cfg.__dict__,
|
||||
"db_path": str(cfg.db_path),
|
||||
"model_tier": cfg.model_tier.value,
|
||||
}
|
||||
run_id = repo.create_run(name=cfg.run_name, config=config_dict)
|
||||
|
||||
market = build_market_summary(ohlcv, symbol=cfg.symbol, timeframe=cfg.timeframe)
|
||||
|
||||
hypothesis_agent = HypothesisAgent(llm=llm)
|
||||
falsification_agent = FalsificationAgent(
|
||||
fees_bp=cfg.fees_bp, n_trials_dsr=cfg.n_trials_dsr
|
||||
)
|
||||
adversarial_agent = AdversarialAgent(fees_bp=cfg.fees_bp)
|
||||
cost_tracker = CostTracker()
|
||||
|
||||
population = build_initial_population(
|
||||
k=cfg.population_size, model_tier=cfg.model_tier, rng=rng
|
||||
)
|
||||
fitnesses: dict[str, float] = {}
|
||||
|
||||
ga_cfg = GAConfig(
|
||||
population_size=cfg.population_size,
|
||||
elite_k=cfg.elite_k,
|
||||
tournament_k=cfg.tournament_k,
|
||||
p_crossover=cfg.p_crossover,
|
||||
)
|
||||
|
||||
try:
|
||||
for gen in range(cfg.n_generations):
|
||||
for genome in population:
|
||||
if genome.id in fitnesses:
|
||||
continue # elite gia' valutata in generazione precedente
|
||||
repo.save_genome(run_id=run_id, generation_idx=gen, genome=genome)
|
||||
proposal = hypothesis_agent.propose(genome, market)
|
||||
# Registra costo per OGNI completion (incluse retry).
|
||||
for completion in proposal.completions:
|
||||
cost_record = cost_tracker.record(
|
||||
input_tokens=completion.input_tokens,
|
||||
output_tokens=completion.output_tokens,
|
||||
tier=completion.tier,
|
||||
run_id=run_id,
|
||||
agent_id=genome.id,
|
||||
)
|
||||
repo.save_cost_record(
|
||||
run_id=run_id,
|
||||
agent_id=genome.id,
|
||||
tier=cost_record.tier.value,
|
||||
input_tokens=cost_record.input_tokens,
|
||||
output_tokens=cost_record.output_tokens,
|
||||
cost_usd=cost_record.cost_usd,
|
||||
)
|
||||
|
||||
if proposal.strategy is None:
|
||||
repo.save_evaluation(
|
||||
run_id=run_id,
|
||||
genome_id=genome.id,
|
||||
fitness=0.0,
|
||||
dsr=0.0,
|
||||
dsr_pvalue=1.0,
|
||||
sharpe=0.0,
|
||||
max_dd=0.0,
|
||||
total_return=0.0,
|
||||
n_trades=0,
|
||||
parse_error=proposal.parse_error,
|
||||
raw_text=proposal.raw_text,
|
||||
)
|
||||
fitnesses[genome.id] = 0.0
|
||||
continue
|
||||
|
||||
fals = falsification_agent.evaluate(proposal.strategy, ohlcv)
|
||||
adv = adversarial_agent.review(proposal.strategy, ohlcv)
|
||||
for finding in adv.findings:
|
||||
repo.save_adversarial_finding(
|
||||
run_id=run_id,
|
||||
genome_id=genome.id,
|
||||
name=finding.name,
|
||||
severity=finding.severity.value,
|
||||
detail=finding.detail,
|
||||
)
|
||||
fit = compute_fitness(fals, adv)
|
||||
repo.save_evaluation(
|
||||
run_id=run_id,
|
||||
genome_id=genome.id,
|
||||
fitness=fit,
|
||||
dsr=fals.dsr,
|
||||
dsr_pvalue=fals.dsr_pvalue,
|
||||
sharpe=fals.sharpe,
|
||||
max_dd=fals.max_drawdown,
|
||||
total_return=fals.total_return,
|
||||
n_trades=fals.n_trades,
|
||||
parse_error=None,
|
||||
raw_text=proposal.raw_text,
|
||||
)
|
||||
fitnesses[genome.id] = fit
|
||||
|
||||
gen_fitnesses = [fitnesses[g.id] for g in population]
|
||||
summary = generation_summary(gen_fitnesses, n_bins=10)
|
||||
repo.save_generation_summary(
|
||||
run_id=run_id,
|
||||
generation_idx=gen,
|
||||
n_genomes=len(population),
|
||||
fitness_median=summary["median"],
|
||||
fitness_max=summary["max"],
|
||||
fitness_p90=summary["p90"],
|
||||
entropy=summary["entropy"],
|
||||
)
|
||||
|
||||
if gen < cfg.n_generations - 1:
|
||||
population = next_generation(population, fitnesses, ga_cfg, rng)
|
||||
|
||||
repo.complete_run(
|
||||
run_id, total_cost=repo.total_cost(run_id), status="completed"
|
||||
)
|
||||
return run_id
|
||||
except Exception:
|
||||
repo.complete_run(
|
||||
run_id, total_cost=repo.total_cost(run_id), status="failed"
|
||||
)
|
||||
raise
|
||||
@@ -0,0 +1,117 @@
|
||||
# Phase 1.5 — Run nemotron tier C — Decision Memo
|
||||
|
||||
**Data**: 11 maggio 2026
|
||||
**Run di riferimento**: `phase1.5-nemotron-001` (id `434c417e2b6f42bb8cf32514e5d0db1d`)
|
||||
**Tier LLM**: C → `nvidia/nemotron-3-super-120b-a12b:free`
|
||||
**Durata wallclock**: 2 h 26 min (08:15 → 10:11 UTC, gen 0 → gen 9)
|
||||
**Spesa totale**: $0.1244 (price-table tier C; il modello effettivo è `:free` su OpenRouter, ma il cost tracker applica la pricing nominale del tier)
|
||||
**Status**: ✅ Completato, ma esito strategico **NO-GO** sulla configurazione corrente
|
||||
|
||||
---
|
||||
|
||||
## 1. Premessa
|
||||
|
||||
Il run `phase1.5-nemotron-001` è la prima esecuzione end-to-end del loop GA con:
|
||||
|
||||
- l'Adversarial layer aggiornato in Phase 1.5 (commits `56a631f` + `d3662f6`), con tre nuovi check HIGH (`flat_too_long`, `fees_eat_alpha`, `time_in_market_too_high`) più i due esistenti rinforzati;
|
||||
- il tier C ribindato a `nvidia/nemotron-3-super-120b-a12b:free`, modello scelto in benchmark contro sette alternative per stabilità JSON e costo nullo;
|
||||
- il fix `EmptyCompletionError` su `llm/client.py` (commit `9d0deb3`) introdotto durante la stessa sessione per gestire le risposte vuote che alcuni provider `:free` ritornano sporadicamente.
|
||||
|
||||
L'obiettivo dichiarato del run era verificare se il nuovo budget di vincoli adversarial — più stretto del v5 — fosse compatibile con la capacità generativa di nemotron, e se la popolazione riuscisse a esplorare una zona di fitness positiva non degenere.
|
||||
|
||||
---
|
||||
|
||||
## 2. Hard gate Phase 1 — ripercorrenza
|
||||
|
||||
I 5 hard gate originali (definiti nello spec strategico di Phase 1) sono stati rivalutati su questo run come sanity check, non come passaggio formale di gate.
|
||||
|
||||
| # | Gate | Soglia | Misura | Esito |
|
||||
|---|------|--------|--------|-------|
|
||||
| 1 | Loop converge | mediana cresce ≥3 gen consecutive | Gen 0→8: median oscilla tra 0.0 e 0.0073 senza crescita strutturale | ❌ FAIL |
|
||||
| 2 | Parse success | ≥80% proposte LLM parse-OK | 81/89 = **91.0%** | ✅ PASS |
|
||||
| 3 | Top-5 ratio | top-5 fitness ≥10× mediana | top-5 = 0.0162–0.0215; mediana ≈ 0 → ratio indefinito | ⚠️ N/A |
|
||||
| 4 | Entropy | ≥0.5 a fine run | 0.845 alla gen 9 | ✅ PASS |
|
||||
| 5 | Budget | costo ≤ cap | $0.1244 vs cap $700 (0.02%) | ✅ PASS |
|
||||
|
||||
Il gate critico è il numero 1. La popolazione non converge: il `max_fitness` resta inchiodato a `0.0215` dalla generazione 0 fino alla 9, segnale che l'elite preservation cattura un singolo genoma poco peggiore degli altri ma altrettanto inadatto, mentre il resto della popolazione non riesce a superarlo. La mediana è zero in 9 generazioni su 10 (singolo picco a 0.0073 in gen 8).
|
||||
|
||||
---
|
||||
|
||||
## 3. Lettura dei top genomi
|
||||
|
||||
I cinque genomi a fitness più alta hanno tutti caratteristiche economicamente disastrose:
|
||||
|
||||
| Genome ID | Fitness | DSR | Sharpe | Total return | n_trades |
|
||||
|-----------|---------|-----|--------|--------------|----------|
|
||||
| `0e1f9d7af25cfd6a` | 0.0215 | 0.000 | −1.083 | −115.9% | 385 |
|
||||
| `85a8116ab2cd2735` | 0.0215 | 0.000 | −1.083 | −115.9% | 385 |
|
||||
| `92aae563277b6f21` | 0.0193 | 0.000 | −1.129 | −131.0% | 597 |
|
||||
| `01d0ca99bbdd7320` | 0.0180 | 0.000 | −1.112 | −131.7% | 602 |
|
||||
| `194b096f7edab53c` | 0.0162 | 0.000 | −1.154 | −150.7% | 369 |
|
||||
|
||||
Il fatto che **DSR sia zero per tutti i top-5** indica che nessuna strategia passa il deflation test di Bailey & López 2014: il loop non sta generando proposte con edge statistico anche solo apparente. Il valore di fitness positivo che li seleziona deriva interamente dal termine `tanh(sharpe) × penalty(dd)` della fitness v1, che resta debolmente non nullo anche per Sharpe negativi grazie alla penalty di drawdown e a saturazioni numeriche. I primi due genomi hanno fitness identico a 0.0215 e total return identico — verosimilmente lo stesso elite riproposto a generazioni adiacenti.
|
||||
|
||||
---
|
||||
|
||||
## 4. Adversarial findings — il sistema fa il suo lavoro
|
||||
|
||||
Il layer Adversarial Phase 1.5 ha emesso 98 finding sul run:
|
||||
|
||||
| Severità | Check | Conteggio |
|
||||
|----------|-------|-----------|
|
||||
| HIGH | `fees_eat_alpha` (nuovo P1.5) | 35 |
|
||||
| MEDIUM | `overtrading` | 19 |
|
||||
| HIGH | `no_trades` | 16 |
|
||||
| HIGH | `flat_too_long` (nuovo P1.5) | 15 |
|
||||
| HIGH | `time_in_market_too_high` (nuovo P1.5) | 8 |
|
||||
| HIGH | `undertrading` | 4 |
|
||||
| HIGH | `degenerate` | 1 |
|
||||
|
||||
Il dato saliente è che i tre check introdotti in Phase 1.5 — `fees_eat_alpha`, `flat_too_long`, `time_in_market_too_high` — sono effettivamente attivi e killano strategie. In particolare `fees_eat_alpha` è la categoria più popolata: 35 occorrenze HIGH. Esempi tipici dai detail dei finding:
|
||||
|
||||
- `Fees $17073.82 = 2032.6% of gross $840.00`;
|
||||
- `Fees $70646.03 = 12671.9% of gross $557.50`;
|
||||
- `Signal flat for 98.8% of bars (>95% threshold)`.
|
||||
|
||||
Il messaggio è netto: il pool di strategie generato da nemotron, ai prompt e ai gradi di libertà attuali, oscilla tra due estremi degeneri — strategie inattive (flat 98%+) e strategie iperattive (overtrading + fee che divorano l'alpha lordo). Phase 1.5 cattura entrambi gli estremi, ma il loop GA non ha materiale di partenza sano da cui evolvere.
|
||||
|
||||
---
|
||||
|
||||
## 5. Decisione
|
||||
|
||||
**Esito**: NO-GO sulla combinazione `tier C = nemotron` + `Phase 1.5 adversarial` come configurazione di Phase 2.
|
||||
|
||||
Le ragioni a supporto della decisione sono tre.
|
||||
|
||||
Primo, la convergenza è assente per nove generazioni consecutive, non un plateau di selezione raggiunto dopo una fase di salita. Non si tratta cioè di un loop che ha già trovato il suo ottimo e lo conserva, ma di un loop che non ne ha trovato uno.
|
||||
|
||||
Secondo, la distanza dal baseline Phase 1 v5 è di un ordine di grandezza: max fitness `0.0215` qui contro `0.3347` nel run di gate Phase 1, mediana che oscilla sullo zero contro una mediana attorno a `0.005`–`0.09`. Nemotron, in questa configurazione, sta producendo proposte qualitativamente più povere di qwen-2.5-72b nello stesso schema operativo.
|
||||
|
||||
Terzo, i finding adversarial non puntano a un bug del sistema ma a una mancanza di edge nelle proposte. Il loop sta sanzionando correttamente — il problema è a monte, nella generazione.
|
||||
|
||||
---
|
||||
|
||||
## 6. Tre direzioni per Phase 2
|
||||
|
||||
Tre opzioni si configurano per il passo successivo. Vanno valutate prima di una nuova esecuzione, non in parallelo a essa.
|
||||
|
||||
**Direzione A — Riportare tier C a `qwen/qwen-2.5-72b-instruct`** (configurazione di gate Phase 1). Il run di riferimento `phase1-real-005` è già un baseline noto: max fitness `0.3347`, top genome problematico (flat 99.8%) ma generato sotto Phase 1 adversarial. Rilanciare lo stesso pool con Phase 1.5 adversarial isolerebbe l'effetto del solo hardening sul medesimo motore generativo, senza confondere variabili. Questo è il percorso più informativo nel breve.
|
||||
|
||||
**Direzione B — Mantenere nemotron ma rilassare i prompt di Hypothesis**. L'ipotesi alternativa è che il prompting attuale, calibrato su qwen, sia troppo terso o troppo vincolato per la modalità di ragionamento di nemotron. Iterare due o tre versioni del prompt — più esempi few-shot, vincoli espliciti su `n_trades` minimo e `time_in_market` target — può cambiare radicalmente la qualità dell'output senza cambiare il modello.
|
||||
|
||||
**Direzione C — Sostituire il tier C con un modello a pagamento di fascia comparabile**. Tra i benchmark precedenti, `deepseek/deepseek-v4-flash` è già usato come tier A/B nel file `.env`; promuoverlo a tier C significa accettare una spesa marginale (stima $1–3 per run di 10 gen × 20 pop) in cambio di una qualità generativa nota.
|
||||
|
||||
La preferenza dell'operatore per modelli cost-conscious orienta verso A o B. La direzione C resta utile come benchmark di controllo se A e B fallissero a loro volta.
|
||||
|
||||
---
|
||||
|
||||
## 7. Operazioni di pulizia eseguite contestualmente
|
||||
|
||||
- Il run zombie `phase1-real-008` (id `6ebcff9f7f6544c18ced50313cf72ca9`, marcato `running` da 07:11 UTC senza processo associato) è stato chiuso a `status='failed'` direttamente in `runs.db`, per evitare contaminazione delle query di dashboard.
|
||||
- Il commit `9d0deb3` (`fix(llm): handle empty completions + missing usage`) è già su `main`. Il `client.py` ora tratta `resp.choices == []` e `resp.usage is None` come errori retryable invece che assertion failure: precondizione necessaria per qualsiasi run successivo su provider `:free`.
|
||||
|
||||
---
|
||||
|
||||
## 8. Note per chi legge
|
||||
|
||||
Questo memo è un documento di decisione, non un rapporto tecnico completo. Il rapporto tecnico esteso del run può essere ricostruito da `runs.db` interrogando le tabelle `runs`, `generations`, `evaluations`, `adversarial_findings`, `cost_records` con `run_id='434c417e2b6f42bb8cf32514e5d0db1d'`. Il design Phase 1.5 e le motivazioni delle soglie adversarial restano definiti nel commit `56a631f` e nei suoi file di test.
|
||||
@@ -0,0 +1,97 @@
|
||||
# Decisione: indicatori `*_pct` per fix bug protocollo unità
|
||||
|
||||
**Data:** 2026-05-15
|
||||
**Status:** Implementato (atr_pct + sma_pct + macd_pct + prompt LLM aggiornato)
|
||||
**Scope:** `multi_swarm_core.protocol.{compiler,grammar,validator}` + `agents/hypothesis.py` + `strategy_crypto/strategies/eth_*.json`
|
||||
|
||||
## Contesto
|
||||
|
||||
Phase 3 baseline-001 abortita il 2026-05-15 dopo 24h+ di paper-trading senza
|
||||
trade. Analisi post-mortem: la strategia `eth_facd6af85d5d.json` aveva 2/4
|
||||
condizioni dead. Bug:
|
||||
|
||||
- `atr(14) > 0.02` su ETH @ ~3000 USDT → ATR vale ~30-50 (unità prezzo
|
||||
assoluto) → **sempre TRUE** → branch neutro
|
||||
- `atr(14) < 0.01` → **sempre FALSE** → branch morto
|
||||
|
||||
L'origine del bug è una **convention mismatch** fra LLM e compiler:
|
||||
- Il LLM in Phase 1-2 ha generato literal frazionali (0.02, 0.01)
|
||||
aspettandosi che `atr` fosse in % del prezzo (convenzione standard quant)
|
||||
- Il compiler restituiva ATR in unità prezzo assoluto
|
||||
|
||||
## Decisione
|
||||
|
||||
**Aggiungere un indicatore separato `atr_pct(length) = atr/close`** che
|
||||
restituisce ATR come frazione del prezzo close. NON modificare `atr` (che
|
||||
resta in unità prezzo assoluto, usato in confronti relativi tipo
|
||||
`atr > sma`).
|
||||
|
||||
## Rationale
|
||||
|
||||
- **Backcompat**: `atr` esistente continua a funzionare per le strategie
|
||||
che lo usano in confronti relativi (es. `btc_fb63e851.json` ha
|
||||
`atr > sma(14)`, entrambi unità prezzo → confronto valido).
|
||||
- **Esplicito**: il nome `atr_pct` segnala chiaramente al lettore (umano
|
||||
e LLM) la convenzione frazionale.
|
||||
- **Diff minimo**: 4 file modificati (compiler, grammar, validator, JSON
|
||||
ETH) + 2 test unitari nuovi. Nessun refactor del prompt LLM richiesto
|
||||
in questa migrazione.
|
||||
- **Replicabilità**: se in futuro emergono altri indicatori con problemi
|
||||
analoghi (sma vs literal, macd vs literal), si applica lo stesso
|
||||
pattern: aggiungere `<indicator>_pct` come variante frazionale.
|
||||
|
||||
## Alternative scartate
|
||||
|
||||
- **Normalizzare `atr` (breaking change)**: avrebbe rotto le strategie
|
||||
che lo usano in confronti relativi. Troppo rischioso post-Phase 1-2.
|
||||
- **Cambiare il prompt LLM per generare literal in unità prezzo**: non
|
||||
scalabile (literal dipende dall'asset corrente) + i 30 run GA già
|
||||
fatti diventano invalidi.
|
||||
- **Auto-detect** (literal piccolo → assume frazione): magic, frangile,
|
||||
hard to debug.
|
||||
|
||||
## Impatto
|
||||
|
||||
- Strategie GA esistenti: nessun impatto su `btc_fb63e851.json`. Patchato
|
||||
`eth_facd6af85d5d.json` (sostituiti 2 `atr` → `atr_pct`).
|
||||
- Prompt LLM: NON aggiornato in questo commit. Future Phase 2.x runs
|
||||
dovranno includere `atr_pct` nella sezione "indicatori disponibili"
|
||||
del system prompt (item separato, fuori scope qui).
|
||||
- Test: +2 test unitari (`test_atr_pct_is_atr_divided_by_close`,
|
||||
`test_atr_pct_in_strategy_eval`) come regression guard.
|
||||
|
||||
## Verifica
|
||||
|
||||
```python
|
||||
from multi_swarm_core.protocol.compiler import _ind_atr, _ind_atr_pct
|
||||
import pandas as pd, numpy as np
|
||||
np.random.seed(42); close = 3000 + np.cumsum(np.random.randn(500) * 30)
|
||||
df = pd.DataFrame({"close": close, "high": close+10, "low": close-10}, ...)
|
||||
_ind_atr(df, 14).mean() # ~32 (unità prezzo)
|
||||
_ind_atr_pct(df, 14).mean() # ~0.011 (1.1% del prezzo)
|
||||
```
|
||||
|
||||
`atr > 0.02` → 500/500 (broken). `atr_pct > 0.02` → 0/500 (real signal).
|
||||
|
||||
## Estensione: sma_pct + macd_pct (2026-05-15)
|
||||
|
||||
Aggiunti anche `sma_pct` e `macd_pct` per coerenza famiglia `*_pct`:
|
||||
|
||||
- **`sma_pct(length) = (close - sma) / sma`** — deviazione frazionale del
|
||||
close dalla SMA. Range tipico ±0.1. NB: NON è `sma/close` (che sarebbe
|
||||
sempre ~1.0, inutile per literal). Uso: `sma_pct(50) > 0.05` significa
|
||||
"close 5% sopra la media a 50 barre" (mean reversion).
|
||||
- **`macd_pct(fast, slow, signal) = macd / close`** — MACD come frazione
|
||||
del prezzo. Range tipico ±0.02. Uso: `macd_pct > 0.005` significa
|
||||
"momentum > 0.5% del prezzo".
|
||||
|
||||
Prompt LLM aggiornato di conseguenza (`agents/hypothesis.py`):
|
||||
- Lista indicatori include `sma_pct` e `macd_pct` con annotazioni unità
|
||||
- Pattern guidance: "Mean reversion strutturale: sma_pct(long) > 0.05",
|
||||
"Momentum positivo conferma: macd_pct(12,26,9) > 0.005"
|
||||
- 3 nuovi test (sma_pct identity, macd_pct identity, validator integration)
|
||||
|
||||
## Open items
|
||||
|
||||
- ~~Aggiornare il system prompt LLM~~ ✅ chiuso 2026-05-15
|
||||
- ~~`sma_pct`, `macd_pct` se emergono usi analoghi~~ ✅ chiuso 2026-05-15
|
||||
@@ -0,0 +1,286 @@
|
||||
# Multi-Swarm Coevolutivo — Stato del progetto e roadmap
|
||||
|
||||
*Data del documento: 14 maggio 2026 — branch `main` allineato a commit `45f273f`.*
|
||||
|
||||
Questo documento riepiloga l'intero percorso del proof-of-concept Multi-Swarm Coevolutive dalla Phase 1 (lean spike) fino allo stato corrente di entrata in Phase 3 (paper-trading forward-test). È inteso come punto di sincronizzazione per riprendere il lavoro: cosa è stato deciso, cosa ha funzionato, cosa no, e quali sono le prossime mosse plausibili.
|
||||
|
||||
---
|
||||
|
||||
## 1. Quadro sintetico
|
||||
|
||||
| Fase | Periodo | Stato | Esito |
|
||||
|------|---------|-------|-------|
|
||||
| **Phase 1** — lean spike | 9-10 maggio 2026 | ✅ chiusa | GO Phase 2 (5/5 hard gate) |
|
||||
| **Phase 1.5** — adversarial hardening | 11 maggio 2026 | ✅ chiusa | NO-GO sulla combo nemotron, hardening conservato |
|
||||
| **Phase 2** — feature temporali + qwen3-235b | 11 maggio 2026 | ✅ chiusa | NO-GO sul modello (rollback a qwen-2.5-72b) |
|
||||
| **Phase 2.5** — LLM prompt mutator | 11-12 maggio 2026 | ✅ chiusa | Operator integrato, sweet spot weight 0.20-0.30 |
|
||||
| **Phase 2.6** — Walk-Forward Validation | 12-13 maggio 2026 | ✅ chiusa | WFA 70/30 introdotta, min-trades parametrico |
|
||||
| **Phase 2.7** — portabilità cross-asset (BTC/ETH/SOL) | 13 maggio 2026 | ✅ chiusa | BTC strong, ETH adequate, SOL failure |
|
||||
| **Phase 3** — paper-trading forward-test | 13-14 maggio 2026 | 🟢 in corso | Runner BTC+ETH operativo, smoke OK |
|
||||
|
||||
Dal punto di vista del DB locale: 30 run GA completate, costo cumulato LLM **≈ $3.74**, due paper-trading run avviati (`phase3-smoke-001`, `phase3-papertrade-001`).
|
||||
|
||||
---
|
||||
|
||||
## 2. Phase 1 — lean spike (chiusa 10 maggio)
|
||||
|
||||
### Obiettivo
|
||||
Validare end-to-end l'idea co-evolutiva: GA → popolazione di prompt LLM → strategie JSON → backtest deterministico → fitness → selezione. Cinque hard gate vincolanti.
|
||||
|
||||
### Risultato
|
||||
Run di riferimento `phase1-real-005` su BTC-PERPETUAL Deribit 1h, 2024-01-01 → 2026-01-01, K=20, 10 generazioni, **costo $0.069 in 29 minuti**.
|
||||
|
||||
| Hard gate | Soglia | Misurato | Esito |
|
||||
|-----------|--------|----------|-------|
|
||||
| Loop convergence | median sale | 0.0001 → 0.0188 in 3 gen | ✓ |
|
||||
| Parse success | ≥ 95% | 100% (98/98) post refactor JSON | ✓ |
|
||||
| Top-5 vs median | ≥ 10× | 1116× | ✓ |
|
||||
| Entropy fitness gen 9 | ≥ 0.5 | 0.914 | ✓ |
|
||||
| Costo totale | ≤ $700 | $0.069 | ✓ |
|
||||
|
||||
Iterazione: 5 run prima del PASS, ognuna ha scoperto un bug strutturale (max_dd su equity assoluta, cap Cerbero 5000 candele, validator arity, switch grammar S-expr→JSON, fitness clip-to-0 troppo dura).
|
||||
|
||||
### Caveat critico
|
||||
Il top-1 ha reso **+2.66% in 2 anni vs B&H BTC +106%**, essendo *flat* nel 99,8% del tempo. Conferma che la fitness v1 premiava "non-strategie" sicure invece di alpha vero. Da qui la Phase 1.5.
|
||||
|
||||
### Documenti chiave
|
||||
- `docs/decisions/2026-05-10-gate-phase1.md` — decision memo.
|
||||
- `docs/reports/2026-05-10-phase1-technical-report.md` — report tecnico.
|
||||
|
||||
---
|
||||
|
||||
## 3. Phase 1.5 — adversarial hardening (chiusa 11 maggio)
|
||||
|
||||
Quattro nuovi check `HIGH` aggiunti all'agente Adversarial per killare strategie degeneri:
|
||||
|
||||
1. `overtrading` ricalibrato `n_bars/20` (era `n_bars/5`).
|
||||
2. `undertrading` promosso a HIGH se `n_trades < 10`.
|
||||
3. `flat_too_long` (nuovo HIGH) — segnale flat > 95% bar.
|
||||
4. `fees_eat_alpha` (nuovo HIGH) — `fees / |gross_pnl| > 0.5` con gross positivo.
|
||||
5. `time_in_market_too_high` (nuovo HIGH) — segnale LONG||SHORT > 80% bar (kill leveraged-B&H camuffato).
|
||||
|
||||
**Run di test `phase1.5-nemotron-001`** (tier C nemotron, 2h26', $0.12) → **NO-GO**: max fitness 0.0215 stagnante, median 0 su 9 gen, top-5 con DSR=0 e Sharpe ≈ −1.1. I check Phase 1.5 funzionavano (98 findings emessi); il problema era il modello: prompt calibrato su qwen, nemotron produceva materiale qualitativamente più povero.
|
||||
|
||||
Bugfix collaterale (`9d0deb3`): `EmptyCompletionError` reso retryable + gestione `resp.usage=None` per provider `:free`.
|
||||
|
||||
---
|
||||
|
||||
## 4. Phase 2 — feature temporali + tier C qwen3-235b (chiusa 11 maggio)
|
||||
|
||||
Due lavori in parallelo, esiti opposti:
|
||||
|
||||
**4.1 Feature temporali in protocol layer** — `KNOWN_FEATURES` esteso con `hour`, `dow`, `is_weekend`, `minute_of_hour`. Compiler dispatcher temporale (`9d1f97c`), validator parametrizzato, integration test gating temporale+SMA. Few-shot example nel prompt Hypothesis. **Successo strutturale**: tutte le top strategie successive sfruttano questo asset.
|
||||
|
||||
**4.2 Upgrade tier C a `qwen/qwen-2.5-72b-instruct` → `qwen3-235b-a22b`** — run `phase2-qwen3-001`: max fitness 0.0238 stuck per 8 gen, entropy 0.199 stuck per 7 gen, 4 dei 5 top genomi con fitness/Sharpe/DD identici. Il **run controllo** identico ma con qwen-2.5-72b: 0.0311 (+30%), median raggiunge top in 4 gen, entropy 0.85, ½ tempo e costo. **Rollback a qwen-2.5-72b** (`8ec45c5`).
|
||||
|
||||
Lezione consolidata: il prompt è calibrato sulla famiglia qwen-2.5; un modello "più nuovo / più grande" non è automaticamente meglio se il prompt non viene ricalibrato in parallelo.
|
||||
|
||||
---
|
||||
|
||||
## 5. Phase 2.5 — operator `mutate_prompt_llm` (chiusa 12 maggio)
|
||||
|
||||
Quinto operatore di mutazione che riscrive il `system_prompt` via LLM tier B (`deepseek-v4-flash`) anziché perturbare scalari. Sei istruzioni atomiche: `tighten_threshold`, `swap_comparator`, `add_condition`, `remove_condition`, `change_timeframe`, `add_temporal_gate`. Validation gate (lunghezza ≥ 50, keyword tecnica, diff Levenshtein > 5%) + fallback `random_mutate`. Dispatcher pesato `weighted_random_mutate` (CLI `--prompt-mutation-weight`, default 0.0).
|
||||
|
||||
### Sweet spot empirico (seed 42, pop 20, 10 gen)
|
||||
|
||||
| weight | max fit | median fin | Sharpe top | trades | verdetto |
|
||||
|--------|---------|-----------|-----------|--------|----------|
|
||||
| 0.00 | 0.0311 | 0.0000 | −1.08 | 274 | baseline |
|
||||
| **0.30** | **0.1012** ⭐ | **0.0745** | **−0.25** | 62 | sweet spot (ma seed-lucky) |
|
||||
| 0.50 | 0.0311 | 0.0000 | −1.08 | 274 | regressione |
|
||||
|
||||
### Validazione robustezza
|
||||
Confronti seed multipli (7, 99, 123) hanno mostrato che il **+225%** del run 004 era **outlier seed-specific**. Beneficio medio reale del prompt-mutator: **+10–23%** sopra baseline. La leva più affidabile e seed-indipendente è risultata `fees_eat_alpha_threshold 0.7` (anziché 0.5): +23% stabile, Sharpe top −0.70 vs −1.08.
|
||||
|
||||
### Combo vincente (pop=30 + weight=0.30 + fees=0.7)
|
||||
Run `pop30-combo-001`: max fitness 0.0459 (+48% vs control), **median finale = max** (convergenza ≥50% pop), Sharpe top −0.63, 226 trades. Mutator overhead ≈ 5,4% del costo totale.
|
||||
|
||||
### Cost attribution (Task 6)
|
||||
`cost_records.call_kind` (`hypothesis` / `mutation`) attivo da `ba4eb09`. Permette breakdown costo per operatore: il prompt-mutator costa 3-9% del totale, trascurabile.
|
||||
|
||||
---
|
||||
|
||||
## 6. Phase 2.6 — Walk-Forward Validation (chiusa 13 maggio)
|
||||
|
||||
Aggiunte tre leve metodologiche:
|
||||
|
||||
- **WFA 70/30**: split temporale `train/OOS` con OOS intoccato durante GA, valutato solo a fine run.
|
||||
- **`--min-trades-threshold`** parametrico: filtra survivors con n_trades insufficiente prima del ranking.
|
||||
- **Fitness v2 soft-kill** (`cf42dd8`): solo `no_trades` + `degenerate` + `undertrading` azzerano hard. Altri HIGH applicano penalty moltiplicativa `1/(1+0.4·n)` (1 HIGH = 0,71×, 2 = 0,56×, 3 = 0,45×). CLI `--fitness-v2` + `--fitness-soft-penalty`.
|
||||
- **Pattern guidance nel system prompt** (`67ae6ff`): forma curve attese + criterio ripetibilità.
|
||||
- **Fitness multi-obiettivo** (`1a1dfb7`): `combined = α·IS + (1−α)·OOS` opt-in.
|
||||
|
||||
Effetto cumulativo: la pipeline produce strategie con migliore generalizzazione cross-split senza dover degradare le adversarial hard.
|
||||
|
||||
---
|
||||
|
||||
## 7. Phase 2.7 — backtest 7 anni e portabilità cross-asset (chiusa 13 maggio)
|
||||
|
||||
### 7.1 Validazione 7,33 anni su BTC
|
||||
|
||||
Backtest dei top genome scoperti sulle varie sotto-fasi sui **64.297 bar 1h** completi (2018-09-01 → 2026-01-01), fees 5 bp:
|
||||
|
||||
| Genome | Origine | Total P/L 7y | CAGR | Sharpe ann | MaxDD | Verdetto |
|
||||
|--------|---------|--------------|------|-----------|-------|----------|
|
||||
| `5226503a` | run004 outlier 2y bull | **−310,69%** | wiped out | −0,155 | 280,9% | crash totale OOS |
|
||||
| `e52604ba` | flat-ablation top 2y | −37,17% | −6,14% | −0,063 | 182,0% | SMA non generalizza |
|
||||
| `ec06a3d4` | fitness-v2-combo top 2y | +142,51% | +12,85% | +0,229 | 64,9% | hour-gated regge |
|
||||
| `4e1be9fa` | 7y-v2-WFA top IS | +67,60% | +7,30% | +0,240 | 79,1% | top IS ingannevole |
|
||||
| `63411199` | 7y-v2-WFA top OOS | **+660,11%** | **+31,88%** | +0,238 | 77,1% | leveraged-B&H camuffato |
|
||||
| **`fb63e851`** ⭐ | 7y multi-seed99 top OOS | +130,37% | +12,06% | **+0,264** | **54,8%** | **true alpha** |
|
||||
|
||||
Conclusioni:
|
||||
- Il top by `fit_IS` è sistematicamente ingannevole su orizzonti lunghi.
|
||||
- Pattern SMA-puri collassano cross-regime.
|
||||
- Pattern *hour-gated* (filtri intraday) reggono cross-regime.
|
||||
- `fb63e851` è il candidato più robusto: 4 AND × 2 rule × filtro intraday → attiva l'1-2% del tempo, Sharpe cross-regime più alto.
|
||||
|
||||
### 7.2 Portabilità BTC → ETH → SOL
|
||||
|
||||
Tre run **identici** (`population=30`, `n_gen=10`, `prompt_mutation_weight=0.30`, fitness v2, WFA 70/30, `fees_eat_alpha_threshold=0.7`, undertrading 20) su Deribit perpetuals.
|
||||
|
||||
| Asset | Storia | Top OOS Sharpe | Verdetto |
|
||||
|-------|--------|----------------|----------|
|
||||
| **BTC** | 7,33 y | `fb63e851` +0,50 OOS (+20,16%) | **STRONG** |
|
||||
| **ETH** | 6,75 y | `facd6af85d5d` +0,19 OOS (+16,14%) | **ADEQUATE** |
|
||||
| **SOL** | 3,00 y | **0 survivors / 247 evals** | **FAILURE** |
|
||||
|
||||
Pattern scoperti **divergenti**: BTC = mean reversion intraday contrarian; ETH = trend-following long-bias + vol regime. **Non esiste "una strategia universale"**: la metodologia (GA + WFA + adversarial v2) è portabile, il pattern no. SOL ha fallito per finestra dati troppo corta (3y) e regime bull-only post-FTX.
|
||||
|
||||
---
|
||||
|
||||
## 8. Phase 3 — paper-trading forward-test (in corso)
|
||||
|
||||
### Componenti implementati (`45f273f`)
|
||||
|
||||
Modulo `src/multi_swarm/paper_trading/`:
|
||||
- `portfolio.py` — multi-asset portfolio con sleeve uguali per asset, fees in bp.
|
||||
- `executor.py` — `PaperExecutor` carica una strategia JSON, compila, valuta l'ultimo bar.
|
||||
- `persistence.py` — `PaperRepository` su SQLite (tabelle `paper_trading_runs`, `paper_trading_ticks`, `paper_trading_equity`, `paper_trading_trades`, `paper_trading_positions`).
|
||||
|
||||
Runner `scripts/run_paper_trading.py`:
|
||||
- Loop poll OHLCV Cerbero ogni `--poll-seconds` (default 300).
|
||||
- Riconosce *nuovo bar chiuso* confrontando ultimo timestamp; tick consecutivi su stesso bar = hold.
|
||||
- Snapshot equity ogni tick.
|
||||
- Supporta `--max-ticks N` per smoke test (0 = infinito).
|
||||
|
||||
Strategie freezate per il forward-test:
|
||||
- `strategies/btc_fb63e851.json` — RSI estremi + ATR vs SMA + filtro orario 9-17.
|
||||
- `strategies/eth_facd6af85d5d.json` — ATR + realized_vol + golden/death cross.
|
||||
|
||||
### Stato corrente
|
||||
- Schema DB esteso e validato.
|
||||
- Run smoke completato (`phase3-smoke-001`).
|
||||
- Run live in corso (`phase3-papertrade-001`).
|
||||
|
||||
---
|
||||
|
||||
## 9. Architettura cumulata
|
||||
|
||||
```
|
||||
src/multi_swarm/
|
||||
├── config.py
|
||||
├── data/{cerbero_ohlcv,splits}.py ← splits.py per WFA
|
||||
├── backtest/{orders,engine}.py
|
||||
├── metrics/{basic,dsr,diversity}.py ← diversity per Phase 2.5
|
||||
├── cerbero/{client,tools}.py
|
||||
├── protocol/{grammar,parser,validator,compiler}.py
|
||||
│ └── KNOWN_FEATURES include hour/dow/is_weekend/minute_of_hour
|
||||
├── genome/
|
||||
│ ├── hypothesis.py
|
||||
│ ├── mutation.py ← 4 operatori scalari
|
||||
│ ├── mutation_prompt_llm.py ← Phase 2.5: 5° operatore LLM
|
||||
│ └── crossover.py
|
||||
├── llm/{client,cost_tracker}.py ← cost_kind tracking
|
||||
├── agents/{hypothesis,falsification,adversarial,market_summary}.py
|
||||
│ └── adversarial: 5 check HIGH parametrici (CLI knobs)
|
||||
├── ga/
|
||||
│ ├── selection.py
|
||||
│ ├── fitness.py ← v1 + v2 soft-kill + combined IS/OOS
|
||||
│ ├── loop.py
|
||||
│ ├── summary.py
|
||||
│ └── initial.py
|
||||
├── persistence/{schema,repository}.py ← +tabelle paper_trading_*
|
||||
├── paper_trading/ ← NEW Phase 3
|
||||
│ ├── portfolio.py
|
||||
│ ├── executor.py
|
||||
│ └── persistence.py
|
||||
├── orchestrator/run.py
|
||||
└── dashboard/
|
||||
├── nicegui_app.py ← unica GUI, porta parametrica via SWARM_DASHBOARD_PORT
|
||||
└── data.py
|
||||
```
|
||||
|
||||
CLI knobs accumulati per ablation:
|
||||
- `--prompt-mutation-weight FLOAT` (Phase 2.5)
|
||||
- `--fees-eat-alpha-threshold FLOAT` (default 0.5, suggerito 0.7)
|
||||
- `--flat-too-long-threshold FLOAT` (default 0.95)
|
||||
- `--undertrading-threshold INT` (default 20)
|
||||
- `--fitness-v2` + `--fitness-soft-penalty FLOAT`
|
||||
- `--fitness-combined-alpha FLOAT` (multi-obiettivo IS/OOS)
|
||||
- `--min-trades-threshold INT` (WFA OOS filter)
|
||||
|
||||
---
|
||||
|
||||
## 10. Cosa resta da fare
|
||||
|
||||
### 10.1 Phase 3 — completamento paper-trading
|
||||
- [ ] **Definire criterio di STOP/GO Phase 3**: durata minima forward-test (es. 4-8 settimane), soglie sopravvivenza (Sharpe live > 50% del Sharpe OOS atteso, DD live < 1,5× DD OOS).
|
||||
- [ ] **Pagina dashboard paper-trading**: estendere NiceGUI con tab live equity + open positions + tick log per `paper_trading_runs`. Oggi i dati esistono in DB ma non hanno UI dedicata.
|
||||
- [ ] **Monitoring & alerting**: notifica se il runner si ferma (Cerbero down, processo killato). Considerare systemd unit o supervisor.
|
||||
- [ ] **Robustezza fetch live**: oggi `loader._fetch(req)` bypassa la cache; aggiungere retry esplicito (oltre a quello tenacity già presente nel client) e log strutturato dei fallimenti per asset.
|
||||
- [ ] **Confronto live vs OOS atteso**: script che a fine settimana confronta P/L, Sharpe rolling, hit rate vs i numeri del backtest 7y per individuare *regime mismatch* precoce.
|
||||
|
||||
### 10.2 Estensioni metodologiche
|
||||
- [ ] **Multi-seed ensembling**: invece di scegliere un singolo top genome, valutare ensemble (mediana o weighted) dei top-K trovati con seed diversi sullo stesso asset. La varianza seed è il rischio numero uno (vedi sezione 5).
|
||||
- [ ] **Asset universe expansion**: testare la metodologia su asset non-crypto (oro, forex EURUSD) per smentire l'ipotesi che funzioni solo perché BTC/ETH hanno alta volatilità. `yfinance` è già in dipendenze (`9d1ef8a`).
|
||||
- [ ] **Fitness regime-aware**: oggi fitness è single-objective sull'intero train. Considerare fitness condizionata al regime (bull/bear/range) per favorire strategie con performance bilanciata cross-regime invece di top assoluto.
|
||||
- [ ] **Phase 2.7 retry su SOL** con configurazione mirata: train più corto, undertrading_threshold ridotto, prompt few-shot di strategie short-vol-only. Verificare se è davvero il dato a fallire o se è la calibrazione.
|
||||
|
||||
### 10.3 Hardening tecnico
|
||||
- [ ] **Cleanup zombie runs**: `phase2-6-flat-wfa-001` è ancora `failed` nel DB. Verificare che il flush di stato sia idempotente per tutti i path di crash.
|
||||
- [x] **Port completo dashboard a NiceGUI** *(chiuso 14 maggio 2026, commit `03f723f`)*: Streamlit deprecata e rimossa insieme ai file legacy (`streamlit_app.py`, `aquarium.py`, `pages/0[1-4]_*.py`); dep `streamlit>=1.40` cancellata da `pyproject.toml` con 10 transitive (pydeck, watchdog, jsonschema, pillow, …). NiceGUI espone 3 pagine (`/`, `/convergence`, `/genomes`) su porta parametrica `SWARM_DASHBOARD_PORT` (default 8080). **Aquarium non riportata per scelta** (decisione utente: non più ritenuta utile). Deploy in produzione via Docker + Traefik su `https://swarm.tielogic.xyz` (compose `docker-compose.yml`, commit `8e5efde`).
|
||||
- [ ] **Pruning DB**: dopo 30+ run la SQLite cresce. Aggiungere uno script di archiviazione/compressione delle run completate più vecchie di N giorni.
|
||||
- [ ] **CI/test coverage**: i 180+ test girano localmente; non c'è ancora CI esterna (Gitea Actions o equivalente).
|
||||
|
||||
### 10.4 Documentazione e governance
|
||||
- [ ] **Decision memo Phase 2.5 + Phase 2.6 + Phase 2.7** formalizzati come `docs/decisions/2026-05-1{2,3}-*.md` (esistono solo memory + commit message; manca il pendant pubblico dei due memo già esistenti per Phase 1 e Phase 1.5).
|
||||
- [ ] **Phase 3 charter**: documento che fissa a priori cosa significherà "successo" o "fallimento" del forward-test, per evitare *moving goalposts* a posteriori.
|
||||
- [ ] **Threats to validity update**: il memo Phase 1 ne elencava 6; integrarli con le scoperte successive (varianza seed, portabilità asset-specifica, divergenza pattern BTC/ETH).
|
||||
|
||||
---
|
||||
|
||||
## 11. Caveat e rischi aperti
|
||||
|
||||
1. **Varianza seed**: con seed diversi (7, 99, 123) sullo stesso identico setup il max fitness varia di un fattore 3-4×. Qualunque metrica single-seed è statisticamente debole; finché Phase 3 non raccoglie N≥5 forward-test indipendenti, il vantaggio del prompt-mutator resta nel rumore.
|
||||
2. **Sharpe OOS positivi ma bassi**: BTC `+0,50` ed ETH `+0,19` sono migliori del coin-flip ma lontani dai target retail "investment-grade" (≥ 1,0). La metodologia è validata, l'alpha catturata è modesta.
|
||||
3. **`time_in_market_too_high` come red-flag chiave**: `63411199` ha CAGR +31,88% ma esposizione 90% del tempo — è leveraged-B&H camuffato, non alpha. Phase 3 deve preferire `fb63e851` (selettività 1-2%) anche se ha return assoluto minore.
|
||||
4. **Dipendenza dal modello qwen-2.5-72b**: rollback Phase 2 ha dimostrato che il prompt è calibrato su questa specifica famiglia. Se il modello venisse deprecato da OpenRouter, sarebbe necessario un giro di ricalibrazione prompt → rischio di operatività.
|
||||
5. **Cerbero MCP come single point of failure**: tutti i fetch OHLCV passano da lì. Da considerare un fallback (ccxt o yfinance) almeno per il paper-trading.
|
||||
|
||||
---
|
||||
|
||||
## 12. Costi cumulati
|
||||
|
||||
- **Phase 1 (5 run iterazione)**: $0,19.
|
||||
- **Phase 1.5 nemotron**: $0,12.
|
||||
- **Phase 2 + 2.5 + 2.6 + 2.7**: ≈ $3,24 cumulati su 25+ run.
|
||||
- **Totale LLM progetto a oggi**: ≈ **$3,74** (DB locale).
|
||||
- **Phase 3 paper-trading**: $0 incrementali per LLM (le strategie sono fisse), solo costi Cerbero (incluso nel servizio esistente).
|
||||
|
||||
Resta amplissimo margine rispetto al cap originale Phase 1 di $700.
|
||||
|
||||
---
|
||||
|
||||
## 13. Riferimenti
|
||||
|
||||
- README.md — overview e setup.
|
||||
- `docs/decisions/2026-05-10-gate-phase1.md`, `docs/decisions/2026-05-11-phase1-5-nemotron-run.md`.
|
||||
- `docs/reports/2026-05-10-phase1-technical-report.md`.
|
||||
- `docs/superpowers/specs/2026-05-09-decisione-strategica-design.md`, `docs/superpowers/specs/2026-05-11-temporal-features-design.md`.
|
||||
- `docs/superpowers/plans/2026-05-09-phase1-lean-spike.md`, `docs/superpowers/plans/2026-05-11-mutate-prompt-llm-phase-2-5.md`, `docs/superpowers/plans/2026-05-11-temporal-features.md`.
|
||||
- DB locale `runs.db` per dettaglio run-by-run.
|
||||
|
||||
---
|
||||
|
||||
*Prossimo checkpoint suggerito: rivedere questo documento al termine del primo ciclo completo di Phase 3 (≥ 2 settimane di forward-test continuo) per consolidare i risultati live e decidere GO/NO-GO verso un eventuale Phase 4 (capitale reale ridotto o estensione del universe).*
|
||||
+71
-13
@@ -1,6 +1,7 @@
|
||||
"""Adversarial agent: ispeziona una :class:`Strategy` con check euristici
|
||||
hand-crafted per scovare patologie note (degenerate, no-trade, over/under
|
||||
trading, flat-too-long, fees-eat-alpha) prima del training vero e proprio.
|
||||
trading, flat-too-long, time-in-market-too-high, fees-eat-alpha) prima
|
||||
del training vero e proprio.
|
||||
|
||||
Pipeline:
|
||||
|
||||
@@ -11,10 +12,13 @@ falsificazione, ma sega presto i casi degeneri (es. ``gt close -1e9`` →
|
||||
sempre long) che inquinerebbero il leaderboard del swarm.
|
||||
|
||||
Phase 1.5 hardening: soglie strette per overtrading (n_trades > n_bars/20)
|
||||
e undertrading (HIGH se n_trades < 10), piu' due nuovi check HIGH:
|
||||
``flat_too_long`` (signal flat >95% delle bar) e ``fees_eat_alpha``
|
||||
(fees > 50% del gross_pnl positivo). Killano le strategie "lucky shot"
|
||||
e quelle con margine sottile non sostenibile in produzione.
|
||||
e undertrading (HIGH se n_trades < 10), piu' tre nuovi check HIGH:
|
||||
``flat_too_long`` (signal flat >95% delle bar),
|
||||
``time_in_market_too_high`` (signal long/short >80% delle bar, di fatto
|
||||
leveraged buy-and-hold con funding/tail-risk cumulato) e
|
||||
``fees_eat_alpha`` (fees > 50% del gross_pnl positivo). Killano le
|
||||
strategie "lucky shot", le sempre-in-market e quelle con margine sottile
|
||||
non sostenibile in produzione.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -55,8 +59,17 @@ class AdversarialReport:
|
||||
class AdversarialAgent:
|
||||
"""Agente hand-crafted che applica check euristici a una strategia."""
|
||||
|
||||
def __init__(self, fees_bp: float = 5.0) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
fees_bp: float = 5.0,
|
||||
fees_eat_alpha_threshold: float = 0.5,
|
||||
flat_too_long_threshold: float = 0.95,
|
||||
undertrading_threshold: int = 10,
|
||||
) -> None:
|
||||
self._engine = BacktestEngine(fees_bp=fees_bp)
|
||||
self._fees_eat_alpha_threshold = fees_eat_alpha_threshold
|
||||
self._flat_too_long_threshold = flat_too_long_threshold
|
||||
self._undertrading_threshold = undertrading_threshold
|
||||
|
||||
def review(self, strategy: Strategy, ohlcv: pd.DataFrame) -> AdversarialReport:
|
||||
signal_fn = compile_strategy(strategy)
|
||||
@@ -107,12 +120,15 @@ class AdversarialAgent:
|
||||
# Undertrading: < 10 trade -> HIGH (Phase 1.5: era < 5 MEDIUM).
|
||||
# Sample size troppo piccolo per distinguere edge da rumore: e'
|
||||
# un "lucky shot" non riproducibile out-of-sample.
|
||||
if n_trades < 10:
|
||||
if n_trades < self._undertrading_threshold:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
name="undertrading",
|
||||
severity=Severity.HIGH,
|
||||
detail=f"only {n_trades} trades — likely lucky shot (<10 over training)",
|
||||
detail=(
|
||||
f"only {n_trades} trades — likely lucky shot "
|
||||
f"(<{self._undertrading_threshold} over training)"
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
@@ -124,22 +140,46 @@ class AdversarialAgent:
|
||||
n_active = int(((signals == Side.LONG) | (signals == Side.SHORT)).sum())
|
||||
n_flat_or_nan = n_bars - n_active
|
||||
flat_ratio = n_flat_or_nan / n_bars if n_bars > 0 else 1.0
|
||||
if flat_ratio > 0.95:
|
||||
if flat_ratio > self._flat_too_long_threshold:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
name="flat_too_long",
|
||||
severity=Severity.HIGH,
|
||||
detail=f"Signal flat for {flat_ratio * 100:.1f}% of bars (>95% threshold)",
|
||||
detail=(
|
||||
f"Signal flat for {flat_ratio * 100:.1f}% of bars "
|
||||
f"(>{self._flat_too_long_threshold * 100:.0f}% threshold)"
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
# Fees-eat-alpha: gross_pnl > 0 ma fees > 50% del lordo.
|
||||
# Time-in-market-too-high: signal LONG o SHORT >80% delle bar.
|
||||
# Simmetrico opposto di flat_too_long: una strategia sempre-in-market
|
||||
# e' di fatto leveraged buy-and-hold, esposta a funding cumulato su
|
||||
# perp (paid ogni 8h), tail risk eventi notturni/weekend, nessuna
|
||||
# opportunity-cost flexibility. Sweet spot fitness positiva: 5-80%
|
||||
# time in market (combinato con flat_too_long).
|
||||
active_ratio = n_active / n_bars if n_bars > 0 else 0.0
|
||||
if active_ratio > 0.80:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
name="time_in_market_too_high",
|
||||
severity=Severity.HIGH,
|
||||
detail=(
|
||||
f"Signal long/short for {active_ratio * 100:.1f}% of bars "
|
||||
"(>80% threshold); esposizione cumulativa funding + tail risk, "
|
||||
"di fatto leveraged B&H"
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
# Fees-eat-alpha: gross_pnl > 0 ma fees > soglia del lordo.
|
||||
# La strategia ha edge teorico ma il margine viene mangiato dai
|
||||
# costi di transazione: non sostenibile in produzione.
|
||||
# Se gross_pnl <= 0 il check non si applica (gia' perdente).
|
||||
# Se gross_pnl <= 0 il check non si applica (la condizione e' coperta
|
||||
# da ``negative_net_pnl`` sotto).
|
||||
gross_pnl = sum(t.gross_pnl for t in result.trades)
|
||||
total_fees = sum(t.fees for t in result.trades)
|
||||
if gross_pnl > 0 and total_fees / gross_pnl > 0.5:
|
||||
if gross_pnl > 0 and total_fees / gross_pnl > self._fees_eat_alpha_threshold:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
name="fees_eat_alpha",
|
||||
@@ -151,4 +191,22 @@ class AdversarialAgent:
|
||||
)
|
||||
)
|
||||
|
||||
# Negative-net-pnl: somma di ``trade.net_pnl`` < 0 sul training.
|
||||
# Cattura sia il caso "gross negativo" (no edge direzionale) sia il
|
||||
# caso "gross positivo ma fees superiori a gross" (edge insufficiente).
|
||||
# Sintesi del net-after-fees su finestra continua: deal-breaker, non
|
||||
# negoziabile via soft penalty.
|
||||
net_pnl = gross_pnl - total_fees
|
||||
if net_pnl < 0:
|
||||
report.findings.append(
|
||||
Finding(
|
||||
name="negative_net_pnl",
|
||||
severity=Severity.HIGH,
|
||||
detail=(
|
||||
f"Net PnL ${net_pnl:.2f} < 0 after fees over {n_bars} bars; "
|
||||
f"gross ${gross_pnl:.2f}, fees ${total_fees:.2f}"
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
return report
|
||||
@@ -0,0 +1,483 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
from ..genome.hypothesis import HypothesisAgentGenome
|
||||
from ..genome.prompt_library import PromptLibrary
|
||||
from ..llm.client import CompletionResult, EmptyCompletionError, LLMClient, OpusAgentError, OpusAgentTransientError
|
||||
from ..protocol.parser import ParseError, Strategy, parse_strategy
|
||||
from ..protocol.validator import ValidationError, validate_strategy
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MarketSummary:
|
||||
symbol: str
|
||||
timeframe: str
|
||||
n_bars: int
|
||||
return_mean: float
|
||||
return_std: float
|
||||
skew: float
|
||||
kurtosis: float
|
||||
volatility_regime: str
|
||||
autocorr_lag1_recent: float = 0.0 # AR(1) ultimi 500 bar
|
||||
autocorr_lag1_baseline: float = 0.0 # AR(1) full sample (riferimento)
|
||||
hurst_recent: float = 0.5 # Hurst ultimi 500 bar
|
||||
vol_percentile: float = 50.0 # 0-100 percentile della vol corrente
|
||||
seasonality_hour: float = 0.0 # 0-1 varianza spiegata da hour
|
||||
seasonality_dow: float = 0.0 # 0-1 varianza spiegata da dow
|
||||
efficiency_ratio: float = 0.0 # Kaufman, 0-1
|
||||
tail_index_left: float = 5.0 # Hill left tail
|
||||
tail_index_right: float = 5.0 # Hill right tail
|
||||
structural_uptrend: float = 0.5 # HH/HL score 0-1
|
||||
compression: float = 1.0 # range recent / range ref
|
||||
spectral_entropy: float = 1.0 # 0-1, Shannon FFT normalizzata
|
||||
dominant_cycle: float | None = None # periodo barre, None se spectrum piatto
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HypothesisProposal:
|
||||
"""Risultato di una propose() del HypothesisAgent.
|
||||
|
||||
``completions`` contiene SEMPRE almeno un elemento: il primo tentativo.
|
||||
Se il primo tentativo fallisce e c'e' budget di retry, vengono accodate
|
||||
le completions successive, una per ogni retry effettuato.
|
||||
``n_attempts == len(completions)``. ``raw_text`` riflette l'ULTIMO output
|
||||
LLM osservato (quello che ha prodotto strategy o l'ultimo parse_error).
|
||||
"""
|
||||
|
||||
strategy: Strategy | None
|
||||
raw_text: str
|
||||
completions: list[CompletionResult] = field(default_factory=list)
|
||||
parse_error: str | None = None
|
||||
n_attempts: int = 1
|
||||
|
||||
|
||||
# === CORE SCAFFOLD constants (universal, legato al protocol/compiler) ===
|
||||
|
||||
_SYSTEM_GRAMMAR_SPEC = """\
|
||||
Devi proporre una strategia di trading espressa in JSON STRETTO.
|
||||
La risposta deve essere un singolo oggetto JSON dentro fence ```json...```
|
||||
con questa shape:
|
||||
|
||||
```json
|
||||
{{
|
||||
"rules": [
|
||||
{{"condition": <nodo>, "action": "entry-long|entry-short|exit|flat"}}
|
||||
]
|
||||
}}
|
||||
```
|
||||
|
||||
NODI DISPONIBILI
|
||||
|
||||
Operatori logici:
|
||||
{{"op": "and", "args": [<nodo>, <nodo>, ...]}} // >=2 nodi
|
||||
{{"op": "or", "args": [<nodo>, <nodo>, ...]}} // >=2 nodi
|
||||
{{"op": "not", "args": [<nodo>]}} // 1 nodo
|
||||
|
||||
Comparatori (ritornano boolean series):
|
||||
{{"op": "gt", "args": [<a>, <b>]}} // a > b
|
||||
{{"op": "lt", "args": [<a>, <b>]}} // a < b
|
||||
{{"op": "eq", "args": [<a>, <b>]}} // a == b
|
||||
|
||||
Crossover (eventi su 2 serie):
|
||||
{{"op": "crossover", "args": [<serie_a>, <serie_b>]}}
|
||||
{{"op": "crossunder", "args": [<serie_a>, <serie_b>]}}
|
||||
|
||||
Leaf - indicatori (calcolati su close):
|
||||
{{"kind": "indicator", "name": "sma", "params": [<length>]}} // media mobile, UNITÀ PREZZO
|
||||
{{"kind": "indicator", "name": "sma_pct", "params": [<length>]}} // (close-sma)/sma, FRAZIONE ±0.1
|
||||
{{"kind": "indicator", "name": "rsi", "params": [<length>]}} // 0-100, adimensionale
|
||||
{{"kind": "indicator", "name": "atr", "params": [<length>]}} // true range, UNITÀ PREZZO
|
||||
{{"kind": "indicator", "name": "atr_pct", "params": [<length>]}} // atr/close, FRAZIONE 0.0-0.1
|
||||
{{"kind": "indicator", "name": "realized_vol", "params": [<window>]}} // std dei returns, FRAZIONE
|
||||
{{"kind": "indicator", "name": "macd", "params": [<fast>, <slow>, <signal>]}} // UNITÀ PREZZO
|
||||
{{"kind": "indicator", "name": "macd_pct", "params": [<fast>, <slow>, <signal>]}} // macd/close, FRAZIONE ±0.02
|
||||
// params: 0-3 numeri (tutti opzionali, default 12, 26, 9)
|
||||
|
||||
UNITÀ — REGOLA CRITICA per i confronti con literal numerici:
|
||||
* Confronti con literal FRAZIONALI (0.01, 0.02, 0.05): usa le varianti _pct
|
||||
Esempi CORRETTI:
|
||||
`atr_pct(14) > 0.02` "ATR > 2% del prezzo" (volatilità alta)
|
||||
`sma_pct(50) > 0.05` "close 5% sopra SMA(50)" (deviazione media)
|
||||
`macd_pct(12,26,9) > 0.005` "momentum > 0.5% del prezzo"
|
||||
Esempi ERRATI (sempre TRUE/FALSE su crypto, dead branch):
|
||||
`atr(14) > 0.02` atr in dollari (~30 su ETH) >> 0.02
|
||||
`sma(50) > 0.02` sma in dollari (~3000) >> 0.02
|
||||
`macd > 0.02` macd in dollari, ordine ±10
|
||||
* Confronti RELATIVI fra indicatori in stessa unità: usa nomi senza _pct
|
||||
Esempi: `atr(14) > sma(14)` (entrambi $), `sma(50) > sma(200)` (golden cross)
|
||||
`close > sma(50)` (entrambi $) — preferito su `sma_pct(50) > 0` (equivalente)
|
||||
* RSI usa literal 0-100 (mai frazione): `rsi(14) > 70`, `rsi(14) < 30`
|
||||
|
||||
Leaf - feature OHLCV:
|
||||
{{"kind": "feature", "name": "open|high|low|close|volume"}}
|
||||
|
||||
Leaf - feature TEMPORALI (sempre accessibili, UTC):
|
||||
{{"kind": "feature", "name": "hour"}} // range 0-23
|
||||
{{"kind": "feature", "name": "dow"}} // range 0-6 (lun=0, dom=6)
|
||||
{{"kind": "feature", "name": "is_weekend"}} // 0 o 1
|
||||
{{"kind": "feature", "name": "minute_of_hour"}} // range 0-59
|
||||
|
||||
Esempi di gating temporale:
|
||||
// Solo durante la sessione US (14:00-22:00 UTC)
|
||||
{{"op": "and", "args": [
|
||||
{{"op": "gt", "args": [{{"kind": "feature", "name": "hour"}}, {{"kind": "literal", "value": 14}}]}},
|
||||
{{"op": "lt", "args": [{{"kind": "feature", "name": "hour"}}, {{"kind": "literal", "value": 22}}]}}
|
||||
]}}
|
||||
|
||||
// Solo nel weekend (sab+dom)
|
||||
{{"op": "eq", "args": [{{"kind": "feature", "name": "is_weekend"}}, {{"kind": "literal", "value": 1}}]}}
|
||||
|
||||
Leaf - letterale numerico:
|
||||
{{"kind": "literal", "value": 70.0}}"""
|
||||
|
||||
_SYSTEM_CONSTRAINTS = """\
|
||||
VINCOLI
|
||||
- Gli indicator NON sono annidabili: 'params' accetta solo numeri, mai altri nodi.
|
||||
- Le regole sono valutate in ordine; la prima che matcha vince per ogni timestamp.
|
||||
- Default action se nessuna regola matcha = flat.
|
||||
- 'op' e 'kind' sono mutuamente esclusivi sullo stesso nodo.
|
||||
|
||||
Rispondi SOLO con il fence ```json...``` contenente l'oggetto strategy."""
|
||||
|
||||
_SYSTEM_EXAMPLE = """\
|
||||
Esempio:
|
||||
|
||||
```json
|
||||
{{
|
||||
"rules": [
|
||||
{{
|
||||
"condition": {{"op": "gt", "args": [
|
||||
{{"kind": "indicator", "name": "rsi", "params": [14]}},
|
||||
{{"kind": "literal", "value": 70.0}}
|
||||
]}},
|
||||
"action": "entry-short"
|
||||
}},
|
||||
{{
|
||||
"condition": {{"op": "lt", "args": [
|
||||
{{"kind": "indicator", "name": "rsi", "params": [14]}},
|
||||
{{"kind": "literal", "value": 30.0}}
|
||||
]}},
|
||||
"action": "entry-long"
|
||||
}}
|
||||
]
|
||||
}}
|
||||
```"""
|
||||
|
||||
|
||||
def _build_system_prompt(lib: PromptLibrary, genome: HypothesisAgentGenome) -> str:
|
||||
"""Compone il SYSTEM prompt da scaffold core + contenuto strategy-specific."""
|
||||
parts: list[str] = []
|
||||
# 1. Header strategy-specific (da prompts.json)
|
||||
parts.append(lib.agent_role)
|
||||
parts.append("")
|
||||
# 2. Cognitive style + directive (sempre, dal genome)
|
||||
parts.append(f"Il tuo stile cognitivo: {genome.cognitive_style}")
|
||||
parts.append(f"Direttiva personale: {genome.system_prompt}")
|
||||
parts.append("")
|
||||
# 3. Grammar spec (core scaffold)
|
||||
parts.append(_SYSTEM_GRAMMAR_SPEC)
|
||||
# 3b. Custom indicators spec (da prompts.json, opzionale) - estende la lista
|
||||
# "Leaf - indicatori" con firme strategy-specific non note al core.
|
||||
if lib.custom_indicators_spec:
|
||||
parts.append("\nINDICATORI CUSTOM (firme aggiuntive, applica le stesse regole grammaticali):\n")
|
||||
parts.append(lib.custom_indicators_spec)
|
||||
parts.append("")
|
||||
# 4. Pattern guidance (da prompts.json, opzionale)
|
||||
if lib.pattern_guidance:
|
||||
parts.append(
|
||||
"\nPATTERN GUIDANCE (oltre agli indicatori, considera forma delle curve e ripetibilità):\n"
|
||||
)
|
||||
parts.append(lib.pattern_guidance)
|
||||
parts.append(
|
||||
"\n Cerca pattern che si REPLICANO nei dati storici, non singoli eventi rari.\n"
|
||||
)
|
||||
# 5. Domain warnings (da prompts.json, opzionale)
|
||||
if lib.domain_warnings:
|
||||
parts.append("\nWARNING DI DOMINIO:\n")
|
||||
parts.append(lib.domain_warnings)
|
||||
parts.append("")
|
||||
# 6. Vincoli (core scaffold)
|
||||
parts.append(_SYSTEM_CONSTRAINTS)
|
||||
# 7. NEW v3.1: anti-pattern e output priorities (da prompts.json, opzionali)
|
||||
if lib.anti_patterns:
|
||||
parts.append("\nANTI-PATTERN DA EVITARE:\n")
|
||||
parts.append(lib.anti_patterns)
|
||||
parts.append("")
|
||||
if lib.output_priorities:
|
||||
parts.append("\nPRIORITA' DI OUTPUT (trade-off):\n")
|
||||
parts.append(lib.output_priorities)
|
||||
parts.append("")
|
||||
# 8. Esempio (core scaffold)
|
||||
parts.append(_SYSTEM_EXAMPLE)
|
||||
return "\n".join(parts)
|
||||
|
||||
|
||||
USER_TEMPLATE = """\
|
||||
Mercato: {symbol} timeframe {timeframe}, {n_bars} barre osservate.
|
||||
Statistiche return: mean={return_mean:.5f}, std={return_std:.5f}, \
|
||||
skew={skew:.3f}, kurt={kurtosis:.3f}.
|
||||
Regime volatilità: {volatility_regime}.
|
||||
|
||||
Regime recente (ultime 500 barre):
|
||||
autocorr_lag1: {autocorr_lag1_recent:.3f} (baseline storica: {autocorr_lag1_baseline:.3f})
|
||||
hurst: {hurst_recent:.3f} (0.5 = random walk, >0.55 trending, <0.45 mean rev)
|
||||
vol_pct: {vol_percentile:.0f}° percentile storico
|
||||
stagionalita: hour={seasonality_hour:.2f}, dow={seasonality_dow:.2f} (0-1, varianza spiegata)
|
||||
|
||||
Geometria & frattali:
|
||||
efficiency_ratio: {efficiency_ratio:.3f} (Kaufman, 0=noise, 1=trend efficiente)
|
||||
tail_index: left={tail_index_left:.2f}, right={tail_index_right:.2f} (Hill; <2 fat tail, >5 light)
|
||||
structural_uptrend: {structural_uptrend:.2f} (HH/HL score, 0.5=range)
|
||||
compression: {compression:.2f} (range recent / ref; <1 compressione, >1 espansione)
|
||||
spectral_entropy: {spectral_entropy:.2f} (0=struttura, 1=rumore bianco)
|
||||
dominant_cycle: {dominant_cycle_str} (None se spettro piatto)
|
||||
|
||||
Feature accessibili dal tuo genoma: {feature_access}.
|
||||
Lookback massimo che puoi usare nel ragionamento: {lookback_window} barre.
|
||||
|
||||
{instruction}
|
||||
"""
|
||||
|
||||
|
||||
def _render_focus_block(keys: list[str], market: MarketSummary) -> str:
|
||||
"""Renderizza 'Focus per la tua lente' come riga del USER_TEMPLATE.
|
||||
|
||||
Mappa nomi simbolici a valori della MarketSummary. Skippa silenziosamente
|
||||
chiavi sconosciute (fault-tolerant per evoluzione futura).
|
||||
"""
|
||||
field_map: dict[str, Any] = {
|
||||
# Statistiche base
|
||||
"mean": market.return_mean,
|
||||
"std": market.return_std,
|
||||
"skew": market.skew,
|
||||
"kurt": market.kurtosis,
|
||||
"vol_regime": market.volatility_regime,
|
||||
# Regime recente
|
||||
"autocorr_recent": market.autocorr_lag1_recent,
|
||||
"autocorr_baseline": market.autocorr_lag1_baseline,
|
||||
"hurst": market.hurst_recent,
|
||||
"vol_pct": market.vol_percentile,
|
||||
"seasonality_hour": market.seasonality_hour,
|
||||
"seasonality_dow": market.seasonality_dow,
|
||||
# Geometria/frattali
|
||||
"efficiency_ratio": market.efficiency_ratio,
|
||||
"tail_left": market.tail_index_left,
|
||||
"tail_right": market.tail_index_right,
|
||||
"structural_uptrend": market.structural_uptrend,
|
||||
"compression": market.compression,
|
||||
"spectral_entropy": market.spectral_entropy,
|
||||
"dominant_cycle": market.dominant_cycle,
|
||||
}
|
||||
parts: list[str] = []
|
||||
for k in keys:
|
||||
if k not in field_map:
|
||||
continue
|
||||
v = field_map[k]
|
||||
if v is None:
|
||||
parts.append(f"{k}=N/A")
|
||||
elif isinstance(v, float):
|
||||
parts.append(f"{k}={v:.3f}")
|
||||
else:
|
||||
parts.append(f"{k}={v}")
|
||||
if not parts:
|
||||
return ""
|
||||
return "\nFocus per la tua lente: " + ", ".join(parts) + "\n"
|
||||
|
||||
|
||||
_RETRY_TEMPLATE = """\
|
||||
Il JSON che hai generato contiene un errore: {previous_error}
|
||||
|
||||
Correggi e rispondi di nuovo con un singolo oggetto JSON valido
|
||||
dentro fence ```json...```, seguendo strettamente lo schema fornito.
|
||||
"""
|
||||
|
||||
|
||||
_JSON_FENCE_RE = re.compile(
|
||||
r"```(?:json)?\s*(\{[\s\S]*\})\s*```",
|
||||
re.MULTILINE,
|
||||
)
|
||||
|
||||
|
||||
def _balance_braces(s: str) -> str | None:
|
||||
"""Ritorna il prefix di ``s`` che chiude la prima ``{`` con bilanciamento.
|
||||
|
||||
Usato come fallback quando l'LLM ritorna JSON top-level senza fence ma
|
||||
seguito da prosa: troviamo dove finisce il primo oggetto e tagliamo.
|
||||
"""
|
||||
if not s.startswith("{"):
|
||||
return None
|
||||
depth = 0
|
||||
in_string = False
|
||||
escape = False
|
||||
for i, ch in enumerate(s):
|
||||
if in_string:
|
||||
if escape:
|
||||
escape = False
|
||||
elif ch == "\\":
|
||||
escape = True
|
||||
elif ch == '"':
|
||||
in_string = False
|
||||
continue
|
||||
if ch == '"':
|
||||
in_string = True
|
||||
elif ch == "{":
|
||||
depth += 1
|
||||
elif ch == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
return s[: i + 1]
|
||||
return None
|
||||
|
||||
|
||||
def _extract_json(text: str) -> str | None:
|
||||
"""Estrai un oggetto JSON dal testo del completion.
|
||||
|
||||
Strategie di estrazione, in ordine:
|
||||
1. Fence ```json...``` (greedy: cattura fino all'ultimo ``}`` prima della
|
||||
chiusura del fence).
|
||||
2. Testo che inizia direttamente con ``{`` (dopo strip), bilanciato a
|
||||
livello di parentesi graffe.
|
||||
"""
|
||||
m = _JSON_FENCE_RE.search(text)
|
||||
if m:
|
||||
return m.group(1)
|
||||
stripped = text.strip()
|
||||
return _balance_braces(stripped)
|
||||
|
||||
|
||||
def _try_parse(text: str) -> tuple[Strategy | None, str | None]:
|
||||
"""Estrai+parsea+valida. Ritorna (strategy, error). Esattamente uno e' None."""
|
||||
payload = _extract_json(text)
|
||||
if payload is None:
|
||||
return None, "no JSON object found in output"
|
||||
try:
|
||||
ast = parse_strategy(payload)
|
||||
validate_strategy(ast)
|
||||
except (ParseError, ValidationError) as e:
|
||||
return None, str(e)
|
||||
return ast, None
|
||||
|
||||
|
||||
class HypothesisAgent:
|
||||
def __init__(
|
||||
self,
|
||||
llm: LLMClient,
|
||||
max_retries: int = 1,
|
||||
prompt_library: PromptLibrary | None = None,
|
||||
):
|
||||
if max_retries < 0:
|
||||
raise ValueError("max_retries must be >= 0")
|
||||
self._llm = llm
|
||||
self._max_retries = max_retries
|
||||
self._prompt_library = prompt_library or PromptLibrary.default()
|
||||
|
||||
def propose(
|
||||
self,
|
||||
genome: HypothesisAgentGenome,
|
||||
market: MarketSummary,
|
||||
) -> HypothesisProposal:
|
||||
system = _build_system_prompt(self._prompt_library, genome)
|
||||
dominant_cycle_str = (
|
||||
f"{market.dominant_cycle:.0f} barre"
|
||||
if market.dominant_cycle is not None
|
||||
else "N/A (spettro piatto)"
|
||||
)
|
||||
focus_keys = self._prompt_library.focus_metrics_for(genome.cognitive_style)
|
||||
focus_block = _render_focus_block(focus_keys, market) if focus_keys else ""
|
||||
instruction = (
|
||||
self._prompt_library.instruction
|
||||
or "Genera una strategia che cerchi anomalie sfruttabili in questo regime."
|
||||
)
|
||||
original_user = USER_TEMPLATE.format(
|
||||
symbol=market.symbol,
|
||||
timeframe=market.timeframe,
|
||||
n_bars=market.n_bars,
|
||||
return_mean=market.return_mean,
|
||||
return_std=market.return_std,
|
||||
skew=market.skew,
|
||||
kurtosis=market.kurtosis,
|
||||
volatility_regime=market.volatility_regime,
|
||||
autocorr_lag1_recent=market.autocorr_lag1_recent,
|
||||
autocorr_lag1_baseline=market.autocorr_lag1_baseline,
|
||||
hurst_recent=market.hurst_recent,
|
||||
vol_percentile=market.vol_percentile,
|
||||
seasonality_hour=market.seasonality_hour,
|
||||
seasonality_dow=market.seasonality_dow,
|
||||
efficiency_ratio=market.efficiency_ratio,
|
||||
tail_index_left=market.tail_index_left,
|
||||
tail_index_right=market.tail_index_right,
|
||||
structural_uptrend=market.structural_uptrend,
|
||||
compression=market.compression,
|
||||
spectral_entropy=market.spectral_entropy,
|
||||
dominant_cycle_str=dominant_cycle_str,
|
||||
feature_access=", ".join(genome.feature_access),
|
||||
lookback_window=genome.lookback_window,
|
||||
instruction=instruction,
|
||||
) + focus_block
|
||||
|
||||
completions: list[CompletionResult] = []
|
||||
errors: list[str] = []
|
||||
last_raw = ""
|
||||
max_attempts = 1 + self._max_retries
|
||||
session_id: str | None = None
|
||||
|
||||
try:
|
||||
for attempt in range(max_attempts):
|
||||
if attempt == 0:
|
||||
user = original_user
|
||||
req_session_id = "new"
|
||||
else:
|
||||
user = _RETRY_TEMPLATE.format(previous_error=errors[-1])
|
||||
req_session_id = session_id or "new"
|
||||
|
||||
try:
|
||||
completion = self._llm.complete(
|
||||
genome, system=system, user=user,
|
||||
session_id=req_session_id,
|
||||
summarize=True,
|
||||
)
|
||||
except EmptyCompletionError as e:
|
||||
errors.append(f"empty_completion: {e}")
|
||||
last_raw = ""
|
||||
continue
|
||||
except OpusAgentTransientError as e:
|
||||
errors.append(f"transient_error: {e}")
|
||||
last_raw = ""
|
||||
continue
|
||||
except OpusAgentError as e:
|
||||
errors.append(f"opus_agent_error: {e}")
|
||||
last_raw = ""
|
||||
continue
|
||||
completions.append(completion)
|
||||
last_raw = completion.text
|
||||
if completion.session_id:
|
||||
session_id = completion.session_id
|
||||
|
||||
strategy, err = _try_parse(completion.text)
|
||||
if strategy is not None:
|
||||
return HypothesisProposal(
|
||||
strategy=strategy,
|
||||
raw_text=completion.text,
|
||||
completions=completions,
|
||||
parse_error=None,
|
||||
n_attempts=len(completions),
|
||||
)
|
||||
assert err is not None
|
||||
errors.append(err)
|
||||
finally:
|
||||
if session_id:
|
||||
self._llm.close_session(session_id)
|
||||
|
||||
chained = " | ".join(
|
||||
f"attempt {i + 1}: {e}" for i, e in enumerate(errors)
|
||||
)
|
||||
return HypothesisProposal(
|
||||
strategy=None,
|
||||
raw_text=last_raw,
|
||||
completions=completions,
|
||||
parse_error=chained,
|
||||
n_attempts=len(completions),
|
||||
)
|
||||
@@ -0,0 +1,77 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
from scipy import stats # type: ignore[import-untyped]
|
||||
|
||||
from ..metrics.basic import (
|
||||
autocorr_lag1,
|
||||
compression_ratio,
|
||||
efficiency_ratio_kaufman,
|
||||
hurst_exponent,
|
||||
seasonality_strength,
|
||||
spectral_entropy_and_cycle,
|
||||
structural_uptrend_score,
|
||||
tail_index_hill,
|
||||
vol_percentile_historical,
|
||||
)
|
||||
from .hypothesis import MarketSummary
|
||||
|
||||
|
||||
def build_market_summary(
|
||||
ohlcv: pd.DataFrame,
|
||||
symbol: str,
|
||||
timeframe: str,
|
||||
) -> MarketSummary:
|
||||
returns = ohlcv["close"].pct_change().dropna()
|
||||
return_mean = float(returns.mean())
|
||||
return_std = float(returns.std(ddof=1))
|
||||
skew = float(stats.skew(returns, bias=False))
|
||||
kurt = float(stats.kurtosis(returns, fisher=True, bias=False))
|
||||
|
||||
if return_std < 0.005:
|
||||
regime = "low"
|
||||
elif return_std < 0.02:
|
||||
regime = "medium"
|
||||
else:
|
||||
regime = "high"
|
||||
|
||||
recent_window = min(500, len(returns))
|
||||
recent_returns = returns.iloc[-recent_window:]
|
||||
|
||||
autocorr_recent = autocorr_lag1(recent_returns)
|
||||
autocorr_baseline = autocorr_lag1(returns)
|
||||
hurst_r = hurst_exponent(recent_returns)
|
||||
vol_pct = vol_percentile_historical(returns, current_window=24, ref_window=2000)
|
||||
season_h = seasonality_strength(returns, by="hour")
|
||||
season_d = seasonality_strength(returns, by="dow")
|
||||
|
||||
eff_ratio = efficiency_ratio_kaufman(ohlcv["close"], length=100)
|
||||
tail_l = tail_index_hill(returns, side="left")
|
||||
tail_r = tail_index_hill(returns, side="right")
|
||||
hh_hl = structural_uptrend_score(ohlcv["close"], window=5)
|
||||
compress = compression_ratio(ohlcv["close"], recent_window=50, ref_window=200)
|
||||
spec_entropy, dom_cycle = spectral_entropy_and_cycle(returns, length=256)
|
||||
|
||||
return MarketSummary(
|
||||
symbol=symbol,
|
||||
timeframe=timeframe,
|
||||
n_bars=len(ohlcv),
|
||||
return_mean=return_mean,
|
||||
return_std=return_std,
|
||||
skew=skew,
|
||||
kurtosis=kurt,
|
||||
volatility_regime=regime,
|
||||
autocorr_lag1_recent=autocorr_recent,
|
||||
autocorr_lag1_baseline=autocorr_baseline,
|
||||
hurst_recent=hurst_r,
|
||||
vol_percentile=vol_pct,
|
||||
seasonality_hour=season_h,
|
||||
seasonality_dow=season_d,
|
||||
efficiency_ratio=eff_ratio,
|
||||
tail_index_left=tail_l,
|
||||
tail_index_right=tail_r,
|
||||
structural_uptrend=hh_hl,
|
||||
compression=compress,
|
||||
spectral_entropy=spec_entropy,
|
||||
dominant_cycle=dom_cycle,
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
"""Backtest layer: order/position/trade dataclasses + engine."""
|
||||
|
||||
from .engine import BacktestEngine, BacktestResult
|
||||
from .orders import Order, Position, Side, Trade
|
||||
|
||||
__all__ = [
|
||||
"BacktestEngine",
|
||||
"BacktestResult",
|
||||
"Order",
|
||||
"Position",
|
||||
"Side",
|
||||
"Trade",
|
||||
]
|
||||
@@ -0,0 +1,138 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from .orders import Position, Side, Trade
|
||||
|
||||
Signal = Side # alias semantico
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BacktestResult:
|
||||
equity_curve: pd.Series
|
||||
returns: pd.Series
|
||||
trades: list[Trade]
|
||||
|
||||
|
||||
class BacktestEngine:
|
||||
"""Engine event-driven sincrono: itera bar per bar, applica segnali con
|
||||
delay di 1 bar (segnale a t -> eseguito a t+1 open) per evitare lookahead.
|
||||
|
||||
Position sizing: 1 unit per posizione. Fees applicati su entry+exit.
|
||||
Niente leva, niente liquidation, niente funding (semplificazione Phase 1).
|
||||
"""
|
||||
|
||||
def __init__(self, fees_bp: float = 5.0) -> None:
|
||||
self.fees_bp = fees_bp
|
||||
|
||||
def run(self, ohlcv: pd.DataFrame, signals: pd.Series) -> BacktestResult:
|
||||
n = len(ohlcv)
|
||||
if n == 0:
|
||||
empty = pd.Series([], dtype=float)
|
||||
return BacktestResult(equity_curve=empty, returns=empty, trades=[])
|
||||
|
||||
signals = signals.reindex(ohlcv.index).ffill().fillna(Side.FLAT)
|
||||
# Esecuzione con delay 1: segnale a t-1 esegue a open di t.
|
||||
executed = pd.Series(
|
||||
[Side.FLAT, *list(signals.iloc[:-1])],
|
||||
index=ohlcv.index,
|
||||
dtype=object,
|
||||
)
|
||||
|
||||
# Codifica side in int per vectorizzazione: 0=FLAT, +1=LONG, -1=SHORT.
|
||||
side_code = np.where(
|
||||
executed.values == Side.LONG, 1,
|
||||
np.where(executed.values == Side.SHORT, -1, 0),
|
||||
).astype(np.int8)
|
||||
opens = ohlcv["open"].to_numpy(dtype=np.float64)
|
||||
closes = ohlcv["close"].to_numpy(dtype=np.float64)
|
||||
ts_index = ohlcv.index
|
||||
|
||||
# Identifica transizioni: punto in cui side[i] != side[i-1] (con side[-1]=0).
|
||||
prev = np.concatenate(([0], side_code[:-1]))
|
||||
change = side_code != prev
|
||||
|
||||
# Indici di entry (cambio verso side != 0).
|
||||
entry_idxs = np.flatnonzero(change & (side_code != 0))
|
||||
# Indici di chiusura: per ogni entry, il prossimo indice dove side[i] != side_entry.
|
||||
# Vectorized: per ogni entry_idx, cerca change & side != side_entry oltre l'entry.
|
||||
|
||||
trades: list[Trade] = []
|
||||
# realized_pnl[t]: PnL netto cumulato dopo le chiusure avvenute a OPEN di t.
|
||||
realized_pnl = np.zeros(n, dtype=np.float64)
|
||||
fees_rate = self.fees_bp / 10000.0
|
||||
size = 1.0
|
||||
|
||||
# Posizione corrente all'inizio di ogni indice t (prima di applicare il transitorio):
|
||||
# used per MtM computation. open_side_at_t / open_entry_at_t.
|
||||
open_side = np.zeros(n, dtype=np.int8)
|
||||
open_entry = np.zeros(n, dtype=np.float64)
|
||||
|
||||
for entry_idx in entry_idxs:
|
||||
entry_side = int(side_code[entry_idx])
|
||||
entry_price = opens[entry_idx]
|
||||
# Cerca exit: primo indice > entry_idx dove side differisce.
|
||||
after = side_code[entry_idx + 1:]
|
||||
rel = np.flatnonzero(after != entry_side)
|
||||
if rel.size > 0:
|
||||
exit_idx = entry_idx + 1 + int(rel[0])
|
||||
exit_price = opens[exit_idx]
|
||||
exit_ts = ts_index[exit_idx]
|
||||
gross = entry_side * (exit_price - entry_price) * size
|
||||
fees = fees_rate * size * (entry_price + exit_price)
|
||||
net = gross - fees
|
||||
# La chiusura avviene a open[exit_idx]: dal bar exit_idx in poi il
|
||||
# PnL e' realizzato (non piu' MtM).
|
||||
realized_pnl[exit_idx:] += net
|
||||
# Posizione aperta in [entry_idx, exit_idx-1].
|
||||
open_side[entry_idx:exit_idx] = entry_side
|
||||
open_entry[entry_idx:exit_idx] = entry_price
|
||||
trades.append(Trade(
|
||||
entry_ts=ts_index[entry_idx],
|
||||
exit_ts=exit_ts,
|
||||
side=Side.LONG if entry_side == 1 else Side.SHORT,
|
||||
size=size,
|
||||
entry_price=entry_price,
|
||||
exit_price=exit_price,
|
||||
fees_bp=self.fees_bp,
|
||||
))
|
||||
else:
|
||||
# Ultima posizione ancora aperta: chiusura forced a close[-1].
|
||||
# Parita' col loop legacy: MtM su [entry_idx, n-1), realized totale
|
||||
# SOLO al bar n-1 (legacy fa equity_history[-1] = equity).
|
||||
last_close = closes[-1]
|
||||
gross = entry_side * (last_close - entry_price) * size
|
||||
fees = fees_rate * size * (entry_price + last_close)
|
||||
net = gross - fees
|
||||
if entry_idx < n - 1:
|
||||
open_side[entry_idx:n - 1] = entry_side
|
||||
open_entry[entry_idx:n - 1] = entry_price
|
||||
realized_pnl[-1] += net
|
||||
trades.append(Trade(
|
||||
entry_ts=ts_index[entry_idx],
|
||||
exit_ts=ts_index[-1],
|
||||
side=Side.LONG if entry_side == 1 else Side.SHORT,
|
||||
size=size,
|
||||
entry_price=entry_price,
|
||||
exit_price=last_close,
|
||||
fees_bp=self.fees_bp,
|
||||
))
|
||||
|
||||
# MtM unrealized per ogni bar in cui c'e' una posizione aperta.
|
||||
mtm = open_side.astype(np.float64) * (closes - open_entry) * size
|
||||
equity_arr = realized_pnl + mtm
|
||||
# Returns = first diff dell'equity (col loop legacy il primo bar e' equity[0]-0).
|
||||
returns_arr = np.concatenate(([equity_arr[0]], np.diff(equity_arr)))
|
||||
|
||||
return BacktestResult(
|
||||
equity_curve=pd.Series(equity_arr, index=ts_index, name="equity"),
|
||||
returns=pd.Series(returns_arr, index=ts_index, name="returns"),
|
||||
trades=trades,
|
||||
)
|
||||
|
||||
|
||||
# Lo facade Position re-export e' tenuto per backward-compat con import legacy.
|
||||
__all__ = ["BacktestEngine", "BacktestResult", "Position", "Side", "Signal", "Trade"]
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Cerbero data-provider HTTP client."""
|
||||
|
||||
from .client import CerberoClient
|
||||
|
||||
__all__ = ["CerberoClient"]
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Pydantic settings loader for Multi_Swarm_Coevolutive.
|
||||
|
||||
Loads configuration from environment variables and an optional ``.env`` file
|
||||
in the project root. Required secrets are validated at instantiation time.
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import AliasChoices, Field, SecretStr
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=".env",
|
||||
env_file_encoding="utf-8",
|
||||
extra="ignore",
|
||||
case_sensitive=False,
|
||||
)
|
||||
|
||||
cerbero_base_url: str = "http://localhost:9000"
|
||||
cerbero_testnet_token: SecretStr
|
||||
cerbero_mainnet_token: SecretStr | None = None
|
||||
cerbero_bot_tag: str = "swarm-poc-phase1"
|
||||
|
||||
opus_agent_api_key: SecretStr
|
||||
opus_agent_base_url: str = "https://opus-agent.tielogic.xyz"
|
||||
|
||||
llm_model_tier_s: str = "claude-opus-4-7"
|
||||
llm_model_tier_a: str = "claude-opus-4-7"
|
||||
llm_model_tier_b: str = "claude-sonnet-4-6"
|
||||
llm_model_tier_c: str = "claude-sonnet-4-6"
|
||||
llm_model_tier_d: str = "claude-haiku-4-5-20251001"
|
||||
|
||||
run_name: str = "phase1-spike-001"
|
||||
data_dir: Path = Field(default=Path("./data"))
|
||||
series_dir: Path = Field(default=Path("./series"))
|
||||
|
||||
# GA core DB (tabelle universali: runs, generations, genomes, evaluations, ...)
|
||||
# Alias DB_PATH legacy mantenuto per backcompat (deprecato, rimuovere nei prossimi cicli).
|
||||
ga_db_path: Path = Field(
|
||||
default=Path("./state/runs.db"),
|
||||
validation_alias=AliasChoices("GA_DB_PATH", "DB_PATH"),
|
||||
)
|
||||
|
||||
# DB per la strategia crypto (tabelle paper_trading_*, isolato dal core)
|
||||
strategy_crypto_db_path: Path = Field(
|
||||
default=Path("./state/strategy_crypto.db"),
|
||||
validation_alias="STRATEGY_CRYPTO_DB_PATH",
|
||||
)
|
||||
|
||||
@property
|
||||
def db_path(self) -> Path:
|
||||
"""Backcompat alias: legge ga_db_path. Deprecato — usare ga_db_path."""
|
||||
return self.ga_db_path
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
# Required fields are populated from environment / .env, not init kwargs.
|
||||
return Settings() # type: ignore[call-arg]
|
||||
+15
-1
@@ -1,3 +1,8 @@
|
||||
"""GA data access functions for the core dashboard.
|
||||
|
||||
Reads exclusively from runs.db (GA tables).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
@@ -6,7 +11,16 @@ from typing import Any
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from ..persistence.repository import Repository
|
||||
from multi_swarm_core.persistence.repository import Repository
|
||||
|
||||
__all__ = [
|
||||
"get_repo",
|
||||
"list_runs_df",
|
||||
"get_run_overview",
|
||||
"generations_df",
|
||||
"evaluations_df",
|
||||
"genomes_df",
|
||||
]
|
||||
|
||||
|
||||
def get_repo(db_path: str | Path) -> Repository:
|
||||
@@ -0,0 +1,563 @@
|
||||
"""Multi-Swarm Core Dashboard — GA pages: /, /convergence, /genomes.
|
||||
|
||||
Avvio: ``uv run python -m multi_swarm_core.dashboard.nicegui_app``
|
||||
Legge SOLO runs.db (tabelle GA).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import plotly.graph_objects as go # type: ignore[import-untyped]
|
||||
from nicegui import app, ui
|
||||
|
||||
from multi_swarm_core.dashboard.data import (
|
||||
evaluations_df,
|
||||
generations_df,
|
||||
genomes_df,
|
||||
get_repo,
|
||||
get_run_overview,
|
||||
list_runs_df,
|
||||
)
|
||||
from multi_swarm_core.dashboard.theme import (
|
||||
COLOR_ACCENT,
|
||||
COLOR_PRIMARY,
|
||||
COLOR_SECONDARY,
|
||||
COLOR_SURFACE,
|
||||
COLOR_TEXT,
|
||||
COLOR_TEXT_MUTED,
|
||||
_STATUS_BADGE,
|
||||
_apply_theme,
|
||||
_build_header,
|
||||
_json_to_html,
|
||||
)
|
||||
|
||||
GA_DB_PATH = os.environ.get("GA_DB_PATH", "./state/runs.db")
|
||||
DASHBOARD_ROOT_PATH = os.environ.get("DASHBOARD_ROOT_PATH", "")
|
||||
REFRESH_INTERVAL_S = 3.0
|
||||
|
||||
|
||||
def _runs_options() -> dict[str, str]:
|
||||
repo = get_repo(GA_DB_PATH)
|
||||
runs = list_runs_df(repo)
|
||||
if runs.empty:
|
||||
return {}
|
||||
return {
|
||||
row["id"]: f"{row['name']} — {row['status']} ({row['started_at'][:16]})"
|
||||
for _, row in runs.iterrows()
|
||||
}
|
||||
|
||||
|
||||
def _snapshot(run_id: str) -> dict[str, Any]:
|
||||
repo = get_repo(GA_DB_PATH)
|
||||
ov = get_run_overview(repo, run_id)
|
||||
evals = evaluations_df(repo, run_id)
|
||||
gens = generations_df(repo, run_id)
|
||||
|
||||
cfg = ov["config"]
|
||||
pop_size = int(cfg.get("population_size", 0))
|
||||
n_gens = int(cfg.get("n_generations", 0))
|
||||
evals_total = max(pop_size * n_gens, 1)
|
||||
evals_done = len(evals)
|
||||
gens_done = int(gens["completed_at"].notna().sum()) if not gens.empty else 0
|
||||
live_cost = float(repo.total_cost(run_id)) if ov["status"] == "running" else float(
|
||||
ov["total_cost_usd"]
|
||||
)
|
||||
|
||||
top_fit = float(evals["fitness"].max()) if evals_done else float("nan")
|
||||
median_fit = float(evals["fitness"].median()) if evals_done else float("nan")
|
||||
parse_success = (
|
||||
100.0 * float(evals["parse_error"].isna().sum()) / evals_done if evals_done else 0.0
|
||||
)
|
||||
|
||||
return {
|
||||
"status": ov["status"],
|
||||
"name": cfg.get("run_name", "—"),
|
||||
"started_at": ov["started_at"],
|
||||
"completed_at": ov["completed_at"] or "—",
|
||||
"cost_usd": live_cost,
|
||||
"pop_size": pop_size,
|
||||
"n_gens": n_gens,
|
||||
"evals_done": evals_done,
|
||||
"evals_total": evals_total,
|
||||
"gens_done": gens_done,
|
||||
"top_fit": top_fit,
|
||||
"median_fit": median_fit,
|
||||
"parse_success": parse_success,
|
||||
"config": cfg,
|
||||
"gens_df": gens,
|
||||
}
|
||||
|
||||
|
||||
def _convergence_figure(gens_df: Any) -> go.Figure:
|
||||
fig = go.Figure()
|
||||
if gens_df.empty:
|
||||
fig.add_annotation(
|
||||
text="Nessuna generazione registrata", x=0.5, y=0.5, showarrow=False,
|
||||
font={"color": COLOR_TEXT_MUTED, "size": 14},
|
||||
)
|
||||
else:
|
||||
fig.add_trace(
|
||||
go.Scatter(
|
||||
x=gens_df["generation_idx"], y=gens_df["fitness_max"],
|
||||
name="max", mode="lines+markers",
|
||||
line={"color": COLOR_PRIMARY, "width": 3, "shape": "spline", "smoothing": 0.6},
|
||||
marker={"size": 9, "color": COLOR_PRIMARY,
|
||||
"line": {"color": "#fff", "width": 1}},
|
||||
fill="tozeroy",
|
||||
fillcolor="rgba(255, 45, 135, 0.12)",
|
||||
)
|
||||
)
|
||||
fig.add_trace(
|
||||
go.Scatter(
|
||||
x=gens_df["generation_idx"], y=gens_df["fitness_p90"],
|
||||
name="p90", mode="lines+markers",
|
||||
line={"color": COLOR_ACCENT, "width": 2, "dash": "dot", "shape": "spline"},
|
||||
marker={"size": 7, "color": COLOR_ACCENT},
|
||||
)
|
||||
)
|
||||
fig.add_trace(
|
||||
go.Scatter(
|
||||
x=gens_df["generation_idx"], y=gens_df["fitness_median"],
|
||||
name="median", mode="lines+markers",
|
||||
line={"color": COLOR_SECONDARY, "width": 2, "shape": "spline"},
|
||||
marker={"size": 7, "color": COLOR_SECONDARY},
|
||||
)
|
||||
)
|
||||
fig.update_layout(
|
||||
template="plotly_dark",
|
||||
paper_bgcolor=COLOR_SURFACE,
|
||||
plot_bgcolor=COLOR_SURFACE,
|
||||
font={"color": COLOR_TEXT},
|
||||
xaxis={"title": "generation", "gridcolor": "rgba(148, 163, 184, 0.08)", "dtick": 1},
|
||||
yaxis={"title": "fitness", "gridcolor": "rgba(148, 163, 184, 0.08)"},
|
||||
title={"text": "Fitness convergence", "font": {"color": COLOR_TEXT, "size": 18}},
|
||||
legend={"bgcolor": "rgba(19, 19, 26, 0.95)", "bordercolor": COLOR_PRIMARY, "borderwidth": 1},
|
||||
margin={"l": 50, "r": 30, "t": 50, "b": 50},
|
||||
)
|
||||
return fig
|
||||
|
||||
|
||||
def _entropy_figure(gens_df: Any) -> go.Figure:
|
||||
fig = go.Figure()
|
||||
if not gens_df.empty:
|
||||
fig.add_trace(
|
||||
go.Scatter(
|
||||
x=gens_df["generation_idx"], y=gens_df["entropy"],
|
||||
mode="lines+markers",
|
||||
line={"color": COLOR_SECONDARY, "width": 3, "shape": "spline", "smoothing": 0.6},
|
||||
marker={"size": 9, "color": COLOR_SECONDARY,
|
||||
"line": {"color": "#fff", "width": 1}},
|
||||
fill="tozeroy",
|
||||
fillcolor="rgba(0, 217, 255, 0.12)",
|
||||
name="entropy",
|
||||
)
|
||||
)
|
||||
fig.add_hline(
|
||||
y=0.5, line_dash="dash", line_color=COLOR_ACCENT,
|
||||
annotation_text="gate threshold (0.5)",
|
||||
annotation_font_color=COLOR_ACCENT,
|
||||
)
|
||||
fig.update_layout(
|
||||
template="plotly_dark",
|
||||
paper_bgcolor=COLOR_SURFACE,
|
||||
plot_bgcolor=COLOR_SURFACE,
|
||||
font={"color": COLOR_TEXT},
|
||||
xaxis={"title": "generation", "gridcolor": "rgba(148, 163, 184, 0.08)", "dtick": 1},
|
||||
yaxis={"title": "entropy", "gridcolor": "rgba(148, 163, 184, 0.08)"},
|
||||
title={"text": "Diversity (fitness entropy)", "font": {"color": COLOR_TEXT, "size": 18}},
|
||||
margin={"l": 50, "r": 30, "t": 50, "b": 50},
|
||||
)
|
||||
return fig
|
||||
|
||||
|
||||
@ui.page("/")
|
||||
def index() -> None:
|
||||
_apply_theme()
|
||||
_build_header(
|
||||
active="/",
|
||||
brand_subtitle="Coevolutivo / GA",
|
||||
nav_items=[("/", "Overview"), ("/convergence", "Convergence"), ("/genomes", "Genomes")],
|
||||
db_label=f"⛁ {Path(GA_DB_PATH).resolve().name}",
|
||||
)
|
||||
|
||||
options = _runs_options()
|
||||
if not options:
|
||||
ui.label("Nessuna run nel database.").classes("text-h5")
|
||||
return
|
||||
|
||||
state: dict[str, Any] = {"run_id": next(iter(options))}
|
||||
|
||||
with ui.row().classes("w-full items-center gap-4 q-mb-md"):
|
||||
select = ui.select(options=options, value=state["run_id"], label="Run").classes(
|
||||
"flex-grow"
|
||||
)
|
||||
status_badge = ui.badge("…", color="primary").classes("text-body1 q-pa-sm")
|
||||
ui.button("🔄 Refresh", on_click=lambda: refresh()).props("outline color=primary")
|
||||
|
||||
with ui.card().classes("w-full"):
|
||||
ui.label("Progresso run").classes("text-subtitle1")
|
||||
gen_label = ui.label("Generations: 0/0")
|
||||
gen_bar = ui.linear_progress(0.0, show_value=False).props("size=20px color=primary")
|
||||
eval_label = ui.label("Evaluations: 0/0 (0.0%)")
|
||||
eval_bar = ui.linear_progress(0.0, show_value=False).props("size=20px color=accent")
|
||||
|
||||
with ui.row().classes("w-full gap-4"):
|
||||
with ui.card().classes("flex-grow metric-card accent-cyan"):
|
||||
ui.label("Top fitness").classes("text-caption")
|
||||
top_lbl = ui.label("—").classes("text-h4")
|
||||
with ui.card().classes("flex-grow metric-card accent-purple"):
|
||||
ui.label("Median fitness").classes("text-caption")
|
||||
median_lbl = ui.label("—").classes("text-h4")
|
||||
with ui.card().classes("flex-grow metric-card accent-amber"):
|
||||
ui.label("Parse success").classes("text-caption")
|
||||
parse_lbl = ui.label("—").classes("text-h4")
|
||||
with ui.card().classes("flex-grow metric-card accent-green"):
|
||||
ui.label("Cost (USD)").classes("text-caption")
|
||||
cost_lbl = ui.label("—").classes("text-h4")
|
||||
|
||||
with ui.row().classes("w-full gap-4 q-mt-md"):
|
||||
started_lbl = ui.label("Started: —")
|
||||
completed_lbl = ui.label("Completed: —")
|
||||
ui.separator()
|
||||
ui.label("Config").classes("text-subtitle1")
|
||||
cfg_code = ui.html('<pre class="config-block"></pre>').classes("w-full")
|
||||
|
||||
def refresh() -> None:
|
||||
run_id = select.value
|
||||
if not run_id:
|
||||
return
|
||||
try:
|
||||
s = _snapshot(run_id)
|
||||
except Exception as e: # noqa: BLE001
|
||||
ui.notify(f"Errore: {e}", type="negative")
|
||||
return
|
||||
|
||||
text, color = _STATUS_BADGE.get(s["status"], (s["status"], "primary"))
|
||||
status_badge.text = text
|
||||
status_badge.props(f"color={color}")
|
||||
|
||||
gen_frac = min(s["gens_done"] / max(s["n_gens"], 1), 1.0)
|
||||
eval_frac = min(s["evals_done"] / s["evals_total"], 1.0)
|
||||
gen_bar.value = gen_frac
|
||||
eval_bar.value = eval_frac
|
||||
gen_label.text = f"Generations: {s['gens_done']}/{s['n_gens']}"
|
||||
eval_label.text = (
|
||||
f"Evaluations: {s['evals_done']}/{s['evals_total']} ({100 * eval_frac:.1f}%)"
|
||||
)
|
||||
|
||||
top_lbl.text = f"{s['top_fit']:.4f}" if s["evals_done"] else "—"
|
||||
median_lbl.text = f"{s['median_fit']:.4f}" if s["evals_done"] else "—"
|
||||
parse_lbl.text = f"{s['parse_success']:.1f}%" if s["evals_done"] else "—"
|
||||
cost_lbl.text = f"${s['cost_usd']:.4f}"
|
||||
|
||||
started_lbl.text = f"Started: {s['started_at']}"
|
||||
completed_lbl.text = f"Completed: {s['completed_at']}"
|
||||
cfg_code.content = f'<pre class="config-block">{_json_to_html(s["config"])}</pre>'
|
||||
|
||||
def on_select_change() -> None:
|
||||
state["run_id"] = select.value
|
||||
refresh()
|
||||
|
||||
select.on_value_change(on_select_change)
|
||||
_timer = ui.timer(REFRESH_INTERVAL_S, refresh)
|
||||
ui.context.client.on_disconnect(_timer.deactivate)
|
||||
refresh()
|
||||
|
||||
|
||||
@ui.page("/convergence")
|
||||
def convergence() -> None:
|
||||
_apply_theme()
|
||||
_build_header(
|
||||
active="/convergence",
|
||||
brand_subtitle="Coevolutivo / GA",
|
||||
nav_items=[("/", "Overview"), ("/convergence", "Convergence"), ("/genomes", "Genomes")],
|
||||
db_label=f"⛁ {Path(GA_DB_PATH).resolve().name}",
|
||||
)
|
||||
|
||||
options = _runs_options()
|
||||
if not options:
|
||||
ui.label("Nessuna run nel database.").classes("text-h5")
|
||||
return
|
||||
|
||||
state: dict[str, Any] = {"run_id": next(iter(options))}
|
||||
|
||||
with ui.row().classes("w-full items-center gap-4 q-mb-md"):
|
||||
select = ui.select(options=options, value=state["run_id"], label="Run").classes(
|
||||
"flex-grow"
|
||||
)
|
||||
gen_count_lbl = ui.label("Gens: 0/0").classes("text-body1").style(
|
||||
f"color: {COLOR_PRIMARY}; font-weight: 600;"
|
||||
)
|
||||
ui.button("🔄 Refresh", on_click=lambda: refresh()).props("outline color=primary")
|
||||
|
||||
fitness_plot = ui.plotly(_convergence_figure(generations_df(get_repo(GA_DB_PATH), state["run_id"]))).classes("w-full")
|
||||
entropy_plot = ui.plotly(_entropy_figure(generations_df(get_repo(GA_DB_PATH), state["run_id"]))).classes("w-full q-mt-md")
|
||||
|
||||
ui.separator()
|
||||
ui.label("Tabella generazioni").classes("text-subtitle1 q-mt-md")
|
||||
gens_table = ui.table(
|
||||
columns=[
|
||||
{"name": "generation_idx", "label": "gen", "field": "generation_idx", "sortable": True},
|
||||
{"name": "n_genomes", "label": "n", "field": "n_genomes"},
|
||||
{"name": "fitness_max", "label": "max", "field": "fitness_max"},
|
||||
{"name": "fitness_p90", "label": "p90", "field": "fitness_p90"},
|
||||
{"name": "fitness_median", "label": "median", "field": "fitness_median"},
|
||||
{"name": "entropy", "label": "entropy", "field": "entropy"},
|
||||
{"name": "completed_at", "label": "completed", "field": "completed_at"},
|
||||
],
|
||||
rows=[],
|
||||
row_key="generation_idx",
|
||||
).classes("w-full")
|
||||
|
||||
def refresh() -> None:
|
||||
run_id = select.value
|
||||
if not run_id:
|
||||
return
|
||||
try:
|
||||
gens = generations_df(get_repo(GA_DB_PATH), run_id)
|
||||
ov = get_run_overview(get_repo(GA_DB_PATH), run_id)
|
||||
except Exception as e: # noqa: BLE001
|
||||
ui.notify(f"Errore: {e}", type="negative")
|
||||
return
|
||||
|
||||
n_gens = int(ov["config"].get("n_generations", 0))
|
||||
gens_done = int(gens["completed_at"].notna().sum()) if not gens.empty else 0
|
||||
gen_count_lbl.text = f"Gens: {gens_done}/{n_gens}"
|
||||
|
||||
fitness_plot.update_figure(_convergence_figure(gens))
|
||||
entropy_plot.update_figure(_entropy_figure(gens))
|
||||
|
||||
if gens.empty:
|
||||
gens_table.rows = []
|
||||
else:
|
||||
display_cols = [
|
||||
"generation_idx", "n_genomes",
|
||||
"fitness_max", "fitness_p90", "fitness_median",
|
||||
"entropy", "completed_at",
|
||||
]
|
||||
gens_table.rows = [
|
||||
{
|
||||
col: (round(v, 6) if isinstance(v, float) else v)
|
||||
for col, v in row.items()
|
||||
if col in display_cols
|
||||
}
|
||||
for _, row in gens.iterrows()
|
||||
]
|
||||
gens_table.update()
|
||||
|
||||
def on_select_change() -> None:
|
||||
state["run_id"] = select.value
|
||||
refresh()
|
||||
|
||||
select.on_value_change(on_select_change)
|
||||
_timer = ui.timer(REFRESH_INTERVAL_S, refresh)
|
||||
ui.context.client.on_disconnect(_timer.deactivate)
|
||||
refresh()
|
||||
|
||||
|
||||
@ui.page("/genomes")
|
||||
def genomes() -> None:
|
||||
_apply_theme()
|
||||
_build_header(
|
||||
active="/genomes",
|
||||
brand_subtitle="Coevolutivo / GA",
|
||||
nav_items=[("/", "Overview"), ("/convergence", "Convergence"), ("/genomes", "Genomes")],
|
||||
db_label=f"⛁ {Path(GA_DB_PATH).resolve().name}",
|
||||
)
|
||||
|
||||
options = _runs_options()
|
||||
if not options:
|
||||
ui.label("Nessuna run nel database.").classes("text-h5")
|
||||
return
|
||||
|
||||
state: dict[str, Any] = {
|
||||
"run_id": next(iter(options)),
|
||||
"selected_gid": None,
|
||||
"merged": None,
|
||||
}
|
||||
|
||||
with ui.row().classes("w-full items-center gap-4 q-mb-md"):
|
||||
select = ui.select(options=options, value=state["run_id"], label="Run").classes(
|
||||
"flex-grow"
|
||||
)
|
||||
top_k_select = ui.select(
|
||||
options={10: "Top 10", 25: "Top 25", 50: "Top 50"},
|
||||
value=10,
|
||||
label="Top K",
|
||||
)
|
||||
ui.button("🔄 Refresh", on_click=lambda: refresh()).props("outline color=primary")
|
||||
|
||||
ui.label("Top genomi per fitness").classes("text-subtitle1 q-mt-sm")
|
||||
top_table = ui.table(
|
||||
columns=[
|
||||
{"name": "genome_id", "label": "id", "field": "genome_id", "align": "left"},
|
||||
{"name": "fitness", "label": "fitness", "field": "fitness", "sortable": True},
|
||||
{"name": "dsr", "label": "DSR", "field": "dsr"},
|
||||
{"name": "sharpe", "label": "Sharpe", "field": "sharpe"},
|
||||
{"name": "max_dd", "label": "max DD", "field": "max_dd"},
|
||||
{"name": "n_trades", "label": "trades", "field": "n_trades"},
|
||||
{"name": "cognitive_style", "label": "style", "field": "cognitive_style"},
|
||||
{"name": "temperature", "label": "T", "field": "temperature"},
|
||||
{"name": "lookback_window", "label": "lookback", "field": "lookback_window"},
|
||||
],
|
||||
rows=[],
|
||||
row_key="genome_id",
|
||||
selection="single",
|
||||
).classes("w-full")
|
||||
|
||||
ui.separator().classes("q-my-md")
|
||||
|
||||
with ui.card().classes("w-full"):
|
||||
ui.label("Ispezione genoma").classes("text-subtitle1")
|
||||
detail_hint = ui.label("Seleziona un genoma dalla tabella sopra.").classes(
|
||||
"text-caption"
|
||||
).style(f"color: {COLOR_TEXT_MUTED};")
|
||||
|
||||
with ui.row().classes("w-full gap-4 q-mt-sm"):
|
||||
with ui.card().classes("flex-grow metric-card accent-cyan"):
|
||||
ui.label("fitness").classes("text-caption")
|
||||
fit_lbl = ui.label("—").classes("text-h4")
|
||||
with ui.card().classes("flex-grow metric-card accent-purple"):
|
||||
ui.label("DSR").classes("text-caption")
|
||||
dsr_lbl = ui.label("—").classes("text-h4")
|
||||
with ui.card().classes("flex-grow metric-card accent-amber"):
|
||||
ui.label("Sharpe").classes("text-caption")
|
||||
sharpe_lbl = ui.label("—").classes("text-h4")
|
||||
with ui.card().classes("flex-grow metric-card"):
|
||||
ui.label("max DD").classes("text-caption")
|
||||
dd_lbl = ui.label("—").classes("text-h4")
|
||||
with ui.card().classes("flex-grow metric-card accent-green"):
|
||||
ui.label("trades").classes("text-caption")
|
||||
trades_lbl = ui.label("—").classes("text-h4")
|
||||
with ui.card().classes("flex-grow metric-card"):
|
||||
ui.label("style").classes("text-caption")
|
||||
style_lbl = ui.label("—").classes("text-h4")
|
||||
|
||||
ui.label("System prompt").classes("text-subtitle1 q-mt-md")
|
||||
prompt_code = ui.html('<pre class="raw-block">—</pre>').classes("w-full")
|
||||
|
||||
ui.label("Raw LLM output").classes("text-subtitle1 q-mt-md")
|
||||
raw_code = ui.html('<pre class="raw-block">—</pre>').classes("w-full")
|
||||
|
||||
parse_error_lbl = ui.label("").classes("q-mt-sm").style(
|
||||
"color: #FF6B6B; font-weight: 600;"
|
||||
)
|
||||
|
||||
def _render_detail(row: dict[str, Any]) -> None:
|
||||
detail_hint.text = f"Genoma: {row.get('genome_id', '—')}"
|
||||
fit_lbl.text = f"{float(row.get('fitness', 0)):.4f}"
|
||||
dsr_lbl.text = f"{float(row.get('dsr', 0)):.4f}"
|
||||
sharpe_lbl.text = f"{float(row.get('sharpe', 0)):.3f}"
|
||||
dd_lbl.text = f"{float(row.get('max_dd', 0)):.3f}"
|
||||
trades_lbl.text = str(int(row.get("n_trades", 0)))
|
||||
style_lbl.text = str(row.get("cognitive_style", "—"))
|
||||
prompt_code.content = (
|
||||
f'<pre class="raw-block">{html.escape(str(row.get("system_prompt", "—")))}</pre>'
|
||||
)
|
||||
raw_code.content = (
|
||||
f'<pre class="raw-block">{html.escape(str(row.get("raw_text", "—") or "—"))}</pre>'
|
||||
)
|
||||
pe = row.get("parse_error")
|
||||
parse_error_lbl.text = f"❌ Parse error: {pe}" if pe else ""
|
||||
|
||||
def refresh() -> None:
|
||||
run_id = select.value
|
||||
if not run_id:
|
||||
return
|
||||
try:
|
||||
repo = get_repo(GA_DB_PATH)
|
||||
evals = evaluations_df(repo, run_id)
|
||||
gens = genomes_df(repo, run_id)
|
||||
except Exception as e: # noqa: BLE001
|
||||
ui.notify(f"Errore: {e}", type="negative")
|
||||
return
|
||||
|
||||
if evals.empty:
|
||||
top_table.rows = []
|
||||
top_table.update()
|
||||
return
|
||||
|
||||
merged = evals.merge(
|
||||
gens, left_on="genome_id", right_on="id", how="left", suffixes=("", "_g")
|
||||
)
|
||||
state["merged"] = merged
|
||||
|
||||
k = int(top_k_select.value)
|
||||
top = merged.sort_values("fitness", ascending=False).head(k)
|
||||
|
||||
rows = []
|
||||
for _, r in top.iterrows():
|
||||
rows.append(
|
||||
{
|
||||
"genome_id": str(r.get("genome_id", "—"))[:12] + "…",
|
||||
"fitness": round(float(r.get("fitness", 0)), 4),
|
||||
"dsr": round(float(r.get("dsr", 0)), 4),
|
||||
"sharpe": round(float(r.get("sharpe", 0)), 3),
|
||||
"max_dd": round(float(r.get("max_dd", 0)), 3),
|
||||
"n_trades": int(r.get("n_trades", 0)),
|
||||
"cognitive_style": str(r.get("cognitive_style", "—")),
|
||||
"temperature": round(float(r.get("temperature", 0)), 2),
|
||||
"lookback_window": int(r.get("lookback_window", 0)),
|
||||
"_full_id": str(r.get("genome_id", "")),
|
||||
}
|
||||
)
|
||||
top_table.rows = rows
|
||||
top_table.update()
|
||||
|
||||
sel = state.get("selected_gid")
|
||||
if sel:
|
||||
match = merged[merged["genome_id"] == sel]
|
||||
if not match.empty:
|
||||
_render_detail(match.iloc[0].to_dict())
|
||||
|
||||
def on_row_selected(e: Any) -> None:
|
||||
rows = (e.args or {}).get("rows") or []
|
||||
if not rows:
|
||||
return
|
||||
full_id = rows[0].get("_full_id")
|
||||
if not full_id:
|
||||
return
|
||||
state["selected_gid"] = full_id
|
||||
merged = state.get("merged")
|
||||
if merged is None:
|
||||
return
|
||||
match = merged[merged["genome_id"] == full_id]
|
||||
if not match.empty:
|
||||
_render_detail(match.iloc[0].to_dict())
|
||||
|
||||
def on_select_change() -> None:
|
||||
state["run_id"] = select.value
|
||||
state["selected_gid"] = None
|
||||
refresh()
|
||||
|
||||
select.on_value_change(on_select_change)
|
||||
top_k_select.on_value_change(lambda _: refresh())
|
||||
top_table.on("selection", on_row_selected)
|
||||
_timer = ui.timer(REFRESH_INTERVAL_S, refresh)
|
||||
ui.context.client.on_disconnect(_timer.deactivate)
|
||||
refresh()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
app.on_startup(
|
||||
lambda: print(
|
||||
f"GA DB: {Path(GA_DB_PATH).resolve()} | root_path: {DASHBOARD_ROOT_PATH or '/'}"
|
||||
)
|
||||
)
|
||||
ui.run(
|
||||
host="0.0.0.0",
|
||||
port=int(os.environ.get("SWARM_DASHBOARD_PORT", "8080")),
|
||||
title="Multi-Swarm Core Dashboard",
|
||||
reload=False,
|
||||
show=False,
|
||||
dark=True,
|
||||
root_path=DASHBOARD_ROOT_PATH,
|
||||
)
|
||||
|
||||
|
||||
if __name__ in {"__main__", "__mp_main__"}:
|
||||
main()
|
||||
@@ -0,0 +1,383 @@
|
||||
"""Shared theme module for NiceGUI dashboards.
|
||||
|
||||
Exports palette constants, CSS, and helper functions used by both
|
||||
multi_swarm_core.dashboard and strategy_crypto.frontend dashboards.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
from typing import Any
|
||||
|
||||
from nicegui import ui
|
||||
|
||||
__all__ = [
|
||||
"COLOR_BG",
|
||||
"COLOR_SURFACE",
|
||||
"COLOR_SURFACE_2",
|
||||
"COLOR_BORDER",
|
||||
"COLOR_BORDER_HOVER",
|
||||
"COLOR_PRIMARY",
|
||||
"COLOR_SECONDARY",
|
||||
"COLOR_ACCENT",
|
||||
"COLOR_SUCCESS",
|
||||
"COLOR_DANGER",
|
||||
"COLOR_TEXT",
|
||||
"COLOR_TEXT_MUTED",
|
||||
"_STATUS_BADGE",
|
||||
"_CUSTOM_CSS",
|
||||
"_json_to_html",
|
||||
"_apply_theme",
|
||||
"_build_header",
|
||||
]
|
||||
|
||||
# --- Neon Trading Dashboard palette ---
|
||||
COLOR_BG = "#0A0A0F"
|
||||
COLOR_SURFACE = "#13131A"
|
||||
COLOR_SURFACE_2 = "#1C1C26"
|
||||
COLOR_BORDER = "rgba(255, 45, 135, 0.12)"
|
||||
COLOR_BORDER_HOVER = "rgba(255, 45, 135, 0.45)"
|
||||
COLOR_PRIMARY = "#FF2D87"
|
||||
COLOR_SECONDARY = "#00D9FF"
|
||||
COLOR_ACCENT = "#FFB800"
|
||||
COLOR_SUCCESS = "#00E676"
|
||||
COLOR_DANGER = "#FF3D60"
|
||||
COLOR_TEXT = "#FFFFFF"
|
||||
COLOR_TEXT_MUTED = "#7A7A8C"
|
||||
|
||||
_STATUS_BADGE: dict[str, tuple[str, str]] = {
|
||||
"running": ("● running", "positive"),
|
||||
"completed": ("✓ completed", "positive"),
|
||||
"failed": ("✕ failed", "negative"),
|
||||
}
|
||||
|
||||
_CUSTOM_CSS = f"""
|
||||
<style>
|
||||
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap');
|
||||
|
||||
html, body, .q-page, .q-card, .q-btn, .q-field, .q-table, .text-h4, .text-h6, .text-subtitle1, .text-caption, .text-body1, .nav-link, .brand, label, p, span, div {{
|
||||
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
||||
letter-spacing: -0.01em;
|
||||
}}
|
||||
.material-icons, .material-icons-outlined, .material-symbols-outlined, .q-icon, i.q-icon, i[class*="material"] {{
|
||||
font-family: 'Material Icons' !important;
|
||||
font-feature-settings: 'liga';
|
||||
letter-spacing: normal !important;
|
||||
}}
|
||||
code, pre, .q-code, .nicegui-code {{ font-family: 'JetBrains Mono', 'Fira Code', monospace !important; font-size: 13.5px !important; }}
|
||||
|
||||
body, .q-page-container, .q-page {{
|
||||
background: {COLOR_BG} !important;
|
||||
color: {COLOR_TEXT};
|
||||
background-image:
|
||||
radial-gradient(ellipse 800px 400px at 20% 0%, rgba(255, 45, 135, 0.08) 0%, transparent 60%),
|
||||
radial-gradient(ellipse 600px 400px at 80% 100%, rgba(0, 217, 255, 0.06) 0%, transparent 60%);
|
||||
background-attachment: fixed;
|
||||
}}
|
||||
|
||||
.q-card {{
|
||||
background: {COLOR_SURFACE} !important;
|
||||
color: {COLOR_TEXT} !important;
|
||||
border: 1px solid {COLOR_BORDER};
|
||||
border-radius: 14px !important;
|
||||
box-shadow:
|
||||
0 1px 2px rgba(0,0,0,0.5),
|
||||
0 8px 24px rgba(0,0,0,0.25),
|
||||
inset 0 1px 0 rgba(255,255,255,0.04);
|
||||
transition: all 0.2s ease;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}}
|
||||
.q-card::before {{
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0; left: 0; right: 0;
|
||||
height: 1px;
|
||||
background: linear-gradient(90deg, transparent, rgba(255, 45, 135, 0.4), transparent);
|
||||
opacity: 0.5;
|
||||
}}
|
||||
.q-card:hover {{
|
||||
border-color: rgba(255, 45, 135, 0.5);
|
||||
box-shadow:
|
||||
0 1px 2px rgba(0,0,0,0.5),
|
||||
0 8px 32px rgba(255, 45, 135, 0.15),
|
||||
inset 0 1px 0 rgba(255,255,255,0.05);
|
||||
}}
|
||||
|
||||
.metric-card {{
|
||||
padding: 20px 16px;
|
||||
text-align: left;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
min-width: 140px;
|
||||
}}
|
||||
.metric-card .text-caption {{
|
||||
color: {COLOR_TEXT_MUTED} !important;
|
||||
font-size: 11px !important;
|
||||
font-weight: 500 !important;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.06em;
|
||||
}}
|
||||
.metric-card .text-h4 {{
|
||||
color: {COLOR_TEXT} !important;
|
||||
font-weight: 600 !important;
|
||||
font-size: 26px !important;
|
||||
line-height: 1.2 !important;
|
||||
font-feature-settings: 'tnum';
|
||||
}}
|
||||
.metric-card.accent-cyan .text-h4 {{ color: {COLOR_PRIMARY} !important; }}
|
||||
.metric-card.accent-purple .text-h4 {{ color: {COLOR_SECONDARY} !important; }}
|
||||
.metric-card.accent-amber .text-h4 {{ color: {COLOR_ACCENT} !important; }}
|
||||
.metric-card.accent-green .text-h4 {{ color: {COLOR_SUCCESS} !important; }}
|
||||
|
||||
.q-header {{
|
||||
background: rgba(10, 10, 15, 0.75) !important;
|
||||
backdrop-filter: blur(20px) saturate(180%);
|
||||
-webkit-backdrop-filter: blur(20px) saturate(180%);
|
||||
border-bottom: 1px solid {COLOR_BORDER} !important;
|
||||
box-shadow: 0 1px 0 rgba(255, 45, 135, 0.15) !important;
|
||||
}}
|
||||
|
||||
.nav-link {{
|
||||
color: {COLOR_TEXT_MUTED} !important;
|
||||
padding: 8px 14px;
|
||||
border-radius: 8px;
|
||||
text-decoration: none;
|
||||
font-size: 13.5px;
|
||||
font-weight: 500;
|
||||
transition: all 0.15s ease;
|
||||
position: relative;
|
||||
}}
|
||||
.nav-link:hover {{
|
||||
color: {COLOR_TEXT} !important;
|
||||
background: {COLOR_SURFACE_2};
|
||||
}}
|
||||
.nav-link.active {{
|
||||
color: {COLOR_PRIMARY} !important;
|
||||
background: rgba(255, 45, 135, 0.08);
|
||||
}}
|
||||
.nav-link.active::after {{
|
||||
content: '';
|
||||
position: absolute;
|
||||
bottom: -16px;
|
||||
left: 14px;
|
||||
right: 14px;
|
||||
height: 2px;
|
||||
background: {COLOR_PRIMARY};
|
||||
border-radius: 2px 2px 0 0;
|
||||
}}
|
||||
|
||||
.brand {{
|
||||
color: {COLOR_TEXT};
|
||||
font-weight: 700;
|
||||
font-size: 15px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}}
|
||||
.brand-dot {{
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
border-radius: 50%;
|
||||
background: {COLOR_PRIMARY};
|
||||
box-shadow: 0 0 16px {COLOR_PRIMARY}, 0 0 4px {COLOR_PRIMARY};
|
||||
animation: pulse-pink 2s ease-in-out infinite;
|
||||
}}
|
||||
@keyframes pulse-pink {{
|
||||
0%, 100% {{ box-shadow: 0 0 16px {COLOR_PRIMARY}, 0 0 4px {COLOR_PRIMARY}; }}
|
||||
50% {{ box-shadow: 0 0 24px {COLOR_PRIMARY}, 0 0 8px {COLOR_PRIMARY}; }}
|
||||
}}
|
||||
|
||||
.q-linear-progress {{ height: 8px !important; border-radius: 6px !important; }}
|
||||
.q-linear-progress__track {{ background: {COLOR_SURFACE_2} !important; }}
|
||||
.q-linear-progress__model {{ border-radius: 6px !important; }}
|
||||
|
||||
.q-separator {{ background: {COLOR_BORDER} !important; }}
|
||||
|
||||
.q-field--outlined .q-field__control {{
|
||||
background: {COLOR_SURFACE} !important;
|
||||
border-radius: 8px !important;
|
||||
}}
|
||||
.q-field--outlined .q-field__control:before {{ border-color: {COLOR_BORDER} !important; }}
|
||||
.q-field--outlined.q-field--focused .q-field__control:after {{ border-color: {COLOR_PRIMARY} !important; }}
|
||||
.q-field__label {{ color: {COLOR_TEXT_MUTED} !important; }}
|
||||
.q-field__native, .q-field__input {{ color: {COLOR_TEXT} !important; }}
|
||||
|
||||
.q-btn {{ border-radius: 8px !important; font-weight: 500 !important; text-transform: none !important; letter-spacing: 0 !important; }}
|
||||
|
||||
.q-table {{ background: transparent !important; color: {COLOR_TEXT} !important; }}
|
||||
.q-table thead tr {{ background: {COLOR_SURFACE_2} !important; }}
|
||||
.q-table th {{
|
||||
color: {COLOR_TEXT_MUTED} !important;
|
||||
font-size: 11px !important;
|
||||
font-weight: 600 !important;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.06em;
|
||||
}}
|
||||
.q-table tbody tr {{ transition: background 0.15s; }}
|
||||
.q-table tbody tr:hover {{ background: rgba(255, 45, 135, 0.05) !important; }}
|
||||
.q-table tbody tr.selected {{ background: rgba(255, 45, 135, 0.12) !important; }}
|
||||
.q-table td {{ border-bottom: 1px solid {COLOR_BORDER} !important; font-feature-settings: 'tnum'; }}
|
||||
|
||||
.text-h6 {{ font-weight: 600 !important; letter-spacing: -0.015em !important; }}
|
||||
.text-subtitle1 {{
|
||||
color: {COLOR_TEXT_MUTED} !important;
|
||||
font-size: 11px !important;
|
||||
font-weight: 600 !important;
|
||||
text-transform: uppercase !important;
|
||||
letter-spacing: 0.08em !important;
|
||||
margin-bottom: 8px !important;
|
||||
}}
|
||||
|
||||
code, pre, .nicegui-code {{
|
||||
background: #1A1A24 !important;
|
||||
color: {COLOR_TEXT} !important;
|
||||
border: 1px solid {COLOR_BORDER};
|
||||
border-radius: 10px !important;
|
||||
padding: 16px !important;
|
||||
font-size: 13.5px !important;
|
||||
line-height: 1.6 !important;
|
||||
}}
|
||||
.hljs {{ background: transparent !important; color: {COLOR_TEXT} !important; }}
|
||||
.hljs-attr, .hljs-attribute {{ color: {COLOR_SECONDARY} !important; font-weight: 500; }}
|
||||
.hljs-string {{ color: {COLOR_SUCCESS} !important; }}
|
||||
.hljs-number, .hljs-literal {{ color: {COLOR_PRIMARY} !important; font-weight: 500; }}
|
||||
.hljs-keyword, .hljs-built_in {{ color: {COLOR_ACCENT} !important; }}
|
||||
.hljs-punctuation, .hljs-meta {{ color: {COLOR_TEXT_MUTED} !important; }}
|
||||
.hljs-comment {{ color: {COLOR_TEXT_MUTED} !important; font-style: italic; }}
|
||||
.hljs-name, .hljs-title {{ color: {COLOR_PRIMARY} !important; }}
|
||||
|
||||
/* Prism.js tokens (NiceGUI usa Prism per ui.code) */
|
||||
.token.property, .token.attr-name, .token.tag {{ color: {COLOR_SECONDARY} !important; font-weight: 500; }}
|
||||
.token.string, .token.url {{ color: {COLOR_SUCCESS} !important; }}
|
||||
.token.number, .token.boolean, .token.null, .token.symbol {{ color: {COLOR_PRIMARY} !important; font-weight: 500; }}
|
||||
.token.keyword, .token.constant, .token.builtin, .token.atrule {{ color: {COLOR_ACCENT} !important; }}
|
||||
.token.punctuation, .token.operator {{ color: {COLOR_TEXT_MUTED} !important; }}
|
||||
.token.comment {{ color: {COLOR_TEXT_MUTED} !important; font-style: italic; }}
|
||||
.token.function, .token.class-name {{ color: {COLOR_PRIMARY} !important; }}
|
||||
pre[class*="language-"], code[class*="language-"] {{
|
||||
color: {COLOR_TEXT} !important;
|
||||
text-shadow: none !important;
|
||||
}}
|
||||
|
||||
.q-badge {{ border-radius: 6px !important; font-weight: 500 !important; padding: 4px 10px !important; font-size: 12px !important; }}
|
||||
|
||||
.config-block {{
|
||||
background: #1A1A24;
|
||||
color: {COLOR_TEXT};
|
||||
border: 1px solid {COLOR_BORDER};
|
||||
border-radius: 10px;
|
||||
padding: 18px 20px;
|
||||
font-family: 'JetBrains Mono', 'Fira Code', monospace;
|
||||
font-size: 13.5px;
|
||||
line-height: 1.7;
|
||||
overflow-x: auto;
|
||||
white-space: pre;
|
||||
margin: 0;
|
||||
}}
|
||||
.config-block .cb-key {{ color: {COLOR_SECONDARY}; font-weight: 500; }}
|
||||
.config-block .cb-string {{ color: {COLOR_SUCCESS}; }}
|
||||
.config-block .cb-number {{ color: {COLOR_PRIMARY}; font-weight: 500; }}
|
||||
.config-block .cb-bool {{ color: {COLOR_ACCENT}; }}
|
||||
.config-block .cb-null {{ color: {COLOR_ACCENT}; font-style: italic; }}
|
||||
.config-block .cb-punct {{ color: {COLOR_TEXT_MUTED}; }}
|
||||
|
||||
.raw-block {{
|
||||
background: #1A1A24;
|
||||
color: {COLOR_TEXT};
|
||||
border: 1px solid {COLOR_BORDER};
|
||||
border-radius: 10px;
|
||||
padding: 18px 20px;
|
||||
font-family: 'JetBrains Mono', 'Fira Code', monospace;
|
||||
font-size: 13px;
|
||||
line-height: 1.6;
|
||||
overflow-x: auto;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
margin: 0;
|
||||
}}
|
||||
</style>
|
||||
"""
|
||||
|
||||
|
||||
def _json_to_html(obj: Any, indent: int = 0) -> str:
|
||||
"""Render JSON con span colorati espliciti. Garantisce leggibilità ovunque."""
|
||||
pad = " " * indent
|
||||
inner_pad = " " * (indent + 1)
|
||||
if isinstance(obj, dict):
|
||||
if not obj:
|
||||
return '<span class="cb-punct">{}</span>'
|
||||
items = []
|
||||
for k, v in obj.items():
|
||||
key = f'<span class="cb-key">"{html.escape(str(k))}"</span>'
|
||||
val = _json_to_html(v, indent + 1)
|
||||
items.append(f"{inner_pad}{key}<span class=\"cb-punct\">:</span> {val}")
|
||||
return ('<span class="cb-punct">{</span>\n'
|
||||
+ '<span class="cb-punct">,</span>\n'.join(items)
|
||||
+ f'\n{pad}<span class="cb-punct">}}</span>')
|
||||
if isinstance(obj, list):
|
||||
if not obj:
|
||||
return '<span class="cb-punct">[]</span>'
|
||||
items = [_json_to_html(x, indent + 1) for x in obj]
|
||||
return ('<span class="cb-punct">[</span>\n'
|
||||
+ '<span class="cb-punct">,</span>\n'.join(inner_pad + i for i in items)
|
||||
+ f'\n{pad}<span class="cb-punct">]</span>')
|
||||
if isinstance(obj, bool):
|
||||
return f'<span class="cb-bool">{str(obj).lower()}</span>'
|
||||
if obj is None:
|
||||
return '<span class="cb-null">null</span>'
|
||||
if isinstance(obj, (int, float)):
|
||||
return f'<span class="cb-number">{obj}</span>'
|
||||
return f'<span class="cb-string">"{html.escape(str(obj))}"</span>'
|
||||
|
||||
|
||||
def _apply_theme() -> None:
|
||||
ui.add_head_html(_CUSTOM_CSS)
|
||||
ui.dark_mode().enable()
|
||||
ui.colors(
|
||||
primary=COLOR_PRIMARY,
|
||||
secondary=COLOR_SECONDARY,
|
||||
accent=COLOR_ACCENT,
|
||||
dark=COLOR_BG,
|
||||
dark_page=COLOR_BG,
|
||||
positive=COLOR_SUCCESS,
|
||||
negative=COLOR_DANGER,
|
||||
info=COLOR_PRIMARY,
|
||||
warning=COLOR_ACCENT,
|
||||
)
|
||||
|
||||
|
||||
def _build_header(
|
||||
active: str,
|
||||
brand_subtitle: str,
|
||||
nav_items: list[tuple[str, str]],
|
||||
db_label: str,
|
||||
) -> None:
|
||||
"""Render the top navigation header.
|
||||
|
||||
Args:
|
||||
active: URL path of the currently active page (e.g. "/").
|
||||
brand_subtitle: Text shown after the brand dot, e.g. "Coevolutivo / GA".
|
||||
nav_items: List of (path, label) tuples for nav links.
|
||||
db_label: Short DB identifier shown in the top-right corner.
|
||||
"""
|
||||
with ui.header().classes("items-center justify-between q-px-lg q-py-md"):
|
||||
with ui.row().classes("items-center gap-8"):
|
||||
with ui.row().classes("items-center gap-2").classes("brand"):
|
||||
ui.html('<span class="brand-dot"></span>')
|
||||
ui.html(
|
||||
f'<span class="brand">Multi-Swarm <span style="color:{COLOR_TEXT_MUTED}'
|
||||
f';font-weight:400;">/ {brand_subtitle}</span></span>'
|
||||
)
|
||||
with ui.row().classes("items-center gap-1"):
|
||||
for path, label in nav_items:
|
||||
cls = "nav-link active" if active == path else "nav-link"
|
||||
ui.link(label, path).classes(cls)
|
||||
with ui.row().classes("items-center gap-3"):
|
||||
ui.html(
|
||||
f'<span style="color:{COLOR_TEXT_MUTED};font-size:12px;'
|
||||
f'font-family:JetBrains Mono,monospace;">{db_label}</span>'
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Data loaders: OHLCV via Cerbero, train/test splits."""
|
||||
|
||||
from .cerbero_ohlcv import CerberoOHLCVLoader, OHLCVRequest
|
||||
|
||||
__all__ = ["CerberoOHLCVLoader", "OHLCVRequest"]
|
||||
@@ -0,0 +1,108 @@
|
||||
"""Fitness function della Phase 1/2.
|
||||
|
||||
Combina :class:`FalsificationReport` (metriche di robustezza) e
|
||||
:class:`AdversarialReport` (findings euristici) in uno scalare ``>= 0`` che il
|
||||
GA usa per selezione e ranking.
|
||||
|
||||
**v1** (default, backward compat): ogni finding ``HIGH`` azzera la fitness.
|
||||
Kill-switch hard a 360 gradi.
|
||||
|
||||
**v2** (opt-in via ``hard_kill_findings``): solo findings nel set ``hard_kill``
|
||||
azzerano; gli altri HIGH applicano una penalità moltiplicativa
|
||||
``1 / (1 + soft_penalty * n_soft_high)``. Restituisce gradient continuo anche
|
||||
su strategie marginalmente killate da gate adversarial, permettendo
|
||||
all'evoluzione di esplorare zone con 1-2 finding HIGH "soft" (es.
|
||||
``fees_eat_alpha``, ``flat_too_long``, ``time_in_market_too_high``).
|
||||
|
||||
Formula::
|
||||
|
||||
sharpe_norm = 0.5 * (tanh(sharpe) + 1.0) # in [0, 1]
|
||||
base = dsr_weight * dsr + sharpe_weight * sharpe_norm
|
||||
dd_penalty = 1.0 / (1.0 + drawdown_penalty * max_drawdown)
|
||||
adv_penalty = 1.0 (v1) o 1/(1+soft*n_soft_high) (v2)
|
||||
fitness = max(0.0, base * dd_penalty * adv_penalty)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from collections.abc import Iterable
|
||||
|
||||
from ..agents.adversarial import AdversarialReport, Severity
|
||||
from ..agents.falsification import FalsificationReport
|
||||
|
||||
|
||||
def compute_combined_fitness(
|
||||
fitness_train: float,
|
||||
fitness_oos: float | None,
|
||||
alpha: float = 0.5,
|
||||
) -> float:
|
||||
"""Combina fitness IS e OOS in uno scalare per selection multi-objective.
|
||||
|
||||
Formula::
|
||||
|
||||
combined = alpha * fitness_train + (1 - alpha) * fitness_oos
|
||||
|
||||
Se ``fitness_oos`` è ``None`` o NaN, ritorna ``fitness_train`` (fallback).
|
||||
alpha=1.0 → solo IS (= comportamento default). alpha=0.0 → solo OOS.
|
||||
alpha=0.5 → bilanciato.
|
||||
"""
|
||||
if fitness_oos is None or fitness_oos != fitness_oos: # noqa: PLR0124 (NaN check)
|
||||
return fitness_train
|
||||
return alpha * fitness_train + (1.0 - alpha) * fitness_oos
|
||||
|
||||
|
||||
def compute_fitness(
|
||||
falsification: FalsificationReport,
|
||||
adversarial: AdversarialReport,
|
||||
drawdown_penalty: float = 1.0,
|
||||
dsr_weight: float = 0.5,
|
||||
sharpe_weight: float = 0.5,
|
||||
hard_kill_findings: Iterable[str] | None = None,
|
||||
adversarial_soft_penalty: float = 0.4,
|
||||
) -> float:
|
||||
"""Calcola la fitness scalare di una strategia.
|
||||
|
||||
Args:
|
||||
falsification: report con DSR, Sharpe, max_drawdown, n_trades.
|
||||
adversarial: report con eventuali findings euristici.
|
||||
drawdown_penalty: peso del max drawdown nel denominatore della
|
||||
penalita' moltiplicativa (default 1.0).
|
||||
dsr_weight: peso del DSR nella base (default 0.5).
|
||||
sharpe_weight: peso dello Sharpe normalizzato nella base
|
||||
(default 0.5).
|
||||
hard_kill_findings: nomi di findings che azzerano la fitness se
|
||||
``HIGH``. ``None`` (default v1) = TUTTI gli HIGH azzerano.
|
||||
Per v2 passare es. ``{"no_trades", "degenerate"}``: solo
|
||||
questi azzerano, gli altri HIGH applicano soft penalty.
|
||||
adversarial_soft_penalty: in v2, fattore della penalità
|
||||
moltiplicativa per ogni HIGH soft (default 0.4 →
|
||||
``1/(1+0.4*n)``: 1 → 0.71, 2 → 0.56, 3 → 0.45).
|
||||
|
||||
Returns:
|
||||
Fitness ``>= 0``. Zero indica strategia da scartare (no-trade o
|
||||
kill adversarial).
|
||||
"""
|
||||
if falsification.n_trades == 0:
|
||||
return 0.0
|
||||
|
||||
high_findings = [f for f in adversarial.findings if f.severity == Severity.HIGH]
|
||||
|
||||
if hard_kill_findings is None:
|
||||
# v1: tutti gli HIGH azzerano la fitness.
|
||||
if high_findings:
|
||||
return 0.0
|
||||
adv_penalty = 1.0
|
||||
else:
|
||||
# v2: solo finding con name in hard_kill_findings azzerano.
|
||||
hard_set = frozenset(hard_kill_findings)
|
||||
if any(f.name in hard_set for f in high_findings):
|
||||
return 0.0
|
||||
n_soft_high = sum(1 for f in high_findings if f.name not in hard_set)
|
||||
adv_penalty = 1.0 / (1.0 + adversarial_soft_penalty * n_soft_high)
|
||||
|
||||
dsr = max(0.0, min(1.0, float(falsification.dsr)))
|
||||
sharpe_norm = 0.5 * (math.tanh(float(falsification.sharpe)) + 1.0)
|
||||
base = dsr_weight * dsr + sharpe_weight * sharpe_norm
|
||||
dd_penalty = 1.0 / (1.0 + drawdown_penalty * float(falsification.max_drawdown))
|
||||
return max(0.0, float(base * dd_penalty * adv_penalty))
|
||||
+15
-30
@@ -3,34 +3,12 @@ from __future__ import annotations
|
||||
import random
|
||||
|
||||
from ..genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
from ..genome.mutation import COGNITIVE_STYLES
|
||||
from ..genome.prompt_library import PromptLibrary
|
||||
|
||||
STYLE_PROMPTS: dict[str, str] = {
|
||||
"physicist": (
|
||||
"Cerca leggi conservative, simmetrie, regimi di scala. "
|
||||
"Pensa in termini di flussi e potenziali."
|
||||
),
|
||||
"biologist": (
|
||||
"Cerca pattern adattivi, nicchie ecologiche, "
|
||||
"predator-prey dynamics tra partecipanti del mercato."
|
||||
),
|
||||
"historian": (
|
||||
"Cerca pattern ricorrenti su scale temporali multiple, "
|
||||
"analogie con regimi storici, mean reversion strutturali."
|
||||
),
|
||||
"meteorologist": (
|
||||
"Cerca regimi di volatilità che si autoalimentano, "
|
||||
"transizioni di stato come fronti, persistenza locale."
|
||||
),
|
||||
"ecologist": (
|
||||
"Cerca interazioni multi-asset, correlazioni cluster, "
|
||||
"segnali di stress sistemico nelle dinamiche di flusso."
|
||||
),
|
||||
"engineer": (
|
||||
"Cerca segnali con rapporto S/N favorevole, filtri causali, "
|
||||
"robustezza a perturbazioni di calibrazione."
|
||||
),
|
||||
}
|
||||
# Mantenuto come alias backcompat: equivalente a PromptLibrary.default().styles.
|
||||
# Nuovi caller dovrebbero usare PromptLibrary direttamente per supportare
|
||||
# l'override via prompts.json di una strategia.
|
||||
STYLE_PROMPTS: dict[str, str] = PromptLibrary.default().styles
|
||||
|
||||
|
||||
def build_initial_population(
|
||||
@@ -38,15 +16,22 @@ def build_initial_population(
|
||||
model_tier: ModelTier,
|
||||
rng: random.Random,
|
||||
feature_pool: tuple[str, ...] = ("close", "high", "low", "volume"),
|
||||
prompt_library: PromptLibrary | None = None,
|
||||
) -> list[HypothesisAgentGenome]:
|
||||
"""Costruisce una popolazione iniziale K varia per stile cognitivo + parametri."""
|
||||
"""Costruisce una popolazione iniziale K varia per stile cognitivo + parametri.
|
||||
|
||||
``prompt_library`` controlla quali stili sono disponibili e quale system_prompt
|
||||
iniziale viene assegnato. Default = builtin 6 stili (physicist, biologist, ...).
|
||||
Override tipico: ``PromptLibrary.from_json(strategy_crypto/prompts.json)``.
|
||||
"""
|
||||
lib = prompt_library or PromptLibrary.default()
|
||||
population: list[HypothesisAgentGenome] = []
|
||||
for i in range(k):
|
||||
style = COGNITIVE_STYLES[i % len(COGNITIVE_STYLES)]
|
||||
style = lib.style_at(i)
|
||||
n_features = rng.randint(1, len(feature_pool))
|
||||
feats = sorted(rng.sample(feature_pool, k=n_features))
|
||||
g = HypothesisAgentGenome(
|
||||
system_prompt=STYLE_PROMPTS[style],
|
||||
system_prompt=lib.directive(style),
|
||||
feature_access=feats,
|
||||
temperature=round(rng.uniform(0.7, 1.2), 2),
|
||||
top_p=0.95,
|
||||
@@ -2,10 +2,11 @@ from __future__ import annotations
|
||||
|
||||
import random
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from ..genome.crossover import uniform_crossover
|
||||
from ..genome.hypothesis import HypothesisAgentGenome
|
||||
from ..genome.mutation import random_mutate
|
||||
from ..genome.mutation import weighted_random_mutate
|
||||
from .selection import elite_select, tournament_select
|
||||
|
||||
|
||||
@@ -15,6 +16,7 @@ class GAConfig:
|
||||
elite_k: int
|
||||
tournament_k: int
|
||||
p_crossover: float
|
||||
prompt_mutation_weight: float = 0.0 # Phase 2.5: opt-in LLM mutator
|
||||
|
||||
|
||||
def next_generation(
|
||||
@@ -22,8 +24,18 @@ def next_generation(
|
||||
fitnesses: dict[str, float],
|
||||
cfg: GAConfig,
|
||||
rng: random.Random,
|
||||
llm: Any | None = None,
|
||||
cost_tracker: Any | None = None,
|
||||
repo: Any | None = None,
|
||||
run_id: str | None = None,
|
||||
) -> list[HypothesisAgentGenome]:
|
||||
"""Costruisce la prossima generazione: elitismo + tournament + crossover/mutate."""
|
||||
"""Costruisce la prossima generazione: elitismo + tournament + crossover/mutate.
|
||||
|
||||
Quando ``cfg.prompt_mutation_weight > 0`` e ``llm`` è fornito, la mutazione
|
||||
invoca ``weighted_random_mutate`` che con quella probabilità usa
|
||||
``mutate_prompt_llm`` (Phase 2.5). Cost tracker/repo/run_id si propagano
|
||||
per registrare ``call_kind="mutation"`` sulle call mutator.
|
||||
"""
|
||||
new_pop: list[HypothesisAgentGenome] = list(
|
||||
elite_select(population, fitnesses, cfg.elite_k)
|
||||
)
|
||||
@@ -35,7 +47,14 @@ def next_generation(
|
||||
child = uniform_crossover(p1, p2, rng)
|
||||
else:
|
||||
parent = tournament_select(population, fitnesses, cfg.tournament_k, rng)
|
||||
child = random_mutate(parent, rng)
|
||||
child = weighted_random_mutate(
|
||||
parent, rng,
|
||||
llm=llm,
|
||||
prompt_mutation_weight=cfg.prompt_mutation_weight,
|
||||
cost_tracker=cost_tracker,
|
||||
repo=repo,
|
||||
run_id=run_id,
|
||||
)
|
||||
new_pop.append(child)
|
||||
|
||||
return new_pop[: cfg.population_size]
|
||||
+5
-5
@@ -8,11 +8,11 @@ from typing import Any
|
||||
|
||||
|
||||
class ModelTier(StrEnum):
|
||||
S = "S" # top-tier reasoning (Opus / equivalent) via Anthropic
|
||||
A = "A" # premium override via Anthropic
|
||||
B = "B" # Sonnet 4.6 via Anthropic
|
||||
C = "C" # Qwen 2.5 72B via OpenRouter
|
||||
D = "D" # ultra-economic (Llama / cheap models) via OpenRouter
|
||||
S = "S" # top-tier reasoning → opus via OpusAgent
|
||||
A = "A" # premium → opus via OpusAgent
|
||||
B = "B" # standard → sonnet via OpusAgent
|
||||
C = "C" # default GA → sonnet via OpusAgent
|
||||
D = "D" # economic → haiku via OpusAgent
|
||||
|
||||
|
||||
@dataclass
|
||||
+44
@@ -7,6 +7,10 @@ from .hypothesis import HypothesisAgentGenome
|
||||
|
||||
FEATURE_POOL: tuple[str, ...] = ("open", "high", "low", "close", "volume")
|
||||
|
||||
# Lista di default builtin (allineata con PromptLibrary.default()).
|
||||
# Il dispatcher run_phase1 sovrascrive `COGNITIVE_STYLES` con la lista letta
|
||||
# da prompts.json prima del loop GA, cosi' `mutate_cognitive_style` pesca
|
||||
# dai candidati corretti per la strategia in corso.
|
||||
COGNITIVE_STYLES: tuple[str, ...] = (
|
||||
"physicist",
|
||||
"biologist",
|
||||
@@ -17,6 +21,18 @@ COGNITIVE_STYLES: tuple[str, ...] = (
|
||||
)
|
||||
|
||||
|
||||
def set_cognitive_styles(styles: tuple[str, ...]) -> None:
|
||||
"""Sovrascrive la lista globale di stili candidati per la mutation.
|
||||
|
||||
Da chiamare PRIMA del GA loop (es. in run_phase1 dopo aver caricato la
|
||||
PromptLibrary). Non thread-safe: pensata per uno script CLI.
|
||||
"""
|
||||
global COGNITIVE_STYLES
|
||||
if not styles:
|
||||
raise ValueError("set_cognitive_styles: lista vuota")
|
||||
COGNITIVE_STYLES = tuple(styles)
|
||||
|
||||
|
||||
def _clone_with(g: HypothesisAgentGenome, **overrides: Any) -> HypothesisAgentGenome:
|
||||
payload: dict[str, Any] = g.to_dict()
|
||||
payload.update(overrides)
|
||||
@@ -75,3 +91,31 @@ MUTATION_OPS = (
|
||||
def random_mutate(g: HypothesisAgentGenome, rng: random.Random) -> HypothesisAgentGenome:
|
||||
op = rng.choice(MUTATION_OPS)
|
||||
return op(g, rng)
|
||||
|
||||
|
||||
def weighted_random_mutate(
|
||||
g: HypothesisAgentGenome,
|
||||
rng: random.Random,
|
||||
llm: Any | None = None,
|
||||
prompt_mutation_weight: float = 0.0,
|
||||
cost_tracker: Any | None = None,
|
||||
repo: Any | None = None,
|
||||
run_id: str | None = None,
|
||||
) -> HypothesisAgentGenome:
|
||||
"""Dispatcher pesato fra mutate_prompt_llm e random_mutate scalare.
|
||||
|
||||
Con probabilità ``prompt_mutation_weight`` invoca ``mutate_prompt_llm``,
|
||||
altrimenti ``random_mutate``. Se ``llm`` è ``None`` o il peso è 0,
|
||||
è equivalente a ``random_mutate`` (backward-compat).
|
||||
|
||||
Se ``cost_tracker``, ``repo`` e ``run_id`` sono forniti, vengono propagati a
|
||||
``mutate_prompt_llm`` per tracciare la call con ``call_kind="mutation"``.
|
||||
"""
|
||||
if llm is not None and prompt_mutation_weight > 0 and rng.random() < prompt_mutation_weight:
|
||||
# Import inline per evitare ciclo: mutation_prompt_llm importa da mutation.
|
||||
from .mutation_prompt_llm import mutate_prompt_llm
|
||||
|
||||
return mutate_prompt_llm(
|
||||
g, llm, rng, cost_tracker=cost_tracker, repo=repo, run_id=run_id
|
||||
)
|
||||
return random_mutate(g, rng)
|
||||
@@ -0,0 +1,196 @@
|
||||
"""Phase 2.5 operator: ``mutate_prompt_llm``.
|
||||
|
||||
Quinto operatore di mutazione che riscrive il ``system_prompt`` di un genoma
|
||||
usando un LLM tier B come "mutator". Genera diversità prompt-level dove gli
|
||||
altri quattro operatori toccano solo i quattro parametri scalari.
|
||||
|
||||
Fallback sicuro: se la mutazione LLM produce output invalido o troppo simile
|
||||
al parent, l'operatore degrada silenziosamente a ``random_mutate``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
import re
|
||||
from dataclasses import replace
|
||||
from difflib import SequenceMatcher
|
||||
from typing import Any, Protocol
|
||||
|
||||
from .hypothesis import HypothesisAgentGenome, ModelTier
|
||||
from .mutation import _clone_with, random_mutate
|
||||
|
||||
# Sei tipi di mutazione "atomiche", scelti uniformemente.
|
||||
MUTATION_INSTRUCTIONS: dict[str, str] = {
|
||||
"tighten_threshold": (
|
||||
"Rendi UNA soglia numerica nella strategia più restrittiva del 10-20%. "
|
||||
"Esempio: se RSI > 70 diventa RSI > 78. Lascia tutto il resto identico."
|
||||
),
|
||||
"swap_comparator": (
|
||||
"Inverti UN comparator (gt -> lt, gte -> lte o viceversa) in una sola "
|
||||
"condizione. Mantieni lo stesso intent generale della strategia."
|
||||
),
|
||||
"add_condition": (
|
||||
"Aggiungi UNA condizione AND a una rule esistente per renderla più "
|
||||
"selettiva. La condizione deve usare una feature/indicator coerente con "
|
||||
"il resto della strategia."
|
||||
),
|
||||
"remove_condition": (
|
||||
"Rimuovi UNA condizione ridondante o ovvia da una rule, semplificando la "
|
||||
"logica senza alterarne l'intent principale."
|
||||
),
|
||||
"change_timeframe": (
|
||||
"Modifica UNA finestra rolling/lookback di +/- 20-40% (es. SMA(50) -> "
|
||||
"SMA(70)). Solo un parametro temporale."
|
||||
),
|
||||
"add_temporal_gate": (
|
||||
"Aggiungi UN gate temporale alla strategia usando una delle feature "
|
||||
"'hour', 'dow', 'is_weekend', 'minute_of_hour' per filtrare il "
|
||||
"trading a specifici momenti."
|
||||
),
|
||||
}
|
||||
|
||||
# Keyword tecniche minime per validare che il prompt sia ancora "una strategia".
|
||||
_VALID_KEYWORDS = (
|
||||
"rsi", "sma", "sma_pct", "ema", "atr", "atr_pct", "realized_vol",
|
||||
"momentum", "breakout", "mean", "reversion",
|
||||
"macd", "macd_pct", "vwap", "bb", "bollinger", "stoch", "trend", "signal", "buy",
|
||||
"sell", "long", "short", "entry", "exit", "stop", "rule", "condition",
|
||||
"if", "when", "and", "or", "gt", "lt", ">", "<", "ge", "le",
|
||||
"hour", "dow", "weekend", "indicator", "feature",
|
||||
)
|
||||
|
||||
_MIN_PROMPT_LENGTH = 50
|
||||
_MIN_DIFF_RATIO = 0.05 # Levenshtein-like: prompt deve essere almeno 5% diverso
|
||||
|
||||
_MUTATOR_SYSTEM_PROMPT = (
|
||||
"Sei un mutator evolutivo per prompt di strategie di trading algoritmico. "
|
||||
"Ricevi un PROMPT originale e una ISTRUZIONE di mutazione atomica. "
|
||||
"Produci una versione modificata del prompt che applica SOLO quella "
|
||||
"mutazione, preservando intent e struttura generale. "
|
||||
"Output: solo il nuovo prompt fra tag <prompt>...</prompt>. "
|
||||
"Nessun preambolo, nessuna spiegazione."
|
||||
)
|
||||
|
||||
_PROMPT_RE = re.compile(r"<prompt>\s*(.*?)\s*</prompt>", re.DOTALL | re.IGNORECASE)
|
||||
|
||||
|
||||
class _LLMClientLike(Protocol):
|
||||
"""Subset minimo dell'API LLMClient che usa l'operatore.
|
||||
|
||||
Permette di mockare l'LLM nei test senza importare la classe concreta.
|
||||
"""
|
||||
|
||||
def complete(
|
||||
self,
|
||||
genome: HypothesisAgentGenome,
|
||||
system: str,
|
||||
user: str,
|
||||
max_tokens: int = ...,
|
||||
) -> Any: ...
|
||||
|
||||
|
||||
def _extract_prompt(text: str) -> str:
|
||||
"""Estrae il prompt mutato dal completion text.
|
||||
|
||||
Cerca tag ``<prompt>...</prompt>``. Se assenti, ritorna il testo strip.
|
||||
"""
|
||||
m = _PROMPT_RE.search(text)
|
||||
if m:
|
||||
return m.group(1).strip()
|
||||
return text.strip()
|
||||
|
||||
|
||||
def _string_diff_ratio(a: str, b: str) -> float:
|
||||
"""Ritorna ``1 - similarity`` (0.0 = identici, 1.0 = completamente diversi)."""
|
||||
if not a and not b:
|
||||
return 0.0
|
||||
return 1.0 - SequenceMatcher(None, a, b).ratio()
|
||||
|
||||
|
||||
def is_valid_prompt(new_prompt: str, parent_prompt: str) -> bool:
|
||||
"""Validation gate per il prompt LLM-mutato.
|
||||
|
||||
Tre check:
|
||||
1. Lunghezza minima 50 caratteri.
|
||||
2. Contiene almeno una keyword tecnica (rsi, sma, signal, ecc).
|
||||
3. Diversità Levenshtein-like > 5% rispetto al parent.
|
||||
"""
|
||||
if len(new_prompt) < _MIN_PROMPT_LENGTH:
|
||||
return False
|
||||
lowered = new_prompt.lower()
|
||||
if not any(kw in lowered for kw in _VALID_KEYWORDS):
|
||||
return False
|
||||
if _string_diff_ratio(new_prompt, parent_prompt) < _MIN_DIFF_RATIO:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def mutate_prompt_llm(
|
||||
g: HypothesisAgentGenome,
|
||||
llm: _LLMClientLike,
|
||||
rng: random.Random,
|
||||
mutator_tier: ModelTier = ModelTier.B,
|
||||
max_tokens: int = 2000,
|
||||
cost_tracker: Any | None = None,
|
||||
repo: Any | None = None,
|
||||
run_id: str | None = None,
|
||||
) -> HypothesisAgentGenome:
|
||||
"""Operatore di mutazione prompt-level via LLM mutator.
|
||||
|
||||
Sceglie una mutation-instruction casuale fra sei tipi, fa una chiamata
|
||||
LLM tier B per ottenere il prompt mutato, valida l'output. Su validation
|
||||
fail (output troppo corto, non-strategia, troppo simile al parent),
|
||||
fallback silenzioso a ``random_mutate``.
|
||||
|
||||
Se ``cost_tracker``, ``repo`` e ``run_id`` sono forniti, la chiamata mutator
|
||||
viene registrata con ``call_kind="mutation"`` per audit budget.
|
||||
"""
|
||||
instruction_key = rng.choice(list(MUTATION_INSTRUCTIONS))
|
||||
instruction = MUTATION_INSTRUCTIONS[instruction_key]
|
||||
|
||||
user_prompt = (
|
||||
f"PROMPT ORIGINALE:\n{g.system_prompt}\n\n"
|
||||
f"ISTRUZIONE DI MUTAZIONE ({instruction_key}):\n{instruction}\n\n"
|
||||
f"Genera la versione modificata fra tag <prompt>...</prompt>."
|
||||
)
|
||||
|
||||
# Mutator usa un tier diverso (B) — clone temporaneo del genoma con tier override.
|
||||
mutator_genome = replace(g, model_tier=mutator_tier)
|
||||
|
||||
try:
|
||||
result = llm.complete(
|
||||
mutator_genome,
|
||||
system=_MUTATOR_SYSTEM_PROMPT,
|
||||
user=user_prompt,
|
||||
max_tokens=max_tokens,
|
||||
)
|
||||
except Exception:
|
||||
return random_mutate(g, rng)
|
||||
|
||||
# Cost tracking call_kind="mutation" se sink fornito.
|
||||
if cost_tracker is not None and repo is not None and run_id is not None:
|
||||
in_tok = getattr(result, "input_tokens", 0)
|
||||
out_tok = getattr(result, "output_tokens", 0)
|
||||
cr = cost_tracker.record(
|
||||
input_tokens=in_tok,
|
||||
output_tokens=out_tok,
|
||||
tier=mutator_tier,
|
||||
run_id=run_id,
|
||||
agent_id=g.id,
|
||||
call_kind="mutation",
|
||||
)
|
||||
repo.save_cost_record(
|
||||
run_id=run_id,
|
||||
agent_id=g.id,
|
||||
tier=mutator_tier.value,
|
||||
input_tokens=in_tok,
|
||||
output_tokens=out_tok,
|
||||
cost_usd=cr.cost_usd,
|
||||
call_kind="mutation",
|
||||
)
|
||||
|
||||
new_prompt = _extract_prompt(getattr(result, "text", ""))
|
||||
if not is_valid_prompt(new_prompt, g.system_prompt):
|
||||
return random_mutate(g, rng)
|
||||
|
||||
return _clone_with(g, system_prompt=new_prompt)
|
||||
@@ -0,0 +1,247 @@
|
||||
"""Libreria di stili cognitivi + direttive system_prompt per il GA.
|
||||
|
||||
Carica da un file JSON esterno (tipicamente shippato dal singolo strategy
|
||||
member, es. ``strategy_crypto/prompts.json``) le coppie ``style -> directive``
|
||||
usate da:
|
||||
- :func:`multi_swarm_core.ga.initial.build_initial_population` per il
|
||||
bootstrap della popolazione (style assegnato round-robin, directive
|
||||
come system_prompt iniziale).
|
||||
- :func:`multi_swarm_core.genome.mutation.mutate_cognitive_style` per
|
||||
pescare i candidati di mutazione (range = key del JSON).
|
||||
|
||||
Schema JSON atteso::
|
||||
|
||||
{
|
||||
"styles": {
|
||||
"<name>": {"directive": "<testo system_prompt>"},
|
||||
...
|
||||
},
|
||||
"agent_role": "<descrizione agente, opzionale>",
|
||||
"pattern_guidance": "<sezione PATTERN GUIDANCE, opzionale>",
|
||||
"instruction": "<frase finale USER, opzionale>",
|
||||
"domain_warnings": "<warning di dominio, opzionale>"
|
||||
}
|
||||
|
||||
I 6 stili default (physicist, biologist, historian, meteorologist,
|
||||
ecologist, engineer) sono comunque disponibili via :meth:`PromptLibrary.default`
|
||||
per backcompat con test/script senza file esterno.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
_DEFAULT_STYLES: dict[str, str] = {
|
||||
"physicist": (
|
||||
"Cerca leggi conservative, simmetrie, regimi di scala. "
|
||||
"Pensa in termini di flussi e potenziali."
|
||||
),
|
||||
"biologist": (
|
||||
"Cerca pattern adattivi, nicchie ecologiche, "
|
||||
"predator-prey dynamics tra partecipanti del mercato."
|
||||
),
|
||||
"historian": (
|
||||
"Cerca pattern ricorrenti su scale temporali multiple, "
|
||||
"analogie con regimi storici, mean reversion strutturali."
|
||||
),
|
||||
"meteorologist": (
|
||||
"Cerca regimi di volatilità che si autoalimentano, "
|
||||
"transizioni di stato come fronti, persistenza locale."
|
||||
),
|
||||
"ecologist": (
|
||||
"Cerca interazioni multi-asset, correlazioni cluster, "
|
||||
"segnali di stress sistemico nelle dinamiche di flusso."
|
||||
),
|
||||
"engineer": (
|
||||
"Cerca segnali con rapporto S/N favorevole, filtri causali, "
|
||||
"robustezza a perturbazioni di calibrazione."
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
class PromptLibraryError(ValueError):
|
||||
"""Sollevata su JSON malformato o stili invalid."""
|
||||
|
||||
|
||||
_DEFAULT_PATTERN_GUIDANCE = (
|
||||
"Forme di curva: trend (SMA cross), compressione volatilita (atr_pct basso), "
|
||||
"espansione volatilita (atr_pct alto), mean reversion (sma_pct estremo), "
|
||||
"momentum (macd_pct, rsi zone).\n\n"
|
||||
" Ripetibilita dell'andamento: crossover/crossunder ricorrenti, pattern intra-day "
|
||||
"(hour gate), pattern settimanali (dow/is_weekend), range breakout."
|
||||
)
|
||||
|
||||
_DEFAULT_AGENT_ROLE = (
|
||||
"Sei un agente generatore di ipotesi di trading quantitativo per un sistema swarm."
|
||||
)
|
||||
|
||||
_DEFAULT_INSTRUCTION = (
|
||||
"Genera una strategia che cerchi anomalie sfruttabili in questo regime."
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PromptLibrary:
|
||||
"""Set immutabile di stili cognitivi + direttive system_prompt.
|
||||
|
||||
v3.0: aggiunge 4 campi top-level strategy-specific iniettabili nel prompt
|
||||
dal compositor ``_build_system_prompt``. Tutti opzionali con default sensati
|
||||
per backcompat.
|
||||
v3.1: aggiunge anti_patterns e output_priorities iniettati dopo i VINCOLI core.
|
||||
"""
|
||||
|
||||
styles: dict[str, str]
|
||||
focus: dict[str, list[str]] # style -> lista metriche prioritarie
|
||||
# NEW v3.0: contenuto strategy-specific iniettabile nel prompt
|
||||
agent_role: str = field(default="") # header SYSTEM, descrive chi e' l'agente
|
||||
pattern_guidance: str = field(default="") # sezione PATTERN GUIDANCE del SYSTEM
|
||||
instruction: str = field(default="") # frase finale USER ("Genera una strategia...")
|
||||
domain_warnings: str = field(default="") # opzionale: warning di dominio (es. crypto 24/7)
|
||||
anti_patterns: str = field(default="") # NEW v3.1: lista esplicita di pattern da evitare
|
||||
output_priorities: str = field(default="") # NEW v3.1: trade-off espliciti di output
|
||||
# NEW v3.2: firme formali (arity + range) di indicatori strategy-specific
|
||||
# non noti al core. Iniettato in _SYSTEM_GRAMMAR_SPEC come estensione della
|
||||
# lista "Leaf - indicatori" per consentire alla LLM di chiamarli correttamente.
|
||||
custom_indicators_spec: str = field(default="")
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not self.styles:
|
||||
raise PromptLibraryError("PromptLibrary deve avere almeno uno stile")
|
||||
for name, directive in self.styles.items():
|
||||
if not isinstance(name, str) or not name.strip():
|
||||
raise PromptLibraryError(f"nome stile invalido: {name!r}")
|
||||
if not isinstance(directive, str) or not directive.strip():
|
||||
raise PromptLibraryError(
|
||||
f"directive vuota o invalida per stile {name!r}"
|
||||
)
|
||||
# Validate new optional string fields: if provided must be non-whitespace
|
||||
for field_name, value in (
|
||||
("agent_role", self.agent_role),
|
||||
("pattern_guidance", self.pattern_guidance),
|
||||
("instruction", self.instruction),
|
||||
("domain_warnings", self.domain_warnings),
|
||||
("anti_patterns", self.anti_patterns),
|
||||
("output_priorities", self.output_priorities),
|
||||
("custom_indicators_spec", self.custom_indicators_spec),
|
||||
):
|
||||
if not isinstance(value, str):
|
||||
raise PromptLibraryError(
|
||||
f"campo '{field_name}' deve essere stringa, non {type(value)}"
|
||||
)
|
||||
if value and not value.strip():
|
||||
raise PromptLibraryError(
|
||||
f"campo '{field_name}' fornito ma contiene solo whitespace"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def default(cls) -> PromptLibrary:
|
||||
"""Libreria builtin con i 6 stili originali (fallback senza file)."""
|
||||
return cls(
|
||||
styles=dict(_DEFAULT_STYLES),
|
||||
focus={},
|
||||
agent_role=_DEFAULT_AGENT_ROLE,
|
||||
pattern_guidance=_DEFAULT_PATTERN_GUIDANCE,
|
||||
instruction=_DEFAULT_INSTRUCTION,
|
||||
domain_warnings="",
|
||||
anti_patterns="",
|
||||
output_priorities="",
|
||||
custom_indicators_spec="",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, path: Path | str) -> PromptLibrary:
|
||||
"""Carica da file JSON.
|
||||
|
||||
Schema::
|
||||
|
||||
{
|
||||
"styles": {<name>: {"directive": "...", "focus_metrics": [...]}},
|
||||
"agent_role": "...", // opzionale
|
||||
"pattern_guidance": "...", // opzionale
|
||||
"instruction": "...", // opzionale
|
||||
"domain_warnings": "..." // opzionale
|
||||
}
|
||||
"""
|
||||
p = Path(path)
|
||||
try:
|
||||
data = json.loads(p.read_text(encoding="utf-8"))
|
||||
except FileNotFoundError as e:
|
||||
raise PromptLibraryError(f"file non trovato: {p}") from e
|
||||
except json.JSONDecodeError as e:
|
||||
raise PromptLibraryError(f"JSON malformato in {p}: {e}") from e
|
||||
|
||||
if not isinstance(data, dict):
|
||||
raise PromptLibraryError(f"root JSON deve essere un object, non {type(data)}")
|
||||
styles_raw = data.get("styles")
|
||||
if not isinstance(styles_raw, dict):
|
||||
raise PromptLibraryError("manca chiave 'styles' (object) nel JSON")
|
||||
|
||||
styles: dict[str, str] = {}
|
||||
focus: dict[str, list[str]] = {}
|
||||
for name, entry in styles_raw.items():
|
||||
if not isinstance(entry, dict):
|
||||
raise PromptLibraryError(
|
||||
f"entry per stile {name!r} deve essere object, non {type(entry)}"
|
||||
)
|
||||
directive = entry.get("directive")
|
||||
if not isinstance(directive, str):
|
||||
raise PromptLibraryError(
|
||||
f"manca 'directive' (string) per stile {name!r}"
|
||||
)
|
||||
styles[name] = directive
|
||||
fm = entry.get("focus_metrics", [])
|
||||
if not isinstance(fm, list):
|
||||
raise PromptLibraryError(
|
||||
f"focus_metrics di {name!r} deve essere lista, non {type(fm)}"
|
||||
)
|
||||
focus[name] = [str(x) for x in fm]
|
||||
|
||||
# Parse new optional top-level fields (v3.0)
|
||||
agent_role = data.get("agent_role", "")
|
||||
pattern_guidance = data.get("pattern_guidance", "")
|
||||
instruction = data.get("instruction", "")
|
||||
domain_warnings = data.get("domain_warnings", "")
|
||||
# Parse new optional top-level fields (v3.1)
|
||||
anti_patterns_raw = data.get("anti_patterns", "")
|
||||
output_priorities_raw = data.get("output_priorities", "")
|
||||
if not isinstance(anti_patterns_raw, str):
|
||||
raise PromptLibraryError(f"anti_patterns deve essere stringa, non {type(anti_patterns_raw)}")
|
||||
if not isinstance(output_priorities_raw, str):
|
||||
raise PromptLibraryError(f"output_priorities deve essere stringa, non {type(output_priorities_raw)}")
|
||||
# Parse new optional top-level field (v3.2)
|
||||
custom_indicators_spec_raw = data.get("custom_indicators_spec", "")
|
||||
if not isinstance(custom_indicators_spec_raw, str):
|
||||
raise PromptLibraryError(
|
||||
f"custom_indicators_spec deve essere stringa, non {type(custom_indicators_spec_raw)}"
|
||||
)
|
||||
|
||||
return cls(
|
||||
styles=styles,
|
||||
focus=focus,
|
||||
agent_role=agent_role,
|
||||
pattern_guidance=pattern_guidance,
|
||||
instruction=instruction,
|
||||
domain_warnings=domain_warnings,
|
||||
anti_patterns=anti_patterns_raw,
|
||||
output_priorities=output_priorities_raw,
|
||||
custom_indicators_spec=custom_indicators_spec_raw,
|
||||
)
|
||||
|
||||
@property
|
||||
def cognitive_styles(self) -> tuple[str, ...]:
|
||||
"""Tupla immutabile dei nomi degli stili, nell'ordine di insertion del JSON."""
|
||||
return tuple(self.styles)
|
||||
|
||||
def directive(self, style: str) -> str:
|
||||
"""Ritorna la directive di ``style`` o solleva KeyError."""
|
||||
return self.styles[style]
|
||||
|
||||
def style_at(self, index: int) -> str:
|
||||
"""Round-robin: ``cognitive_styles[index % len()]``."""
|
||||
return self.cognitive_styles[index % len(self.cognitive_styles)]
|
||||
|
||||
def focus_metrics_for(self, style: str) -> list[str]:
|
||||
"""Ritorna lista delle metriche prioritarie per ``style``. Empty list se non specificate."""
|
||||
return self.focus.get(style, [])
|
||||
@@ -0,0 +1,201 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
|
||||
import httpx
|
||||
from tenacity import (
|
||||
retry,
|
||||
retry_if_exception_type,
|
||||
stop_after_attempt,
|
||||
wait_exponential,
|
||||
)
|
||||
|
||||
from ..genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
MODEL_TIER_MAP: dict[ModelTier, str] = {
|
||||
ModelTier.S: "claude-opus-4-7",
|
||||
ModelTier.A: "claude-opus-4-7",
|
||||
ModelTier.B: "claude-sonnet-4-6",
|
||||
ModelTier.C: "claude-sonnet-4-6",
|
||||
ModelTier.D: "claude-haiku-4-5-20251001",
|
||||
}
|
||||
|
||||
|
||||
class EmptyCompletionError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class OpusAgentError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class OpusAgentTransientError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
_RETRYABLE_EXCEPTIONS: tuple[type[BaseException], ...] = (
|
||||
httpx.ConnectError,
|
||||
httpx.TimeoutException,
|
||||
OpusAgentTransientError,
|
||||
EmptyCompletionError,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CompletionResult:
|
||||
text: str
|
||||
input_tokens: int
|
||||
output_tokens: int
|
||||
tier: ModelTier
|
||||
model: str
|
||||
session_id: str | None = None
|
||||
|
||||
|
||||
class LLMClient:
|
||||
def __init__(
|
||||
self,
|
||||
opus_agent_api_key: str,
|
||||
opus_agent_base_url: str = "https://opus-agent.tielogic.xyz",
|
||||
model_tier_s: str = "claude-opus-4-7",
|
||||
model_tier_a: str = "claude-opus-4-7",
|
||||
model_tier_b: str = "claude-sonnet-4-6",
|
||||
model_tier_c: str = "claude-sonnet-4-6",
|
||||
model_tier_d: str = "claude-haiku-4-5-20251001",
|
||||
poll_interval: float = 3.0,
|
||||
poll_timeout: float = 180.0,
|
||||
) -> None:
|
||||
self._base_url = opus_agent_base_url.rstrip("/")
|
||||
self._api_key = opus_agent_api_key
|
||||
self._tier_map: dict[ModelTier, str] = {
|
||||
ModelTier.S: model_tier_s,
|
||||
ModelTier.A: model_tier_a,
|
||||
ModelTier.B: model_tier_b,
|
||||
ModelTier.C: model_tier_c,
|
||||
ModelTier.D: model_tier_d,
|
||||
}
|
||||
self._poll_interval = poll_interval
|
||||
self._poll_timeout = poll_timeout
|
||||
self._topic_cache: dict[str, str] = {}
|
||||
self._topic_lock = threading.Lock()
|
||||
self._client = httpx.Client(
|
||||
base_url=self._base_url,
|
||||
headers={"X-Api-Key": self._api_key, "Content-Type": "application/json"},
|
||||
timeout=30.0,
|
||||
)
|
||||
|
||||
def _get_or_create_topic(self, system_prompt: str) -> str:
|
||||
prompt_hash = hashlib.sha256(system_prompt.encode()).hexdigest()[:16]
|
||||
if prompt_hash in self._topic_cache:
|
||||
return self._topic_cache[prompt_hash]
|
||||
|
||||
with self._topic_lock:
|
||||
if prompt_hash in self._topic_cache:
|
||||
return self._topic_cache[prompt_hash]
|
||||
|
||||
topic_name = f"swarm-{prompt_hash}"
|
||||
resp = self._client.post("/api/topics", json={
|
||||
"name": topic_name,
|
||||
"system_prompt": system_prompt,
|
||||
})
|
||||
|
||||
if resp.status_code == 409:
|
||||
list_resp = self._client.get("/api/topics")
|
||||
list_resp.raise_for_status()
|
||||
for topic in list_resp.json()["data"]:
|
||||
if topic["name"] == topic_name:
|
||||
self._topic_cache[prompt_hash] = topic["id"]
|
||||
return topic["id"]
|
||||
raise OpusAgentError(f"Topic {topic_name} conflict but not found")
|
||||
|
||||
if resp.status_code >= 500:
|
||||
raise OpusAgentTransientError(f"Server error {resp.status_code}")
|
||||
resp.raise_for_status()
|
||||
|
||||
topic_id = resp.json()["data"]["id"]
|
||||
self._topic_cache[prompt_hash] = topic_id
|
||||
logger.debug("Created topic %s -> %s", topic_name, topic_id)
|
||||
return topic_id
|
||||
|
||||
def _poll_result(self, request_id: str) -> dict:
|
||||
deadline = time.monotonic() + self._poll_timeout
|
||||
while time.monotonic() < deadline:
|
||||
resp = self._client.get(f"/api/requests/{request_id}")
|
||||
resp.raise_for_status()
|
||||
data = resp.json()["data"]
|
||||
status = data["status"]
|
||||
if status == "completed":
|
||||
return data
|
||||
if status == "failed":
|
||||
error = data.get("error") or "unknown error"
|
||||
raise OpusAgentError(f"Request {request_id} failed: {error}")
|
||||
time.sleep(self._poll_interval)
|
||||
raise OpusAgentTransientError(
|
||||
f"Request {request_id} timed out after {self._poll_timeout}s"
|
||||
)
|
||||
|
||||
def close_session(self, session_id: str) -> None:
|
||||
try:
|
||||
self._client.delete(f"/api/sessions/{session_id}")
|
||||
except httpx.HTTPError:
|
||||
logger.debug("Failed to close session %s", session_id)
|
||||
|
||||
@retry(
|
||||
stop=stop_after_attempt(3),
|
||||
wait=wait_exponential(multiplier=2.0, min=2.0, max=30.0),
|
||||
retry=retry_if_exception_type(_RETRYABLE_EXCEPTIONS),
|
||||
reraise=True,
|
||||
)
|
||||
def complete(
|
||||
self,
|
||||
genome: HypothesisAgentGenome,
|
||||
system: str,
|
||||
user: str,
|
||||
max_tokens: int = 2000,
|
||||
session_id: str | None = None,
|
||||
summarize: bool = False,
|
||||
) -> CompletionResult:
|
||||
model = self._tier_map[genome.model_tier]
|
||||
topic_id = self._get_or_create_topic(system)
|
||||
|
||||
prompt = f"[SYSTEM]\n{system}\n\n[USER]\n{user}" if session_id in (None, "new") else user
|
||||
|
||||
body: dict = {
|
||||
"topic_id": topic_id,
|
||||
"prompt": prompt,
|
||||
"model": model,
|
||||
}
|
||||
if session_id is not None:
|
||||
body["session_id"] = session_id
|
||||
if summarize:
|
||||
body["summarize"] = True
|
||||
|
||||
resp = self._client.post("/api/requests", json=body)
|
||||
|
||||
if resp.status_code == 429:
|
||||
raise OpusAgentTransientError("Rate limited")
|
||||
if resp.status_code >= 500:
|
||||
raise OpusAgentTransientError(f"Server error {resp.status_code}")
|
||||
if resp.status_code != 202:
|
||||
raise OpusAgentError(f"Unexpected status {resp.status_code}: {resp.text}")
|
||||
|
||||
request_id = resp.json()["data"]["id"]
|
||||
result = self._poll_result(request_id)
|
||||
|
||||
text = result.get("result") or ""
|
||||
if not text:
|
||||
raise EmptyCompletionError(f"empty response from OpusAgent ({model})")
|
||||
|
||||
return CompletionResult(
|
||||
text=text,
|
||||
input_tokens=0,
|
||||
output_tokens=0,
|
||||
tier=genome.model_tier,
|
||||
model=model,
|
||||
session_id=result.get("session_id"),
|
||||
)
|
||||
+16
-4
@@ -8,11 +8,11 @@ from typing import Any
|
||||
from ..genome.hypothesis import ModelTier
|
||||
|
||||
PRICE_PER_M_TOKENS: dict[ModelTier, dict[str, float]] = {
|
||||
ModelTier.S: {"input": 15.00, "output": 75.00},
|
||||
ModelTier.A: {"input": 3.00, "output": 15.00},
|
||||
ModelTier.B: {"input": 3.00, "output": 15.00},
|
||||
ModelTier.S: {"input": 0.50, "output": 3.00},
|
||||
ModelTier.A: {"input": 0.14, "output": 0.28},
|
||||
ModelTier.B: {"input": 0.14, "output": 0.28},
|
||||
ModelTier.C: {"input": 0.40, "output": 0.40},
|
||||
ModelTier.D: {"input": 0.10, "output": 0.30},
|
||||
ModelTier.D: {"input": 0.03, "output": 0.14},
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ class CostRecord:
|
||||
input_tokens: int
|
||||
output_tokens: int
|
||||
cost_usd: float
|
||||
call_kind: str = "hypothesis" # "hypothesis" | "mutation"
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -43,6 +44,7 @@ class CostTracker:
|
||||
tier: ModelTier,
|
||||
run_id: str,
|
||||
agent_id: str,
|
||||
call_kind: str = "hypothesis",
|
||||
) -> CostRecord:
|
||||
cost = estimate_cost(input_tokens, output_tokens, tier)
|
||||
rec = CostRecord(
|
||||
@@ -53,6 +55,7 @@ class CostTracker:
|
||||
input_tokens=input_tokens,
|
||||
output_tokens=output_tokens,
|
||||
cost_usd=cost,
|
||||
call_kind=call_kind,
|
||||
)
|
||||
self.records.append(rec)
|
||||
return rec
|
||||
@@ -61,16 +64,25 @@ class CostTracker:
|
||||
by_tier: dict[str, dict[str, float]] = defaultdict(
|
||||
lambda: {"calls": 0, "input_tokens": 0, "output_tokens": 0, "cost_usd": 0.0}
|
||||
)
|
||||
by_call_kind: dict[str, dict[str, float]] = defaultdict(
|
||||
lambda: {"calls": 0, "input_tokens": 0, "output_tokens": 0, "cost_usd": 0.0}
|
||||
)
|
||||
for r in self.records:
|
||||
t = r.tier.value
|
||||
by_tier[t]["calls"] += 1
|
||||
by_tier[t]["input_tokens"] += r.input_tokens
|
||||
by_tier[t]["output_tokens"] += r.output_tokens
|
||||
by_tier[t]["cost_usd"] += r.cost_usd
|
||||
ck = r.call_kind
|
||||
by_call_kind[ck]["calls"] += 1
|
||||
by_call_kind[ck]["input_tokens"] += r.input_tokens
|
||||
by_call_kind[ck]["output_tokens"] += r.output_tokens
|
||||
by_call_kind[ck]["cost_usd"] += r.cost_usd
|
||||
return {
|
||||
"calls": len(self.records),
|
||||
"input_tokens": sum(r.input_tokens for r in self.records),
|
||||
"output_tokens": sum(r.output_tokens for r in self.records),
|
||||
"cost_usd": sum(r.cost_usd for r in self.records),
|
||||
"by_tier": dict(by_tier),
|
||||
"by_call_kind": dict(by_call_kind),
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
|
||||
def autocorr_lag1(returns: pd.Series) -> float:
|
||||
"""Autocorrelazione dei ritorni con lag 1 (correlazione di Pearson)."""
|
||||
if len(returns) < 2:
|
||||
return 0.0
|
||||
val = returns.autocorr(lag=1)
|
||||
return float(val) if pd.notna(val) else 0.0
|
||||
|
||||
|
||||
def hurst_exponent(returns: pd.Series) -> float:
|
||||
"""Hurst exponent via R/S analysis (rescaled range).
|
||||
|
||||
Range 0-1: 0.5 random walk, >0.55 trending persistente, <0.45 mean reverting.
|
||||
Implementazione classica con scale multiple (2^k bins).
|
||||
"""
|
||||
n = len(returns)
|
||||
if n < 100:
|
||||
return 0.5 # insufficiente dati, ritorna random-walk default
|
||||
|
||||
series = returns.dropna().values
|
||||
n = len(series)
|
||||
if n < 100:
|
||||
return 0.5
|
||||
|
||||
# Scale: 2^k che dividono n in segmenti
|
||||
scales = [2**k for k in range(4, int(np.log2(n // 2)) + 1)]
|
||||
if not scales:
|
||||
return 0.5
|
||||
|
||||
rs_values: list[float] = []
|
||||
for scale in scales:
|
||||
n_chunks = n // scale
|
||||
if n_chunks < 1:
|
||||
continue
|
||||
rs_chunk: list[float] = []
|
||||
for i in range(n_chunks):
|
||||
chunk = series[i * scale : (i + 1) * scale]
|
||||
mean = chunk.mean()
|
||||
cumdev = (chunk - mean).cumsum()
|
||||
r = cumdev.max() - cumdev.min()
|
||||
s = chunk.std()
|
||||
if s > 0:
|
||||
rs_chunk.append(r / s)
|
||||
if rs_chunk:
|
||||
rs_values.append(float(np.mean(rs_chunk)))
|
||||
|
||||
if len(rs_values) < 2:
|
||||
return 0.5
|
||||
|
||||
log_scales = np.log(scales[: len(rs_values)])
|
||||
log_rs = np.log(rs_values)
|
||||
# Hurst = slope della regressione log-log
|
||||
h, _ = np.polyfit(log_scales, log_rs, 1)
|
||||
return float(np.clip(h, 0.0, 1.0))
|
||||
|
||||
|
||||
def vol_percentile_historical(
|
||||
returns: pd.Series,
|
||||
current_window: int = 24,
|
||||
ref_window: int = 2000,
|
||||
) -> float:
|
||||
"""Percentile (0-100) della vol corrente nella distribuzione storica.
|
||||
|
||||
Vol = std rolling su current_window barre. Confronta l'ultimo valore contro
|
||||
la distribuzione dei valori della stessa std rolling sugli ultimi ref_window.
|
||||
Output: 0 (vol attuale tra le piu basse), 100 (tra le piu alte).
|
||||
"""
|
||||
if len(returns) < max(current_window, 100):
|
||||
return 50.0
|
||||
|
||||
rolling_vol = returns.rolling(current_window, min_periods=current_window).std()
|
||||
rolling_vol = rolling_vol.dropna()
|
||||
if len(rolling_vol) < 10:
|
||||
return 50.0
|
||||
|
||||
# Limita ref_window all'effettiva disponibilita
|
||||
ref = rolling_vol.iloc[-ref_window:] if len(rolling_vol) > ref_window else rolling_vol
|
||||
current = float(rolling_vol.iloc[-1])
|
||||
pct = float((ref < current).sum()) / len(ref) * 100.0
|
||||
return pct
|
||||
|
||||
|
||||
def seasonality_strength(
|
||||
returns: pd.Series,
|
||||
by: str,
|
||||
) -> float:
|
||||
"""Frazione di varianza dei ritorni spiegata dalla feature temporale `by`.
|
||||
|
||||
`by` in {"hour", "dow"}. Output 0-1: 0 = no seasonality, 1 = tutta la varianza
|
||||
e dovuta al ciclo. Calcolato come 1 - (var residua / var totale) usando i
|
||||
gruppi indotti dalla feature.
|
||||
"""
|
||||
if not isinstance(returns.index, pd.DatetimeIndex):
|
||||
return 0.0
|
||||
if len(returns) < 50:
|
||||
return 0.0
|
||||
|
||||
if by == "hour":
|
||||
groups = returns.index.hour
|
||||
elif by == "dow":
|
||||
groups = returns.index.dayofweek
|
||||
else:
|
||||
raise ValueError(f"by deve essere 'hour' o 'dow', non {by!r}")
|
||||
|
||||
total_var = float(returns.var())
|
||||
if total_var <= 0:
|
||||
return 0.0
|
||||
|
||||
grouped = returns.groupby(groups)
|
||||
group_means = grouped.transform("mean")
|
||||
residuals = returns - group_means
|
||||
residual_var = float(residuals.var())
|
||||
|
||||
explained = 1.0 - (residual_var / total_var)
|
||||
return float(np.clip(explained, 0.0, 1.0))
|
||||
|
||||
|
||||
def sharpe_ratio(returns: pd.Series, periods_per_year: int = 8760, rf: float = 0.0) -> float:
|
||||
"""Sharpe annualizzato. periods_per_year=8760 per dati orari."""
|
||||
excess = returns - rf / periods_per_year
|
||||
std = excess.std(ddof=1)
|
||||
if std == 0 or np.isnan(std):
|
||||
return 0.0
|
||||
return float(np.sqrt(periods_per_year) * excess.mean() / std)
|
||||
|
||||
|
||||
def max_drawdown(equity: pd.Series) -> float:
|
||||
"""Max drawdown percentuale (positivo)."""
|
||||
peak = equity.cummax()
|
||||
dd = (peak - equity) / peak.replace(0, np.nan)
|
||||
dd = dd.fillna(0.0)
|
||||
return float(dd.max())
|
||||
|
||||
|
||||
def total_return(equity: pd.Series) -> float:
|
||||
if equity.iloc[0] == 0:
|
||||
return float(equity.iloc[-1])
|
||||
return float(equity.iloc[-1] / equity.iloc[0] - 1.0)
|
||||
|
||||
|
||||
def efficiency_ratio_kaufman(prices: pd.Series, length: int = 100) -> float:
|
||||
"""Kaufman Efficiency Ratio: |net move| / sum(|step|) su rolling length.
|
||||
|
||||
Output 0-1: 0 = puro noise (movimento dissipativo), 1 = puro trend efficiente.
|
||||
Discriminatore trending/ranging robusto.
|
||||
"""
|
||||
if len(prices) < length + 1:
|
||||
return 0.0
|
||||
recent = prices.iloc[-length:]
|
||||
net_move = abs(recent.iloc[-1] - recent.iloc[0])
|
||||
total_move = recent.diff().abs().sum()
|
||||
if total_move == 0 or pd.isna(total_move):
|
||||
return 0.0
|
||||
return float(net_move / total_move)
|
||||
|
||||
|
||||
def tail_index_hill(returns: pd.Series, side: str, top_frac: float = 0.05) -> float:
|
||||
"""Hill estimator del tail index (pendenza coda) per side in {'left', 'right'}.
|
||||
|
||||
Output: indice di pesantezza coda. Valori piu bassi = coda piu pesante.
|
||||
<2 = varianza infinita (Cauchy-like), 3-4 = tipico crypto, >5 = quasi-Gaussiana.
|
||||
Robusto al singolo outlier (vs kurtosis).
|
||||
"""
|
||||
r = returns.dropna()
|
||||
n = len(r)
|
||||
if n < 50:
|
||||
return 5.0 # default quasi-Gaussiano
|
||||
k = max(int(n * top_frac), 5)
|
||||
if side == "left":
|
||||
tail = (-r).nlargest(k)
|
||||
elif side == "right":
|
||||
tail = r.nlargest(k)
|
||||
else:
|
||||
raise ValueError(f"side deve essere 'left' o 'right', non {side!r}")
|
||||
# Filtra valori non-positivi (Hill richiede tail positiva)
|
||||
tail = tail[tail > 0]
|
||||
if len(tail) < 5:
|
||||
return 5.0
|
||||
log_tail = np.log(tail.values)
|
||||
threshold = log_tail[-1]
|
||||
excess = log_tail[:-1] - threshold
|
||||
if excess.sum() <= 0:
|
||||
return 5.0
|
||||
hill = (len(excess)) / excess.sum()
|
||||
return float(np.clip(hill, 1.0, 10.0))
|
||||
|
||||
|
||||
def structural_uptrend_score(prices: pd.Series, window: int = 5) -> float:
|
||||
"""Frazione di periodi in struttura HH/HL (Dow-style uptrend).
|
||||
|
||||
Identifica swing high/low usando rolling max/min con finestra ``window``.
|
||||
Conta sequenze higher-high + higher-low (uptrend) vs lower-high + lower-low (downtrend).
|
||||
Output: 0 (puro downtrend) - 0.5 (range/mixed) - 1 (puro uptrend).
|
||||
"""
|
||||
if len(prices) < 4 * window:
|
||||
return 0.5
|
||||
swing_high = prices.rolling(2 * window + 1, center=True).max() == prices
|
||||
swing_low = prices.rolling(2 * window + 1, center=True).min() == prices
|
||||
|
||||
highs = prices[swing_high].dropna()
|
||||
lows = prices[swing_low].dropna()
|
||||
|
||||
if len(highs) < 3 or len(lows) < 3:
|
||||
return 0.5
|
||||
|
||||
hh = (highs.diff() > 0).sum()
|
||||
lh = (highs.diff() < 0).sum()
|
||||
hl = (lows.diff() > 0).sum()
|
||||
ll = (lows.diff() < 0).sum()
|
||||
|
||||
up_signals = hh + hl
|
||||
down_signals = lh + ll
|
||||
total = up_signals + down_signals
|
||||
if total == 0:
|
||||
return 0.5
|
||||
return float(up_signals / total)
|
||||
|
||||
|
||||
def compression_ratio(prices: pd.Series, recent_window: int = 50, ref_window: int = 200) -> float:
|
||||
"""Range(recent) / Range(ref). <1 = compressione vol, >1 = espansione.
|
||||
|
||||
Range = high - low della finestra. Cattura il "coil" pre-breakout.
|
||||
"""
|
||||
if len(prices) < ref_window:
|
||||
return 1.0
|
||||
recent = prices.iloc[-recent_window:]
|
||||
ref = prices.iloc[-ref_window:]
|
||||
recent_range = float(recent.max() - recent.min())
|
||||
ref_range = float(ref.max() - ref.min())
|
||||
if ref_range <= 0:
|
||||
return 1.0
|
||||
return recent_range / ref_range
|
||||
|
||||
|
||||
def spectral_entropy_and_cycle(
|
||||
returns: pd.Series, length: int = 256
|
||||
) -> tuple[float, float | None]:
|
||||
"""FFT su returns -> (entropy normalizzata, dominant_cycle gated).
|
||||
|
||||
entropy: 0-1, Shannon entropy normalizzata dello spettro di potenza.
|
||||
0 = una sola frequenza domina, 1 = spettro piatto (rumore bianco).
|
||||
dominant_cycle: periodo (barre) della frequenza dominante,
|
||||
MA solo se entropy < 0.6 (struttura presente). Altrimenti None.
|
||||
"""
|
||||
r = returns.dropna()
|
||||
if len(r) < length:
|
||||
return 1.0, None
|
||||
series = r.iloc[-length:].values
|
||||
# Detrend
|
||||
series = series - series.mean()
|
||||
fft = np.fft.rfft(series)
|
||||
power = np.abs(fft) ** 2
|
||||
if power.sum() <= 0:
|
||||
return 1.0, None
|
||||
p = power / power.sum()
|
||||
# Entropy normalizzata (Shannon / log(N))
|
||||
nonzero = p[p > 0]
|
||||
entropy = float(-(nonzero * np.log(nonzero)).sum() / np.log(len(nonzero)))
|
||||
entropy = max(0.0, min(1.0, entropy))
|
||||
|
||||
if entropy >= 0.6:
|
||||
return entropy, None
|
||||
# Skip DC component (index 0)
|
||||
if len(power) <= 1:
|
||||
return entropy, None
|
||||
peak_idx = int(np.argmax(power[1:])) + 1
|
||||
if peak_idx == 0:
|
||||
return entropy, None
|
||||
cycle = float(length / peak_idx)
|
||||
return entropy, cycle
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Metriche di diversità popolazione.
|
||||
|
||||
``population_prompt_diversity`` calcola la diversità media fra i prompt di una
|
||||
popolazione tramite la similarity di ``difflib.SequenceMatcher`` (proxy di
|
||||
Levenshtein normalizzata): 0.0 = tutti i prompt identici, 1.0 = tutti diversi.
|
||||
|
||||
Usata come telemetry Phase 2.5 per monitorare se ``mutate_prompt_llm`` sta
|
||||
effettivamente introducendo diversità di prompt nel pool.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from difflib import SequenceMatcher
|
||||
from itertools import combinations
|
||||
|
||||
|
||||
def population_prompt_diversity(prompts: list[str]) -> float:
|
||||
"""Diversità media (0.0 - 1.0) sui prompt della popolazione.
|
||||
|
||||
Calcolo: media di ``1 - similarity(a, b)`` su tutte le coppie distinte.
|
||||
Per N prompt il numero di coppie è ``N*(N-1)/2``. Con N=20 sono 190 coppie
|
||||
— trascurabile a livello di compute.
|
||||
"""
|
||||
if len(prompts) < 2:
|
||||
return 0.0
|
||||
diffs = [
|
||||
1.0 - SequenceMatcher(None, a, b).ratio()
|
||||
for a, b in combinations(prompts, 2)
|
||||
]
|
||||
return sum(diffs) / len(diffs)
|
||||
@@ -0,0 +1,343 @@
|
||||
"""End-to-end orchestrator per un run di Phase 1.
|
||||
|
||||
Pipeline per ogni generazione:
|
||||
|
||||
population -> hypothesis_agent.propose -> falsification + adversarial
|
||||
-> compute_fitness -> persistenza -> next_generation
|
||||
|
||||
Tutto e' loggato sulla repository SQLite (runs, generations, genomes,
|
||||
evaluations, cost_records, adversarial_findings) cosi' che la GUI Streamlit
|
||||
possa leggere lo stato a run terminato (o in corso).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from ..agents.adversarial import AdversarialAgent
|
||||
from ..agents.falsification import FalsificationAgent
|
||||
from ..agents.hypothesis import HypothesisAgent, HypothesisProposal, MarketSummary
|
||||
from ..agents.market_summary import build_market_summary
|
||||
from ..ga.fitness import compute_fitness
|
||||
from ..ga.initial import build_initial_population
|
||||
from ..ga.loop import GAConfig, next_generation
|
||||
from ..ga.summary import generation_summary
|
||||
from ..genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
from ..genome.mutation import set_cognitive_styles
|
||||
from ..genome.prompt_library import PromptLibrary
|
||||
from ..llm.client import LLMClient
|
||||
from ..llm.cost_tracker import CostTracker
|
||||
from ..persistence.repository import Repository
|
||||
|
||||
|
||||
@dataclass
|
||||
class RunConfig:
|
||||
"""Parametri di un run end-to-end della Phase 1."""
|
||||
|
||||
run_name: str
|
||||
population_size: int = 20
|
||||
n_generations: int = 10
|
||||
elite_k: int = 2
|
||||
tournament_k: int = 3
|
||||
p_crossover: float = 0.5
|
||||
seed: int = 42
|
||||
model_tier: ModelTier = ModelTier.C
|
||||
symbol: str = "BTC/USDT"
|
||||
timeframe: str = "1h"
|
||||
fees_bp: float = 5.0
|
||||
n_trials_dsr: int = 50
|
||||
db_path: Path = field(default_factory=lambda: Path("./runs.db"))
|
||||
prompt_mutation_weight: float = 0.0 # Phase 2.5: opt-in LLM mutator
|
||||
fees_eat_alpha_threshold: float = 0.5 # adversarial gate, allenta verso 0.7-0.8
|
||||
flat_too_long_threshold: float = 0.95 # adversarial gate, allenta verso 0.98-0.99
|
||||
undertrading_threshold: int = 10 # min trades, sotto = "lucky shot" HIGH
|
||||
# Fitness v2: tuple non vuota → soft-kill (solo findings listate azzerano).
|
||||
# None/empty → v1 (tutti HIGH azzerano, backward compat).
|
||||
fitness_hard_kill_findings: tuple[str, ...] | None = None
|
||||
fitness_adversarial_soft_penalty: float = 0.4
|
||||
# Walk-Forward Validation: train sui primi train_split% delle bar, OOS re-eval
|
||||
# dei top genomi sui restanti. None/0 = no WFA (eval full ohlcv).
|
||||
wfa_train_split: float | None = None
|
||||
wfa_top_k: int = 5 # quanti top genomi rivalutare OOS
|
||||
# Multi-objective selection: se True, ogni genome viene valutato anche su
|
||||
# test_ohlcv durante il loop e la fitness usata per tournament/elite è
|
||||
# combined = alpha*IS + (1-alpha)*OOS. Richiede wfa_train_split attivo.
|
||||
# 2x costo backtest engine.
|
||||
eval_oos_during_loop: bool = False
|
||||
fitness_combined_alpha: float = 0.5 # peso IS (1-alpha = OOS)
|
||||
# Libreria di stili cognitivi + system_prompt iniziali. Se None usa
|
||||
# i 6 builtin (PromptLibrary.default()). Tipicamente caricata da
|
||||
# strategy_crypto/prompts.json via PromptLibrary.from_json().
|
||||
prompt_library: PromptLibrary | None = None
|
||||
# Numero di propose() LLM concorrenti per generazione. 1 = sequenziale (default,
|
||||
# backward compat). OpusAgent processa in coda FIFO; concurrency > 1
|
||||
# accoda piu' richieste in parallelo ma il throughput dipende dal server.
|
||||
llm_concurrency: int = 1
|
||||
|
||||
|
||||
def _parallel_propose(
|
||||
agent: HypothesisAgent,
|
||||
genomes: list[HypothesisAgentGenome],
|
||||
market: MarketSummary,
|
||||
n_workers: int,
|
||||
) -> list[HypothesisProposal]:
|
||||
"""Esegue ``agent.propose()`` su una lista di genomi, opzionalmente in parallelo.
|
||||
|
||||
``n_workers <= 1`` mantiene il comportamento serial originale (ordine fisso,
|
||||
determinismo data un seed). ``n_workers > 1`` usa un thread pool: l'order
|
||||
dei risultati e' preservato (1:1 con ``genomes``). LLMClient (OpusAgent)
|
||||
e' thread-safe; ``PromptLibrary`` e ``HypothesisAgent`` non hanno stato mutabile.
|
||||
"""
|
||||
if n_workers <= 1 or len(genomes) <= 1:
|
||||
return [agent.propose(g, market) for g in genomes]
|
||||
with ThreadPoolExecutor(max_workers=n_workers) as pool:
|
||||
return list(pool.map(lambda g: agent.propose(g, market), genomes))
|
||||
|
||||
|
||||
def run_phase1(
|
||||
cfg: RunConfig,
|
||||
ohlcv: pd.DataFrame,
|
||||
llm: LLMClient,
|
||||
) -> str:
|
||||
"""Esegue il loop GA end-to-end e ritorna l'``id`` del run.
|
||||
|
||||
Su qualunque eccezione marca il run come ``failed`` e rilancia.
|
||||
"""
|
||||
rng = random.Random(cfg.seed)
|
||||
|
||||
repo = Repository(cfg.db_path)
|
||||
repo.init_schema()
|
||||
# Escludi prompt_library (PromptLibrary dataclass non e' JSON-serializable);
|
||||
# salva solo i nomi degli stili per reproducibility.
|
||||
config_dict = {
|
||||
**{k: v for k, v in cfg.__dict__.items() if k != "prompt_library"},
|
||||
"db_path": str(cfg.db_path),
|
||||
"model_tier": cfg.model_tier.value,
|
||||
"prompt_library_styles": (
|
||||
list(cfg.prompt_library.cognitive_styles)
|
||||
if cfg.prompt_library is not None else None
|
||||
),
|
||||
}
|
||||
run_id = repo.create_run(name=cfg.run_name, config=config_dict)
|
||||
|
||||
# WFA split: se attivo, GA usa solo train_ohlcv; OOS re-eval su test_ohlcv a fine run.
|
||||
if cfg.wfa_train_split is not None and 0.0 < cfg.wfa_train_split < 1.0:
|
||||
split_idx = int(len(ohlcv) * cfg.wfa_train_split)
|
||||
train_ohlcv = ohlcv.iloc[:split_idx]
|
||||
test_ohlcv = ohlcv.iloc[split_idx:]
|
||||
else:
|
||||
train_ohlcv = ohlcv
|
||||
test_ohlcv = None
|
||||
|
||||
market = build_market_summary(train_ohlcv, symbol=cfg.symbol, timeframe=cfg.timeframe)
|
||||
|
||||
# Propaga la libreria di stili al modulo mutation (cosi' mutate_cognitive_style
|
||||
# pesca dai candidati coerenti col JSON della strategia in corso). Va FATTO
|
||||
# PRIMA di istanziare HypothesisAgent (che la riceve in costruttore).
|
||||
prompt_library = cfg.prompt_library or PromptLibrary.default()
|
||||
set_cognitive_styles(prompt_library.cognitive_styles)
|
||||
|
||||
hypothesis_agent = HypothesisAgent(llm=llm, prompt_library=prompt_library)
|
||||
falsification_agent = FalsificationAgent(
|
||||
fees_bp=cfg.fees_bp, n_trials_dsr=cfg.n_trials_dsr
|
||||
)
|
||||
adversarial_agent = AdversarialAgent(
|
||||
fees_bp=cfg.fees_bp,
|
||||
fees_eat_alpha_threshold=cfg.fees_eat_alpha_threshold,
|
||||
flat_too_long_threshold=cfg.flat_too_long_threshold,
|
||||
undertrading_threshold=cfg.undertrading_threshold,
|
||||
)
|
||||
cost_tracker = CostTracker()
|
||||
|
||||
population = build_initial_population(
|
||||
k=cfg.population_size,
|
||||
model_tier=cfg.model_tier,
|
||||
rng=rng,
|
||||
prompt_library=prompt_library,
|
||||
)
|
||||
fitnesses: dict[str, float] = {}
|
||||
|
||||
ga_cfg = GAConfig(
|
||||
population_size=cfg.population_size,
|
||||
elite_k=cfg.elite_k,
|
||||
tournament_k=cfg.tournament_k,
|
||||
p_crossover=cfg.p_crossover,
|
||||
prompt_mutation_weight=cfg.prompt_mutation_weight,
|
||||
)
|
||||
|
||||
try:
|
||||
for gen in range(cfg.n_generations):
|
||||
# Step 1: raccogli i genomi da valutare in questa generazione (esclude
|
||||
# elite gia' presenti nella cache fitnesses) e lancia propose() in
|
||||
# parallelo. La sezione DB-write resta serial sotto.
|
||||
uncached = [g for g in population if g.id not in fitnesses]
|
||||
proposals = _parallel_propose(
|
||||
hypothesis_agent, uncached, market, cfg.llm_concurrency
|
||||
)
|
||||
proposal_by_id = {g.id: p for g, p in zip(uncached, proposals, strict=True)}
|
||||
|
||||
for genome in population:
|
||||
if genome.id in fitnesses:
|
||||
continue # elite gia' valutata in generazione precedente
|
||||
repo.save_genome(run_id=run_id, generation_idx=gen, genome=genome)
|
||||
proposal = proposal_by_id[genome.id]
|
||||
# Registra costo per OGNI completion (incluse retry).
|
||||
for completion in proposal.completions:
|
||||
cost_record = cost_tracker.record(
|
||||
input_tokens=completion.input_tokens,
|
||||
output_tokens=completion.output_tokens,
|
||||
tier=completion.tier,
|
||||
run_id=run_id,
|
||||
agent_id=genome.id,
|
||||
)
|
||||
repo.save_cost_record(
|
||||
run_id=run_id,
|
||||
agent_id=genome.id,
|
||||
tier=cost_record.tier.value,
|
||||
input_tokens=cost_record.input_tokens,
|
||||
output_tokens=cost_record.output_tokens,
|
||||
cost_usd=cost_record.cost_usd,
|
||||
)
|
||||
|
||||
if proposal.strategy is None:
|
||||
repo.save_evaluation(
|
||||
run_id=run_id,
|
||||
genome_id=genome.id,
|
||||
fitness=0.0,
|
||||
dsr=0.0,
|
||||
dsr_pvalue=1.0,
|
||||
sharpe=0.0,
|
||||
max_dd=0.0,
|
||||
total_return=0.0,
|
||||
n_trades=0,
|
||||
parse_error=proposal.parse_error,
|
||||
raw_text=proposal.raw_text,
|
||||
)
|
||||
fitnesses[genome.id] = 0.0
|
||||
continue
|
||||
|
||||
fals = falsification_agent.evaluate(proposal.strategy, train_ohlcv)
|
||||
adv = adversarial_agent.review(proposal.strategy, train_ohlcv)
|
||||
for finding in adv.findings:
|
||||
repo.save_adversarial_finding(
|
||||
run_id=run_id,
|
||||
genome_id=genome.id,
|
||||
name=finding.name,
|
||||
severity=finding.severity.value,
|
||||
detail=finding.detail,
|
||||
)
|
||||
fit = compute_fitness(
|
||||
fals, adv,
|
||||
hard_kill_findings=cfg.fitness_hard_kill_findings,
|
||||
adversarial_soft_penalty=cfg.fitness_adversarial_soft_penalty,
|
||||
)
|
||||
# Multi-objective: se attivo, eval OOS subito e combina via alpha.
|
||||
if (
|
||||
cfg.eval_oos_during_loop
|
||||
and test_ohlcv is not None
|
||||
and len(test_ohlcv) >= 100
|
||||
and fit > 0
|
||||
):
|
||||
try:
|
||||
fals_oos_inloop = falsification_agent.evaluate(
|
||||
proposal.strategy, test_ohlcv
|
||||
)
|
||||
adv_oos_inloop = adversarial_agent.review(
|
||||
proposal.strategy, test_ohlcv
|
||||
)
|
||||
fit_oos_inloop = compute_fitness(
|
||||
fals_oos_inloop, adv_oos_inloop,
|
||||
hard_kill_findings=cfg.fitness_hard_kill_findings,
|
||||
adversarial_soft_penalty=cfg.fitness_adversarial_soft_penalty,
|
||||
)
|
||||
fit = (
|
||||
cfg.fitness_combined_alpha * fit
|
||||
+ (1.0 - cfg.fitness_combined_alpha) * fit_oos_inloop
|
||||
)
|
||||
except Exception:
|
||||
pass # fallback: usa solo IS
|
||||
repo.save_evaluation(
|
||||
run_id=run_id,
|
||||
genome_id=genome.id,
|
||||
fitness=fit,
|
||||
dsr=fals.dsr,
|
||||
dsr_pvalue=fals.dsr_pvalue,
|
||||
sharpe=fals.sharpe,
|
||||
max_dd=fals.max_drawdown,
|
||||
total_return=fals.total_return,
|
||||
n_trades=fals.n_trades,
|
||||
parse_error=None,
|
||||
raw_text=proposal.raw_text,
|
||||
)
|
||||
fitnesses[genome.id] = fit
|
||||
|
||||
gen_fitnesses = [fitnesses[g.id] for g in population]
|
||||
summary = generation_summary(gen_fitnesses, n_bins=10)
|
||||
repo.save_generation_summary(
|
||||
run_id=run_id,
|
||||
generation_idx=gen,
|
||||
n_genomes=len(population),
|
||||
fitness_median=summary["median"],
|
||||
fitness_max=summary["max"],
|
||||
fitness_p90=summary["p90"],
|
||||
entropy=summary["entropy"],
|
||||
)
|
||||
|
||||
if gen < cfg.n_generations - 1:
|
||||
population = next_generation(
|
||||
population, fitnesses, ga_cfg, rng,
|
||||
llm=llm if cfg.prompt_mutation_weight > 0 else None,
|
||||
cost_tracker=cost_tracker if cfg.prompt_mutation_weight > 0 else None,
|
||||
repo=repo if cfg.prompt_mutation_weight > 0 else None,
|
||||
run_id=run_id if cfg.prompt_mutation_weight > 0 else None,
|
||||
)
|
||||
|
||||
# WFA re-eval: i top_k genomi (by fitness in-sample > 0) vengono rivalutati
|
||||
# sul test_ohlcv. Le metriche OOS finiscono in evaluations.fitness_oos etc.
|
||||
if test_ohlcv is not None and len(test_ohlcv) >= 100:
|
||||
from ..agents.hypothesis import _try_parse
|
||||
|
||||
all_evals = repo.list_evaluations(run_id)
|
||||
top_evals = sorted(
|
||||
(e for e in all_evals if e["fitness"] > 0 and not e.get("parse_error")),
|
||||
key=lambda x: x["fitness"],
|
||||
reverse=True,
|
||||
)[: cfg.wfa_top_k]
|
||||
for ev in top_evals:
|
||||
strategy, parse_err = _try_parse(ev["raw_text"] or "")
|
||||
if strategy is None:
|
||||
continue
|
||||
try:
|
||||
fals_oos = falsification_agent.evaluate(strategy, test_ohlcv)
|
||||
adv_oos = adversarial_agent.review(strategy, test_ohlcv)
|
||||
except Exception:
|
||||
continue
|
||||
fit_oos = compute_fitness(
|
||||
fals_oos, adv_oos,
|
||||
hard_kill_findings=cfg.fitness_hard_kill_findings,
|
||||
adversarial_soft_penalty=cfg.fitness_adversarial_soft_penalty,
|
||||
)
|
||||
repo.update_evaluation_oos(
|
||||
run_id=run_id,
|
||||
genome_id=ev["genome_id"],
|
||||
fitness_oos=fit_oos,
|
||||
sharpe_oos=float(fals_oos.sharpe),
|
||||
return_oos=float(fals_oos.total_return),
|
||||
max_dd_oos=float(fals_oos.max_drawdown),
|
||||
n_trades_oos=int(fals_oos.n_trades),
|
||||
)
|
||||
|
||||
repo.complete_run(
|
||||
run_id, total_cost=repo.total_cost(run_id), status="completed"
|
||||
)
|
||||
return run_id
|
||||
except Exception:
|
||||
repo.complete_run(
|
||||
run_id, total_cost=repo.total_cost(run_id), status="failed"
|
||||
)
|
||||
raise
|
||||
+47
-2
@@ -26,6 +26,26 @@ class Repository:
|
||||
self.db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with self._conn() as conn:
|
||||
conn.executescript(SCHEMA_SQL)
|
||||
# Migration soft per DB pre-Task 6: aggiunge call_kind se manca.
|
||||
try:
|
||||
conn.execute(
|
||||
"ALTER TABLE cost_records ADD COLUMN call_kind "
|
||||
"TEXT NOT NULL DEFAULT 'hypothesis'"
|
||||
)
|
||||
except sqlite3.OperationalError:
|
||||
pass # colonna già presente
|
||||
# Migration WFA: colonne fitness_oos e altre OOS su evaluations.
|
||||
for col_def in (
|
||||
"fitness_oos REAL",
|
||||
"sharpe_oos REAL",
|
||||
"return_oos REAL",
|
||||
"max_dd_oos REAL",
|
||||
"n_trades_oos INTEGER",
|
||||
):
|
||||
try:
|
||||
conn.execute(f"ALTER TABLE evaluations ADD COLUMN {col_def}")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def _now() -> str:
|
||||
@@ -167,6 +187,29 @@ class Repository:
|
||||
),
|
||||
)
|
||||
|
||||
def update_evaluation_oos(
|
||||
self,
|
||||
run_id: str,
|
||||
genome_id: str,
|
||||
fitness_oos: float,
|
||||
sharpe_oos: float,
|
||||
return_oos: float,
|
||||
max_dd_oos: float,
|
||||
n_trades_oos: int,
|
||||
) -> None:
|
||||
"""Aggiorna le metriche OOS per un genome (WFA re-eval)."""
|
||||
with self._conn() as conn:
|
||||
conn.execute(
|
||||
"""UPDATE evaluations SET
|
||||
fitness_oos=?, sharpe_oos=?, return_oos=?,
|
||||
max_dd_oos=?, n_trades_oos=?
|
||||
WHERE run_id=? AND genome_id=?""",
|
||||
(
|
||||
fitness_oos, sharpe_oos, return_oos,
|
||||
max_dd_oos, n_trades_oos, run_id, genome_id,
|
||||
),
|
||||
)
|
||||
|
||||
def list_evaluations(self, run_id: str) -> list[dict[str, Any]]:
|
||||
with self._conn() as conn:
|
||||
rows = conn.execute(
|
||||
@@ -184,12 +227,13 @@ class Repository:
|
||||
input_tokens: int,
|
||||
output_tokens: int,
|
||||
cost_usd: float,
|
||||
call_kind: str = "hypothesis",
|
||||
) -> None:
|
||||
with self._conn() as conn:
|
||||
conn.execute(
|
||||
"""INSERT INTO cost_records
|
||||
(run_id, agent_id, ts, tier, input_tokens, output_tokens, cost_usd)
|
||||
VALUES (?,?,?,?,?,?,?)""",
|
||||
(run_id, agent_id, ts, tier, input_tokens, output_tokens, cost_usd, call_kind)
|
||||
VALUES (?,?,?,?,?,?,?,?)""",
|
||||
(
|
||||
run_id,
|
||||
agent_id,
|
||||
@@ -198,6 +242,7 @@ class Repository:
|
||||
input_tokens,
|
||||
output_tokens,
|
||||
cost_usd,
|
||||
call_kind,
|
||||
),
|
||||
)
|
||||
|
||||
+6
@@ -45,6 +45,11 @@ CREATE TABLE IF NOT EXISTS evaluations (
|
||||
parse_error TEXT,
|
||||
raw_text TEXT,
|
||||
eval_ts TEXT NOT NULL,
|
||||
fitness_oos REAL,
|
||||
sharpe_oos REAL,
|
||||
return_oos REAL,
|
||||
max_dd_oos REAL,
|
||||
n_trades_oos INTEGER,
|
||||
PRIMARY KEY (run_id, genome_id),
|
||||
FOREIGN KEY (run_id) REFERENCES runs(id)
|
||||
);
|
||||
@@ -58,6 +63,7 @@ CREATE TABLE IF NOT EXISTS cost_records (
|
||||
input_tokens INTEGER NOT NULL,
|
||||
output_tokens INTEGER NOT NULL,
|
||||
cost_usd REAL NOT NULL,
|
||||
call_kind TEXT NOT NULL DEFAULT 'hypothesis',
|
||||
FOREIGN KEY (run_id) REFERENCES runs(id)
|
||||
);
|
||||
|
||||
+68
@@ -25,6 +25,12 @@ from typing import Any
|
||||
import numpy as np
|
||||
import pandas as pd # type: ignore[import-untyped]
|
||||
|
||||
from strategy_pythagoras.indicators import (
|
||||
candle_pattern as _public_candle_pattern,
|
||||
fractal_mirror as _public_fractal_mirror,
|
||||
pythagorean_ratio as _public_pythagorean_ratio,
|
||||
)
|
||||
|
||||
from ..backtest.orders import Side
|
||||
from .parser import (
|
||||
FeatureNode,
|
||||
@@ -80,6 +86,37 @@ def _ind_atr(df: pd.DataFrame, length: float) -> pd.Series:
|
||||
return _atr(df, int(length))
|
||||
|
||||
|
||||
def _ind_atr_pct(df: pd.DataFrame, length: float) -> pd.Series:
|
||||
# ATR normalizzato come frazione del prezzo close.
|
||||
# Convenzione: confronti con literal numerici tipo `atr_pct > 0.02` significano
|
||||
# "ATR > 2% del prezzo". Risolve il bug protocol_unit_bug dove `atr > 0.02`
|
||||
# su asset crypto (close ~3000 USDT, atr ~30) e' sempre TRUE -> dead branch.
|
||||
return _atr(df, int(length)) / df["close"]
|
||||
|
||||
|
||||
def _ind_sma_pct(df: pd.DataFrame, length: float) -> pd.Series:
|
||||
# Deviazione frazionale del close dalla SMA: (close - sma) / sma.
|
||||
# Range tipico +/- 0.1 (close +/- 10% dalla media). Uso ideale:
|
||||
# sma_pct(50) > 0.05 -> "close 5% sopra la media a 50 barre"
|
||||
# NB: non e' "sma/close" perche' quel valore (sempre ~1.0) e' inutile
|
||||
# per confronti con literal frazionali.
|
||||
sma = _sma(df["close"], int(length))
|
||||
return (df["close"] - sma) / sma
|
||||
|
||||
|
||||
def _ind_macd_pct(
|
||||
df: pd.DataFrame,
|
||||
fast: float = 12,
|
||||
slow: float = 26,
|
||||
signal: float = 9,
|
||||
) -> pd.Series:
|
||||
# MACD normalizzato come frazione del prezzo close: macd_value / close.
|
||||
# Range tipico +/- 0.02. Uso: `macd_pct > 0` (momentum positivo) o
|
||||
# `macd_pct > 0.005` (momentum positivo >= 0.5% del prezzo).
|
||||
macd = _ind_macd(df, fast, slow, signal)
|
||||
return macd / df["close"]
|
||||
|
||||
|
||||
def _ind_realized_vol(df: pd.DataFrame, window: float) -> pd.Series:
|
||||
return _realized_vol(df["close"], int(window))
|
||||
|
||||
@@ -95,16 +132,45 @@ def _ind_macd(
|
||||
return macd_line - signal_line
|
||||
|
||||
|
||||
def _ind_candle_pattern(df: pd.DataFrame, *params: float) -> pd.Series:
|
||||
# Adapter: il dispatch in _eval_node fa ``fn(df, *node.params)``, ma la
|
||||
# public API in strategy_pythagoras.indicators accetta ``params: list[float]``
|
||||
# come singolo argomento. Re-pack qui per mantenere indicators.py testabile
|
||||
# in isolamento.
|
||||
return _public_candle_pattern(df, list(params))
|
||||
|
||||
|
||||
def _ind_pythagorean_ratio(df: pd.DataFrame, lookback: float) -> pd.Series:
|
||||
return _public_pythagorean_ratio(df, [lookback])
|
||||
|
||||
|
||||
def _ind_fractal_mirror(df: pd.DataFrame, k: float, axis_int: float) -> pd.Series:
|
||||
return _public_fractal_mirror(df, [k, axis_int])
|
||||
|
||||
|
||||
# Annotated as ``dict[str, Any]`` deliberately: each indicator has its own
|
||||
# arity and parameter names, so a single ``Callable`` signature would be a
|
||||
# lie. Dispatch happens in :func:`_eval_node`, which validates the verb name
|
||||
# against this map.
|
||||
INDICATOR_FNS: dict[str, Any] = {
|
||||
"sma": _ind_sma,
|
||||
"sma_pct": _ind_sma_pct,
|
||||
"rsi": _ind_rsi,
|
||||
"atr": _ind_atr,
|
||||
"atr_pct": _ind_atr_pct,
|
||||
"realized_vol": _ind_realized_vol,
|
||||
"macd": _ind_macd,
|
||||
"macd_pct": _ind_macd_pct,
|
||||
"candle_pattern": _ind_candle_pattern,
|
||||
"pythagorean_ratio": _ind_pythagorean_ratio,
|
||||
"fractal_mirror": _ind_fractal_mirror,
|
||||
}
|
||||
|
||||
_TIME_FEATURE_FNS: dict[str, Callable[[pd.DatetimeIndex], pd.Series]] = {
|
||||
"hour": lambda idx: pd.Series(idx.hour, index=idx, dtype="int64"),
|
||||
"dow": lambda idx: pd.Series(idx.dayofweek, index=idx, dtype="int64"),
|
||||
"is_weekend": lambda idx: pd.Series((idx.dayofweek >= 5).astype("int64"), index=idx),
|
||||
"minute_of_hour": lambda idx: pd.Series(idx.minute, index=idx, dtype="int64"),
|
||||
}
|
||||
|
||||
|
||||
@@ -134,6 +200,8 @@ def _eval_bool_arg(node: Node, df: pd.DataFrame) -> pd.Series:
|
||||
|
||||
def _eval_node(node: Node, df: pd.DataFrame) -> pd.Series:
|
||||
if isinstance(node, FeatureNode):
|
||||
if node.name in _TIME_FEATURE_FNS:
|
||||
return _TIME_FEATURE_FNS[node.name](df.index)
|
||||
return df[node.name]
|
||||
|
||||
if isinstance(node, IndicatorNode):
|
||||
+4
-2
@@ -17,10 +17,12 @@ ACTION_VALUES: frozenset[str] = frozenset(
|
||||
KIND_VALUES: frozenset[str] = frozenset({"indicator", "feature", "literal"})
|
||||
|
||||
KNOWN_INDICATORS: frozenset[str] = frozenset(
|
||||
{"sma", "rsi", "atr", "macd", "realized_vol"}
|
||||
{"sma", "sma_pct", "rsi", "atr", "atr_pct", "macd", "macd_pct", "realized_vol",
|
||||
"candle_pattern", "pythagorean_ratio", "fractal_mirror"}
|
||||
)
|
||||
KNOWN_FEATURES: frozenset[str] = frozenset(
|
||||
{"open", "high", "low", "close", "volume"}
|
||||
{"open", "high", "low", "close", "volume",
|
||||
"hour", "dow", "is_weekend", "minute_of_hour"}
|
||||
)
|
||||
|
||||
# Convenience union (utile a validator / parser).
|
||||
+43
-2
@@ -31,11 +31,18 @@ from .parser import (
|
||||
# Numero di parametri numerici accettati dopo il nome dell'indicatore.
|
||||
# (min, max) sui soli numeri. Indicatori non sono annidabili in Phase 1.
|
||||
INDICATOR_ARITY: dict[str, tuple[int, int]] = {
|
||||
"sma": (1, 1), # length
|
||||
"sma": (1, 1), # length (assoluto, unita' prezzo)
|
||||
"sma_pct": (1, 1), # length: (close - sma)/sma, deviazione frazionale
|
||||
"rsi": (1, 1), # length
|
||||
"atr": (1, 1), # length
|
||||
"atr": (1, 1), # length (assoluto, unita' prezzo)
|
||||
"atr_pct": (1, 1), # length (frazione del close, per confronti con literal)
|
||||
"realized_vol": (1, 1), # window
|
||||
"macd": (0, 3), # fast, slow, signal (tutti opzionali)
|
||||
"macd_pct": (0, 3), # macd/close, frazionale (per confronti con literal)
|
||||
# Pythagoras indicators (params encoded as floats)
|
||||
"candle_pattern": (4, 13), # [length, sym0, ..., sym_{length-1}]
|
||||
"pythagorean_ratio": (1, 1), # lookback in [12,200]
|
||||
"fractal_mirror": (2, 2), # k in [3,12], axis_int in {0=h,1=v}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,3 +114,37 @@ def _validate_indicator(node: IndicatorNode) -> None:
|
||||
raise ValidationError(
|
||||
f"indicator '{node.name}' arity {n_params} out of [{min_p},{max_p}]"
|
||||
)
|
||||
|
||||
# Pythagoras-specific param semantics
|
||||
name = node.name
|
||||
if name == "candle_pattern":
|
||||
length = int(node.params[0])
|
||||
if not (3 <= length <= 12):
|
||||
raise ValidationError(
|
||||
f"candle_pattern length must be in [3,12], got {length}"
|
||||
)
|
||||
if n_params != 1 + length:
|
||||
raise ValidationError(
|
||||
f"candle_pattern: expected 1+length={1 + length} params, got {n_params}"
|
||||
)
|
||||
for i, sym in enumerate(node.params[1:], start=1):
|
||||
sym_int = int(sym)
|
||||
if sym_int not in (0, 1, 2):
|
||||
raise ValidationError(
|
||||
f"candle_pattern sym[{i - 1}] must be 0(U)/1(D)/2(doji), got {sym}"
|
||||
)
|
||||
elif name == "pythagorean_ratio":
|
||||
lookback = int(node.params[0])
|
||||
if not (12 <= lookback <= 200):
|
||||
raise ValidationError(
|
||||
f"pythagorean_ratio lookback in [12,200], got {lookback}"
|
||||
)
|
||||
elif name == "fractal_mirror":
|
||||
k = int(node.params[0])
|
||||
if not (3 <= k <= 12):
|
||||
raise ValidationError(f"fractal_mirror k must be in [3,12], got {k}")
|
||||
axis_int = int(node.params[1])
|
||||
if axis_int not in (0, 1):
|
||||
raise ValidationError(
|
||||
f"fractal_mirror axis must be 0(h)/1(v), got {axis_int}"
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
[project]
|
||||
name = "multi-swarm-core"
|
||||
version = "0.1.0"
|
||||
description = "Multi-Swarm Coevolutive core: GA, genome, protocol, backtest, cerbero, data, llm, persistence"
|
||||
authors = [{ name = "Adriano Dal Pastro", email = "adrianodalpastro@tielogic.com" }]
|
||||
requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
"pandas>=2.2",
|
||||
"numpy>=2.1",
|
||||
"scipy>=1.14",
|
||||
"pydantic>=2.9",
|
||||
"pydantic-settings>=2.6",
|
||||
"sqlmodel>=0.0.22",
|
||||
"openai>=1.55",
|
||||
"httpx>=0.28",
|
||||
"requests>=2.32",
|
||||
"tenacity>=9.0",
|
||||
"pyyaml>=6.0",
|
||||
"pyarrow>=18.0",
|
||||
"yfinance>=1.3.0",
|
||||
"nicegui>=3.11.1",
|
||||
"plotly>=5.24",
|
||||
]
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
+43
-4
@@ -5,10 +5,10 @@ import numpy as np
|
||||
import pandas as pd
|
||||
import pytest
|
||||
|
||||
from multi_swarm.genome.hypothesis import ModelTier
|
||||
from multi_swarm.llm.client import CompletionResult
|
||||
from multi_swarm.orchestrator.run import RunConfig, run_phase1
|
||||
from multi_swarm.persistence.repository import Repository
|
||||
from multi_swarm_core.genome.hypothesis import ModelTier
|
||||
from multi_swarm_core.llm.client import CompletionResult
|
||||
from multi_swarm_core.orchestrator.run import RunConfig, run_phase1
|
||||
from multi_swarm_core.persistence.repository import Repository
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -100,3 +100,42 @@ def test_e2e_minimal_run_completes(
|
||||
assert len(gens) == 2
|
||||
evals = repo.list_evaluations(run_id)
|
||||
assert len(evals) >= 5 # almeno una popolazione
|
||||
|
||||
|
||||
def test_e2e_wfa_populates_fitness_oos(
|
||||
tmp_path: Path,
|
||||
synthetic_ohlcv,
|
||||
fake_llm,
|
||||
mocker,
|
||||
):
|
||||
"""WFA: train_split=0.7 → top genomi devono avere fitness_oos popolato.
|
||||
|
||||
Usa fitness v2 con hard-kill minimale (solo no_trades): il fixture sintetico
|
||||
non produce strategie profittevoli, quindi i check aggressivi
|
||||
fees_eat_alpha/negative_net_pnl azzererebbero tutti i genomi rendendo
|
||||
inverificabile il wiring WFA.
|
||||
"""
|
||||
cfg = RunConfig(
|
||||
run_name="e2e-wfa-test",
|
||||
population_size=5,
|
||||
n_generations=2,
|
||||
elite_k=1,
|
||||
tournament_k=2,
|
||||
p_crossover=0.5,
|
||||
seed=42,
|
||||
model_tier=ModelTier.C,
|
||||
symbol="BTC/USDT",
|
||||
timeframe="1h",
|
||||
fees_bp=5.0,
|
||||
n_trials_dsr=10,
|
||||
db_path=tmp_path / "runs.db",
|
||||
wfa_train_split=0.7,
|
||||
wfa_top_k=3,
|
||||
fitness_hard_kill_findings=("no_trades",),
|
||||
)
|
||||
run_id = run_phase1(cfg, ohlcv=synthetic_ohlcv, llm=fake_llm)
|
||||
repo = Repository(db_path=tmp_path / "runs.db")
|
||||
evals = repo.list_evaluations(run_id)
|
||||
# Almeno 1 genome con fitness > 0 deve avere fitness_oos popolato.
|
||||
oos_evals = [e for e in evals if e.get("fitness_oos") is not None]
|
||||
assert len(oos_evals) >= 1, f"Nessun OOS popolato; evals={evals}"
|
||||
@@ -0,0 +1,104 @@
|
||||
"""Integration test Phase 2.5: GA loop con LLM mutator attivo.
|
||||
|
||||
Verifica che ``next_generation`` con ``prompt_mutation_weight > 0`` e ``llm``
|
||||
fornito produca figli con system_prompt mutato dall'LLM (e non solo scalari).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from dataclasses import dataclass
|
||||
|
||||
from multi_swarm_core.ga.loop import GAConfig, next_generation
|
||||
from multi_swarm_core.genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
|
||||
_PROMPT_TEMPLATES = (
|
||||
"Strategia mean-reversion 1h. Entry long RSI(14) < 30 e close > SMA(50). Stop 2%.",
|
||||
"Strategia momentum breakout. Entry long close > SMA(20) e ATR(14) crescente.",
|
||||
"Strategia trend-following 4h. Long SMA(20) > SMA(50). Short opposito.",
|
||||
)
|
||||
|
||||
|
||||
def _make_pop(n: int) -> list[HypothesisAgentGenome]:
|
||||
return [
|
||||
HypothesisAgentGenome(
|
||||
system_prompt=_PROMPT_TEMPLATES[i % len(_PROMPT_TEMPLATES)],
|
||||
feature_access=["close", "high"],
|
||||
temperature=0.9 + 0.01 * i,
|
||||
top_p=0.95,
|
||||
model_tier=ModelTier.C,
|
||||
lookback_window=200,
|
||||
cognitive_style="physicist",
|
||||
)
|
||||
for i in range(n)
|
||||
]
|
||||
|
||||
|
||||
@dataclass
|
||||
class _Result:
|
||||
text: str
|
||||
|
||||
|
||||
class _MutatorLLM:
|
||||
"""Mock che produce un prompt diverso (e valido) a ogni call."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.calls = 0
|
||||
|
||||
def complete(self, genome, system, user, max_tokens: int = 2000) -> _Result:
|
||||
self.calls += 1
|
||||
# Prompt sempre diverso per garantire validation pass.
|
||||
return _Result(
|
||||
text=(
|
||||
f"<prompt>Strategia evolved #{self.calls}. Entry long quando "
|
||||
f"RSI(14) < {25 + self.calls % 10} e close > SMA({40 + self.calls}). "
|
||||
f"Exit short quando momentum decade. Trade rule {self.calls}.</prompt>"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_loop_with_prompt_mutator_produces_prompt_diversity() -> None:
|
||||
"""Con weight 1.0 + crossover 0 (solo mutation), tutti i child non-elite
|
||||
devono avere system_prompt diverso dai parent (LLM-mutated)."""
|
||||
rng = random.Random(0)
|
||||
pop = _make_pop(5)
|
||||
fitnesses = {g.id: 0.0 for g in pop}
|
||||
cfg = GAConfig(
|
||||
population_size=5,
|
||||
elite_k=1,
|
||||
tournament_k=2,
|
||||
p_crossover=0.0, # nessun crossover → tutta la non-elite è mutation
|
||||
prompt_mutation_weight=1.0,
|
||||
)
|
||||
llm = _MutatorLLM()
|
||||
|
||||
new_pop = next_generation(pop, fitnesses, cfg, rng, llm=llm)
|
||||
|
||||
assert len(new_pop) == 5
|
||||
# 4 non-elite figli, tutti con prompt evoluti.
|
||||
parent_prompts = {g.system_prompt for g in pop}
|
||||
evolved = [g for g in new_pop[1:] if g.system_prompt not in parent_prompts]
|
||||
assert len(evolved) >= 3, f"Solo {len(evolved)} figli con prompt mutato"
|
||||
assert llm.calls >= 4
|
||||
|
||||
|
||||
def test_loop_backward_compat_no_llm_no_prompt_mutation() -> None:
|
||||
"""Default weight=0.0 + llm=None → comportamento identico a Phase 2."""
|
||||
rng = random.Random(0)
|
||||
pop = _make_pop(5)
|
||||
fitnesses = {g.id: float(i) for i, g in enumerate(pop)}
|
||||
cfg = GAConfig(
|
||||
population_size=5,
|
||||
elite_k=1,
|
||||
tournament_k=2,
|
||||
p_crossover=0.0,
|
||||
prompt_mutation_weight=0.0,
|
||||
)
|
||||
|
||||
new_pop = next_generation(pop, fitnesses, cfg, rng, llm=None)
|
||||
|
||||
assert len(new_pop) == 5
|
||||
# Nessun child con prompt diverso dai parent: solo mutazioni scalari.
|
||||
parent_prompts = {g.system_prompt for g in pop}
|
||||
for child in new_pop:
|
||||
assert child.system_prompt in parent_prompts
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user