Пример #1
0
        /// <inheritdoc />
        protected override void OnInnerAdapterNewOutMessage(Message message)
        {
            switch (message.Type)
            {
            case MessageTypes.Security:
            {
                var secMsg   = (SecurityMessage)message;
                var security = _securityStorage.LookupById(secMsg.SecurityId);

                if (security == null)
                {
                    security = secMsg.ToSecurity(_exchangeInfoProvider);
                }
                else
                {
                    security.ApplyChanges(secMsg, _exchangeInfoProvider, OverrideSecurityData);
                }

                _securityStorage.Save(security, OverrideSecurityData);
                break;
            }

            case MessageTypes.Board:
            {
                var boardMsg = (BoardMessage)message;
                var board    = _exchangeInfoProvider.GetExchangeBoard(boardMsg.Code);

                if (board == null)
                {
                    board = _exchangeInfoProvider.GetOrCreateBoard(boardMsg.Code, code =>
                        {
                            var exchange = _exchangeInfoProvider.GetExchange(boardMsg.ExchangeCode) ?? boardMsg.ToExchange(new Exchange
                            {
                                Name = boardMsg.ExchangeCode
                            });

                            return(new ExchangeBoard
                            {
                                Code = code,
                                Exchange = exchange
                            });
                        });
                }

                board.ApplyChanges(boardMsg);

                _exchangeInfoProvider.Save(board.Exchange);
                _exchangeInfoProvider.Save(board);
                break;
            }

            case MessageTypes.Portfolio:
            {
                var portfolioMsg = (PortfolioMessage)message;

                if (portfolioMsg.Error != null)
                {
                    break;
                }

                var portfolio = _positionStorage.GetPortfolio(portfolioMsg.PortfolioName) ?? new Portfolio
                {
                    Name = portfolioMsg.PortfolioName
                };

                portfolioMsg.ToPortfolio(portfolio, _exchangeInfoProvider);
                _positionStorage.Save(portfolio);

                break;
            }

            case MessageTypes.PositionChange:
            {
                var positionMsg = (PositionChangeMessage)message;

                if (positionMsg.IsMoney())
                {
                    var portfolio = _positionStorage.GetPortfolio(positionMsg.PortfolioName) ?? new Portfolio
                    {
                        Name = positionMsg.PortfolioName
                    };

                    portfolio.ApplyChanges(positionMsg, _exchangeInfoProvider);
                    _positionStorage.Save(portfolio);
                }
                else
                {
                    var position = GetPosition(positionMsg.SecurityId, positionMsg.PortfolioName);

                    if (position == null)
                    {
                        break;
                    }

                    if (!positionMsg.DepoName.IsEmpty())
                    {
                        position.DepoName = positionMsg.DepoName;
                    }

                    if (positionMsg.LimitType != null)
                    {
                        position.LimitType = positionMsg.LimitType;
                    }

                    if (!positionMsg.Description.IsEmpty())
                    {
                        position.Description = positionMsg.Description;
                    }

                    position.ApplyChanges(positionMsg);
                    _positionStorage.Save(position);
                }

                break;
            }
            }

            base.OnInnerAdapterNewOutMessage(message);
        }
 /// <inheritdoc />
 public override Portfolio CreatePortfolio(string name)
 {
     return(_positionStorage.GetPortfolio(name) ?? base.CreatePortfolio(name));
 }