Skip to content

Commit 32e85bf

Browse files
committed
Added documentation to the rest of the SummaryService
1 parent 5598a54 commit 32e85bf

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

Services/Services/SummaryService.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Services
88
{
99
/// <summary>
10-
/// A service that is responsible for computing summaries (total profit, cost,...) of orders, portfolio entries and
10+
/// A service that is responsible for calculating summaries (total profit, cost,...) of orders, portfolio entries and
1111
/// even portfolios.
1212
/// </summary>
1313
public interface ISummaryService
@@ -27,25 +27,45 @@ public interface ISummaryService
2727
/// </param>
2828
public record Summary(decimal AbsoluteChange, decimal RelativeChange, decimal MarketValue, decimal Cost);
2929

30+
/// <summary>
31+
/// Calculates a summary of the given market order
32+
/// </summary>
33+
/// <param name="order">Order whose summary should be calculated</param>
34+
/// <param name="assetPrice">Market price of the asset to be used when calculating the summary</param>
35+
/// <returns>Summary of the given order</returns>
3036
public Summary GetMarketOrderSummary(MarketOrder order, decimal assetPrice);
3137

38+
/// <summary>
39+
/// Calculates a summary of the given list of orders of a portfolio entry
40+
/// </summary>
41+
/// <param name="portfolioEntryOrders">List of portfolio entries whose summary is to be calculated</param>
42+
/// <param name="assetPrice">Market price of the asset to be used when calculating the summary</param>
43+
/// <returns>A summary of the given list of orders</returns>
3244
public Summary GetPortfolioEntrySummary(List<MarketOrder> portfolioEntryOrders, decimal assetPrice);
3345

46+
/// <summary>
47+
/// Calculates a summary of the given portfolio entry summaries
48+
/// </summary>
49+
/// <param name="portfolioEntrySummaries">List of entry summaries whos summary is to be calculated</param>
50+
/// <returns>Summary of the given list of summaries</returns>
3451
public Summary GetPortfolioSummary(List<Summary> portfolioEntrySummaries);
3552
}
3653

3754
public class SummaryServiceImpl : ISummaryService
3855
{
3956
public ISummaryService.Summary GetMarketOrderSummary(MarketOrder order, decimal assetPrice)
4057
{
58+
// order summary does not take into account whether it was a buy or a sell (same as Blockfolio app)
4159
var marketValue = order.Size * assetPrice;
4260
var cost = (order.Size * order.FilledPrice) + order.Fee;
4361
if (cost == 0)
4462
{
63+
// when the cost is zero do not compute anything else
4564
return new(0, 0, 0, 0);
4665
}
4766

4867
var relativeChange = (marketValue / cost) - new decimal(1);
68+
// absolute change is the difference between the current market value of the order subtracted by order's cost
4969
var absoluteChange = marketValue - cost;
5070
return new(absoluteChange, relativeChange, marketValue, cost);
5171
}
@@ -56,7 +76,7 @@ public ISummaryService.Summary GetAverageOfSummaries(IEnumerable<ISummaryService
5676
decimal totalCost = 0;
5777
decimal totalAbsoluteChange = 0;
5878

59-
// iterate over all orders and compute their summaries
79+
// iterate over summaries and sum market value, cost and absolute change
6080
foreach (var summary in summaries)
6181
{
6282
totalMarketValue += summary.MarketValue;
@@ -66,6 +86,7 @@ public ISummaryService.Summary GetAverageOfSummaries(IEnumerable<ISummaryService
6686

6787
if (totalCost == 0)
6888
{
89+
// when the cost is zero, do not compute anything else
6990
return new ISummaryService.Summary(0, 0, 0, 0);
7091
}
7192

@@ -81,10 +102,14 @@ public ISummaryService.Summary GetPortfolioEntrySummary(List<MarketOrder> portfo
81102
decimal totalSellValue = 0;
82103
decimal totalCost = 0;
83104
decimal totalFee = 0;
105+
106+
// compute summary of market orders in the same was as the Blockfolio does
84107
portfolioEntryOrders
85108
.ForEach(order =>
86109
{
110+
// determine the holding size (negative when the order was a sell) and add it tot he sum of all holdings
87111
totalHoldingSize += order.Size * (order.Buy ? 1 : -1);
112+
// compute the value of the order
88113
var orderValue = order.Size * order.FilledPrice;
89114
if (!order.Buy)
90115
{
@@ -103,15 +128,18 @@ public ISummaryService.Summary GetPortfolioEntrySummary(List<MarketOrder> portfo
103128
return new ISummaryService.Summary(0, 0, 0, 0);
104129
}
105130

131+
// current total holding value is computing by multiplying the total holding size with the given price of the asset
106132
decimal currentTotalHoldingValue = totalHoldingSize * assetPrice;
107133

134+
// use the same formula as Blockfolio app does to compute the total absolute change
108135
decimal totalAbsoluteChange = currentTotalHoldingValue + totalSellValue - totalCost - totalFee;
109136
decimal totalRelativeChange = totalAbsoluteChange / (totalCost + totalFee);
110137

111138
return new ISummaryService.Summary(totalAbsoluteChange, totalRelativeChange, currentTotalHoldingValue,
112139
totalCost + totalFee);
113140
}
114141

142+
// summary of a portfolio is calculated by computing the average of all entry summaries present in it
115143
public ISummaryService.Summary GetPortfolioSummary(List<ISummaryService.Summary> portfolioEntrySummaries) =>
116144
GetAverageOfSummaries(portfolioEntrySummaries);
117145
}

0 commit comments

Comments
 (0)