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

446 lines
11 KiB
C#

namespace StockSharp.Algo.Testing.Generation;
/// <summary>
/// The order book generator using random method.
/// </summary>
/// <remarks>
/// Initialize <see cref="MarketDepthGenerator"/>.
/// </remarks>
/// <param name="securityId">The identifier of the instrument, for which data shall be generated.</param>
public abstract class MarketDepthGenerator(SecurityId securityId) : MarketDataGenerator(securityId)
{
/// <inheritdoc />
public override DataType DataType => DataType.MarketDepth;
/// <summary>
/// To use to generate best quotes in the order book volume of history trades.
/// </summary>
/// <remarks>
/// The default value is <see langword="true" />.
/// </remarks>
public bool UseTradeVolume { get; set; } = true; // TODO
private int _minSpreadStepCount = 1;
/// <summary>
/// The minimal value of spread between the best quotes in units of price increments number. The spread value will be selected randomly between <see cref="MinSpreadStepCount"/> and <see cref="MaxSpreadStepCount"/>.
/// </summary>
/// <remarks>
/// The default value is 1.
/// </remarks>
public int MinSpreadStepCount
{
get => _minSpreadStepCount;
set
{
if (value < 1)
throw new ArgumentOutOfRangeException(nameof(value), value, LocalizedStrings.InvalidValue);
_minSpreadStepCount = value;
}
}
private int _maxSpreadStepCount = int.MaxValue;
/// <summary>
/// The maximal value of spread between the best quotes in units of price increments number. The spread value will be selected randomly between <see cref="MinSpreadStepCount"/> and <see cref="MaxSpreadStepCount"/>.
/// </summary>
/// <remarks>
/// The default value is <see cref="int.MaxValue"/>.
/// </remarks>
public int MaxSpreadStepCount
{
get => _maxSpreadStepCount;
set
{
if (value < 1)
throw new ArgumentOutOfRangeException(nameof(value), value, LocalizedStrings.InvalidValue);
_maxSpreadStepCount = value;
}
}
private int _maxBidsDepth = 10;
/// <summary>
/// The maximal depth of bids.
/// </summary>
/// <remarks>
/// The default value is 10.
/// </remarks>
public int MaxBidsDepth
{
get => _maxBidsDepth;
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value), value, LocalizedStrings.InvalidValue);
_maxBidsDepth = value;
}
}
private int _maxAsksDepth = 10;
/// <summary>
/// The maximal depth of offers.
/// </summary>
/// <remarks>
/// The default value is 10.
/// </remarks>
public int MaxAsksDepth
{
get => _maxAsksDepth;
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value), value, LocalizedStrings.InvalidValue);
_maxAsksDepth = value;
}
}
/// <summary>
/// Shall order books be generated after each trade. The default is <see langword="false" />.
/// </summary>
public bool GenerateDepthOnEachTrade { get; set; }
/// <summary>
/// Generate <see cref="QuoteChange.OrdersCount"/>.
/// </summary>
public bool GenerateOrdersCount { get; set; }
/// <inheritdoc />
public override void Init()
{
if (MinSpreadStepCount > MaxSpreadStepCount)
throw new ArgumentException($"{nameof(MinSpreadStepCount)} ({MinSpreadStepCount}) > {nameof(MaxSpreadStepCount)} ({MaxSpreadStepCount})");
base.Init();
}
private int _maxGenerations = 20;
/// <summary>
/// The maximal number of generations after last occurrence of source data for the order book.
/// </summary>
/// <remarks>
/// The default value equals 20.
/// </remarks>
public int MaxGenerations
{
get => _maxGenerations;
set
{
if (value < 1)
throw new ArgumentOutOfRangeException(nameof(value), value, LocalizedStrings.InvalidValue);
_maxGenerations = value;
}
}
/// <summary>
/// To create the quote using random method.
/// </summary>
/// <param name="startPrice">The initial price, based on which a quote price shall be got using random method.</param>
/// <param name="side">The quote direction.</param>
/// <returns>The random quote.</returns>
protected QuoteChange CreateQuote(decimal startPrice, Sides side)
{
var priceStep = SecurityDefinition.PriceStep ?? 0.01m;
var price = startPrice + (side == Sides.Sell ? 1 : -1) * Steps.Next() * priceStep;
if (price <= 0)
price = priceStep;
int? ordersCount = null;
if (GenerateOrdersCount && RandomProvider.GetBool())
ordersCount = Volumes.Next();
return new QuoteChange(price, Volumes.Next(), ordersCount);
}
/// <summary>
/// Copy the message into the <paramref name="destination" />.
/// </summary>
/// <param name="destination">The object, to which copied information.</param>
protected void CopyTo(MarketDepthGenerator destination)
{
base.CopyTo(destination);
destination.UseTradeVolume = UseTradeVolume;
destination.MinSpreadStepCount = MinSpreadStepCount;
destination.MaxSpreadStepCount = MaxSpreadStepCount;
destination.MaxBidsDepth = MaxBidsDepth;
destination.MaxAsksDepth = MaxAsksDepth;
destination.MaxGenerations = MaxGenerations;
destination.GenerateDepthOnEachTrade = GenerateDepthOnEachTrade;
destination.GenerateOrdersCount = GenerateOrdersCount;
}
}
/// <summary>
/// The order book generator, accounting for trades sequence.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="TrendMarketDepthGenerator"/>.
/// </remarks>
/// <param name="securityId">The identifier of the instrument, for which data shall be generated.</param>
public class TrendMarketDepthGenerator(SecurityId securityId) : MarketDepthGenerator(securityId)
{
private bool _newTrades;
// не генерировать стаканы, если у нас давно не было сделок
private int _currGenerations;
private decimal? _lastTradePrice;
private decimal? _prevTradePrice;
private decimal? _bestAskPrice;
private decimal? _bestBidPrice;
private BoardMessage _boardDefinition;
/// <inheritdoc />
public override void Init()
{
base.Init();
_lastTradePrice = null;
_prevTradePrice = null;
_bestAskPrice = null;
_bestBidPrice = null;
_newTrades = false;
_currGenerations = MaxGenerations;
_boardDefinition = null;
}
/// <inheritdoc />
protected override Message OnProcess(Message message)
{
if (_boardDefinition == null)
{
if (message.Type == MessageTypes.Board)
_boardDefinition = (BoardMessage)message.Clone();
return null;
}
DateTime time;
switch (message.Type)
{
case MessageTypes.Level1Change:
{
var l1Msg = (Level1ChangeMessage)message;
var value = l1Msg.TryGetDecimal(Level1Fields.LastTradePrice);
if (value != null)
_lastTradePrice = value.Value;
value = l1Msg.TryGetDecimal(Level1Fields.BestBidPrice);
if (value != null)
_bestBidPrice = value.Value;
value = l1Msg.TryGetDecimal(Level1Fields.BestAskPrice);
if (value != null)
_bestAskPrice = value.Value;
time = l1Msg.ServerTime;
break;
}
case MessageTypes.Execution:
{
var execMsg = (ExecutionMessage)message;
if (execMsg.DataType == DataType.Ticks)
{
var tradePrice = execMsg.TradePrice;
if (null == _prevTradePrice)
{
_prevTradePrice = tradePrice;
_bestAskPrice = tradePrice;
_bestBidPrice = tradePrice;
}
switch (execMsg.OriginSide)
{
case null:
{
if (tradePrice > _prevTradePrice)
{
_bestAskPrice = tradePrice;
//BestBid = PrevTrade;
_prevTradePrice = tradePrice;
}
else if (tradePrice < _prevTradePrice)
{
_bestBidPrice = tradePrice;
//BestAsk = PrevTrade;
_prevTradePrice = tradePrice;
}
break;
}
case Sides.Buy:
_bestAskPrice = tradePrice;
break;
default:
_bestBidPrice = tradePrice;
break;
}
_lastTradePrice = tradePrice;
_newTrades = true;
}
else
return null;
time = execMsg.ServerTime;
break;
}
case MessageTypes.Time:
{
var timeMsg = (TimeMessage)message;
time = timeMsg.ServerTime;
break;
}
default:
return null;
}
if (_currGenerations == 0 || _bestBidPrice == null || _bestAskPrice == null)
return null;
var isTradeTime = _boardDefinition.IsWorkingTime(time);
var canProcess = GenerateDepthOnEachTrade && _newTrades
? isTradeTime
: (IsTimeToGenerate(time) && isTradeTime);
if (!canProcess)
return null;
var wasNewTrade = _newTrades;
var depth = new QuoteChangeMessage
{
SecurityId = SecurityId,
ServerTime = time,
LocalTime = time,
};
if (_bestBidPrice == null || _bestAskPrice == null)
{
if (_lastTradePrice == null)
throw new InvalidOperationException(LocalizedStrings.NoInfoAboutLastTrade);
_bestBidPrice = _bestAskPrice = _lastTradePrice;
}
if (_currGenerations == 0)
throw new InvalidOperationException(LocalizedStrings.NoData2);
var bidPrice = _bestBidPrice;
var askPrice = _bestAskPrice;
var minSpred = MinSpreadStepCount * SecurityDefinition.PriceStep;
var maxStread = MaxSpreadStepCount * SecurityDefinition.PriceStep;
if ((askPrice - bidPrice) < minSpred)
{
if (_bestBidPrice == _lastTradePrice) // up trend
askPrice = bidPrice + minSpred;
else
bidPrice = askPrice - minSpred;
}
else if ((askPrice - bidPrice) > maxStread)
{
if (_bestBidPrice == _lastTradePrice) // down trend
askPrice = bidPrice + maxStread;
else
bidPrice = askPrice - maxStread;
}
var bids = new List<QuoteChange>();
//{
// new QuoteChange(Sides.Buy, bidPrice.Value, Volumes.Next())
//};
var count = MaxBidsDepth/* - bids.Count*/;
for (var i = 0; i < count; i++)
{
var quote = CreateQuote(bidPrice.Value, Sides.Buy);
if (quote.Price <= 0)
break;
bids.Add(quote);
bidPrice = quote.Price;
}
var asks = new List<QuoteChange>();
//{
// new QuoteChange(Sides.Sell, askPrice.Value, Volumes.Next())
//};
count = MaxAsksDepth/* - asks.Count*/;
for (var i = 0; i < count; i++)
{
var quote = CreateQuote(askPrice.Value, Sides.Sell);
if (quote.Price <= 0)
break;
asks.Add(quote);
askPrice = quote.Price;
}
depth.Bids = [.. bids];
depth.Asks = [.. asks];
_newTrades = false;
LastGenerationTime = time;
if (wasNewTrade)
_currGenerations = MaxGenerations;
else
_currGenerations--;
return depth;
}
//private static bool IsTickMessage(Message message)
//{
// if (message.Type != MessageTypes.Execution)
// return false;
// var tradeMessage = (ExecutionMessage)message;
// return tradeMessage.DataType == DataType.Ticks;
//}
/// <summary>
/// Create a copy of <see cref="TrendMarketDepthGenerator"/>.
/// </summary>
/// <returns>Copy.</returns>
public override MarketDataGenerator Clone()
{
var clone = new TrendMarketDepthGenerator(SecurityId);
CopyTo(clone);
return clone;
}
}