Browse Source

Carry forward of market prices between chart dates

pull/8009/head
Thomas Kaul 2 weeks ago
parent
commit
c5198c2678
  1. 9
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
  2. 10
      apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-manual-buy-with-market-price-between-chart-dates.spec.ts
  3. 4
      apps/api/src/app/portfolio/current-rate.service.mock.ts

9
apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

@ -810,7 +810,7 @@ export abstract class PortfolioCalculator {
while ( while (
indexOfMarketPriceDate < marketPriceDates.length && indexOfMarketPriceDate < marketPriceDates.length &&
marketPriceDates[indexOfMarketPriceDate] < dateString marketPriceDates[indexOfMarketPriceDate] <= dateString
) { ) {
lastMarketPrice = lastMarketPrice =
marketSymbolMap[marketPriceDates[indexOfMarketPriceDate]][ marketSymbolMap[marketPriceDates[indexOfMarketPriceDate]][
@ -830,13 +830,8 @@ export abstract class PortfolioCalculator {
} }
} }
const marketPrice = marketSymbolMap[dateString]?.[assetProfileIdentifier];
const unitPrice = const unitPrice =
marketPrice ?? lastMarketPrice ?? lastActivityUnitPrice ?? unitPriceAtEndDate;
lastMarketPrice ??
lastActivityUnitPrice ??
unitPriceAtEndDate;
if (activitiesOfDate?.length > 0) { if (activitiesOfDate?.length > 0) {
for (const activity of activitiesOfDate) { for (const activity of activitiesOfDate) {

10
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-manual-buy-with-market-price-between-chart-dates.spec.ts

@ -120,13 +120,14 @@ describe('PortfolioCalculator', () => {
); );
/** /**
* The only historical market price is on 2023-06-15, which is not a * The only historical market prices are on 2023-06-14 and 2023-06-15,
* chart date (every third day from 2021-01-03) * which are not chart dates (every third day from 2021-01-03)
*/ */
expect(historicalDataByDate['2023-06-14']).toBeUndefined();
expect(historicalDataByDate['2023-06-15']).toBeUndefined(); expect(historicalDataByDate['2023-06-15']).toBeUndefined();
/** /**
* The chart dates before the market price use the unit price of the * The chart dates before the market prices use the unit price of the
* activity: 50 * activity: 50
*/ */
expect(historicalDataByDate['2023-06-13']).toMatchObject({ expect(historicalDataByDate['2023-06-13']).toMatchObject({
@ -136,7 +137,8 @@ describe('PortfolioCalculator', () => {
}); });
/** /**
* The chart dates after the market price carry it forward: 100 * The chart dates after the market prices carry the latest one forward
* (100), not the first one (80)
*/ */
expect(historicalDataByDate['2023-06-16']).toMatchObject({ expect(historicalDataByDate['2023-06-16']).toMatchObject({
netPerformance: 500, // 10 * (100 - 50) = 500 netPerformance: 500, // 10 * (100 - 50) = 500

4
apps/api/src/app/portfolio/current-rate.service.mock.ts

@ -33,7 +33,9 @@ function mockGetValue({
case '6c0c5cee-0208-4975-b473-03baf2518497': case '6c0c5cee-0208-4975-b473-03baf2518497':
// Sparse market data with the quote of today, which falls back to the // Sparse market data with the quote of today, which falls back to the
// latest market price // latest market price
if ( if (isSameDay(parseDate('2023-06-14'), date)) {
return { marketPrice: 80 };
} else if (
isSameDay(parseDate('2023-06-15'), date) || isSameDay(parseDate('2023-06-15'), date) ||
isSameDay(parseDate('2024-01-31'), date) isSameDay(parseDate('2024-01-31'), date)
) { ) {

Loading…
Cancel
Save