public async Task <IEnumerable <IAccountAssetPair> > AssignInstruments(string tradingConditionId, string baseAssetId, string[] instruments) { var defaults = _settings.DefaultAccountAssetsSettings ?? new AccountAssetsSettings(); var currentInstruments = (await _pairsRepository.GetAllAsync(tradingConditionId, baseAssetId)).ToArray(); if (currentInstruments.Any()) { var toRemove = currentInstruments.Where(x => !instruments.Contains(x.Instrument)).ToArray(); var existingOrderGroups = _orderReader.GetAll() .Where(o => o.TradingConditionId == tradingConditionId && o.AccountAssetId == baseAssetId) .GroupBy(o => o.Instrument) .Where(o => toRemove.Any(i => i.Instrument == o.Key)) .ToArray(); if (existingOrderGroups.Any()) { var errorMessage = "Unable to remove following instruments as they have active orders: "; foreach (var group in existingOrderGroups) { errorMessage += $"{group.Key}({group.Count()} orders) "; } throw new InvalidOperationException(errorMessage); } foreach (var pair in toRemove) { await _pairsRepository.Remove(pair.TradingConditionId, pair.BaseAssetId, pair.Instrument); } } var pairsToAdd = instruments.Where(x => currentInstruments.All(y => y.Instrument != x)); var addedPairs = await _pairsRepository.AddAssetPairs(tradingConditionId, baseAssetId, pairsToAdd, defaults); await UpdateAccountAssetsCache(); await _clientNotifyService.NotifyTradingConditionsChanged(tradingConditionId); return(addedPairs); }
public List <SummaryAssetInfo> GetAssetsInfo() { var result = new List <SummaryAssetInfo>(); var orders = _ordersReader.GetAll().ToList(); foreach (var order in orders) { var assetInfo = result.FirstOrDefault(item => item.AssetPairId == order.Instrument); if (assetInfo == null) { result.Add(new SummaryAssetInfo { AssetPairId = order.Instrument, PnL = order.GetFpl(), VolumeLong = order.GetOrderType() == OrderDirection.Buy ? order.GetMatchedVolume() : 0, VolumeShort = order.GetOrderType() == OrderDirection.Sell ? order.GetMatchedVolume() : 0 }); } else { assetInfo.PnL += order.GetFpl(); if (order.GetOrderType() == OrderDirection.Buy) { assetInfo.VolumeLong += order.GetMatchedVolume(); } else { assetInfo.VolumeShort += order.GetMatchedVolume(); } } } return(result); }