pub trait Simulate<X, Y>{
// Required method
fn simulate(
&self,
sim: &Simulator<X, Y>,
exit: ExitPolicy,
) -> Result<SimulationStatsResult, SimulationError>;
}Expand description
Trait for simulating trading strategies across multiple price paths.
This trait enables strategies to be tested against various market scenarios by running them through multiple simulated price paths (random walks) and evaluating their performance based on defined exit policies.
§Type Parameters
X- The type representing time steps in the simulationY- The type representing price values in the simulation
§Examples
ⓘ
use optionstratlib::simulation::{Simulate, ExitPolicy};
use rust_decimal_macros::dec;
let strategy = ShortPut::new(/* ... */);
let simulator = Simulator::new(/* ... */);
let exit_policy = ExitPolicy::profit_or_loss(dec!(0.5), dec!(1.0));
let results = strategy.simulate(&simulator, exit_policy)?;Required Methods§
Sourcefn simulate(
&self,
sim: &Simulator<X, Y>,
exit: ExitPolicy,
) -> Result<SimulationStatsResult, SimulationError>
fn simulate( &self, sim: &Simulator<X, Y>, exit: ExitPolicy, ) -> Result<SimulationStatsResult, SimulationError>
Simulates the strategy across multiple price paths.
Evaluates the strategy’s performance by running it through each random walk in the simulator, checking exit conditions at each step, and calculating final P&L based on either exit triggers or expiration.
§Parameters
sim- The simulator containing multiple random walks to test againstexit- The exit policy defining when to close positions
§Returns
A SimulationStats struct containing:
- Individual
SimulationResultfor each run (with P&L, exit reason, holding period, etc.) - Aggregate statistics (average P&L, win rate, std deviation, etc.)
§Errors
Returns an error if:
- Option pricing calculations fail
- P&L calculations encounter errors
- Invalid strategy parameters are detected
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".