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.
115 lines
3.4 KiB
C#
115 lines
3.4 KiB
C#
namespace StockSharp.Tests;
|
|
|
|
using StockSharp.Diagram;
|
|
using StockSharp.Diagram.Elements;
|
|
|
|
/// <summary>
|
|
/// The random element feeds a strategy a drawn value, so a diagram containing one produced a
|
|
/// different backtest on every run - and an optimisation over such a diagram compared results
|
|
/// that were not made under the same conditions. The source is the strategy's, so one setting
|
|
/// covers every element in the diagram: a seeded source repeats a run, a stub states outright
|
|
/// what the element will output.
|
|
/// </summary>
|
|
[TestClass]
|
|
public class RandomDiagramElementTests : BaseTestClass
|
|
{
|
|
/// <summary>
|
|
/// A source handing out values that were decided in advance.
|
|
/// </summary>
|
|
private class StubRandomProvider(params double[] values) : IRandomProvider
|
|
{
|
|
private int _index;
|
|
|
|
private double Take() => values[_index++ % values.Length];
|
|
|
|
int IRandomProvider.Next(int min, int max) => min + (int)(Take() * (max - min));
|
|
long IRandomProvider.NextLong(long min, long max) => min + (long)(Take() * (max - min));
|
|
double IRandomProvider.NextDouble() => Take();
|
|
void IRandomProvider.NextBytes(byte[] buffer) => throw new NotSupportedException();
|
|
}
|
|
|
|
private static readonly DateTime _time = new(2026, 8, 4, 10, 0, 0, DateTimeKind.Utc);
|
|
|
|
private static RandomDiagramElement Attach(RandomDiagramElement element, IRandomProvider provider)
|
|
{
|
|
var model = new CompositionModel<InMemoryCompositionModelNode, InMemoryCompositionModelLink>(new InMemoryCompositionModelBehavior());
|
|
var strategy = new DiagramStrategy();
|
|
|
|
if (provider is not null)
|
|
strategy.RandomProvider = provider;
|
|
|
|
var composition = new CompositionDiagramElement(model)
|
|
{
|
|
Strategy = strategy,
|
|
};
|
|
|
|
element.Init(composition);
|
|
return element;
|
|
}
|
|
|
|
private static List<decimal> Run(IRandomProvider provider, int count)
|
|
{
|
|
var element = Attach(new RandomDiagramElement
|
|
{
|
|
Min = 0,
|
|
Max = 100,
|
|
}, provider);
|
|
|
|
var values = new List<decimal>();
|
|
element.ProcessOutput += v => values.Add(v.GetValue<Unit>().Value);
|
|
|
|
// Start raises the first value, each Flush one more.
|
|
element.Start(_time);
|
|
|
|
while (values.Count < count)
|
|
element.Flush(_time);
|
|
|
|
return values;
|
|
}
|
|
|
|
[TestMethod]
|
|
public void The_strategys_source_decides_what_the_element_outputs()
|
|
{
|
|
// The point of the whole arrangement: a test states the draws and knows the output.
|
|
var values = Run(new StubRandomProvider(0, 0.25, 0.5), 3);
|
|
|
|
AreEqual(0m, values[0]);
|
|
AreEqual(25m, values[1]);
|
|
AreEqual(50m, values[2]);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void A_seeded_strategy_runs_the_same_series_twice()
|
|
{
|
|
var a = Run(new SeededRandomProvider(42), 20);
|
|
var b = Run(new SeededRandomProvider(42), 20);
|
|
|
|
a.SequenceEqual(b).AssertTrue("the same seed must give the same series");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Different_seeds_do_not_run_the_same_series()
|
|
{
|
|
var a = Run(new SeededRandomProvider(1), 20);
|
|
var b = Run(new SeededRandomProvider(2), 20);
|
|
|
|
a.SequenceEqual(b).AssertFalse("two seeds that differ must not give the same series");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void A_strategy_left_alone_still_varies()
|
|
{
|
|
var a = Run(null, 20);
|
|
var b = Run(null, 20);
|
|
|
|
a.SequenceEqual(b).AssertFalse("with nobody holding a seed the element must behave as it always did");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Values_stay_within_the_bounds()
|
|
{
|
|
foreach (var value in Run(new SeededRandomProvider(3), 200))
|
|
(value is >= 0 and <= 100).AssertTrue($"{value} is outside the range the element was given");
|
|
}
|
|
}
|