mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 08:32:23 +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.
176 lines
5.0 KiB
C#
176 lines
5.0 KiB
C#
namespace StockSharp.Algo;
|
|
|
|
/// <summary>
|
|
/// Fill gaps by <see cref="IFillGapsBehaviour"/> message adapter.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Initializes a new instance of the <see cref="FillGapsMessageAdapter"/>.
|
|
/// </remarks>
|
|
/// <param name="innerAdapter">The adapter, to which messages will be directed.</param>
|
|
/// <param name="behaviour"><see cref="IFillGapsBehaviour"/>.</param>
|
|
public class FillGapsMessageAdapter(IMessageAdapter innerAdapter, IFillGapsBehaviour behaviour) : MessageAdapterWrapper(innerAdapter)
|
|
{
|
|
private class FillGapInfo(ISubscriptionMessage original, SecurityId secId, FillGapsDays days)
|
|
{
|
|
public ISubscriptionMessage Original { get; } = original;
|
|
public SecurityId SecId { get; } = secId;
|
|
public FillGapsDays Days { get; } = days;
|
|
public ISubscriptionMessage Current { get; set; }
|
|
public bool ResponseSent { get; set; }
|
|
}
|
|
|
|
private readonly SynchronizedDictionary<long, FillGapInfo> _gapsRequests = [];
|
|
private readonly IFillGapsBehaviour _behaviour = behaviour ?? throw new ArgumentNullException(nameof(behaviour));
|
|
|
|
/// <inheritdoc />
|
|
protected override async ValueTask OnSendInMessageAsync(Message message, CancellationToken cancellationToken)
|
|
{
|
|
switch (message.Type)
|
|
{
|
|
case MessageTypes.Reset:
|
|
{
|
|
_gapsRequests.Clear();
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
if (message is ISubscriptionMessage subscrMsg)
|
|
{
|
|
if (subscrMsg.IsSubscribe)
|
|
{
|
|
if (subscrMsg.FillGaps is FillGapsDays days &&
|
|
subscrMsg.From is DateTime from &&
|
|
subscrMsg is ISecurityIdMessage secIdMsg &&
|
|
!secIdMsg.IsAllSecurity())
|
|
{
|
|
var (gapsStart, gapsEnd) = await _behaviour.TryGetNextGapAsync(secIdMsg.SecurityId, subscrMsg.DataType, from, subscrMsg.To ?? CurrentTime, days, cancellationToken);
|
|
|
|
if (gapsStart is null)
|
|
break;
|
|
|
|
var current = subscrMsg.TypedClone();
|
|
|
|
current.From = gapsStart.Value;
|
|
current.To = gapsEnd.Value;
|
|
current.FillGaps = null;
|
|
|
|
_gapsRequests.Add(subscrMsg.TransactionId, new(subscrMsg.TypedClone(), secIdMsg.SecurityId, days) { Current = current });
|
|
|
|
message = (Message)current;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_gapsRequests.Remove(subscrMsg.OriginalTransactionId);
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
await base.OnSendInMessageAsync(message, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override async ValueTask OnInnerAdapterNewOutMessageAsync(Message message, CancellationToken cancellationToken)
|
|
{
|
|
switch (message.Type)
|
|
{
|
|
case MessageTypes.SubscriptionFinished:
|
|
{
|
|
var finished = (SubscriptionFinishedMessage)message;
|
|
|
|
if (!_gapsRequests.TryGetValue(finished.OriginalTransactionId, out var info))
|
|
break;
|
|
|
|
await ProcessSubscriptionFinishedAsync(finished, info, cancellationToken);
|
|
return;
|
|
}
|
|
case MessageTypes.SubscriptionOnline:
|
|
{
|
|
var online = (SubscriptionOnlineMessage)message;
|
|
_gapsRequests.Remove(online.OriginalTransactionId);
|
|
break;
|
|
}
|
|
case MessageTypes.SubscriptionResponse:
|
|
{
|
|
var response = (SubscriptionResponseMessage)message;
|
|
|
|
if (!_gapsRequests.TryGetValue(response.OriginalTransactionId, out var info))
|
|
break;
|
|
|
|
if (!response.IsOk())
|
|
_gapsRequests.Remove(response.OriginalTransactionId);
|
|
else
|
|
{
|
|
if (info.ResponseSent)
|
|
return;
|
|
|
|
info.ResponseSent = true;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
await base.OnInnerAdapterNewOutMessageAsync(message, cancellationToken);
|
|
}
|
|
|
|
private async ValueTask ProcessSubscriptionFinishedAsync(SubscriptionFinishedMessage finished, FillGapInfo info, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var current = info.Current;
|
|
|
|
var (gapsStart, gapsEnd) = await _behaviour.TryGetNextGapAsync(info.SecId, info.Original.DataType, current.To.Value.Date.AddDays(1), info.Original.To ?? CurrentTime, info.Days, cancellationToken);
|
|
|
|
if (gapsStart is null)
|
|
{
|
|
_gapsRequests.Remove(finished.OriginalTransactionId);
|
|
|
|
if (info.Original.To is null)
|
|
{
|
|
var original = info.Original.TypedClone();
|
|
|
|
original.From = null;
|
|
original.FillGaps = null;
|
|
|
|
original.LoopBack(this);
|
|
await RaiseNewOutMessageAsync((Message)original, cancellationToken);
|
|
}
|
|
else
|
|
{
|
|
await base.OnInnerAdapterNewOutMessageAsync(finished, cancellationToken);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
current = current.TypedClone();
|
|
|
|
current.From = gapsStart.Value;
|
|
current.To = gapsEnd.Value;
|
|
|
|
info.Current = current;
|
|
|
|
current.LoopBack(this);
|
|
await RaiseNewOutMessageAsync((Message)current, cancellationToken);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.AddErrorLog(ex);
|
|
await base.OnInnerAdapterNewOutMessageAsync(finished, cancellationToken);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a copy of <see cref="FillGapsMessageAdapter"/>.
|
|
/// </summary>
|
|
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
|
|
/// <returns>Copy.</returns>
|
|
public override async ValueTask<IMessageAdapter> CloneAsync(CancellationToken cancellationToken)
|
|
=> new FillGapsMessageAdapter(await InnerAdapter.CloneAsync(cancellationToken), _behaviour);
|
|
}
|