Browse Source
Task/include first and last date of each calendar year in getChartDateMap() (#6069)
* Include first and last date of each calendar year in getChartDateMap()
* Update changelog
pull/5843/merge
Sven Günther
5 days ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with
69 additions and
0 deletions
-
CHANGELOG.md
-
apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
-
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy.spec.ts
-
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur-in-base-currency-eur.spec.ts
-
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur.spec.ts
-
apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd.spec.ts
|
|
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 |
|
|
|
|
|
|
|
## Unreleased |
|
|
|
|
|
|
|
### Added |
|
|
|
|
|
|
|
- Included the calendar year boundaries in the portfolio calculations |
|
|
|
|
|
|
|
### Changed |
|
|
|
|
|
|
|
- Removed the deprecated _Angular CLI_ decorator (`decorate-angular-cli.js`) |
|
|
|
|
|
|
|
@ -44,11 +44,15 @@ import { plainToClass } from 'class-transformer'; |
|
|
|
import { |
|
|
|
differenceInDays, |
|
|
|
eachDayOfInterval, |
|
|
|
eachYearOfInterval, |
|
|
|
endOfDay, |
|
|
|
endOfYear, |
|
|
|
format, |
|
|
|
isAfter, |
|
|
|
isBefore, |
|
|
|
isWithinInterval, |
|
|
|
min, |
|
|
|
startOfYear, |
|
|
|
subDays |
|
|
|
} from 'date-fns'; |
|
|
|
import { isNumber, sortBy, sum, uniqBy } from 'lodash'; |
|
|
|
@ -889,6 +893,24 @@ export abstract class PortfolioCalculator { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Make sure the first and last date of each calendar year is present
|
|
|
|
const interval = { start: startDate, end: endDate }; |
|
|
|
|
|
|
|
for (const date of eachYearOfInterval(interval)) { |
|
|
|
const yearStart = startOfYear(date); |
|
|
|
const yearEnd = endOfYear(date); |
|
|
|
|
|
|
|
if (isWithinInterval(yearStart, interval)) { |
|
|
|
// Add start of year (YYYY-01-01)
|
|
|
|
chartDateMap[format(yearStart, DATE_FORMAT)] = true; |
|
|
|
} |
|
|
|
|
|
|
|
if (isWithinInterval(yearEnd, interval)) { |
|
|
|
// Add end of year (YYYY-12-31)
|
|
|
|
chartDateMap[format(yearEnd, DATE_FORMAT)] = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return chartDateMap; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -109,6 +109,12 @@ describe('PortfolioCalculator', () => { |
|
|
|
|
|
|
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
|
|
|
|
|
|
|
const historicalDataDates = portfolioSnapshot.historicalData.map( |
|
|
|
({ date }) => { |
|
|
|
return date; |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
const investments = portfolioCalculator.getInvestments(); |
|
|
|
|
|
|
|
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
|
|
|
@ -170,8 +176,12 @@ describe('PortfolioCalculator', () => { |
|
|
|
totalLiabilitiesWithCurrencyEffect: new Big('0') |
|
|
|
}); |
|
|
|
|
|
|
|
expect(historicalDataDates).not.toContain('2021-01-01'); |
|
|
|
expect(historicalDataDates).not.toContain('2021-12-31'); |
|
|
|
|
|
|
|
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
|
|
|
expect.objectContaining({ |
|
|
|
date: '2021-12-18', |
|
|
|
netPerformance: 23.05, |
|
|
|
netPerformanceInPercentage: 0.08437042459736457, |
|
|
|
netPerformanceInPercentageWithCurrencyEffect: 0.08437042459736457, |
|
|
|
|
|
|
|
@ -130,6 +130,17 @@ describe('PortfolioCalculator', () => { |
|
|
|
|
|
|
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
|
|
|
|
|
|
|
const historicalDataDates = portfolioSnapshot.historicalData.map( |
|
|
|
({ date }) => { |
|
|
|
return date; |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
expect(historicalDataDates).not.toContain('2021-01-01'); |
|
|
|
expect(historicalDataDates).toContain('2021-12-31'); |
|
|
|
expect(historicalDataDates).toContain('2022-01-01'); |
|
|
|
expect(historicalDataDates).not.toContain('2022-12-31'); |
|
|
|
|
|
|
|
expect(portfolioSnapshot.positions[0].fee).toEqual(new Big(4.46)); |
|
|
|
expect( |
|
|
|
portfolioSnapshot.positions[0].feeInBaseCurrency.toNumber() |
|
|
|
|
|
|
|
@ -118,6 +118,12 @@ describe('PortfolioCalculator', () => { |
|
|
|
|
|
|
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
|
|
|
|
|
|
|
const historicalDataDates = portfolioSnapshot.historicalData.map( |
|
|
|
({ date }) => { |
|
|
|
return date; |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
const investments = portfolioCalculator.getInvestments(); |
|
|
|
|
|
|
|
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
|
|
|
@ -225,6 +231,11 @@ describe('PortfolioCalculator', () => { |
|
|
|
totalLiabilitiesWithCurrencyEffect: new Big('0') |
|
|
|
}); |
|
|
|
|
|
|
|
expect(historicalDataDates).not.toContain('2021-01-01'); |
|
|
|
expect(historicalDataDates).toContain('2021-12-31'); |
|
|
|
expect(historicalDataDates).toContain('2022-01-01'); |
|
|
|
expect(historicalDataDates).not.toContain('2022-12-31'); |
|
|
|
|
|
|
|
expect(investments).toEqual([ |
|
|
|
{ date: '2021-12-12', investment: new Big('44558.42') } |
|
|
|
]); |
|
|
|
|
|
|
|
@ -118,6 +118,12 @@ describe('PortfolioCalculator', () => { |
|
|
|
|
|
|
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
|
|
|
|
|
|
|
const historicalDataDates = portfolioSnapshot.historicalData.map( |
|
|
|
({ date }) => { |
|
|
|
return date; |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
const investments = portfolioCalculator.getInvestments(); |
|
|
|
|
|
|
|
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
|
|
|
@ -225,6 +231,11 @@ describe('PortfolioCalculator', () => { |
|
|
|
totalLiabilitiesWithCurrencyEffect: new Big('0') |
|
|
|
}); |
|
|
|
|
|
|
|
expect(historicalDataDates).not.toContain('2021-01-01'); |
|
|
|
expect(historicalDataDates).toContain('2021-12-31'); |
|
|
|
expect(historicalDataDates).toContain('2022-01-01'); |
|
|
|
expect(historicalDataDates).not.toContain('2022-12-31'); |
|
|
|
|
|
|
|
expect(investments).toEqual([ |
|
|
|
{ date: '2021-12-12', investment: new Big('44558.42') } |
|
|
|
]); |
|
|
|
|