Skip to content

simulation

The simulation module (std::core::simulation) provides domain-agnostic simulation primitives for event-driven state machines over time series. Finance, IoT, and physics modules build on these primitives.

from std::core::simulation use {
signal_mode_config, full_mode_config, state_only_config,
get_results, get_final_state, get_processed_count, replay
}

simulate() is a method on data tables that runs a state machine over each row. The handler function receives (row, state, index) and returns either:

  • The new state directly (no result collected)
  • { state: newState, result: value } to collect a result
let result = prices.simulate(
|row, state, idx| {
if row.close > state.threshold {
{ state: { ...state, position: 1 }, result: "buy" }
} else {
state
}
},
{ initial_state: { position: 0, threshold: 100.0 } }
)
FieldTypeDescription
initial_stateobjectStarting state for the state machine
modestring"full" for row access, "signal" for numeric values
collect_resultsboolWhether to accumulate results
collect_event_logboolWhether to collect event log for replay
FieldTypeDescription
final_stateobjectState after processing all rows
resultsarrayCollected results from handler
elements_processedintNumber of rows processed
  • signal_mode_config(initial_state?) — Config optimized for pre-computed SIMD signals.
  • full_mode_config(initial_state?) — Config with full row access.
  • state_only_config(initial_state?) — Config that skips result collection (saves memory).
  • get_results(sim_result) — Extract the collected results array.
  • get_final_state(sim_result) — Extract the final state.
  • get_processed_count(sim_result) — Get count of processed elements.

simulate_correlated() runs a state machine over multiple aligned series. The handler receives (context, state, index) where context maps series names to current values.

let result = simulate_correlated(
{ spy: spy_prices, vix: vix_prices },
|ctx, state, idx| {
let spread = ctx.spy - ctx.vix * 10
if spread > 50 && state.position == 0 {
{ state: { position: 1 }, result: "enter_long" }
} else {
state
}
},
{ initial_state: { position: 0 } }
)

All series must have the same length (aligned timestamps).

Re-run a simulation with event log collection enabled, useful for debugging simulation behavior deterministically.

let debug_result = replay(prices, my_handler, my_config)
// debug_result includes event_log array
  • Finance — financial backtesting built on simulation
  • IoT — device monitoring built on simulation