コード例 #1
0
        private void DisposeControl(IContentWindow window)
        {
            _contents.RemoveByValue(window);

            window.Control.Dispose();

            if (window.Tag == null)
            {
                return;
            }

            var sender = window.Tag;

            var strategyContainer = window.Tag as StrategyContainer;

            if (strategyContainer != null)
            {
                sender = strategyContainer.Strategy;
            }

            var info = window.Tag as StrategyInfo;

            if (info != null)
            {
                sender = window.Control.GetKey();
            }

            ConfigManager
            .GetService <IStudioCommandService>()
            .UnBind(sender);
        }
コード例 #2
0
        private void ChangeState(SubscriptionInfo info, SubscriptionStates state)
        {
            var id = info.Subscription.TransactionId;

            const string text = "Subscription {0} {1}->{2}.";

            if (info.State.IsOk(state))
            {
                this.AddInfoLog(text, id, info.State, state);
            }
            else
            {
                this.AddWarningLog(text, id, info.State, state);
            }

            info.State = state;

            if (!state.IsActive())
            {
                _subscriptionsByKey.RemoveByValue(info);
                this.AddInfoLog(LocalizedStrings.OnlineSubscriptionRemoved, id);
            }
        }
コード例 #3
0
        private void OnStorageChanged(string storageName, SecurityIdMapping mapping)
        {
            if (!StorageName.CompareIgnoreCase(storageName))
            {
                return;
            }

            // if adapter code is empty means mapping removed
            // also mapping can be changed (new adapter code for old security code)

            lock (_syncRoot)
            {
                _securityIds.RemoveByValue(mapping.StockSharpId);
                _securityIds.Add(mapping.StockSharpId, mapping.AdapterId);
            }
        }
コード例 #4
0
        private void OnStorageMappingChanged(string storageName, SecurityId securityId, SecurityId adapterId)
        {
            if (!InnerAdapter.StorageName.CompareIgnoreCase(storageName))
            {
                return;
            }

            // if adapter code is empty means mapping removed
            // also mapping can be changed (new adapter code for old security code)

            _securityIds.RemoveByValue(securityId);

            if (!adapterId.IsDefault())
            {
                _securityIds.Add(securityId, adapterId);
            }
        }
コード例 #5
0
        /// <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);
            }

            bool UpdateSubscriptionResult(SubscriptionResponseMessage responseMsg)
            {
                HashSet <long> subscribers = null;

                lock (_sync)
                {
                    if (responseMsg.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(false);
                                }
                            }
                            else
                            {
                                ChangeState(info, SubscriptionStates.Active);
                            }
                        }
                    }
                    else
                    {
                        if (!_historicalRequests.Remove(prevOriginId))
                        {
                            if (_subscriptionsById.TryGetAndRemove(prevOriginId, out var info))
                            {
                                ChangeState(info, SubscriptionStates.Error);

                                _replaceId.Remove(newOriginId);
                                _subscriptionsByKey.RemoveByValue(info);

                                var set = new HashSet <long>(info.Subscribers.Cache);
                                set.Remove(prevOriginId);
                                subscribers = set;
                            }
                        }
                    }
                }

                if (subscribers != null)
                {
                    foreach (var subscriber in subscribers)
                    {
                        base.OnInnerAdapterNewOutMessage(subscriber.CreateSubscriptionResponse(responseMsg.Error));
                    }
                }

                return(true);
            }

            switch (message.Type)
            {
            case MessageTypes.SubscriptionResponse:
            {
                if (!UpdateSubscriptionResult((SubscriptionResponseMessage)message))
                {
                    return;
                }

                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:
            case MessageTypes.SecurityLookupResult:
            case MessageTypes.PortfolioLookupResult:
            case MessageTypes.OrderStatus:
            {
                lock (_sync)
                {
                    if (_replaceId.ContainsKey(newOriginId))
                    {
                        return;
                    }

                    _historicalRequests.Remove(prevOriginId);
                }

                break;
            }

            default:
            {
                if (message is ISubscriptionIdMessage subscrMsg)
                {
                    lock (_sync)
                    {
                        if (subscrMsg.OriginalTransactionId != 0 && _historicalRequests.ContainsKey(subscrMsg.OriginalTransactionId))
                        {
                            subscrMsg.SetSubscriptionIds(subscriptionId: subscrMsg.OriginalTransactionId);
                        }
                        else
                        {
                            if (subscrMsg.OriginalTransactionId != 0 && _subscriptionsById.TryGetValue(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.SetSubscriptionIds(info.Subscribers.Cache);
                        }
                    }
                }

                break;
            }
            }

            base.OnInnerAdapterNewOutMessage(message);

            switch (message.Type)
            {
            case ExtendedMessageTypes.ReconnectingFinished:
            {
                Message[] subscriptions;

                lock (_sync)
                {
                    _replaceId.Clear();

                    subscriptions = _subscriptionsById.Values.Distinct().Select(i =>
                        {
                            var subscription           = (ISubscriptionMessage)i.Subscription.Clone();
                            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).LoopBack(this));
                        }).ToArray();
                }

                foreach (var subscription in subscriptions)
                {
                    base.OnInnerAdapterNewOutMessage(subscription);
                }

                break;
            }
            }
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
0
        /// <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;
            }
            }
        }