public void BindOrderToOecOrderVersion(BusinessEntities.Order order, OEC.API.Version ver) { if (ver.ID > 0) { order.Id = ver.ID; } if (_versionOrderSet.ContainsKey(ver)) { if (_versionOrderSet[ver] != order) { _connector.AddWarningLog("Старая версия заявки не была удалена из коллекции."); _versionOrderSet.Remove(ver); _versionOrderSet[ver] = order; } } else { _versionOrderSet[ver] = order; } }
protected override bool OnRemove(IMarketDataStorage item) { _transactionIds.Remove(item); return(base.OnRemove(item)); }
/// <inheritdoc /> protected override void OnInnerAdapterNewOutMessage(Message message) { long TryReplaceOriginId(long id) { if (id == 0) { return(0); } lock (_sync) return(_replaceId.TryGetValue(id, out var prevId) ? prevId : id); } var prevOriginId = 0L; var newOriginId = 0L; if (message is IOriginalTransactionIdMessage originIdMsg1) { newOriginId = originIdMsg1.OriginalTransactionId; prevOriginId = originIdMsg1.OriginalTransactionId = TryReplaceOriginId(newOriginId); } switch (message.Type) { case MessageTypes.SubscriptionResponse: { lock (_sync) { if (((SubscriptionResponseMessage)message).IsOk()) { if (_subscriptionsById.TryGetValue(prevOriginId, out var info)) { // no need send response after re-subscribe cause response was handled prev time if (_replaceId.ContainsKey(newOriginId)) { if (info.State != SubscriptionStates.Stopped) { return; } } else { ChangeState(info, SubscriptionStates.Active); } } } else { if (!_historicalRequests.Remove(prevOriginId)) { if (_subscriptionsById.TryGetAndRemove(prevOriginId, out var info)) { ChangeState(info, SubscriptionStates.Error); _replaceId.Remove(newOriginId); } } } } break; } case MessageTypes.SubscriptionOnline: { lock (_sync) { if (!_subscriptionsById.TryGetValue(prevOriginId, out var info)) { break; } if (_replaceId.ContainsKey(newOriginId)) { // no need send response after re-subscribe cause response was handled prev time if (info.State == SubscriptionStates.Online) { return; } } else { ChangeState(info, SubscriptionStates.Online); } } break; } case MessageTypes.SubscriptionFinished: { lock (_sync) { if (_replaceId.ContainsKey(newOriginId)) { return; } _historicalRequests.Remove(prevOriginId); if (_subscriptionsById.TryGetValue(newOriginId, out var info)) { ChangeState(info, SubscriptionStates.Finished); } } break; } default: { if (message is ISubscriptionIdMessage subscrMsg) { lock (_sync) { var ids = subscrMsg.GetSubscriptionIds(); if (ids.Length == 0) { if (subscrMsg.OriginalTransactionId != 0 && _historicalRequests.ContainsKey(subscrMsg.OriginalTransactionId)) { subscrMsg.SetSubscriptionIds(subscriptionId: subscrMsg.OriginalTransactionId); } } else { lock (_sync) { if (_replaceId.Count > 0) { subscrMsg.SetSubscriptionIds(ids.Select(id => _replaceId.TryGetValue2(id) ?? id).ToArray()); } } } } } break; } } base.OnInnerAdapterNewOutMessage(message); switch (message.Type) { case ExtendedMessageTypes.ReconnectingFinished: { ProcessSuspendedMessage supended = null; lock (_sync) { _replaceId.Clear(); _reMapSubscriptions.Clear(); _reMapSubscriptions.AddRange(_subscriptionsById.Values.Distinct().Where(i => i.State.IsActive()).Select(i => { var subscription = i.Subscription.TypedClone(); subscription.TransactionId = TransactionIdGenerator.GetNextId(); _replaceId.Add(subscription.TransactionId, i.Subscription.TransactionId); this.AddInfoLog("Re-map subscription: {0}->{1} for '{2}'.", i.Subscription.TransactionId, subscription.TransactionId, i.Subscription); return((Message)subscription); })); if (_reMapSubscriptions.Count > 0) { supended = new ProcessSuspendedMessage(this); } } if (supended != null) { base.OnInnerAdapterNewOutMessage(supended); } break; } } }
/// <inheritdoc /> protected override void OnInnerAdapterNewOutMessage(Message message) { switch (message.Type) { case MessageTypes.MarketData: { var resMsg = (MarketDataMessage)message; if (!resMsg.IsOk()) { lock (_sync) { if (!_historicalRequests.Remove(resMsg.OriginalTransactionId)) { if (_subscriptionsById.TryGetKey(resMsg.OriginalTransactionId, out var info)) { _subscriptionsById.Remove(info); _subscriptionsByKey.RemoveByValue(info); } } } } break; } case MessageTypes.MarketDataFinished: { var resMsg = (MarketDataFinishedMessage)message; lock (_sync) _historicalRequests.Remove(resMsg.OriginalTransactionId); break; } case MessageTypes.PortfolioLookupResult: { var resMsg = (PortfolioLookupResultMessage)message; lock (_sync) _historicalRequests.Remove(resMsg.OriginalTransactionId); break; } default: { if (message is ISubscriptionIdMessage subscrMsg) { lock (_sync) { if (subscrMsg.OriginalTransactionId != 0 && _historicalRequests.Contains(subscrMsg.OriginalTransactionId)) { subscrMsg.SubscriptionId = subscrMsg.OriginalTransactionId; } else { if (subscrMsg.OriginalTransactionId != 0 && _subscriptionsById.TryGetKey(subscrMsg.OriginalTransactionId, out var info)) { } else { var dataType = message.Type.ToDataType((message as CandleMessage)?.Arg ?? (message as ExecutionMessage)?.ExecutionType); var secId = GetSecurityId(dataType, (subscrMsg as ISecurityIdMessage)?.SecurityId ?? default); if (!_subscriptionsByKey.TryGetValue(Tuple.Create(dataType, secId), out info)) { break; } } subscrMsg.SubscriptionIds = info.Subscribers.Cache; } } } break; } } base.OnInnerAdapterNewOutMessage(message); }
/// <inheritdoc /> protected override void OnInnerAdapterNewOutMessage(Message message) { long TryReplaceOriginId(long id) { lock (_sync) return(_replaceId.TryGetValue(id, out var prevId) ? prevId : id); } var newOriginId = 0L; if (message is IOriginalTransactionIdMessage originIdMsg1) { newOriginId = originIdMsg1.OriginalTransactionId; originIdMsg1.OriginalTransactionId = TryReplaceOriginId(newOriginId); } switch (message.Type) { case MessageTypes.MarketData: { var responseMsg = (MarketDataMessage)message; var originId = responseMsg.OriginalTransactionId; lock (_sync) { if (responseMsg.IsOk()) { // no need send response after re-subscribe cause response was handled prev time if (_replaceId.ContainsKey(newOriginId)) { return; } } else { if (!_historicalRequests.Remove(originId)) { if (_subscriptionsById.TryGetKey(originId, out var info)) { _replaceId.Remove(newOriginId); _subscriptionsById.Remove(info); _subscriptionsByKey.RemoveByValue(info); } } } } break; } case MessageTypes.MarketDataFinished: case MessageTypes.SecurityLookupResult: case MessageTypes.PortfolioLookupResult: case MessageTypes.OrderStatus: { var resultMsg = (IOriginalTransactionIdMessage)message; lock (_sync) { _replaceId.Remove(newOriginId); _historicalRequests.Remove(resultMsg.OriginalTransactionId); } break; } default: { if (message is ISubscriptionIdMessage subscrMsg) { lock (_sync) { if (subscrMsg.OriginalTransactionId != 0 && _historicalRequests.Contains(subscrMsg.OriginalTransactionId)) { subscrMsg.SubscriptionId = subscrMsg.OriginalTransactionId; } else { if (subscrMsg.OriginalTransactionId != 0 && _subscriptionsById.TryGetKey(subscrMsg.OriginalTransactionId, out var info)) { } else { var dataType = message.Type.ToDataType((message as CandleMessage)?.Arg ?? (message as ExecutionMessage)?.ExecutionType); var secId = GetSecurityId(dataType, (subscrMsg as ISecurityIdMessage)?.SecurityId ?? default); if (!_subscriptionsByKey.TryGetValue(Tuple.Create(dataType, secId), out info)) { break; } } subscrMsg.SubscriptionIds = info.Subscribers.Cache; } } } break; } } base.OnInnerAdapterNewOutMessage(message); switch (message.Type) { case ExtendedMessageTypes.ReconnectingFinished: { Message[] subscriptions; lock (_sync) { _replaceId.Clear(); subscriptions = _subscriptionsById.Keys.Select(i => { var subscription = (ISubscriptionMessage)i.Subscription.Clone(); subscription.TransactionId = TransactionIdGenerator.GetNextId(); _replaceId.Add(subscription.TransactionId, i.Subscription.TransactionId); var msg = (Message)subscription; msg.Adapter = this; msg.IsBack = true; return(msg); }).ToArray(); } foreach (var subscription in subscriptions) { base.OnInnerAdapterNewOutMessage(subscription); } break; } } }