mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 00:21:31 +00:00
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.
74 lines
1.9 KiB
C#
74 lines
1.9 KiB
C#
namespace StockSharp.Tests;
|
|
|
|
using StockSharp.Algo.Testing;
|
|
|
|
/// <summary>
|
|
/// Mock random provider for testing with configurable behavior.
|
|
/// </summary>
|
|
public class MockEmulationRandomizer : IEmulationRandomizer
|
|
{
|
|
private Func<decimal> _nextVolume;
|
|
private Func<int, int> _nextSpreadStep;
|
|
private Func<bool> _shouldMatch;
|
|
private Func<double, bool> _shouldFail;
|
|
|
|
/// <summary>
|
|
/// Function to generate volume. Default returns 50.
|
|
/// </summary>
|
|
public Func<decimal> NextVolumeFunc
|
|
{
|
|
get => _nextVolume;
|
|
set => _nextVolume = value ?? throw new ArgumentNullException(nameof(value));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Function to generate spread step. Default returns 1.
|
|
/// </summary>
|
|
public Func<int, int> NextSpreadStepFunc
|
|
{
|
|
get => _nextSpreadStep;
|
|
set => _nextSpreadStep = value ?? throw new ArgumentNullException(nameof(value));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Function to determine if should match. Default returns false.
|
|
/// </summary>
|
|
public Func<bool> ShouldMatchFunc
|
|
{
|
|
get => _shouldMatch;
|
|
set => _shouldMatch = value ?? throw new ArgumentNullException(nameof(value));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Function to determine if should fail. Default returns false.
|
|
/// </summary>
|
|
public Func<double, bool> ShouldFailFunc
|
|
{
|
|
get => _shouldFail;
|
|
set => _shouldFail = value ?? throw new ArgumentNullException(nameof(value));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates mock provider with default deterministic behavior.
|
|
/// </summary>
|
|
public MockEmulationRandomizer()
|
|
{
|
|
_nextVolume = () => 50;
|
|
_nextSpreadStep = max => 1;
|
|
_shouldMatch = () => false;
|
|
_shouldFail = _ => false;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public decimal NextVolume() => _nextVolume();
|
|
|
|
/// <inheritdoc />
|
|
public int NextSpreadStep(int maxSpreadSize) => _nextSpreadStep(maxSpreadSize);
|
|
|
|
/// <inheritdoc />
|
|
public bool ShouldMatch() => _shouldMatch();
|
|
|
|
/// <inheritdoc />
|
|
public bool ShouldFail(double failingPercent) => _shouldFail(failingPercent);
|
|
}
|