Files
StockSharp/Algo.Testing/IEmulationRandomizer.cs
mika@stocksharp.com 131c1e3a5a Draw random values from a source the caller states
Anything in the engine that drew at random reached for the static generator, so no
result that depended on a draw could be produced a second time. That is fine for a
nonce and wrong for everything here: a backtest measured on generated order books,
logs and trades; a genetic search that landed on a set of parameters; an emulated
venue deciding a fill; a diagram feeding a strategy a drawn value. Each of those is
a result someone looks at, argues about and wants back.

A strategy now carries the source the whole run draws from. It is never null - left
alone it is the shared unseeded one and the strategy varies exactly as before - and
setting it covers everything the run touches at once: the genetic search over its
parameters, and every Random element in a diagram, which is the case that mattered
most, since that element's Min and Max are optimisable and an optimisation over such
a diagram was comparing runs made under different conditions. A test can put a stub
there and state outright what the element will output.

Elsewhere the source is a parameter or a property like any other, never an optional
one that quietly falls back: a strategy parameter asked for a random value, the
market-data generators, the emulator's randomizer which held its own Random until
now. Every call site says where its values come from.

The sample indicator template is changed for the same reason in all three of its
languages: it is what a user copies when writing their own, and it was teaching them
to reach for the global generator.

The tests are the first to exercise a diagram element on its own, which needed a
dispatcher registered in the assembly setup - a socket asks for one as soon as an
element is built.
2026-08-04 02:28:14 +03:00

81 lines
2.4 KiB
C#

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