コード例 #1
0
        private async Task <AssetRealisedPnL> CalculateAsync(TradeData tradeData, string walletId, string assetId)
        {
            AssetRealisedPnL prevAssetPnL = await _assetRealisedPnLRepository.GetLastAsync(walletId, assetId) ??
                                            new AssetRealisedPnL();

            bool inverted = tradeData.QuoteAsset == assetId;

            string crossAssetId = inverted
                ? tradeData.BaseAsset
                : tradeData.QuoteAsset;

            Quote quote = await _quoteService.GetAsync(assetId, QuoteAssetId);

            Quote crossQuote = await _quoteService.GetAsync(crossAssetId, QuoteAssetId);

            RealisedPnLResult realisedPnLResult = RealisedPnLCalculator.Calculate(
                tradeData.Price,
                tradeData.Volume,
                inverted,
                tradeData.Type == TradeType.Sell ? -1 : 1,
                prevAssetPnL.CumulativeVolume,
                prevAssetPnL.CumulativeOppositeVolume,
                quote.Mid,
                prevAssetPnL.AvgPrice,
                crossQuote.Mid);

            return(new AssetRealisedPnL
            {
                WalletId = walletId,
                AssetId = assetId,
                Time = tradeData.Time,
                Exchange = tradeData.Exchange,

                TradeId = tradeData.Id,
                TradeAssetPair = tradeData.AssetPair,
                TradePrice = tradeData.Price,
                TradeVolume = tradeData.Volume,
                TradeType = tradeData.Type,

                CrossAssetPair = crossQuote.AssetPair,
                CrossPrice = crossQuote.Mid,

                Price = realisedPnLResult.Price,
                Volume = realisedPnLResult.Volume,
                OppositeVolume = realisedPnLResult.OppositeVolume,
                Inverted = inverted,

                PrevAvgPrice = prevAssetPnL.AvgPrice,
                PrevCumulativeVolume = prevAssetPnL.CumulativeVolume,
                PrevCumulativeOppositeVolume = prevAssetPnL.CumulativeOppositeVolume,

                OpenPrice = prevAssetPnL.AvgPrice,
                ClosePrice = realisedPnLResult.Price,
                CloseVolume = realisedPnLResult.ClosedVolume,
                RealisedPnL = realisedPnLResult.RealisedPnL,

                AvgPrice = realisedPnLResult.AvgPrice,
                CumulativeVolume = realisedPnLResult.CumulativeVolume,
                CumulativeOppositeVolume = realisedPnLResult.CumulativeOppositeVolume,
                CumulativeRealisedPnL = prevAssetPnL.CumulativeRealisedPnL + realisedPnLResult.RealisedPnL,

                Rate = quote.Mid,
                UnrealisedPnL = realisedPnLResult.UnrealisedPnL,

                LimitOrderId = tradeData.LimitOrderId,
                OppositeClientId = tradeData.OppositeClientId,
                OppositeLimitOrderId = tradeData.OppositeLimitOrderId
            });
        }