Skip to content

[discussion] Should QuantGPT support a custom factor expression DSL? (RFC) #2

Description

@Miasyster

Why this RFC

QuantGPT's expression parser currently accepts pandas-style operators (ts_mean, cs_rank, ...) inline as Python expressions. This works but creates friction:

  1. AI agents need to learn syntax + valid operator names — scattered across docs/code, no formal grammar
  2. Users can't easily share factor definitions — expressions are tied to a Python execution context
  3. Reproducibility is fragile — operator semantics live in code, not in a spec

Platforms like WorldQuant BRAIN, Alphalens, and Qlib have moved to a dedicated factor DSL with explicit grammar, type checking, and a documented operator catalog.

Proposal sketch

factor "smart_momentum" {
    inputs: close, volume, amount
    formula: ts_corr(ts_returns(close, 5), volume / amount, 20)
    universe: hs300
    rebalance: daily
    neutralize: industry
}

Compiled internally to the current Python expression representation. But:

  • .factor files are standalone, version-controllable, shareable
  • Schema validation catches errors before backtest
  • LLM agent's contract becomes deterministic ("emit one .factor file")

Trade-offs

Pros

  • Cleaner LLM agent contract (structured output > free-form Python)
  • Factor definitions become first-class artifacts
  • Foundation for a public factor library / leaderboard
  • Validation before execution = faster iteration

Cons

  • Yet-another-language to learn
  • Implementation cost: parser, compiler, error messages
  • Risk of feature gap vs. raw Python expressions
  • Lock-in to specific operator semantics

Alternatives considered

  1. JSON / YAML factor specs — less expressive, no parser needed
  2. Python decorators — keep Python, enforce structure via @factor(...)
  3. Status quo — stick with Python, invest in better docs

Questions for the community

  1. Have you needed to share factor definitions across projects or teams? How did you handle it?
  2. If you use WorldQuant BRAIN / Qlib, would a similar DSL feel natural here?
  3. Do LLM agents in your workflow benefit from structured output formats (function schemas, JSON tool calls)?
  4. Any DSL — good or bad — we should learn from?

This is exploratory. No commitment to implement. Comment with use cases, alternatives, or pushback.

Difficulty: N/A (discussion)

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionOpen-ended technical discussion

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions