mirror of https://github.com/ghostfolio/ghostfolio
committed by
GitHub
11 changed files with 1777 additions and 34 deletions
@ -0,0 +1,185 @@ |
|||||
|
import { |
||||
|
loadActivitiesFromExportFile, |
||||
|
userDummyData |
||||
|
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
||||
|
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
||||
|
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
||||
|
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
||||
|
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
||||
|
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
||||
|
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
||||
|
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
||||
|
import { parseDate } from '@ghostfolio/common/helper'; |
||||
|
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
||||
|
|
||||
|
import { Big } from 'big.js'; |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
||||
|
return { |
||||
|
CurrentRateService: jest.fn().mockImplementation(() => { |
||||
|
return CurrentRateServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
jest.mock( |
||||
|
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
||||
|
() => { |
||||
|
return { |
||||
|
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
||||
|
return PortfolioSnapshotServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
||||
|
return { |
||||
|
RedisCacheService: jest.fn().mockImplementation(() => { |
||||
|
return RedisCacheServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
describe('PortfolioCalculator (ROI)', () => { |
||||
|
let configurationService: ConfigurationService; |
||||
|
let currentRateService: CurrentRateService; |
||||
|
let exchangeRateDataService: ExchangeRateDataService; |
||||
|
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
||||
|
let portfolioSnapshotService: PortfolioSnapshotService; |
||||
|
let redisCacheService: RedisCacheService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
PortfolioSnapshotServiceMock.reset(); |
||||
|
RedisCacheServiceMock.reset(); |
||||
|
|
||||
|
configurationService = new ConfigurationService(); |
||||
|
|
||||
|
currentRateService = new CurrentRateService(null, null, null); |
||||
|
|
||||
|
exchangeRateDataService = new ExchangeRateDataService( |
||||
|
null, |
||||
|
null, |
||||
|
null, |
||||
|
null |
||||
|
); |
||||
|
|
||||
|
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
||||
|
|
||||
|
redisCacheService = new RedisCacheService(null, null); |
||||
|
|
||||
|
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
||||
|
configurationService, |
||||
|
currentRateService, |
||||
|
exchangeRateDataService, |
||||
|
portfolioSnapshotService, |
||||
|
redisCacheService |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
describe('get current positions', () => { |
||||
|
it.only('with BALN.SW buy and buy', async () => { |
||||
|
jest.useFakeTimers().setSystemTime(parseDate('2021-12-18').getTime()); |
||||
|
|
||||
|
const { activities, userCurrency } = loadActivitiesFromExportFile( |
||||
|
'baln-buy-and-buy.json' |
||||
|
); |
||||
|
|
||||
|
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
|
activities, |
||||
|
calculationType: PerformanceCalculationType.ROI, |
||||
|
currency: userCurrency, |
||||
|
userId: userDummyData.id |
||||
|
}); |
||||
|
|
||||
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
|
||||
|
const investments = portfolioCalculator.getInvestments(); |
||||
|
|
||||
|
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
||||
|
data: portfolioSnapshot.historicalData, |
||||
|
groupBy: 'month' |
||||
|
}); |
||||
|
|
||||
|
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
||||
|
data: portfolioSnapshot.historicalData, |
||||
|
groupBy: 'year' |
||||
|
}); |
||||
|
|
||||
|
expect(portfolioSnapshot).toMatchObject({ |
||||
|
currentValueInBaseCurrency: new Big('595.6'), |
||||
|
errors: [], |
||||
|
hasErrors: false, |
||||
|
positions: [ |
||||
|
{ |
||||
|
activitiesCount: 2, |
||||
|
averageInvestment: new Big('559'), |
||||
|
averageInvestmentWithCurrencyEffect: new Big('559'), |
||||
|
averagePrice: new Big('139.75'), |
||||
|
currency: 'CHF', |
||||
|
dataSource: 'YAHOO', |
||||
|
dateOfFirstActivity: '2021-11-22', |
||||
|
dividend: new Big('0'), |
||||
|
dividendInBaseCurrency: new Big('0'), |
||||
|
fee: new Big('3.2'), |
||||
|
feeInBaseCurrency: new Big('3.2'), |
||||
|
grossPerformance: new Big('36.6'), |
||||
|
grossPerformancePercentage: new Big('0.0654740608228980322'), |
||||
|
grossPerformancePercentageWithCurrencyEffect: new Big( |
||||
|
'0.0654740608228980322' |
||||
|
), |
||||
|
grossPerformanceWithCurrencyEffect: new Big('36.6'), |
||||
|
investment: new Big('559'), |
||||
|
investmentWithCurrencyEffect: new Big('559'), |
||||
|
netPerformance: new Big('33.4'), |
||||
|
netPerformancePercentage: new Big('0.05974955277280858676'), |
||||
|
netPerformancePercentageWithCurrencyEffectMap: { |
||||
|
max: new Big('0.05974955277280858676') |
||||
|
}, |
||||
|
netPerformanceWithCurrencyEffectMap: { |
||||
|
max: new Big('33.4') |
||||
|
}, |
||||
|
marketPrice: 148.9, |
||||
|
marketPriceInBaseCurrency: 148.9, |
||||
|
quantity: new Big('4'), |
||||
|
symbol: 'BALN.SW', |
||||
|
tags: [], |
||||
|
valueInBaseCurrency: new Big('595.6') |
||||
|
} |
||||
|
], |
||||
|
totalFeesWithCurrencyEffect: new Big('3.2'), |
||||
|
totalInterestWithCurrencyEffect: new Big('0'), |
||||
|
totalInvestment: new Big('559'), |
||||
|
totalInvestmentWithCurrencyEffect: new Big('559'), |
||||
|
totalLiabilitiesWithCurrencyEffect: new Big('0') |
||||
|
}); |
||||
|
|
||||
|
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
||||
|
expect.objectContaining({ |
||||
|
netPerformance: 33.4, |
||||
|
netPerformanceInPercentage: 0.059749552772808585, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: 0.059749552772808585, |
||||
|
netPerformanceWithCurrencyEffect: 33.4, |
||||
|
totalInvestment: 559, |
||||
|
totalInvestmentValueWithCurrencyEffect: 559 |
||||
|
}) |
||||
|
); |
||||
|
|
||||
|
expect(investments).toEqual([ |
||||
|
{ date: '2021-11-22', investment: new Big('285.8') }, |
||||
|
{ date: '2021-11-30', investment: new Big('559') } |
||||
|
]); |
||||
|
|
||||
|
expect(investmentsByMonth).toEqual([ |
||||
|
{ date: '2021-11-01', investment: 559 }, |
||||
|
{ date: '2021-12-01', investment: 0 } |
||||
|
]); |
||||
|
|
||||
|
expect(investmentsByYear).toEqual([ |
||||
|
{ date: '2021-01-01', investment: 559 } |
||||
|
]); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,222 @@ |
|||||
|
import { |
||||
|
getPerformanceByDateRange, |
||||
|
loadActivitiesFromExportFile, |
||||
|
userDummyData |
||||
|
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
||||
|
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
||||
|
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
||||
|
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
||||
|
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
||||
|
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
||||
|
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
||||
|
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
||||
|
import { parseDate } from '@ghostfolio/common/helper'; |
||||
|
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
||||
|
|
||||
|
import { Big } from 'big.js'; |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
||||
|
return { |
||||
|
CurrentRateService: jest.fn().mockImplementation(() => { |
||||
|
return CurrentRateServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
jest.mock( |
||||
|
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
||||
|
() => { |
||||
|
return { |
||||
|
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
||||
|
return PortfolioSnapshotServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
||||
|
return { |
||||
|
RedisCacheService: jest.fn().mockImplementation(() => { |
||||
|
return RedisCacheServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
describe('PortfolioCalculator (ROI)', () => { |
||||
|
let configurationService: ConfigurationService; |
||||
|
let currentRateService: CurrentRateService; |
||||
|
let exchangeRateDataService: ExchangeRateDataService; |
||||
|
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
||||
|
let portfolioSnapshotService: PortfolioSnapshotService; |
||||
|
let redisCacheService: RedisCacheService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
PortfolioSnapshotServiceMock.reset(); |
||||
|
RedisCacheServiceMock.reset(); |
||||
|
|
||||
|
configurationService = new ConfigurationService(); |
||||
|
|
||||
|
currentRateService = new CurrentRateService(null, null, null); |
||||
|
|
||||
|
exchangeRateDataService = new ExchangeRateDataService( |
||||
|
null, |
||||
|
null, |
||||
|
null, |
||||
|
null |
||||
|
); |
||||
|
|
||||
|
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
||||
|
|
||||
|
redisCacheService = new RedisCacheService(null, null); |
||||
|
|
||||
|
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
||||
|
configurationService, |
||||
|
currentRateService, |
||||
|
exchangeRateDataService, |
||||
|
portfolioSnapshotService, |
||||
|
redisCacheService |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
describe('get current positions', () => { |
||||
|
it.only('with BALN.SW buy and sell', async () => { |
||||
|
jest.useFakeTimers().setSystemTime(parseDate('2021-12-18').getTime()); |
||||
|
|
||||
|
const { activities, userCurrency } = loadActivitiesFromExportFile( |
||||
|
'baln-buy-and-sell.json' |
||||
|
); |
||||
|
|
||||
|
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
|
activities, |
||||
|
calculationType: PerformanceCalculationType.ROI, |
||||
|
currency: userCurrency, |
||||
|
usePortfolioSnapshotCache: false, |
||||
|
userId: userDummyData.id |
||||
|
}); |
||||
|
|
||||
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
|
||||
|
const performanceByDateRange = await getPerformanceByDateRange({ |
||||
|
portfolioCalculator, |
||||
|
dateRanges: ['1d', 'max', 'ytd'] |
||||
|
}); |
||||
|
|
||||
|
const investments = portfolioCalculator.getInvestments(); |
||||
|
|
||||
|
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
||||
|
data: portfolioSnapshot.historicalData, |
||||
|
groupBy: 'month' |
||||
|
}); |
||||
|
|
||||
|
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
||||
|
data: portfolioSnapshot.historicalData, |
||||
|
groupBy: 'year' |
||||
|
}); |
||||
|
|
||||
|
expect(portfolioSnapshot).toMatchObject({ |
||||
|
currentValueInBaseCurrency: new Big('0'), |
||||
|
errors: [], |
||||
|
hasErrors: false, |
||||
|
positions: [ |
||||
|
{ |
||||
|
activitiesCount: 2, |
||||
|
averageInvestment: new Big('285.8'), |
||||
|
averageInvestmentWithCurrencyEffect: new Big('285.8'), |
||||
|
averagePrice: new Big('0'), |
||||
|
currency: 'CHF', |
||||
|
dataSource: 'YAHOO', |
||||
|
dateOfFirstActivity: '2021-11-22', |
||||
|
dividend: new Big('0'), |
||||
|
dividendInBaseCurrency: new Big('0'), |
||||
|
fee: new Big('3.2'), |
||||
|
feeInBaseCurrency: new Big('3.2'), |
||||
|
grossPerformance: new Big('-12.6'), |
||||
|
grossPerformancePercentage: new Big('-0.0440867739678096571'), |
||||
|
grossPerformancePercentageWithCurrencyEffect: new Big( |
||||
|
'-0.0440867739678096571' |
||||
|
), |
||||
|
grossPerformanceWithCurrencyEffect: new Big('-12.6'), |
||||
|
investment: new Big('0'), |
||||
|
investmentWithCurrencyEffect: new Big('0'), |
||||
|
netPerformance: new Big('-15.8'), |
||||
|
netPerformancePercentage: new Big('-0.0552834149755073478'), |
||||
|
netPerformancePercentageWithCurrencyEffectMap: { |
||||
|
max: new Big('-0.0552834149755073478') |
||||
|
}, |
||||
|
netPerformanceWithCurrencyEffectMap: { |
||||
|
max: new Big('-15.8') |
||||
|
}, |
||||
|
marketPrice: 148.9, |
||||
|
marketPriceInBaseCurrency: 148.9, |
||||
|
quantity: new Big('0'), |
||||
|
symbol: 'BALN.SW', |
||||
|
tags: [], |
||||
|
valueInBaseCurrency: new Big('0') |
||||
|
} |
||||
|
], |
||||
|
totalFeesWithCurrencyEffect: new Big('3.2'), |
||||
|
totalInterestWithCurrencyEffect: new Big('0'), |
||||
|
totalInvestment: new Big('0'), |
||||
|
totalInvestmentWithCurrencyEffect: new Big('0'), |
||||
|
totalLiabilitiesWithCurrencyEffect: new Big('0') |
||||
|
}); |
||||
|
|
||||
|
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
||||
|
expect.objectContaining({ |
||||
|
netPerformance: -15.8, |
||||
|
netPerformanceInPercentage: -0.05528341497550734703, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: -0.05528341497550734703, |
||||
|
netPerformanceWithCurrencyEffect: -15.8, |
||||
|
totalInvestment: 0, |
||||
|
totalInvestmentValueWithCurrencyEffect: 0 |
||||
|
}) |
||||
|
); |
||||
|
|
||||
|
expect(investments).toEqual([ |
||||
|
{ date: '2021-11-22', investment: new Big('285.8') }, |
||||
|
{ date: '2021-11-30', investment: new Big('0') } |
||||
|
]); |
||||
|
|
||||
|
expect(investmentsByMonth).toEqual([ |
||||
|
{ date: '2021-11-01', investment: 0 }, |
||||
|
{ date: '2021-12-01', investment: 0 } |
||||
|
]); |
||||
|
|
||||
|
expect(investmentsByYear).toEqual([ |
||||
|
{ date: '2021-01-01', investment: 0 } |
||||
|
]); |
||||
|
|
||||
|
expect(performanceByDateRange).toMatchObject({ |
||||
|
'1d': { |
||||
|
date: '2021-12-18', |
||||
|
netPerformance: 0, |
||||
|
netPerformanceInPercentage: 0, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: 0, |
||||
|
netPerformanceWithCurrencyEffect: 0, |
||||
|
totalInvestmentValueWithCurrencyEffect: 0, |
||||
|
valueWithCurrencyEffect: 0 |
||||
|
}, |
||||
|
max: { |
||||
|
date: '2021-12-18', |
||||
|
netPerformance: -15.8, |
||||
|
netPerformanceInPercentage: -0.05528341497550735, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: -0.05528341497550735, |
||||
|
netPerformanceWithCurrencyEffect: -15.8, |
||||
|
totalInvestmentValueWithCurrencyEffect: 0, |
||||
|
valueWithCurrencyEffect: 0 |
||||
|
}, |
||||
|
ytd: { |
||||
|
date: '2021-12-18', |
||||
|
netPerformance: -15.8, |
||||
|
netPerformanceInPercentage: -0.05528341497550735, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: -0.05528341497550735, |
||||
|
netPerformanceWithCurrencyEffect: -15.8, |
||||
|
totalInvestmentValueWithCurrencyEffect: 0, |
||||
|
valueWithCurrencyEffect: 0 |
||||
|
} |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,276 @@ |
|||||
|
import { |
||||
|
loadActivitiesFromExportFile, |
||||
|
userDummyData |
||||
|
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
||||
|
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
||||
|
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
||||
|
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
||||
|
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
||||
|
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
||||
|
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
||||
|
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
||||
|
import { parseDate } from '@ghostfolio/common/helper'; |
||||
|
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
||||
|
|
||||
|
import { Big } from 'big.js'; |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
||||
|
return { |
||||
|
CurrentRateService: jest.fn().mockImplementation(() => { |
||||
|
return CurrentRateServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
jest.mock( |
||||
|
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
||||
|
() => { |
||||
|
return { |
||||
|
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
||||
|
return PortfolioSnapshotServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
||||
|
return { |
||||
|
RedisCacheService: jest.fn().mockImplementation(() => { |
||||
|
return RedisCacheServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
describe('PortfolioCalculator (ROI)', () => { |
||||
|
let configurationService: ConfigurationService; |
||||
|
let currentRateService: CurrentRateService; |
||||
|
let exchangeRateDataService: ExchangeRateDataService; |
||||
|
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
||||
|
let portfolioSnapshotService: PortfolioSnapshotService; |
||||
|
let redisCacheService: RedisCacheService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
PortfolioSnapshotServiceMock.reset(); |
||||
|
RedisCacheServiceMock.reset(); |
||||
|
|
||||
|
configurationService = new ConfigurationService(); |
||||
|
|
||||
|
currentRateService = new CurrentRateService(null, null, null); |
||||
|
|
||||
|
exchangeRateDataService = new ExchangeRateDataService( |
||||
|
null, |
||||
|
null, |
||||
|
null, |
||||
|
null |
||||
|
); |
||||
|
|
||||
|
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
||||
|
|
||||
|
redisCacheService = new RedisCacheService(null, null); |
||||
|
|
||||
|
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
||||
|
configurationService, |
||||
|
currentRateService, |
||||
|
exchangeRateDataService, |
||||
|
portfolioSnapshotService, |
||||
|
redisCacheService |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
describe('get current positions', () => { |
||||
|
it.only('with BALN.SW buy', async () => { |
||||
|
jest.useFakeTimers().setSystemTime(parseDate('2021-12-18').getTime()); |
||||
|
|
||||
|
const { activities, userCurrency } = |
||||
|
loadActivitiesFromExportFile('baln-buy.json'); |
||||
|
|
||||
|
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
|
activities, |
||||
|
calculationType: PerformanceCalculationType.ROI, |
||||
|
currency: userCurrency, |
||||
|
userId: userDummyData.id |
||||
|
}); |
||||
|
|
||||
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
|
||||
|
const historicalDataDates = portfolioSnapshot.historicalData.map( |
||||
|
({ date }) => { |
||||
|
return date; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
const investments = portfolioCalculator.getInvestments(); |
||||
|
|
||||
|
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
||||
|
data: portfolioSnapshot.historicalData, |
||||
|
groupBy: 'month' |
||||
|
}); |
||||
|
|
||||
|
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
||||
|
data: portfolioSnapshot.historicalData, |
||||
|
groupBy: 'year' |
||||
|
}); |
||||
|
|
||||
|
expect(portfolioSnapshot).toMatchObject({ |
||||
|
currentValueInBaseCurrency: new Big('297.8'), |
||||
|
errors: [], |
||||
|
hasErrors: false, |
||||
|
positions: [ |
||||
|
{ |
||||
|
activitiesCount: 1, |
||||
|
averageInvestment: new Big('273.2'), |
||||
|
averageInvestmentWithCurrencyEffect: new Big('273.2'), |
||||
|
averagePrice: new Big('136.6'), |
||||
|
currency: 'CHF', |
||||
|
dataSource: 'YAHOO', |
||||
|
dateOfFirstActivity: '2021-11-30', |
||||
|
dividend: new Big('0'), |
||||
|
dividendInBaseCurrency: new Big('0'), |
||||
|
fee: new Big('1.55'), |
||||
|
feeInBaseCurrency: new Big('1.55'), |
||||
|
grossPerformance: new Big('24.6'), |
||||
|
grossPerformancePercentage: new Big('0.09004392386530014641'), |
||||
|
grossPerformancePercentageWithCurrencyEffect: new Big( |
||||
|
'0.09004392386530014641' |
||||
|
), |
||||
|
grossPerformanceWithCurrencyEffect: new Big('24.6'), |
||||
|
investment: new Big('273.2'), |
||||
|
investmentWithCurrencyEffect: new Big('273.2'), |
||||
|
netPerformance: new Big('23.05'), |
||||
|
netPerformancePercentage: new Big('0.08437042459736456808'), |
||||
|
netPerformancePercentageWithCurrencyEffectMap: { |
||||
|
max: new Big('0.08437042459736456808') |
||||
|
}, |
||||
|
netPerformanceWithCurrencyEffectMap: { |
||||
|
'1d': new Big('10.00'), // 2 * (148.9 - 143.9) -> no fees in this time period
|
||||
|
'1y': new Big('23.05'), // 2 * (148.9 - 136.6) - 1.55
|
||||
|
'5y': new Big('23.05'), // 2 * (148.9 - 136.6) - 1.55
|
||||
|
max: new Big('23.05'), // 2 * (148.9 - 136.6) - 1.55
|
||||
|
mtd: new Big('24.60'), // 2 * (148.9 - 136.6) -> no fees in this time period
|
||||
|
wtd: new Big('13.80'), // 2 * (148.9 - 142.0) -> no fees in this time period
|
||||
|
ytd: new Big('23.05') // 2 * (148.9 - 136.6) - 1.55
|
||||
|
}, |
||||
|
marketPrice: 148.9, |
||||
|
marketPriceInBaseCurrency: 148.9, |
||||
|
quantity: new Big('2'), |
||||
|
symbol: 'BALN.SW', |
||||
|
tags: [], |
||||
|
valueInBaseCurrency: new Big('297.8') |
||||
|
} |
||||
|
], |
||||
|
totalFeesWithCurrencyEffect: new Big('1.55'), |
||||
|
totalInterestWithCurrencyEffect: new Big('0'), |
||||
|
totalInvestment: new Big('273.2'), |
||||
|
totalInvestmentWithCurrencyEffect: new Big('273.2'), |
||||
|
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, |
||||
|
netPerformanceWithCurrencyEffect: 23.05, |
||||
|
totalInvestment: 273.2, |
||||
|
totalInvestmentValueWithCurrencyEffect: 273.2 |
||||
|
}) |
||||
|
); |
||||
|
|
||||
|
expect(investments).toEqual([ |
||||
|
{ date: '2021-11-30', investment: new Big('273.2') } |
||||
|
]); |
||||
|
|
||||
|
expect(investmentsByMonth).toEqual([ |
||||
|
{ date: '2021-11-01', investment: 273.2 }, |
||||
|
{ date: '2021-12-01', investment: 0 } |
||||
|
]); |
||||
|
|
||||
|
expect(investmentsByYear).toEqual([ |
||||
|
{ date: '2021-01-01', investment: 273.2 } |
||||
|
]); |
||||
|
}); |
||||
|
|
||||
|
it.only('with BALN.SW buy (with unit price lower than closing price)', async () => { |
||||
|
jest.useFakeTimers().setSystemTime(parseDate('2021-12-18').getTime()); |
||||
|
|
||||
|
const { activities, userCurrency } = loadActivitiesFromExportFile( |
||||
|
'baln-buy-with-unit-price-lower-than-closing-price.json' |
||||
|
); |
||||
|
|
||||
|
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
|
activities, |
||||
|
calculationType: PerformanceCalculationType.ROI, |
||||
|
currency: userCurrency, |
||||
|
userId: userDummyData.id |
||||
|
}); |
||||
|
|
||||
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
const snapshotOnBuyDate = portfolioSnapshot.historicalData.find( |
||||
|
({ date }) => { |
||||
|
return date === '2021-11-30'; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
// Closing price on 2021-11-30: 136.6
|
||||
|
expect(snapshotOnBuyDate?.netPerformanceWithCurrencyEffect).toEqual(1.65); // 2 * (136.6 - 135.0) - 1.55 = 1.65
|
||||
|
}); |
||||
|
|
||||
|
it.only('with BALN.SW buy (with unit price lower than closing price), calculated on buy date', async () => { |
||||
|
jest.useFakeTimers().setSystemTime(parseDate('2021-11-30').getTime()); |
||||
|
|
||||
|
const { activities, userCurrency } = loadActivitiesFromExportFile( |
||||
|
'baln-buy-with-unit-price-lower-than-closing-price.json' |
||||
|
); |
||||
|
|
||||
|
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
|
activities, |
||||
|
calculationType: PerformanceCalculationType.ROI, |
||||
|
currency: userCurrency, |
||||
|
userId: userDummyData.id |
||||
|
}); |
||||
|
|
||||
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
const snapshotOnBuyDate = portfolioSnapshot.historicalData.find( |
||||
|
({ date }) => { |
||||
|
return date === '2021-11-30'; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
// Closing price on 2021-11-30: 136.6
|
||||
|
expect(snapshotOnBuyDate?.netPerformanceWithCurrencyEffect).toEqual(1.65); // 2 * (136.6 - 135.0) - 1.55 = 1.65
|
||||
|
}); |
||||
|
|
||||
|
it.only('with BALN.SW buy (on a date without historical market data)', async () => { |
||||
|
jest.useFakeTimers().setSystemTime(parseDate('2021-12-18').getTime()); |
||||
|
|
||||
|
const { activities, userCurrency } = |
||||
|
loadActivitiesFromExportFile('baln-buy.json'); |
||||
|
|
||||
|
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
|
activities, |
||||
|
calculationType: PerformanceCalculationType.ROI, |
||||
|
currency: userCurrency, |
||||
|
userId: userDummyData.id |
||||
|
}); |
||||
|
|
||||
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
const snapshotOnDateWithoutMarketData = |
||||
|
portfolioSnapshot.historicalData.find(({ date }) => { |
||||
|
return date === '2021-12-05'; |
||||
|
}); |
||||
|
|
||||
|
// No historical market data on 2021-12-05: carry forward the closing
|
||||
|
// price of 2021-11-30, which is the date of the activity
|
||||
|
expect(snapshotOnDateWithoutMarketData?.value).toEqual(273.2); // 2 * 136.6
|
||||
|
expect( |
||||
|
snapshotOnDateWithoutMarketData?.netPerformanceWithCurrencyEffect |
||||
|
).toEqual(-1.55); // 2 * (136.6 - 136.6) - 1.55
|
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,282 @@ |
|||||
|
import { |
||||
|
getPerformanceByDateRange, |
||||
|
loadActivitiesFromExportFile, |
||||
|
userDummyData |
||||
|
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
||||
|
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
||||
|
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
||||
|
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
||||
|
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
||||
|
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
||||
|
import { ExchangeRateDataServiceMock } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service.mock'; |
||||
|
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
||||
|
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
||||
|
import { parseDate } from '@ghostfolio/common/helper'; |
||||
|
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
||||
|
|
||||
|
import { Big } from 'big.js'; |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
||||
|
return { |
||||
|
CurrentRateService: jest.fn().mockImplementation(() => { |
||||
|
return CurrentRateServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
jest.mock( |
||||
|
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
||||
|
() => { |
||||
|
return { |
||||
|
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
||||
|
return PortfolioSnapshotServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
||||
|
return { |
||||
|
RedisCacheService: jest.fn().mockImplementation(() => { |
||||
|
return RedisCacheServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
jest.mock( |
||||
|
'@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service', |
||||
|
() => { |
||||
|
return { |
||||
|
ExchangeRateDataService: jest.fn().mockImplementation(() => { |
||||
|
return ExchangeRateDataServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
describe('PortfolioCalculator (ROI)', () => { |
||||
|
let configurationService: ConfigurationService; |
||||
|
let currentRateService: CurrentRateService; |
||||
|
let exchangeRateDataService: ExchangeRateDataService; |
||||
|
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
||||
|
let portfolioSnapshotService: PortfolioSnapshotService; |
||||
|
let redisCacheService: RedisCacheService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
PortfolioSnapshotServiceMock.reset(); |
||||
|
RedisCacheServiceMock.reset(); |
||||
|
|
||||
|
configurationService = new ConfigurationService(); |
||||
|
|
||||
|
currentRateService = new CurrentRateService(null, null, null); |
||||
|
|
||||
|
exchangeRateDataService = new ExchangeRateDataService( |
||||
|
null, |
||||
|
null, |
||||
|
null, |
||||
|
null |
||||
|
); |
||||
|
|
||||
|
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
||||
|
|
||||
|
redisCacheService = new RedisCacheService(null, null); |
||||
|
|
||||
|
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
||||
|
configurationService, |
||||
|
currentRateService, |
||||
|
exchangeRateDataService, |
||||
|
portfolioSnapshotService, |
||||
|
redisCacheService |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
describe('get current positions', () => { |
||||
|
it.only('with BTCUSD buy and sell partially', async () => { |
||||
|
jest.useFakeTimers().setSystemTime(parseDate('2018-01-01').getTime()); |
||||
|
|
||||
|
const { activities, userCurrency } = loadActivitiesFromExportFile( |
||||
|
'btcusd-buy-and-sell-partially.json' |
||||
|
); |
||||
|
|
||||
|
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
|
activities, |
||||
|
calculationType: PerformanceCalculationType.ROI, |
||||
|
currency: userCurrency, |
||||
|
usePortfolioSnapshotCache: false, |
||||
|
userId: userDummyData.id |
||||
|
}); |
||||
|
|
||||
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
|
||||
|
const performanceByDateRange = await getPerformanceByDateRange({ |
||||
|
portfolioCalculator, |
||||
|
dateRanges: ['1d', 'max', 'ytd'] |
||||
|
}); |
||||
|
|
||||
|
const investments = portfolioCalculator.getInvestments(); |
||||
|
|
||||
|
const investmentsByMonth = portfolioCalculator.getInvestmentsByGroup({ |
||||
|
data: portfolioSnapshot.historicalData, |
||||
|
groupBy: 'month' |
||||
|
}); |
||||
|
|
||||
|
const investmentsByYear = portfolioCalculator.getInvestmentsByGroup({ |
||||
|
data: portfolioSnapshot.historicalData, |
||||
|
groupBy: 'year' |
||||
|
}); |
||||
|
|
||||
|
expect(portfolioSnapshot).toMatchObject({ |
||||
|
currentValueInBaseCurrency: new Big('13298.425356'), |
||||
|
errors: [], |
||||
|
hasErrors: false, |
||||
|
positions: [ |
||||
|
{ |
||||
|
activitiesCount: 2, |
||||
|
averageInvestment: new Big('624.0246078'), |
||||
|
averageInvestmentWithCurrencyEffect: new Big( |
||||
|
'637.085334599999935914' |
||||
|
), |
||||
|
averagePrice: new Big('320.43'), |
||||
|
currency: 'USD', |
||||
|
dataSource: 'YAHOO', |
||||
|
dateOfFirstActivity: '2015-01-01', |
||||
|
dividend: new Big('0'), |
||||
|
dividendInBaseCurrency: new Big('0'), |
||||
|
fee: new Big('0'), |
||||
|
feeInBaseCurrency: new Big('0'), |
||||
|
grossPerformance: new Big('27172.74').mul(0.97373), |
||||
|
grossPerformancePercentage: new Big('42.40043067128546016291'), |
||||
|
grossPerformancePercentageWithCurrencyEffect: new Big( |
||||
|
'41.62112555619955206456' |
||||
|
), |
||||
|
grossPerformanceWithCurrencyEffect: new Big( |
||||
|
'26516.208701400000064086' |
||||
|
), |
||||
|
investment: new Big('320.43').mul(0.97373), |
||||
|
investmentWithCurrencyEffect: new Big('318.542667299999967957'), |
||||
|
marketPrice: 13657.2, |
||||
|
marketPriceInBaseCurrency: 13298.425356, |
||||
|
netPerformance: new Big('27172.74').mul(0.97373), |
||||
|
netPerformancePercentage: new Big('42.40043067128546016291'), |
||||
|
netPerformancePercentageWithCurrencyEffectMap: { |
||||
|
'1d': new Big('-0.04016229506406263535'), |
||||
|
max: new Big('41.62112555619955206456'), |
||||
|
ytd: new Big('-0.04016229506406263535') |
||||
|
}, |
||||
|
netPerformanceWithCurrencyEffectMap: { |
||||
|
'1d': new Big('-556.443324'), |
||||
|
max: new Big('26516.208701400000064086'), |
||||
|
ytd: new Big('-556.443324') |
||||
|
}, |
||||
|
quantity: new Big('1'), |
||||
|
symbol: 'BTCUSD', |
||||
|
tags: [], |
||||
|
valueInBaseCurrency: new Big('13298.425356') |
||||
|
} |
||||
|
], |
||||
|
totalFeesWithCurrencyEffect: new Big('0'), |
||||
|
totalInterestWithCurrencyEffect: new Big('0'), |
||||
|
totalInvestment: new Big('320.43').mul(0.97373), |
||||
|
totalInvestmentWithCurrencyEffect: new Big('318.542667299999967957'), |
||||
|
totalLiabilitiesWithCurrencyEffect: new Big('0') |
||||
|
}); |
||||
|
|
||||
|
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
||||
|
expect.objectContaining({ |
||||
|
netPerformance: new Big('27172.74').mul(0.97373).toNumber(), |
||||
|
netPerformanceInPercentage: 42.40043067128546, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: 41.62112555619955, |
||||
|
netPerformanceWithCurrencyEffect: 26516.2087014, |
||||
|
totalInvestment: 312.0123039, |
||||
|
totalInvestmentValueWithCurrencyEffect: 318.54266729999995 |
||||
|
}) |
||||
|
); |
||||
|
|
||||
|
expect(investments).toEqual([ |
||||
|
{ date: '2015-01-01', investment: new Big('640.86') }, |
||||
|
{ date: '2017-12-31', investment: new Big('320.43') } |
||||
|
]); |
||||
|
|
||||
|
expect(investmentsByMonth).toEqual([ |
||||
|
{ date: '2014-12-01', investment: 0 }, |
||||
|
{ date: '2015-01-01', investment: 637.0853345999999 }, |
||||
|
{ date: '2015-02-01', investment: 0 }, |
||||
|
{ date: '2015-03-01', investment: 0 }, |
||||
|
{ date: '2015-04-01', investment: 0 }, |
||||
|
{ date: '2015-05-01', investment: 0 }, |
||||
|
{ date: '2015-06-01', investment: 0 }, |
||||
|
{ date: '2015-07-01', investment: 0 }, |
||||
|
{ date: '2015-08-01', investment: 0 }, |
||||
|
{ date: '2015-09-01', investment: 0 }, |
||||
|
{ date: '2015-10-01', investment: 0 }, |
||||
|
{ date: '2015-11-01', investment: 0 }, |
||||
|
{ date: '2015-12-01', investment: 0 }, |
||||
|
{ date: '2016-01-01', investment: 0 }, |
||||
|
{ date: '2016-02-01', investment: 0 }, |
||||
|
{ date: '2016-03-01', investment: 0 }, |
||||
|
{ date: '2016-04-01', investment: 0 }, |
||||
|
{ date: '2016-05-01', investment: 0 }, |
||||
|
{ date: '2016-06-01', investment: 0 }, |
||||
|
{ date: '2016-07-01', investment: 0 }, |
||||
|
{ date: '2016-08-01', investment: 0 }, |
||||
|
{ date: '2016-09-01', investment: 0 }, |
||||
|
{ date: '2016-10-01', investment: 0 }, |
||||
|
{ date: '2016-11-01', investment: 0 }, |
||||
|
{ date: '2016-12-01', investment: 0 }, |
||||
|
{ date: '2017-01-01', investment: 0 }, |
||||
|
{ date: '2017-02-01', investment: 0 }, |
||||
|
{ date: '2017-03-01', investment: 0 }, |
||||
|
{ date: '2017-04-01', investment: 0 }, |
||||
|
{ date: '2017-05-01', investment: 0 }, |
||||
|
{ date: '2017-06-01', investment: 0 }, |
||||
|
{ date: '2017-07-01', investment: 0 }, |
||||
|
{ date: '2017-08-01', investment: 0 }, |
||||
|
{ date: '2017-09-01', investment: 0 }, |
||||
|
{ date: '2017-10-01', investment: 0 }, |
||||
|
{ date: '2017-11-01', investment: 0 }, |
||||
|
{ date: '2017-12-01', investment: -318.54266729999995 }, |
||||
|
{ date: '2018-01-01', investment: 0 } |
||||
|
]); |
||||
|
|
||||
|
expect(investmentsByYear).toEqual([ |
||||
|
{ date: '2014-01-01', investment: 0 }, |
||||
|
{ date: '2015-01-01', investment: 637.0853345999999 }, |
||||
|
{ date: '2016-01-01', investment: 0 }, |
||||
|
{ date: '2017-01-01', investment: -318.54266729999995 }, |
||||
|
{ date: '2018-01-01', investment: 0 } |
||||
|
]); |
||||
|
|
||||
|
expect(performanceByDateRange).toMatchObject({ |
||||
|
'1d': { |
||||
|
date: '2018-01-01', |
||||
|
netPerformance: -486.0860160000011, |
||||
|
netPerformanceInPercentage: -0.035263202509112565, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: -0.040162295064062624, |
||||
|
netPerformanceWithCurrencyEffect: -556.4433239999998, |
||||
|
totalInvestmentValueWithCurrencyEffect: 318.54266729999995, |
||||
|
valueWithCurrencyEffect: 13298.425356 |
||||
|
}, |
||||
|
max: { |
||||
|
date: '2018-01-01', |
||||
|
netPerformance: 26458.9121202, |
||||
|
netPerformanceInPercentage: 42.400430671285456, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: 41.62112555619955, |
||||
|
netPerformanceWithCurrencyEffect: 26516.2087014, |
||||
|
totalInvestmentValueWithCurrencyEffect: 318.54266729999995, |
||||
|
valueWithCurrencyEffect: 13298.425356 |
||||
|
}, |
||||
|
ytd: { |
||||
|
date: '2018-01-01', |
||||
|
netPerformance: -486.0860160000011, |
||||
|
netPerformanceInPercentage: -0.035263202509112565, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: -0.040162295064062624, |
||||
|
netPerformanceWithCurrencyEffect: -556.4433239999998, |
||||
|
totalInvestmentValueWithCurrencyEffect: 318.54266729999995, |
||||
|
valueWithCurrencyEffect: 13298.425356 |
||||
|
} |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,202 @@ |
|||||
|
import { |
||||
|
getPerformanceByDateRange, |
||||
|
loadActivitiesFromExportFile, |
||||
|
userDummyData |
||||
|
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; |
||||
|
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; |
||||
|
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service'; |
||||
|
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock'; |
||||
|
import { RedisCacheService } from '@ghostfolio/api/app/redis-cache/redis-cache.service'; |
||||
|
import { RedisCacheServiceMock } from '@ghostfolio/api/app/redis-cache/redis-cache.service.mock'; |
||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; |
||||
|
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; |
||||
|
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock'; |
||||
|
import { parseDate } from '@ghostfolio/common/helper'; |
||||
|
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type'; |
||||
|
|
||||
|
import { Big } from 'big.js'; |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => { |
||||
|
return { |
||||
|
CurrentRateService: jest.fn().mockImplementation(() => { |
||||
|
return CurrentRateServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
jest.mock( |
||||
|
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service', |
||||
|
() => { |
||||
|
return { |
||||
|
PortfolioSnapshotService: jest.fn().mockImplementation(() => { |
||||
|
return PortfolioSnapshotServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
} |
||||
|
); |
||||
|
|
||||
|
jest.mock('@ghostfolio/api/app/redis-cache/redis-cache.service', () => { |
||||
|
return { |
||||
|
RedisCacheService: jest.fn().mockImplementation(() => { |
||||
|
return RedisCacheServiceMock; |
||||
|
}) |
||||
|
}; |
||||
|
}); |
||||
|
|
||||
|
describe('PortfolioCalculator (ROI)', () => { |
||||
|
let configurationService: ConfigurationService; |
||||
|
let currentRateService: CurrentRateService; |
||||
|
let exchangeRateDataService: ExchangeRateDataService; |
||||
|
let portfolioCalculatorFactory: PortfolioCalculatorFactory; |
||||
|
let portfolioSnapshotService: PortfolioSnapshotService; |
||||
|
let redisCacheService: RedisCacheService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
PortfolioSnapshotServiceMock.reset(); |
||||
|
RedisCacheServiceMock.reset(); |
||||
|
|
||||
|
configurationService = new ConfigurationService(); |
||||
|
|
||||
|
currentRateService = new CurrentRateService(null, null, null); |
||||
|
|
||||
|
exchangeRateDataService = new ExchangeRateDataService( |
||||
|
null, |
||||
|
null, |
||||
|
null, |
||||
|
null |
||||
|
); |
||||
|
|
||||
|
portfolioSnapshotService = new PortfolioSnapshotService(null, null); |
||||
|
|
||||
|
redisCacheService = new RedisCacheService(null, null); |
||||
|
|
||||
|
portfolioCalculatorFactory = new PortfolioCalculatorFactory( |
||||
|
configurationService, |
||||
|
currentRateService, |
||||
|
exchangeRateDataService, |
||||
|
portfolioSnapshotService, |
||||
|
redisCacheService |
||||
|
); |
||||
|
}); |
||||
|
|
||||
|
describe('get current positions', () => { |
||||
|
it.only('with MSFT buy', async () => { |
||||
|
jest.useFakeTimers().setSystemTime(parseDate('2023-07-10').getTime()); |
||||
|
|
||||
|
const { activities, userCurrency } = loadActivitiesFromExportFile( |
||||
|
'msft-buy-with-dividend.json' |
||||
|
); |
||||
|
|
||||
|
const portfolioCalculator = portfolioCalculatorFactory.createCalculator({ |
||||
|
activities, |
||||
|
calculationType: PerformanceCalculationType.ROI, |
||||
|
currency: userCurrency, |
||||
|
usePortfolioSnapshotCache: false, |
||||
|
userId: userDummyData.id |
||||
|
}); |
||||
|
|
||||
|
const portfolioSnapshot = await portfolioCalculator.computeSnapshot(); |
||||
|
|
||||
|
const performanceByDateRange = await getPerformanceByDateRange({ |
||||
|
portfolioCalculator, |
||||
|
dateRanges: ['1d', 'max', 'ytd'] |
||||
|
}); |
||||
|
|
||||
|
expect(portfolioSnapshot).toMatchObject({ |
||||
|
dividendYieldPercent: new Big('0.001144362748184'), |
||||
|
dividendYieldPercentWithCurrencyEffect: new Big('0.001144362748184'), |
||||
|
errors: [], |
||||
|
hasErrors: false, |
||||
|
positions: [ |
||||
|
{ |
||||
|
activitiesCount: 2, |
||||
|
averagePrice: new Big('298.58'), |
||||
|
currency: 'USD', |
||||
|
dataSource: 'YAHOO', |
||||
|
dateOfFirstActivity: '2021-09-16', |
||||
|
dividend: new Big('0.62'), |
||||
|
dividendInBaseCurrency: new Big('0.62'), |
||||
|
dividendYieldPercent: new Big('0.001144362748184'), |
||||
|
dividendYieldPercentWithCurrencyEffect: new Big( |
||||
|
'0.001144362748184' |
||||
|
), |
||||
|
fee: new Big('19'), |
||||
|
grossPerformance: new Big('33.87'), |
||||
|
grossPerformancePercentage: new Big('0.11343693482483756447'), |
||||
|
grossPerformancePercentageWithCurrencyEffect: new Big( |
||||
|
'0.11343693482483756447' |
||||
|
), |
||||
|
grossPerformanceWithCurrencyEffect: new Big('33.87'), |
||||
|
investment: new Big('298.58'), |
||||
|
investmentWithCurrencyEffect: new Big('298.58'), |
||||
|
marketPrice: 331.83, |
||||
|
marketPriceInBaseCurrency: 331.83, |
||||
|
netPerformance: new Big('14.87'), |
||||
|
netPerformancePercentage: new Big('0.04980239801728180052'), |
||||
|
netPerformancePercentageWithCurrencyEffectMap: { |
||||
|
max: new Big('0.04980239801728180052') |
||||
|
}, |
||||
|
netPerformanceWithCurrencyEffectMap: { |
||||
|
'1d': new Big('-5.39'), |
||||
|
'5y': new Big('14.87'), |
||||
|
max: new Big('14.87'), |
||||
|
wtd: new Big('-5.39') |
||||
|
}, |
||||
|
quantity: new Big('1'), |
||||
|
symbol: 'MSFT', |
||||
|
tags: [] |
||||
|
} |
||||
|
], |
||||
|
totalFeesWithCurrencyEffect: new Big('19'), |
||||
|
totalInterestWithCurrencyEffect: new Big('0'), |
||||
|
totalInvestment: new Big('298.58'), |
||||
|
totalInvestmentWithCurrencyEffect: new Big('298.58'), |
||||
|
totalLiabilitiesWithCurrencyEffect: new Big('0') |
||||
|
}); |
||||
|
|
||||
|
expect(portfolioSnapshot.historicalData.at(-1)).toMatchObject( |
||||
|
expect.objectContaining({ |
||||
|
totalInvestment: 298.58, |
||||
|
totalInvestmentValueWithCurrencyEffect: 298.58 |
||||
|
}) |
||||
|
); |
||||
|
|
||||
|
expect(performanceByDateRange).toMatchObject({ |
||||
|
'1d': { |
||||
|
date: '2023-07-10', |
||||
|
dividendInBaseCurrency: 0, |
||||
|
dividendInPercentageWithCurrencyEffect: 0, |
||||
|
netPerformance: -5.390000000000002, |
||||
|
netPerformanceInPercentage: -0.015983630864124316, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: -0.015983630864124316, |
||||
|
netPerformanceWithCurrencyEffect: -5.390000000000002, |
||||
|
totalInvestmentValueWithCurrencyEffect: 298.58, |
||||
|
valueWithCurrencyEffect: 331.83 |
||||
|
}, |
||||
|
max: { |
||||
|
date: '2023-07-10', |
||||
|
dividendInBaseCurrency: 0.62, |
||||
|
dividendInPercentageWithCurrencyEffect: 0.0020764954116149776, |
||||
|
netPerformance: 14.87, |
||||
|
netPerformanceInPercentage: 0.0498023980172818, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: 0.0498023980172818, |
||||
|
netPerformanceWithCurrencyEffect: 14.87, |
||||
|
totalInvestmentValueWithCurrencyEffect: 298.58, |
||||
|
valueWithCurrencyEffect: 331.83 |
||||
|
}, |
||||
|
ytd: { |
||||
|
date: '2023-07-10', |
||||
|
dividendInBaseCurrency: 0, |
||||
|
dividendInPercentageWithCurrencyEffect: 0, |
||||
|
netPerformance: -7.6800000000000015, |
||||
|
netPerformanceInPercentage: -0.022620835910577012, |
||||
|
netPerformanceInPercentageWithCurrencyEffect: -0.022620835910577012, |
||||
|
netPerformanceWithCurrencyEffect: -7.6800000000000015, |
||||
|
totalInvestmentValueWithCurrencyEffect: 298.58, |
||||
|
valueWithCurrencyEffect: 331.83 |
||||
|
} |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
Loading…
Reference in new issue