Files
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

153 lines
3.4 KiB
C#

namespace StockSharp.Algo.Testing.Generation;
/// <summary>
/// Tick trades generator using random method.
/// </summary>
public abstract class TradeGenerator : MarketDataGenerator
{
/// <summary>
/// Initialize <see cref="TradeGenerator"/>.
/// </summary>
/// <param name="securityId">The identifier of the instrument, for which data shall be generated.</param>
protected TradeGenerator(SecurityId securityId)
: base(securityId)
{
IdGenerator = new IncrementalIdGenerator();
}
/// <inheritdoc />
public override DataType DataType => DataType.Ticks;
private IdGenerator _idGenerator;
/// <summary>
/// The trade identifier generator <see cref="ExecutionMessage.TradeId"/>.
/// </summary>
public IdGenerator IdGenerator
{
get => _idGenerator;
set => _idGenerator = value ?? throw new ArgumentNullException(nameof(value));
}
}
/// <summary>
/// The trade generator based on normal distribution.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="RandomWalkTradeGenerator"/>.
/// </remarks>
/// <param name="securityId">The identifier of the instrument, for which data shall be generated.</param>
public class RandomWalkTradeGenerator(SecurityId securityId) : TradeGenerator(securityId)
{
private decimal _lastTradePrice;
/// <inheritdoc />
public override void Init()
{
base.Init();
_lastTradePrice = default;
}
/// <summary>
/// To generate the value for <see cref="ExecutionMessage.OriginSide"/>. By default is disabled.
/// </summary>
public bool GenerateOriginSide { get; set; }
/// <inheritdoc />
protected override Message OnProcess(Message message)
{
DateTime time;
switch (message.Type)
{
case MessageTypes.Board:
return null;
case MessageTypes.Level1Change:
{
var l1Msg = (Level1ChangeMessage)message;
var value = l1Msg.TryGetDecimal(Level1Fields.LastTradePrice);
if (value != null)
_lastTradePrice = value.Value;
time = l1Msg.ServerTime;
break;
}
case MessageTypes.Execution:
{
var execMsg = (ExecutionMessage)message;
var price = execMsg.TradePrice;
if (price != null)
_lastTradePrice = price.Value;
else if (execMsg.DataType != DataType.OrderLog)
return null;
time = execMsg.ServerTime;
break;
}
case MessageTypes.Time:
{
var timeMsg = (TimeMessage)message;
time = timeMsg.ServerTime;
break;
}
default:
return null;
}
if (!IsTimeToGenerate(time))
return null;
var v = Volumes.Next();
if(v == 0)
v = 1;
var trade = new ExecutionMessage
{
SecurityId = SecurityId,
TradeId = IdGenerator.GetNextId(),
ServerTime = time,
LocalTime = time,
OriginSide = GenerateOriginSide ? RandomProvider.GetEnum<Sides>() : null,
TradeVolume = v * (SecurityDefinition?.VolumeStep ?? 1m),
DataTypeEx = DataType.Ticks
};
var priceStep = SecurityDefinition?.PriceStep ?? 0.01m;
_lastTradePrice += RandomProvider.GetInt(-MaxPriceStepCount, MaxPriceStepCount) * priceStep;
if (_lastTradePrice <= 0)
_lastTradePrice = priceStep;
trade.TradePrice = _lastTradePrice;
LastGenerationTime = time;
return trade;
}
/// <summary>
/// Create a copy of <see cref="RandomWalkTradeGenerator"/>.
/// </summary>
/// <returns>Copy.</returns>
public override MarketDataGenerator Clone()
{
var clone = new RandomWalkTradeGenerator(SecurityId)
{
GenerateOriginSide = GenerateOriginSide,
};
CopyTo(clone);
return clone;
}
}