Files
StockSharp/Tests/StorageHelperLoadMessagesTests.cs
mika@stocksharp.com 223297f299 Build requested candle series with calendar bounds and accurate source updates
Honor an explicit smaller time-frame BuildFrom even when the source offers the requested larger time frame. Require the source time frame to be offered, smaller and an exact divisor of the requested series.

Use calendar bounds for monthly, multi-day and uneven intraday candles. Align multi-day candles to a fixed grid, end the last intraday slot at midnight when no session is specified, and shift candle opens by the calendar when finishing, filling gaps and resuming compressed series.

Count each source candle once using its latest cumulative state, including repeated and out-of-order updates. Restore candle and cloneable volume-profile snapshots for updates, rebuild a forming candle when an earlier source changes, and ignore updates to candles already finished.

Keep separate series and compressors for each security. Translate requested candle counts into source time ranges and finish each security's forming candle at the end of history. Sum source trade counts and buy/sell volumes, keep the latest open interest, and preserve missing trade counts.

Cover the behavior through compressor, message-adapter and storage regression tests.
2026-10-22 19:04:57 +03:00

1203 lines
42 KiB
C#

namespace StockSharp.Tests;
using StockSharp.Algo.Candles.Compression;
[TestClass]
public class StorageHelperLoadMessagesTests : BaseTestClass
{
// The drive a test reads through: what it holds is the other drive's, and it tells how many bytes of each day's file
// were read and how long that file is.
private sealed class ReadCountingDrive(IMarketDataDrive inner) : BaseMarketDataDrive
{
private readonly Dictionary<IMarketDataStorageDrive, IMarketDataStorageDrive> _drives = [];
private readonly Dictionary<DateTime, long> _read = [];
private readonly Dictionary<DateTime, long> _lengths = [];
public long ReadOf(DateTime day) => _read.TryGetValue(day);
public long LengthOf(DateTime day) => _lengths[day];
public override string Path
{
get => inner.Path;
set => throw new NotSupportedException();
}
public override IAsyncEnumerable<SecurityId> GetAvailableSecuritiesAsync()
=> inner.GetAvailableSecuritiesAsync();
public override IAsyncEnumerable<DataType> GetAvailableDataTypesAsync(SecurityId securityId, StorageFormats format)
=> inner.GetAvailableDataTypesAsync(securityId, format);
public override IMarketDataStorageDrive GetStorageDrive(SecurityId securityId, DataType dataType, StorageFormats format)
=> _drives.SafeAdd(inner.GetStorageDrive(securityId, dataType, format), key => new StorageDrive(this, key));
public override ValueTask VerifyAsync(CancellationToken cancellationToken)
=> inner.VerifyAsync(cancellationToken);
public override IAsyncEnumerable<SecurityMessage> LookupSecuritiesAsync(SecurityLookupMessage criteria, ISecurityProvider securityProvider)
=> inner.LookupSecuritiesAsync(criteria, securityProvider);
private void OnRead(DateTime day, long length, int bytes)
{
_lengths[day] = length;
_read[day] = _read.TryGetValue(day) + bytes;
}
private sealed class StorageDrive(ReadCountingDrive drive, IMarketDataStorageDrive underlying) : IMarketDataStorageDrive
{
IMarketDataDrive IMarketDataStorageDrive.Drive => drive;
IAsyncEnumerable<DateTime> IMarketDataStorageDrive.GetDatesAsync() => underlying.GetDatesAsync();
ValueTask IMarketDataStorageDrive.ClearDatesCacheAsync(CancellationToken cancellationToken)
=> underlying.ClearDatesCacheAsync(cancellationToken);
ValueTask IMarketDataStorageDrive.DeleteAsync(DateTime date, CancellationToken cancellationToken)
=> throw new NotSupportedException();
ValueTask IMarketDataStorageDrive.SaveStreamAsync(DateTime date, Stream stream, CancellationToken cancellationToken)
=> throw new NotSupportedException();
async ValueTask<Stream> IMarketDataStorageDrive.LoadStreamAsync(DateTime date, bool readOnly, CancellationToken cancellationToken)
{
if (!readOnly)
throw new NotSupportedException();
var stream = await underlying.LoadStreamAsync(date, readOnly, cancellationToken);
// The storage tells a missing day by this very instance.
if (stream == Stream.Null)
return stream;
return new CountingStream(stream, bytes => drive.OnRead(date, stream.Length, bytes));
}
}
private sealed class CountingStream(Stream underlying, Action<int> read) : Stream
{
public override bool CanRead => underlying.CanRead;
public override bool CanSeek => underlying.CanSeek;
public override bool CanWrite => false;
public override long Length => underlying.Length;
public override long Position
{
get => underlying.Position;
set => underlying.Position = value;
}
public override int Read(byte[] buffer, int offset, int count)
{
var bytes = underlying.Read(buffer, offset, count);
read(bytes);
return bytes;
}
public override long Seek(long offset, SeekOrigin origin) => underlying.Seek(offset, origin);
public override void Flush() => underlying.Flush();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
protected override void Dispose(bool disposing)
{
if (disposing)
underlying.Dispose();
base.Dispose(disposing);
}
}
}
private static (StorageCoreSettings settings, CandleBuilderProvider candleBuilderProvider, SecurityId secId) CreateEnv(StorageModes mode = StorageModes.Incremental)
{
var fs = Helper.MemorySystem;
var registry = fs.GetStorage(fs.GetSubTemp());
var settings = new StorageCoreSettings
{
StorageRegistry = registry,
Drive = registry.DefaultDrive,
Format = StorageFormats.Binary,
Mode = mode,
};
var candleBuilderProvider = new CandleBuilderProvider(registry.ExchangeInfoProvider);
var secId = new SecurityId { SecurityCode = "TEST", BoardCode = BoardCodes.Test };
return (settings, candleBuilderProvider, secId);
}
private static ExecutionMessage CreateTick(SecurityId secId, DateTime serverTime, long tradeId, decimal price, decimal volume = 1m) => new()
{
SecurityId = secId,
DataTypeEx = DataType.Ticks,
ServerTime = serverTime,
TradeId = tradeId,
TradePrice = price,
TradeVolume = volume,
};
private static ExecutionMessage CreateOrderLogTrade(SecurityId secId, DateTime serverTime, long tradeId, decimal price, decimal volume = 1m) => new()
{
SecurityId = secId,
DataTypeEx = DataType.OrderLog,
ServerTime = serverTime,
TransactionId = tradeId,
SeqNum = tradeId,
OrderId = tradeId,
OrderState = OrderStates.Done,
OrderPrice = price,
OrderVolume = volume,
Side = Sides.Buy,
TradeId = tradeId,
TradePrice = price,
TradeVolume = volume,
};
private static void AssertTimeFrameCandle(
TimeFrameCandleMessage candle,
SecurityId secId,
TimeSpan tf,
DateTime openTime,
DateTime closeTime,
decimal open,
decimal high,
decimal low,
decimal close,
decimal totalVolume,
int? totalTicks,
CandleStates state,
DataType buildFrom,
long transactionId)
{
candle.SecurityId.AssertEqual(secId);
candle.DataType.GetTimeFrame().AssertEqual(tf);
candle.OpenTime.AssertEqual(openTime);
candle.CloseTime.AssertEqual(closeTime);
candle.OpenPrice.AssertEqual(open);
candle.HighPrice.AssertEqual(high);
candle.LowPrice.AssertEqual(low);
candle.ClosePrice.AssertEqual(close);
candle.TotalVolume.AssertEqual(totalVolume);
if (totalTicks is null)
candle.TotalTicks.AssertNull();
else
{
candle.TotalTicks.AssertNotNull();
candle.TotalTicks.Value.AssertEqual(totalTicks.Value);
}
candle.State.AssertEqual(state);
if (buildFrom is null)
candle.BuildFrom.AssertNull();
else
candle.BuildFrom.AssertEqual(buildFrom);
candle.OriginalTransactionId.AssertEqual(transactionId);
candle.SubscriptionId.AssertEqual(transactionId);
candle.OfflineMode.AssertEqual(MessageOfflineModes.Ignore);
}
private static Level1ChangeMessage CreateLevel1(SecurityId secId, DateTime serverTime, decimal? lastPrice = null, decimal? lastVolume = null, decimal? bid = null, decimal? bidVol = null, decimal? ask = null, decimal? askVol = null)
{
var msg = new Level1ChangeMessage
{
SecurityId = secId,
ServerTime = serverTime,
};
if (lastPrice != null)
msg.Add(Level1Fields.LastTradePrice, lastPrice.Value);
if (lastVolume != null)
msg.Add(Level1Fields.LastTradeVolume, lastVolume.Value);
if (bid != null)
msg.Add(Level1Fields.BestBidPrice, bid.Value);
if (bidVol != null)
msg.Add(Level1Fields.BestBidVolume, bidVol.Value);
if (ask != null)
msg.Add(Level1Fields.BestAskPrice, ask.Value);
if (askVol != null)
msg.Add(Level1Fields.BestAskVolume, askVol.Value);
return msg;
}
private static QuoteChangeMessage CreateQuotes(SecurityId secId, DateTime serverTime, decimal bid, decimal ask) => new()
{
SecurityId = secId,
ServerTime = serverTime,
Bids = [new QuoteChange(bid, 1)],
Asks = [new QuoteChange(ask, 1)],
};
private static TimeFrameCandleMessage CreateTfCandle(SecurityId secId, DateTime openTime, TimeSpan tf, decimal open, decimal high, decimal low, decimal close, decimal volume, int? totalTicks = null, IEnumerable<CandlePriceLevel> priceLevels = null) => new()
{
SecurityId = secId,
TypedArg = tf,
OpenTime = openTime,
HighTime = openTime,
LowTime = openTime,
CloseTime = openTime + tf,
OpenPrice = open,
HighPrice = high,
LowPrice = low,
ClosePrice = close,
TotalVolume = volume,
TotalTicks = totalTicks,
State = CandleStates.Finished,
DataType = tf.TimeFrame(),
PriceLevels = priceLevels,
};
[TestMethod]
public async Task FromNull_ReturnsNull_AndSendsNothing()
{
var (settings, provider, secId) = CreateEnv();
var output = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1,
IsSubscribe = true,
SecurityId = secId,
DataType2 = DataType.Ticks,
From = null,
}, context))
output.Add(msg);
context.HasData.AssertFalse();
output.Count.AssertEqual(0);
}
[TestMethod]
public async Task Ticks_Load_RespectsSkipAndCount()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
var ticks = new[]
{
CreateTick(secId, date.AddMinutes(0), tradeId: 1, price: 100),
CreateTick(secId, date.AddMinutes(1), tradeId: 2, price: 101),
CreateTick(secId, date.AddMinutes(2), tradeId: 3, price: 102),
};
await settings.GetStorage<ExecutionMessage>(secId, DataType.Ticks).SaveAsync(ticks, token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 100,
IsSubscribe = true,
SecurityId = secId,
DataType2 = DataType.Ticks,
From = date,
To = date.AddMinutes(10),
BuildMode = MarketDataBuildModes.Load,
Skip = 1,
Count = 1,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
context.LastDate.AssertEqual(ticks[1].ServerTime);
context.Left.AssertEqual(0L);
outMessages.Count.AssertEqual(2);
outMessages[0].AssertOfType<SubscriptionResponseMessage>();
((SubscriptionResponseMessage)outMessages[0]).OriginalTransactionId.AssertEqual(100);
outMessages[1].AssertOfType<ExecutionMessage>();
var tick = (ExecutionMessage)outMessages[1];
tick.TradeId.AssertEqual(2L);
tick.OfflineMode.AssertEqual(MessageOfflineModes.Ignore);
tick.OriginalTransactionId.AssertEqual(100L);
tick.SubscriptionId.AssertEqual(100L);
}
[TestMethod]
public async Task Level1_BuildFromMarketDepth_BuildsAndSends()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<QuoteChangeMessage>(secId, DataType.MarketDepth).SaveAsync(
[
CreateQuotes(secId, date.AddMinutes(0), bid: 99, ask: 101),
CreateQuotes(secId, date.AddMinutes(1), bid: 100, ask: 102),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 200,
IsSubscribe = true,
SecurityId = secId,
DataType2 = DataType.Level1,
BuildMode = MarketDataBuildModes.Build,
BuildFrom = DataType.MarketDepth,
From = date.AddMinutes(-1),
To = date.AddMinutes(10),
Count = 10,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
outMessages.OfType<SubscriptionResponseMessage>().Count().AssertEqual(1);
var level1 = outMessages.OfType<Level1ChangeMessage>().ToArray();
level1.Length.AssertEqual(2);
level1.All(m => m.SubscriptionId == 200 && m.OriginalTransactionId == 200 && m.OfflineMode == MessageOfflineModes.Ignore).AssertTrue();
level1[0].TryGetDecimal(Level1Fields.BestBidPrice).AssertEqual(99m);
level1[0].TryGetDecimal(Level1Fields.BestAskPrice).AssertEqual(101m);
level1[1].TryGetDecimal(Level1Fields.BestBidPrice).AssertEqual(100m);
level1[1].TryGetDecimal(Level1Fields.BestAskPrice).AssertEqual(102m);
}
[TestMethod]
public async Task MarketDepth_BuildFromLevel1_BuildsAndSends()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<Level1ChangeMessage>(secId, DataType.Level1).SaveAsync(
[
CreateLevel1(secId, date.AddMinutes(0), bid: 99, bidVol: 1, ask: 101, askVol: 2),
CreateLevel1(secId, date.AddMinutes(1), bid: 100, bidVol: 1, ask: 102, askVol: 2),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 300,
IsSubscribe = true,
SecurityId = secId,
DataType2 = DataType.MarketDepth,
BuildMode = MarketDataBuildModes.Build,
BuildFrom = DataType.Level1,
From = date.AddMinutes(-1),
To = date.AddMinutes(10),
Count = 10,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
outMessages.OfType<SubscriptionResponseMessage>().Count().AssertEqual(1);
var depths = outMessages.OfType<QuoteChangeMessage>().ToArray();
depths.Length.AssertEqual(2);
depths.All(m => m.SubscriptionId == 300 && m.OriginalTransactionId == 300 && m.OfflineMode == MessageOfflineModes.Ignore).AssertTrue();
depths[0].Bids.Single().Price.AssertEqual(99m);
depths[0].Bids.Single().Volume.AssertEqual(1m);
depths[0].Asks.Single().Price.AssertEqual(101m);
depths[0].Asks.Single().Volume.AssertEqual(2m);
depths[1].Bids.Single().Price.AssertEqual(100m);
depths[1].Bids.Single().Volume.AssertEqual(1m);
depths[1].Asks.Single().Price.AssertEqual(102m);
depths[1].Asks.Single().Volume.AssertEqual(2m);
}
[TestMethod]
public async Task Ticks_BuildFromLevel1_BuildsAndSends()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<Level1ChangeMessage>(secId, DataType.Level1).SaveAsync(
[
CreateLevel1(secId, date.AddSeconds(0), lastPrice: 100, lastVolume: 1, bid: 99, ask: 101),
CreateLevel1(secId, date.AddSeconds(30), lastPrice: 101, lastVolume: 2, bid: 100, ask: 102),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 400,
IsSubscribe = true,
SecurityId = secId,
DataType2 = DataType.Ticks,
BuildMode = MarketDataBuildModes.Build,
BuildFrom = DataType.Level1,
From = date.AddMinutes(-1),
To = date.AddMinutes(10),
Count = 10,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
outMessages.OfType<SubscriptionResponseMessage>().Count().AssertEqual(1);
var ticks = outMessages.OfType<ExecutionMessage>().ToArray();
ticks.Length.AssertEqual(2);
ticks.All(m => m.SubscriptionId == 400 && m.OriginalTransactionId == 400 && m.OfflineMode == MessageOfflineModes.Ignore).AssertTrue();
ticks[0].TradePrice.AssertEqual(100m);
ticks[0].TradeVolume.AssertEqual(1m);
ticks[1].TradePrice.AssertEqual(101m);
ticks[1].TradeVolume.AssertEqual(2m);
}
[TestMethod]
public async Task Candles_BuildFromTicks_BuildsAndSends()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<ExecutionMessage>(secId, DataType.Ticks).SaveAsync(
[
CreateTick(secId, date.AddSeconds(0), tradeId: 1, price: 100, volume: 10),
CreateTick(secId, date.AddSeconds(30), tradeId: 2, price: 101, volume: 20),
CreateTick(secId, date.AddSeconds(60), tradeId: 3, price: 99, volume: 30),
CreateTick(secId, date.AddSeconds(90), tradeId: 4, price: 102, volume: 40),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
var tf = TimeSpan.FromMinutes(1);
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 500,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Build,
BuildFrom = DataType.Ticks,
From = date.AddMinutes(-1),
To = date.AddMinutes(10),
Count = 10,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
outMessages.OfType<SubscriptionResponseMessage>().Count().AssertEqual(1);
var candles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
candles.Length.AssertEqual(2);
AssertTimeFrameCandle(candles[0], secId, tf, date, date.AddSeconds(30), 100, 101, 100, 101, totalVolume: 30, totalTicks: 2, CandleStates.Finished, DataType.Ticks, transactionId: 500);
AssertTimeFrameCandle(candles[1], secId, tf, date.AddMinutes(1), date.AddSeconds(90), 99, 102, 99, 102, totalVolume: 70, totalTicks: 2, CandleStates.Active, DataType.Ticks, transactionId: 500);
}
[TestMethod]
public async Task Candles_BuildFromLevel1_SpreadMiddle_BuildsAndSends()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<Level1ChangeMessage>(secId, DataType.Level1).SaveAsync(
[
CreateLevel1(secId, date.AddSeconds(0), bid: 99, bidVol: 1, ask: 101, askVol: 1),
CreateLevel1(secId, date.AddSeconds(30), bid: 100, bidVol: 1, ask: 102, askVol: 1),
CreateLevel1(secId, date.AddSeconds(60), bid: 98, bidVol: 1, ask: 100, askVol: 1),
CreateLevel1(secId, date.AddSeconds(90), bid: 99, bidVol: 1, ask: 101, askVol: 1),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
var tf = TimeSpan.FromMinutes(1);
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 600,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Build,
BuildFrom = DataType.Level1,
BuildField = Level1Fields.SpreadMiddle,
From = date.AddMinutes(-1),
To = date.AddMinutes(10),
Count = 10,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
outMessages.OfType<SubscriptionResponseMessage>().Count().AssertEqual(1);
var candles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
candles.Length.AssertEqual(2);
AssertTimeFrameCandle(candles[0], secId, tf, date, date.AddSeconds(30), 100, 101, 100, 101, totalVolume: 2, totalTicks: 2, CandleStates.Finished, DataType.MarketDepth, transactionId: 600);
AssertTimeFrameCandle(candles[1], secId, tf, date.AddMinutes(1), date.AddSeconds(90), 99, 100, 99, 100, totalVolume: 2, totalTicks: 2, CandleStates.Active, DataType.MarketDepth, transactionId: 600);
}
[TestMethod]
public async Task Candles_BuildFromMarketDepth_DefaultField_BuildsAndSends()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<QuoteChangeMessage>(secId, DataType.MarketDepth).SaveAsync(
[
CreateQuotes(secId, date.AddSeconds(0), bid: 99, ask: 101),
CreateQuotes(secId, date.AddSeconds(30), bid: 100, ask: 102),
CreateQuotes(secId, date.AddSeconds(60), bid: 98, ask: 100),
CreateQuotes(secId, date.AddSeconds(90), bid: 99, ask: 101),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
var tf = TimeSpan.FromMinutes(1);
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 700,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Build,
BuildFrom = DataType.MarketDepth,
From = date.AddMinutes(-1),
To = date.AddMinutes(10),
Count = 10,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
outMessages.OfType<SubscriptionResponseMessage>().Count().AssertEqual(1);
var candles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
candles.Length.AssertEqual(2);
AssertTimeFrameCandle(candles[0], secId, tf, date, date.AddSeconds(30), 100, 101, 100, 101, totalVolume: 2, totalTicks: 2, CandleStates.Finished, DataType.MarketDepth, transactionId: 700);
AssertTimeFrameCandle(candles[1], secId, tf, date.AddMinutes(1), date.AddSeconds(90), 99, 100, 99, 100, totalVolume: 2, totalTicks: 2, CandleStates.Active, DataType.MarketDepth, transactionId: 700);
}
[TestMethod]
public async Task Candles_BuildFromOrderLog_LastTradePrice_BuildsAndSends()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<ExecutionMessage>(secId, DataType.OrderLog).SaveAsync(
[
CreateOrderLogTrade(secId, date.AddSeconds(0), tradeId: 1, price: 100, volume: 10),
CreateOrderLogTrade(secId, date.AddSeconds(30), tradeId: 2, price: 101, volume: 20),
CreateOrderLogTrade(secId, date.AddSeconds(60), tradeId: 3, price: 99, volume: 30),
CreateOrderLogTrade(secId, date.AddSeconds(90), tradeId: 4, price: 102, volume: 40),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
var tf = TimeSpan.FromMinutes(1);
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 800,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Build,
BuildFrom = DataType.OrderLog,
From = date.AddMinutes(-1),
To = date.AddMinutes(10),
Count = 10,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
outMessages.OfType<SubscriptionResponseMessage>().Count().AssertEqual(1);
var candles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
candles.Length.AssertEqual(2);
AssertTimeFrameCandle(candles[0], secId, tf, date, date.AddSeconds(30), 100, 101, 100, 101, totalVolume: 30, totalTicks: 2, CandleStates.Finished, DataType.OrderLog, transactionId: 800);
AssertTimeFrameCandle(candles[1], secId, tf, date.AddMinutes(1), date.AddSeconds(90), 99, 102, 99, 102, totalVolume: 70, totalTicks: 2, CandleStates.Active, DataType.OrderLog, transactionId: 800);
}
[TestMethod]
public async Task Candles_Load_VolumeProfileFilter_FiltersOutCandlesWithoutPriceLevels()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var tf = TimeSpan.FromMinutes(1);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<CandleMessage>(secId, tf.TimeFrame()).SaveAsync(
[
CreateTfCandle(secId, date.AddMinutes(0), tf, 100, 101, 99, 100, 10, totalTicks: 1, priceLevels: null),
CreateTfCandle(secId, date.AddMinutes(1), tf, 100, 102, 98, 101, 20, totalTicks: 2, priceLevels: [new CandlePriceLevel { Price = 100m, TotalVolume = 20m }]),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 900,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Load,
IsCalcVolumeProfile = true,
From = date,
To = date.AddMinutes(10),
Count = 10,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
var candles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
candles.Length.AssertEqual(1);
AssertTimeFrameCandle(candles[0], secId, tf, date.AddMinutes(1), date.AddMinutes(2), 100, 102, 98, 101, totalVolume: 20, totalTicks: 2, CandleStates.Finished, buildFrom: null, transactionId: 900);
candles[0].PriceLevels.AssertNotNull();
var levels = candles[0].PriceLevels.ToArray();
levels.Length.AssertEqual(1);
levels[0].Price.AssertEqual(100m);
levels[0].TotalVolume.AssertEqual(20m);
}
[TestMethod]
public async Task Candles_LoadAndBuild_LoadsFromStorage_ThenBuildsRemaining()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var tf = TimeSpan.FromMinutes(5);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<CandleMessage>(secId, tf.TimeFrame()).SaveAsync(
[
CreateTfCandle(secId, date.AddMinutes(0), tf, 100, 105, 95, 102, 1000, totalTicks: 100),
], token);
await settings.GetStorage<ExecutionMessage>(secId, DataType.Ticks).SaveAsync(
[
CreateTick(secId, date.AddMinutes(5), tradeId: 1, price: 103, volume: 1),
CreateTick(secId, date.AddMinutes(6), tradeId: 2, price: 104, volume: 2),
CreateTick(secId, date.AddMinutes(9), tradeId: 3, price: 101, volume: 3),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1000,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.LoadAndBuild,
BuildFrom = DataType.Ticks,
AllowBuildFromSmallerTimeFrame = false,
From = date,
To = date.AddMinutes(10),
Count = 2,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
var candles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
candles.Length.AssertEqual(2);
AssertTimeFrameCandle(candles[0], secId, tf, date, date.AddMinutes(5), 100, 105, 95, 102, totalVolume: 1000, totalTicks: 100, CandleStates.Finished, buildFrom: null, transactionId: 1000);
AssertTimeFrameCandle(candles[1], secId, tf, date.AddMinutes(5), date.AddMinutes(9), 103, 104, 101, 101, totalVolume: 6, totalTicks: 3, CandleStates.Active, DataType.Ticks, transactionId: 1000);
}
[TestMethod]
public async Task Candles_AllowBuildFromSmallerTimeFrame_BuildsHigherTimeFrameFromSmaller()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var smallTf = TimeSpan.FromMinutes(1);
var bigTf = TimeSpan.FromMinutes(5);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
var oneMinCandles = Enumerable.Range(0, 10)
.Select(i => CreateTfCandle(secId, date.AddMinutes(i), smallTf, 100 + i, 101 + i, 99 + i, 100 + i, 100))
.Cast<CandleMessage>()
.ToArray();
await settings.GetStorage<CandleMessage>(secId, smallTf.TimeFrame()).SaveAsync(oneMinCandles, token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1100,
IsSubscribe = true,
SecurityId = secId,
DataType2 = bigTf.TimeFrame(),
AllowBuildFromSmallerTimeFrame = true,
BuildMode = MarketDataBuildModes.Load,
From = date,
To = date.AddMinutes(10),
Count = 10,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
var bigCandles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
bigCandles.Length.AssertEqual(2);
// the minutes carry no count of trades, so neither do the candles built of them
AssertTimeFrameCandle(bigCandles[0], secId, bigTf, date, date.AddMinutes(4), 100, 105, 99, 104, totalVolume: 500, totalTicks: null, CandleStates.Finished, smallTf.TimeFrame(), transactionId: 1100);
AssertTimeFrameCandle(bigCandles[1], secId, bigTf, date.AddMinutes(5), date.AddMinutes(9), 105, 110, 104, 109, totalVolume: 500, totalTicks: null, CandleStates.Finished, smallTf.TimeFrame(), transactionId: 1100);
}
[TestMethod]
public async Task Candles_LoadAndBuild_AllowBuildFromSmallerTimeFrame_LoadsFromSmaller_ThenBuildsFromTicks()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var smallTf = TimeSpan.FromMinutes(1);
var bigTf = TimeSpan.FromMinutes(5);
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
await settings.GetStorage<CandleMessage>(secId, smallTf.TimeFrame()).SaveAsync(
[
CreateTfCandle(secId, date.AddMinutes(0), smallTf, 100, 110, 90, 105, 10),
CreateTfCandle(secId, date.AddMinutes(1), smallTf, 105, 108, 100, 102, 20),
CreateTfCandle(secId, date.AddMinutes(2), smallTf, 102, 112, 101, 111, 30),
CreateTfCandle(secId, date.AddMinutes(3), smallTf, 111, 115, 109, 110, 40),
CreateTfCandle(secId, date.AddMinutes(4), smallTf, 110, 113, 107, 108, 50),
], token);
await settings.GetStorage<ExecutionMessage>(secId, DataType.Ticks).SaveAsync(
[
CreateTick(secId, date.AddMinutes(5), tradeId: 1, price: 200, volume: 2),
CreateTick(secId, date.AddMinutes(5).AddSeconds(20), tradeId: 2, price: 198, volume: 1),
CreateTick(secId, date.AddMinutes(6), tradeId: 3, price: 205, volume: 3),
CreateTick(secId, date.AddMinutes(9).AddSeconds(50), tradeId: 4, price: 202, volume: 4),
], token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1200,
IsSubscribe = true,
SecurityId = secId,
DataType2 = bigTf.TimeFrame(),
AllowBuildFromSmallerTimeFrame = true,
BuildMode = MarketDataBuildModes.LoadAndBuild,
BuildFrom = DataType.Ticks,
From = date,
To = date.AddMinutes(10),
Count = 2,
}, context, token))
outMessages.Add(msg);
context.HasData.AssertTrue();
var candles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
candles.Length.AssertEqual(2);
// 10:00 candle is loaded via buildable storage from 1m candles, 10:05 candle is built from ticks.
AssertTimeFrameCandle(candles[0], secId, bigTf, date, date.AddMinutes(4), 100, 115, 90, 108, totalVolume: 150, totalTicks: null, CandleStates.Finished, smallTf.TimeFrame(), transactionId: 1200);
AssertTimeFrameCandle(candles[1], secId, bigTf, date.AddMinutes(5), date.AddMinutes(9).AddSeconds(50), 200, 205, 198, 202, totalVolume: 10, totalTicks: 4, CandleStates.Active, DataType.Ticks, transactionId: 1200);
}
[TestMethod]
public async Task EarlyTermination_Break_UpdatesContext()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv();
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
var ticks = new[]
{
CreateTick(secId, date.AddMinutes(0), tradeId: 1, price: 100),
CreateTick(secId, date.AddMinutes(1), tradeId: 2, price: 101),
CreateTick(secId, date.AddMinutes(2), tradeId: 3, price: 102),
CreateTick(secId, date.AddMinutes(3), tradeId: 4, price: 103),
CreateTick(secId, date.AddMinutes(4), tradeId: 5, price: 104),
};
await settings.GetStorage<ExecutionMessage>(secId, DataType.Ticks).SaveAsync(ticks, token);
var context = new StorageLoadContext();
var receivedCount = 0;
DateTime? lastReceivedTime = null;
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1,
IsSubscribe = true,
SecurityId = secId,
DataType2 = DataType.Ticks,
From = date,
To = date.AddHours(1),
}, context, token))
{
if (msg is ExecutionMessage exec)
{
lastReceivedTime = exec.ServerTime;
receivedCount++;
// Early termination after receiving 2 ticks
if (receivedCount >= 2)
break;
}
}
// Context should be updated despite early termination
context.HasData.AssertTrue();
context.LastDate.AssertNotNull();
context.LastDate.Value.AssertEqual(lastReceivedTime.Value);
}
[TestMethod]
public async Task EarlyTermination_Dispose_UpdatesContext()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv();
var date = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
var ticks = new[]
{
CreateTick(secId, date.AddMinutes(0), tradeId: 1, price: 100),
CreateTick(secId, date.AddMinutes(1), tradeId: 2, price: 101),
CreateTick(secId, date.AddMinutes(2), tradeId: 3, price: 102),
};
await settings.GetStorage<ExecutionMessage>(secId, DataType.Ticks).SaveAsync(ticks, token);
var context = new StorageLoadContext();
DateTime? lastReceivedTime = null;
await using (var enumerator = settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1,
IsSubscribe = true,
SecurityId = secId,
DataType2 = DataType.Ticks,
From = date,
To = date.AddHours(1),
}, context, token).GetAsyncEnumerator(token))
{
// Move to first message (SubscriptionResponseMessage)
await enumerator.MoveNextAsync();
// Move to first tick
await enumerator.MoveNextAsync();
if (enumerator.Current is ExecutionMessage exec)
lastReceivedTime = exec.ServerTime;
// Dispose without completing enumeration
}
// Context should be updated on Dispose
context.HasData.AssertTrue();
context.LastDate.AssertNotNull();
context.LastDate.Value.AssertEqual(lastReceivedTime.Value);
}
[TestMethod]
public async Task Candles_Load_CountWithoutFrom_ReturnsLastN()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var tf = TimeSpan.FromMinutes(5);
var d1 = new DateTime(2025, 1, 1, 10, 0, 0, DateTimeKind.Utc);
var d2 = new DateTime(2025, 1, 10, 10, 0, 0, DateTimeKind.Utc);
var d3 = new DateTime(2025, 1, 20, 10, 0, 0, DateTimeKind.Utc);
CandleMessage[] Day(DateTime day, decimal basePrice) => [.. Enumerable.Range(0, 4)
.Select(i => CreateTfCandle(secId, day.AddMinutes(5 * i), tf, basePrice + i, basePrice + i + 1, basePrice + i - 1, basePrice + i, 10, totalTicks: 1))
.Cast<CandleMessage>()];
var storage = settings.GetStorage<CandleMessage>(secId, tf.TimeFrame());
await storage.SaveAsync(Day(d1, 100), token); // 100..103
await storage.SaveAsync(Day(d2, 200), token); // 200..203
await storage.SaveAsync(Day(d3, 300), token); // 300..303
var outMessages = new List<Message>();
var context = new StorageLoadContext();
// A count request with no start date must return the last Count candles,
// walking storage back across the empty gaps between the three days.
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1300,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Load,
From = null,
Count = 6,
}, context, token))
outMessages.Add(msg);
var candles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
// Last 6 of [100..103, 200..203, 300..303] = 202, 203, 300, 301, 302, 303.
candles.Length.AssertEqual(6);
for (var i = 1; i < candles.Length; i++)
(candles[i].OpenTime > candles[i - 1].OpenTime).AssertTrue();
candles[0].OpenPrice.AssertEqual(202m);
candles[^1].OpenPrice.AssertEqual(303m);
}
[TestMethod]
public async Task Candles_Load_CountWithoutFrom_FewerThanCount_ReturnsAll()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var tf = TimeSpan.FromMinutes(5);
var date = new DateTime(2025, 3, 1, 10, 0, 0, DateTimeKind.Utc);
var candlesToSave = Enumerable.Range(0, 3)
.Select(i => CreateTfCandle(secId, date.AddMinutes(5 * i), tf, 100 + i, 101 + i, 99 + i, 100 + i, 10, totalTicks: 1))
.Cast<CandleMessage>()
.ToArray();
await settings.GetStorage<CandleMessage>(secId, tf.TimeFrame()).SaveAsync(candlesToSave, token);
var outMessages = new List<Message>();
var context = new StorageLoadContext();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1301,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Load,
From = null,
Count = 50, // more than exist
}, context, token))
outMessages.Add(msg);
var candles = outMessages.OfType<TimeFrameCandleMessage>().ToArray();
candles.Length.AssertEqual(3);
candles[0].OpenPrice.AssertEqual(100m);
candles[^1].OpenPrice.AssertEqual(102m);
}
// A chart that scrolls into its past asks for the candles before the first one it holds: a count and an end, with
// no start. The end falls inside a day, and the candles of that day after it are no part of the answer.
private async Task<TimeFrameCandleMessage[]> LoadCountBefore(int count, int dayOfEnd, int minuteOfEnd)
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var tf = TimeSpan.FromMinutes(5);
CandleMessage[] Day(DateTime day, decimal basePrice) => [.. Enumerable.Range(0, 8)
.Select(i => CreateTfCandle(secId, day.AddMinutes(5 * i), tf, basePrice + i, basePrice + i + 1, basePrice + i - 1, basePrice + i, 10, totalTicks: 1))
.Cast<CandleMessage>()];
var storage = settings.GetStorage<CandleMessage>(secId, tf.TimeFrame());
await storage.SaveAsync(Day(new DateTime(2025, 1, 10, 10, 0, 0, DateTimeKind.Utc), 200), token); // 200..207
await storage.SaveAsync(Day(new DateTime(2025, 1, 20, 10, 0, 0, DateTimeKind.Utc), 300), token); // 300..307
var outMessages = new List<Message>();
await foreach (var msg in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1303,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Load,
From = null,
To = new DateTime(2025, 1, dayOfEnd, 10, minuteOfEnd, 0, DateTimeKind.Utc),
Count = count,
}, new StorageLoadContext(), token))
outMessages.Add(msg);
return [.. outMessages.OfType<TimeFrameCandleMessage>()];
}
[TestMethod]
public async Task Candles_Load_CountBeforeAMomentOfTheDay_ReturnsTheLastNBeforeIt()
{
// The end is the 10:10 candle of the second day: 300, 301 and 302 open no later.
var candles = await LoadCountBefore(3, 20, 10);
candles.Select(c => c.OpenPrice).ToArray().AssertEqual([300m, 301m, 302m]);
}
[TestMethod]
public async Task Candles_Load_CountBeforeAMomentOfTheDay_WalksBackIntoTheDaysBefore()
{
// Three of the second day open no later than 10:10; the other two come from the end of the first day.
var candles = await LoadCountBefore(5, 20, 10);
candles.Select(c => c.OpenPrice).ToArray().AssertEqual([206m, 207m, 300m, 301m, 302m]);
}
[TestMethod]
public async Task Candles_Load_CountWithDefaultTo_Throws()
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var tf = TimeSpan.FromMinutes(5);
var date = new DateTime(2025, 1, 20, 10, 0, 0, DateTimeKind.Utc);
var candles = Enumerable.Range(0, 4)
.Select(i => CreateTfCandle(secId, date.AddMinutes(5 * i), tf, 300 + i, 301 + i, 299 + i, 300 + i, 10, totalTicks: 1))
.Cast<CandleMessage>()
.ToArray();
await settings.GetStorage<CandleMessage>(secId, tf.TimeFrame()).SaveAsync(candles, token);
// A set time bound must be valid. A To of default(DateTime) (0001-01-01) is a caller bug —
// an unset nullable To must be null, not the zero value — so the load fails loudly instead
// of silently clipping the count-from-end window to nothing.
await ThrowsAsync<ArgumentException>(async () =>
{
await foreach (var _ in settings.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1302,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Load,
From = null,
To = default(DateTime),
Count = 6,
}, new StorageLoadContext(), token))
{
}
});
}
// The three days the read-counting tests fill with candles, from 10 February 2025.
private static DateTime WholeDay(int index) => new DateTime(2025, 2, 10, 0, 0, 0, DateTimeKind.Utc).AddDays(index);
// Three whole days of five-minute candles, 288 a day, read back through a drive that tells how much of each day was
// read: the last Count of them, before the given end or before none. They are asked for as stored, or as built from
// smaller time frames where any are held, which is how a request asks for candles unless it says otherwise.
private async Task<(TimeFrameCandleMessage[] candles, ReadCountingDrive drive)> LoadLastOfWholeDays(int count, DateTime? to, bool buildFromSmaller)
{
var token = CancellationToken;
var (settings, provider, secId) = CreateEnv(StorageModes.Incremental);
var tf = TimeSpan.FromMinutes(5);
var storage = settings.GetStorage<CandleMessage>(secId, tf.TimeFrame());
for (var day = 0; day < 3; day++)
{
var basePrice = 1000m * (day + 1);
await storage.SaveAsync([.. Enumerable.Range(0, 288)
.Select(i => CreateTfCandle(secId, WholeDay(day).AddMinutes(5 * i), tf, basePrice + i, basePrice + i + 1, basePrice + i - 1, basePrice + i, 10, totalTicks: 1))
.Cast<CandleMessage>()], token);
}
var drive = new ReadCountingDrive(settings.Drive);
var reading = new StorageCoreSettings
{
StorageRegistry = settings.StorageRegistry,
Drive = drive,
Format = settings.Format,
Mode = settings.Mode,
};
var outMessages = new List<Message>();
await foreach (var msg in reading.LoadMessagesAsync(provider, new MarketDataMessage
{
TransactionId = 1304,
IsSubscribe = true,
SecurityId = secId,
DataType2 = tf.TimeFrame(),
BuildMode = MarketDataBuildModes.Load,
AllowBuildFromSmallerTimeFrame = buildFromSmaller,
From = null,
To = to,
Count = count,
}, new StorageLoadContext(), token))
outMessages.Add(msg);
return ([.. outMessages.OfType<TimeFrameCandleMessage>()], drive);
}
private static void AssertReadOnce(ReadCountingDrive drive, DateTime day)
=> (drive.ReadOf(day) < 2 * drive.LengthOf(day)).AssertTrue($"{day:yyyy-MM-dd}: {drive.ReadOf(day)} bytes read of a {drive.LengthOf(day)}-byte file.");
[TestMethod]
[DataRow(false)]
[DataRow(true)]
public async Task Candles_Load_CountWithoutFrom_ReadsEachDayOfTheWindowOnce(bool buildFromSmaller)
{
// The last day whole and the last 112 candles of the day before it, from 14:40 on.
var (candles, drive) = await LoadLastOfWholeDays(400, null, buildFromSmaller);
candles.Length.AssertEqual(400);
candles[0].OpenTime.AssertEqual(WholeDay(1).AddMinutes(5 * 176));
candles[^1].OpenTime.AssertEqual(WholeDay(2).AddMinutes(5 * 287));
// A day counted by its header is read by the load alone, and a day read to be counted is not read again.
AssertReadOnce(drive, WholeDay(1));
AssertReadOnce(drive, WholeDay(2));
}
[TestMethod]
[DataRow(false)]
[DataRow(true)]
public async Task Candles_Load_CountBeforeAMoment_ReadsEachDayOfTheWindowOnce(bool buildFromSmaller)
{
// 121 candles of the last day open no later than 10:00; the other 179 are the end of the day before, from 09:05 on.
var (candles, drive) = await LoadLastOfWholeDays(300, WholeDay(2).AddHours(10), buildFromSmaller);
candles.Length.AssertEqual(300);
candles[0].OpenTime.AssertEqual(WholeDay(1).AddMinutes(5 * 109));
candles[^1].OpenTime.AssertEqual(WholeDay(2).AddHours(10));
AssertReadOnce(drive, WholeDay(1));
AssertReadOnce(drive, WholeDay(2));
}
}