mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 00:21:31 +00:00
Instrument lookups, configuration, credentials, CSV entities, snapshots and the alert processing service are read and written asynchronously; the blocking synchronous forms are obsolete wrappers. IAsyncPersistable replaces IPersistable: every type that saves and loads its settings has LoadAsync and SaveAsync only, and nested objects are read with GetValueAsync, LoadEntireAsync and LoadAllAsync. The synchronous Load and Save are the obsolete Ecng extensions. The MultiConnect sample keeps IPersistable for two settings classes, because the settings window of the published StockSharp.Xaml package takes it. Copies that were made by saving and loading are made asynchronously. IMessageAdapter, IIndicator, the risk, commission, PnL, latency, slippage and position managers, IStorageBuffer, Strategy, DiagramElement, ICompositionModel and its nodes get CloneAsync and no longer implement ICloneable; Clone is an obsolete blocking wrapper. A strategy copy keeps every security and portfolio parameter the original holds. Messages, FieldMapping, BankDetails and WithdrawInfo are copied in memory. A CommissionRuleMessage shares its rule and the emulator takes a copy of it. Envelope gets a constructor that takes all three lines and CreateAsync. ToBruteForce and ToBruteForceRandom become ToBruteForceAsync and ToBruteForceRandomAsync and return IAsyncEnumerable; BruteForceOptimizer runs over it and takes the next strategy through an asynchronous delegate. Diagram elements keep their own state in SaveState, OnLoading and LoadState. Undo restores an in-memory snapshot; a chart element holds its series and axes in it by reference. An alert rule keeps its value in the form a message carries it. A diagram strategy applies composition settings that were loaded before the composition was set in SetCompositionAsync or at the start, and saves them while they wait. Strategies get an OnStartedAsync hook; diagram strategies resolve securities, underlyings and strikes before the start so value processing never looks up instruments. Paths.Serialize, SerializeInvariant, AppStartSettings.TrySave, ICredentialsProvider.Save, ISnapshotSerializer.Serialize and the composition registry gain async forms. ICredentialsProvider.Delete becomes DeleteAsync, so loading, saving and deleting the credentials wait for one another. Paths.InstalledVersion and AppNameWithVersion are obsolete in favour of GetInstalledVersionAsync and GetAppNameWithVersionAsync. A file is opened for writing only after the last cancellation check, because opening it empties it; a cancelled read reports the cancellation instead of an empty result. A snapshot flush that failed leaves what it took due for the next one. The indicator templates for C#, F# and Python override LoadAsync and SaveAsync.
178 lines
6.3 KiB
C#
178 lines
6.3 KiB
C#
namespace StockSharp.Tests;
|
|
|
|
[TestClass]
|
|
public class BoardMappingMessageAdapterTests : BaseTestClass
|
|
{
|
|
private const string _defaultVenueBoard = "BNB";
|
|
|
|
private sealed class VenueAdapter : MessageAdapter
|
|
{
|
|
public VenueAdapter()
|
|
: base(new IncrementalIdGenerator())
|
|
{
|
|
}
|
|
|
|
public List<Message> Received { get; } = [];
|
|
|
|
protected override ValueTask OnSendInMessageAsync(Message message, CancellationToken cancellationToken)
|
|
{
|
|
Received.Add(message);
|
|
return default;
|
|
}
|
|
|
|
public ValueTask SayAsync(Message message, CancellationToken cancellationToken)
|
|
=> SendOutMessageAsync(message, cancellationToken);
|
|
|
|
public override ValueTask<IMessageAdapter> CloneAsync(CancellationToken cancellationToken)
|
|
=> new(new VenueAdapter());
|
|
}
|
|
|
|
private static (BoardMappingMessageAdapter mapped, VenueAdapter venue, List<Message> heard) Create()
|
|
{
|
|
var venue = new VenueAdapter();
|
|
|
|
var mapped = new BoardMappingMessageAdapter(venue, new Dictionary<string, string>
|
|
{
|
|
{ "BNB", "SS" },
|
|
{ "BNB_FUT", "SS_FUT" },
|
|
{ "BNBCN", "SS_CN" },
|
|
}, _defaultVenueBoard);
|
|
|
|
var heard = new List<Message>();
|
|
((IMessageAdapter)mapped).NewOutMessageAsync += (m, ct) => { heard.Add(m); return default; };
|
|
|
|
return (mapped, venue, heard);
|
|
}
|
|
|
|
private static SecurityId Id(string code, string board) => new() { SecurityCode = code, BoardCode = board };
|
|
|
|
[TestMethod]
|
|
public async Task ClientOrder_ReachesTheVenueOnItsOwnBoard()
|
|
{
|
|
var (mapped, venue, _) = Create();
|
|
|
|
await mapped.SendInMessageAsync(new OrderRegisterMessage { SecurityId = Id("ETHBTC", "SS") }, CancellationToken);
|
|
|
|
var sent = (OrderRegisterMessage)venue.Received.Single();
|
|
sent.SecurityId.BoardCode.AssertEqual("BNB");
|
|
sent.SecurityId.SecurityCode.AssertEqual("ETHBTC", "only the board is ours");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task VenueQuote_ReachesTheClientOnOurBoard()
|
|
{
|
|
var (_, venue, heard) = Create();
|
|
|
|
await venue.SayAsync(new Level1ChangeMessage { SecurityId = Id("ETHBTC", "BNB") }, CancellationToken);
|
|
|
|
var quote = (Level1ChangeMessage)heard.Single();
|
|
quote.SecurityId.BoardCode.AssertEqual("SS");
|
|
quote.SecurityId.SecurityCode.AssertEqual("ETHBTC");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task EveryBoardNamedKeepsItsOwnPair()
|
|
{
|
|
var (mapped, venue, heard) = Create();
|
|
|
|
await venue.SayAsync(new Level1ChangeMessage { SecurityId = Id("ETHBTC", "BNB_FUT") }, CancellationToken);
|
|
await venue.SayAsync(new Level1ChangeMessage { SecurityId = Id("ETHUSD", "BNBCN") }, CancellationToken);
|
|
|
|
((Level1ChangeMessage)heard[0]).SecurityId.BoardCode.AssertEqual("SS_FUT");
|
|
((Level1ChangeMessage)heard[1]).SecurityId.BoardCode.AssertEqual("SS_CN");
|
|
|
|
await mapped.SendInMessageAsync(new OrderRegisterMessage { SecurityId = Id("ETHBTC", "SS_FUT") }, CancellationToken);
|
|
await mapped.SendInMessageAsync(new OrderRegisterMessage { SecurityId = Id("ETHUSD", "SS_CN") }, CancellationToken);
|
|
|
|
((OrderRegisterMessage)venue.Received[0]).SecurityId.BoardCode.AssertEqual("BNB_FUT");
|
|
((OrderRegisterMessage)venue.Received[1]).SecurityId.BoardCode.AssertEqual("BNBCN");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AnUnknownBoardGoesToTheVenueOnItsDefault()
|
|
{
|
|
var (mapped, venue, _) = Create();
|
|
|
|
await mapped.SendInMessageAsync(new OrderRegisterMessage { SecurityId = Id("ETHBTC", "SOMETHING") }, CancellationToken);
|
|
await mapped.SendInMessageAsync(new OrderRegisterMessage { SecurityId = Id("ETHBTC", string.Empty) }, CancellationToken);
|
|
|
|
((OrderRegisterMessage)venue.Received[0]).SecurityId.BoardCode.AssertEqual(_defaultVenueBoard);
|
|
((OrderRegisterMessage)venue.Received[1]).SecurityId.BoardCode.AssertEqual(_defaultVenueBoard);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ABoardTheVenueNamesButWeDoNot_IsShownAsItIs()
|
|
{
|
|
var (_, venue, heard) = Create();
|
|
|
|
// Naming it as one of ours would offer a market we cannot route back.
|
|
await venue.SayAsync(new Level1ChangeMessage { SecurityId = Id("ETHBTC", "BNB_OPT") }, CancellationToken);
|
|
|
|
((Level1ChangeMessage)heard.Single()).SecurityId.BoardCode.AssertEqual("BNB_OPT");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task SpellingOfTheBoardDoesNotMatter()
|
|
{
|
|
var (mapped, venue, heard) = Create();
|
|
|
|
await venue.SayAsync(new Level1ChangeMessage { SecurityId = Id("ETHBTC", "bnb_fut") }, CancellationToken);
|
|
((Level1ChangeMessage)heard.Single()).SecurityId.BoardCode.AssertEqual("SS_FUT");
|
|
|
|
await mapped.SendInMessageAsync(new OrderRegisterMessage { SecurityId = Id("ETHBTC", "ss") }, CancellationToken);
|
|
((OrderRegisterMessage)venue.Received.Single()).SecurityId.BoardCode.AssertEqual("BNB");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task MessageWithoutAnInstrument_PassesBothWays()
|
|
{
|
|
var (mapped, venue, heard) = Create();
|
|
|
|
await mapped.SendInMessageAsync(new TimeMessage(), CancellationToken);
|
|
await venue.SayAsync(new ConnectMessage(), CancellationToken);
|
|
|
|
venue.Received.Single().Type.AssertEqual(MessageTypes.Time);
|
|
heard.Single().Type.AssertEqual(MessageTypes.Connect);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task EveryInstrumentCarryingMessageIsMapped()
|
|
{
|
|
var (_, venue, heard) = Create();
|
|
|
|
await venue.SayAsync(new ExecutionMessage { DataTypeEx = DataType.Transactions, SecurityId = Id("ETHBTC", "BNB") }, CancellationToken);
|
|
await venue.SayAsync(new SecurityMessage { SecurityId = Id("BTCUSDT", "BNB_FUT") }, CancellationToken);
|
|
|
|
((ExecutionMessage)heard[0]).SecurityId.BoardCode.AssertEqual("SS");
|
|
((SecurityMessage)heard[1]).SecurityId.BoardCode.AssertEqual("SS_FUT");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task WhatWentDownComesBackTheSame()
|
|
{
|
|
var (mapped, venue, heard) = Create();
|
|
|
|
var original = Id("ETHBTC", "SS_FUT");
|
|
|
|
await mapped.SendInMessageAsync(new OrderRegisterMessage { SecurityId = original }, CancellationToken);
|
|
|
|
var asTheVenueSawIt = ((OrderRegisterMessage)venue.Received.Single()).SecurityId;
|
|
await venue.SayAsync(new ExecutionMessage { DataTypeEx = DataType.Transactions, SecurityId = asTheVenueSawIt }, CancellationToken);
|
|
|
|
((ExecutionMessage)heard.Single()).SecurityId.AssertEqual(original);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ACopyMapsTheSameWay()
|
|
{
|
|
var (mapped, _, _) = Create();
|
|
|
|
var clone = (BoardMappingMessageAdapter)await mapped.CloneAsync(CancellationToken);
|
|
|
|
await clone.SendInMessageAsync(new OrderRegisterMessage { SecurityId = Id("ETHBTC", "SS_FUT") }, CancellationToken);
|
|
|
|
var venue = (VenueAdapter)clone.InnerAdapter;
|
|
((OrderRegisterMessage)venue.Received.Single()).SecurityId.BoardCode.AssertEqual("BNB_FUT");
|
|
}
|
|
}
|