Exemplo n.º 1
0
 private MonthlyRankingHistoryResponse GetMonthlyRankingHistoryResponse(double equity, AdvisorRankingHistory advisorMonthBeginningHistory, AdvisorMonthlyRanking advisorMonthlyRanking)
 {
     if (advisorMonthBeginningHistory != null)
     {
         var historyMonthBeginningPortfolioValue = advisorMonthBeginningHistory.AdvisorProfitHistory.Where(c => c.OrderStatusType != OrderStatusType.Close).Sum(c => c.TotalDollar);
         return(new MonthlyRankingHistoryResponse()
         {
             PortfolioReferenceDate = advisorMonthBeginningHistory.ReferenceDate,
             PortfolioValue = historyMonthBeginningPortfolioValue,
             ProfitPercentage = equity / historyMonthBeginningPortfolioValue - 1
         });
     }
     else if (advisorMonthlyRanking != null)
     {
         return(new MonthlyRankingHistoryResponse()
         {
             Ranking = advisorMonthlyRanking.Ranking,
             ProfitPercentage = advisorMonthlyRanking.AverageReturn
         });
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 2
0
        public AdvisorResponse GetAdvisorResponse(AdvisorRanking advisorRanking, int totalAdvisors, IEnumerable <FollowAdvisor> advisorFollowers, User loggedUser,
                                                  List <DomainObjects.Asset.Asset> assets, AdvisorRankingHistory advisorLastDayHistory, AdvisorRankingHistory advisorMonthBeginningHistory,
                                                  AdvisorMonthlyRanking advisorMonthlyRanking)
        {
            var advFollowers                    = advisorFollowers?.Where(c => c.AdvisorId == advisorRanking.Id);
            var totalAvailable                  = advisorRanking.AdvisorProfit.First(c => c.AssetId == AssetUSDId).TotalDollar;
            var totalOpenOrders                 = advisorRanking.AdvisorProfit.Where(c => c.AssetId != AssetUSDId && c.OrderStatusType == OrderStatusType.Open).Sum(c => c.TotalDollar);
            var totalAllocated                  = advisorRanking.AdvisorProfit.Where(c => c.AssetId != AssetUSDId && c.OrderStatusType == OrderStatusType.Executed).Sum(c => c.TotalDollar - c.SummedProfitDollar);
            var consideredProfits               = advisorRanking.AdvisorProfit.Where(c => c.AssetId != AssetUSDId && c.OrderStatusType != OrderStatusType.Open);
            var historyLastDayTotalAvailable    = advisorLastDayHistory?.AdvisorProfitHistory.First(c => c.AssetId == AssetUSDId).TotalDollar;
            var historyLastDayTotalAllocated    = advisorLastDayHistory?.AdvisorProfitHistory.Where(c => c.AssetId != AssetUSDId && c.OrderStatusType != OrderStatusType.Close).Sum(c => c.TotalDollar);
            var historyLastDayConsideredProfits = advisorLastDayHistory?.AdvisorProfitHistory.Where(c => c.AssetId != AssetUSDId && c.OrderStatusType != OrderStatusType.Open);
            var runningProfits                  = consideredProfits.Where(c => c.OrderStatusType == OrderStatusType.Executed);
            var assetsTraded                    = consideredProfits.Select(c => c.AssetId);
            var totalOrders   = consideredProfits.Any() ? consideredProfits.Sum(c => c.OrderCount) : 0;
            var successRate   = totalOrders > 0 ? (double)consideredProfits.Sum(c => c.SuccessCount) / totalOrders : 0.0;
            var averageReturn = totalOrders > 0 ? consideredProfits.Sum(c => c.SummedProfitDollar) / consideredProfits.Sum(c => c.TotalDollar - c.SummedProfitDollar) : 0.0;
            var openProfit    = runningProfits.Any() ? runningProfits.Sum(c => c.SummedProfitDollar) : 0.0;
            var totalVirtual  = totalAvailable + totalAllocated + totalOpenOrders + openProfit;
            var historyLastDayPortfolioValue = historyLastDayTotalAvailable.HasValue ? historyLastDayTotalAvailable + historyLastDayTotalAllocated : (double?)null;
            var allProfit            = totalVirtual - VirtualMoney;
            var allProfitPercentage  = totalVirtual / VirtualMoney - 1;
            var closedOrdersWithTime = consideredProfits.Where(c => c.OrderStatusType == OrderStatusType.Close && c.SummedTradeMinutes.HasValue);
            var averageTradeMinutes  = closedOrdersWithTime.Any() ? closedOrdersWithTime.Sum(c => c.SummedTradeMinutes.Value) / closedOrdersWithTime.Sum(c => c.OrderCount) : (int?)null;

            List <AdvisorAssetResponse> openPositions   = null;
            List <AdvisorAssetResponse> closedPositions = null;

            closedPositions = consideredProfits.Where(c => c.OrderStatusType == OrderStatusType.Close).Select(c => GetAdvisorAssetResponse(c, assets)).Where(c => c != null).OrderByDescending(c => c.OrderCount).ToList();
            openPositions   = runningProfits.Select(c => GetAdvisorAssetResponse(c, assets)).Where(c => c != null).OrderByDescending(c => c.TotalInvested).ToList();

            List <AdvisorAssetHistoryResponse> assetsHistory = new List <AdvisorAssetHistoryResponse>();

            if (historyLastDayConsideredProfits?.Any() == true)
            {
                var assetData = historyLastDayConsideredProfits.GroupBy(c => c.AssetId);
                foreach (var data in assetData)
                {
                    var assetCurrentValue = 0.0;
                    var assetCurrentData  = closedPositions.Where(c => c.AssetId == data.Key).Concat(openPositions.Where(c => c.AssetId == data.Key));
                    if (assetCurrentData.Any())
                    {
                        assetCurrentValue = assetCurrentData.Sum(c => c.TotalVirtual);
                    }

                    var totalDollar    = data.Sum(c => c.TotalDollar);
                    var totalQuantity  = data.Sum(c => c.TotalQuantity);
                    var totalProfit    = data.Sum(c => c.SummedProfitDollar);
                    var assetLastValue = totalDollar + totalProfit;
                    assetsHistory.Add(new AdvisorAssetHistoryResponse()
                    {
                        AssetId             = data.Key,
                        TotalInvested       = totalDollar,
                        TotalQuantity       = totalQuantity,
                        TotalProfit         = totalProfit,
                        TotalVirtual        = assetLastValue,
                        AveragePrice        = totalDollar / totalQuantity,
                        Profit24hValue      = assetCurrentValue != 0 ? assetCurrentValue - assetLastValue : (double?)null,
                        Profit24hPercentage = assetCurrentValue != 0 ? assetCurrentValue / assetLastValue - 1 : (double?)null
                    });
                }
            }
            return(new AdvisorResponse()
            {
                UserId = advisorRanking.Id,
                Name = advisorRanking.Name,
                UrlGuid = advisorRanking.UrlGuid.ToString(),
                CreationDate = advisorRanking.BecameAdvisorDate,
                Description = advisorRanking.Description,
                Owner = loggedUser != null && advisorRanking.Id == loggedUser.Id,
                NumberOfFollowers = advFollowers?.Count() ?? 0,
                TotalAssetsTraded = assetsTraded.Any() ? assetsTraded.Distinct().Count() : 0,
                TotalTrades = totalOrders,
                Following = loggedUser != null && loggedUser?.FollowedAdvisors?.Any(c => c == advisorRanking.Id) == true,
                AverageReturn = averageReturn,
                SuccessRate = successRate,
                Ranking = advisorRanking.Ranking,
                Rating = advisorRanking.Rating,
                TotalAdvisors = totalAdvisors,
                TotalAllocated = totalAllocated,
                TotalAvailable = totalAvailable,
                TotalBalance = totalOpenOrders + totalAvailable,
                Equity = totalVirtual,
                TotalProfit = allProfit,
                TotalProfitPercentage = allProfitPercentage,
                AverageTradeMinutes = averageTradeMinutes,
                ClosedPositions = closedPositions,
                OpenPositions = openPositions,
                TotalFee = closedPositions.Sum(c => c.TotalFee ?? 0) + openPositions.Sum(c => c.TotalFee ?? 0),
                LastPortfolioReferenceDate = advisorLastDayHistory?.ReferenceDate,
                LastPortfolioValue = historyLastDayPortfolioValue,
                Profit24hValue = historyLastDayPortfolioValue.HasValue ? totalVirtual - historyLastDayPortfolioValue : (double?)null,
                Profit24hPercentage = historyLastDayPortfolioValue.HasValue ? totalVirtual / historyLastDayPortfolioValue - 1 : (double?)null,
                AdvisorAsset24hHistory = assetsHistory,
                MonthlyRankingHistory = GetMonthlyRankingHistoryResponse(totalVirtual, advisorMonthBeginningHistory, advisorMonthlyRanking),
                RecommendationDistribution = runningProfits.Any() ? runningProfits.GroupBy(c => c.Type).Select(g => new RecommendationDistributionResponse()
                {
                    Type = g.Key, Total = g.Sum(c => c.OrderCount)
                }).ToList() : new List <RecommendationDistributionResponse>(),
            });
        }