Expand description
Pure, I/O-free backtest engine core for US equity strategies.
§Pipeline
- Build
panel::Panels of market / factor data (dates × symbols,f64). - Put them in an
EvalContext(optionally with a sector map). - Evaluate a lemon JSON
spec::Exprwithrun_strategy→ position panel. - Or run end-to-end with
run_backtest→report::Report(equity, trades, metrics).
No network and no platform deps: matrices in, report out. Compiles to native
and WASM. Conceptual overview: docs/backtest-engine.md. Strategy language:
docs/lemon.md.
§Example
See the crate example basic_backtest:
cargo run -p yuzu-core --example basic_backtestRe-exports§
pub use error::EngineError;pub use eval::run_backtest;pub use eval::run_strategy;pub use eval::EvalContext;
Modules§
- align
alignreshapes two panels onto a shared index before an elementwise op: rows = union of dates (sorted), columns = intersection of symbols (left’s order). Absent cells becomeNaN(standard reshape/align semantics).- backtest
- Daily-equity NAV loop: turns a position-weight matrix + price panel into an
equity curve and a trade list. See
docs/backtest-engine.mdfor the model. - bootstrap
- Circular block bootstrap over daily returns: resample the return series in blocks (preserving short-range autocorrelation), rebuild synthetic equity curves, and report percentile confidence intervals for the headline metrics. Deterministic — a built-in SplitMix64 PRNG with a fixed seed, no external dependencies (WASM-safe).
- error
EngineError: shape violations from panel construction and evaluation failures.- eval
- Evaluates an
Exprtree against a data context (HashMap<String, Panel>). - metrics
- Standard performance metrics (CAGR, Sharpe, Sortino, max drawdown, etc.). All
functions operate on a daily equity curve (
&[f64], base 1.0) or a daily returns slice. Conventions: annualization 252, rf = 0, std ddof = 1. - ops
- DSL operations, grouped by family. Each submodule adds
impl Panelmethods. - panel
- The
Paneltype: a densef64matrix indexed bydates(rows,YYYYMMDD) andsymbols(columns).NaN= missing; booleans are1.0/0.0withNaNfalsy (seeis_true). - report
- Serializable backtest report — the JSON contract the web app renders (equity/drawdown series + trade list + metrics). Engine computes; frontend draws.
- research
- Factor diagnostics and event studies — research workflows that take panels and return JSON, deliberately kept out of the lemon AST (see issue #45). These are library entry points, not strategy ops: they don’t produce positions or change backtest NAV semantics.
- spec
Expr: the serializable strategy AST. A JSON tree (tagged by"op") that the website and the batch runner both emit; the engine’s evaluator walks it against a data context to produce a position matrix.