Skip to main content

Random and template-based alpha expression generator for WorldQuant Brain.

Project description

wq-alphagen

Random and template-based alpha expression generator for WorldQuant Brain.

Generates Brain-syntax alpha expressions like rank(ts_delta(close, 5)) from your own data field catalog and operator reference. Built-in deduplication, type-safe sampling, and syntax validation.

Install

pip install wq-alphagen

What you need to supply

This package does not ship WorldQuant Brain's data — you bring your own:

  • data_fields.csv — your Brain data fields with columns: field, type, coverage, dataset_category, alpha_count
  • wq_operators.json — your Brain operator reference in the schema expected by alphagen (see examples/example_operators.json for the shape)

See the examples/ directory for tiny demo files you can use to try the package immediately.

Quick start

Template mode (inline)

from alphagen import AlphaGenerator

with AlphaGenerator(
    fields_path="data_fields.csv",
    operators_path="wq_operators.json",
    history_path="alpha_history.jsonl",   # or None to skip persistence
) as gen:
    result = gen.from_template_strings([
        "rank(ts_delta({x:MATRIX}, {d:int=5,10,20}))",
        "zscore(ts_mean({x:MATRIX}, {d:int=20,60}))",
        "group_neutralize({x:MATRIX}, {g:GROUP})",
    ], per_template=50)

    print(result.summary())
    for expr in result.alphas:
        print(expr)

Template mode (from file)

result = gen.from_templates("alpha_templates.txt", per_template=50)

A template file is plain text — one expression per line, # for comments. Placeholders use {name:TYPE} or {name:TYPE=value1,value2,...}:

Placeholder Picks
{x:MATRIX} a random MATRIX field
{x:MATRIX=close,vwap} from this explicit allow-list
{g:GROUP} a random GROUP field
{d:int} from [5, 10, 20, 60, 120, 250]
{d:int=5,10,20} from this explicit list
{a:float=0.5,1.0,2.0} random float pick
{f:bool} random true/false
{s:string=a,b,c} random string pick (explicit values required)

Same {name:...} repeated in one expression reuses the same value.

Random mode

with AlphaGenerator(
    fields_path="data_fields.csv",
    operators_path="wq_operators.json",
) as gen:
    result = gen.random(count=100, max_depth=3, min_operators=2)

One-liners

from alphagen import generate_random, generate_from_template_strings

result = generate_random(
    count=100,
    fields_path="data_fields.csv",
    operators_path="wq_operators.json",
    seed=42,
)

result = generate_from_template_strings(
    ["rank({x:MATRIX})", "zscore({x:MATRIX})"],
    per_template=20,
    fields_path="data_fields.csv",
    operators_path="wq_operators.json",
)

Result object

Every generator method returns a GenerationResult:

result.alphas       # list[str] — NEW unique alpha expressions
result.invalid      # [(expr, [errors]), ...] from validator
result.attempts     # total samples drawn
result.duplicates   # how many were already in history
result.seconds      # wall-clock time
result.summary()    # one-line stat string

Iterate directly: for expr in result: ....

Filtering the field pool

gen = AlphaGenerator(
    fields_path="data_fields.csv",
    operators_path="wq_operators.json",
    min_coverage=0.7,                           # require coverage >= 70%
    max_alpha_count=1000,                       # avoid heavily-mined fields
    datasets=("pv1", "fnd6"),                   # only these datasets
    exclude_datasets=("analyst1", "analyst4"),  # never these datasets
    validate=True,                              # auto syntax-check (default)
    seed=42,                                    # reproducible
)

Standalone validator

from alphagen import AlphaValidator

v = AlphaValidator("wq_operators.json", "data_fields.csv")
errors = v.validate("rank(ts_delta(close, 5))")    # [] = OK

History store

The dedup store can be used on its own:

from alphagen import AlphaHistory

with AlphaHistory("alpha_history.jsonl") as h:
    h.add("rank(close)")           # True — new
    h.add("rank( close )")         # False — same expression, whitespace-insensitive
    print(len(h), "unique alphas")

License

MIT — see LICENSE.

Disclaimer

This is an independent open-source tool. It is not affiliated with, endorsed by, or sponsored by WorldQuant.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wq_alphagen-0.1.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wq_alphagen-0.1.0-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file wq_alphagen-0.1.0.tar.gz.

File metadata

  • Download URL: wq_alphagen-0.1.0.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for wq_alphagen-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9a7bec946a5554f91cce0f5941f074e65da1e049e3303f77a803a5a38a8b5bc2
MD5 0f5b417c4235eeb8f9ba3876f836d0d9
BLAKE2b-256 529bb82310d1b5bd89acada2550c5e9e1fb07e8d4825e3d7ac5f097abf5393b0

See more details on using hashes here.

File details

Details for the file wq_alphagen-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: wq_alphagen-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for wq_alphagen-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54dadb6e311e844fde9dad08f26cc87fec9783cb43287a67602107d7f14e1196
MD5 b8dd857d239f690eab6c72d67dfc7b7d
BLAKE2b-256 6613d8190506899a1178c57a875030183974f01abfd4fba87a359c7ee96ee2eb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page