mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 00:21:31 +00:00
Five things in the core promised behaviour nothing held them to, and the suite was green on all of them for one reason: it checked that a value is carried correctly, never that it changes an outcome. A percent activation price is a distance from the market, and the contract says the server snapshots the price at registration. Nothing read the flag, so the raw number became the trigger - a buy stop written as "five percent" fired on the first print and a sell stop written the same way rested at 5 and never fired at all. The only test naming the flag round-tripped it through serialization, which passed precisely because the value is stored correctly. Its two sibling flags each had a behavioural test; this one never got one. It is now snapshotted against the last price the venue printed - the only stream a stop ever fires against - with registration refused outright when there is nothing to snapshot. Take profit turns out to have carried none of the three flags, so both halves of the protective pair go through one path, and a structural test walks the interface so a fourth flag cannot be added without being honoured. The GPU comparison allowed 2.5% of the compared value, five orders of magnitude above what float32 can lose. It is now formed from the epsilon, the window a kernel accumulates over and the scale it works at - and five kernels turn out to compute something else. Jurik seeded its cascades on the first bar where the CPU seeds them for the whole formation window; Ichimoku trimmed its Senkou B store to the Kijun length, returning the previous bar's value whenever the two were equal; VWAP accumulated a whole series in float32; SuperTrend and NickRypockTrailingReverse pick a branch by comparing a price to a level, which float32 rounds more coarsely than the step between them. A candle builder's Timeout was a public setting no code read, its mechanism commented out - gone, with nothing in any repo setting it. The pair strategy template declared two securities and traded one - it trades both now. And Sharpe and Sortino scored five returns spread over a year exactly as they scored the same five over five days. The class declared Period and derived the annualisation from it, so it always promised equal spans, but counted every PnL report as one period and ignored the time it carried. Returns now close on that grid: an idle period is a return of zero, a period opened with no capital records none, and the tail waits for its own boundary. Reading time at all exposed two further things - a report stamped with the default date would have opened the grid in year 0001 and buried a run under seven hundred thousand empty periods, measured at 382 times too low on every optimiser iteration, and Period could be changed while a grid was open. Figures from before are not comparable: a strategy that reports rarely now scores lower, which is the point. The suite itself was deciding results by how loaded the machine was. Eighteen waits had no deadline and hung rather than failed, seventy-two more stood in for a condition with a sleep, and twenty-two method timeouts were no longer than the wait inside them. What remains is the kind that must stay: a window held open to prove something does not happen, a delay that is itself under test, the pacing of a feed. Eighteen files each declared the same empty TestReceiver and nine their own CreateSecurityId - those had drifted, four returning a fresh id and four a fixed one, so they were not merged blindly. StorageTests and IndicatorTests, at about 3900 and 3600 lines, are split into partial classes by theme with every fully qualified name kept. Left as a finding: the ratio grid is fed from two clocks - a position's local time on one path, the engine's market time on the other - which differ by hours in a backtest. Putting both on the connector's clock changes which timestamps MaxProfitDate and MaxDrawdownDate carry, and that is a contract decision.
231 lines
6.7 KiB
C#
231 lines
6.7 KiB
C#
namespace StockSharp.Tests;
|
|
|
|
[TestClass]
|
|
public class OrderBookIncrementManagerStateTests : BaseTestClass
|
|
{
|
|
private static readonly SecurityId _secId = Helper.CreateSecurityId();
|
|
|
|
private static QuoteChangeMessage CreateSnapshot(SecurityId securityId, long[] subscriptionIds)
|
|
{
|
|
var msg = new QuoteChangeMessage
|
|
{
|
|
SecurityId = securityId,
|
|
ServerTime = DateTime.UtcNow,
|
|
LocalTime = DateTime.UtcNow,
|
|
State = QuoteChangeStates.SnapshotComplete,
|
|
Bids = [new QuoteChange(100m, 10m)],
|
|
Asks = [new QuoteChange(101m, 20m)],
|
|
};
|
|
|
|
msg.SetSubscriptionIds(subscriptionIds);
|
|
|
|
return msg;
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AddSubscription_ContainsSubscription_ReturnsTrue()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
var logReceiver = new TestReceiver();
|
|
|
|
state.AddSubscription(1, _secId, logReceiver);
|
|
|
|
state.ContainsSubscription(1).AssertTrue();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void HasAnySubscriptions_Empty_ReturnsFalse()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
|
|
state.HasAnySubscriptions.AssertFalse();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void HasAnySubscriptions_AfterAdd_ReturnsTrue()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
var logReceiver = new TestReceiver();
|
|
|
|
state.AddSubscription(1, _secId, logReceiver);
|
|
|
|
state.HasAnySubscriptions.AssertTrue();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AddPassThrough_IsPassThrough_ReturnsTrue()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
|
|
state.AddPassThrough(1);
|
|
|
|
state.IsPassThrough(1).AssertTrue();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AddAllSecSubscription_GetAllSecIds_ReturnsId()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
|
|
state.AddAllSecSubscription(42);
|
|
|
|
var ids = state.GetAllSecSubscriptionIds();
|
|
|
|
ids.Length.AssertEqual(1);
|
|
ids[0].AssertEqual(42L);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AddAllSecPassThrough_IsAllSecPassThrough_ReturnsTrue()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
|
|
state.AddAllSecPassThrough(7);
|
|
|
|
state.IsAllSecPassThrough(7).AssertTrue();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void OnSubscriptionOnline_SharesBuilder()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
var logReceiver = new TestReceiver();
|
|
|
|
// Add two subscriptions for the same security
|
|
state.AddSubscription(1, _secId, logReceiver);
|
|
state.AddSubscription(2, _secId, logReceiver);
|
|
|
|
// Bring both online - second should share the builder of the first
|
|
state.OnSubscriptionOnline(1);
|
|
state.OnSubscriptionOnline(2);
|
|
|
|
// Apply a snapshot via subscription 1
|
|
var snapshot = CreateSnapshot(_secId, [1]);
|
|
var result = state.TryApply(1, snapshot, out var subscriptionIds);
|
|
|
|
result.AssertNotNull();
|
|
// Both subscription IDs should be returned since they share the builder
|
|
subscriptionIds.Count(id => id == 1L).AssertEqual(1);
|
|
subscriptionIds.Count(id => id == 2L).AssertEqual(1);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TryApply_AfterSnapshot_ReturnsBuiltBook()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
var logReceiver = new TestReceiver();
|
|
|
|
state.AddSubscription(1, _secId, logReceiver);
|
|
state.OnSubscriptionOnline(1);
|
|
|
|
var snapshot = CreateSnapshot(_secId, [1]);
|
|
var result = state.TryApply(1, snapshot, out var subscriptionIds);
|
|
|
|
result.AssertNotNull();
|
|
result.Bids.Length.AssertEqual(1);
|
|
result.Asks.Length.AssertEqual(1);
|
|
result.Bids[0].Price.AssertEqual(100m);
|
|
result.Bids[0].Volume.AssertEqual(10m);
|
|
result.Asks[0].Price.AssertEqual(101m);
|
|
result.Asks[0].Volume.AssertEqual(20m);
|
|
subscriptionIds.AssertNotNull();
|
|
subscriptionIds.Count(id => id == 1L).AssertEqual(1);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RemoveSubscription_ContainsSubscription_ReturnsFalse()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
var logReceiver = new TestReceiver();
|
|
|
|
state.AddSubscription(1, _secId, logReceiver);
|
|
state.RemoveSubscription(1).AssertTrue();
|
|
|
|
state.ContainsSubscription(1).AssertFalse();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TryApply_AllSecSubscription_OnlyBuildsBookDynamically()
|
|
{
|
|
// Reproduces the bug: ALL@ALL subscription for MarketDepth
|
|
// registers in _allSecSubscriptions but TryApply only looks in _byId,
|
|
// so QuoteChange messages are dropped (TryApply returns null).
|
|
var state = new OrderBookIncrementManagerState();
|
|
|
|
// Subscribe ALL@ALL (no per-security subscription)
|
|
state.AddAllSecSubscription(42);
|
|
|
|
state.HasAnySubscriptions.AssertTrue();
|
|
|
|
var snapshot = CreateSnapshot(_secId, [42]);
|
|
var result = state.TryApply(42, snapshot, out var subscriptionIds);
|
|
|
|
// BUG: result is null because _byId doesn't contain subscription 42
|
|
// FIX: should dynamically create BookInfo for the security
|
|
result.AssertNotNull();
|
|
result.Bids.Length.AssertEqual(1);
|
|
result.Asks.Length.AssertEqual(1);
|
|
subscriptionIds.AssertNotNull();
|
|
subscriptionIds.SequenceEqual([42L]).AssertTrue();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TryApply_AllSecSubscription_MultipleSecurities_EachGetsOwnBuilder()
|
|
{
|
|
// Reproduces the bug: ALL subscription shares one subscriptionId for all securities.
|
|
// The old code cached _byId[subscriptionId] = first security's BookInfo,
|
|
// so all subsequent securities incorrectly got the first security's SecurityId.
|
|
var state = new OrderBookIncrementManagerState();
|
|
|
|
state.AddAllSecSubscription(42);
|
|
|
|
var secId1 = new SecurityId { SecurityCode = "SEC1", BoardCode = "TEST" };
|
|
var secId2 = new SecurityId { SecurityCode = "SEC2", BoardCode = "TEST" };
|
|
var secId3 = new SecurityId { SecurityCode = "SEC3", BoardCode = "TEST" };
|
|
|
|
// First security
|
|
var snap1 = CreateSnapshot(secId1, [42]);
|
|
var result1 = state.TryApply(42, snap1, out _);
|
|
result1.AssertNotNull();
|
|
result1.SecurityId.AssertEqual(secId1);
|
|
|
|
// Second security — must get its OWN builder, not SEC1's
|
|
var snap2 = CreateSnapshot(secId2, [42]);
|
|
var result2 = state.TryApply(42, snap2, out _);
|
|
result2.AssertNotNull();
|
|
result2.SecurityId.AssertEqual(secId2);
|
|
|
|
// Third security
|
|
var snap3 = CreateSnapshot(secId3, [42]);
|
|
var result3 = state.TryApply(42, snap3, out _);
|
|
result3.AssertNotNull();
|
|
result3.SecurityId.AssertEqual(secId3);
|
|
|
|
// Re-verify first security still works correctly
|
|
var snap1b = CreateSnapshot(secId1, [42]);
|
|
var result1b = state.TryApply(42, snap1b, out _);
|
|
result1b.AssertNotNull();
|
|
result1b.SecurityId.AssertEqual(secId1);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Clear_RemovesAll()
|
|
{
|
|
var state = new OrderBookIncrementManagerState();
|
|
var logReceiver = new TestReceiver();
|
|
|
|
state.AddSubscription(1, _secId, logReceiver);
|
|
state.AddPassThrough(2);
|
|
state.AddAllSecSubscription(3);
|
|
state.AddAllSecPassThrough(4);
|
|
|
|
state.Clear();
|
|
|
|
state.HasAnySubscriptions.AssertFalse();
|
|
state.ContainsSubscription(1).AssertFalse();
|
|
state.IsPassThrough(2).AssertFalse();
|
|
state.GetAllSecSubscriptionIds().Length.AssertEqual(0);
|
|
state.IsAllSecPassThrough(4).AssertFalse();
|
|
}
|
|
}
|