Skip to main content

Build a Simulation Monitor

A 6-chapter project tutorial. You will build a trading simulation that generates events, detect patterns with fabula's incremental engine, react to matches in real time, score them by surprise, and use engine forking for speculative "what-if" analysis.

Total time~2 hours
DifficultyIntermediate
PrerequisitesGetting Started, basic Rust

What you'll build

A command-line program that:

  1. Runs a simple trading simulation producing timestamped events (trades, price changes, alerts)
  2. Detects three patterns: insider trading (sequence + negation), market manipulation (repeat-range), and flash crash (concurrent group)
  3. Reacts to SiftEvent::Completed and SiftEvent::Negated as they occur
  4. Uses why_not to explain near-misses
  5. Scores matches with SurpriseScorer and StuScorer
  6. Forks the engine to evaluate hypothetical "what if this trade happened?" scenarios

Chapters

#ChapterWhat you learn
1Simulation LoopBuild an event-producing simulation with MemGraph
2Define PatternsWrite 3 patterns using both builder API and DSL
3Incremental MatchingWire the engine into the simulation loop
4React to EventsHandle SiftEvents, drain matches, use deadlines
5Score and RankAdd surprise scoring to rank matches
6Speculate with MCTSFork the engine for what-if analysis

Each chapter builds on the previous one. Complete code at the end of each chapter.

Setup

cargo new fabula-monitor
cd fabula-monitor

Add to Cargo.toml:

[dependencies]
fabula = { path = "../fabula/crates/fabula" }
fabula-memory = { path = "../fabula/crates/fabula-memory" }
fabula-dsl = { path = "../fabula/crates/fabula-dsl" }
fabula-narratives = { path = "../fabula/crates/fabula-narratives" }

Or, if using published crates:

[dependencies]
fabula = "0.1"
fabula-memory = "0.1"
fabula-dsl = "0.1"
fabula-narratives = "0.1"

Start Chapter 1 →