Skip to content

0xSanei/darwinia

Repository files navigation

🧬 Darwinia

Give your AI agent the power to evolve its own trading strategies.

Trading agents that discover strategies through Darwinian selection and adversarial self-play — not human-written rules.

Tests License Python OpenClaw Skill Claude Code 17 Genes 6 Attack Types 215 Tests Open in Colab

Darwinia Demo


For OpenClaw / Claude Code agents

Tell your agent:

"Install Darwinia and evolve a BTC trading strategy."

Darwinia ships as an OpenClaw skill and Claude Code skill. Your agent can install it, run evolution, and report results — all through natural language.

Manual skill installation

OpenClaw:

mkdir -p ~/.openclaw/skills/darwinia
cp .openclaw/SKILL.md ~/.openclaw/skills/darwinia/SKILL.md

Claude Code: Auto-detected from .claude/SKILL.md in project root.


The problem

You hand-code RSI > 70 = sell. Market regime changes. Strategy dies. You tweak parameters manually. Repeat forever.

The Darwinia approach

50 agents with random DNA compete on real BTC data. The weak die. The strong breed. After 50 generations, survivors handle rug pulls, fake breakouts, and whipsaws — not because you told them to, but because agents that couldn't handle these attacks didn't survive to reproduce.

Before:  Human writes rules → Agent executes → Market changes → Strategy dies
After:   Human sets environment → Agents evolve → Survivors adapt → Patterns emerge

Quick Start

git clone https://github.com/0xSanei/darwinia.git
cd darwinia
pip install -e ".[dev]"

# Evolve trading strategies (30 seconds)
python -m darwinia evolve -g 10

# Test champion against adversarial attacks
python -m darwinia arena

# Launch interactive dashboard
python -m darwinia dashboard

No API keys. No cloud. Just Python + numpy. BTC data included.

Demo Output (click to expand)

Evolution run:

🧬 Darwinia — Evolution Engine
   Generations: 20 | Population: 30 | Data: BTC/USDT 1h (10,946 candles)

   Gen   0 | ██████░░░░░░░░░░░░░░ | champ=0.32 avg=0.04 div=1.56
   Gen   1 | █████████████░░░░░░░ | champ=0.69 avg=0.24 div=1.60
   Gen   2 | ███████████████████████████ | champ=1.35 avg=0.64 div=1.32
   Gen   5 | █████████████████████████████ | champ=1.46 avg=0.43 div=0.81
   Gen  12 | █████████████████████ | champ=1.09 avg=0.82 div=0.72
   Gen  13 | ████████████████████████ | champ=1.24 avg=0.64 div=0.57
   Gen  19 | ████████░░░░░░░░░░░░ | champ=0.42 avg=0.12 div=0.44

✅ Evolution complete! 16 patterns discovered.

Adversarial arena test:

⚔️ Darwinia — Adversarial Arena
   Testing evolved champion against targeted attacks...

   whipsaw              | PnL: +0.00% | ✅ survived
   fake_breakout        | PnL: +0.00% | ✅ survived
   pump_and_dump        | PnL: +0.00% | ✅ survived
   rug_pull             | PnL: -3.21% | ✅ survived
   slow_bleed           | PnL: -1.05% | ✅ survived

   Survival rate: 100.0%

How It Works

   Genesis Pool          Adversarial Arena         Pattern Discovery
   ┌──────────┐         ┌───────────────┐         ┌────────────────┐
   │ 50 agents │───────▶│ Alpha agent   │───────▶│ Analyze WHY     │
   │ with random│        │    vs         │        │ survivors won   │
   │ DNA + seeds│        │ Adversary     │        │                 │
   └──────────┘         │ (6 attacks)   │        │ → Discover new  │
                        └───────┬───────┘        │   market patterns│
                                │                └────────┬────────┘
                    ┌───────────▼───────────┐             │
                    │   NATURAL SELECTION    │◀────────────┘
                    │                       │
                    │ Top 20% survive       │
                    │ Crossover + Mutation   │
                    │ → Next Generation     │
                    └───────────────────────┘

Each generation: CompeteSurvive attacksSelectBreedMutateDiscover

17-Gene DNA

Every trading decision lives in a genome:

Category Genes Controls
Signal 5 What matters — momentum, volume, volatility, mean reversion, trend
Threshold 4 When to act — entry/exit triggers, stop loss, take profit
Personality 5 How to behave — risk appetite, time horizon, contrarian bias, patience, sizing
Adaptation 3 How to learn — regime sensitivity, memory length, noise filtering

Adversarial Arena

Most bots are tested against history. Darwinia agents are tested against an adversary trying to destroy them.

Attack What It Does Who It Traps
Rug Pull Steady rise → sudden 90% crash Trend followers without stops
Fake Breakout Breaks resistance → immediate reversal Breakout traders
Slow Bleed Gradual decline + misleading bounces Patient agents with loose stops
Whipsaw Rapid alternating moves Tight-stop agents
Volume Mirage Volume spike, no follow-through Volume-dependent strategies
Pump & Dump Rapid pump → rapid dump Momentum chasers

