Exemplo n.º 1
0
        private async Task UpdateInstrumentsInfo(HashSet <IStockModel> stocks = null)
        {
            if (stocks == null)
            {
                stocks = new HashSet <IStockModel>(_mainModel.Stocks.Values);
            }

            try
            {
                var info = await GetInstrumentsInfo();

                if (info.Status.Equals("Ok", StringComparison.InvariantCultureIgnoreCase))
                {
                    LastInstrumentsUpdate = DateTime.Now;
                    var instruments = info.Payload.Values.ToDictionary(v => v.Symbol.Ticker);

                    ExchangeStatus = info.Payload.Values
                                     .Select(v => new { v.Symbol.Exchange, v.ExchangeStatus }).Distinct()
                                     .Select(s => new ExchangeStatus(s.Exchange, s.ExchangeStatus)).ToArray();

                    foreach (var stock in stocks)
                    {
                        if (instruments.ContainsKey(stock.Ticker))
                        {
                            var instrumentInfo = new InstrumentInfo(instruments[stock.Ticker]);
                            if (Instruments.ContainsKey(stock.Ticker))
                            {
                                Instruments[stock.Ticker].ReadFrom(instrumentInfo);
                            }
                            else
                            {
                                Instruments[stock.Ticker] = instrumentInfo;
                            }

                            if (stock.Exchange != instrumentInfo.Exchange)
                            {
                                stock.Exchange = instrumentInfo.Exchange;
                            }
                            if (stock.CanBeShorted != instrumentInfo.ShortIsEnabled)
                            {
                                stock.CanBeShorted = instrumentInfo.ShortIsEnabled;
                            }
                        }
                        else
                        {
                            stock.Status = "Инструмент не торгуется";
                            stock.IsDead = true;
                        }
                    }
                }
                else
                {
                    throw new Exception(info.Status);
                }
            }
            catch (Exception ex)
            {
                LogError($"Ошибка получения сведений об инструментах: {ex.Message}");
                LastInstrumentsUpdate = DateTime.MinValue;
            }
        }