public async Task <RiskExposureReport> GetAsync() { IReadOnlyCollection <AssetInvestment> investments = _investmentService.GetAll(); IReadOnlyCollection <IndexSettings> indicesSettings = await _indexSettingsService.GetAllAsync(); IReadOnlyCollection <IndexPrice> indexPrices = await _indexPriceService.GetAllAsync(); IReadOnlyCollection <Token> tokens = await _tokenService.GetAllAsync(); IReadOnlyCollection <AssetHedgeSettings> assetHedgeSettings = await _assetHedgeSettingsService.GetAllAsync(); IReadOnlyCollection <Position> positions = await _positionService.GetAllAsync(); IReadOnlyCollection <string> assets = indexPrices .SelectMany(o => o.Weights?.Select(e => e.AssetId) ?? new string[0]) .Distinct() .ToArray(); decimal totalRemainingAmount = investments.Sum(o => o.RemainingAmount); return(new RiskExposureReport { // If remaining amount is negative position in usd should be displayed as positive. UsdCash = -totalRemainingAmount, Indices = GetIndexReports(indicesSettings, indexPrices, tokens), Tokens = GetTokensDelta(indicesSettings, indexPrices, tokens), Assets = GetAssetsDelta(assets, assetHedgeSettings, positions) }); }
public async Task DeleteAssetPairAsync(string assetPair, string exchange, string userId) { AssetPairSettings existingAssetPairSettings = await GetAssetPairAsync(assetPair, exchange); if (existingAssetPairSettings == null) { throw new EntityNotFoundException(); } IReadOnlyCollection <AssetHedgeSettings> assetHedgeSettings = await _assetHedgeSettingsService.GetAllAsync(); if (assetHedgeSettings.Any(o => o.Exchange == exchange && o.AssetPairId == assetPair)) { throw new InvalidOperationException("Asset pair is used by asset hedge settings."); } await _assetPairSettingsRepository.DeleteAsync(assetPair, exchange); _assetPairsCache.Remove(GetKey(assetPair, exchange)); InitializeAssetPairs(); _log.InfoWithDetails("Asset settings deleted", new { existingAssetPairSettings, userId }); }
public async Task <IReadOnlyCollection <AssetHedgeSettingsModel> > GetAllAsync() { IReadOnlyCollection <AssetHedgeSettings> assetHedgeSettings = await _assetHedgeSettingsService.GetAllAsync(); return(Mapper.Map <List <AssetHedgeSettingsModel> >(assetHedgeSettings)); }
private async Task <IReadOnlyCollection <PositionReport> > CreateReports() { IReadOnlyCollection <Position> positions = await _positionService.GetAllAsync(); IReadOnlyCollection <AssetInvestment> assetInvestments = _investmentService.GetAll(); IReadOnlyCollection <HedgeLimitOrder> hedgeLimitOrders = _hedgeLimitOrderService.GetAll(); HedgeSettings hedgeSettings = await _hedgeSettingsService.GetAsync(); IReadOnlyCollection <AssetHedgeSettings> assetsHedgeSettings = await _assetHedgeSettingsService.GetAllAsync(); string[] assets = positions.Select(o => o.AssetId) .Union(assetInvestments.Select(o => o.AssetId)) .Union(assetsHedgeSettings.Select(o => o.AssetId)) .ToArray(); var positionReports = new List <PositionReport>(); foreach (string assetId in assets) { AssetHedgeSettings assetHedgeSettings = await _assetHedgeSettingsService.EnsureAsync(assetId); Position currentPosition = positions .SingleOrDefault(o => o.AssetId == assetId && o.Exchange == assetHedgeSettings.Exchange); HedgeLimitOrder hedgeLimitOrder = hedgeLimitOrders.SingleOrDefault(o => o.AssetId == assetId); AssetInvestment assetInvestment = assetInvestments.SingleOrDefault(o => o.AssetId == assetId); decimal?volumeInUsd = null; if (currentPosition != null) { volumeInUsd = GetVolumeInUsd(currentPosition.AssetId, currentPosition.Exchange, currentPosition.Volume); } Quote assetQuote; if (assetInvestment == null) { assetQuote = _rateService.GetQuoteUsd(assetHedgeSettings.AssetId, assetHedgeSettings.Exchange); } else { assetQuote = assetInvestment.Quote; } positionReports.Add(new PositionReport { AssetId = assetId, Exchange = assetHedgeSettings.Exchange, Quote = assetQuote, Volume = currentPosition?.Volume, VolumeInUsd = volumeInUsd, OppositeVolume = currentPosition?.OppositeVolume, PnL = volumeInUsd.HasValue ? currentPosition.OppositeVolume + volumeInUsd : null, HedgeLimitOrder = hedgeLimitOrder, AssetInvestment = assetInvestment, Error = ValidateAssetHedgeSettings(assetHedgeSettings) ?? ValidateInvestments(assetInvestment) ?? ValidateThresholdCritical(assetInvestment, hedgeSettings, assetHedgeSettings) ?? ValidateQuote(assetQuote) }); IEnumerable <Position> otherPositions = positions .Where(o => o.AssetId == assetId && o.Exchange != assetHedgeSettings.Exchange); foreach (Position position in otherPositions) { Quote otherPositionQuote = _rateService.GetQuoteUsd(position.AssetId, position.Exchange); volumeInUsd = GetVolumeInUsd(position.AssetId, position.Exchange, position.Volume); positionReports.Add(new PositionReport { AssetId = assetId, Exchange = position.Exchange, Quote = otherPositionQuote, Volume = position.Volume, VolumeInUsd = volumeInUsd, OppositeVolume = position.OppositeVolume, PnL = volumeInUsd.HasValue ? position.OppositeVolume + volumeInUsd : null, HedgeLimitOrder = null, AssetInvestment = null, Error = ValidateAssetHedgeSettings(assetHedgeSettings) ?? ValidateQuote(otherPositionQuote) }); } } foreach (PositionReport positionReport in positionReports) { if (positionReport.Exchange == ExchangeNames.Virtual) { positionReport.ActualPnL = -1 * positionReport.PnL; } else { positionReport.ActualPnL = positionReport.PnL; } } return(positionReports .OrderBy(o => o.AssetId) .ToArray()); }