The adversary reads the agent's DNA and picks attacks targeting its weaknesses. Over generations, this creates an arms race.

Pattern Discovery

After each generation, Darwinia finds what survivors agree on:

  • Gene convergence: All survivors evolved high noise filtering? That's a pattern.
  • Linked genes: High risk appetite always paired with short time horizon? That's a combo.
  • Human mapping: Evolved "high contrarian_bias" = mean reversion strategy.
  • Novel patterns: Gene combos with no known human equivalent = new discovery.

Results

50-generation evolution on 10,946 BTC/USDT 1h candles:

Metric Gen 0 Gen 50
Champion Fitness ~0.15 ~0.75+
Attack Survival ~30% 98-100%
Strategy Species 1 (random) 3-4 distinct
Patterns Found 0 10-20

Agent Integration

All commands support --json for machine-readable output:

python -m darwinia evolve -g 50 --json
{
  "champion": {
    "id": "a3f2c1d8",
    "fitness": 1.35,
    "genes": {"weight_trend": 0.87, "stop_loss_pct": 0.04, "risk_appetite": 0.35}
  },
  "evolution_summary": {
    "generations_run": 50,
    "patterns_discovered": 16
  }
}
Platform Integration
OpenClaw .openclaw/SKILL.mdintegration guide
Claude Code .claude/SKILL.md — auto-detected in project
Any CLI agent python -m darwinia evolve --json

CLI Reference

python -m darwinia evolve -g 100 -p 80        # 100 gens, 80 agents
python -m darwinia evolve -d my_data.csv       # Custom data
python -m darwinia evolve --json               # JSON output for agents
python -m darwinia arena -c output/champions/champion_gen_0049.json
python -m darwinia arena -r 10 --json          # 10 rounds, JSON output
python -m darwinia validate -w 3 -g 20         # Walk-forward overfitting check
python -m darwinia explain -c champion.json    # Gene ablation analysis
python -m darwinia evolve --multi               # Multi-asset evolution (BTC+ETH+SOL)
python -m darwinia evolve --macro              # Macro-aware evolution (regime overlay)
python -m darwinia fetch ETHUSDT -i 4h -d 90   # Fetch live data from Binance
python -m darwinia fetch BTCUSD --source coingecko  # Fetch from CoinGecko
python -m darwinia scan                        # Discover trending crypto assets
python -m darwinia scan --volatile             # Most volatile assets
python -m darwinia scan --recommend            # Recommended pairs for evolution
python -m darwinia analytics -g 20 -p 50       # Population convergence & clustering
python -m darwinia tournament -n 8 -g 30       # Champion round-robin leaderboard
python -m darwinia repair -c champion.json     # Auto-detect and fix strategy degradation
python -m darwinia dashboard                   # Web UI
python -m darwinia info --json                 # System info as JSON

Dashboard

Four interactive views built with Streamlit:

Evolution Family Tree
Evolution Family Tree
Fitness metrics + population stats Champion ancestry treemap
Arms Race Discoveries
Arms Race Discoveries
Fitness under adversarial pressure Emergent patterns table

Architecture

darwinia/
├── core/          # DNA, agent, market environment
├── evolution/     # Population, fitness, selection, breeding
├── arena/         # Adversarial attacks and combat
├── discovery/     # Pattern analysis and naming
├── chronicle/     # History recording and species tracking
├── personality/   # Personality profiling + market regime detection
├── knowledge/     # Layer 3: Pattern marketplace and knowledge exchange
├── data/          # Live data fetching (Binance, CoinGecko)
├── macro/         # Macro regime simulation and regime-aware fitness
├── integrations/  # Skill composability layer (SkillBridge, SkillRegistry)
├── analytics/     # Population statistics, clustering, diversity metrics
├── repair/        # Self-repair: health monitoring + auto-fix degraded strategies
└── __main__.py    # CLI entry point (22 commands)

dashboard/         # Streamlit visualization (4 pages)
scripts/           # Competitor monitoring, utilities

Validation & Explainability

Walk-Forward Validation splits data chronologically (train on past, test on future). If fitness degrades out-of-sample, the strategy is overfitting.

python -m darwinia validate -w 3 --json

Gene Ablation zeroes each gene one at a time and measures fitness drop. Genes that cause the biggest drop are the most important — proving the agent found real signal, not noise.

python -m darwinia explain -c champion.json --json

Macro-Aware Evolution

Standard backtests ignore macro conditions. With --macro, Darwinia generates a regime overlay (risk-on, risk-off, transition) and rewards agents that adapt position sizing to macro state. Agents that go full-size during risk-off get penalized. Agents that scale down survive.

python -m darwinia evolve --macro --json

Asset Auto-Discovery

Instead of hardcoding which asset to train on, scan finds high-volume volatile assets — the best training data for genetic evolution.

python -m darwinia scan --recommend --json

Self-Repair

Strategies degrade as markets shift. repair monitors fitness drift and applies targeted fixes — re-randomizing only the weakest genes instead of re-evolving from scratch.

python -m darwinia repair -c champion.json --method targeted --json

