Skip to main content

strategy-genome

A typed genetic algorithm (GA) and genetic programming (GP) optimizer for Python, with an Optuna sampler so any Optuna study can use it.

  • You bring the fitness function. evolve(fitness, space) calls fitness(genome) and expects a float, or a tuple of floats for multi-objective runs. Nothing about trading is built in.
  • Mixed search spaces: floats (optionally on a step grid), integers, log-scale floats, categoricals and booleans.
  • Real GA machinery: elitism, tournament selection, SBX / arithmetic / uniform / one- and two-point crossover, gaussian / polynomial / creep / boundary mutation, adaptive mutation rate and adaptive operator choice, NSGA-II for multiple objectives, an island model, stagnation restarts, and a hall of fame.
  • Reproducible: the same seed and the same fitness give the same run.
  • Light: the GA core is pure standard-library Python. numpy is used by the GP submodule and the trading examples. Optuna is an optional extra.

Carved out of srfm-lab (ml/genetic), MIT licensed. See provenance for what was changed and which upstream bugs were fixed on the way out.

Install

pip install strategy-genome            # GA core + GP (numpy)
pip install "strategy-genome[optuna]"  # adds the Optuna sampler

Python 3.10 or newer.

Quickstart

from strategy_genome import evolve, space as sp

def fitness(g):
    # g["name"] reads one parameter; g.params is the whole dict
    return (g["x"] - 3.0) ** 2 + (g["y"] + 1.0) ** 2

result = evolve(
    fitness,
    {"x": sp.Float(-10, 10), "y": sp.Float(-10, 10)},
    population=40,
    generations=80,
    seed=7,
    direction="minimize",
)

result.best_params        # {'x': 3.00..., 'y': -0.99...}
result.best_value         # what your function returned for the best genome
result.hall_of_fame       # best unique genomes seen, best first
result.diversity_history  # mean pairwise parameter distance per generation

Search spaces

space = {
    "fast":  sp.Int(2, 50),
    "slow":  sp.Int(10, 200, step=5),
    "stop":  sp.Float(0.005, 0.10),
    "lr":    sp.LogFloat(1e-5, 1e-1),
    "kind":  sp.Categorical(["ema", "sma", "wma"]),
    "short": sp.Bool(),
}

Shorthand also works: (0, 10) is an integer range, (0.0, 1.0) a float range, and a list is a categorical.

Several objectives

Return a tuple and give one direction per objective. The run switches to NSGA-II and result.pareto_front holds the non-dominated genomes.

def fitness(g):
    returns = backtest(g.params)
    return sharpe(returns), max_drawdown(returns)

result = evolve(fitness, space, population=60, generations=100, seed=1,
                directions=["maximize", "minimize"])
for g in result.pareto_front:
    print(g.values, g.params)

Failures

If fitness raises or returns NaN, that genome scores -inf and the run continues. result.n_failed_evaluations counts them. Pass raise_errors=True to stop on the first error instead.

Use it from Optuna (and freqtrade)

import optuna
from strategy_genome.optuna import GeneticStrategySampler

study = optuna.create_study(sampler=GeneticStrategySampler(population_size=30, seed=1))
study.optimize(objective, n_trials=500)

freqtrade's hyperopt runs on Optuna and lets a strategy return any optuna.samplers.BaseSampler from HyperOpt.generate_estimator, so the sampler drops straight in. See Optuna and freqtrade, including a benchmark against Random, NSGA-II and TPE.

More

License

MIT. Copyright (c) 2026 Matthew Busel. See LICENSE and NOTICE.

Metadata

Release files for strategy-genome 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strategy-genome 0.1.0
File Size Uploaded Provenance Actions
strategy_genome-0.1.0.tar.gz 113.5 kB With provenance Details for strategy_genome-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for strategy-genome 0.1.0
File Interpreter ABI Platform Actions
strategy_genome-0.1.0-py3-none-any.whl Python 3 none any Details for strategy_genome-0.1.0-py3-none-any.whl

Total release size: 212.8 kB

Release files / strategy_genome-0.1.0.tar.gz

Download URL strategy_genome-0.1.0.tar.gz
Size 113.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fdac375e1e3cce00f6b99adf303b86b9be3640b57bdcf432953a0915fdd92fe3
BLAKE2b-256 checksum
How to use checksums
74865978c620f4b5d83cd053fc55fbb7a559959538b70a63a8ef3735242e36f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Release files / strategy_genome-0.1.0-py3-none-any.whl

Download URL strategy_genome-0.1.0-py3-none-any.whl
Size 99.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ed86c8aa176849c07e25dad20c3e8a46d95affaceb416f78411ecb0e58931ab
BLAKE2b-256 checksum
How to use checksums
a2a87475cda83c1be12f39a56210b710c74833693ca986b7d5e889b0839c85f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Security for strategy-genome 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for strategy_genome-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page