public IEnumerable <AdvisorResponse> ListAdvisorsMonthlyRanking(int?year, int?month) { var advisors = AdvisorRankingBusiness.ListAdvisorsFullData(); var advisorsIds = advisors.Select(c => c.Id).Distinct().ToHashSet(); var advisorsFollowers = FollowAdvisorBusiness.ListFollowers(advisorsIds, false); var user = GetLoggedUser(); List <AdvisorMonthlyRanking> ranking = null; List <AdvisorRankingHistory> monthBeginningHistory = null; if (year.HasValue && month.HasValue && !(year == Data.GetDateTimeNow().Year&& month == Data.GetDateTimeNow().Month)) { ranking = AdvisorMonthlyRankingBusiness.ListAdvisorsMonthlyRanking(year.Value, month.Value); } if (ranking == null || ranking.Count == 0) { monthBeginningHistory = AdvisorRankingHistoryBusiness.ListAdvisorsRankingAndProfitForMonthBeginning(advisorsIds); } var result = new List <AdvisorResponse>(); foreach (var advisor in advisors) { if (monthBeginningHistory != null) { var data = monthBeginningHistory.FirstOrDefault(c => c.UserId == advisor.Id); if (data != null) { result.Add(AdvisorRankingBusiness.GetAdvisorResponse(advisor, advisors.Count, advisorsFollowers, user, null, null, data, null)); } } else { var data = ranking.FirstOrDefault(c => c.UserId == advisor.Id); if (data != null) { result.Add(AdvisorRankingBusiness.GetAdvisorResponse(advisor, advisors.Count, advisorsFollowers, user, null, null, null, data)); } } } if (monthBeginningHistory != null) { result = result.OrderByDescending(c => c.MonthlyRankingHistory.ProfitPercentage).ThenByDescending(c => c.UserId).ToList(); for (int i = 0; i < result.Count; ++i) { result[i].MonthlyRankingHistory.Ranking = i + 1; } } else { result = result.OrderBy(c => c.MonthlyRankingHistory.Ranking).ToList(); } return(result); }
public AdvisorResponse GetAdvisorData(int advisorId) { AdvisorRanking advisor = null; List <DomainObjects.Asset.Asset> assets = null; List <FollowAdvisor> advisorsFollowers = null; AdvisorRankingHistory advisorHistory = null; Parallel.Invoke(() => advisor = AdvisorRankingBusiness.GetAdvisorFullData(advisorId), () => assets = AssetBusiness.ListAssets(false), () => advisorsFollowers = FollowAdvisorBusiness.ListFollowers(new int[] { advisorId }, false), () => advisorHistory = AdvisorRankingHistoryBusiness.GetLastAdvisorRankingAndProfit(advisorId)); return(AdvisorRankingBusiness.GetAdvisorResponse(advisor, advisor.TotalAdvisors, advisorsFollowers, GetLoggedUser(), assets, advisorHistory, null, null)); }
public void SetAdvisorsMonthlyRanking() { var now = Data.GetDateTimeNow(); var lastMonth = now.AddMonths(-1); var consideredStartDate = new DateTime(lastMonth.Year, lastMonth.Month, 1, 0, 0, 0, DateTimeKind.Utc); var lastMonthRanking = ListAdvisorsMonthlyRanking(lastMonth.Year, lastMonth.Month); if (lastMonthRanking.Count == 0) { var advisorsId = AdvisorBusiness.ListAllAdvisors().Select(c => c.Id).Distinct().ToHashSet(); var monthlyHistory = AdvisorRankingHistoryBusiness.ListAdvisorRankingAndProfitHistory(advisorsId, consideredStartDate, now); if (monthlyHistory.Any()) { var advisorsMonthlyRanking = new List <AdvisorMonthlyRanking>(); var consideredEndDate = new DateTime(now.Year, now.Month, 1, 0, 0, 0, DateTimeKind.Utc); var advisorsHistory = monthlyHistory.GroupBy(c => c.UserId); foreach (var history in advisorsHistory) { var monthlyAdvisorHistory = history.OrderBy(c => c.ReferenceDate); var firstMonthlyHistory = monthlyAdvisorHistory.FirstOrDefault(c => c.ReferenceDate >= consideredStartDate && c.ReferenceDate < consideredEndDate); var lastMonthlyHistory = monthlyAdvisorHistory.FirstOrDefault(c => c.ReferenceDate >= consideredEndDate); if (firstMonthlyHistory == null || lastMonthlyHistory == null) { continue; } var firstEquity = firstMonthlyHistory.AdvisorProfitHistory.Where(c => c.OrderStatusType != OrderStatusType.Close).Sum(c => c.TotalDollar); var lastEquity = lastMonthlyHistory.AdvisorProfitHistory.Where(c => c.OrderStatusType != OrderStatusType.Close).Sum(c => c.TotalDollar); advisorsMonthlyRanking.Add(new AdvisorMonthlyRanking() { CreationDate = now, Year = lastMonth.Year, Month = lastMonth.Month, UserId = history.Key, AverageReturn = lastEquity / firstEquity - 1 }); } advisorsMonthlyRanking = advisorsMonthlyRanking.OrderByDescending(c => c.AverageReturn).ThenByDescending(c => c.UserId).ToList(); for (var i = 0; i < advisorsMonthlyRanking.Count; ++i) { advisorsMonthlyRanking[i].Ranking = i + 1; } Data.SetAdvisorMonthlyRanking(advisorsMonthlyRanking); } } }
public AdvisorPerformanceResponse GetAdvisorPeformance(int advisorId) { AdvisorRanking advisor = AdvisorRankingBusiness.GetAdvisorFullData(advisorId); if (advisor == null) { throw new NotFoundException("Advisor not found."); } List <DailyPerformanceResponse> dailyPerformance = null; List <Order> closedOrders = null; Parallel.Invoke(() => dailyPerformance = AdvisorRankingHistoryBusiness.ListDailyPerformance(advisorId), () => closedOrders = OrderBusiness.ListOrders(new int[] { advisorId }, null, new OrderStatusType[] { OrderStatusType.Close })); return(new AdvisorPerformanceResponse() { DailyPerformance = dailyPerformance, BestTrade = closedOrders.Any() ? closedOrders.Max(c => c.Profit ?? double.MinValue) : 0, WorstTrade = closedOrders.Any() ? closedOrders.Min(c => c.Profit ?? double.MinValue) : 0, DailyDrawdown = dailyPerformance.Any() ? dailyPerformance.Min(c => c.Variation) : 0 }); }