Three repair modes: targeted (fix weak genes via ablation), full (re-evolve from seed), ensemble (mutate 3 variants, pick best).

Backtesting & Strategy Export

Full performance analysis with quantitative finance metrics:

# Single-pass backtest with Sharpe, Sortino, Calmar, max drawdown
python -m darwinia backtest -c champion.json -d data/btc_1h.csv

# Walk-forward backtest (5 rolling windows)
python -m darwinia backtest -c champion.json --walk-forward -w 5

# Multi-asset generalization test
python -m darwinia backtest -c champion.json --multi

# Export strategy as portable JSON
python -m darwinia export -c champion.json

Metrics: Sharpe ratio, Sortino ratio, Calmar ratio, max drawdown ($ and %), drawdown duration, win rate, profit factor, average win/loss, annualized return. Walk-forward mode tests stability across rolling windows. Multi-asset mode tests whether a strategy generalizes beyond its training data.

Ensemble Committee

Combine multiple evolved strategies into a committee that votes on each trade decision:

# 5-member committee with majority voting
python -m darwinia ensemble -d data/btc_1h.csv -s 5 --mode majority

# Fitness-weighted voting (better strategies count more)
python -m darwinia ensemble --mode weighted

# Unanimous mode (all must agree, else hold)
python -m darwinia ensemble --mode unanimous

Monte Carlo Stress Testing

Run hundreds of randomized market simulations to test strategy robustness:

# 500 bootstrap simulations
python -m darwinia montecarlo -c champion.json -n 500

# Add noise to prices
python -m darwinia montecarlo -c champion.json --method noise

# Shuffle returns to destroy structure
python -m darwinia montecarlo -c champion.json --method shuffle

Reports 95%/99% confidence intervals, probability of profit, and return distribution.

Benchmark Baselines

Compare your evolved strategy against 5 standard baselines:

python -m darwinia benchmark -c champion.json -d data/btc_1h.csv

Baselines: Buy-and-Hold, Random Trader, Mean Reversion, Momentum, DCA. Ranked by Sharpe ratio so you can see exactly where evolution adds value.

Strategy Fingerprint

Visualize a strategy's genetic identity:

python -m darwinia fingerprint -c champion.json
python -m darwinia fingerprint -c champion.json --json

Shows DNA as an ASCII radar chart, classifies into archetypes (Aggressive Momentum, Conservative Mean-Reversion, Scalper, Trend Follower, Contrarian, Balanced Adaptive), and highlights dominant traits.

Portfolio Allocator

Distribute capital across a basket of evolved strategies:

python -m darwinia portfolio --champions out/a.json out/b.json out/c.json --method risk_parity
python -m darwinia portfolio -s 5 --method kelly --json

Five allocation methods: equal_weight, sharpe_weighted, risk_parity, inverse_variance, and fractional kelly. Reports portfolio Sharpe, volatility, and the diversification ratio so you can see when stacking strategies actually adds value vs. concentrating capital in the best one.

Market Regime Detection

Classify market data into regimes for adaptive strategy behavior:

python -m darwinia regime -d data/btc_1h.csv -w 20
python -m darwinia regime -d data/btc_1h.csv --json

Five regimes: trending_up, trending_down, mean_reverting, high_volatility, low_volatility. Uses rolling z-scored trend strength, volatility, and autocorrelation. Reports regime distribution, segment analysis, transition matrix, and stability score.

Cross-Strategy Correlation

Identify redundant vs diversified strategies:

python -m darwinia correlation --champions a.json b.json c.json
python -m darwinia correlation -s 6 --json

Runs multiple strategies on the same data, bins trade returns into time buckets, and computes pairwise Pearson correlation. ASCII heatmap, auto-clustering, and pair extremes (most/least correlated). Critical input for portfolio construction — high correlation means you're doubling down, not diversifying.

Skill Composability

Darwinia exposes a SkillBridge API for other ClawHub skills to call it programmatically, and a SkillRegistry for Darwinia to call external skills (macro-liquidity, crypto-market-rank, okx-dex-market). This makes Darwinia a composable building block in multi-skill agent workflows.

Three Layers

Layer Status What It Does
Evolution Engine ✅ Implemented Genetic algorithm + adversarial arena + pattern discovery
Personality Engine ✅ Implemented Quantified trading personalities + market regime detection
Knowledge Protocol ✅ Implemented Agents trade discovered patterns through a marketplace

Beyond Trading

The evolution engine is domain-agnostic. The DNA → Fitness → Selection → Breed cycle works for any agent behavior that can be scored. This release targets crypto trading, but the framework applies to portfolio optimization, risk tuning, resource allocation, game strategy — any domain where you can define a fitness function.


Development

make setup       # Install dependencies
make test        # Run 176 tests
make evolve      # Run 50 generations
make arena       # Adversarial arena
make dashboard   # Streamlit dashboard

Design Philosophy

Traditional quant: human designs strategy from theory. Darwinia: human designs the environment. Agents discover the strategy.

Your role shifts from strategy author to environment designer — define the data, the fitness function, the attacks. Let evolution find the rest.

License

MIT