fix single buy test

pull/239/head
Valentin Zickner 3 years ago committed by Thomas
parent 948c45c602
commit 136563c949

@ -626,7 +626,7 @@ describe('PortfolioCalculator', () => {
.spyOn(Date, 'now') .spyOn(Date, 'now')
.mockImplementation(() => new Date(Date.UTC(2021, 6, 26)).getTime()); // 2021-07-26 .mockImplementation(() => new Date(Date.UTC(2021, 6, 26)).getTime()); // 2021-07-26
const currentPositions = await portfolioCalculator.getCurrentPositions( const currentPositions = await portfolioCalculator.getCurrentPositions(
parse('2021-07-26', 'yyyy-MM-dd', new Date()) parse('2020-01-21', 'yyyy-MM-dd', new Date())
); );
spy.mockRestore(); spy.mockRestore();
@ -638,7 +638,7 @@ describe('PortfolioCalculator', () => {
currency: 'USD', currency: 'USD',
firstBuyDate: '2021-01-01', firstBuyDate: '2021-01-01',
grossPerformance: new Big('-61.84'), // 657.62-719.46=-61.84 grossPerformance: new Big('-61.84'), // 657.62-719.46=-61.84
grossPerformancePercentage: new Big('-0.0859533539043171'), // (657.62-719.46)/719.46=-0.085953353904317 grossPerformancePercentage: new Big('-0.08595335390431712673'), // (657.62-719.46)/719.46=-0.085953353904317
investment: new Big('719.46'), investment: new Big('719.46'),
marketPrice: 657.62, marketPrice: 657.62,
name: 'Tesla, Inc.', name: 'Tesla, Inc.',
@ -673,9 +673,9 @@ describe('PortfolioCalculator', () => {
currency: 'USD', currency: 'USD',
firstBuyDate: '2019-02-01', firstBuyDate: '2019-02-01',
// see next test for details about how to calculate this // see next test for details about how to calculate this
grossPerformance: new Big('265.2'), grossPerformance: new Big('240.4'),
grossPerformancePercentage: new Big( grossPerformancePercentage: new Big(
'0.37322057787174066244232522865731355471028555367747465860626740684417274277219590953836818016777856' '0.349632913145865078264579821060810370805662039085569533288730749607797361322474717934042420125015808'
), ),
investment: new Big('4460.95'), investment: new Big('4460.95'),
marketPrice: 194.86, marketPrice: 194.86,

@ -212,7 +212,11 @@ export class PortfolioCalculator {
); );
continue; continue;
} }
if (!marketSymbolMap[currentDate]?.[item.symbol]) { let currentValue = marketSymbolMap[currentDate]?.[item.symbol];
if (!isAfter(parseDate(currentDate), parseDate(item.firstBuyDate))) {
currentValue = item.investment.div(item.quantity);
}
if (!currentValue) {
invalidSymbols.push(item.symbol); invalidSymbols.push(item.symbol);
hasErrors = true; hasErrors = true;
console.error( console.error(
@ -221,9 +225,7 @@ export class PortfolioCalculator {
continue; continue;
} }
holdingPeriodReturns[item.symbol] = oldHoldingPeriodReturn.mul( holdingPeriodReturns[item.symbol] = oldHoldingPeriodReturn.mul(
marketSymbolMap[nextDate][item.symbol].div( marketSymbolMap[nextDate][item.symbol].div(currentValue)
marketSymbolMap[currentDate][item.symbol]
)
); );
let oldGrossPerformance = grossPerformance[item.symbol]; let oldGrossPerformance = grossPerformance[item.symbol];
if (!oldGrossPerformance) { if (!oldGrossPerformance) {
@ -231,7 +233,7 @@ export class PortfolioCalculator {
} }
grossPerformance[item.symbol] = oldGrossPerformance.plus( grossPerformance[item.symbol] = oldGrossPerformance.plus(
marketSymbolMap[nextDate][item.symbol] marketSymbolMap[nextDate][item.symbol]
.minus(marketSymbolMap[currentDate][item.symbol]) .minus(currentValue)
.mul(item.quantity) .mul(item.quantity)
); );
} }

Loading…
Cancel
Save