public async Task HandleInternalTradesAsync(IReadOnlyCollection <InternalTrade> internalTrades) { await _semaphore.WaitAsync(); try { IReadOnlyCollection <IndexSettings> indicesSettings = await _indexSettingsService.GetAllAsync(); foreach (InternalTrade internalTrade in internalTrades) { IndexSettings indexSettings = indicesSettings .SingleOrDefault(o => o.AssetPairId == internalTrade.AssetPairId); if (indexSettings != null) { await _internalTradeService.RegisterAsync(internalTrade); await _tokenService.UpdateVolumeAsync(indexSettings.AssetId, internalTrade); } } } finally { _semaphore.Release(); } }
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 UpdateLimitOrdersAsync() { IReadOnlyCollection <string> assets = await GetAssetsAsync(); await CancelLimitOrdersAsync(assets); if (!await ValidateIndexPricesAsync()) { return; } IReadOnlyCollection <Token> tokens = await _tokenService.GetAllAsync(); IReadOnlyCollection <Position> positions = await GetCurrentPositionsAsync(); IReadOnlyCollection <IndexPrice> indexPrices = await _indexPriceService.GetAllAsync(); IReadOnlyCollection <IndexSettings> indicesSettings = await _indexSettingsService.GetAllAsync(); IReadOnlyDictionary <string, Quote> assetPrices = await GetAssetPricesAsync(assets); IReadOnlyCollection <AssetInvestment> assetInvestments = InvestmentCalculator.Calculate(assets, indicesSettings, tokens, indexPrices, positions, assetPrices); _log.InfoWithDetails("Investments calculated", assetInvestments); IReadOnlyCollection <HedgeLimitOrder> hedgeLimitOrders = await CreateLimitOrdersAsync(assetInvestments); await ValidateHedgeLimitOrdersAsync(hedgeLimitOrders); _log.InfoWithDetails("Hedge limit orders calculated", hedgeLimitOrders); _investmentService.Update(assetInvestments); await ApplyLimitOrdersAsync(assets, hedgeLimitOrders); }
public async Task <IReadOnlyCollection <IndexReport> > GetAsync() { IReadOnlyCollection <IndexSettings> indicesSettings = await _indexSettingsService.GetAllAsync(); var indexReports = new List <IndexReport>(); foreach (IndexSettings indexSettings in indicesSettings) { IndexPrice indexPrice = await _indexPriceService.GetByIndexAsync(indexSettings.Name); Token token = await _tokenService.GetAsync(indexSettings.AssetId); Balance balance = _balanceService.GetByAssetId(ExchangeNames.Lykke, indexSettings.AssetId); indexReports.Add(new IndexReport { Name = indexSettings.Name, AssetId = indexSettings.AssetId, AssetPairId = indexSettings.AssetPairId, Value = indexPrice?.Value, Price = indexPrice?.Price, Timestamp = indexPrice?.Timestamp, K = indexPrice?.K, Amount = token.Amount, OpenVolume = token.OpenVolume, OppositeVolume = token.OppositeVolume, Balance = balance.Amount, Alpha = indexSettings.Alpha, TrackingFee = indexSettings.TrackingFee, PerformanceFee = indexSettings.PerformanceFee, SellMarkup = indexSettings.SellMarkup, SellVolume = indexSettings.SellVolume, BuyVolume = indexSettings.BuyVolume, Weights = indexPrice?.Weights ?? new AssetWeight[0] }); } return(indexReports); }
public async Task <ProfitLossReport> GetAsync() { IReadOnlyCollection <Position> positions = await _positionService.GetAllAsync(); decimal positionsVolumeInUsd = 0; decimal positionsOppositeVolume = 0; foreach (Position position in positions.Where(position => position.Exchange != ExchangeNames.Virtual)) { positionsOppositeVolume += position.OppositeVolume; if (_rateService.TryConvertToUsd(position.AssetId, position.Exchange, position.Volume, out decimal amountInUsd)) { positionsVolumeInUsd += amountInUsd; } } decimal investments = 0; decimal openTokensAmountInUsd = 0; IReadOnlyCollection <IndexSettings> indicesSettings = await _indexSettingsService.GetAllAsync(); foreach (IndexSettings indexSettings in indicesSettings) { IndexPrice indexPrice = await _indexPriceService.GetByIndexAsync(indexSettings.Name); Token token = await _tokenService.GetAsync(indexSettings.AssetId); investments += token?.OppositeVolume ?? 0; openTokensAmountInUsd += token?.OpenVolume * indexPrice?.Price ?? 0; } decimal balance = positionsVolumeInUsd + (investments - Math.Abs(positionsOppositeVolume)); decimal pnl = openTokensAmountInUsd - balance; return(new ProfitLossReport(balance, pnl)); }
public async Task <IReadOnlyCollection <IndexSettingsModel> > GetAllAsync() { IReadOnlyCollection <IndexSettings> indexSettings = await _indexSettingsService.GetAllAsync(); return(Mapper.Map <List <IndexSettingsModel> >(indexSettings)); }
private async Task <bool> ValidateSettingsAsync(string assetPairId) { IReadOnlyCollection <IndexSettings> indexSettings = await _indexSettingsService.GetAllAsync(); return(indexSettings.Any(o => o.AssetPairId == assetPairId)); }