/// <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); }
/// <summary> /// Import from CSV file. /// </summary> /// <param name="fileName">File name.</param> /// <param name="updateProgress">Progress notification.</param> /// <param name="isCancelled">The processor, returning process interruption sign.</param> public void Import(string fileName, Action <int> updateProgress, Func <bool> isCancelled) { var buffer = new List <dynamic>(); this.AddInfoLog(LocalizedStrings.Str2870Params.Put(fileName, DataType.MessageType.Name)); try { var len = new FileInfo(fileName).Length; var prevPercent = 0; var lineIndex = 0; foreach (var instance in Parse(fileName, isCancelled)) { if (!(instance is SecurityMessage secMsg)) { buffer.Add(instance); if (buffer.Count > 1000) { FlushBuffer(buffer); } } else { var security = _securityStorage.LookupById(secMsg.SecurityId); var isNew = true; if (security != null) { if (!UpdateDuplicateSecurities) { this.AddErrorLog(LocalizedStrings.Str1453.Put(secMsg.SecurityId)); continue; } isNew = false; security.ApplyChanges(secMsg, _exchangeInfoProvider, UpdateDuplicateSecurities); } else { security = secMsg.ToSecurity(_exchangeInfoProvider); } _securityStorage.Save(security, UpdateDuplicateSecurities); ExtendedInfoStorageItem?.Add(secMsg.SecurityId, secMsg.ExtensionInfo); SecurityUpdated?.Invoke(security, isNew); } var percent = (int)(((double)lineIndex / len) * 100 - 1).Round(); lineIndex++; if (percent <= prevPercent) { continue; } prevPercent = percent; updateProgress?.Invoke(prevPercent); }
/// <inheritdoc /> public override Security CreateSecurity(string id) { return(_securityStorage.LookupById(id) ?? base.CreateSecurity(id)); }
/// <summary> /// Import from CSV file. /// </summary> /// <param name="fileName">File name.</param> /// <param name="updateProgress">Progress notification.</param> /// <param name="isCancelled">The processor, returning process interruption sign.</param> public void Import(string fileName, Action <int> updateProgress, Func <bool> isCancelled) { var buffer = new List <Message>(); this.AddInfoLog(LocalizedStrings.Str2870Params.Put(fileName, DataType.MessageType.Name)); Func <Message, SecurityId> getSecurityId = null; if (DataType == DataType.Securities) { getSecurityId = m => ((SecurityMessage)m).SecurityId; } else if (DataType == DataType.MarketDepth) { getSecurityId = m => ((QuoteChangeMessage)m).SecurityId; } else if (DataType == DataType.Level1) { getSecurityId = m => ((Level1ChangeMessage)m).SecurityId; } else if (DataType == DataType.PositionChanges) { getSecurityId = m => ((PositionChangeMessage)m).SecurityId; } else if (DataType == DataType.Ticks || DataType == DataType.OrderLog || DataType == DataType.Transactions) { getSecurityId = m => ((ExecutionMessage)m).SecurityId; } else if (DataType.IsCandles) { getSecurityId = m => ((CandleMessage)m).SecurityId; } try { var len = new FileInfo(fileName).Length; var prevPercent = 0; var lineIndex = 0; foreach (var msg in Parse(fileName, isCancelled)) { if (msg is SecurityMappingMessage) { continue; } if (!(msg is SecurityMessage secMsg)) { buffer.Add(msg); if (buffer.Count > 1000) { FlushBuffer(buffer, getSecurityId); } } else { var security = _securityStorage.LookupById(secMsg.SecurityId); var isNew = true; if (security != null) { if (!UpdateDuplicateSecurities) { this.AddErrorLog(LocalizedStrings.Str1453.Put(secMsg.SecurityId)); continue; } isNew = false; security.ApplyChanges(secMsg, _exchangeInfoProvider, UpdateDuplicateSecurities); } else { security = secMsg.ToSecurity(_exchangeInfoProvider); } _securityStorage.Save(security, UpdateDuplicateSecurities); ExtendedInfoStorageItem?.Add(secMsg.SecurityId, secMsg.ExtensionInfo); SecurityUpdated?.Invoke(security, isNew); } var percent = (int)(((double)lineIndex / len) * 100 - 1).Round(); lineIndex++; if (percent <= prevPercent) { continue; } prevPercent = percent; updateProgress?.Invoke(prevPercent); }
/// <summary> /// Import from CSV file. /// </summary> /// <param name="fileName">File name.</param> /// <param name="updateProgress">Progress notification.</param> /// <param name="isCancelled">The processor, returning process interruption sign.</param> /// <returns>Count and last time.</returns> public (int, DateTimeOffset?) Import(string fileName, Action <int> updateProgress, Func <bool> isCancelled) { var count = 0; var lastTime = default(DateTimeOffset?); var buffer = new List <Message>(); void Flush() { count += buffer.Count; if (buffer.LastOrDefault() is IServerTimeMessage timeMsg) { lastTime = timeMsg.ServerTime; } FlushBuffer(buffer); } this.AddInfoLog(LocalizedStrings.Str2870Params.Put(fileName, DataType.MessageType.Name)); try { var len = new FileInfo(fileName).Length; var prevPercent = 0; var lineIndex = 0; foreach (var msg in Parse(fileName, isCancelled)) { if (msg is SecurityMappingMessage) { continue; } if (msg is not SecurityMessage secMsg) { buffer.Add(msg); if (buffer.Count > 1000) { Flush(); } } else { var security = _securityStorage.LookupById(secMsg.SecurityId); var isNew = true; if (security != null) { if (!UpdateDuplicateSecurities) { this.AddErrorLog(LocalizedStrings.Str1453.Put(secMsg.SecurityId)); continue; } isNew = false; security.ApplyChanges(secMsg, _exchangeInfoProvider, UpdateDuplicateSecurities); } else { security = secMsg.ToSecurity(_exchangeInfoProvider); } _securityStorage.Save(security, UpdateDuplicateSecurities); ExtendedInfoStorageItem?.Add(secMsg.SecurityId, secMsg.ExtensionInfo); SecurityUpdated?.Invoke(security, isNew); } var percent = (int)(((double)lineIndex / len) * 100 - 1).Round(); lineIndex++; if (percent <= prevPercent) { continue; } prevPercent = percent; updateProgress?.Invoke(prevPercent); }