namespace StockSharp.Algo.Testing; /// /// Provides random values for market emulation. /// public interface IEmulationRandomizer { /// /// Gets next random volume for synthetic order book generation. /// /// Random volume. decimal NextVolume(); /// /// Gets next spread step multiplier for order book generation. /// /// Maximum spread size from settings. /// Spread step multiplier (1 to maxSpreadSize). int NextSpreadStep(int maxSpreadSize); /// /// Determines whether order should be matched when processing order book changes. /// /// True if should match. bool ShouldMatch(); /// /// Determines whether operation should fail (for failure simulation). /// /// Failing percentage from settings (0-100). /// True if should fail. bool ShouldFail(double failingPercent); } /// /// The decisions an emulated venue makes, drawn from a source of random values. /// public class DefaultEmulationRandomizer : IEmulationRandomizer { private readonly IRandomProvider _random; /// /// Initializes a new instance with a source nobody holds the seed of, so runs vary. /// public DefaultEmulationRandomizer() : this(new DefaultRandomProvider()) { } /// /// Initializes a new instance drawing from the stated point, so the run can be had again. /// /// Where the series starts. public DefaultEmulationRandomizer(int seed) : this(new SeededRandomProvider(seed)) { } /// /// Initializes a new instance drawing from the given source. /// /// Source of random values. public DefaultEmulationRandomizer(IRandomProvider random) { _random = random ?? throw new ArgumentNullException(nameof(random)); } /// public decimal NextVolume() => _random.GetInt(10, 99); /// // A spread of one step leaves nothing to choose: the step is that one. public int NextSpreadStep(int maxSpreadSize) => _random.GetInt(1, maxSpreadSize > 1 ? maxSpreadSize - 1 : 1); /// public bool ShouldMatch() => _random.GetBool(); /// public bool ShouldFail(double failingPercent) => _random.GetDouble() < (failingPercent / 100.0); }