market-solve

A general-purpose framework for stochastic market simulation, optimal strategy computation, and multi-agent backtesting.

Goal

market-solve is a tool for anyone who needs to simulate financial markets, evaluate trading strategies, or compute optimal policies under uncertainty. The framework treats processes, models, strategies, and exchange mechanics as replaceable components that can be mixed and matched at compile time with zero runtime overhead.

What you can do with it

  • Simulate price paths in batch: thousands of parallel Monte Carlo trajectories across 8 stochastic process families, from simple GBM to Hawkes-driven order flow.
  • Compute optimal strategies via HJB: solve Hamilton-Jacobi-Bellman equations using finite-difference policy iteration or BSDE-based least-squares Monte Carlo, with dimension-agnostic implementations that compose multi-factor models (inventory + volatility + intensity).
  • Backtest against a realistic exchange: run strategies inside a multi-agent engine with configurable order matching (simple, stochastic Poisson-based), exchange mechanics, observation filtering, and portfolio tracking.
  • Train RL agents on vectorized environments: run hundreds of independent simulation episodes in parallel via rayon, with reward functions including PnL and differential Sharpe ratio.
  • Generate synthetic order book data: produce BBO quotes from a composable price process. Full L2/L3 order book generation is planned.
  • Call from Python: the engine exposes strategies and backtest infrastructure via PyO3 bindings.

Workspace structure

The framework is split into three crates with clear responsibility boundaries:

CrateRoleKey exports
market_modelSimulation. Generate paths.Simulatable, SimulationRunner, PriceStrategy, Signal
solverOptimization. Solve HJB equations.BsdeSolver, PolicyIterator, Model<N>, analytical solutions
engineBacktesting. Run market simulations.Engine, Exchange, Matcher, Strategy, VecEnv

Dependency graph

graph BT
    market_model
    solver --> market_model
    engine --> market_model
    engine --> solver
    py_bindings --> engine

solver depends on market_model for process simulation and SimulationRunner. engine depends on market_model for processes and on solver for HJB-derived optimal strategies.

Quick start

git clone https://github.com/marci/market-solve
cd market-solve
cargo build --release
cargo test --workspace
cargo clippy --workspace -- -D warnings

Generate API documentation:

cargo doc --workspace --no-deps --open

Build the user guide (this book):

cargo install mdbook
cd docs && mdbook build

Example: simulation + backtest

#![allow(unused)]
fn main() {
use market_model::process::gbm::GeometricBrownianMotion;
use market_model::runner::SimulationRunner;
use market_model::types::SimulationConfig;

// 1. Simulate 10,000 GBM paths
let gbm = GeometricBrownianMotion::new(0.05, 0.2, 100.0);
let config = SimulationConfig {
    n_paths: 10_000,
    n_steps: 252,
    dt: 1.0 / 252.0,
    seed: 42,
};
let runner = SimulationRunner::new(gbm, config);
let result = runner.run();

// 2. Access results
let terminal_price = result.terminal(0).unwrap();  // path 0 final price
let path_prices = result.path(1).unwrap();          // all entries for path 1
}

For a full backtest with an order-book-aware strategy, the engine crate wraps the simulated data into an exchange with matching and portfolio tracking. See the engine page for a complete example.