Files
StockSharp/Algo/Connector_ProcessMessage.cs
mika@stocksharp.com 9540565603 Connector: a request for orders it already holds no longer puts an order back to an older state
Somebody who asks a connector for orders or positions it already holds is
answered from its cache: the subscription comes online without data of its own,
and the entities of the cache are sent to it as messages. That answer was built
by an adapter in the chain, at the moment the reply passed through it, while the
cache is changed later - when the connector takes messages off its out channel.
A change of an order still waiting in that channel was therefore followed by an
answer built before it, and applying the answer put the order back to what it
had been: an order partly filled while a second subscription was coming online
kept its old balance until the venue mentioned it again. Every connector asks
for orders by itself on connect, so each further request - an orders table, a
strategy - is such a second subscription, and after a reconnect they all come
online together.

The answer for orders and positions is now built by the connector itself, where
it applies messages to the cache and in the same order. The adapter in the chain
keeps answering market data, which the adapters above it still have to see, and
the orders an adapter above it asks for by itself: the filtered order book
learns of the account's own orders that way, and its request never passes the
connector.
2026-10-12 12:44:06 +03:00

1642 lines
44 KiB
C#

namespace StockSharp.Algo;
using StockSharp.Algo.Risk;
partial class Connector
{
private readonly Lock _marketTimerSync = new();
private ControllablePeriodicTimer _marketTimer;
private readonly TimeMessage _marketTimeMessage = new();
private bool _isMarketTimeHandled;
private void CreateTimer()
{
using (_marketTimerSync.EnterScope())
{
_isMarketTimeHandled = true;
if (_marketTimer != null)
return;
_marketTimer = AsyncHelper
.CreatePeriodicTimer(async () =>
{
try
{
// TimeMsg required for notify invoke CurrentTimeChanged event (and active time based IMarketRule-s)
// No need to put _marketTimeMessage again, if it still in queue.
using (_marketTimerSync.EnterScope())
{
if (_marketTimer == null || !_isMarketTimeHandled)
return;
_isMarketTimeHandled = false;
}
_marketTimeMessage.LocalTime = CurrentTime;
await SendOutMessageAsync(_marketTimeMessage, default);
}
catch (Exception ex)
{
LogError(ex);
}
})
.Start(MarketTimeChangedInterval);
}
}
private void CloseTimer()
{
using (_marketTimerSync.EnterScope())
{
_marketTimer?.Dispose();
_marketTimer = null;
_isMarketTimeHandled = false;
}
}
/// <inheritdoc />
public bool CanConnect => Adapter.InnerAdapters.SortedAdapters.Any();
private readonly ResetMessage _disposeMessage = new();
private ValueTask AdapterOnNewOutMessage(Message message, CancellationToken cancellationToken)
{
if (message.IsBack())
{
//message.IsBack = false;
// lookup messages now sends in BasketMessageAdapter
// nested subscription ignores by Connector
//
//if (message.Type == MessageTypes.MarketData)
//{
// var mdMsg = (MarketDataMessage)message;
// var security = !mdMsg.DataType.IsSecurityRequired() ? null : GetSecurity(mdMsg.SecurityId);
// _subscriptionManager.ProcessRequest(security, mdMsg, true);
//}
//else
if (message.Type == MessageTypes.OrderGroupCancel)
{
var cancelMsg = (OrderGroupCancelMessage)message;
// offline (back) and risk managers can generate the message
_entityCache.TryAddMassCancelationId(cancelMsg.TransactionId);
}
return SendInMessageAsync(message, cancellationToken);
}
return SendOutMessageAsync(message, cancellationToken);
}
private IMessageChannel _inMessageChannel;
/// <summary>
/// Input message channel.
/// </summary>
public IMessageChannel InMessageChannel
{
get => _inMessageChannel;
protected set
{
if (value == _inMessageChannel)
return;
if (_inMessageChannel != null)
{
_inMessageChannel.NewOutMessageAsync -= InMessageChannelOnNewOutMessage;
_inMessageChannel.Dispose();
}
_inMessageChannel = value;
if (_inMessageChannel != null)
_inMessageChannel.NewOutMessageAsync += InMessageChannelOnNewOutMessage;
}
}
private IMessageChannel _outMessageChannel;
/// <summary>
/// Outgoing message channel.
/// </summary>
public IMessageChannel OutMessageChannel
{
get => _outMessageChannel;
protected set
{
if (value == _outMessageChannel)
return;
if (_outMessageChannel != null)
{
_outMessageChannel.NewOutMessageAsync -= OutMessageChannelOnNewOutMessage;
_outMessageChannel.Dispose();
}
_outMessageChannel = value;
if (_outMessageChannel != null)
_outMessageChannel.NewOutMessageAsync += OutMessageChannelOnNewOutMessage;
}
}
/// <summary>
/// What of the cache one of the two places a request passes answers with.
/// </summary>
private sealed class CacheSnapshots(ISnapshotHolder cache, Func<ISubscriptionMessage, bool> isAnswered) : ISnapshotHolder
{
IEnumerable<Message> ISnapshotHolder.GetSnapshot(ISubscriptionMessage subscription)
{
if (subscription is null)
throw new ArgumentNullException(nameof(subscription));
return isAnswered(subscription) ? cache.GetSnapshot(subscription) : [];
}
}
private static bool IsState(ISubscriptionMessage subscription)
=> subscription.DataType == DataType.Transactions || subscription.DataType == DataType.PositionChanges;
private async ValueTask InMessageChannelOnNewOutMessage(Message message, CancellationToken cancellationToken)
{
if (SupportSnapshots)
_stateSnapshots.ProcessInMessage(message);
await (_inAdapter?.SendInMessageAsync(message, cancellationToken) ?? default);
if (message != _disposeMessage)
return;
InMessageChannel = null;
Adapter = null;
OutMessageChannel = null;
}
private ValueTask OutMessageChannelOnNewOutMessage(Message message, CancellationToken cancellationToken)
{
if (!SupportSnapshots)
return OnProcessMessage(message, cancellationToken);
var (forward, snapshots) = _stateSnapshots.ProcessOutMessage(message);
return snapshots.Length == 0
? OnProcessMessage(forward, cancellationToken)
: ProcessWithSnapshotsAsync(forward, snapshots, cancellationToken);
}
private async ValueTask ProcessWithSnapshotsAsync(Message message, Message[] snapshots, CancellationToken cancellationToken)
{
await OnProcessMessage(message, cancellationToken);
foreach (var snapshot in snapshots)
await OnProcessMessage(snapshot, cancellationToken);
}
private IMessageAdapterWrapper _inAdapter;
/// <summary>
/// Inner message adapter.
/// </summary>
public IMessageAdapterWrapper InnerAdapter
{
get => _inAdapter;
set
{
if (_inAdapter == value)
return;
if (_inAdapter != null)
{
_inAdapter.NewOutMessageAsync -= AdapterOnNewOutMessage;
}
if (_adapter != null)
{
_adapter.InnerAdapters.Added -= InnerAdaptersOnAdded;
_adapter.InnerAdapters.Removed -= InnerAdaptersOnRemoved;
_adapter.InnerAdapters.Cleared -= InnerAdaptersOnCleared;
}
_inAdapter = value;
_adapter = null;
StorageAdapter = null;
if (_inAdapter == null)
return;
var adapter = _inAdapter as IMessageAdapterWrapper;
while (adapter != null)
{
if (adapter is StorageMetaInfoMessageAdapter storage)
StorageAdapter = storage;
if (adapter.InnerAdapter is BasketMessageAdapter basket)
_adapter = basket;
adapter = adapter.InnerAdapter as IMessageAdapterWrapper;
}
if (_adapter != null)
{
_subscriptionManager.TransactionIdGenerator = _adapter.TransactionIdGenerator;
_adapter.InnerAdapters.Added += InnerAdaptersOnAdded;
_adapter.InnerAdapters.Removed += InnerAdaptersOnRemoved;
_adapter.InnerAdapters.Cleared += InnerAdaptersOnCleared;
foreach (var inner in _adapter.InnerAdapters)
InnerAdaptersOnAdded(inner);
}
_inAdapter.NewOutMessageAsync += AdapterOnNewOutMessage;
}
}
private BasketMessageAdapter _adapter;
/// <summary>
/// Message adapter.
/// </summary>
public BasketMessageAdapter Adapter
{
get => _adapter;
protected set
{
if (!_isDisposing && value == null)
throw new ArgumentNullException(nameof(value));
if (_adapter == value)
return;
if (_adapter != null)
{
_adapter.InnerAdapters.Added -= InnerAdaptersOnAdded;
_adapter.InnerAdapters.Removed -= InnerAdaptersOnRemoved;
_adapter.InnerAdapters.Cleared -= InnerAdaptersOnCleared;
//SendInMessage(new ResetMessage());
_inAdapter.NewOutMessageAsync -= AdapterOnNewOutMessage;
_inAdapter.Dispose();
//if (_inAdapter != _adapter)
// _adapter.Dispose();
}
_adapter = value;
_inAdapter = _adapter;
if (_adapter != null)
{
_subscriptionManager.TransactionIdGenerator = _adapter.TransactionIdGenerator;
_adapter.InnerAdapters.Added += InnerAdaptersOnAdded;
_adapter.InnerAdapters.Removed += InnerAdaptersOnRemoved;
_adapter.InnerAdapters.Cleared += InnerAdaptersOnCleared;
_adapter.Parent = this;
//_inAdapter = new ChannelMessageAdapter(_inAdapter, InMessageChannel, OutMessageChannel)
//{
// //OwnOutputChannel = true,
// OwnInnerAdapter = true
//};
if (RiskManager != null)
_inAdapter = new RiskMessageAdapter(_inAdapter, RiskManager) { OwnInnerAdapter = true };
if (SecurityStorage != null && StorageRegistry != null)
{
_inAdapter = StorageAdapter = new StorageMetaInfoMessageAdapter(_inAdapter, SecurityStorage, PositionStorage, StorageRegistry.ExchangeInfoProvider, _adapter.StorageProcessor)
{
OwnInnerAdapter = true,
OverrideSecurityData = OverrideSecurityData
};
}
if (Buffer != null)
_inAdapter = new BufferMessageAdapter(_inAdapter, _adapter.StorageSettings, Buffer, SnapshotRegistry);
if (SupportBasketSecurities)
_inAdapter = new BasketSecurityMessageAdapter(_inAdapter, this, BasketSecurityProcessorProvider, ExchangeInfoProvider) { OwnInnerAdapter = true };
if (SupportSnapshots)
_inAdapter = new SnapshotHolderMessageAdapter(_inAdapter, _chainSnapshots) { OwnInnerAdapter = true };
if (SupportAssociatedSecurity)
_inAdapter = new AssociatedSecurityAdapter(_inAdapter) { OwnInnerAdapter = true };
if (SupportFilteredMarketDepth)
_inAdapter = new FilteredMarketDepthAdapter(_inAdapter) { OwnInnerAdapter = true };
_inAdapter.NewOutMessageAsync += AdapterOnNewOutMessage;
}
}
}
private bool _supportBasketSecurities;
/// <summary>
/// Use <see cref="BasketSecurityMessageAdapter"/>.
/// </summary>
public bool SupportBasketSecurities
{
get => _supportBasketSecurities;
set
{
if (_supportBasketSecurities == value)
return;
if (value)
EnableAdapter(
a => new BasketSecurityMessageAdapter(a, this, BasketSecurityProcessorProvider, ExchangeInfoProvider) { OwnInnerAdapter = true },
typeof(BufferMessageAdapter),
typeof(SnapshotHolderMessageAdapter), typeof(AssociatedSecurityAdapter), typeof(FilteredMarketDepthAdapter));
else
DisableAdapter<BasketSecurityMessageAdapter>();
_supportBasketSecurities = value;
}
}
private bool _supportFilteredMarketDepth;
/// <summary>
/// Use <see cref="FilteredMarketDepthAdapter"/>.
/// </summary>
public bool SupportFilteredMarketDepth
{
get => _supportFilteredMarketDepth;
set
{
if (_supportFilteredMarketDepth == value)
return;
if (value)
EnableAdapter(a => new FilteredMarketDepthAdapter(a) { OwnInnerAdapter = true }, typeof(AssociatedSecurityAdapter));
else
DisableAdapter<FilteredMarketDepthAdapter>();
_supportFilteredMarketDepth = value;
}
}
private bool _supportSnapshots = true;
/// <summary>
/// Use <see cref="SnapshotHolderMessageAdapter"/>.
/// </summary>
public virtual bool SupportSnapshots
{
get => _supportSnapshots;
set
{
if (_supportSnapshots == value)
return;
if (value)
EnableAdapter(
a => new SnapshotHolderMessageAdapter(a, _chainSnapshots) { OwnInnerAdapter = true },
typeof(BasketSecurityMessageAdapter),
typeof(AssociatedSecurityAdapter), typeof(FilteredMarketDepthAdapter));
else
DisableAdapter<SnapshotHolderMessageAdapter>();
_supportSnapshots = value;
}
}
private bool _supportAssociatedSecurity;
/// <summary>
/// Use <see cref="AssociatedSecurityAdapter"/>.
/// </summary>
public bool SupportAssociatedSecurity
{
get => _supportAssociatedSecurity;
set
{
if (_supportAssociatedSecurity == value)
return;
if (value)
EnableAdapter(
a => new AssociatedSecurityAdapter(a) { OwnInnerAdapter = true },
typeof(SnapshotHolderMessageAdapter),
typeof(FilteredMarketDepthAdapter));
else
DisableAdapter<AssociatedSecurityAdapter>();
_supportAssociatedSecurity = value;
}
}
/// <summary>
/// Use <see cref="Level1DepthBuilderAdapter"/>.
/// </summary>
[Obsolete("Use MarketDataMessage.BuildFrom property.")]
public bool SupportLevel1DepthBuilder { get; set; }
/// <summary>
/// Storage buffer.
/// </summary>
public IStorageBuffer Buffer { get; }
private (IMessageAdapter prev, IMessageAdapter adapter, IMessageAdapter next) GetAdapter(Type type)
{
var adapter = _inAdapter;
if (adapter == null)
return default;
var prev = adapter?.InnerAdapter;
var next = (IMessageAdapter)null;
while (true)
{
if (adapter.GetType() == type)
return (prev, adapter, next);
next = adapter;
adapter = prev as IMessageAdapterWrapper;
if (adapter == null)
return default;
prev = adapter?.InnerAdapter;
}
}
private (IMessageAdapter prev, IMessageAdapter adapter, IMessageAdapter next) GetAdapter<T>()
where T : IMessageAdapterWrapper
{
return GetAdapter(typeof(T));
}
private void EnableAdapter(Func<IMessageAdapter, IMessageAdapterWrapper> create, Type innerType, params Type[] outerTypes)
{
if (_inAdapter == null)
return;
var tuple = innerType != null ? GetAdapter(innerType) : default;
var adapter = tuple.adapter;
if (adapter != null)
{
//if (after)
//{
if (tuple.next is IMessageAdapterWrapper nextWrapper)
nextWrapper.InnerAdapter = create(adapter);
else
AddAdapter(create);
//}
//else
//{
// var prevWrapper = tuple.prev;
// var nextWrapper = adapter as IMessageAdapterWrapper;
// if (prevWrapper == null)
// throw new InvalidOperationException("Adapter wrapper cannot be added to the beginning of the chain.");
// if (nextWrapper == null)
// throw new InvalidOperationException(LocalizedStrings.TypeNotImplemented.Put(adapter.GetType(), nameof(IMessageAdapterWrapper)));
// nextWrapper.InnerAdapter = create(prevWrapper);
//}
}
else
{
foreach (var outerType in outerTypes)
{
if (GetAdapter(outerType).adapter is not IMessageAdapterWrapper outer)
continue;
outer.InnerAdapter = create(outer.InnerAdapter);
return;
}
AddAdapter(create);
}
}
private void AddAdapter(Func<IMessageAdapter, IMessageAdapterWrapper> create)
{
_inAdapter.NewOutMessageAsync -= AdapterOnNewOutMessage;
_inAdapter = create(_inAdapter);
_inAdapter.NewOutMessageAsync += AdapterOnNewOutMessage;
}
private void DisableAdapter<T>()
where T : IMessageAdapterWrapper
{
var tuple = GetAdapter<T>();
if (tuple == default)
return;
var adapterWrapper = (MessageAdapterWrapper)tuple.adapter;
var nextWrapper = (MessageAdapterWrapper)tuple.next;
if (nextWrapper == null)
{
adapterWrapper.NewOutMessageAsync -= AdapterOnNewOutMessage;
_inAdapter = (IMessageAdapterWrapper)adapterWrapper.InnerAdapter;
_inAdapter.NewOutMessageAsync += AdapterOnNewOutMessage;
}
else
nextWrapper.InnerAdapter = adapterWrapper.InnerAdapter;
adapterWrapper.OwnInnerAdapter = false;
adapterWrapper.Dispose();
}
private void InnerAdaptersOnAdded(IMessageAdapter adapter)
{
if (adapter.IsTransactional())
TransactionAdapter = adapter;
if (adapter.IsMarketData())
MarketDataAdapter = adapter;
}
private void InnerAdaptersOnRemoved(IMessageAdapter adapter)
{
if (TransactionAdapter == adapter)
TransactionAdapter = null;
if (MarketDataAdapter == adapter)
MarketDataAdapter = null;
}
private void InnerAdaptersOnCleared()
{
TransactionAdapter = null;
MarketDataAdapter = null;
}
/// <inheritdoc />
public IMessageAdapter TransactionAdapter { get; private set; }
/// <inheritdoc />
public IMessageAdapter MarketDataAdapter { get; private set; }
/// <summary>
/// Storage adapter.
/// </summary>
public StorageMetaInfoMessageAdapter StorageAdapter { get; private set; }
private ValueTask SendMessage(IMessageChannel channel, Message message, CancellationToken cancellationToken)
{
if (channel is null)
return default;
message.TryInitLocalTime(this);
if (!channel.IsOpened())
channel.Open();
return channel.SendInMessageAsync(message, cancellationToken);
}
/// <inheritdoc />
public ValueTask SendInMessageAsync(Message message, CancellationToken cancellationToken)
=> SendMessage(InMessageChannel, message, cancellationToken);
/// <inheritdoc />
public ValueTask SendOutMessageAsync(Message message, CancellationToken cancellationToken)
=> SendMessage(OutMessageChannel, message, cancellationToken);
/// <inheritdoc />
[Obsolete("Use SendInMessageAsync instead.")]
public void SendInMessage(Message message)
=> _ = SendInMessageAsync(message, default);
/// <inheritdoc />
[Obsolete("Use SendOutMessageAsync instead.")]
public void SendOutMessage(Message message)
=> _ = SendOutMessageAsync(message, default);
/// <summary>
/// Send error message.
/// </summary>
/// <param name="error">Error details.</param>
[Obsolete("Blocking sync-over-async wrapper. Use SendOutErrorAsync instead.")]
public void SendOutError(Exception error)
=> AsyncHelper.Run(() => SendOutErrorAsync(error, default));
/// <summary>
/// Send error message.
/// </summary>
/// <param name="error">Error details.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
public ValueTask SendOutErrorAsync(Exception error, CancellationToken cancellationToken)
=> SendOutMessageAsync(error.ToErrorMessage(), cancellationToken);
/// <summary>
/// Process message.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
protected virtual async ValueTask OnProcessMessage(Message message, CancellationToken cancellationToken)
{
if (message.Type is not MessageTypes.Time and not MessageTypes.QuoteChange)
LogVerbose("BP:{0}", message);
ProcessTimeInterval(message);
await RaiseNewMessage(message, cancellationToken);
try
{
switch (message.Type)
{
case MessageTypes.Connect:
await ProcessConnectMessage((ConnectMessage)message, cancellationToken);
break;
case MessageTypes.Disconnect:
await ProcessDisconnectMessageAsync((DisconnectMessage)message, cancellationToken);
break;
case MessageTypes.ConnectionLost:
ProcessConnectionLostMessage(message);
break;
case MessageTypes.ConnectionRestored:
ProcessConnectionRestoredMessage(message);
break;
case MessageTypes.QuoteChange:
await ProcessQuotesMessage((QuoteChangeMessage)message, cancellationToken);
break;
case MessageTypes.Board:
ProcessBoardMessage((BoardMessage)message);
break;
case MessageTypes.BoardState:
ProcessBoardStateMessage((BoardStateMessage)message);
break;
case MessageTypes.Security:
await ProcessSecurityMessage((SecurityMessage)message, cancellationToken);
break;
case MessageTypes.DataTypeInfo:
ProcessDataTypeInfoMessage((DataTypeInfoMessage)message);
break;
case MessageTypes.Level1Change:
await ProcessLevel1ChangeMessage((Level1ChangeMessage)message, cancellationToken);
break;
case MessageTypes.News:
await ProcessNewsMessage((NewsMessage)message, cancellationToken);
break;
case MessageTypes.Execution:
await ProcessExecutionMessage((ExecutionMessage)message, cancellationToken);
break;
case MessageTypes.Portfolio:
ProcessPortfolioMessage((PortfolioMessage)message);
break;
case MessageTypes.PositionChange:
await ProcessPositionChangeMessage((PositionChangeMessage)message, cancellationToken);
break;
//case MessageTypes.Time:
// break;
case MessageTypes.SubscriptionResponse:
ProcessSubscriptionResponseMessage((SubscriptionResponseMessage)message);
break;
case MessageTypes.SubscriptionFinished:
await ProcessSubscriptionFinishedMessage((SubscriptionFinishedMessage)message, cancellationToken);
break;
case MessageTypes.SubscriptionOnline:
ProcessSubscriptionOnlineMessage((SubscriptionOnlineMessage)message);
break;
case MessageTypes.Error:
ProcessErrorMessage((ErrorMessage)message);
break;
case ExtendedMessageTypes.RemoveSecurity:
await ProcessSecurityRemoveMessage((SecurityRemoveMessage)message, cancellationToken);
break;
case MessageTypes.ChangePassword:
ProcessChangePasswordMessage((ChangePasswordMessage)message);
break;
default:
{
if (message is CandleMessage candleMsg)
ProcessCandleMessage(candleMsg);
else if (message is ISubscriptionIdMessage subscrMsg)
ProcessSubscriptionMessage(subscrMsg);
// если адаптеры передают специфичные сообщения
// throw new ArgumentOutOfRangeException(LocalizedStrings.UnknownType.Put(message.Type));
break;
}
}
}
catch (Exception ex)
{
RaiseError(new InvalidOperationException(LocalizedStrings.MessageCauseError.Put(message), ex));
}
}
private void ProcessSubscriptionResponseMessage(SubscriptionResponseMessage replyMsg)
{
var error = replyMsg.Error;
var subscription = _subscriptionManager.ProcessResponse(replyMsg, out var originalMsg, out var unexpectedCancelled, out var items);
if (originalMsg == null)
{
if (error != null)
RaiseError(error);
return;
}
if (originalMsg is MarketDataMessage mdMdg)
{
if (originalMsg.IsSubscribe)
{
if (replyMsg.IsOk())
RaiseMarketDataSubscriptionSucceeded(mdMdg, subscription);
else
{
if (unexpectedCancelled)
RaiseMarketDataUnexpectedCancelled(mdMdg, error ?? new NotSupportedException(LocalizedStrings.SubscriptionNotSupported.Put(originalMsg)), subscription);
else
RaiseMarketDataSubscriptionFailed(mdMdg, replyMsg, subscription);
}
}
else
{
if (replyMsg.IsOk())
RaiseMarketDataUnSubscriptionSucceeded(mdMdg, subscription);
else
RaiseMarketDataUnSubscriptionFailed(mdMdg, replyMsg, subscription);
}
}
else
{
if (error == null)
RaiseSubscriptionStarted(subscription);
else
{
RaiseSubscriptionFailed(subscription, error, originalMsg.IsSubscribe);
T[] typed<T>() => items.Cast<T>().ToArray();
if (originalMsg is SecurityLookupMessage secLookup)
RaiseLookupSecuritiesResult(secLookup, error, typed<Security>());
else if (originalMsg is PortfolioLookupMessage pfLookup)
RaiseLookupPortfoliosResult(pfLookup, error, typed<Portfolio>());
}
}
}
private async ValueTask ProcessSubscriptionFinishedMessage(SubscriptionFinishedMessage message, CancellationToken cancellationToken)
{
var subscription = _subscriptionManager.ProcessSubscriptionFinishedMessage(message, out var items);
if (subscription == null)
return;
if (message.Body?.Length > 0)
{
if (subscription.DataType == DataType.Securities)
{
var secMsgs = new List<SecurityMessage>();
await foreach (var secMsg in message.Body.ExtractSecuritiesAsync().WithCancellation(cancellationToken))
{
await ProcessSecurityMessage(secMsg, cancellationToken);
secMsgs.Add(secMsg);
}
items = [.. items, .. secMsgs];
}
else if (subscription.DataType == DataType.Board)
{
var boardMsgs = new List<BoardMessage>();
await foreach (var boardMsg in message.Body.ExtractBoardsAsync().WithCancellation(cancellationToken))
{
ProcessBoardMessage(boardMsg);
boardMsgs.Add(boardMsg);
}
items = [.. items, .. boardMsgs];
}
}
RaiseMarketDataSubscriptionFinished(message, subscription);
ProcessSubscriptionResult(subscription, items);
}
private void ProcessSubscriptionOnlineMessage(SubscriptionOnlineMessage message)
{
var subscription = _subscriptionManager.ProcessSubscriptionOnlineMessage(message, out var items);
if (subscription == null)
return;
RaiseMarketDataSubscriptionOnline(subscription);
ProcessSubscriptionResult(subscription, items);
}
/// <summary>
/// While an order book or a tick stream runs it is the authority on the Level1 fields it carries and
/// what Level1 says about them is ignored. That ends with the stream: once the last one for the
/// security is gone, Level1 is the only source left and has to report those fields again, or the
/// caller is shown the final value of a stream that ended as if it were the current market.
/// </summary>
private void ReleaseLevel1Ownership(Subscription subscription)
{
var dataType = subscription.DataType;
if (dataType != DataType.MarketDepth && dataType != DataType.Ticks)
return;
if (subscription.SecurityId is not SecurityId secId)
return;
foreach (var other in _subscriptionManager.Subscriptions)
{
if (other != subscription && other.DataType == dataType && other.SecurityId == secId && other.State.IsActive())
return;
}
_entityCache.ReleaseLevel1Ownership(secId, dataType);
}
private void ProcessSubscriptionResult(Subscription subscription, object[] items)
{
T[] typed<T>() => items.Cast<T>().ToArray();
if (subscription.SubscriptionMessage is SecurityLookupMessage secLookup)
{
RaiseLookupSecuritiesResult(secLookup, null, typed<Security>());
}
else if (subscription.SubscriptionMessage is PortfolioLookupMessage pfLookup)
{
RaiseLookupPortfoliosResult(pfLookup, null, typed<Portfolio>());
}
}
private async ValueTask ProcessSecurityRemoveMessage(SecurityRemoveMessage message, CancellationToken cancellationToken)
{
if (message == null)
throw new ArgumentNullException(nameof(message));
var securityId = message.SecurityId;
var security = await SecurityStorage.LookupByIdAsync(securityId, cancellationToken);
if (security != null)
{
await SecurityStorage.DeleteAsync(security, cancellationToken);
_removed?.Invoke([security]);
}
}
private async ValueTask ProcessConnectMessage(ConnectMessage message, CancellationToken cancellationToken)
{
var adapter = message.Adapter;
var error = message.Error;
if (error == null)
{
if (adapter == Adapter)
{
await ApplySubscriptionManagerActionsAsync(_subscriptionManager.HandleConnected(subscription => Adapter.IsMessageSupported(subscription.SubscriptionMessage.Type)), cancellationToken);
// raise event after re subscriptions cause handler on Connected event can send some subscriptions
RaiseConnected();
}
else
RaiseConnectedEx(adapter);
}
else
{
if (adapter == Adapter)
RaiseConnectionError(error);
else
RaiseConnectionErrorEx(adapter, error);
}
}
private async ValueTask ProcessDisconnectMessageAsync(DisconnectMessage message, CancellationToken cancellationToken)
{
var adapter = message.Adapter;
var error = message.Error;
if (error == null)
{
if (adapter == Adapter)
await RaiseDisconnectedAsync(cancellationToken);
else
RaiseDisconnectedEx(adapter);
}
else
{
if (adapter == Adapter)
RaiseConnectionError(error);
else
RaiseConnectionErrorEx(adapter, error);
}
}
private void ProcessConnectionLostMessage(Message message)
{
RaiseConnectionLost(message.Adapter);
}
private void ProcessConnectionRestoredMessage(Message message)
{
RaiseConnectionRestored(message.Adapter);
}
private void ProcessBoardStateMessage(BoardStateMessage message)
{
ExchangeBoard board;
if (message.BoardCode.IsEmpty())
board = null;
else
board = ExchangeInfoProvider.GetOrCreateBoard(message.BoardCode);
RaiseReceived(board, message, BoardReceived);
}
private void ProcessBoardMessage(BoardMessage message)
{
var board = ExchangeInfoProvider.GetOrCreateBoard(message.Code, out var isNew, code =>
{
var b = new ExchangeBoard
{
Code = code,
Exchange = message.ToExchange(),
};
return b.ApplyChanges(message);
});
var subscriptions = _subscriptionManager.ProcessLookupResponse(message, board);
RaiseReceived(board, subscriptions, BoardReceived);
}
private async ValueTask ProcessSecurityMessage(SecurityMessage message, CancellationToken cancellationToken)
{
var security = await GetSecurityAsync(message.SecurityId, s =>
{
if (!UpdateSecurityByDefinition)
return false;
s.ApplyChanges(message, ExchangeInfoProvider, OverrideSecurityData);
return true;
}, cancellationToken);
var subscriptions = _subscriptionManager.ProcessLookupResponse(message, security);
RaiseReceived(security, subscriptions, SecurityReceived);
}
private void ProcessDataTypeInfoMessage(DataTypeInfoMessage message)
{
var dt = message.FileDataType ?? throw new InvalidOperationException(LocalizedStrings.NoDataTypeSelected);
_subscriptionManager.ProcessLookupResponse(message, dt);
RaiseReceived(dt, message, DataTypeReceived);
}
private async ValueTask ProcessLevel1ChangeMessage(Level1ChangeMessage message, CancellationToken cancellationToken)
{
Security security = null;
if (RaiseReceived(message, message, RaiseLevel1Received, out var anyCanOnline) != true)
{
if (anyCanOnline != true)
return;
security = await EnsureGetSecurityAsync(message, cancellationToken);
if (_entityCache.HasLevel1Info(security))
return;
}
#pragma warning disable CS0618 // Type or member is obsolete
if (UpdateSecurityByLevel1)
{
security ??= await EnsureGetSecurityAsync(message, cancellationToken);
security.ApplyChanges(message);
}
#pragma warning restore CS0618 // Type or member is obsolete
if (ValuesChanged is not null)
{
security ??= await EnsureGetSecurityAsync(message, cancellationToken);
var time = message.ServerTime;
var info = _entityCache.GetSecurityValues(security, time);
var changes = message.Changes;
var cloned = false;
foreach (var change in message.Changes)
{
var field = change.Key;
if (!info.CanLastTrade && field.IsLastTradeField())
{
if (!cloned)
{
changes = changes.ToDictionary();
cloned = true;
}
changes.Remove(field);
continue;
}
if (!info.CanBestQuotes && (field.IsBestBidField() || field.IsBestAskField()))
{
if (!cloned)
{
changes = changes.ToDictionary();
cloned = true;
}
changes.Remove(field);
continue;
}
info.SetValue(time, field, change.Value);
}
if (changes.Count > 0)
RaiseValuesChanged(security, changes, message.ServerTime, message.LocalTime);
}
}
/// <inheritdoc />
public Portfolio LookupByPortfolioName(string name) => GetPortfolio(name, null, out _);
/// <summary>
/// To get the portfolio by the code name.
/// </summary>
/// <param name="name">Portfolio code name.</param>
/// <returns>The got portfolio. If there is no portfolio by given criteria, <see langword="null" /> is returned.</returns>
public Portfolio GetPortfolio(string name) => LookupByPortfolioName(name);
private Portfolio GetPortfolio(string name, Func<Portfolio, bool> changePortfolio, out bool isNew)
{
if (name.IsEmpty())
throw new ArgumentNullException(nameof(name));
var portfolio = PositionStorage.GetOrCreatePortfolio(name,
key => new Portfolio { Name = key } ?? throw new InvalidOperationException(LocalizedStrings.PortfolioNotCreated.Put(name)),
out isNew);
var isChanged = false;
if (changePortfolio != null)
isChanged = changePortfolio(portfolio);
if (_existingPortfolios.TryAdd(portfolio))
{
LogInfo(LocalizedStrings.NewPortfolioCreated, portfolio.Name);
RaiseNewPortfolio(portfolio);
}
else if (isChanged)
RaisePortfolioChanged(portfolio);
return portfolio;
}
private void ProcessPortfolioMessage(PortfolioMessage message)
{
var portfolio = GetPortfolio(message.PortfolioName, p =>
{
message.ToPortfolio(p, ExchangeInfoProvider);
return true;
}, out var isNew);
//if (message.OriginalTransactionId == 0)
// return;
if (isNew)
_subscriptionManager.ProcessLookupResponse(message, portfolio);
RaiseReceived(portfolio, message, PortfolioReceived);
}
private async ValueTask ProcessPositionChangeMessage(PositionChangeMessage message, CancellationToken cancellationToken)
{
Portfolio portfolio;
if (message.IsMoney())
{
portfolio = GetPortfolio(message.PortfolioName, pf =>
{
if (message.LimitType != null || !UpdatePortfolioByChange)
return false;
pf.ApplyChanges(message, ExchangeInfoProvider);
return true;
}, out _);
RaiseReceived(portfolio, message, PortfolioReceived);
}
var security = await EnsureGetSecurityAsync(message, cancellationToken);
portfolio = LookupByPortfolioName(message.PortfolioName);
var entityMessage = message;
var valueInLots = message.TryGetDecimal(PositionChangeTypes.CurrentValueInLots);
if (valueInLots != null)
{
entityMessage = message.TypedClone();
if (!entityMessage.Changes.ContainsKey(PositionChangeTypes.CurrentValue))
{
var currValue = valueInLots.Value * (security.Multiplier ?? 1);
entityMessage.Add(PositionChangeTypes.CurrentValue, currValue);
}
entityMessage.Changes.Remove(PositionChangeTypes.CurrentValueInLots);
}
var position = GetPosition(portfolio, security, message.StrategyId, message.Side, message.ClientCode, message.DepoName, message.LimitType, message.Description);
position.ApplyChanges(entityMessage);
RaisePositionChanged(position);
RaiseReceived(position, message, PositionReceived);
}
private async ValueTask ProcessNewsMessage(NewsMessage message, CancellationToken cancellationToken)
{
var security = message.SecurityId == null ? null : await GetSecurityAsync(message.SecurityId.Value, cancellationToken);
var news = _entityCache.ProcessNewsMessage(security, message);
if (RaiseReceived(news.news, message, NewsReceived) == false)
return;
}
private async ValueTask ProcessQuotesMessage(QuoteChangeMessage message, CancellationToken cancellationToken)
{
if (RaiseReceived(message, message, OrderBookReceived) != true)
return;
if (message.IsFiltered || message.State != null)
return;
_entityCache.UpdateOrderBookSnapshot(message);
var bestBid = message.GetBestBid();
var bestAsk = message.GetBestAsk();
var fromLevel1 = message.BuildFrom == DataType.Level1;
var time = message.ServerTime;
Security security = null;
if (ValuesChanged is not null && !fromLevel1 && !Adapter.Level1Extend && (bestBid != null || bestAsk != null))
{
security ??= await EnsureGetSecurityAsync(message, cancellationToken);
var info = _entityCache.GetSecurityValues(security, time);
info.ClearBestQuotes(time);
var changes = new List<KeyValuePair<Level1Fields, object>>(4);
if (bestBid != null)
{
var q = bestBid.Value;
info.SetValue(time, Level1Fields.BestBidPrice, q.Price);
changes.Add(new (Level1Fields.BestBidPrice, q.Price));
if (q.Volume != 0)
{
info.SetValue(time, Level1Fields.BestBidVolume, q.Volume);
changes.Add(new (Level1Fields.BestBidVolume, q.Volume));
}
}
if (bestAsk != null)
{
var q = bestAsk.Value;
info.SetValue(time, Level1Fields.BestAskPrice, q.Price);
changes.Add(new (Level1Fields.BestAskPrice, q.Price));
if (q.Volume != 0)
{
info.SetValue(time, Level1Fields.BestAskVolume, q.Volume);
changes.Add(new (Level1Fields.BestAskVolume, q.Volume));
}
}
RaiseValuesChanged(security, changes, message.ServerTime, message.LocalTime);
}
#pragma warning disable CS0618 // Type or member is obsolete
if (UpdateSecurityLastQuotes)
{
security ??= await EnsureGetSecurityAsync(message, cancellationToken);
var updated = false;
if (!fromLevel1 || bestBid != null)
{
updated = true;
security.BestBid = bestBid;
}
if (!fromLevel1 || bestAsk != null)
{
updated = true;
security.BestAsk = bestAsk;
}
if (updated)
{
security.LocalTime = message.LocalTime;
security.LastChangeTime = message.ServerTime;
// стаканы по ALL обновляют BestXXX по конкретным инструментам
if (security.Board?.Code == SecurityId.AssociatedBoardCode)
{
var changedSecurities = new Dictionary<Security, RefPair<bool, bool>>();
foreach (var bid in message.Bids)
{
if (bid.BoardCode.IsEmpty())
continue;
var innerSecurity = await GetSecurityAsync(new SecurityId
{
SecurityCode = security.Code,
BoardCode = bid.BoardCode
}, cancellationToken);
var info = changedSecurities.SafeAdd(innerSecurity);
if (info.First)
continue;
info.First = true;
innerSecurity.BestBid = bid;
innerSecurity.LocalTime = message.LocalTime;
innerSecurity.LastChangeTime = message.ServerTime;
}
foreach (var ask in message.Asks)
{
if (ask.BoardCode.IsEmpty())
continue;
var innerSecurity = await GetSecurityAsync(new SecurityId
{
SecurityCode = security.Code,
BoardCode = ask.BoardCode
}, cancellationToken);
var info = changedSecurities.SafeAdd(innerSecurity);
if (info.Second)
continue;
info.Second = true;
innerSecurity.BestAsk = ask;
innerSecurity.LocalTime = message.LocalTime;
innerSecurity.LastChangeTime = message.ServerTime;
}
}
}
}
#pragma warning restore CS0618 // Type or member is obsolete
}
private void ProcessOrderLogMessage(ExecutionMessage message)
{
if (RaiseReceived(message, message, OrderLogReceived) == false)
return;
}
private async ValueTask ProcessTradeMessage(ExecutionMessage message, CancellationToken cancellationToken)
{
if (RaiseReceived(message, message, TickTradeReceived) != true)
return;
Security security = null;
if (ValuesChanged is not null)
{
security ??= await EnsureGetSecurityAsync(message, cancellationToken);
var time = message.ServerTime;
var info = _entityCache.GetSecurityValues(security, time);
info.ClearLastTrade(time);
var price = message.TradePrice ?? 0;
var changes = new List<KeyValuePair<Level1Fields, object>>(4)
{
new (Level1Fields.LastTradeTime, message.ServerTime),
new (Level1Fields.LastTradePrice, price)
};
info.SetValue(time, Level1Fields.LastTradeTime, message.ServerTime);
info.SetValue(time, Level1Fields.LastTradePrice, price);
if (message.IsSystem is bool isSystem)
{
info.SetValue(time, Level1Fields.IsSystem, isSystem);
changes.Add(new(Level1Fields.IsSystem, isSystem));
}
if (message.TradeId is long tradeId)
{
info.SetValue(time, Level1Fields.LastTradeId, tradeId);
changes.Add(new(Level1Fields.LastTradeId, tradeId));
}
if (!message.TradeStringId.IsEmpty())
{
info.SetValue(time, Level1Fields.LastTradeStringId, message.TradeStringId);
changes.Add(new(Level1Fields.LastTradeStringId, message.TradeStringId));
}
if (message.TradeVolume is decimal tradeVol)
{
info.SetValue(time, Level1Fields.LastTradeVolume, tradeVol);
changes.Add(new(Level1Fields.LastTradeVolume, tradeVol));
}
if (message.OriginSide is Sides side)
{
info.SetValue(time, Level1Fields.LastTradeOrigin, side);
changes.Add(new(Level1Fields.LastTradeOrigin, side));
}
if (message.IsUpTick is bool isUpTick)
{
info.SetValue(time, Level1Fields.LastTradeUpDown, isUpTick);
changes.Add(new(Level1Fields.LastTradeUpDown, isUpTick));
}
RaiseValuesChanged(security, changes, message.ServerTime, message.LocalTime);
}
#pragma warning disable CS0618 // Type or member is obsolete
if (UpdateSecurityLastQuotes)
{
security ??= await EnsureGetSecurityAsync(message, cancellationToken);
security.LastTick = message;
}
#pragma warning restore CS0618 // Type or member is obsolete
}
private void ProcessOrderMessage(Order o, Security security, ExecutionMessage message, long transactionId/*, bool isStatusRequest*/)
{
if (message.OrderState != OrderStates.Failed && message.Error == null)
{
foreach (var change in _entityCache.ProcessOrderMessage(o, security, message, transactionId, LookupByPortfolioName))
{
if (change == EntityCache.OrderChangeInfo.NotExist)
{
LogWarning(LocalizedStrings.OrderNotFound, message.OrderId.To<string>() ?? message.OrderStringId);
continue;
}
var order = change.Order;
_entityCache.TrySetAdapter(order, message.Adapter);
if (change.IsNew)
{
this.AddOrderInfoLog(order, "New order");
RaiseNewOrder(order);
}
else if (change.IsChanged)
{
this.AddOrderInfoLog(order, "Order changed");
RaiseOrderChanged(order);
if (change.IsEdit)
RaiseOrderEdited(transactionId, order);
}
RaiseReceived(order, message, OrderReceived);
}
}
else
{
if (message.OriginalTransactionId == 0)
{
LogError("Unknown error response for order {0}: {1}.", o, message.Error);
return;
}
foreach (var (fail, operation) in _entityCache.ProcessOrderFailMessage(o, security, message))
{
var order = fail.Order;
_entityCache.TrySetAdapter(order, message.Adapter);
//TryProcessFilteredMarketDepth(fail.Order.Security, message);
//var isRegisterFail = (fail.Order.Id == null && fail.Order.StringId.IsEmpty()) || fail.Order.Status == OrderStatus.RejectedBySystem;
_entityCache.AddFail(operation, fail);
switch (operation)
{
case OrderOperations.Register:
{
RaiseOrderRegisterFailed(message.OriginalTransactionId, fail);
RaiseReceived(fail, message, OrderRegisterFailReceived);
break;
}
case OrderOperations.Cancel:
{
RaiseOrderCancelFailed(message.OriginalTransactionId, fail);
RaiseReceived(fail, message, OrderCancelFailReceived);
break;
}
case OrderOperations.Edit:
{
RaiseOrderEditFailed(message.OriginalTransactionId, fail);
RaiseReceived(fail, message, OrderEditFailReceived);
break;
}
default:
throw new ArgumentOutOfRangeException(operation.ToString());
}
}
}
}
private void ProcessOwnTradeMessage(Order order, Security security, ExecutionMessage message, long transactionId)
{
var (trade, isNew) = _entityCache.ProcessOwnTradeMessage(order, security, message, transactionId);
if (trade == null)
return;
if (isNew)
RaiseNewMyTrade(trade);
//LogWarning("Duplicate own trade message: {0}", message);
RaiseReceived(trade, message, OwnTradeReceived);
}
private async ValueTask ProcessTransactionMessage(ExecutionMessage message, CancellationToken cancellationToken)
{
var originId = message.OriginalTransactionId;
if (_entityCache.IsMassCancelation(originId))
{
if (message.IsOk())
RaiseMassOrderCanceled(originId, message.ServerTime);
else
RaiseMassOrderCancelFailed(originId, message.Error, message.ServerTime);
return;
}
var isStatusRequest = _entityCache.IsOrderStatusRequest(originId);
if (!message.IsOk() && isStatusRequest)
{
// TransId != 0 means contains failed order info (not just status response)
if (message.TransactionId == 0)
return;
}
Order order = null;
var transactionId = message.TransactionId;
if (transactionId == 0)
{
transactionId = isStatusRequest || _entityCache.IsMassCancelation(originId) ? 0 : originId;
if (transactionId == 0)
order = _entityCache.TryGetOrder(message.OrderId, message.OrderStringId);
}
if (transactionId != 0)
{
if (message.HasTradeInfo())
order = _entityCache.TryGetOrder(transactionId, OrderOperations.Register);
else
order = _entityCache.TryGetOrder(transactionId, OrderOperations.Edit) ?? _entityCache.TryGetOrder(transactionId, OrderOperations.Cancel) ?? _entityCache.TryGetOrder(transactionId, OrderOperations.Register);
}
Security security;
if (order == null)
{
if (message.SecurityId == default)
{
LogWarning(LocalizedStrings.EmptySecId);
LogWarning(message.ToString());
return;
}
security = await EnsureGetSecurityAsync(message, cancellationToken);
if (transactionId == 0 && isStatusRequest)
transactionId = TransactionIdGenerator.GetNextId();
}
else
security = order.Security;
LogDebug("Order '{0}': {1}", order?.TransactionId, message);
var processed = false;
if (message.HasOrderInfo())
{
processed = true;
ProcessOrderMessage(order, security, message, transactionId);
}
if (message.HasTradeInfo())
{
processed = true;
ProcessOwnTradeMessage(order, security, message, transactionId);
}
if (!processed)
throw new ArgumentOutOfRangeException(nameof(message), message.DataType, LocalizedStrings.UnknownType.Put(message));
}
private async ValueTask ProcessExecutionMessage(ExecutionMessage message, CancellationToken cancellationToken)
{
if (message.DataType == DataType.Transactions)
await ProcessTransactionMessage(message, cancellationToken);
else if (message.DataType == DataType.Ticks)
await ProcessTradeMessage(message, cancellationToken);
else if (message.DataType == DataType.OrderLog)
ProcessOrderLogMessage(message);
else
throw new ArgumentOutOfRangeException(nameof(message), message.DataType, LocalizedStrings.UnknownType.Put(message));
}
private void ProcessCandleMessage(CandleMessage message)
{
foreach (var (subscription, candle) in _subscriptionManager.UpdateCandles(message))
{
CandleReceived?.Invoke(subscription, candle);
RaiseSubscriptionReceived(subscription, message);
}
}
private void ProcessChangePasswordMessage(ChangePasswordMessage message)
{
RaiseChangePassword(message.OriginalTransactionId, message.Error);
}
private void ProcessSubscriptionMessage(ISubscriptionIdMessage subscrMsg)
{
RaiseReceived((Message)subscrMsg, subscrMsg, RaiseSubscriptionReceived);
}
private void ProcessErrorMessage(ErrorMessage message)
{
RaiseError(message.Error);
}
}